blob: dfca5121de53e60cbbc5b987005f9c78dabc83e2 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Server-side XDR for NFSv4
3 *
4 * Copyright (c) 2002 The Regents of the University of Michigan.
5 * All rights reserved.
6 *
7 * Kendrick Smith <kmsmith@umich.edu>
8 * Andy Adamson <andros@umich.edu>
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 *
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
24 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
30 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *
35 * TODO: Neil Brown made the following observation: We currently
36 * initially reserve NFSD_BUFSIZE space on the transmit queue and
37 * never release any of that until the request is complete.
38 * It would be good to calculate a new maximum response size while
39 * decoding the COMPOUND, and call svc_reserve with this number
40 * at the end of nfs4svc_decode_compoundargs.
41 */
42
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090043#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/namei.h>
Boaz Harrosh341eb182009-12-03 20:29:12 +020045#include <linux/statfs.h>
Andy Adamson0733d212009-04-03 08:28:01 +030046#include <linux/utsname.h>
Bryan Schumaker17456802011-07-13 10:50:48 -040047#include <linux/pagemap.h>
J. Bruce Fields4796f452007-07-17 04:04:51 -070048#include <linux/sunrpc/svcauth_gss.h>
Boaz Harrosh9a74af22009-12-03 20:30:56 +020049
J. Bruce Fields2ca72e12011-01-04 17:37:15 -050050#include "idmap.h"
51#include "acl.h"
Boaz Harrosh9a74af22009-12-03 20:30:56 +020052#include "xdr4.h"
J. Bruce Fields0a3adad2009-11-04 18:12:35 -050053#include "vfs.h"
Bryan Schumaker17456802011-07-13 10:50:48 -040054#include "state.h"
J. Bruce Fields10910062011-01-24 12:11:02 -050055#include "cache.h"
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +030056#include "netns.h"
J. Bruce Fields2ca72e12011-01-04 17:37:15 -050057
David Quigley18032ca2013-05-02 13:19:10 -040058#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
59#include <linux/security.h>
60#endif
61
62
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#define NFSDDBG_FACILITY NFSDDBG_XDR
64
J.Bruce Fields42ca0992006-10-04 02:16:20 -070065/*
66 * As per referral draft, the fsid for a referral MUST be different from the fsid of the containing
67 * directory in order to indicate to the client that a filesystem boundary is present
68 * We use a fixed fsid for a referral
69 */
70#define NFS4_REFERRAL_FSID_MAJOR 0x8000000ULL
71#define NFS4_REFERRAL_FSID_MINOR 0x8000000ULL
72
Al Virob37ad282006-10-19 23:28:59 -070073static __be32
J. Bruce Fieldsa36b1722012-11-25 16:31:00 -050074check_filename(char *str, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075{
76 int i;
77
78 if (len == 0)
79 return nfserr_inval;
80 if (isdotent(str, len))
J. Bruce Fieldsa36b1722012-11-25 16:31:00 -050081 return nfserr_badname;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 for (i = 0; i < len; i++)
83 if (str[i] == '/')
J. Bruce Fieldsa36b1722012-11-25 16:31:00 -050084 return nfserr_badname;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 return 0;
86}
87
Linus Torvalds1da177e2005-04-16 15:20:36 -070088#define DECODE_HEAD \
Al Viro2ebbc012006-10-19 23:28:58 -070089 __be32 *p; \
Al Virob37ad282006-10-19 23:28:59 -070090 __be32 status
Linus Torvalds1da177e2005-04-16 15:20:36 -070091#define DECODE_TAIL \
92 status = 0; \
93out: \
94 return status; \
95xdr_error: \
Chuck Lever817cb9d2007-09-11 18:01:20 -040096 dprintk("NFSD: xdr error (%s:%d)\n", \
97 __FILE__, __LINE__); \
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 status = nfserr_bad_xdr; \
99 goto out
100
101#define READ32(x) (x) = ntohl(*p++)
102#define READ64(x) do { \
103 (x) = (u64)ntohl(*p++) << 32; \
104 (x) |= ntohl(*p++); \
105} while (0)
106#define READTIME(x) do { \
107 p++; \
108 (x) = ntohl(*p++); \
109 p++; \
110} while (0)
111#define READMEM(x,nbytes) do { \
112 x = (char *)p; \
113 p += XDR_QUADLEN(nbytes); \
114} while (0)
115#define SAVEMEM(x,nbytes) do { \
116 if (!(x = (p==argp->tmp || p == argp->tmpp) ? \
117 savemem(argp, p, nbytes) : \
118 (char *)p)) { \
Chuck Lever817cb9d2007-09-11 18:01:20 -0400119 dprintk("NFSD: xdr error (%s:%d)\n", \
120 __FILE__, __LINE__); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 goto xdr_error; \
122 } \
123 p += XDR_QUADLEN(nbytes); \
124} while (0)
125#define COPYMEM(x,nbytes) do { \
126 memcpy((x), p, nbytes); \
127 p += XDR_QUADLEN(nbytes); \
128} while (0)
129
130/* READ_BUF, read_buf(): nbytes must be <= PAGE_SIZE */
131#define READ_BUF(nbytes) do { \
132 if (nbytes <= (u32)((char *)argp->end - (char *)argp->p)) { \
133 p = argp->p; \
134 argp->p += XDR_QUADLEN(nbytes); \
135 } else if (!(p = read_buf(argp, nbytes))) { \
Chuck Lever817cb9d2007-09-11 18:01:20 -0400136 dprintk("NFSD: xdr error (%s:%d)\n", \
137 __FILE__, __LINE__); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 goto xdr_error; \
139 } \
140} while (0)
141
J. Bruce Fieldsca2a05a2007-11-11 15:43:12 -0500142static __be32 *read_buf(struct nfsd4_compoundargs *argp, u32 nbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
144 /* We want more bytes than seem to be available.
145 * Maybe we need a new page, maybe we have just run out
146 */
J. Bruce Fieldsca2a05a2007-11-11 15:43:12 -0500147 unsigned int avail = (char *)argp->end - (char *)argp->p;
Al Viro2ebbc012006-10-19 23:28:58 -0700148 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 if (avail + argp->pagelen < nbytes)
150 return NULL;
151 if (avail + PAGE_SIZE < nbytes) /* need more than a page !! */
152 return NULL;
153 /* ok, we can do it with the current plus the next page */
154 if (nbytes <= sizeof(argp->tmp))
155 p = argp->tmp;
156 else {
Jesper Juhlf99d49a2005-11-07 01:01:34 -0800157 kfree(argp->tmpp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 p = argp->tmpp = kmalloc(nbytes, GFP_KERNEL);
159 if (!p)
160 return NULL;
161
162 }
J. Bruce Fieldsca2a05a2007-11-11 15:43:12 -0500163 /*
164 * The following memcpy is safe because read_buf is always
165 * called with nbytes > avail, and the two cases above both
166 * guarantee p points to at least nbytes bytes.
167 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 memcpy(p, argp->p, avail);
169 /* step to next page */
170 argp->p = page_address(argp->pagelist[0]);
171 argp->pagelist++;
172 if (argp->pagelen < PAGE_SIZE) {
Neil Brown2bc3c112010-04-20 12:16:52 +1000173 argp->end = argp->p + (argp->pagelen>>2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 argp->pagelen = 0;
175 } else {
Neil Brown2bc3c112010-04-20 12:16:52 +1000176 argp->end = argp->p + (PAGE_SIZE>>2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 argp->pagelen -= PAGE_SIZE;
178 }
179 memcpy(((char*)p)+avail, argp->p, (nbytes - avail));
180 argp->p += XDR_QUADLEN(nbytes - avail);
181 return p;
182}
183
Andy Adamson60adfc52009-04-03 08:28:50 +0300184static int zero_clientid(clientid_t *clid)
185{
186 return (clid->cl_boot == 0) && (clid->cl_id == 0);
187}
188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189static int
190defer_free(struct nfsd4_compoundargs *argp,
191 void (*release)(const void *), void *p)
192{
193 struct tmpbuf *tb;
194
195 tb = kmalloc(sizeof(*tb), GFP_KERNEL);
196 if (!tb)
197 return -ENOMEM;
198 tb->buf = p;
199 tb->release = release;
200 tb->next = argp->to_free;
201 argp->to_free = tb;
202 return 0;
203}
204
Al Viro2ebbc012006-10-19 23:28:58 -0700205static char *savemem(struct nfsd4_compoundargs *argp, __be32 *p, int nbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 if (p == argp->tmp) {
Thomas Meyer67114fe2011-11-17 23:43:40 +0100208 p = kmemdup(argp->tmp, nbytes, GFP_KERNEL);
J. Bruce Fieldsa4db5fe2007-02-16 01:28:30 -0800209 if (!p)
210 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 } else {
Eric Sesterhenn73dff8b2006-10-03 23:37:14 +0200212 BUG_ON(p != argp->tmpp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 argp->tmpp = NULL;
214 }
215 if (defer_free(argp, kfree, p)) {
J. Bruce Fieldsa4db5fe2007-02-16 01:28:30 -0800216 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 return NULL;
218 } else
219 return (char *)p;
220}
221
Al Virob37ad282006-10-19 23:28:59 -0700222static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223nfsd4_decode_bitmap(struct nfsd4_compoundargs *argp, u32 *bmval)
224{
225 u32 bmlen;
226 DECODE_HEAD;
227
228 bmval[0] = 0;
229 bmval[1] = 0;
Andy Adamson7e705702009-04-03 08:29:11 +0300230 bmval[2] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
232 READ_BUF(4);
233 READ32(bmlen);
234 if (bmlen > 1000)
235 goto xdr_error;
236
237 READ_BUF(bmlen << 2);
238 if (bmlen > 0)
239 READ32(bmval[0]);
240 if (bmlen > 1)
241 READ32(bmval[1]);
Andy Adamson7e705702009-04-03 08:29:11 +0300242 if (bmlen > 2)
243 READ32(bmval[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
245 DECODE_TAIL;
246}
247
Al Virob37ad282006-10-19 23:28:59 -0700248static __be32
Yu Zhiguo3c8e0312009-05-16 16:22:31 +0800249nfsd4_decode_fattr(struct nfsd4_compoundargs *argp, u32 *bmval,
David Quigley18032ca2013-05-02 13:19:10 -0400250 struct iattr *iattr, struct nfs4_acl **acl,
251 struct xdr_netobj *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252{
253 int expected_len, len = 0;
254 u32 dummy32;
255 char *buf;
Al Virob8dd7b92006-10-19 23:29:01 -0700256 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
258 DECODE_HEAD;
259 iattr->ia_valid = 0;
260 if ((status = nfsd4_decode_bitmap(argp, bmval)))
261 return status;
262
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 READ_BUF(4);
264 READ32(expected_len);
265
266 if (bmval[0] & FATTR4_WORD0_SIZE) {
267 READ_BUF(8);
268 len += 8;
269 READ64(iattr->ia_size);
270 iattr->ia_valid |= ATTR_SIZE;
271 }
272 if (bmval[0] & FATTR4_WORD0_ACL) {
J. Bruce Fields64a817c2013-03-26 14:11:13 -0400273 u32 nace;
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800274 struct nfs4_ace *ace;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
276 READ_BUF(4); len += 4;
277 READ32(nace);
278
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800279 if (nace > NFS4_ACL_MAX)
280 return nfserr_resource;
281
282 *acl = nfs4_acl_new(nace);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 if (*acl == NULL) {
Al Virob8dd7b92006-10-19 23:29:01 -0700284 host_err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 goto out_nfserr;
286 }
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800287 defer_free(argp, kfree, *acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800289 (*acl)->naces = nace;
290 for (ace = (*acl)->aces; ace < (*acl)->aces + nace; ace++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 READ_BUF(16); len += 16;
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800292 READ32(ace->type);
293 READ32(ace->flag);
294 READ32(ace->access_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 READ32(dummy32);
296 READ_BUF(dummy32);
297 len += XDR_QUADLEN(dummy32) << 2;
298 READMEM(buf, dummy32);
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800299 ace->whotype = nfs4_acl_get_whotype(buf, dummy32);
J. Bruce Fields3c726022011-01-04 17:53:52 -0500300 status = nfs_ok;
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800301 if (ace->whotype != NFS4_ACL_WHO_NAMED)
Eric W. Biedermanab8e4ae2013-02-02 05:18:08 -0800302 ;
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800303 else if (ace->flag & NFS4_ACE_IDENTIFIER_GROUP)
J. Bruce Fields3c726022011-01-04 17:53:52 -0500304 status = nfsd_map_name_to_gid(argp->rqstp,
Eric W. Biedermanab8e4ae2013-02-02 05:18:08 -0800305 buf, dummy32, &ace->who_gid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 else
J. Bruce Fields3c726022011-01-04 17:53:52 -0500307 status = nfsd_map_name_to_uid(argp->rqstp,
Eric W. Biedermanab8e4ae2013-02-02 05:18:08 -0800308 buf, dummy32, &ace->who_uid);
J. Bruce Fields3c726022011-01-04 17:53:52 -0500309 if (status)
310 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 }
312 } else
313 *acl = NULL;
314 if (bmval[1] & FATTR4_WORD1_MODE) {
315 READ_BUF(4);
316 len += 4;
317 READ32(iattr->ia_mode);
318 iattr->ia_mode &= (S_IFMT | S_IALLUGO);
319 iattr->ia_valid |= ATTR_MODE;
320 }
321 if (bmval[1] & FATTR4_WORD1_OWNER) {
322 READ_BUF(4);
323 len += 4;
324 READ32(dummy32);
325 READ_BUF(dummy32);
326 len += (XDR_QUADLEN(dummy32) << 2);
327 READMEM(buf, dummy32);
NeilBrown47c85292011-02-16 13:08:35 +1100328 if ((status = nfsd_map_name_to_uid(argp->rqstp, buf, dummy32, &iattr->ia_uid)))
329 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 iattr->ia_valid |= ATTR_UID;
331 }
332 if (bmval[1] & FATTR4_WORD1_OWNER_GROUP) {
333 READ_BUF(4);
334 len += 4;
335 READ32(dummy32);
336 READ_BUF(dummy32);
337 len += (XDR_QUADLEN(dummy32) << 2);
338 READMEM(buf, dummy32);
NeilBrown47c85292011-02-16 13:08:35 +1100339 if ((status = nfsd_map_name_to_gid(argp->rqstp, buf, dummy32, &iattr->ia_gid)))
340 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 iattr->ia_valid |= ATTR_GID;
342 }
343 if (bmval[1] & FATTR4_WORD1_TIME_ACCESS_SET) {
344 READ_BUF(4);
345 len += 4;
346 READ32(dummy32);
347 switch (dummy32) {
348 case NFS4_SET_TO_CLIENT_TIME:
349 /* We require the high 32 bits of 'seconds' to be 0, and we ignore
350 all 32 bits of 'nseconds'. */
351 READ_BUF(12);
352 len += 12;
Bryan Schumakerbf8d9092013-04-19 16:09:38 -0400353 READ64(iattr->ia_atime.tv_sec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 READ32(iattr->ia_atime.tv_nsec);
355 if (iattr->ia_atime.tv_nsec >= (u32)1000000000)
356 return nfserr_inval;
357 iattr->ia_valid |= (ATTR_ATIME | ATTR_ATIME_SET);
358 break;
359 case NFS4_SET_TO_SERVER_TIME:
360 iattr->ia_valid |= ATTR_ATIME;
361 break;
362 default:
363 goto xdr_error;
364 }
365 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 if (bmval[1] & FATTR4_WORD1_TIME_MODIFY_SET) {
367 READ_BUF(4);
368 len += 4;
369 READ32(dummy32);
370 switch (dummy32) {
371 case NFS4_SET_TO_CLIENT_TIME:
372 /* We require the high 32 bits of 'seconds' to be 0, and we ignore
373 all 32 bits of 'nseconds'. */
374 READ_BUF(12);
375 len += 12;
Bryan Schumakerbf8d9092013-04-19 16:09:38 -0400376 READ64(iattr->ia_mtime.tv_sec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 READ32(iattr->ia_mtime.tv_nsec);
378 if (iattr->ia_mtime.tv_nsec >= (u32)1000000000)
379 return nfserr_inval;
380 iattr->ia_valid |= (ATTR_MTIME | ATTR_MTIME_SET);
381 break;
382 case NFS4_SET_TO_SERVER_TIME:
383 iattr->ia_valid |= ATTR_MTIME;
384 break;
385 default:
386 goto xdr_error;
387 }
388 }
David Quigley18032ca2013-05-02 13:19:10 -0400389
390 label->len = 0;
391#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
392 if (bmval[2] & FATTR4_WORD2_SECURITY_LABEL) {
393 READ_BUF(4);
394 len += 4;
395 READ32(dummy32); /* lfs: we don't use it */
396 READ_BUF(4);
397 len += 4;
398 READ32(dummy32); /* pi: we don't use it either */
399 READ_BUF(4);
400 len += 4;
401 READ32(dummy32);
402 READ_BUF(dummy32);
403 if (dummy32 > NFSD4_MAX_SEC_LABEL_LEN)
404 return nfserr_badlabel;
405 len += (XDR_QUADLEN(dummy32) << 2);
406 READMEM(buf, dummy32);
407 label->data = kzalloc(dummy32 + 1, GFP_KERNEL);
408 if (!label->data)
409 return nfserr_jukebox;
410 defer_free(argp, kfree, label->data);
411 memcpy(label->data, buf, dummy32);
412 }
413#endif
414
Yu Zhiguo3c8e0312009-05-16 16:22:31 +0800415 if (bmval[0] & ~NFSD_WRITEABLE_ATTRS_WORD0
416 || bmval[1] & ~NFSD_WRITEABLE_ATTRS_WORD1
417 || bmval[2] & ~NFSD_WRITEABLE_ATTRS_WORD2)
418 READ_BUF(expected_len - len);
419 else if (len != expected_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 goto xdr_error;
421
422 DECODE_TAIL;
423
424out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -0700425 status = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 goto out;
427}
428
Al Virob37ad282006-10-19 23:28:59 -0700429static __be32
Benny Halevye31a1b62008-08-12 20:46:18 +0300430nfsd4_decode_stateid(struct nfsd4_compoundargs *argp, stateid_t *sid)
431{
432 DECODE_HEAD;
433
434 READ_BUF(sizeof(stateid_t));
435 READ32(sid->si_generation);
436 COPYMEM(&sid->si_opaque, sizeof(stateid_opaque_t));
437
438 DECODE_TAIL;
439}
440
441static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442nfsd4_decode_access(struct nfsd4_compoundargs *argp, struct nfsd4_access *access)
443{
444 DECODE_HEAD;
445
446 READ_BUF(4);
447 READ32(access->ac_req_access);
448
449 DECODE_TAIL;
450}
451
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400452static __be32 nfsd4_decode_cb_sec(struct nfsd4_compoundargs *argp, struct nfsd4_cb_sec *cbs)
453{
454 DECODE_HEAD;
J. Bruce Fields12fc3e92012-11-05 16:01:48 -0500455 u32 dummy, uid, gid;
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400456 char *machine_name;
457 int i;
458 int nr_secflavs;
459
460 /* callback_sec_params4 */
461 READ_BUF(4);
462 READ32(nr_secflavs);
J. Bruce Fields12fc3e92012-11-05 16:01:48 -0500463 cbs->flavor = (u32)(-1);
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400464 for (i = 0; i < nr_secflavs; ++i) {
465 READ_BUF(4);
466 READ32(dummy);
467 switch (dummy) {
468 case RPC_AUTH_NULL:
469 /* Nothing to read */
J. Bruce Fields12fc3e92012-11-05 16:01:48 -0500470 if (cbs->flavor == (u32)(-1))
471 cbs->flavor = RPC_AUTH_NULL;
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400472 break;
473 case RPC_AUTH_UNIX:
474 READ_BUF(8);
475 /* stamp */
476 READ32(dummy);
477
478 /* machine name */
479 READ32(dummy);
480 READ_BUF(dummy);
481 SAVEMEM(machine_name, dummy);
482
483 /* uid, gid */
484 READ_BUF(8);
J. Bruce Fields12fc3e92012-11-05 16:01:48 -0500485 READ32(uid);
486 READ32(gid);
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400487
488 /* more gids */
489 READ_BUF(4);
490 READ32(dummy);
491 READ_BUF(dummy * 4);
J. Bruce Fields12fc3e92012-11-05 16:01:48 -0500492 if (cbs->flavor == (u32)(-1)) {
Eric W. Biederman03bc6d12013-02-02 06:24:49 -0800493 kuid_t kuid = make_kuid(&init_user_ns, uid);
494 kgid_t kgid = make_kgid(&init_user_ns, gid);
495 if (uid_valid(kuid) && gid_valid(kgid)) {
496 cbs->uid = kuid;
497 cbs->gid = kgid;
498 cbs->flavor = RPC_AUTH_UNIX;
499 } else {
500 dprintk("RPC_AUTH_UNIX with invalid"
501 "uid or gid ignoring!\n");
502 }
J. Bruce Fields12fc3e92012-11-05 16:01:48 -0500503 }
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400504 break;
505 case RPC_AUTH_GSS:
506 dprintk("RPC_AUTH_GSS callback secflavor "
507 "not supported!\n");
508 READ_BUF(8);
509 /* gcbp_service */
510 READ32(dummy);
511 /* gcbp_handle_from_server */
512 READ32(dummy);
513 READ_BUF(dummy);
514 p += XDR_QUADLEN(dummy);
515 /* gcbp_handle_from_client */
516 READ_BUF(4);
517 READ32(dummy);
518 READ_BUF(dummy);
519 break;
520 default:
521 dprintk("Illegal callback secflavor\n");
522 return nfserr_inval;
523 }
524 }
525 DECODE_TAIL;
526}
527
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -0400528static __be32 nfsd4_decode_backchannel_ctl(struct nfsd4_compoundargs *argp, struct nfsd4_backchannel_ctl *bc)
529{
530 DECODE_HEAD;
531
532 READ_BUF(4);
533 READ32(bc->bc_cb_program);
534 nfsd4_decode_cb_sec(argp, &bc->bc_cb_sec);
535
536 DECODE_TAIL;
537}
538
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -0400539static __be32 nfsd4_decode_bind_conn_to_session(struct nfsd4_compoundargs *argp, struct nfsd4_bind_conn_to_session *bcts)
540{
541 DECODE_HEAD;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -0400542
543 READ_BUF(NFS4_MAX_SESSIONID_LEN + 8);
544 COPYMEM(bcts->sessionid.data, NFS4_MAX_SESSIONID_LEN);
545 READ32(bcts->dir);
Bryan Schumaker6ce23572011-04-27 15:47:16 -0400546 /* XXX: skipping ctsa_use_conn_in_rdma_mode. Perhaps Tom Tucker
547 * could help us figure out we should be using it. */
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -0400548 DECODE_TAIL;
549}
550
Al Virob37ad282006-10-19 23:28:59 -0700551static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552nfsd4_decode_close(struct nfsd4_compoundargs *argp, struct nfsd4_close *close)
553{
554 DECODE_HEAD;
555
Benny Halevye31a1b62008-08-12 20:46:18 +0300556 READ_BUF(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 READ32(close->cl_seqid);
Benny Halevye31a1b62008-08-12 20:46:18 +0300558 return nfsd4_decode_stateid(argp, &close->cl_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
560 DECODE_TAIL;
561}
562
563
Al Virob37ad282006-10-19 23:28:59 -0700564static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565nfsd4_decode_commit(struct nfsd4_compoundargs *argp, struct nfsd4_commit *commit)
566{
567 DECODE_HEAD;
568
569 READ_BUF(12);
570 READ64(commit->co_offset);
571 READ32(commit->co_count);
572
573 DECODE_TAIL;
574}
575
Al Virob37ad282006-10-19 23:28:59 -0700576static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577nfsd4_decode_create(struct nfsd4_compoundargs *argp, struct nfsd4_create *create)
578{
579 DECODE_HEAD;
580
581 READ_BUF(4);
582 READ32(create->cr_type);
583 switch (create->cr_type) {
584 case NF4LNK:
585 READ_BUF(4);
586 READ32(create->cr_linklen);
587 READ_BUF(create->cr_linklen);
588 SAVEMEM(create->cr_linkname, create->cr_linklen);
589 break;
590 case NF4BLK:
591 case NF4CHR:
592 READ_BUF(8);
593 READ32(create->cr_specdata1);
594 READ32(create->cr_specdata2);
595 break;
596 case NF4SOCK:
597 case NF4FIFO:
598 case NF4DIR:
599 default:
600 break;
601 }
602
603 READ_BUF(4);
604 READ32(create->cr_namelen);
605 READ_BUF(create->cr_namelen);
606 SAVEMEM(create->cr_name, create->cr_namelen);
J. Bruce Fieldsa36b1722012-11-25 16:31:00 -0500607 if ((status = check_filename(create->cr_name, create->cr_namelen)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 return status;
609
Yu Zhiguo3c8e0312009-05-16 16:22:31 +0800610 status = nfsd4_decode_fattr(argp, create->cr_bmval, &create->cr_iattr,
David Quigley18032ca2013-05-02 13:19:10 -0400611 &create->cr_acl, &create->cr_label);
Benny Halevyc0d6fc82009-04-03 08:29:05 +0300612 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 goto out;
614
615 DECODE_TAIL;
616}
617
Al Virob37ad282006-10-19 23:28:59 -0700618static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619nfsd4_decode_delegreturn(struct nfsd4_compoundargs *argp, struct nfsd4_delegreturn *dr)
620{
Benny Halevye31a1b62008-08-12 20:46:18 +0300621 return nfsd4_decode_stateid(argp, &dr->dr_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622}
623
Al Virob37ad282006-10-19 23:28:59 -0700624static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625nfsd4_decode_getattr(struct nfsd4_compoundargs *argp, struct nfsd4_getattr *getattr)
626{
627 return nfsd4_decode_bitmap(argp, getattr->ga_bmval);
628}
629
Al Virob37ad282006-10-19 23:28:59 -0700630static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631nfsd4_decode_link(struct nfsd4_compoundargs *argp, struct nfsd4_link *link)
632{
633 DECODE_HEAD;
634
635 READ_BUF(4);
636 READ32(link->li_namelen);
637 READ_BUF(link->li_namelen);
638 SAVEMEM(link->li_name, link->li_namelen);
J. Bruce Fieldsa36b1722012-11-25 16:31:00 -0500639 if ((status = check_filename(link->li_name, link->li_namelen)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 return status;
641
642 DECODE_TAIL;
643}
644
Al Virob37ad282006-10-19 23:28:59 -0700645static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646nfsd4_decode_lock(struct nfsd4_compoundargs *argp, struct nfsd4_lock *lock)
647{
648 DECODE_HEAD;
649
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 /*
651 * type, reclaim(boolean), offset, length, new_lock_owner(boolean)
652 */
653 READ_BUF(28);
654 READ32(lock->lk_type);
655 if ((lock->lk_type < NFS4_READ_LT) || (lock->lk_type > NFS4_WRITEW_LT))
656 goto xdr_error;
657 READ32(lock->lk_reclaim);
658 READ64(lock->lk_offset);
659 READ64(lock->lk_length);
660 READ32(lock->lk_is_new);
661
662 if (lock->lk_is_new) {
Benny Halevye31a1b62008-08-12 20:46:18 +0300663 READ_BUF(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 READ32(lock->lk_new_open_seqid);
Benny Halevye31a1b62008-08-12 20:46:18 +0300665 status = nfsd4_decode_stateid(argp, &lock->lk_new_open_stateid);
666 if (status)
667 return status;
668 READ_BUF(8 + sizeof(clientid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 READ32(lock->lk_new_lock_seqid);
670 COPYMEM(&lock->lk_new_clientid, sizeof(clientid_t));
671 READ32(lock->lk_new_owner.len);
672 READ_BUF(lock->lk_new_owner.len);
673 READMEM(lock->lk_new_owner.data, lock->lk_new_owner.len);
674 } else {
Benny Halevye31a1b62008-08-12 20:46:18 +0300675 status = nfsd4_decode_stateid(argp, &lock->lk_old_lock_stateid);
676 if (status)
677 return status;
678 READ_BUF(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 READ32(lock->lk_old_lock_seqid);
680 }
681
682 DECODE_TAIL;
683}
684
Al Virob37ad282006-10-19 23:28:59 -0700685static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686nfsd4_decode_lockt(struct nfsd4_compoundargs *argp, struct nfsd4_lockt *lockt)
687{
688 DECODE_HEAD;
689
690 READ_BUF(32);
691 READ32(lockt->lt_type);
692 if((lockt->lt_type < NFS4_READ_LT) || (lockt->lt_type > NFS4_WRITEW_LT))
693 goto xdr_error;
694 READ64(lockt->lt_offset);
695 READ64(lockt->lt_length);
696 COPYMEM(&lockt->lt_clientid, 8);
697 READ32(lockt->lt_owner.len);
698 READ_BUF(lockt->lt_owner.len);
699 READMEM(lockt->lt_owner.data, lockt->lt_owner.len);
700
701 DECODE_TAIL;
702}
703
Al Virob37ad282006-10-19 23:28:59 -0700704static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705nfsd4_decode_locku(struct nfsd4_compoundargs *argp, struct nfsd4_locku *locku)
706{
707 DECODE_HEAD;
708
Benny Halevye31a1b62008-08-12 20:46:18 +0300709 READ_BUF(8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 READ32(locku->lu_type);
711 if ((locku->lu_type < NFS4_READ_LT) || (locku->lu_type > NFS4_WRITEW_LT))
712 goto xdr_error;
713 READ32(locku->lu_seqid);
Benny Halevye31a1b62008-08-12 20:46:18 +0300714 status = nfsd4_decode_stateid(argp, &locku->lu_stateid);
715 if (status)
716 return status;
717 READ_BUF(16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 READ64(locku->lu_offset);
719 READ64(locku->lu_length);
720
721 DECODE_TAIL;
722}
723
Al Virob37ad282006-10-19 23:28:59 -0700724static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725nfsd4_decode_lookup(struct nfsd4_compoundargs *argp, struct nfsd4_lookup *lookup)
726{
727 DECODE_HEAD;
728
729 READ_BUF(4);
730 READ32(lookup->lo_len);
731 READ_BUF(lookup->lo_len);
732 SAVEMEM(lookup->lo_name, lookup->lo_len);
J. Bruce Fieldsa36b1722012-11-25 16:31:00 -0500733 if ((status = check_filename(lookup->lo_name, lookup->lo_len)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 return status;
735
736 DECODE_TAIL;
737}
738
Benny Halevy2c8bd7e2012-02-16 20:57:09 +0200739static __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 -0400740{
741 __be32 *p;
742 u32 w;
743
744 READ_BUF(4);
745 READ32(w);
Benny Halevy2c8bd7e2012-02-16 20:57:09 +0200746 *share_access = w & NFS4_SHARE_ACCESS_MASK;
747 *deleg_want = w & NFS4_SHARE_WANT_MASK;
748 if (deleg_when)
749 *deleg_when = w & NFS4_SHARE_WHEN_MASK;
750
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400751 switch (w & NFS4_SHARE_ACCESS_MASK) {
752 case NFS4_SHARE_ACCESS_READ:
753 case NFS4_SHARE_ACCESS_WRITE:
754 case NFS4_SHARE_ACCESS_BOTH:
755 break;
756 default:
757 return nfserr_bad_xdr;
758 }
Benny Halevyfc0d14f2011-10-27 20:43:01 +0200759 w &= ~NFS4_SHARE_ACCESS_MASK;
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400760 if (!w)
761 return nfs_ok;
762 if (!argp->minorversion)
763 return nfserr_bad_xdr;
764 switch (w & NFS4_SHARE_WANT_MASK) {
765 case NFS4_SHARE_WANT_NO_PREFERENCE:
766 case NFS4_SHARE_WANT_READ_DELEG:
767 case NFS4_SHARE_WANT_WRITE_DELEG:
768 case NFS4_SHARE_WANT_ANY_DELEG:
769 case NFS4_SHARE_WANT_NO_DELEG:
770 case NFS4_SHARE_WANT_CANCEL:
771 break;
772 default:
773 return nfserr_bad_xdr;
774 }
Benny Halevy92bac8c2011-10-19 19:13:29 -0700775 w &= ~NFS4_SHARE_WANT_MASK;
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400776 if (!w)
777 return nfs_ok;
Benny Halevy2c8bd7e2012-02-16 20:57:09 +0200778
779 if (!deleg_when) /* open_downgrade */
780 return nfserr_inval;
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400781 switch (w) {
782 case NFS4_SHARE_SIGNAL_DELEG_WHEN_RESRC_AVAIL:
783 case NFS4_SHARE_PUSH_DELEG_WHEN_UNCONTENDED:
Benny Halevyc668fc62011-10-19 19:13:13 -0700784 case (NFS4_SHARE_SIGNAL_DELEG_WHEN_RESRC_AVAIL |
785 NFS4_SHARE_PUSH_DELEG_WHEN_UNCONTENDED):
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400786 return nfs_ok;
787 }
788xdr_error:
789 return nfserr_bad_xdr;
790}
791
792static __be32 nfsd4_decode_share_deny(struct nfsd4_compoundargs *argp, u32 *x)
793{
794 __be32 *p;
795
796 READ_BUF(4);
797 READ32(*x);
798 /* Note: unlinke access bits, deny bits may be zero. */
Dan Carpenter01cd4af2011-10-17 10:41:17 +0300799 if (*x & ~NFS4_SHARE_DENY_BOTH)
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400800 return nfserr_bad_xdr;
801 return nfs_ok;
802xdr_error:
803 return nfserr_bad_xdr;
804}
805
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -0400806static __be32 nfsd4_decode_opaque(struct nfsd4_compoundargs *argp, struct xdr_netobj *o)
807{
808 __be32 *p;
809
810 READ_BUF(4);
811 READ32(o->len);
812
813 if (o->len == 0 || o->len > NFS4_OPAQUE_LIMIT)
814 return nfserr_bad_xdr;
815
816 READ_BUF(o->len);
817 SAVEMEM(o->data, o->len);
818 return nfs_ok;
819xdr_error:
820 return nfserr_bad_xdr;
821}
822
Al Virob37ad282006-10-19 23:28:59 -0700823static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824nfsd4_decode_open(struct nfsd4_compoundargs *argp, struct nfsd4_open *open)
825{
826 DECODE_HEAD;
Benny Halevy2c8bd7e2012-02-16 20:57:09 +0200827 u32 dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
829 memset(open->op_bmval, 0, sizeof(open->op_bmval));
830 open->op_iattr.ia_valid = 0;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400831 open->op_openowner = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
J. Bruce Fields9d313b12013-02-28 12:51:49 -0800833 open->op_xdr_error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 /* seqid, share_access, share_deny, clientid, ownerlen */
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400835 READ_BUF(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 READ32(open->op_seqid);
Benny Halevy2c8bd7e2012-02-16 20:57:09 +0200837 /* decode, yet ignore deleg_when until supported */
838 status = nfsd4_decode_share_access(argp, &open->op_share_access,
839 &open->op_deleg_want, &dummy);
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400840 if (status)
841 goto xdr_error;
842 status = nfsd4_decode_share_deny(argp, &open->op_share_deny);
843 if (status)
844 goto xdr_error;
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -0400845 READ_BUF(sizeof(clientid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 COPYMEM(&open->op_clientid, sizeof(clientid_t));
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -0400847 status = nfsd4_decode_opaque(argp, &open->op_owner);
848 if (status)
849 goto xdr_error;
850 READ_BUF(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 READ32(open->op_create);
852 switch (open->op_create) {
853 case NFS4_OPEN_NOCREATE:
854 break;
855 case NFS4_OPEN_CREATE:
856 READ_BUF(4);
857 READ32(open->op_createmode);
858 switch (open->op_createmode) {
859 case NFS4_CREATE_UNCHECKED:
860 case NFS4_CREATE_GUARDED:
Benny Halevyc0d6fc82009-04-03 08:29:05 +0300861 status = nfsd4_decode_fattr(argp, open->op_bmval,
David Quigley18032ca2013-05-02 13:19:10 -0400862 &open->op_iattr, &open->op_acl, &open->op_label);
Benny Halevyc0d6fc82009-04-03 08:29:05 +0300863 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 goto out;
865 break;
866 case NFS4_CREATE_EXCLUSIVE:
Chuck Leverab4684d2012-03-02 17:13:50 -0500867 READ_BUF(NFS4_VERIFIER_SIZE);
868 COPYMEM(open->op_verf.data, NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 break;
Benny Halevy79fb54a2009-04-03 08:29:17 +0300870 case NFS4_CREATE_EXCLUSIVE4_1:
871 if (argp->minorversion < 1)
872 goto xdr_error;
Chuck Leverab4684d2012-03-02 17:13:50 -0500873 READ_BUF(NFS4_VERIFIER_SIZE);
874 COPYMEM(open->op_verf.data, NFS4_VERIFIER_SIZE);
Benny Halevy79fb54a2009-04-03 08:29:17 +0300875 status = nfsd4_decode_fattr(argp, open->op_bmval,
David Quigley18032ca2013-05-02 13:19:10 -0400876 &open->op_iattr, &open->op_acl, &open->op_label);
Benny Halevy79fb54a2009-04-03 08:29:17 +0300877 if (status)
878 goto out;
879 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 default:
881 goto xdr_error;
882 }
883 break;
884 default:
885 goto xdr_error;
886 }
887
888 /* open_claim */
889 READ_BUF(4);
890 READ32(open->op_claim_type);
891 switch (open->op_claim_type) {
892 case NFS4_OPEN_CLAIM_NULL:
893 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
894 READ_BUF(4);
895 READ32(open->op_fname.len);
896 READ_BUF(open->op_fname.len);
897 SAVEMEM(open->op_fname.data, open->op_fname.len);
J. Bruce Fieldsa36b1722012-11-25 16:31:00 -0500898 if ((status = check_filename(open->op_fname.data, open->op_fname.len)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 return status;
900 break;
901 case NFS4_OPEN_CLAIM_PREVIOUS:
902 READ_BUF(4);
903 READ32(open->op_delegate_type);
904 break;
905 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
Benny Halevye31a1b62008-08-12 20:46:18 +0300906 status = nfsd4_decode_stateid(argp, &open->op_delegate_stateid);
907 if (status)
908 return status;
909 READ_BUF(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 READ32(open->op_fname.len);
911 READ_BUF(open->op_fname.len);
912 SAVEMEM(open->op_fname.data, open->op_fname.len);
J. Bruce Fieldsa36b1722012-11-25 16:31:00 -0500913 if ((status = check_filename(open->op_fname.data, open->op_fname.len)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 return status;
915 break;
J. Bruce Fields8b289b22011-10-19 11:52:12 -0400916 case NFS4_OPEN_CLAIM_FH:
917 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
918 if (argp->minorversion < 1)
919 goto xdr_error;
920 /* void */
921 break;
922 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
923 if (argp->minorversion < 1)
924 goto xdr_error;
925 status = nfsd4_decode_stateid(argp, &open->op_delegate_stateid);
926 if (status)
927 return status;
928 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 default:
930 goto xdr_error;
931 }
932
933 DECODE_TAIL;
934}
935
Al Virob37ad282006-10-19 23:28:59 -0700936static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937nfsd4_decode_open_confirm(struct nfsd4_compoundargs *argp, struct nfsd4_open_confirm *open_conf)
938{
939 DECODE_HEAD;
940
Benny Halevye31a1b62008-08-12 20:46:18 +0300941 status = nfsd4_decode_stateid(argp, &open_conf->oc_req_stateid);
942 if (status)
943 return status;
944 READ_BUF(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 READ32(open_conf->oc_seqid);
946
947 DECODE_TAIL;
948}
949
Al Virob37ad282006-10-19 23:28:59 -0700950static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951nfsd4_decode_open_downgrade(struct nfsd4_compoundargs *argp, struct nfsd4_open_downgrade *open_down)
952{
953 DECODE_HEAD;
954
Benny Halevye31a1b62008-08-12 20:46:18 +0300955 status = nfsd4_decode_stateid(argp, &open_down->od_stateid);
956 if (status)
957 return status;
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400958 READ_BUF(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 READ32(open_down->od_seqid);
Benny Halevy2c8bd7e2012-02-16 20:57:09 +0200960 status = nfsd4_decode_share_access(argp, &open_down->od_share_access,
961 &open_down->od_deleg_want, NULL);
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400962 if (status)
963 return status;
964 status = nfsd4_decode_share_deny(argp, &open_down->od_share_deny);
965 if (status)
966 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 DECODE_TAIL;
968}
969
Al Virob37ad282006-10-19 23:28:59 -0700970static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971nfsd4_decode_putfh(struct nfsd4_compoundargs *argp, struct nfsd4_putfh *putfh)
972{
973 DECODE_HEAD;
974
975 READ_BUF(4);
976 READ32(putfh->pf_fhlen);
977 if (putfh->pf_fhlen > NFS4_FHSIZE)
978 goto xdr_error;
979 READ_BUF(putfh->pf_fhlen);
980 SAVEMEM(putfh->pf_fhval, putfh->pf_fhlen);
981
982 DECODE_TAIL;
983}
984
Al Virob37ad282006-10-19 23:28:59 -0700985static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986nfsd4_decode_read(struct nfsd4_compoundargs *argp, struct nfsd4_read *read)
987{
988 DECODE_HEAD;
989
Benny Halevye31a1b62008-08-12 20:46:18 +0300990 status = nfsd4_decode_stateid(argp, &read->rd_stateid);
991 if (status)
992 return status;
993 READ_BUF(12);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 READ64(read->rd_offset);
995 READ32(read->rd_length);
996
997 DECODE_TAIL;
998}
999
Al Virob37ad282006-10-19 23:28:59 -07001000static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001nfsd4_decode_readdir(struct nfsd4_compoundargs *argp, struct nfsd4_readdir *readdir)
1002{
1003 DECODE_HEAD;
1004
1005 READ_BUF(24);
1006 READ64(readdir->rd_cookie);
1007 COPYMEM(readdir->rd_verf.data, sizeof(readdir->rd_verf.data));
1008 READ32(readdir->rd_dircount); /* just in case you needed a useless field... */
1009 READ32(readdir->rd_maxcount);
1010 if ((status = nfsd4_decode_bitmap(argp, readdir->rd_bmval)))
1011 goto out;
1012
1013 DECODE_TAIL;
1014}
1015
Al Virob37ad282006-10-19 23:28:59 -07001016static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017nfsd4_decode_remove(struct nfsd4_compoundargs *argp, struct nfsd4_remove *remove)
1018{
1019 DECODE_HEAD;
1020
1021 READ_BUF(4);
1022 READ32(remove->rm_namelen);
1023 READ_BUF(remove->rm_namelen);
1024 SAVEMEM(remove->rm_name, remove->rm_namelen);
J. Bruce Fieldsa36b1722012-11-25 16:31:00 -05001025 if ((status = check_filename(remove->rm_name, remove->rm_namelen)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 return status;
1027
1028 DECODE_TAIL;
1029}
1030
Al Virob37ad282006-10-19 23:28:59 -07001031static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032nfsd4_decode_rename(struct nfsd4_compoundargs *argp, struct nfsd4_rename *rename)
1033{
1034 DECODE_HEAD;
1035
1036 READ_BUF(4);
1037 READ32(rename->rn_snamelen);
1038 READ_BUF(rename->rn_snamelen + 4);
1039 SAVEMEM(rename->rn_sname, rename->rn_snamelen);
1040 READ32(rename->rn_tnamelen);
1041 READ_BUF(rename->rn_tnamelen);
1042 SAVEMEM(rename->rn_tname, rename->rn_tnamelen);
J. Bruce Fieldsa36b1722012-11-25 16:31:00 -05001043 if ((status = check_filename(rename->rn_sname, rename->rn_snamelen)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 return status;
J. Bruce Fieldsa36b1722012-11-25 16:31:00 -05001045 if ((status = check_filename(rename->rn_tname, rename->rn_tnamelen)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 return status;
1047
1048 DECODE_TAIL;
1049}
1050
Al Virob37ad282006-10-19 23:28:59 -07001051static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052nfsd4_decode_renew(struct nfsd4_compoundargs *argp, clientid_t *clientid)
1053{
1054 DECODE_HEAD;
1055
1056 READ_BUF(sizeof(clientid_t));
1057 COPYMEM(clientid, sizeof(clientid_t));
1058
1059 DECODE_TAIL;
1060}
1061
Al Virob37ad282006-10-19 23:28:59 -07001062static __be32
Andy Adamsondcb488a32007-07-17 04:04:51 -07001063nfsd4_decode_secinfo(struct nfsd4_compoundargs *argp,
1064 struct nfsd4_secinfo *secinfo)
1065{
1066 DECODE_HEAD;
1067
1068 READ_BUF(4);
1069 READ32(secinfo->si_namelen);
1070 READ_BUF(secinfo->si_namelen);
1071 SAVEMEM(secinfo->si_name, secinfo->si_namelen);
J. Bruce Fieldsa36b1722012-11-25 16:31:00 -05001072 status = check_filename(secinfo->si_name, secinfo->si_namelen);
Andy Adamsondcb488a32007-07-17 04:04:51 -07001073 if (status)
1074 return status;
1075 DECODE_TAIL;
1076}
1077
1078static __be32
J. Bruce Fields04f4ad12010-12-16 09:51:13 -05001079nfsd4_decode_secinfo_no_name(struct nfsd4_compoundargs *argp,
1080 struct nfsd4_secinfo_no_name *sin)
1081{
1082 DECODE_HEAD;
1083
1084 READ_BUF(4);
1085 READ32(sin->sin_style);
1086 DECODE_TAIL;
1087}
1088
1089static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090nfsd4_decode_setattr(struct nfsd4_compoundargs *argp, struct nfsd4_setattr *setattr)
1091{
Benny Halevye31a1b62008-08-12 20:46:18 +03001092 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
Benny Halevye31a1b62008-08-12 20:46:18 +03001094 status = nfsd4_decode_stateid(argp, &setattr->sa_stateid);
1095 if (status)
1096 return status;
Yu Zhiguo3c8e0312009-05-16 16:22:31 +08001097 return nfsd4_decode_fattr(argp, setattr->sa_bmval, &setattr->sa_iattr,
David Quigley18032ca2013-05-02 13:19:10 -04001098 &setattr->sa_acl, &setattr->sa_label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099}
1100
Al Virob37ad282006-10-19 23:28:59 -07001101static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102nfsd4_decode_setclientid(struct nfsd4_compoundargs *argp, struct nfsd4_setclientid *setclientid)
1103{
1104 DECODE_HEAD;
1105
Chuck Leverab4684d2012-03-02 17:13:50 -05001106 READ_BUF(NFS4_VERIFIER_SIZE);
1107 COPYMEM(setclientid->se_verf.data, NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04001109 status = nfsd4_decode_opaque(argp, &setclientid->se_name);
1110 if (status)
1111 return nfserr_bad_xdr;
1112 READ_BUF(8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 READ32(setclientid->se_callback_prog);
1114 READ32(setclientid->se_callback_netid_len);
1115
1116 READ_BUF(setclientid->se_callback_netid_len + 4);
1117 SAVEMEM(setclientid->se_callback_netid_val, setclientid->se_callback_netid_len);
1118 READ32(setclientid->se_callback_addr_len);
1119
1120 READ_BUF(setclientid->se_callback_addr_len + 4);
1121 SAVEMEM(setclientid->se_callback_addr_val, setclientid->se_callback_addr_len);
1122 READ32(setclientid->se_callback_ident);
1123
1124 DECODE_TAIL;
1125}
1126
Al Virob37ad282006-10-19 23:28:59 -07001127static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128nfsd4_decode_setclientid_confirm(struct nfsd4_compoundargs *argp, struct nfsd4_setclientid_confirm *scd_c)
1129{
1130 DECODE_HEAD;
1131
Chuck Leverab4684d2012-03-02 17:13:50 -05001132 READ_BUF(8 + NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 COPYMEM(&scd_c->sc_clientid, 8);
Chuck Leverab4684d2012-03-02 17:13:50 -05001134 COPYMEM(&scd_c->sc_confirm, NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
1136 DECODE_TAIL;
1137}
1138
1139/* Also used for NVERIFY */
Al Virob37ad282006-10-19 23:28:59 -07001140static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141nfsd4_decode_verify(struct nfsd4_compoundargs *argp, struct nfsd4_verify *verify)
1142{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 DECODE_HEAD;
1144
1145 if ((status = nfsd4_decode_bitmap(argp, verify->ve_bmval)))
1146 goto out;
1147
1148 /* For convenience's sake, we compare raw xdr'd attributes in
J. Bruce Fieldse5f95702012-11-30 17:24:18 -05001149 * nfsd4_proc_verify */
1150
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 READ_BUF(4);
1152 READ32(verify->ve_attrlen);
1153 READ_BUF(verify->ve_attrlen);
1154 SAVEMEM(verify->ve_attrval, verify->ve_attrlen);
1155
1156 DECODE_TAIL;
1157}
1158
Al Virob37ad282006-10-19 23:28:59 -07001159static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160nfsd4_decode_write(struct nfsd4_compoundargs *argp, struct nfsd4_write *write)
1161{
1162 int avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 int len;
1164 DECODE_HEAD;
1165
Benny Halevye31a1b62008-08-12 20:46:18 +03001166 status = nfsd4_decode_stateid(argp, &write->wr_stateid);
1167 if (status)
1168 return status;
1169 READ_BUF(16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 READ64(write->wr_offset);
1171 READ32(write->wr_stable_how);
1172 if (write->wr_stable_how > 2)
1173 goto xdr_error;
1174 READ32(write->wr_buflen);
1175
1176 /* Sorry .. no magic macros for this.. *
1177 * READ_BUF(write->wr_buflen);
1178 * SAVEMEM(write->wr_buf, write->wr_buflen);
1179 */
1180 avail = (char*)argp->end - (char*)argp->p;
1181 if (avail + argp->pagelen < write->wr_buflen) {
Chuck Lever817cb9d2007-09-11 18:01:20 -04001182 dprintk("NFSD: xdr error (%s:%d)\n",
1183 __FILE__, __LINE__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 goto xdr_error;
1185 }
J. Bruce Fields70cc7f72012-11-16 14:16:46 -05001186 write->wr_head.iov_base = p;
1187 write->wr_head.iov_len = avail;
J. Bruce Fields5a80a542012-11-16 10:01:30 -05001188 WARN_ON(avail != (XDR_QUADLEN(avail) << 2));
J. Bruce Fields70cc7f72012-11-16 14:16:46 -05001189 write->wr_pagelist = argp->pagelist;
J. Bruce Fields5a80a542012-11-16 10:01:30 -05001190
1191 len = XDR_QUADLEN(write->wr_buflen) << 2;
1192 if (len >= avail) {
1193 int pages;
1194
1195 len -= avail;
1196
1197 pages = len >> PAGE_SHIFT;
1198 argp->pagelist += pages;
1199 argp->pagelen -= pages * PAGE_SIZE;
1200 len -= pages * PAGE_SIZE;
1201
1202 argp->p = (__be32 *)page_address(argp->pagelist[0]);
1203 argp->end = argp->p + XDR_QUADLEN(PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 }
J. Bruce Fields5a80a542012-11-16 10:01:30 -05001205 argp->p += XDR_QUADLEN(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
1207 DECODE_TAIL;
1208}
1209
Al Virob37ad282006-10-19 23:28:59 -07001210static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211nfsd4_decode_release_lockowner(struct nfsd4_compoundargs *argp, struct nfsd4_release_lockowner *rlockowner)
1212{
1213 DECODE_HEAD;
1214
1215 READ_BUF(12);
1216 COPYMEM(&rlockowner->rl_clientid, sizeof(clientid_t));
1217 READ32(rlockowner->rl_owner.len);
1218 READ_BUF(rlockowner->rl_owner.len);
1219 READMEM(rlockowner->rl_owner.data, rlockowner->rl_owner.len);
1220
Andy Adamson60adfc52009-04-03 08:28:50 +03001221 if (argp->minorversion && !zero_clientid(&rlockowner->rl_clientid))
1222 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 DECODE_TAIL;
1224}
1225
Al Virob37ad282006-10-19 23:28:59 -07001226static __be32
Andy Adamson2db134e2009-04-03 08:27:55 +03001227nfsd4_decode_exchange_id(struct nfsd4_compoundargs *argp,
Andy Adamson0733d212009-04-03 08:28:01 +03001228 struct nfsd4_exchange_id *exid)
Andy Adamson2db134e2009-04-03 08:27:55 +03001229{
Mi Jinlong5afa0402010-11-09 09:39:23 +08001230 int dummy, tmp;
Andy Adamson0733d212009-04-03 08:28:01 +03001231 DECODE_HEAD;
1232
1233 READ_BUF(NFS4_VERIFIER_SIZE);
1234 COPYMEM(exid->verifier.data, NFS4_VERIFIER_SIZE);
1235
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04001236 status = nfsd4_decode_opaque(argp, &exid->clname);
1237 if (status)
1238 return nfserr_bad_xdr;
Andy Adamson0733d212009-04-03 08:28:01 +03001239
1240 READ_BUF(4);
1241 READ32(exid->flags);
1242
1243 /* Ignore state_protect4_a */
1244 READ_BUF(4);
1245 READ32(exid->spa_how);
1246 switch (exid->spa_how) {
1247 case SP4_NONE:
1248 break;
1249 case SP4_MACH_CRED:
1250 /* spo_must_enforce */
1251 READ_BUF(4);
1252 READ32(dummy);
1253 READ_BUF(dummy * 4);
1254 p += dummy;
1255
1256 /* spo_must_allow */
1257 READ_BUF(4);
1258 READ32(dummy);
1259 READ_BUF(dummy * 4);
1260 p += dummy;
1261 break;
1262 case SP4_SSV:
1263 /* ssp_ops */
1264 READ_BUF(4);
1265 READ32(dummy);
1266 READ_BUF(dummy * 4);
1267 p += dummy;
1268
1269 READ_BUF(4);
1270 READ32(dummy);
1271 READ_BUF(dummy * 4);
1272 p += dummy;
1273
1274 /* ssp_hash_algs<> */
1275 READ_BUF(4);
Mi Jinlong5afa0402010-11-09 09:39:23 +08001276 READ32(tmp);
1277 while (tmp--) {
1278 READ_BUF(4);
1279 READ32(dummy);
1280 READ_BUF(dummy);
1281 p += XDR_QUADLEN(dummy);
1282 }
Andy Adamson0733d212009-04-03 08:28:01 +03001283
1284 /* ssp_encr_algs<> */
1285 READ_BUF(4);
Mi Jinlong5afa0402010-11-09 09:39:23 +08001286 READ32(tmp);
1287 while (tmp--) {
1288 READ_BUF(4);
1289 READ32(dummy);
1290 READ_BUF(dummy);
1291 p += XDR_QUADLEN(dummy);
1292 }
Andy Adamson0733d212009-04-03 08:28:01 +03001293
1294 /* ssp_window and ssp_num_gss_handles */
1295 READ_BUF(8);
1296 READ32(dummy);
1297 READ32(dummy);
1298 break;
1299 default:
1300 goto xdr_error;
1301 }
1302
1303 /* Ignore Implementation ID */
1304 READ_BUF(4); /* nfs_impl_id4 array length */
1305 READ32(dummy);
1306
1307 if (dummy > 1)
1308 goto xdr_error;
1309
1310 if (dummy == 1) {
1311 /* nii_domain */
1312 READ_BUF(4);
1313 READ32(dummy);
1314 READ_BUF(dummy);
1315 p += XDR_QUADLEN(dummy);
1316
1317 /* nii_name */
1318 READ_BUF(4);
1319 READ32(dummy);
1320 READ_BUF(dummy);
1321 p += XDR_QUADLEN(dummy);
1322
1323 /* nii_date */
1324 READ_BUF(12);
1325 p += 3;
1326 }
1327 DECODE_TAIL;
Andy Adamson2db134e2009-04-03 08:27:55 +03001328}
1329
1330static __be32
1331nfsd4_decode_create_session(struct nfsd4_compoundargs *argp,
1332 struct nfsd4_create_session *sess)
1333{
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001334 DECODE_HEAD;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001335 u32 dummy;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001336
1337 READ_BUF(16);
1338 COPYMEM(&sess->clientid, 8);
1339 READ32(sess->seqid);
1340 READ32(sess->flags);
1341
1342 /* Fore channel attrs */
1343 READ_BUF(28);
1344 READ32(dummy); /* headerpadsz is always 0 */
1345 READ32(sess->fore_channel.maxreq_sz);
1346 READ32(sess->fore_channel.maxresp_sz);
1347 READ32(sess->fore_channel.maxresp_cached);
1348 READ32(sess->fore_channel.maxops);
1349 READ32(sess->fore_channel.maxreqs);
1350 READ32(sess->fore_channel.nr_rdma_attrs);
1351 if (sess->fore_channel.nr_rdma_attrs == 1) {
1352 READ_BUF(4);
1353 READ32(sess->fore_channel.rdma_attrs);
1354 } else if (sess->fore_channel.nr_rdma_attrs > 1) {
1355 dprintk("Too many fore channel attr bitmaps!\n");
1356 goto xdr_error;
1357 }
1358
1359 /* Back channel attrs */
1360 READ_BUF(28);
1361 READ32(dummy); /* headerpadsz is always 0 */
1362 READ32(sess->back_channel.maxreq_sz);
1363 READ32(sess->back_channel.maxresp_sz);
1364 READ32(sess->back_channel.maxresp_cached);
1365 READ32(sess->back_channel.maxops);
1366 READ32(sess->back_channel.maxreqs);
1367 READ32(sess->back_channel.nr_rdma_attrs);
1368 if (sess->back_channel.nr_rdma_attrs == 1) {
1369 READ_BUF(4);
1370 READ32(sess->back_channel.rdma_attrs);
1371 } else if (sess->back_channel.nr_rdma_attrs > 1) {
1372 dprintk("Too many back channel attr bitmaps!\n");
1373 goto xdr_error;
1374 }
1375
J. Bruce Fieldsacb28872012-03-27 14:50:26 -04001376 READ_BUF(4);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001377 READ32(sess->callback_prog);
J. Bruce Fieldsacb28872012-03-27 14:50:26 -04001378 nfsd4_decode_cb_sec(argp, &sess->cb_sec);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001379 DECODE_TAIL;
Andy Adamson2db134e2009-04-03 08:27:55 +03001380}
1381
1382static __be32
1383nfsd4_decode_destroy_session(struct nfsd4_compoundargs *argp,
1384 struct nfsd4_destroy_session *destroy_session)
1385{
Benny Halevye10e0cf2009-04-03 08:28:38 +03001386 DECODE_HEAD;
1387 READ_BUF(NFS4_MAX_SESSIONID_LEN);
1388 COPYMEM(destroy_session->sessionid.data, NFS4_MAX_SESSIONID_LEN);
1389
1390 DECODE_TAIL;
Andy Adamson2db134e2009-04-03 08:27:55 +03001391}
1392
1393static __be32
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04001394nfsd4_decode_free_stateid(struct nfsd4_compoundargs *argp,
1395 struct nfsd4_free_stateid *free_stateid)
1396{
1397 DECODE_HEAD;
1398
1399 READ_BUF(sizeof(stateid_t));
1400 READ32(free_stateid->fr_stateid.si_generation);
1401 COPYMEM(&free_stateid->fr_stateid.si_opaque, sizeof(stateid_opaque_t));
1402
1403 DECODE_TAIL;
1404}
1405
1406static __be32
Andy Adamson2db134e2009-04-03 08:27:55 +03001407nfsd4_decode_sequence(struct nfsd4_compoundargs *argp,
1408 struct nfsd4_sequence *seq)
1409{
Benny Halevyb85d4c02009-04-03 08:28:08 +03001410 DECODE_HEAD;
1411
1412 READ_BUF(NFS4_MAX_SESSIONID_LEN + 16);
1413 COPYMEM(seq->sessionid.data, NFS4_MAX_SESSIONID_LEN);
1414 READ32(seq->seqid);
1415 READ32(seq->slotid);
1416 READ32(seq->maxslots);
1417 READ32(seq->cachethis);
1418
1419 DECODE_TAIL;
Andy Adamson2db134e2009-04-03 08:27:55 +03001420}
1421
Bryan Schumaker17456802011-07-13 10:50:48 -04001422static __be32
1423nfsd4_decode_test_stateid(struct nfsd4_compoundargs *argp, struct nfsd4_test_stateid *test_stateid)
1424{
Bryan Schumaker17456802011-07-13 10:50:48 -04001425 int i;
Bryan Schumaker03cfb422012-01-27 10:22:49 -05001426 __be32 *p, status;
1427 struct nfsd4_test_stateid_id *stateid;
Bryan Schumaker17456802011-07-13 10:50:48 -04001428
1429 READ_BUF(4);
1430 test_stateid->ts_num_ids = ntohl(*p++);
1431
Bryan Schumaker03cfb422012-01-27 10:22:49 -05001432 INIT_LIST_HEAD(&test_stateid->ts_stateid_list);
Bryan Schumaker17456802011-07-13 10:50:48 -04001433
1434 for (i = 0; i < test_stateid->ts_num_ids; i++) {
Bryan Schumaker03cfb422012-01-27 10:22:49 -05001435 stateid = kmalloc(sizeof(struct nfsd4_test_stateid_id), GFP_KERNEL);
1436 if (!stateid) {
Al Viroafcf6792012-04-13 00:15:37 -04001437 status = nfserrno(-ENOMEM);
Bryan Schumaker03cfb422012-01-27 10:22:49 -05001438 goto out;
1439 }
1440
1441 defer_free(argp, kfree, stateid);
1442 INIT_LIST_HEAD(&stateid->ts_id_list);
1443 list_add_tail(&stateid->ts_id_list, &test_stateid->ts_stateid_list);
1444
1445 status = nfsd4_decode_stateid(argp, &stateid->ts_id_stateid);
Bryan Schumaker17456802011-07-13 10:50:48 -04001446 if (status)
Bryan Schumaker03cfb422012-01-27 10:22:49 -05001447 goto out;
Bryan Schumaker17456802011-07-13 10:50:48 -04001448 }
1449
1450 status = 0;
1451out:
1452 return status;
1453xdr_error:
1454 dprintk("NFSD: xdr error (%s:%d)\n", __FILE__, __LINE__);
1455 status = nfserr_bad_xdr;
1456 goto out;
1457}
1458
Mi Jinlong345c2842011-10-20 17:51:39 +08001459static __be32 nfsd4_decode_destroy_clientid(struct nfsd4_compoundargs *argp, struct nfsd4_destroy_clientid *dc)
1460{
1461 DECODE_HEAD;
1462
1463 READ_BUF(8);
1464 COPYMEM(&dc->clientid, 8);
1465
1466 DECODE_TAIL;
1467}
1468
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04001469static __be32 nfsd4_decode_reclaim_complete(struct nfsd4_compoundargs *argp, struct nfsd4_reclaim_complete *rc)
1470{
1471 DECODE_HEAD;
1472
1473 READ_BUF(4);
1474 READ32(rc->rca_one_fs);
1475
1476 DECODE_TAIL;
1477}
1478
Andy Adamson2db134e2009-04-03 08:27:55 +03001479static __be32
Benny Halevy347e0ad2008-07-02 11:13:41 +03001480nfsd4_decode_noop(struct nfsd4_compoundargs *argp, void *p)
1481{
1482 return nfs_ok;
1483}
1484
Benny Halevy3c375c62008-07-02 11:14:01 +03001485static __be32
1486nfsd4_decode_notsupp(struct nfsd4_compoundargs *argp, void *p)
1487{
Benny Halevy1e685ec2009-03-04 23:06:06 +02001488 return nfserr_notsupp;
Benny Halevy3c375c62008-07-02 11:14:01 +03001489}
1490
Benny Halevy347e0ad2008-07-02 11:13:41 +03001491typedef __be32(*nfsd4_dec)(struct nfsd4_compoundargs *argp, void *);
1492
1493static nfsd4_dec nfsd4_dec_ops[] = {
J. Bruce Fieldsad1060c2008-07-18 15:04:16 -04001494 [OP_ACCESS] = (nfsd4_dec)nfsd4_decode_access,
1495 [OP_CLOSE] = (nfsd4_dec)nfsd4_decode_close,
1496 [OP_COMMIT] = (nfsd4_dec)nfsd4_decode_commit,
1497 [OP_CREATE] = (nfsd4_dec)nfsd4_decode_create,
1498 [OP_DELEGPURGE] = (nfsd4_dec)nfsd4_decode_notsupp,
1499 [OP_DELEGRETURN] = (nfsd4_dec)nfsd4_decode_delegreturn,
1500 [OP_GETATTR] = (nfsd4_dec)nfsd4_decode_getattr,
1501 [OP_GETFH] = (nfsd4_dec)nfsd4_decode_noop,
1502 [OP_LINK] = (nfsd4_dec)nfsd4_decode_link,
1503 [OP_LOCK] = (nfsd4_dec)nfsd4_decode_lock,
1504 [OP_LOCKT] = (nfsd4_dec)nfsd4_decode_lockt,
1505 [OP_LOCKU] = (nfsd4_dec)nfsd4_decode_locku,
1506 [OP_LOOKUP] = (nfsd4_dec)nfsd4_decode_lookup,
1507 [OP_LOOKUPP] = (nfsd4_dec)nfsd4_decode_noop,
1508 [OP_NVERIFY] = (nfsd4_dec)nfsd4_decode_verify,
1509 [OP_OPEN] = (nfsd4_dec)nfsd4_decode_open,
1510 [OP_OPENATTR] = (nfsd4_dec)nfsd4_decode_notsupp,
1511 [OP_OPEN_CONFIRM] = (nfsd4_dec)nfsd4_decode_open_confirm,
1512 [OP_OPEN_DOWNGRADE] = (nfsd4_dec)nfsd4_decode_open_downgrade,
1513 [OP_PUTFH] = (nfsd4_dec)nfsd4_decode_putfh,
J. Bruce Fieldsa1c8c4d2009-03-09 12:17:29 -04001514 [OP_PUTPUBFH] = (nfsd4_dec)nfsd4_decode_noop,
J. Bruce Fieldsad1060c2008-07-18 15:04:16 -04001515 [OP_PUTROOTFH] = (nfsd4_dec)nfsd4_decode_noop,
1516 [OP_READ] = (nfsd4_dec)nfsd4_decode_read,
1517 [OP_READDIR] = (nfsd4_dec)nfsd4_decode_readdir,
1518 [OP_READLINK] = (nfsd4_dec)nfsd4_decode_noop,
1519 [OP_REMOVE] = (nfsd4_dec)nfsd4_decode_remove,
1520 [OP_RENAME] = (nfsd4_dec)nfsd4_decode_rename,
1521 [OP_RENEW] = (nfsd4_dec)nfsd4_decode_renew,
1522 [OP_RESTOREFH] = (nfsd4_dec)nfsd4_decode_noop,
1523 [OP_SAVEFH] = (nfsd4_dec)nfsd4_decode_noop,
1524 [OP_SECINFO] = (nfsd4_dec)nfsd4_decode_secinfo,
1525 [OP_SETATTR] = (nfsd4_dec)nfsd4_decode_setattr,
1526 [OP_SETCLIENTID] = (nfsd4_dec)nfsd4_decode_setclientid,
1527 [OP_SETCLIENTID_CONFIRM] = (nfsd4_dec)nfsd4_decode_setclientid_confirm,
1528 [OP_VERIFY] = (nfsd4_dec)nfsd4_decode_verify,
1529 [OP_WRITE] = (nfsd4_dec)nfsd4_decode_write,
1530 [OP_RELEASE_LOCKOWNER] = (nfsd4_dec)nfsd4_decode_release_lockowner,
Benny Halevy347e0ad2008-07-02 11:13:41 +03001531};
1532
Andy Adamson2db134e2009-04-03 08:27:55 +03001533static nfsd4_dec nfsd41_dec_ops[] = {
Randy Dunlap9064caa2009-04-28 16:48:25 -07001534 [OP_ACCESS] = (nfsd4_dec)nfsd4_decode_access,
1535 [OP_CLOSE] = (nfsd4_dec)nfsd4_decode_close,
1536 [OP_COMMIT] = (nfsd4_dec)nfsd4_decode_commit,
1537 [OP_CREATE] = (nfsd4_dec)nfsd4_decode_create,
1538 [OP_DELEGPURGE] = (nfsd4_dec)nfsd4_decode_notsupp,
1539 [OP_DELEGRETURN] = (nfsd4_dec)nfsd4_decode_delegreturn,
1540 [OP_GETATTR] = (nfsd4_dec)nfsd4_decode_getattr,
1541 [OP_GETFH] = (nfsd4_dec)nfsd4_decode_noop,
1542 [OP_LINK] = (nfsd4_dec)nfsd4_decode_link,
1543 [OP_LOCK] = (nfsd4_dec)nfsd4_decode_lock,
1544 [OP_LOCKT] = (nfsd4_dec)nfsd4_decode_lockt,
1545 [OP_LOCKU] = (nfsd4_dec)nfsd4_decode_locku,
1546 [OP_LOOKUP] = (nfsd4_dec)nfsd4_decode_lookup,
1547 [OP_LOOKUPP] = (nfsd4_dec)nfsd4_decode_noop,
1548 [OP_NVERIFY] = (nfsd4_dec)nfsd4_decode_verify,
1549 [OP_OPEN] = (nfsd4_dec)nfsd4_decode_open,
1550 [OP_OPENATTR] = (nfsd4_dec)nfsd4_decode_notsupp,
1551 [OP_OPEN_CONFIRM] = (nfsd4_dec)nfsd4_decode_notsupp,
1552 [OP_OPEN_DOWNGRADE] = (nfsd4_dec)nfsd4_decode_open_downgrade,
1553 [OP_PUTFH] = (nfsd4_dec)nfsd4_decode_putfh,
1554 [OP_PUTPUBFH] = (nfsd4_dec)nfsd4_decode_notsupp,
1555 [OP_PUTROOTFH] = (nfsd4_dec)nfsd4_decode_noop,
1556 [OP_READ] = (nfsd4_dec)nfsd4_decode_read,
1557 [OP_READDIR] = (nfsd4_dec)nfsd4_decode_readdir,
1558 [OP_READLINK] = (nfsd4_dec)nfsd4_decode_noop,
1559 [OP_REMOVE] = (nfsd4_dec)nfsd4_decode_remove,
1560 [OP_RENAME] = (nfsd4_dec)nfsd4_decode_rename,
1561 [OP_RENEW] = (nfsd4_dec)nfsd4_decode_notsupp,
1562 [OP_RESTOREFH] = (nfsd4_dec)nfsd4_decode_noop,
1563 [OP_SAVEFH] = (nfsd4_dec)nfsd4_decode_noop,
1564 [OP_SECINFO] = (nfsd4_dec)nfsd4_decode_secinfo,
1565 [OP_SETATTR] = (nfsd4_dec)nfsd4_decode_setattr,
1566 [OP_SETCLIENTID] = (nfsd4_dec)nfsd4_decode_notsupp,
1567 [OP_SETCLIENTID_CONFIRM]= (nfsd4_dec)nfsd4_decode_notsupp,
1568 [OP_VERIFY] = (nfsd4_dec)nfsd4_decode_verify,
1569 [OP_WRITE] = (nfsd4_dec)nfsd4_decode_write,
1570 [OP_RELEASE_LOCKOWNER] = (nfsd4_dec)nfsd4_decode_notsupp,
Andy Adamson2db134e2009-04-03 08:27:55 +03001571
1572 /* new operations for NFSv4.1 */
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04001573 [OP_BACKCHANNEL_CTL] = (nfsd4_dec)nfsd4_decode_backchannel_ctl,
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001574 [OP_BIND_CONN_TO_SESSION]= (nfsd4_dec)nfsd4_decode_bind_conn_to_session,
Randy Dunlap9064caa2009-04-28 16:48:25 -07001575 [OP_EXCHANGE_ID] = (nfsd4_dec)nfsd4_decode_exchange_id,
1576 [OP_CREATE_SESSION] = (nfsd4_dec)nfsd4_decode_create_session,
1577 [OP_DESTROY_SESSION] = (nfsd4_dec)nfsd4_decode_destroy_session,
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04001578 [OP_FREE_STATEID] = (nfsd4_dec)nfsd4_decode_free_stateid,
Randy Dunlap9064caa2009-04-28 16:48:25 -07001579 [OP_GET_DIR_DELEGATION] = (nfsd4_dec)nfsd4_decode_notsupp,
1580 [OP_GETDEVICEINFO] = (nfsd4_dec)nfsd4_decode_notsupp,
1581 [OP_GETDEVICELIST] = (nfsd4_dec)nfsd4_decode_notsupp,
1582 [OP_LAYOUTCOMMIT] = (nfsd4_dec)nfsd4_decode_notsupp,
1583 [OP_LAYOUTGET] = (nfsd4_dec)nfsd4_decode_notsupp,
1584 [OP_LAYOUTRETURN] = (nfsd4_dec)nfsd4_decode_notsupp,
J. Bruce Fields04f4ad12010-12-16 09:51:13 -05001585 [OP_SECINFO_NO_NAME] = (nfsd4_dec)nfsd4_decode_secinfo_no_name,
Randy Dunlap9064caa2009-04-28 16:48:25 -07001586 [OP_SEQUENCE] = (nfsd4_dec)nfsd4_decode_sequence,
1587 [OP_SET_SSV] = (nfsd4_dec)nfsd4_decode_notsupp,
Bryan Schumaker17456802011-07-13 10:50:48 -04001588 [OP_TEST_STATEID] = (nfsd4_dec)nfsd4_decode_test_stateid,
Randy Dunlap9064caa2009-04-28 16:48:25 -07001589 [OP_WANT_DELEGATION] = (nfsd4_dec)nfsd4_decode_notsupp,
Mi Jinlong345c2842011-10-20 17:51:39 +08001590 [OP_DESTROY_CLIENTID] = (nfsd4_dec)nfsd4_decode_destroy_clientid,
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04001591 [OP_RECLAIM_COMPLETE] = (nfsd4_dec)nfsd4_decode_reclaim_complete,
Andy Adamson2db134e2009-04-03 08:27:55 +03001592};
1593
Benny Halevyf2feb962008-07-02 11:14:22 +03001594struct nfsd4_minorversion_ops {
1595 nfsd4_dec *decoders;
1596 int nops;
1597};
1598
1599static struct nfsd4_minorversion_ops nfsd4_minorversion[] = {
J. Bruce Fieldsad1060c2008-07-18 15:04:16 -04001600 [0] = { nfsd4_dec_ops, ARRAY_SIZE(nfsd4_dec_ops) },
Andy Adamson2db134e2009-04-03 08:27:55 +03001601 [1] = { nfsd41_dec_ops, ARRAY_SIZE(nfsd41_dec_ops) },
Steve Dickson4bdc33e2013-05-02 13:18:57 -04001602 [2] = { nfsd41_dec_ops, ARRAY_SIZE(nfsd41_dec_ops) },
Benny Halevyf2feb962008-07-02 11:14:22 +03001603};
1604
Benny Halevy347e0ad2008-07-02 11:13:41 +03001605static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606nfsd4_decode_compound(struct nfsd4_compoundargs *argp)
1607{
1608 DECODE_HEAD;
1609 struct nfsd4_op *op;
Benny Halevyf2feb962008-07-02 11:14:22 +03001610 struct nfsd4_minorversion_ops *ops;
J. Bruce Fields10910062011-01-24 12:11:02 -05001611 bool cachethis = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 int i;
1613
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 READ_BUF(4);
1615 READ32(argp->taglen);
1616 READ_BUF(argp->taglen + 8);
1617 SAVEMEM(argp->tag, argp->taglen);
1618 READ32(argp->minorversion);
1619 READ32(argp->opcnt);
1620
1621 if (argp->taglen > NFSD4_MAX_TAGLEN)
1622 goto xdr_error;
1623 if (argp->opcnt > 100)
1624 goto xdr_error;
1625
Tobias Klausere8c96f82006-03-24 03:15:34 -08001626 if (argp->opcnt > ARRAY_SIZE(argp->iops)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 argp->ops = kmalloc(argp->opcnt * sizeof(*argp->ops), GFP_KERNEL);
1628 if (!argp->ops) {
1629 argp->ops = argp->iops;
Chuck Lever817cb9d2007-09-11 18:01:20 -04001630 dprintk("nfsd: couldn't allocate room for COMPOUND\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 goto xdr_error;
1632 }
1633 }
1634
Benny Halevyf2feb962008-07-02 11:14:22 +03001635 if (argp->minorversion >= ARRAY_SIZE(nfsd4_minorversion))
Benny Halevy30cff1f2008-07-02 11:13:18 +03001636 argp->opcnt = 0;
1637
Benny Halevyf2feb962008-07-02 11:14:22 +03001638 ops = &nfsd4_minorversion[argp->minorversion];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 for (i = 0; i < argp->opcnt; i++) {
1640 op = &argp->ops[i];
1641 op->replay = NULL;
1642
J. Bruce Fields8a61b182012-11-16 22:28:38 -05001643 READ_BUF(4);
1644 READ32(op->opnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
Ricardo Labiagade3cab72009-12-11 20:03:27 -08001646 if (op->opnum >= FIRST_NFS4_OP && op->opnum <= LAST_NFS4_OP)
Benny Halevyf2feb962008-07-02 11:14:22 +03001647 op->status = ops->decoders[op->opnum](argp, &op->u);
Benny Halevy347e0ad2008-07-02 11:13:41 +03001648 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 op->opnum = OP_ILLEGAL;
1650 op->status = nfserr_op_illegal;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 }
1652
1653 if (op->status) {
1654 argp->opcnt = i+1;
1655 break;
1656 }
J. Bruce Fields10910062011-01-24 12:11:02 -05001657 /*
1658 * We'll try to cache the result in the DRC if any one
1659 * op in the compound wants to be cached:
1660 */
1661 cachethis |= nfsd4_cache_this_op(op);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 }
J. Bruce Fields10910062011-01-24 12:11:02 -05001663 /* Sessions make the DRC unnecessary: */
1664 if (argp->minorversion)
1665 cachethis = false;
1666 argp->rqstp->rq_cachetype = cachethis ? RC_REPLBUFF : RC_NOCACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667
1668 DECODE_TAIL;
1669}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671#define WRITE32(n) *p++ = htonl(n)
1672#define WRITE64(n) do { \
1673 *p++ = htonl((u32)((n) >> 32)); \
1674 *p++ = htonl((u32)(n)); \
1675} while (0)
Harvey Harrison5108b272008-07-17 21:33:04 -07001676#define WRITEMEM(ptr,nbytes) do { if (nbytes > 0) { \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 *(p + XDR_QUADLEN(nbytes) -1) = 0; \
1678 memcpy(p, ptr, nbytes); \
1679 p += XDR_QUADLEN(nbytes); \
Harvey Harrison5108b272008-07-17 21:33:04 -07001680}} while (0)
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04001681
1682static void write32(__be32 **p, u32 n)
1683{
J. Bruce Fields45eaa1c2012-04-25 18:11:04 -04001684 *(*p)++ = htonl(n);
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04001685}
1686
1687static void write64(__be32 **p, u64 n)
1688{
J. Bruce Fields45eaa1c2012-04-25 18:11:04 -04001689 write32(p, (n >> 32));
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04001690 write32(p, (u32)n);
1691}
1692
1693static void write_change(__be32 **p, struct kstat *stat, struct inode *inode)
1694{
1695 if (IS_I_VERSION(inode)) {
1696 write64(p, inode->i_version);
1697 } else {
1698 write32(p, stat->ctime.tv_sec);
1699 write32(p, stat->ctime.tv_nsec);
1700 }
1701}
1702
1703static void write_cinfo(__be32 **p, struct nfsd4_change_info *c)
1704{
1705 write32(p, c->atomic);
1706 if (c->change_supported) {
1707 write64(p, c->before_change);
1708 write64(p, c->after_change);
1709 } else {
1710 write32(p, c->before_ctime_sec);
1711 write32(p, c->before_ctime_nsec);
1712 write32(p, c->after_ctime_sec);
1713 write32(p, c->after_ctime_nsec);
1714 }
1715}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716
1717#define RESERVE_SPACE(nbytes) do { \
1718 p = resp->p; \
1719 BUG_ON(p + XDR_QUADLEN(nbytes) > resp->end); \
1720} while (0)
1721#define ADJUST_ARGS() resp->p = p
1722
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001723/* Encode as an array of strings the string given with components
Weston Andros Adamsone7a04442012-04-24 11:07:59 -04001724 * separated @sep, escaped with esc_enter and esc_exit.
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001725 */
Weston Andros Adamsone7a04442012-04-24 11:07:59 -04001726static __be32 nfsd4_encode_components_esc(char sep, char *components,
1727 __be32 **pp, int *buflen,
1728 char esc_enter, char esc_exit)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001729{
Al Viro2ebbc012006-10-19 23:28:58 -07001730 __be32 *p = *pp;
1731 __be32 *countp = p;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001732 int strlen, count=0;
Weston Andros Adamsone7a04442012-04-24 11:07:59 -04001733 char *str, *end, *next;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001734
1735 dprintk("nfsd4_encode_components(%s)\n", components);
1736 if ((*buflen -= 4) < 0)
1737 return nfserr_resource;
1738 WRITE32(0); /* We will fill this in with @count later */
1739 end = str = components;
1740 while (*end) {
Weston Andros Adamsone7a04442012-04-24 11:07:59 -04001741 bool found_esc = false;
1742
1743 /* try to parse as esc_start, ..., esc_end, sep */
1744 if (*str == esc_enter) {
1745 for (; *end && (*end != esc_exit); end++)
1746 /* find esc_exit or end of string */;
1747 next = end + 1;
1748 if (*end && (!*next || *next == sep)) {
1749 str++;
1750 found_esc = true;
1751 }
1752 }
1753
1754 if (!found_esc)
1755 for (; *end && (*end != sep); end++)
1756 /* find sep or end of string */;
1757
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001758 strlen = end - str;
1759 if (strlen) {
1760 if ((*buflen -= ((XDR_QUADLEN(strlen) << 2) + 4)) < 0)
1761 return nfserr_resource;
1762 WRITE32(strlen);
1763 WRITEMEM(str, strlen);
1764 count++;
1765 }
1766 else
1767 end++;
1768 str = end;
1769 }
1770 *pp = p;
1771 p = countp;
1772 WRITE32(count);
1773 return 0;
1774}
1775
Weston Andros Adamsone7a04442012-04-24 11:07:59 -04001776/* Encode as an array of strings the string given with components
1777 * separated @sep.
1778 */
1779static __be32 nfsd4_encode_components(char sep, char *components,
1780 __be32 **pp, int *buflen)
1781{
1782 return nfsd4_encode_components_esc(sep, components, pp, buflen, 0, 0);
1783}
1784
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001785/*
1786 * encode a location element of a fs_locations structure
1787 */
Al Virob37ad282006-10-19 23:28:59 -07001788static __be32 nfsd4_encode_fs_location4(struct nfsd4_fs_location *location,
Al Viro2ebbc012006-10-19 23:28:58 -07001789 __be32 **pp, int *buflen)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001790{
Al Virob37ad282006-10-19 23:28:59 -07001791 __be32 status;
Al Viro2ebbc012006-10-19 23:28:58 -07001792 __be32 *p = *pp;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001793
Weston Andros Adamsone7a04442012-04-24 11:07:59 -04001794 status = nfsd4_encode_components_esc(':', location->hosts, &p, buflen,
1795 '[', ']');
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001796 if (status)
1797 return status;
1798 status = nfsd4_encode_components('/', location->path, &p, buflen);
1799 if (status)
1800 return status;
1801 *pp = p;
1802 return 0;
1803}
1804
1805/*
Trond Myklebusted748aa2011-09-12 19:37:06 -04001806 * Encode a path in RFC3530 'pathname4' format
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001807 */
Trond Myklebusted748aa2011-09-12 19:37:06 -04001808static __be32 nfsd4_encode_path(const struct path *root,
1809 const struct path *path, __be32 **pp, int *buflen)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001810{
Trond Myklebusted748aa2011-09-12 19:37:06 -04001811 struct path cur = {
1812 .mnt = path->mnt,
1813 .dentry = path->dentry,
1814 };
1815 __be32 *p = *pp;
1816 struct dentry **components = NULL;
1817 unsigned int ncomponents = 0;
1818 __be32 err = nfserr_jukebox;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001819
Trond Myklebusted748aa2011-09-12 19:37:06 -04001820 dprintk("nfsd4_encode_components(");
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001821
Trond Myklebusted748aa2011-09-12 19:37:06 -04001822 path_get(&cur);
1823 /* First walk the path up to the nfsd root, and store the
1824 * dentries/path components in an array.
1825 */
1826 for (;;) {
1827 if (cur.dentry == root->dentry && cur.mnt == root->mnt)
1828 break;
1829 if (cur.dentry == cur.mnt->mnt_root) {
1830 if (follow_up(&cur))
1831 continue;
1832 goto out_free;
1833 }
1834 if ((ncomponents & 15) == 0) {
1835 struct dentry **new;
1836 new = krealloc(components,
1837 sizeof(*new) * (ncomponents + 16),
1838 GFP_KERNEL);
1839 if (!new)
1840 goto out_free;
1841 components = new;
1842 }
1843 components[ncomponents++] = cur.dentry;
1844 cur.dentry = dget_parent(cur.dentry);
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001845 }
Trond Myklebusted748aa2011-09-12 19:37:06 -04001846
1847 *buflen -= 4;
1848 if (*buflen < 0)
1849 goto out_free;
1850 WRITE32(ncomponents);
1851
1852 while (ncomponents) {
1853 struct dentry *dentry = components[ncomponents - 1];
1854 unsigned int len = dentry->d_name.len;
1855
1856 *buflen -= 4 + (XDR_QUADLEN(len) << 2);
1857 if (*buflen < 0)
1858 goto out_free;
1859 WRITE32(len);
1860 WRITEMEM(dentry->d_name.name, len);
1861 dprintk("/%s", dentry->d_name.name);
1862 dput(dentry);
1863 ncomponents--;
1864 }
1865
1866 *pp = p;
1867 err = 0;
1868out_free:
1869 dprintk(")\n");
1870 while (ncomponents)
1871 dput(components[--ncomponents]);
1872 kfree(components);
1873 path_put(&cur);
1874 return err;
1875}
1876
1877static __be32 nfsd4_encode_fsloc_fsroot(struct svc_rqst *rqstp,
1878 const struct path *path, __be32 **pp, int *buflen)
1879{
1880 struct svc_export *exp_ps;
1881 __be32 res;
1882
1883 exp_ps = rqst_find_fsidzero_export(rqstp);
1884 if (IS_ERR(exp_ps))
1885 return nfserrno(PTR_ERR(exp_ps));
1886 res = nfsd4_encode_path(&exp_ps->ex_path, path, pp, buflen);
1887 exp_put(exp_ps);
1888 return res;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001889}
1890
1891/*
1892 * encode a fs_locations structure
1893 */
Al Virob37ad282006-10-19 23:28:59 -07001894static __be32 nfsd4_encode_fs_locations(struct svc_rqst *rqstp,
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001895 struct svc_export *exp,
Al Viro2ebbc012006-10-19 23:28:58 -07001896 __be32 **pp, int *buflen)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001897{
Al Virob37ad282006-10-19 23:28:59 -07001898 __be32 status;
Al Virocc45f012006-10-19 23:28:44 -07001899 int i;
Al Viro2ebbc012006-10-19 23:28:58 -07001900 __be32 *p = *pp;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001901 struct nfsd4_fs_locations *fslocs = &exp->ex_fslocs;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001902
Trond Myklebusted748aa2011-09-12 19:37:06 -04001903 status = nfsd4_encode_fsloc_fsroot(rqstp, &exp->ex_path, &p, buflen);
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001904 if (status)
1905 return status;
1906 if ((*buflen -= 4) < 0)
1907 return nfserr_resource;
1908 WRITE32(fslocs->locations_count);
1909 for (i=0; i<fslocs->locations_count; i++) {
1910 status = nfsd4_encode_fs_location4(&fslocs->locations[i],
1911 &p, buflen);
1912 if (status)
1913 return status;
1914 }
1915 *pp = p;
1916 return 0;
1917}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918
J. Bruce Fields3d2544b2011-08-15 11:49:30 -04001919static u32 nfs4_file_type(umode_t mode)
1920{
1921 switch (mode & S_IFMT) {
1922 case S_IFIFO: return NF4FIFO;
1923 case S_IFCHR: return NF4CHR;
1924 case S_IFDIR: return NF4DIR;
1925 case S_IFBLK: return NF4BLK;
1926 case S_IFLNK: return NF4LNK;
1927 case S_IFREG: return NF4REG;
1928 case S_IFSOCK: return NF4SOCK;
1929 default: return NF4BAD;
1930 };
1931}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932
Al Virob37ad282006-10-19 23:28:59 -07001933static __be32
Eric W. Biedermanab8e4ae2013-02-02 05:18:08 -08001934nfsd4_encode_name(struct svc_rqst *rqstp, int whotype, kuid_t uid, kgid_t gid,
Al Viro2ebbc012006-10-19 23:28:58 -07001935 __be32 **p, int *buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936{
1937 int status;
1938
1939 if (*buflen < (XDR_QUADLEN(IDMAP_NAMESZ) << 2) + 4)
1940 return nfserr_resource;
1941 if (whotype != NFS4_ACL_WHO_NAMED)
1942 status = nfs4_acl_write_who(whotype, (u8 *)(*p + 1));
Eric W. Biedermanab8e4ae2013-02-02 05:18:08 -08001943 else if (gid_valid(gid))
1944 status = nfsd_map_gid_to_name(rqstp, gid, (u8 *)(*p + 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 else
Eric W. Biedermanab8e4ae2013-02-02 05:18:08 -08001946 status = nfsd_map_uid_to_name(rqstp, uid, (u8 *)(*p + 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 if (status < 0)
1948 return nfserrno(status);
1949 *p = xdr_encode_opaque(*p, NULL, status);
1950 *buflen -= (XDR_QUADLEN(status) << 2) + 4;
1951 BUG_ON(*buflen < 0);
1952 return 0;
1953}
1954
Al Virob37ad282006-10-19 23:28:59 -07001955static inline __be32
Eric W. Biedermanab8e4ae2013-02-02 05:18:08 -08001956nfsd4_encode_user(struct svc_rqst *rqstp, kuid_t user, __be32 **p, int *buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957{
Eric W. Biedermanab8e4ae2013-02-02 05:18:08 -08001958 return nfsd4_encode_name(rqstp, NFS4_ACL_WHO_NAMED, user, INVALID_GID,
1959 p, buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960}
1961
Al Virob37ad282006-10-19 23:28:59 -07001962static inline __be32
Eric W. Biedermanab8e4ae2013-02-02 05:18:08 -08001963nfsd4_encode_group(struct svc_rqst *rqstp, kgid_t group, __be32 **p, int *buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964{
Eric W. Biedermanab8e4ae2013-02-02 05:18:08 -08001965 return nfsd4_encode_name(rqstp, NFS4_ACL_WHO_NAMED, INVALID_UID, group,
1966 p, buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967}
1968
Al Virob37ad282006-10-19 23:28:59 -07001969static inline __be32
Eric W. Biedermanab8e4ae2013-02-02 05:18:08 -08001970nfsd4_encode_aclname(struct svc_rqst *rqstp, struct nfs4_ace *ace,
Al Viro2ebbc012006-10-19 23:28:58 -07001971 __be32 **p, int *buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972{
Eric W. Biedermanab8e4ae2013-02-02 05:18:08 -08001973 kuid_t uid = INVALID_UID;
1974 kgid_t gid = INVALID_GID;
1975
1976 if (ace->whotype == NFS4_ACL_WHO_NAMED) {
1977 if (ace->flag & NFS4_ACE_IDENTIFIER_GROUP)
1978 gid = ace->who_gid;
1979 else
1980 uid = ace->who_uid;
1981 }
1982 return nfsd4_encode_name(rqstp, ace->whotype, uid, gid, p, buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983}
1984
J.Bruce Fields42ca0992006-10-04 02:16:20 -07001985#define WORD0_ABSENT_FS_ATTRS (FATTR4_WORD0_FS_LOCATIONS | FATTR4_WORD0_FSID | \
1986 FATTR4_WORD0_RDATTR_ERROR)
1987#define WORD1_ABSENT_FS_ATTRS FATTR4_WORD1_MOUNTED_ON_FILEID
1988
David Quigley18032ca2013-05-02 13:19:10 -04001989#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
1990static inline __be32
1991nfsd4_encode_security_label(struct svc_rqst *rqstp, void *context, int len, __be32 **pp, int *buflen)
1992{
1993 __be32 *p = *pp;
1994
1995 if (*buflen < ((XDR_QUADLEN(len) << 2) + 4 + 4 + 4))
1996 return nfserr_resource;
1997
1998 /*
1999 * For now we use a 0 here to indicate the null translation; in
2000 * the future we may place a call to translation code here.
2001 */
2002 if ((*buflen -= 8) < 0)
2003 return nfserr_resource;
2004
2005 WRITE32(0); /* lfs */
2006 WRITE32(0); /* pi */
2007 p = xdr_encode_opaque(p, context, len);
2008 *buflen -= (XDR_QUADLEN(len) << 2) + 4;
2009
2010 *pp = p;
2011 return 0;
2012}
2013#else
2014static inline __be32
2015nfsd4_encode_security_label(struct svc_rqst *rqstp, struct dentry *dentry, __be32 **pp, int *buflen)
2016{ return 0; }
2017#endif
2018
Al Virob37ad282006-10-19 23:28:59 -07002019static __be32 fattr_handle_absent_fs(u32 *bmval0, u32 *bmval1, u32 *rdattr_err)
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002020{
2021 /* As per referral draft: */
2022 if (*bmval0 & ~WORD0_ABSENT_FS_ATTRS ||
2023 *bmval1 & ~WORD1_ABSENT_FS_ATTRS) {
2024 if (*bmval0 & FATTR4_WORD0_RDATTR_ERROR ||
2025 *bmval0 & FATTR4_WORD0_FS_LOCATIONS)
2026 *rdattr_err = NFSERR_MOVED;
2027 else
2028 return nfserr_moved;
2029 }
2030 *bmval0 &= WORD0_ABSENT_FS_ATTRS;
2031 *bmval1 &= WORD1_ABSENT_FS_ATTRS;
2032 return 0;
2033}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034
J. Bruce Fieldsae7095a2012-10-01 17:50:56 -04002035
2036static int get_parent_attributes(struct svc_export *exp, struct kstat *stat)
2037{
2038 struct path path = exp->ex_path;
2039 int err;
2040
2041 path_get(&path);
2042 while (follow_up(&path)) {
2043 if (path.dentry != path.mnt->mnt_root)
2044 break;
2045 }
Al Viro3dadecc2013-01-24 02:18:08 -05002046 err = vfs_getattr(&path, stat);
J. Bruce Fieldsae7095a2012-10-01 17:50:56 -04002047 path_put(&path);
2048 return err;
2049}
2050
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051/*
2052 * Note: @fhp can be NULL; in this case, we might have to compose the filehandle
2053 * ourselves.
2054 *
J. Bruce Fields84822d02012-12-14 17:57:50 -05002055 * countp is the buffer size in _words_
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 */
Al Virob37ad282006-10-19 23:28:59 -07002057__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058nfsd4_encode_fattr(struct svc_fh *fhp, struct svc_export *exp,
J. Bruce Fields84822d02012-12-14 17:57:50 -05002059 struct dentry *dentry, __be32 **buffer, int count, u32 *bmval,
Frank Filz406a7ea2007-11-27 11:34:05 -08002060 struct svc_rqst *rqstp, int ignore_crossmnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061{
2062 u32 bmval0 = bmval[0];
2063 u32 bmval1 = bmval[1];
Andy Adamson7e705702009-04-03 08:29:11 +03002064 u32 bmval2 = bmval[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 struct kstat stat;
2066 struct svc_fh tempfh;
2067 struct kstatfs statfs;
J. Bruce Fields84822d02012-12-14 17:57:50 -05002068 int buflen = count << 2;
Al Viro2ebbc012006-10-19 23:28:58 -07002069 __be32 *attrlenp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 u32 dummy;
2071 u64 dummy64;
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002072 u32 rdattr_err = 0;
J. Bruce Fields84822d02012-12-14 17:57:50 -05002073 __be32 *p = *buffer;
Al Virob37ad282006-10-19 23:28:59 -07002074 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07002075 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 int aclsupport = 0;
2077 struct nfs4_acl *acl = NULL;
David Quigley18032ca2013-05-02 13:19:10 -04002078 void *context = NULL;
2079 int contextlen;
2080 bool contextsupport = false;
Andy Adamson7e705702009-04-03 08:29:11 +03002081 struct nfsd4_compoundres *resp = rqstp->rq_resp;
2082 u32 minorversion = resp->cstate.minorversion;
Christoph Hellwigebabe9a2010-07-07 18:53:11 +02002083 struct path path = {
2084 .mnt = exp->ex_path.mnt,
2085 .dentry = dentry,
2086 };
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +03002087 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088
2089 BUG_ON(bmval1 & NFSD_WRITEONLY_ATTRS_WORD1);
Andy Adamson7e705702009-04-03 08:29:11 +03002090 BUG_ON(bmval0 & ~nfsd_suppattrs0(minorversion));
2091 BUG_ON(bmval1 & ~nfsd_suppattrs1(minorversion));
2092 BUG_ON(bmval2 & ~nfsd_suppattrs2(minorversion));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002094 if (exp->ex_fslocs.migrated) {
Andy Adamson7e705702009-04-03 08:29:11 +03002095 BUG_ON(bmval[2]);
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002096 status = fattr_handle_absent_fs(&bmval0, &bmval1, &rdattr_err);
2097 if (status)
2098 goto out;
2099 }
2100
Al Viro3dadecc2013-01-24 02:18:08 -05002101 err = vfs_getattr(&path, &stat);
Al Virob8dd7b92006-10-19 23:29:01 -07002102 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 goto out_nfserr;
J. Bruce Fieldsa16e92e2007-09-28 16:45:51 -04002104 if ((bmval0 & (FATTR4_WORD0_FILES_FREE | FATTR4_WORD0_FILES_TOTAL |
2105 FATTR4_WORD0_MAXNAME)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 (bmval1 & (FATTR4_WORD1_SPACE_AVAIL | FATTR4_WORD1_SPACE_FREE |
2107 FATTR4_WORD1_SPACE_TOTAL))) {
Christoph Hellwigebabe9a2010-07-07 18:53:11 +02002108 err = vfs_statfs(&path, &statfs);
Al Virob8dd7b92006-10-19 23:29:01 -07002109 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 goto out_nfserr;
2111 }
2112 if ((bmval0 & (FATTR4_WORD0_FILEHANDLE | FATTR4_WORD0_FSID)) && !fhp) {
2113 fh_init(&tempfh, NFS4_FHSIZE);
2114 status = fh_compose(&tempfh, exp, dentry, NULL);
2115 if (status)
2116 goto out;
2117 fhp = &tempfh;
2118 }
2119 if (bmval0 & (FATTR4_WORD0_ACL | FATTR4_WORD0_ACLSUPPORT
2120 | FATTR4_WORD0_SUPPORTED_ATTRS)) {
Al Virob8dd7b92006-10-19 23:29:01 -07002121 err = nfsd4_get_nfs4_acl(rqstp, dentry, &acl);
2122 aclsupport = (err == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 if (bmval0 & FATTR4_WORD0_ACL) {
Al Virob8dd7b92006-10-19 23:29:01 -07002124 if (err == -EOPNOTSUPP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 bmval0 &= ~FATTR4_WORD0_ACL;
Al Virob8dd7b92006-10-19 23:29:01 -07002126 else if (err == -EINVAL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 status = nfserr_attrnotsupp;
2128 goto out;
Al Virob8dd7b92006-10-19 23:29:01 -07002129 } else if (err != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 goto out_nfserr;
2131 }
2132 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133
David Quigley18032ca2013-05-02 13:19:10 -04002134#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
2135 if ((bmval[2] & FATTR4_WORD2_SECURITY_LABEL) ||
2136 bmval[0] & FATTR4_WORD0_SUPPORTED_ATTRS) {
2137 err = security_inode_getsecctx(dentry->d_inode,
2138 &context, &contextlen);
2139 contextsupport = (err == 0);
2140 if (bmval2 & FATTR4_WORD2_SECURITY_LABEL) {
2141 if (err == -EOPNOTSUPP)
2142 bmval2 &= ~FATTR4_WORD2_SECURITY_LABEL;
2143 else if (err)
2144 goto out_nfserr;
2145 }
2146 }
2147#endif /* CONFIG_NFSD_V4_SECURITY_LABEL */
2148
Benny Halevy2b44f1b2010-09-30 20:47:46 +02002149 if (bmval2) {
2150 if ((buflen -= 16) < 0)
2151 goto out_resource;
Andy Adamson7e705702009-04-03 08:29:11 +03002152 WRITE32(3);
2153 WRITE32(bmval0);
2154 WRITE32(bmval1);
2155 WRITE32(bmval2);
Benny Halevy2b44f1b2010-09-30 20:47:46 +02002156 } else if (bmval1) {
2157 if ((buflen -= 12) < 0)
2158 goto out_resource;
Andy Adamson7e705702009-04-03 08:29:11 +03002159 WRITE32(2);
2160 WRITE32(bmval0);
2161 WRITE32(bmval1);
2162 } else {
Benny Halevy2b44f1b2010-09-30 20:47:46 +02002163 if ((buflen -= 8) < 0)
2164 goto out_resource;
Andy Adamson7e705702009-04-03 08:29:11 +03002165 WRITE32(1);
2166 WRITE32(bmval0);
2167 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 attrlenp = p++; /* to be backfilled later */
2169
2170 if (bmval0 & FATTR4_WORD0_SUPPORTED_ATTRS) {
Andy Adamson7e705702009-04-03 08:29:11 +03002171 u32 word0 = nfsd_suppattrs0(minorversion);
2172 u32 word1 = nfsd_suppattrs1(minorversion);
2173 u32 word2 = nfsd_suppattrs2(minorversion);
2174
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002175 if (!aclsupport)
2176 word0 &= ~FATTR4_WORD0_ACL;
David Quigley18032ca2013-05-02 13:19:10 -04002177 if (!contextsupport)
2178 word2 &= ~FATTR4_WORD2_SECURITY_LABEL;
Andy Adamson7e705702009-04-03 08:29:11 +03002179 if (!word2) {
Benny Halevy2b44f1b2010-09-30 20:47:46 +02002180 if ((buflen -= 12) < 0)
2181 goto out_resource;
Andy Adamson7e705702009-04-03 08:29:11 +03002182 WRITE32(2);
2183 WRITE32(word0);
2184 WRITE32(word1);
2185 } else {
Benny Halevy2b44f1b2010-09-30 20:47:46 +02002186 if ((buflen -= 16) < 0)
2187 goto out_resource;
Andy Adamson7e705702009-04-03 08:29:11 +03002188 WRITE32(3);
2189 WRITE32(word0);
2190 WRITE32(word1);
2191 WRITE32(word2);
2192 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 }
2194 if (bmval0 & FATTR4_WORD0_TYPE) {
2195 if ((buflen -= 4) < 0)
2196 goto out_resource;
J. Bruce Fields3d2544b2011-08-15 11:49:30 -04002197 dummy = nfs4_file_type(stat.mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 if (dummy == NF4BAD)
2199 goto out_serverfault;
2200 WRITE32(dummy);
2201 }
2202 if (bmval0 & FATTR4_WORD0_FH_EXPIRE_TYPE) {
2203 if ((buflen -= 4) < 0)
2204 goto out_resource;
NeilBrown49640002005-06-23 22:02:58 -07002205 if (exp->ex_flags & NFSEXP_NOSUBTREECHECK)
NeilBrowne34ac862005-07-07 17:59:30 -07002206 WRITE32(NFS4_FH_PERSISTENT);
NeilBrown49640002005-06-23 22:02:58 -07002207 else
NeilBrowne34ac862005-07-07 17:59:30 -07002208 WRITE32(NFS4_FH_PERSISTENT|NFS4_FH_VOL_RENAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 }
2210 if (bmval0 & FATTR4_WORD0_CHANGE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 if ((buflen -= 8) < 0)
2212 goto out_resource;
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002213 write_change(&p, &stat, dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 }
2215 if (bmval0 & FATTR4_WORD0_SIZE) {
2216 if ((buflen -= 8) < 0)
2217 goto out_resource;
2218 WRITE64(stat.size);
2219 }
2220 if (bmval0 & FATTR4_WORD0_LINK_SUPPORT) {
2221 if ((buflen -= 4) < 0)
2222 goto out_resource;
2223 WRITE32(1);
2224 }
2225 if (bmval0 & FATTR4_WORD0_SYMLINK_SUPPORT) {
2226 if ((buflen -= 4) < 0)
2227 goto out_resource;
2228 WRITE32(1);
2229 }
2230 if (bmval0 & FATTR4_WORD0_NAMED_ATTR) {
2231 if ((buflen -= 4) < 0)
2232 goto out_resource;
2233 WRITE32(0);
2234 }
2235 if (bmval0 & FATTR4_WORD0_FSID) {
2236 if ((buflen -= 16) < 0)
2237 goto out_resource;
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002238 if (exp->ex_fslocs.migrated) {
2239 WRITE64(NFS4_REFERRAL_FSID_MAJOR);
2240 WRITE64(NFS4_REFERRAL_FSID_MINOR);
NeilBrownaf6a4e22007-02-14 00:33:12 -08002241 } else switch(fsid_source(fhp)) {
2242 case FSIDSOURCE_FSID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 WRITE64((u64)exp->ex_fsid);
2244 WRITE64((u64)0);
NeilBrownaf6a4e22007-02-14 00:33:12 -08002245 break;
2246 case FSIDSOURCE_DEV:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 WRITE32(0);
2248 WRITE32(MAJOR(stat.dev));
2249 WRITE32(0);
2250 WRITE32(MINOR(stat.dev));
NeilBrownaf6a4e22007-02-14 00:33:12 -08002251 break;
2252 case FSIDSOURCE_UUID:
2253 WRITEMEM(exp->ex_uuid, 16);
2254 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 }
2256 }
2257 if (bmval0 & FATTR4_WORD0_UNIQUE_HANDLES) {
2258 if ((buflen -= 4) < 0)
2259 goto out_resource;
2260 WRITE32(0);
2261 }
2262 if (bmval0 & FATTR4_WORD0_LEASE_TIME) {
2263 if ((buflen -= 4) < 0)
2264 goto out_resource;
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +03002265 WRITE32(nn->nfsd4_lease);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 }
2267 if (bmval0 & FATTR4_WORD0_RDATTR_ERROR) {
2268 if ((buflen -= 4) < 0)
2269 goto out_resource;
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002270 WRITE32(rdattr_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 }
2272 if (bmval0 & FATTR4_WORD0_ACL) {
2273 struct nfs4_ace *ace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274
2275 if (acl == NULL) {
2276 if ((buflen -= 4) < 0)
2277 goto out_resource;
2278
2279 WRITE32(0);
2280 goto out_acl;
2281 }
2282 if ((buflen -= 4) < 0)
2283 goto out_resource;
2284 WRITE32(acl->naces);
2285
J. Bruce Fields28e05dd2007-02-16 01:28:30 -08002286 for (ace = acl->aces; ace < acl->aces + acl->naces; ace++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 if ((buflen -= 4*3) < 0)
2288 goto out_resource;
2289 WRITE32(ace->type);
2290 WRITE32(ace->flag);
2291 WRITE32(ace->access_mask & NFS4_ACE_MASK_ALL);
Eric W. Biedermanab8e4ae2013-02-02 05:18:08 -08002292 status = nfsd4_encode_aclname(rqstp, ace, &p, &buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 if (status == nfserr_resource)
2294 goto out_resource;
2295 if (status)
2296 goto out;
2297 }
2298 }
2299out_acl:
2300 if (bmval0 & FATTR4_WORD0_ACLSUPPORT) {
2301 if ((buflen -= 4) < 0)
2302 goto out_resource;
2303 WRITE32(aclsupport ?
2304 ACL4_SUPPORT_ALLOW_ACL|ACL4_SUPPORT_DENY_ACL : 0);
2305 }
2306 if (bmval0 & FATTR4_WORD0_CANSETTIME) {
2307 if ((buflen -= 4) < 0)
2308 goto out_resource;
2309 WRITE32(1);
2310 }
2311 if (bmval0 & FATTR4_WORD0_CASE_INSENSITIVE) {
2312 if ((buflen -= 4) < 0)
2313 goto out_resource;
J. Bruce Fields2930d382012-06-05 16:52:06 -04002314 WRITE32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 }
2316 if (bmval0 & FATTR4_WORD0_CASE_PRESERVING) {
2317 if ((buflen -= 4) < 0)
2318 goto out_resource;
2319 WRITE32(1);
2320 }
2321 if (bmval0 & FATTR4_WORD0_CHOWN_RESTRICTED) {
2322 if ((buflen -= 4) < 0)
2323 goto out_resource;
2324 WRITE32(1);
2325 }
2326 if (bmval0 & FATTR4_WORD0_FILEHANDLE) {
2327 buflen -= (XDR_QUADLEN(fhp->fh_handle.fh_size) << 2) + 4;
2328 if (buflen < 0)
2329 goto out_resource;
2330 WRITE32(fhp->fh_handle.fh_size);
2331 WRITEMEM(&fhp->fh_handle.fh_base, fhp->fh_handle.fh_size);
2332 }
2333 if (bmval0 & FATTR4_WORD0_FILEID) {
2334 if ((buflen -= 8) < 0)
2335 goto out_resource;
Peter Staubach40ee5dc2007-08-16 12:10:07 -04002336 WRITE64(stat.ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 }
2338 if (bmval0 & FATTR4_WORD0_FILES_AVAIL) {
2339 if ((buflen -= 8) < 0)
2340 goto out_resource;
2341 WRITE64((u64) statfs.f_ffree);
2342 }
2343 if (bmval0 & FATTR4_WORD0_FILES_FREE) {
2344 if ((buflen -= 8) < 0)
2345 goto out_resource;
2346 WRITE64((u64) statfs.f_ffree);
2347 }
2348 if (bmval0 & FATTR4_WORD0_FILES_TOTAL) {
2349 if ((buflen -= 8) < 0)
2350 goto out_resource;
2351 WRITE64((u64) statfs.f_files);
2352 }
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002353 if (bmval0 & FATTR4_WORD0_FS_LOCATIONS) {
2354 status = nfsd4_encode_fs_locations(rqstp, exp, &p, &buflen);
2355 if (status == nfserr_resource)
2356 goto out_resource;
2357 if (status)
2358 goto out;
2359 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 if (bmval0 & FATTR4_WORD0_HOMOGENEOUS) {
2361 if ((buflen -= 4) < 0)
2362 goto out_resource;
2363 WRITE32(1);
2364 }
2365 if (bmval0 & FATTR4_WORD0_MAXFILESIZE) {
2366 if ((buflen -= 8) < 0)
2367 goto out_resource;
2368 WRITE64(~(u64)0);
2369 }
2370 if (bmval0 & FATTR4_WORD0_MAXLINK) {
2371 if ((buflen -= 4) < 0)
2372 goto out_resource;
2373 WRITE32(255);
2374 }
2375 if (bmval0 & FATTR4_WORD0_MAXNAME) {
2376 if ((buflen -= 4) < 0)
2377 goto out_resource;
J. Bruce Fieldsa16e92e2007-09-28 16:45:51 -04002378 WRITE32(statfs.f_namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 }
2380 if (bmval0 & FATTR4_WORD0_MAXREAD) {
2381 if ((buflen -= 8) < 0)
2382 goto out_resource;
Greg Banks7adae482006-10-04 02:15:47 -07002383 WRITE64((u64) svc_max_payload(rqstp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 }
2385 if (bmval0 & FATTR4_WORD0_MAXWRITE) {
2386 if ((buflen -= 8) < 0)
2387 goto out_resource;
Greg Banks7adae482006-10-04 02:15:47 -07002388 WRITE64((u64) svc_max_payload(rqstp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 }
2390 if (bmval1 & FATTR4_WORD1_MODE) {
2391 if ((buflen -= 4) < 0)
2392 goto out_resource;
2393 WRITE32(stat.mode & S_IALLUGO);
2394 }
2395 if (bmval1 & FATTR4_WORD1_NO_TRUNC) {
2396 if ((buflen -= 4) < 0)
2397 goto out_resource;
2398 WRITE32(1);
2399 }
2400 if (bmval1 & FATTR4_WORD1_NUMLINKS) {
2401 if ((buflen -= 4) < 0)
2402 goto out_resource;
2403 WRITE32(stat.nlink);
2404 }
2405 if (bmval1 & FATTR4_WORD1_OWNER) {
2406 status = nfsd4_encode_user(rqstp, stat.uid, &p, &buflen);
2407 if (status == nfserr_resource)
2408 goto out_resource;
2409 if (status)
2410 goto out;
2411 }
2412 if (bmval1 & FATTR4_WORD1_OWNER_GROUP) {
2413 status = nfsd4_encode_group(rqstp, stat.gid, &p, &buflen);
2414 if (status == nfserr_resource)
2415 goto out_resource;
2416 if (status)
2417 goto out;
2418 }
2419 if (bmval1 & FATTR4_WORD1_RAWDEV) {
2420 if ((buflen -= 8) < 0)
2421 goto out_resource;
2422 WRITE32((u32) MAJOR(stat.rdev));
2423 WRITE32((u32) MINOR(stat.rdev));
2424 }
2425 if (bmval1 & FATTR4_WORD1_SPACE_AVAIL) {
2426 if ((buflen -= 8) < 0)
2427 goto out_resource;
2428 dummy64 = (u64)statfs.f_bavail * (u64)statfs.f_bsize;
2429 WRITE64(dummy64);
2430 }
2431 if (bmval1 & FATTR4_WORD1_SPACE_FREE) {
2432 if ((buflen -= 8) < 0)
2433 goto out_resource;
2434 dummy64 = (u64)statfs.f_bfree * (u64)statfs.f_bsize;
2435 WRITE64(dummy64);
2436 }
2437 if (bmval1 & FATTR4_WORD1_SPACE_TOTAL) {
2438 if ((buflen -= 8) < 0)
2439 goto out_resource;
2440 dummy64 = (u64)statfs.f_blocks * (u64)statfs.f_bsize;
2441 WRITE64(dummy64);
2442 }
2443 if (bmval1 & FATTR4_WORD1_SPACE_USED) {
2444 if ((buflen -= 8) < 0)
2445 goto out_resource;
2446 dummy64 = (u64)stat.blocks << 9;
2447 WRITE64(dummy64);
2448 }
2449 if (bmval1 & FATTR4_WORD1_TIME_ACCESS) {
2450 if ((buflen -= 12) < 0)
2451 goto out_resource;
Bryan Schumakerbf8d9092013-04-19 16:09:38 -04002452 WRITE64((s64)stat.atime.tv_sec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 WRITE32(stat.atime.tv_nsec);
2454 }
2455 if (bmval1 & FATTR4_WORD1_TIME_DELTA) {
2456 if ((buflen -= 12) < 0)
2457 goto out_resource;
2458 WRITE32(0);
2459 WRITE32(1);
2460 WRITE32(0);
2461 }
2462 if (bmval1 & FATTR4_WORD1_TIME_METADATA) {
2463 if ((buflen -= 12) < 0)
2464 goto out_resource;
Bryan Schumakerbf8d9092013-04-19 16:09:38 -04002465 WRITE64((s64)stat.ctime.tv_sec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 WRITE32(stat.ctime.tv_nsec);
2467 }
2468 if (bmval1 & FATTR4_WORD1_TIME_MODIFY) {
2469 if ((buflen -= 12) < 0)
2470 goto out_resource;
Bryan Schumakerbf8d9092013-04-19 16:09:38 -04002471 WRITE64((s64)stat.mtime.tv_sec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 WRITE32(stat.mtime.tv_nsec);
2473 }
2474 if (bmval1 & FATTR4_WORD1_MOUNTED_ON_FILEID) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 if ((buflen -= 8) < 0)
2476 goto out_resource;
Frank Filz406a7ea2007-11-27 11:34:05 -08002477 /*
2478 * Get parent's attributes if not ignoring crossmount
2479 * and this is the root of a cross-mounted filesystem.
2480 */
2481 if (ignore_crossmnt == 0 &&
J. Bruce Fieldsae7095a2012-10-01 17:50:56 -04002482 dentry == exp->ex_path.mnt->mnt_root)
2483 get_parent_attributes(exp, &stat);
Peter Staubach40ee5dc2007-08-16 12:10:07 -04002484 WRITE64(stat.ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 }
David Quigley18032ca2013-05-02 13:19:10 -04002486 if (bmval2 & FATTR4_WORD2_SECURITY_LABEL) {
2487 status = nfsd4_encode_security_label(rqstp, context,
2488 contextlen, &p, &buflen);
2489 if (status)
2490 goto out;
2491 }
Benny Halevy8c18f202009-04-03 08:29:14 +03002492 if (bmval2 & FATTR4_WORD2_SUPPATTR_EXCLCREAT) {
2493 WRITE32(3);
2494 WRITE32(NFSD_SUPPATTR_EXCLCREAT_WORD0);
2495 WRITE32(NFSD_SUPPATTR_EXCLCREAT_WORD1);
2496 WRITE32(NFSD_SUPPATTR_EXCLCREAT_WORD2);
2497 }
Andy Adamson7e705702009-04-03 08:29:11 +03002498
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 *attrlenp = htonl((char *)p - (char *)attrlenp - 4);
J. Bruce Fields84822d02012-12-14 17:57:50 -05002500 *buffer = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 status = nfs_ok;
2502
2503out:
David Quigley18032ca2013-05-02 13:19:10 -04002504 if (context)
2505 security_release_secctx(context, contextlen);
J. Bruce Fields28e05dd2007-02-16 01:28:30 -08002506 kfree(acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 if (fhp == &tempfh)
2508 fh_put(&tempfh);
2509 return status;
2510out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07002511 status = nfserrno(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 goto out;
2513out_resource:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 status = nfserr_resource;
2515 goto out;
2516out_serverfault:
2517 status = nfserr_serverfault;
2518 goto out;
2519}
2520
J. Bruce Fieldsc0ce6ec2008-02-11 15:48:47 -05002521static inline int attributes_need_mount(u32 *bmval)
2522{
2523 if (bmval[0] & ~(FATTR4_WORD0_RDATTR_ERROR | FATTR4_WORD0_LEASE_TIME))
2524 return 1;
2525 if (bmval[1] & ~FATTR4_WORD1_MOUNTED_ON_FILEID)
2526 return 1;
2527 return 0;
2528}
2529
Al Virob37ad282006-10-19 23:28:59 -07002530static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531nfsd4_encode_dirent_fattr(struct nfsd4_readdir *cd,
J. Bruce Fields84822d02012-12-14 17:57:50 -05002532 const char *name, int namlen, __be32 **p, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533{
2534 struct svc_export *exp = cd->rd_fhp->fh_export;
2535 struct dentry *dentry;
Al Virob37ad282006-10-19 23:28:59 -07002536 __be32 nfserr;
Frank Filz406a7ea2007-11-27 11:34:05 -08002537 int ignore_crossmnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538
2539 dentry = lookup_one_len(name, cd->rd_fhp->fh_dentry, namlen);
2540 if (IS_ERR(dentry))
2541 return nfserrno(PTR_ERR(dentry));
J. Bruce Fieldsb2c0cea2009-05-05 19:04:29 -04002542 if (!dentry->d_inode) {
2543 /*
2544 * nfsd_buffered_readdir drops the i_mutex between
2545 * readdir and calling this callback, leaving a window
2546 * where this directory entry could have gone away.
2547 */
2548 dput(dentry);
2549 return nfserr_noent;
2550 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551
2552 exp_get(exp);
Frank Filz406a7ea2007-11-27 11:34:05 -08002553 /*
2554 * In the case of a mountpoint, the client may be asking for
2555 * attributes that are only properties of the underlying filesystem
2556 * as opposed to the cross-mounted file system. In such a case,
2557 * we will not follow the cross mount and will fill the attribtutes
2558 * directly from the mountpoint dentry.
2559 */
J. Bruce Fields3227fa42009-10-25 21:43:01 -04002560 if (nfsd_mountpoint(dentry, exp)) {
J.Bruce Fields021d3a72006-12-13 00:35:24 -08002561 int err;
2562
J. Bruce Fields3227fa42009-10-25 21:43:01 -04002563 if (!(exp->ex_flags & NFSEXP_V4ROOT)
2564 && !attributes_need_mount(cd->rd_bmval)) {
2565 ignore_crossmnt = 1;
2566 goto out_encode;
2567 }
Andy Adamsondcb488a32007-07-17 04:04:51 -07002568 /*
2569 * Why the heck aren't we just using nfsd_lookup??
2570 * Different "."/".." handling? Something else?
2571 * At least, add a comment here to explain....
2572 */
J.Bruce Fields021d3a72006-12-13 00:35:24 -08002573 err = nfsd_cross_mnt(cd->rd_rqstp, &dentry, &exp);
2574 if (err) {
2575 nfserr = nfserrno(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 goto out_put;
2577 }
Andy Adamsondcb488a32007-07-17 04:04:51 -07002578 nfserr = check_nfsd_access(exp, cd->rd_rqstp);
2579 if (nfserr)
2580 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581
2582 }
J. Bruce Fields3227fa42009-10-25 21:43:01 -04002583out_encode:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 nfserr = nfsd4_encode_fattr(NULL, exp, dentry, p, buflen, cd->rd_bmval,
Frank Filz406a7ea2007-11-27 11:34:05 -08002585 cd->rd_rqstp, ignore_crossmnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586out_put:
2587 dput(dentry);
2588 exp_put(exp);
2589 return nfserr;
2590}
2591
Al Viro2ebbc012006-10-19 23:28:58 -07002592static __be32 *
Al Virob37ad282006-10-19 23:28:59 -07002593nfsd4_encode_rdattr_error(__be32 *p, int buflen, __be32 nfserr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594{
Al Viro2ebbc012006-10-19 23:28:58 -07002595 __be32 *attrlenp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596
2597 if (buflen < 6)
2598 return NULL;
2599 *p++ = htonl(2);
2600 *p++ = htonl(FATTR4_WORD0_RDATTR_ERROR); /* bmval0 */
2601 *p++ = htonl(0); /* bmval1 */
2602
2603 attrlenp = p++;
2604 *p++ = nfserr; /* no htonl */
2605 *attrlenp = htonl((char *)p - (char *)attrlenp - 4);
2606 return p;
2607}
2608
2609static int
NeilBrowna0ad13e2007-01-26 00:57:10 -08002610nfsd4_encode_dirent(void *ccdv, const char *name, int namlen,
2611 loff_t offset, u64 ino, unsigned int d_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612{
NeilBrowna0ad13e2007-01-26 00:57:10 -08002613 struct readdir_cd *ccd = ccdv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 struct nfsd4_readdir *cd = container_of(ccd, struct nfsd4_readdir, common);
2615 int buflen;
Al Viro2ebbc012006-10-19 23:28:58 -07002616 __be32 *p = cd->buffer;
J. Bruce Fieldsb2c0cea2009-05-05 19:04:29 -04002617 __be32 *cookiep;
Al Virob37ad282006-10-19 23:28:59 -07002618 __be32 nfserr = nfserr_toosmall;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619
2620 /* In nfsv4, "." and ".." never make it onto the wire.. */
2621 if (name && isdotent(name, namlen)) {
2622 cd->common.err = nfs_ok;
2623 return 0;
2624 }
2625
2626 if (cd->offset)
2627 xdr_encode_hyper(cd->offset, (u64) offset);
2628
2629 buflen = cd->buflen - 4 - XDR_QUADLEN(namlen);
2630 if (buflen < 0)
2631 goto fail;
2632
2633 *p++ = xdr_one; /* mark entry present */
J. Bruce Fieldsb2c0cea2009-05-05 19:04:29 -04002634 cookiep = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 p = xdr_encode_hyper(p, NFS_OFFSET_MAX); /* offset of next entry */
2636 p = xdr_encode_array(p, name, namlen); /* name length & name */
2637
J. Bruce Fields84822d02012-12-14 17:57:50 -05002638 nfserr = nfsd4_encode_dirent_fattr(cd, name, namlen, &p, buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 switch (nfserr) {
2640 case nfs_ok:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 break;
2642 case nfserr_resource:
2643 nfserr = nfserr_toosmall;
2644 goto fail;
J. Bruce Fieldsb2c0cea2009-05-05 19:04:29 -04002645 case nfserr_noent:
2646 goto skip_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 default:
2648 /*
2649 * If the client requested the RDATTR_ERROR attribute,
2650 * we stuff the error code into this attribute
2651 * and continue. If this attribute was not requested,
2652 * then in accordance with the spec, we fail the
2653 * entire READDIR operation(!)
2654 */
2655 if (!(cd->rd_bmval[0] & FATTR4_WORD0_RDATTR_ERROR))
2656 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 p = nfsd4_encode_rdattr_error(p, buflen, nfserr);
Fred Isaman34081ef2006-01-18 17:43:40 -08002658 if (p == NULL) {
2659 nfserr = nfserr_toosmall;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 goto fail;
Fred Isaman34081ef2006-01-18 17:43:40 -08002661 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 }
2663 cd->buflen -= (p - cd->buffer);
2664 cd->buffer = p;
J. Bruce Fieldsb2c0cea2009-05-05 19:04:29 -04002665 cd->offset = cookiep;
2666skip_entry:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 cd->common.err = nfs_ok;
2668 return 0;
2669fail:
2670 cd->common.err = nfserr;
2671 return -EINVAL;
2672}
2673
Benny Halevye2f282b2008-08-12 20:45:07 +03002674static void
2675nfsd4_encode_stateid(struct nfsd4_compoundres *resp, stateid_t *sid)
2676{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002677 __be32 *p;
Benny Halevye2f282b2008-08-12 20:45:07 +03002678
2679 RESERVE_SPACE(sizeof(stateid_t));
2680 WRITE32(sid->si_generation);
2681 WRITEMEM(&sid->si_opaque, sizeof(stateid_opaque_t));
2682 ADJUST_ARGS();
2683}
2684
Benny Halevy695e12f2008-07-04 14:38:33 +03002685static __be32
Al Virob37ad282006-10-19 23:28:59 -07002686nfsd4_encode_access(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_access *access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002688 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689
2690 if (!nfserr) {
2691 RESERVE_SPACE(8);
2692 WRITE32(access->ac_supported);
2693 WRITE32(access->ac_resp_access);
2694 ADJUST_ARGS();
2695 }
Benny Halevy695e12f2008-07-04 14:38:33 +03002696 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697}
2698
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002699static __be32 nfsd4_encode_bind_conn_to_session(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_bind_conn_to_session *bcts)
2700{
2701 __be32 *p;
2702
2703 if (!nfserr) {
2704 RESERVE_SPACE(NFS4_MAX_SESSIONID_LEN + 8);
2705 WRITEMEM(bcts->sessionid.data, NFS4_MAX_SESSIONID_LEN);
2706 WRITE32(bcts->dir);
J. Bruce Fields6e67b5d2012-09-13 09:49:43 -04002707 /* Sorry, we do not yet support RDMA over 4.1: */
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002708 WRITE32(0);
2709 ADJUST_ARGS();
2710 }
2711 return nfserr;
2712}
2713
Benny Halevy695e12f2008-07-04 14:38:33 +03002714static __be32
Al Virob37ad282006-10-19 23:28:59 -07002715nfsd4_encode_close(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716{
Benny Halevye2f282b2008-08-12 20:45:07 +03002717 if (!nfserr)
2718 nfsd4_encode_stateid(resp, &close->cl_stateid);
2719
Benny Halevy695e12f2008-07-04 14:38:33 +03002720 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721}
2722
2723
Benny Halevy695e12f2008-07-04 14:38:33 +03002724static __be32
Al Virob37ad282006-10-19 23:28:59 -07002725nfsd4_encode_commit(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_commit *commit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002727 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728
2729 if (!nfserr) {
Chuck Leverab4684d2012-03-02 17:13:50 -05002730 RESERVE_SPACE(NFS4_VERIFIER_SIZE);
2731 WRITEMEM(commit->co_verf.data, NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 ADJUST_ARGS();
2733 }
Benny Halevy695e12f2008-07-04 14:38:33 +03002734 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735}
2736
Benny Halevy695e12f2008-07-04 14:38:33 +03002737static __be32
Al Virob37ad282006-10-19 23:28:59 -07002738nfsd4_encode_create(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_create *create)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002740 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741
2742 if (!nfserr) {
2743 RESERVE_SPACE(32);
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002744 write_cinfo(&p, &create->cr_cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 WRITE32(2);
2746 WRITE32(create->cr_bmval[0]);
2747 WRITE32(create->cr_bmval[1]);
2748 ADJUST_ARGS();
2749 }
Benny Halevy695e12f2008-07-04 14:38:33 +03002750 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751}
2752
Al Virob37ad282006-10-19 23:28:59 -07002753static __be32
2754nfsd4_encode_getattr(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_getattr *getattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755{
2756 struct svc_fh *fhp = getattr->ga_fhp;
2757 int buflen;
2758
2759 if (nfserr)
2760 return nfserr;
2761
2762 buflen = resp->end - resp->p - (COMPOUND_ERR_SLACK_SPACE >> 2);
2763 nfserr = nfsd4_encode_fattr(fhp, fhp->fh_export, fhp->fh_dentry,
J. Bruce Fields84822d02012-12-14 17:57:50 -05002764 &resp->p, buflen, getattr->ga_bmval,
Frank Filz406a7ea2007-11-27 11:34:05 -08002765 resp->rqstp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 return nfserr;
2767}
2768
Benny Halevy695e12f2008-07-04 14:38:33 +03002769static __be32
2770nfsd4_encode_getfh(struct nfsd4_compoundres *resp, __be32 nfserr, struct svc_fh **fhpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771{
Benny Halevy695e12f2008-07-04 14:38:33 +03002772 struct svc_fh *fhp = *fhpp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 unsigned int len;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002774 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775
2776 if (!nfserr) {
2777 len = fhp->fh_handle.fh_size;
2778 RESERVE_SPACE(len + 4);
2779 WRITE32(len);
2780 WRITEMEM(&fhp->fh_handle.fh_base, len);
2781 ADJUST_ARGS();
2782 }
Benny Halevy695e12f2008-07-04 14:38:33 +03002783 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784}
2785
2786/*
2787* Including all fields other than the name, a LOCK4denied structure requires
2788* 8(clientid) + 4(namelen) + 8(offset) + 8(length) + 4(type) = 32 bytes.
2789*/
2790static void
2791nfsd4_encode_lock_denied(struct nfsd4_compoundres *resp, struct nfsd4_lock_denied *ld)
2792{
J. Bruce Fields7c13f342011-08-30 22:15:47 -04002793 struct xdr_netobj *conf = &ld->ld_owner;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002794 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795
J. Bruce Fields7c13f342011-08-30 22:15:47 -04002796 RESERVE_SPACE(32 + XDR_LEN(conf->len));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 WRITE64(ld->ld_start);
2798 WRITE64(ld->ld_length);
2799 WRITE32(ld->ld_type);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04002800 if (conf->len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 WRITEMEM(&ld->ld_clientid, 8);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04002802 WRITE32(conf->len);
2803 WRITEMEM(conf->data, conf->len);
2804 kfree(conf->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 } else { /* non - nfsv4 lock in conflict, no clientid nor owner */
2806 WRITE64((u64)0); /* clientid */
2807 WRITE32(0); /* length of owner name */
2808 }
2809 ADJUST_ARGS();
2810}
2811
Benny Halevy695e12f2008-07-04 14:38:33 +03002812static __be32
Al Virob37ad282006-10-19 23:28:59 -07002813nfsd4_encode_lock(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814{
Benny Halevye2f282b2008-08-12 20:45:07 +03002815 if (!nfserr)
2816 nfsd4_encode_stateid(resp, &lock->lk_resp_stateid);
2817 else if (nfserr == nfserr_denied)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 nfsd4_encode_lock_denied(resp, &lock->lk_denied);
2819
Benny Halevy695e12f2008-07-04 14:38:33 +03002820 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821}
2822
Benny Halevy695e12f2008-07-04 14:38:33 +03002823static __be32
Al Virob37ad282006-10-19 23:28:59 -07002824nfsd4_encode_lockt(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825{
2826 if (nfserr == nfserr_denied)
2827 nfsd4_encode_lock_denied(resp, &lockt->lt_denied);
Benny Halevy695e12f2008-07-04 14:38:33 +03002828 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829}
2830
Benny Halevy695e12f2008-07-04 14:38:33 +03002831static __be32
Al Virob37ad282006-10-19 23:28:59 -07002832nfsd4_encode_locku(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833{
Benny Halevye2f282b2008-08-12 20:45:07 +03002834 if (!nfserr)
2835 nfsd4_encode_stateid(resp, &locku->lu_stateid);
2836
Benny Halevy695e12f2008-07-04 14:38:33 +03002837 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838}
2839
2840
Benny Halevy695e12f2008-07-04 14:38:33 +03002841static __be32
Al Virob37ad282006-10-19 23:28:59 -07002842nfsd4_encode_link(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_link *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002844 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845
2846 if (!nfserr) {
2847 RESERVE_SPACE(20);
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002848 write_cinfo(&p, &link->li_cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 ADJUST_ARGS();
2850 }
Benny Halevy695e12f2008-07-04 14:38:33 +03002851 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852}
2853
2854
Benny Halevy695e12f2008-07-04 14:38:33 +03002855static __be32
Al Virob37ad282006-10-19 23:28:59 -07002856nfsd4_encode_open(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002858 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859
2860 if (nfserr)
2861 goto out;
2862
Benny Halevye2f282b2008-08-12 20:45:07 +03002863 nfsd4_encode_stateid(resp, &open->op_stateid);
2864 RESERVE_SPACE(40);
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002865 write_cinfo(&p, &open->op_cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 WRITE32(open->op_rflags);
2867 WRITE32(2);
2868 WRITE32(open->op_bmval[0]);
2869 WRITE32(open->op_bmval[1]);
2870 WRITE32(open->op_delegate_type);
2871 ADJUST_ARGS();
2872
2873 switch (open->op_delegate_type) {
2874 case NFS4_OPEN_DELEGATE_NONE:
2875 break;
2876 case NFS4_OPEN_DELEGATE_READ:
Benny Halevye2f282b2008-08-12 20:45:07 +03002877 nfsd4_encode_stateid(resp, &open->op_delegate_stateid);
2878 RESERVE_SPACE(20);
NeilBrown7b190fe2005-06-23 22:03:23 -07002879 WRITE32(open->op_recall);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880
2881 /*
2882 * TODO: ACE's in delegations
2883 */
2884 WRITE32(NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
2885 WRITE32(0);
2886 WRITE32(0);
2887 WRITE32(0); /* XXX: is NULL principal ok? */
2888 ADJUST_ARGS();
2889 break;
2890 case NFS4_OPEN_DELEGATE_WRITE:
Benny Halevye2f282b2008-08-12 20:45:07 +03002891 nfsd4_encode_stateid(resp, &open->op_delegate_stateid);
2892 RESERVE_SPACE(32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 WRITE32(0);
2894
2895 /*
2896 * TODO: space_limit's in delegations
2897 */
2898 WRITE32(NFS4_LIMIT_SIZE);
2899 WRITE32(~(u32)0);
2900 WRITE32(~(u32)0);
2901
2902 /*
2903 * TODO: ACE's in delegations
2904 */
2905 WRITE32(NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
2906 WRITE32(0);
2907 WRITE32(0);
2908 WRITE32(0); /* XXX: is NULL principal ok? */
2909 ADJUST_ARGS();
2910 break;
Benny Halevyd24433c2012-02-16 20:57:17 +02002911 case NFS4_OPEN_DELEGATE_NONE_EXT: /* 4.1 */
2912 switch (open->op_why_no_deleg) {
2913 case WND4_CONTENTION:
2914 case WND4_RESOURCE:
2915 RESERVE_SPACE(8);
2916 WRITE32(open->op_why_no_deleg);
2917 WRITE32(0); /* deleg signaling not supported yet */
2918 break;
2919 default:
2920 RESERVE_SPACE(4);
2921 WRITE32(open->op_why_no_deleg);
2922 }
2923 ADJUST_ARGS();
2924 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 default:
2926 BUG();
2927 }
2928 /* XXX save filehandle here */
2929out:
Benny Halevy695e12f2008-07-04 14:38:33 +03002930 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931}
2932
Benny Halevy695e12f2008-07-04 14:38:33 +03002933static __be32
Al Virob37ad282006-10-19 23:28:59 -07002934nfsd4_encode_open_confirm(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935{
Benny Halevye2f282b2008-08-12 20:45:07 +03002936 if (!nfserr)
2937 nfsd4_encode_stateid(resp, &oc->oc_resp_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938
Benny Halevy695e12f2008-07-04 14:38:33 +03002939 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940}
2941
Benny Halevy695e12f2008-07-04 14:38:33 +03002942static __be32
Al Virob37ad282006-10-19 23:28:59 -07002943nfsd4_encode_open_downgrade(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944{
Benny Halevye2f282b2008-08-12 20:45:07 +03002945 if (!nfserr)
2946 nfsd4_encode_stateid(resp, &od->od_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947
Benny Halevy695e12f2008-07-04 14:38:33 +03002948 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949}
2950
Al Virob37ad282006-10-19 23:28:59 -07002951static __be32
2952nfsd4_encode_read(struct nfsd4_compoundres *resp, __be32 nfserr,
NeilBrown44524352006-10-04 02:15:46 -07002953 struct nfsd4_read *read)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954{
2955 u32 eof;
J. Bruce Fieldsafc59402012-12-10 18:01:37 -05002956 int v;
2957 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 unsigned long maxcount;
2959 long len;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002960 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961
2962 if (nfserr)
2963 return nfserr;
2964 if (resp->xbuf->page_len)
2965 return nfserr_resource;
2966
2967 RESERVE_SPACE(8); /* eof flag and byte count */
2968
Greg Banks7adae482006-10-04 02:15:47 -07002969 maxcount = svc_max_payload(resp->rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 if (maxcount > read->rd_length)
2971 maxcount = read->rd_length;
2972
2973 len = maxcount;
2974 v = 0;
2975 while (len > 0) {
J. Bruce Fieldsafc59402012-12-10 18:01:37 -05002976 page = *(resp->rqstp->rq_next_page);
2977 if (!page) { /* ran out of pages */
J. Bruce Fieldsd5f50b02012-12-04 18:25:10 -05002978 maxcount -= len;
2979 break;
2980 }
J. Bruce Fieldsafc59402012-12-10 18:01:37 -05002981 resp->rqstp->rq_vec[v].iov_base = page_address(page);
NeilBrown3cc03b12006-10-04 02:15:47 -07002982 resp->rqstp->rq_vec[v].iov_len =
NeilBrown44524352006-10-04 02:15:46 -07002983 len < PAGE_SIZE ? len : PAGE_SIZE;
J. Bruce Fieldsafc59402012-12-10 18:01:37 -05002984 resp->rqstp->rq_next_page++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 v++;
2986 len -= PAGE_SIZE;
2987 }
2988 read->rd_vlen = v;
2989
J. Bruce Fields039a87c2010-07-30 11:33:32 -04002990 nfserr = nfsd_read_file(read->rd_rqstp, read->rd_fhp, read->rd_filp,
NeilBrown3cc03b12006-10-04 02:15:47 -07002991 read->rd_offset, resp->rqstp->rq_vec, read->rd_vlen,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 &maxcount);
2993
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 if (nfserr)
2995 return nfserr;
NeilBrown44524352006-10-04 02:15:46 -07002996 eof = (read->rd_offset + maxcount >=
2997 read->rd_fhp->fh_dentry->d_inode->i_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998
2999 WRITE32(eof);
3000 WRITE32(maxcount);
3001 ADJUST_ARGS();
NeilBrown6ed6dec2006-04-10 22:55:32 -07003002 resp->xbuf->head[0].iov_len = (char*)p
3003 - (char*)resp->xbuf->head[0].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 resp->xbuf->page_len = maxcount;
3005
NeilBrown6ed6dec2006-04-10 22:55:32 -07003006 /* Use rest of head for padding and remaining ops: */
NeilBrown6ed6dec2006-04-10 22:55:32 -07003007 resp->xbuf->tail[0].iov_base = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 resp->xbuf->tail[0].iov_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 if (maxcount&3) {
NeilBrown6ed6dec2006-04-10 22:55:32 -07003010 RESERVE_SPACE(4);
3011 WRITE32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 resp->xbuf->tail[0].iov_base += maxcount&3;
3013 resp->xbuf->tail[0].iov_len = 4 - (maxcount&3);
NeilBrown6ed6dec2006-04-10 22:55:32 -07003014 ADJUST_ARGS();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 }
3016 return 0;
3017}
3018
Al Virob37ad282006-10-19 23:28:59 -07003019static __be32
3020nfsd4_encode_readlink(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_readlink *readlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021{
3022 int maxcount;
3023 char *page;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003024 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025
3026 if (nfserr)
3027 return nfserr;
3028 if (resp->xbuf->page_len)
3029 return nfserr_resource;
J. Bruce Fieldsafc59402012-12-10 18:01:37 -05003030 if (!*resp->rqstp->rq_next_page)
J. Bruce Fieldsd5f50b02012-12-04 18:25:10 -05003031 return nfserr_resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032
J. Bruce Fieldsafc59402012-12-10 18:01:37 -05003033 page = page_address(*(resp->rqstp->rq_next_page++));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034
3035 maxcount = PAGE_SIZE;
3036 RESERVE_SPACE(4);
3037
3038 /*
3039 * XXX: By default, the ->readlink() VFS op will truncate symlinks
3040 * if they would overflow the buffer. Is this kosher in NFSv4? If
3041 * not, one easy fix is: if ->readlink() precisely fills the buffer,
3042 * assume that truncation occurred, and return NFS4ERR_RESOURCE.
3043 */
3044 nfserr = nfsd_readlink(readlink->rl_rqstp, readlink->rl_fhp, page, &maxcount);
3045 if (nfserr == nfserr_isdir)
3046 return nfserr_inval;
3047 if (nfserr)
3048 return nfserr;
3049
3050 WRITE32(maxcount);
3051 ADJUST_ARGS();
NeilBrown6ed6dec2006-04-10 22:55:32 -07003052 resp->xbuf->head[0].iov_len = (char*)p
3053 - (char*)resp->xbuf->head[0].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 resp->xbuf->page_len = maxcount;
NeilBrown6ed6dec2006-04-10 22:55:32 -07003055
3056 /* Use rest of head for padding and remaining ops: */
NeilBrown6ed6dec2006-04-10 22:55:32 -07003057 resp->xbuf->tail[0].iov_base = p;
3058 resp->xbuf->tail[0].iov_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 if (maxcount&3) {
NeilBrown6ed6dec2006-04-10 22:55:32 -07003060 RESERVE_SPACE(4);
3061 WRITE32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 resp->xbuf->tail[0].iov_base += maxcount&3;
3063 resp->xbuf->tail[0].iov_len = 4 - (maxcount&3);
NeilBrown6ed6dec2006-04-10 22:55:32 -07003064 ADJUST_ARGS();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 }
3066 return 0;
3067}
3068
Al Virob37ad282006-10-19 23:28:59 -07003069static __be32
3070nfsd4_encode_readdir(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_readdir *readdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071{
3072 int maxcount;
3073 loff_t offset;
Al Viro2ebbc012006-10-19 23:28:58 -07003074 __be32 *page, *savep, *tailbase;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003075 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076
3077 if (nfserr)
3078 return nfserr;
3079 if (resp->xbuf->page_len)
3080 return nfserr_resource;
J. Bruce Fieldsafc59402012-12-10 18:01:37 -05003081 if (!*resp->rqstp->rq_next_page)
J. Bruce Fieldsd5f50b02012-12-04 18:25:10 -05003082 return nfserr_resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083
Chuck Leverab4684d2012-03-02 17:13:50 -05003084 RESERVE_SPACE(NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 savep = p;
3086
3087 /* XXX: Following NFSv3, we ignore the READDIR verifier for now. */
3088 WRITE32(0);
3089 WRITE32(0);
3090 ADJUST_ARGS();
3091 resp->xbuf->head[0].iov_len = ((char*)resp->p) - (char*)resp->xbuf->head[0].iov_base;
NeilBrownbb6e8a92006-04-10 22:55:33 -07003092 tailbase = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093
3094 maxcount = PAGE_SIZE;
3095 if (maxcount > readdir->rd_maxcount)
3096 maxcount = readdir->rd_maxcount;
3097
3098 /*
3099 * Convert from bytes to words, account for the two words already
3100 * written, make sure to leave two words at the end for the next
3101 * pointer and eof field.
3102 */
3103 maxcount = (maxcount >> 2) - 4;
3104 if (maxcount < 0) {
3105 nfserr = nfserr_toosmall;
3106 goto err_no_verf;
3107 }
3108
J. Bruce Fieldsafc59402012-12-10 18:01:37 -05003109 page = page_address(*(resp->rqstp->rq_next_page++));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 readdir->common.err = 0;
3111 readdir->buflen = maxcount;
3112 readdir->buffer = page;
3113 readdir->offset = NULL;
3114
3115 offset = readdir->rd_cookie;
3116 nfserr = nfsd_readdir(readdir->rd_rqstp, readdir->rd_fhp,
3117 &offset,
3118 &readdir->common, nfsd4_encode_dirent);
3119 if (nfserr == nfs_ok &&
3120 readdir->common.err == nfserr_toosmall &&
3121 readdir->buffer == page)
3122 nfserr = nfserr_toosmall;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 if (nfserr)
3124 goto err_no_verf;
3125
3126 if (readdir->offset)
3127 xdr_encode_hyper(readdir->offset, offset);
3128
3129 p = readdir->buffer;
3130 *p++ = 0; /* no more entries */
3131 *p++ = htonl(readdir->common.err == nfserr_eof);
J. Bruce Fieldsafc59402012-12-10 18:01:37 -05003132 resp->xbuf->page_len = ((char*)p) -
3133 (char*)page_address(*(resp->rqstp->rq_next_page-1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134
NeilBrownbb6e8a92006-04-10 22:55:33 -07003135 /* Use rest of head for padding and remaining ops: */
NeilBrownbb6e8a92006-04-10 22:55:33 -07003136 resp->xbuf->tail[0].iov_base = tailbase;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 resp->xbuf->tail[0].iov_len = 0;
3138 resp->p = resp->xbuf->tail[0].iov_base;
NeilBrownbb6e8a92006-04-10 22:55:33 -07003139 resp->end = resp->p + (PAGE_SIZE - resp->xbuf->head[0].iov_len)/4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140
3141 return 0;
3142err_no_verf:
3143 p = savep;
3144 ADJUST_ARGS();
3145 return nfserr;
3146}
3147
Benny Halevy695e12f2008-07-04 14:38:33 +03003148static __be32
Al Virob37ad282006-10-19 23:28:59 -07003149nfsd4_encode_remove(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_remove *remove)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003151 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152
3153 if (!nfserr) {
3154 RESERVE_SPACE(20);
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04003155 write_cinfo(&p, &remove->rm_cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 ADJUST_ARGS();
3157 }
Benny Halevy695e12f2008-07-04 14:38:33 +03003158 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159}
3160
Benny Halevy695e12f2008-07-04 14:38:33 +03003161static __be32
Al Virob37ad282006-10-19 23:28:59 -07003162nfsd4_encode_rename(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_rename *rename)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003164 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165
3166 if (!nfserr) {
3167 RESERVE_SPACE(40);
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04003168 write_cinfo(&p, &rename->rn_sinfo);
3169 write_cinfo(&p, &rename->rn_tinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170 ADJUST_ARGS();
3171 }
Benny Halevy695e12f2008-07-04 14:38:33 +03003172 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173}
3174
Benny Halevy695e12f2008-07-04 14:38:33 +03003175static __be32
Mi Jinlong22b6dee2010-12-27 14:29:57 +08003176nfsd4_do_encode_secinfo(struct nfsd4_compoundres *resp,
Chuck Levera77c8062013-03-16 15:55:10 -04003177 __be32 nfserr, struct svc_export *exp)
Andy Adamsondcb488a32007-07-17 04:04:51 -07003178{
Chuck Lever676e4eb2013-04-30 18:48:54 -04003179 u32 i, nflavs, supported;
J. Bruce Fields4796f452007-07-17 04:04:51 -07003180 struct exp_flavor_info *flavs;
3181 struct exp_flavor_info def_flavs[2];
Chuck Lever676e4eb2013-04-30 18:48:54 -04003182 __be32 *p, *flavorsp;
3183 static bool report = true;
Andy Adamsondcb488a32007-07-17 04:04:51 -07003184
3185 if (nfserr)
3186 goto out;
J. Bruce Fields4796f452007-07-17 04:04:51 -07003187 if (exp->ex_nflavors) {
3188 flavs = exp->ex_flavors;
3189 nflavs = exp->ex_nflavors;
3190 } else { /* Handling of some defaults in absence of real secinfo: */
3191 flavs = def_flavs;
3192 if (exp->ex_client->flavour->flavour == RPC_AUTH_UNIX) {
3193 nflavs = 2;
3194 flavs[0].pseudoflavor = RPC_AUTH_UNIX;
3195 flavs[1].pseudoflavor = RPC_AUTH_NULL;
3196 } else if (exp->ex_client->flavour->flavour == RPC_AUTH_GSS) {
3197 nflavs = 1;
3198 flavs[0].pseudoflavor
3199 = svcauth_gss_flavor(exp->ex_client);
3200 } else {
3201 nflavs = 1;
3202 flavs[0].pseudoflavor
3203 = exp->ex_client->flavour->flavour;
3204 }
3205 }
3206
Chuck Lever676e4eb2013-04-30 18:48:54 -04003207 supported = 0;
Andy Adamsondcb488a32007-07-17 04:04:51 -07003208 RESERVE_SPACE(4);
Chuck Lever676e4eb2013-04-30 18:48:54 -04003209 flavorsp = p++; /* to be backfilled later */
Andy Adamsondcb488a32007-07-17 04:04:51 -07003210 ADJUST_ARGS();
Chuck Lever676e4eb2013-04-30 18:48:54 -04003211
J. Bruce Fields4796f452007-07-17 04:04:51 -07003212 for (i = 0; i < nflavs; i++) {
Chuck Lever676e4eb2013-04-30 18:48:54 -04003213 rpc_authflavor_t pf = flavs[i].pseudoflavor;
Chuck Levera77c8062013-03-16 15:55:10 -04003214 struct rpcsec_gss_info info;
Andy Adamsondcb488a32007-07-17 04:04:51 -07003215
Chuck Lever676e4eb2013-04-30 18:48:54 -04003216 if (rpcauth_get_gssinfo(pf, &info) == 0) {
3217 supported++;
Chuck Levered9411a2013-04-30 18:48:45 -04003218 RESERVE_SPACE(4 + 4 + info.oid.len + 4 + 4);
Andy Adamsondcb488a32007-07-17 04:04:51 -07003219 WRITE32(RPC_AUTH_GSS);
Chuck Levera77c8062013-03-16 15:55:10 -04003220 WRITE32(info.oid.len);
3221 WRITEMEM(info.oid.data, info.oid.len);
Chuck Levera77c8062013-03-16 15:55:10 -04003222 WRITE32(info.qop);
Chuck Levera77c8062013-03-16 15:55:10 -04003223 WRITE32(info.service);
Andy Adamsondcb488a32007-07-17 04:04:51 -07003224 ADJUST_ARGS();
Chuck Lever676e4eb2013-04-30 18:48:54 -04003225 } else if (pf < RPC_AUTH_MAXFLAVOR) {
3226 supported++;
Andy Adamsondcb488a32007-07-17 04:04:51 -07003227 RESERVE_SPACE(4);
Chuck Lever676e4eb2013-04-30 18:48:54 -04003228 WRITE32(pf);
Andy Adamsondcb488a32007-07-17 04:04:51 -07003229 ADJUST_ARGS();
Chuck Lever676e4eb2013-04-30 18:48:54 -04003230 } else {
3231 if (report)
3232 pr_warn("NFS: SECINFO: security flavor %u "
3233 "is not supported\n", pf);
Andy Adamsondcb488a32007-07-17 04:04:51 -07003234 }
3235 }
Chuck Levera77c8062013-03-16 15:55:10 -04003236
Chuck Lever676e4eb2013-04-30 18:48:54 -04003237 if (nflavs != supported)
3238 report = false;
3239 *flavorsp = htonl(supported);
3240
Andy Adamsondcb488a32007-07-17 04:04:51 -07003241out:
3242 if (exp)
3243 exp_put(exp);
Benny Halevy695e12f2008-07-04 14:38:33 +03003244 return nfserr;
Andy Adamsondcb488a32007-07-17 04:04:51 -07003245}
3246
Mi Jinlong22b6dee2010-12-27 14:29:57 +08003247static __be32
3248nfsd4_encode_secinfo(struct nfsd4_compoundres *resp, __be32 nfserr,
3249 struct nfsd4_secinfo *secinfo)
3250{
3251 return nfsd4_do_encode_secinfo(resp, nfserr, secinfo->si_exp);
3252}
3253
3254static __be32
3255nfsd4_encode_secinfo_no_name(struct nfsd4_compoundres *resp, __be32 nfserr,
3256 struct nfsd4_secinfo_no_name *secinfo)
3257{
3258 return nfsd4_do_encode_secinfo(resp, nfserr, secinfo->sin_exp);
3259}
3260
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261/*
3262 * The SETATTR encode routine is special -- it always encodes a bitmap,
3263 * regardless of the error status.
3264 */
Benny Halevy695e12f2008-07-04 14:38:33 +03003265static __be32
Al Virob37ad282006-10-19 23:28:59 -07003266nfsd4_encode_setattr(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_setattr *setattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003268 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269
David Quigley18032ca2013-05-02 13:19:10 -04003270 RESERVE_SPACE(16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 if (nfserr) {
David Quigley18032ca2013-05-02 13:19:10 -04003272 WRITE32(3);
3273 WRITE32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274 WRITE32(0);
3275 WRITE32(0);
3276 }
3277 else {
David Quigley18032ca2013-05-02 13:19:10 -04003278 WRITE32(3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 WRITE32(setattr->sa_bmval[0]);
3280 WRITE32(setattr->sa_bmval[1]);
David Quigley18032ca2013-05-02 13:19:10 -04003281 WRITE32(setattr->sa_bmval[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 }
3283 ADJUST_ARGS();
Benny Halevy695e12f2008-07-04 14:38:33 +03003284 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285}
3286
Benny Halevy695e12f2008-07-04 14:38:33 +03003287static __be32
Al Virob37ad282006-10-19 23:28:59 -07003288nfsd4_encode_setclientid(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_setclientid *scd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003290 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291
3292 if (!nfserr) {
Chuck Leverab4684d2012-03-02 17:13:50 -05003293 RESERVE_SPACE(8 + NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294 WRITEMEM(&scd->se_clientid, 8);
Chuck Leverab4684d2012-03-02 17:13:50 -05003295 WRITEMEM(&scd->se_confirm, NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296 ADJUST_ARGS();
3297 }
3298 else if (nfserr == nfserr_clid_inuse) {
3299 RESERVE_SPACE(8);
3300 WRITE32(0);
3301 WRITE32(0);
3302 ADJUST_ARGS();
3303 }
Benny Halevy695e12f2008-07-04 14:38:33 +03003304 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305}
3306
Benny Halevy695e12f2008-07-04 14:38:33 +03003307static __be32
Al Virob37ad282006-10-19 23:28:59 -07003308nfsd4_encode_write(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_write *write)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003310 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311
3312 if (!nfserr) {
3313 RESERVE_SPACE(16);
3314 WRITE32(write->wr_bytes_written);
3315 WRITE32(write->wr_how_written);
Chuck Leverab4684d2012-03-02 17:13:50 -05003316 WRITEMEM(write->wr_verifier.data, NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317 ADJUST_ARGS();
3318 }
Benny Halevy695e12f2008-07-04 14:38:33 +03003319 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320}
3321
Benny Halevy695e12f2008-07-04 14:38:33 +03003322static __be32
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003323nfsd4_encode_exchange_id(struct nfsd4_compoundres *resp, __be32 nfserr,
Andy Adamson2db134e2009-04-03 08:27:55 +03003324 struct nfsd4_exchange_id *exid)
3325{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003326 __be32 *p;
Andy Adamson0733d212009-04-03 08:28:01 +03003327 char *major_id;
3328 char *server_scope;
3329 int major_id_sz;
3330 int server_scope_sz;
3331 uint64_t minor_id = 0;
3332
3333 if (nfserr)
3334 return nfserr;
3335
3336 major_id = utsname()->nodename;
3337 major_id_sz = strlen(major_id);
3338 server_scope = utsname()->nodename;
3339 server_scope_sz = strlen(server_scope);
3340
3341 RESERVE_SPACE(
3342 8 /* eir_clientid */ +
3343 4 /* eir_sequenceid */ +
3344 4 /* eir_flags */ +
3345 4 /* spr_how (SP4_NONE) */ +
3346 8 /* so_minor_id */ +
3347 4 /* so_major_id.len */ +
3348 (XDR_QUADLEN(major_id_sz) * 4) +
3349 4 /* eir_server_scope.len */ +
3350 (XDR_QUADLEN(server_scope_sz) * 4) +
3351 4 /* eir_server_impl_id.count (0) */);
3352
3353 WRITEMEM(&exid->clientid, 8);
3354 WRITE32(exid->seqid);
3355 WRITE32(exid->flags);
3356
3357 /* state_protect4_r. Currently only support SP4_NONE */
3358 BUG_ON(exid->spa_how != SP4_NONE);
3359 WRITE32(exid->spa_how);
3360
3361 /* The server_owner struct */
3362 WRITE64(minor_id); /* Minor id */
3363 /* major id */
3364 WRITE32(major_id_sz);
3365 WRITEMEM(major_id, major_id_sz);
3366
3367 /* Server scope */
3368 WRITE32(server_scope_sz);
3369 WRITEMEM(server_scope, server_scope_sz);
3370
3371 /* Implementation id */
3372 WRITE32(0); /* zero length nfs_impl_id4 array */
3373 ADJUST_ARGS();
3374 return 0;
Andy Adamson2db134e2009-04-03 08:27:55 +03003375}
3376
3377static __be32
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003378nfsd4_encode_create_session(struct nfsd4_compoundres *resp, __be32 nfserr,
Andy Adamson2db134e2009-04-03 08:27:55 +03003379 struct nfsd4_create_session *sess)
3380{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003381 __be32 *p;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003382
3383 if (nfserr)
3384 return nfserr;
3385
3386 RESERVE_SPACE(24);
3387 WRITEMEM(sess->sessionid.data, NFS4_MAX_SESSIONID_LEN);
3388 WRITE32(sess->seqid);
3389 WRITE32(sess->flags);
3390 ADJUST_ARGS();
3391
3392 RESERVE_SPACE(28);
3393 WRITE32(0); /* headerpadsz */
3394 WRITE32(sess->fore_channel.maxreq_sz);
3395 WRITE32(sess->fore_channel.maxresp_sz);
3396 WRITE32(sess->fore_channel.maxresp_cached);
3397 WRITE32(sess->fore_channel.maxops);
3398 WRITE32(sess->fore_channel.maxreqs);
3399 WRITE32(sess->fore_channel.nr_rdma_attrs);
3400 ADJUST_ARGS();
3401
3402 if (sess->fore_channel.nr_rdma_attrs) {
3403 RESERVE_SPACE(4);
3404 WRITE32(sess->fore_channel.rdma_attrs);
3405 ADJUST_ARGS();
3406 }
3407
3408 RESERVE_SPACE(28);
3409 WRITE32(0); /* headerpadsz */
3410 WRITE32(sess->back_channel.maxreq_sz);
3411 WRITE32(sess->back_channel.maxresp_sz);
3412 WRITE32(sess->back_channel.maxresp_cached);
3413 WRITE32(sess->back_channel.maxops);
3414 WRITE32(sess->back_channel.maxreqs);
3415 WRITE32(sess->back_channel.nr_rdma_attrs);
3416 ADJUST_ARGS();
3417
3418 if (sess->back_channel.nr_rdma_attrs) {
3419 RESERVE_SPACE(4);
3420 WRITE32(sess->back_channel.rdma_attrs);
3421 ADJUST_ARGS();
3422 }
3423 return 0;
Andy Adamson2db134e2009-04-03 08:27:55 +03003424}
3425
3426static __be32
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003427nfsd4_encode_destroy_session(struct nfsd4_compoundres *resp, __be32 nfserr,
Andy Adamson2db134e2009-04-03 08:27:55 +03003428 struct nfsd4_destroy_session *destroy_session)
3429{
Andy Adamson2db134e2009-04-03 08:27:55 +03003430 return nfserr;
3431}
3432
Daniel Mackc47d8322011-05-16 16:38:14 +02003433static __be32
J. Bruce Fieldsd1829b32012-04-25 18:04:54 -04003434nfsd4_encode_free_stateid(struct nfsd4_compoundres *resp, __be32 nfserr,
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003435 struct nfsd4_free_stateid *free_stateid)
3436{
3437 __be32 *p;
3438
3439 if (nfserr)
3440 return nfserr;
3441
3442 RESERVE_SPACE(4);
J. Bruce Fieldsd1829b32012-04-25 18:04:54 -04003443 *p++ = nfserr;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003444 ADJUST_ARGS();
3445 return nfserr;
3446}
3447
3448static __be32
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003449nfsd4_encode_sequence(struct nfsd4_compoundres *resp, __be32 nfserr,
Andy Adamson2db134e2009-04-03 08:27:55 +03003450 struct nfsd4_sequence *seq)
3451{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003452 __be32 *p;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003453
3454 if (nfserr)
3455 return nfserr;
3456
3457 RESERVE_SPACE(NFS4_MAX_SESSIONID_LEN + 20);
3458 WRITEMEM(seq->sessionid.data, NFS4_MAX_SESSIONID_LEN);
3459 WRITE32(seq->seqid);
3460 WRITE32(seq->slotid);
J. Bruce Fieldsb7d7ca32011-08-31 15:39:30 -04003461 /* Note slotid's are numbered from zero: */
3462 WRITE32(seq->maxslots - 1); /* sr_highest_slotid */
3463 WRITE32(seq->maxslots - 1); /* sr_target_highest_slotid */
J. Bruce Fields0d7bb712010-11-18 08:30:33 -05003464 WRITE32(seq->status_flags);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003465
3466 ADJUST_ARGS();
Andy Adamson557ce262009-08-28 08:45:04 -04003467 resp->cstate.datap = p; /* DRC cache data pointer */
Benny Halevyb85d4c02009-04-03 08:28:08 +03003468 return 0;
Andy Adamson2db134e2009-04-03 08:27:55 +03003469}
3470
J. Bruce Fields2355c592012-04-25 16:56:22 -04003471static __be32
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003472nfsd4_encode_test_stateid(struct nfsd4_compoundres *resp, __be32 nfserr,
Bryan Schumaker17456802011-07-13 10:50:48 -04003473 struct nfsd4_test_stateid *test_stateid)
3474{
Bryan Schumaker03cfb422012-01-27 10:22:49 -05003475 struct nfsd4_test_stateid_id *stateid, *next;
Bryan Schumaker17456802011-07-13 10:50:48 -04003476 __be32 *p;
Bryan Schumaker17456802011-07-13 10:50:48 -04003477
Bryan Schumaker03cfb422012-01-27 10:22:49 -05003478 RESERVE_SPACE(4 + (4 * test_stateid->ts_num_ids));
Bryan Schumaker17456802011-07-13 10:50:48 -04003479 *p++ = htonl(test_stateid->ts_num_ids);
Bryan Schumaker17456802011-07-13 10:50:48 -04003480
Bryan Schumaker03cfb422012-01-27 10:22:49 -05003481 list_for_each_entry_safe(stateid, next, &test_stateid->ts_stateid_list, ts_id_list) {
Al Viro02f5fde2012-04-13 00:10:34 -04003482 *p++ = stateid->ts_id_status;
Bryan Schumaker17456802011-07-13 10:50:48 -04003483 }
Bryan Schumaker17456802011-07-13 10:50:48 -04003484
Bryan Schumaker03cfb422012-01-27 10:22:49 -05003485 ADJUST_ARGS();
Bryan Schumaker17456802011-07-13 10:50:48 -04003486 return nfserr;
3487}
3488
Andy Adamson2db134e2009-04-03 08:27:55 +03003489static __be32
Benny Halevy695e12f2008-07-04 14:38:33 +03003490nfsd4_encode_noop(struct nfsd4_compoundres *resp, __be32 nfserr, void *p)
3491{
3492 return nfserr;
3493}
3494
3495typedef __be32(* nfsd4_enc)(struct nfsd4_compoundres *, __be32, void *);
3496
Andy Adamson2db134e2009-04-03 08:27:55 +03003497/*
3498 * Note: nfsd4_enc_ops vector is shared for v4.0 and v4.1
3499 * since we don't need to filter out obsolete ops as this is
3500 * done in the decoding phase.
3501 */
Benny Halevy695e12f2008-07-04 14:38:33 +03003502static nfsd4_enc nfsd4_enc_ops[] = {
J. Bruce Fieldsad1060c2008-07-18 15:04:16 -04003503 [OP_ACCESS] = (nfsd4_enc)nfsd4_encode_access,
3504 [OP_CLOSE] = (nfsd4_enc)nfsd4_encode_close,
3505 [OP_COMMIT] = (nfsd4_enc)nfsd4_encode_commit,
3506 [OP_CREATE] = (nfsd4_enc)nfsd4_encode_create,
3507 [OP_DELEGPURGE] = (nfsd4_enc)nfsd4_encode_noop,
3508 [OP_DELEGRETURN] = (nfsd4_enc)nfsd4_encode_noop,
3509 [OP_GETATTR] = (nfsd4_enc)nfsd4_encode_getattr,
3510 [OP_GETFH] = (nfsd4_enc)nfsd4_encode_getfh,
3511 [OP_LINK] = (nfsd4_enc)nfsd4_encode_link,
3512 [OP_LOCK] = (nfsd4_enc)nfsd4_encode_lock,
3513 [OP_LOCKT] = (nfsd4_enc)nfsd4_encode_lockt,
3514 [OP_LOCKU] = (nfsd4_enc)nfsd4_encode_locku,
3515 [OP_LOOKUP] = (nfsd4_enc)nfsd4_encode_noop,
3516 [OP_LOOKUPP] = (nfsd4_enc)nfsd4_encode_noop,
3517 [OP_NVERIFY] = (nfsd4_enc)nfsd4_encode_noop,
3518 [OP_OPEN] = (nfsd4_enc)nfsd4_encode_open,
Benny Halevy84f09f42009-03-04 23:05:35 +02003519 [OP_OPENATTR] = (nfsd4_enc)nfsd4_encode_noop,
J. Bruce Fieldsad1060c2008-07-18 15:04:16 -04003520 [OP_OPEN_CONFIRM] = (nfsd4_enc)nfsd4_encode_open_confirm,
3521 [OP_OPEN_DOWNGRADE] = (nfsd4_enc)nfsd4_encode_open_downgrade,
3522 [OP_PUTFH] = (nfsd4_enc)nfsd4_encode_noop,
3523 [OP_PUTPUBFH] = (nfsd4_enc)nfsd4_encode_noop,
3524 [OP_PUTROOTFH] = (nfsd4_enc)nfsd4_encode_noop,
3525 [OP_READ] = (nfsd4_enc)nfsd4_encode_read,
3526 [OP_READDIR] = (nfsd4_enc)nfsd4_encode_readdir,
3527 [OP_READLINK] = (nfsd4_enc)nfsd4_encode_readlink,
3528 [OP_REMOVE] = (nfsd4_enc)nfsd4_encode_remove,
3529 [OP_RENAME] = (nfsd4_enc)nfsd4_encode_rename,
3530 [OP_RENEW] = (nfsd4_enc)nfsd4_encode_noop,
3531 [OP_RESTOREFH] = (nfsd4_enc)nfsd4_encode_noop,
3532 [OP_SAVEFH] = (nfsd4_enc)nfsd4_encode_noop,
3533 [OP_SECINFO] = (nfsd4_enc)nfsd4_encode_secinfo,
3534 [OP_SETATTR] = (nfsd4_enc)nfsd4_encode_setattr,
3535 [OP_SETCLIENTID] = (nfsd4_enc)nfsd4_encode_setclientid,
3536 [OP_SETCLIENTID_CONFIRM] = (nfsd4_enc)nfsd4_encode_noop,
3537 [OP_VERIFY] = (nfsd4_enc)nfsd4_encode_noop,
3538 [OP_WRITE] = (nfsd4_enc)nfsd4_encode_write,
3539 [OP_RELEASE_LOCKOWNER] = (nfsd4_enc)nfsd4_encode_noop,
Andy Adamson2db134e2009-04-03 08:27:55 +03003540
3541 /* NFSv4.1 operations */
3542 [OP_BACKCHANNEL_CTL] = (nfsd4_enc)nfsd4_encode_noop,
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003543 [OP_BIND_CONN_TO_SESSION] = (nfsd4_enc)nfsd4_encode_bind_conn_to_session,
Andy Adamson2db134e2009-04-03 08:27:55 +03003544 [OP_EXCHANGE_ID] = (nfsd4_enc)nfsd4_encode_exchange_id,
3545 [OP_CREATE_SESSION] = (nfsd4_enc)nfsd4_encode_create_session,
3546 [OP_DESTROY_SESSION] = (nfsd4_enc)nfsd4_encode_destroy_session,
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003547 [OP_FREE_STATEID] = (nfsd4_enc)nfsd4_encode_free_stateid,
Andy Adamson2db134e2009-04-03 08:27:55 +03003548 [OP_GET_DIR_DELEGATION] = (nfsd4_enc)nfsd4_encode_noop,
3549 [OP_GETDEVICEINFO] = (nfsd4_enc)nfsd4_encode_noop,
3550 [OP_GETDEVICELIST] = (nfsd4_enc)nfsd4_encode_noop,
3551 [OP_LAYOUTCOMMIT] = (nfsd4_enc)nfsd4_encode_noop,
3552 [OP_LAYOUTGET] = (nfsd4_enc)nfsd4_encode_noop,
3553 [OP_LAYOUTRETURN] = (nfsd4_enc)nfsd4_encode_noop,
Mi Jinlong22b6dee2010-12-27 14:29:57 +08003554 [OP_SECINFO_NO_NAME] = (nfsd4_enc)nfsd4_encode_secinfo_no_name,
Andy Adamson2db134e2009-04-03 08:27:55 +03003555 [OP_SEQUENCE] = (nfsd4_enc)nfsd4_encode_sequence,
3556 [OP_SET_SSV] = (nfsd4_enc)nfsd4_encode_noop,
Bryan Schumaker17456802011-07-13 10:50:48 -04003557 [OP_TEST_STATEID] = (nfsd4_enc)nfsd4_encode_test_stateid,
Andy Adamson2db134e2009-04-03 08:27:55 +03003558 [OP_WANT_DELEGATION] = (nfsd4_enc)nfsd4_encode_noop,
3559 [OP_DESTROY_CLIENTID] = (nfsd4_enc)nfsd4_encode_noop,
3560 [OP_RECLAIM_COMPLETE] = (nfsd4_enc)nfsd4_encode_noop,
Benny Halevy695e12f2008-07-04 14:38:33 +03003561};
3562
Andy Adamson496c2622009-04-03 08:28:48 +03003563/*
3564 * Calculate the total amount of memory that the compound response has taken
Mi Jinlong58e7b332011-08-28 18:18:56 +08003565 * after encoding the current operation with pad.
Andy Adamson496c2622009-04-03 08:28:48 +03003566 *
Mi Jinlong58e7b332011-08-28 18:18:56 +08003567 * pad: if operation is non-idempotent, pad was calculate by op_rsize_bop()
3568 * which was specified at nfsd4_operation, else pad is zero.
Andy Adamson496c2622009-04-03 08:28:48 +03003569 *
Mi Jinlong58e7b332011-08-28 18:18:56 +08003570 * Compare this length to the session se_fmaxresp_sz and se_fmaxresp_cached.
Andy Adamson496c2622009-04-03 08:28:48 +03003571 *
3572 * Our se_fmaxresp_cached will always be a multiple of PAGE_SIZE, and so
3573 * will be at least a page and will therefore hold the xdr_buf head.
3574 */
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003575__be32 nfsd4_check_resp_size(struct nfsd4_compoundres *resp, u32 pad)
Andy Adamson496c2622009-04-03 08:28:48 +03003576{
Andy Adamson496c2622009-04-03 08:28:48 +03003577 struct xdr_buf *xb = &resp->rqstp->rq_res;
Andy Adamson496c2622009-04-03 08:28:48 +03003578 struct nfsd4_session *session = NULL;
3579 struct nfsd4_slot *slot = resp->cstate.slot;
Mi Jinlong58e7b332011-08-28 18:18:56 +08003580 u32 length, tlen = 0;
Andy Adamson496c2622009-04-03 08:28:48 +03003581
3582 if (!nfsd4_has_session(&resp->cstate))
Mi Jinlong58e7b332011-08-28 18:18:56 +08003583 return 0;
Andy Adamson496c2622009-04-03 08:28:48 +03003584
3585 session = resp->cstate.session;
Mi Jinlong58e7b332011-08-28 18:18:56 +08003586 if (session == NULL)
3587 return 0;
Andy Adamson496c2622009-04-03 08:28:48 +03003588
3589 if (xb->page_len == 0) {
3590 length = (char *)resp->p - (char *)xb->head[0].iov_base + pad;
3591 } else {
3592 if (xb->tail[0].iov_base && xb->tail[0].iov_len > 0)
3593 tlen = (char *)resp->p - (char *)xb->tail[0].iov_base;
3594
3595 length = xb->head[0].iov_len + xb->page_len + tlen + pad;
3596 }
3597 dprintk("%s length %u, xb->page_len %u tlen %u pad %u\n", __func__,
3598 length, xb->page_len, tlen, pad);
3599
Mi Jinlong58e7b332011-08-28 18:18:56 +08003600 if (length > session->se_fchannel.maxresp_sz)
3601 return nfserr_rep_too_big;
3602
J. Bruce Fields73e79482012-02-13 16:39:00 -05003603 if ((slot->sl_flags & NFSD4_SLOT_CACHETHIS) &&
Mi Jinlong58e7b332011-08-28 18:18:56 +08003604 length > session->se_fchannel.maxresp_cached)
Andy Adamson496c2622009-04-03 08:28:48 +03003605 return nfserr_rep_too_big_to_cache;
Mi Jinlong58e7b332011-08-28 18:18:56 +08003606
3607 return 0;
Andy Adamson496c2622009-04-03 08:28:48 +03003608}
3609
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610void
3611nfsd4_encode_operation(struct nfsd4_compoundres *resp, struct nfsd4_op *op)
3612{
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04003613 struct nfs4_stateowner *so = resp->cstate.replay_owner;
Al Viro2ebbc012006-10-19 23:28:58 -07003614 __be32 *statp;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003615 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616
3617 RESERVE_SPACE(8);
3618 WRITE32(op->opnum);
3619 statp = p++; /* to be backfilled at the end */
3620 ADJUST_ARGS();
3621
Benny Halevy695e12f2008-07-04 14:38:33 +03003622 if (op->opnum == OP_ILLEGAL)
3623 goto status;
3624 BUG_ON(op->opnum < 0 || op->opnum >= ARRAY_SIZE(nfsd4_enc_ops) ||
3625 !nfsd4_enc_ops[op->opnum]);
3626 op->status = nfsd4_enc_ops[op->opnum](resp, op->status, &op->u);
Andy Adamson496c2622009-04-03 08:28:48 +03003627 /* nfsd4_check_drc_limit guarantees enough room for error status */
Mi Jinlong58e7b332011-08-28 18:18:56 +08003628 if (!op->status)
3629 op->status = nfsd4_check_resp_size(resp, 0);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04003630 if (so) {
3631 so->so_replay.rp_status = op->status;
3632 so->so_replay.rp_buflen = (char *)resp->p - (char *)(statp+1);
3633 memcpy(so->so_replay.rp_buf, statp+1, so->so_replay.rp_buflen);
3634 }
Benny Halevy695e12f2008-07-04 14:38:33 +03003635status:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636 /*
3637 * Note: We write the status directly, instead of using WRITE32(),
3638 * since it is already in network byte order.
3639 */
3640 *statp = op->status;
3641}
3642
3643/*
3644 * Encode the reply stored in the stateowner reply cache
3645 *
3646 * XDR note: do not encode rp->rp_buflen: the buffer contains the
3647 * previously sent already encoded operation.
3648 *
3649 * called with nfs4_lock_state() held
3650 */
3651void
3652nfsd4_encode_replay(struct nfsd4_compoundres *resp, struct nfsd4_op *op)
3653{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003654 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655 struct nfs4_replay *rp = op->replay;
3656
3657 BUG_ON(!rp);
3658
3659 RESERVE_SPACE(8);
3660 WRITE32(op->opnum);
3661 *p++ = rp->rp_status; /* already xdr'ed */
3662 ADJUST_ARGS();
3663
3664 RESERVE_SPACE(rp->rp_buflen);
3665 WRITEMEM(rp->rp_buf, rp->rp_buflen);
3666 ADJUST_ARGS();
3667}
3668
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669int
Al Viro2ebbc012006-10-19 23:28:58 -07003670nfs4svc_encode_voidres(struct svc_rqst *rqstp, __be32 *p, void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671{
3672 return xdr_ressize_check(rqstp, p);
3673}
3674
J. Bruce Fields3e98abf2011-07-16 17:15:10 -04003675int nfsd4_release_compoundargs(void *rq, __be32 *p, void *resp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676{
J. Bruce Fields3e98abf2011-07-16 17:15:10 -04003677 struct svc_rqst *rqstp = rq;
3678 struct nfsd4_compoundargs *args = rqstp->rq_argp;
3679
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680 if (args->ops != args->iops) {
3681 kfree(args->ops);
3682 args->ops = args->iops;
3683 }
Jesper Juhlf99d49a2005-11-07 01:01:34 -08003684 kfree(args->tmpp);
3685 args->tmpp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686 while (args->to_free) {
3687 struct tmpbuf *tb = args->to_free;
3688 args->to_free = tb->next;
3689 tb->release(tb->buf);
3690 kfree(tb);
3691 }
J. Bruce Fields3e98abf2011-07-16 17:15:10 -04003692 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693}
3694
3695int
Al Viro2ebbc012006-10-19 23:28:58 -07003696nfs4svc_decode_compoundargs(struct svc_rqst *rqstp, __be32 *p, struct nfsd4_compoundargs *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698 args->p = p;
3699 args->end = rqstp->rq_arg.head[0].iov_base + rqstp->rq_arg.head[0].iov_len;
3700 args->pagelist = rqstp->rq_arg.pages;
3701 args->pagelen = rqstp->rq_arg.page_len;
3702 args->tmpp = NULL;
3703 args->to_free = NULL;
3704 args->ops = args->iops;
3705 args->rqstp = rqstp;
3706
J. Bruce Fields3e98abf2011-07-16 17:15:10 -04003707 return !nfsd4_decode_compound(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708}
3709
3710int
Al Viro2ebbc012006-10-19 23:28:58 -07003711nfs4svc_encode_compoundres(struct svc_rqst *rqstp, __be32 *p, struct nfsd4_compoundres *resp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712{
3713 /*
3714 * All that remains is to write the tag and operation count...
3715 */
Andy Adamson557ce262009-08-28 08:45:04 -04003716 struct nfsd4_compound_state *cs = &resp->cstate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717 struct kvec *iov;
3718 p = resp->tagp;
3719 *p++ = htonl(resp->taglen);
3720 memcpy(p, resp->tag, resp->taglen);
3721 p += XDR_QUADLEN(resp->taglen);
3722 *p++ = htonl(resp->opcnt);
3723
3724 if (rqstp->rq_res.page_len)
3725 iov = &rqstp->rq_res.tail[0];
3726 else
3727 iov = &rqstp->rq_res.head[0];
3728 iov->iov_len = ((char*)resp->p) - (char*)iov->iov_base;
3729 BUG_ON(iov->iov_len > PAGE_SIZE);
J. Bruce Fields26c0c752010-04-24 15:35:43 -04003730 if (nfsd4_has_session(cs)) {
3731 if (cs->status != nfserr_replay_cache) {
3732 nfsd4_store_cache_entry(resp);
J. Bruce Fields73e79482012-02-13 16:39:00 -05003733 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
J. Bruce Fields26c0c752010-04-24 15:35:43 -04003734 }
Benny Halevyd7682982010-05-12 00:13:54 +03003735 /* Renew the clientid on success and on replay */
J. Bruce Fields221a6872013-04-01 22:23:49 -04003736 put_client_renew(cs->session->se_client);
3737 nfsd4_put_session(cs->session);
Andy Adamsonda3846a2009-04-03 08:28:22 +03003738 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739 return 1;
3740}
3741
3742/*
3743 * Local variables:
3744 * c-basic-offset: 8
3745 * End:
3746 */