blob: b77f6bdd522ac2215f0e0465513f74a9ed276e13 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Server-side XDR for NFSv4
3 *
4 * Copyright (c) 2002 The Regents of the University of Michigan.
5 * All rights reserved.
6 *
7 * Kendrick Smith <kmsmith@umich.edu>
8 * Andy Adamson <andros@umich.edu>
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 *
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
24 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
30 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *
35 * TODO: Neil Brown made the following observation: We currently
36 * initially reserve NFSD_BUFSIZE space on the transmit queue and
37 * never release any of that until the request is complete.
38 * It would be good to calculate a new maximum response size while
39 * decoding the COMPOUND, and call svc_reserve with this number
40 * at the end of nfs4svc_decode_compoundargs.
41 */
42
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090043#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/namei.h>
Boaz Harrosh341eb182009-12-03 20:29:12 +020045#include <linux/statfs.h>
Andy Adamson0733d212009-04-03 08:28:01 +030046#include <linux/utsname.h>
Bryan Schumaker17456802011-07-13 10:50:48 -040047#include <linux/pagemap.h>
J. Bruce Fields4796f452007-07-17 04:04:51 -070048#include <linux/sunrpc/svcauth_gss.h>
Boaz Harrosh9a74af22009-12-03 20:30:56 +020049
J. Bruce Fields2ca72e12011-01-04 17:37:15 -050050#include "idmap.h"
51#include "acl.h"
Boaz Harrosh9a74af22009-12-03 20:30:56 +020052#include "xdr4.h"
J. Bruce Fields0a3adad2009-11-04 18:12:35 -050053#include "vfs.h"
Bryan Schumaker17456802011-07-13 10:50:48 -040054#include "state.h"
J. Bruce Fields10910062011-01-24 12:11:02 -050055#include "cache.h"
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +030056#include "netns.h"
J. Bruce Fields2ca72e12011-01-04 17:37:15 -050057
David Quigley18032ca2013-05-02 13:19:10 -040058#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
59#include <linux/security.h>
60#endif
61
62
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#define NFSDDBG_FACILITY NFSDDBG_XDR
64
J.Bruce Fields42ca0992006-10-04 02:16:20 -070065/*
66 * As per referral draft, the fsid for a referral MUST be different from the fsid of the containing
67 * directory in order to indicate to the client that a filesystem boundary is present
68 * We use a fixed fsid for a referral
69 */
70#define NFS4_REFERRAL_FSID_MAJOR 0x8000000ULL
71#define NFS4_REFERRAL_FSID_MINOR 0x8000000ULL
72
Al Virob37ad282006-10-19 23:28:59 -070073static __be32
J. Bruce Fieldsa36b1722012-11-25 16:31:00 -050074check_filename(char *str, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075{
76 int i;
77
78 if (len == 0)
79 return nfserr_inval;
80 if (isdotent(str, len))
J. Bruce Fieldsa36b1722012-11-25 16:31:00 -050081 return nfserr_badname;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 for (i = 0; i < len; i++)
83 if (str[i] == '/')
J. Bruce Fieldsa36b1722012-11-25 16:31:00 -050084 return nfserr_badname;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 return 0;
86}
87
Linus Torvalds1da177e2005-04-16 15:20:36 -070088#define DECODE_HEAD \
Al Viro2ebbc012006-10-19 23:28:58 -070089 __be32 *p; \
Al Virob37ad282006-10-19 23:28:59 -070090 __be32 status
Linus Torvalds1da177e2005-04-16 15:20:36 -070091#define DECODE_TAIL \
92 status = 0; \
93out: \
94 return status; \
95xdr_error: \
Chuck Lever817cb9d2007-09-11 18:01:20 -040096 dprintk("NFSD: xdr error (%s:%d)\n", \
97 __FILE__, __LINE__); \
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 status = nfserr_bad_xdr; \
99 goto out
100
101#define READ32(x) (x) = ntohl(*p++)
102#define READ64(x) do { \
103 (x) = (u64)ntohl(*p++) << 32; \
104 (x) |= ntohl(*p++); \
105} while (0)
106#define READTIME(x) do { \
107 p++; \
108 (x) = ntohl(*p++); \
109 p++; \
110} while (0)
111#define READMEM(x,nbytes) do { \
112 x = (char *)p; \
113 p += XDR_QUADLEN(nbytes); \
114} while (0)
115#define SAVEMEM(x,nbytes) do { \
116 if (!(x = (p==argp->tmp || p == argp->tmpp) ? \
117 savemem(argp, p, nbytes) : \
118 (char *)p)) { \
Chuck Lever817cb9d2007-09-11 18:01:20 -0400119 dprintk("NFSD: xdr error (%s:%d)\n", \
120 __FILE__, __LINE__); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 goto xdr_error; \
122 } \
123 p += XDR_QUADLEN(nbytes); \
124} while (0)
125#define COPYMEM(x,nbytes) do { \
126 memcpy((x), p, nbytes); \
127 p += XDR_QUADLEN(nbytes); \
128} while (0)
129
130/* READ_BUF, read_buf(): nbytes must be <= PAGE_SIZE */
131#define READ_BUF(nbytes) do { \
132 if (nbytes <= (u32)((char *)argp->end - (char *)argp->p)) { \
133 p = argp->p; \
134 argp->p += XDR_QUADLEN(nbytes); \
135 } else if (!(p = read_buf(argp, nbytes))) { \
Chuck Lever817cb9d2007-09-11 18:01:20 -0400136 dprintk("NFSD: xdr error (%s:%d)\n", \
137 __FILE__, __LINE__); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 goto xdr_error; \
139 } \
140} while (0)
141
J. Bruce Fields590b7432013-06-21 17:06:29 -0400142static void next_decode_page(struct nfsd4_compoundargs *argp)
143{
J. Bruce Fields590b7432013-06-21 17:06:29 -0400144 argp->p = page_address(argp->pagelist[0]);
J. Bruce Fields365da4a2013-11-19 17:32:43 -0500145 argp->pagelist++;
J. Bruce Fields590b7432013-06-21 17:06:29 -0400146 if (argp->pagelen < PAGE_SIZE) {
147 argp->end = argp->p + (argp->pagelen>>2);
148 argp->pagelen = 0;
149 } else {
150 argp->end = argp->p + (PAGE_SIZE>>2);
151 argp->pagelen -= PAGE_SIZE;
152 }
153}
154
J. Bruce Fieldsca2a05a2007-11-11 15:43:12 -0500155static __be32 *read_buf(struct nfsd4_compoundargs *argp, u32 nbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
157 /* We want more bytes than seem to be available.
158 * Maybe we need a new page, maybe we have just run out
159 */
J. Bruce Fieldsca2a05a2007-11-11 15:43:12 -0500160 unsigned int avail = (char *)argp->end - (char *)argp->p;
Al Viro2ebbc012006-10-19 23:28:58 -0700161 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 if (avail + argp->pagelen < nbytes)
163 return NULL;
164 if (avail + PAGE_SIZE < nbytes) /* need more than a page !! */
165 return NULL;
166 /* ok, we can do it with the current plus the next page */
167 if (nbytes <= sizeof(argp->tmp))
168 p = argp->tmp;
169 else {
Jesper Juhlf99d49a2005-11-07 01:01:34 -0800170 kfree(argp->tmpp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 p = argp->tmpp = kmalloc(nbytes, GFP_KERNEL);
172 if (!p)
173 return NULL;
174
175 }
J. Bruce Fieldsca2a05a2007-11-11 15:43:12 -0500176 /*
177 * The following memcpy is safe because read_buf is always
178 * called with nbytes > avail, and the two cases above both
179 * guarantee p points to at least nbytes bytes.
180 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 memcpy(p, argp->p, avail);
J. Bruce Fields590b7432013-06-21 17:06:29 -0400182 next_decode_page(argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 memcpy(((char*)p)+avail, argp->p, (nbytes - avail));
184 argp->p += XDR_QUADLEN(nbytes - avail);
185 return p;
186}
187
Andy Adamson60adfc52009-04-03 08:28:50 +0300188static int zero_clientid(clientid_t *clid)
189{
190 return (clid->cl_boot == 0) && (clid->cl_id == 0);
191}
192
Christoph Hellwig2d8498d2013-11-20 00:24:11 -0800193/**
194 * defer_free - mark an allocation as deferred freed
195 * @argp: NFSv4 compound argument structure to be freed with
196 * @release: release callback to free @p, typically kfree()
197 * @p: pointer to be freed
198 *
199 * Marks @p to be freed when processing the compound operation
200 * described in @argp finishes.
201 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202static int
203defer_free(struct nfsd4_compoundargs *argp,
204 void (*release)(const void *), void *p)
205{
206 struct tmpbuf *tb;
207
208 tb = kmalloc(sizeof(*tb), GFP_KERNEL);
209 if (!tb)
210 return -ENOMEM;
211 tb->buf = p;
212 tb->release = release;
213 tb->next = argp->to_free;
214 argp->to_free = tb;
215 return 0;
216}
217
Christoph Hellwig2d8498d2013-11-20 00:24:11 -0800218/**
219 * savemem - duplicate a chunk of memory for later processing
220 * @argp: NFSv4 compound argument structure to be freed with
221 * @p: pointer to be duplicated
222 * @nbytes: length to be duplicated
223 *
224 * Returns a pointer to a copy of @nbytes bytes of memory at @p
225 * that are preserved until processing of the NFSv4 compound
226 * operation described by @argp finishes.
227 */
Al Viro2ebbc012006-10-19 23:28:58 -0700228static char *savemem(struct nfsd4_compoundargs *argp, __be32 *p, int nbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 if (p == argp->tmp) {
Thomas Meyer67114fe2011-11-17 23:43:40 +0100231 p = kmemdup(argp->tmp, nbytes, GFP_KERNEL);
J. Bruce Fieldsa4db5fe2007-02-16 01:28:30 -0800232 if (!p)
233 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 } else {
Eric Sesterhenn73dff8b2006-10-03 23:37:14 +0200235 BUG_ON(p != argp->tmpp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 argp->tmpp = NULL;
237 }
238 if (defer_free(argp, kfree, p)) {
J. Bruce Fieldsa4db5fe2007-02-16 01:28:30 -0800239 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 return NULL;
241 } else
242 return (char *)p;
243}
244
Al Virob37ad282006-10-19 23:28:59 -0700245static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246nfsd4_decode_bitmap(struct nfsd4_compoundargs *argp, u32 *bmval)
247{
248 u32 bmlen;
249 DECODE_HEAD;
250
251 bmval[0] = 0;
252 bmval[1] = 0;
Andy Adamson7e705702009-04-03 08:29:11 +0300253 bmval[2] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
255 READ_BUF(4);
256 READ32(bmlen);
257 if (bmlen > 1000)
258 goto xdr_error;
259
260 READ_BUF(bmlen << 2);
261 if (bmlen > 0)
262 READ32(bmval[0]);
263 if (bmlen > 1)
264 READ32(bmval[1]);
Andy Adamson7e705702009-04-03 08:29:11 +0300265 if (bmlen > 2)
266 READ32(bmval[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
268 DECODE_TAIL;
269}
270
Al Virob37ad282006-10-19 23:28:59 -0700271static __be32
Yu Zhiguo3c8e0312009-05-16 16:22:31 +0800272nfsd4_decode_fattr(struct nfsd4_compoundargs *argp, u32 *bmval,
David Quigley18032ca2013-05-02 13:19:10 -0400273 struct iattr *iattr, struct nfs4_acl **acl,
274 struct xdr_netobj *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
276 int expected_len, len = 0;
277 u32 dummy32;
278 char *buf;
279
280 DECODE_HEAD;
281 iattr->ia_valid = 0;
282 if ((status = nfsd4_decode_bitmap(argp, bmval)))
283 return status;
284
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 READ_BUF(4);
286 READ32(expected_len);
287
288 if (bmval[0] & FATTR4_WORD0_SIZE) {
289 READ_BUF(8);
290 len += 8;
291 READ64(iattr->ia_size);
292 iattr->ia_valid |= ATTR_SIZE;
293 }
294 if (bmval[0] & FATTR4_WORD0_ACL) {
J. Bruce Fields64a817c2013-03-26 14:11:13 -0400295 u32 nace;
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800296 struct nfs4_ace *ace;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
298 READ_BUF(4); len += 4;
299 READ32(nace);
300
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800301 if (nace > NFS4_ACL_MAX)
302 return nfserr_resource;
303
304 *acl = nfs4_acl_new(nace);
Kinglong Meeeba1c992013-12-09 18:23:46 +0800305 if (*acl == NULL)
306 return nfserr_jukebox;
307
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800308 defer_free(argp, kfree, *acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800310 (*acl)->naces = nace;
311 for (ace = (*acl)->aces; ace < (*acl)->aces + nace; ace++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 READ_BUF(16); len += 16;
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800313 READ32(ace->type);
314 READ32(ace->flag);
315 READ32(ace->access_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 READ32(dummy32);
317 READ_BUF(dummy32);
318 len += XDR_QUADLEN(dummy32) << 2;
319 READMEM(buf, dummy32);
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800320 ace->whotype = nfs4_acl_get_whotype(buf, dummy32);
J. Bruce Fields3c726022011-01-04 17:53:52 -0500321 status = nfs_ok;
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800322 if (ace->whotype != NFS4_ACL_WHO_NAMED)
Eric W. Biedermanab8e4ae2013-02-02 05:18:08 -0800323 ;
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800324 else if (ace->flag & NFS4_ACE_IDENTIFIER_GROUP)
J. Bruce Fields3c726022011-01-04 17:53:52 -0500325 status = nfsd_map_name_to_gid(argp->rqstp,
Eric W. Biedermanab8e4ae2013-02-02 05:18:08 -0800326 buf, dummy32, &ace->who_gid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 else
J. Bruce Fields3c726022011-01-04 17:53:52 -0500328 status = nfsd_map_name_to_uid(argp->rqstp,
Eric W. Biedermanab8e4ae2013-02-02 05:18:08 -0800329 buf, dummy32, &ace->who_uid);
J. Bruce Fields3c726022011-01-04 17:53:52 -0500330 if (status)
331 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 }
333 } else
334 *acl = NULL;
335 if (bmval[1] & FATTR4_WORD1_MODE) {
336 READ_BUF(4);
337 len += 4;
338 READ32(iattr->ia_mode);
339 iattr->ia_mode &= (S_IFMT | S_IALLUGO);
340 iattr->ia_valid |= ATTR_MODE;
341 }
342 if (bmval[1] & FATTR4_WORD1_OWNER) {
343 READ_BUF(4);
344 len += 4;
345 READ32(dummy32);
346 READ_BUF(dummy32);
347 len += (XDR_QUADLEN(dummy32) << 2);
348 READMEM(buf, dummy32);
NeilBrown47c85292011-02-16 13:08:35 +1100349 if ((status = nfsd_map_name_to_uid(argp->rqstp, buf, dummy32, &iattr->ia_uid)))
350 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 iattr->ia_valid |= ATTR_UID;
352 }
353 if (bmval[1] & FATTR4_WORD1_OWNER_GROUP) {
354 READ_BUF(4);
355 len += 4;
356 READ32(dummy32);
357 READ_BUF(dummy32);
358 len += (XDR_QUADLEN(dummy32) << 2);
359 READMEM(buf, dummy32);
NeilBrown47c85292011-02-16 13:08:35 +1100360 if ((status = nfsd_map_name_to_gid(argp->rqstp, buf, dummy32, &iattr->ia_gid)))
361 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 iattr->ia_valid |= ATTR_GID;
363 }
364 if (bmval[1] & FATTR4_WORD1_TIME_ACCESS_SET) {
365 READ_BUF(4);
366 len += 4;
367 READ32(dummy32);
368 switch (dummy32) {
369 case NFS4_SET_TO_CLIENT_TIME:
370 /* We require the high 32 bits of 'seconds' to be 0, and we ignore
371 all 32 bits of 'nseconds'. */
372 READ_BUF(12);
373 len += 12;
Bryan Schumakerbf8d9092013-04-19 16:09:38 -0400374 READ64(iattr->ia_atime.tv_sec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 READ32(iattr->ia_atime.tv_nsec);
376 if (iattr->ia_atime.tv_nsec >= (u32)1000000000)
377 return nfserr_inval;
378 iattr->ia_valid |= (ATTR_ATIME | ATTR_ATIME_SET);
379 break;
380 case NFS4_SET_TO_SERVER_TIME:
381 iattr->ia_valid |= ATTR_ATIME;
382 break;
383 default:
384 goto xdr_error;
385 }
386 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 if (bmval[1] & FATTR4_WORD1_TIME_MODIFY_SET) {
388 READ_BUF(4);
389 len += 4;
390 READ32(dummy32);
391 switch (dummy32) {
392 case NFS4_SET_TO_CLIENT_TIME:
393 /* We require the high 32 bits of 'seconds' to be 0, and we ignore
394 all 32 bits of 'nseconds'. */
395 READ_BUF(12);
396 len += 12;
Bryan Schumakerbf8d9092013-04-19 16:09:38 -0400397 READ64(iattr->ia_mtime.tv_sec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 READ32(iattr->ia_mtime.tv_nsec);
399 if (iattr->ia_mtime.tv_nsec >= (u32)1000000000)
400 return nfserr_inval;
401 iattr->ia_valid |= (ATTR_MTIME | ATTR_MTIME_SET);
402 break;
403 case NFS4_SET_TO_SERVER_TIME:
404 iattr->ia_valid |= ATTR_MTIME;
405 break;
406 default:
407 goto xdr_error;
408 }
409 }
David Quigley18032ca2013-05-02 13:19:10 -0400410
411 label->len = 0;
412#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
413 if (bmval[2] & FATTR4_WORD2_SECURITY_LABEL) {
414 READ_BUF(4);
415 len += 4;
416 READ32(dummy32); /* lfs: we don't use it */
417 READ_BUF(4);
418 len += 4;
419 READ32(dummy32); /* pi: we don't use it either */
420 READ_BUF(4);
421 len += 4;
422 READ32(dummy32);
423 READ_BUF(dummy32);
424 if (dummy32 > NFSD4_MAX_SEC_LABEL_LEN)
425 return nfserr_badlabel;
426 len += (XDR_QUADLEN(dummy32) << 2);
427 READMEM(buf, dummy32);
428 label->data = kzalloc(dummy32 + 1, GFP_KERNEL);
429 if (!label->data)
430 return nfserr_jukebox;
J. Bruce Fields3378b7f2013-11-01 10:42:15 -0400431 label->len = dummy32;
David Quigley18032ca2013-05-02 13:19:10 -0400432 defer_free(argp, kfree, label->data);
433 memcpy(label->data, buf, dummy32);
434 }
435#endif
436
Yu Zhiguo3c8e0312009-05-16 16:22:31 +0800437 if (bmval[0] & ~NFSD_WRITEABLE_ATTRS_WORD0
438 || bmval[1] & ~NFSD_WRITEABLE_ATTRS_WORD1
439 || bmval[2] & ~NFSD_WRITEABLE_ATTRS_WORD2)
440 READ_BUF(expected_len - len);
441 else if (len != expected_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 goto xdr_error;
443
444 DECODE_TAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445}
446
Al Virob37ad282006-10-19 23:28:59 -0700447static __be32
Benny Halevye31a1b62008-08-12 20:46:18 +0300448nfsd4_decode_stateid(struct nfsd4_compoundargs *argp, stateid_t *sid)
449{
450 DECODE_HEAD;
451
452 READ_BUF(sizeof(stateid_t));
453 READ32(sid->si_generation);
454 COPYMEM(&sid->si_opaque, sizeof(stateid_opaque_t));
455
456 DECODE_TAIL;
457}
458
459static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460nfsd4_decode_access(struct nfsd4_compoundargs *argp, struct nfsd4_access *access)
461{
462 DECODE_HEAD;
463
464 READ_BUF(4);
465 READ32(access->ac_req_access);
466
467 DECODE_TAIL;
468}
469
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400470static __be32 nfsd4_decode_cb_sec(struct nfsd4_compoundargs *argp, struct nfsd4_cb_sec *cbs)
471{
472 DECODE_HEAD;
J. Bruce Fields12fc3e92012-11-05 16:01:48 -0500473 u32 dummy, uid, gid;
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400474 char *machine_name;
475 int i;
476 int nr_secflavs;
477
478 /* callback_sec_params4 */
479 READ_BUF(4);
480 READ32(nr_secflavs);
J. Bruce Fields57569a72013-05-17 16:25:32 -0400481 if (nr_secflavs)
482 cbs->flavor = (u32)(-1);
483 else
484 /* Is this legal? Be generous, take it to mean AUTH_NONE: */
485 cbs->flavor = 0;
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400486 for (i = 0; i < nr_secflavs; ++i) {
487 READ_BUF(4);
488 READ32(dummy);
489 switch (dummy) {
490 case RPC_AUTH_NULL:
491 /* Nothing to read */
J. Bruce Fields12fc3e92012-11-05 16:01:48 -0500492 if (cbs->flavor == (u32)(-1))
493 cbs->flavor = RPC_AUTH_NULL;
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400494 break;
495 case RPC_AUTH_UNIX:
496 READ_BUF(8);
497 /* stamp */
498 READ32(dummy);
499
500 /* machine name */
501 READ32(dummy);
502 READ_BUF(dummy);
503 SAVEMEM(machine_name, dummy);
504
505 /* uid, gid */
506 READ_BUF(8);
J. Bruce Fields12fc3e92012-11-05 16:01:48 -0500507 READ32(uid);
508 READ32(gid);
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400509
510 /* more gids */
511 READ_BUF(4);
512 READ32(dummy);
513 READ_BUF(dummy * 4);
J. Bruce Fields12fc3e92012-11-05 16:01:48 -0500514 if (cbs->flavor == (u32)(-1)) {
Eric W. Biederman03bc6d12013-02-02 06:24:49 -0800515 kuid_t kuid = make_kuid(&init_user_ns, uid);
516 kgid_t kgid = make_kgid(&init_user_ns, gid);
517 if (uid_valid(kuid) && gid_valid(kgid)) {
518 cbs->uid = kuid;
519 cbs->gid = kgid;
520 cbs->flavor = RPC_AUTH_UNIX;
521 } else {
522 dprintk("RPC_AUTH_UNIX with invalid"
523 "uid or gid ignoring!\n");
524 }
J. Bruce Fields12fc3e92012-11-05 16:01:48 -0500525 }
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400526 break;
527 case RPC_AUTH_GSS:
528 dprintk("RPC_AUTH_GSS callback secflavor "
529 "not supported!\n");
530 READ_BUF(8);
531 /* gcbp_service */
532 READ32(dummy);
533 /* gcbp_handle_from_server */
534 READ32(dummy);
535 READ_BUF(dummy);
536 p += XDR_QUADLEN(dummy);
537 /* gcbp_handle_from_client */
538 READ_BUF(4);
539 READ32(dummy);
540 READ_BUF(dummy);
541 break;
542 default:
543 dprintk("Illegal callback secflavor\n");
544 return nfserr_inval;
545 }
546 }
547 DECODE_TAIL;
548}
549
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -0400550static __be32 nfsd4_decode_backchannel_ctl(struct nfsd4_compoundargs *argp, struct nfsd4_backchannel_ctl *bc)
551{
552 DECODE_HEAD;
553
554 READ_BUF(4);
555 READ32(bc->bc_cb_program);
556 nfsd4_decode_cb_sec(argp, &bc->bc_cb_sec);
557
558 DECODE_TAIL;
559}
560
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -0400561static __be32 nfsd4_decode_bind_conn_to_session(struct nfsd4_compoundargs *argp, struct nfsd4_bind_conn_to_session *bcts)
562{
563 DECODE_HEAD;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -0400564
565 READ_BUF(NFS4_MAX_SESSIONID_LEN + 8);
566 COPYMEM(bcts->sessionid.data, NFS4_MAX_SESSIONID_LEN);
567 READ32(bcts->dir);
Bryan Schumaker6ce23572011-04-27 15:47:16 -0400568 /* XXX: skipping ctsa_use_conn_in_rdma_mode. Perhaps Tom Tucker
569 * could help us figure out we should be using it. */
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -0400570 DECODE_TAIL;
571}
572
Al Virob37ad282006-10-19 23:28:59 -0700573static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574nfsd4_decode_close(struct nfsd4_compoundargs *argp, struct nfsd4_close *close)
575{
576 DECODE_HEAD;
577
Benny Halevye31a1b62008-08-12 20:46:18 +0300578 READ_BUF(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 READ32(close->cl_seqid);
Benny Halevye31a1b62008-08-12 20:46:18 +0300580 return nfsd4_decode_stateid(argp, &close->cl_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
582 DECODE_TAIL;
583}
584
585
Al Virob37ad282006-10-19 23:28:59 -0700586static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587nfsd4_decode_commit(struct nfsd4_compoundargs *argp, struct nfsd4_commit *commit)
588{
589 DECODE_HEAD;
590
591 READ_BUF(12);
592 READ64(commit->co_offset);
593 READ32(commit->co_count);
594
595 DECODE_TAIL;
596}
597
Al Virob37ad282006-10-19 23:28:59 -0700598static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599nfsd4_decode_create(struct nfsd4_compoundargs *argp, struct nfsd4_create *create)
600{
601 DECODE_HEAD;
602
603 READ_BUF(4);
604 READ32(create->cr_type);
605 switch (create->cr_type) {
606 case NF4LNK:
607 READ_BUF(4);
608 READ32(create->cr_linklen);
609 READ_BUF(create->cr_linklen);
610 SAVEMEM(create->cr_linkname, create->cr_linklen);
611 break;
612 case NF4BLK:
613 case NF4CHR:
614 READ_BUF(8);
615 READ32(create->cr_specdata1);
616 READ32(create->cr_specdata2);
617 break;
618 case NF4SOCK:
619 case NF4FIFO:
620 case NF4DIR:
621 default:
622 break;
623 }
624
625 READ_BUF(4);
626 READ32(create->cr_namelen);
627 READ_BUF(create->cr_namelen);
628 SAVEMEM(create->cr_name, create->cr_namelen);
J. Bruce Fieldsa36b1722012-11-25 16:31:00 -0500629 if ((status = check_filename(create->cr_name, create->cr_namelen)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 return status;
631
Yu Zhiguo3c8e0312009-05-16 16:22:31 +0800632 status = nfsd4_decode_fattr(argp, create->cr_bmval, &create->cr_iattr,
David Quigley18032ca2013-05-02 13:19:10 -0400633 &create->cr_acl, &create->cr_label);
Benny Halevyc0d6fc82009-04-03 08:29:05 +0300634 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 goto out;
636
637 DECODE_TAIL;
638}
639
Al Virob37ad282006-10-19 23:28:59 -0700640static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641nfsd4_decode_delegreturn(struct nfsd4_compoundargs *argp, struct nfsd4_delegreturn *dr)
642{
Benny Halevye31a1b62008-08-12 20:46:18 +0300643 return nfsd4_decode_stateid(argp, &dr->dr_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644}
645
Al Virob37ad282006-10-19 23:28:59 -0700646static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647nfsd4_decode_getattr(struct nfsd4_compoundargs *argp, struct nfsd4_getattr *getattr)
648{
649 return nfsd4_decode_bitmap(argp, getattr->ga_bmval);
650}
651
Al Virob37ad282006-10-19 23:28:59 -0700652static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653nfsd4_decode_link(struct nfsd4_compoundargs *argp, struct nfsd4_link *link)
654{
655 DECODE_HEAD;
656
657 READ_BUF(4);
658 READ32(link->li_namelen);
659 READ_BUF(link->li_namelen);
660 SAVEMEM(link->li_name, link->li_namelen);
J. Bruce Fieldsa36b1722012-11-25 16:31:00 -0500661 if ((status = check_filename(link->li_name, link->li_namelen)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 return status;
663
664 DECODE_TAIL;
665}
666
Al Virob37ad282006-10-19 23:28:59 -0700667static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668nfsd4_decode_lock(struct nfsd4_compoundargs *argp, struct nfsd4_lock *lock)
669{
670 DECODE_HEAD;
671
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 /*
673 * type, reclaim(boolean), offset, length, new_lock_owner(boolean)
674 */
675 READ_BUF(28);
676 READ32(lock->lk_type);
677 if ((lock->lk_type < NFS4_READ_LT) || (lock->lk_type > NFS4_WRITEW_LT))
678 goto xdr_error;
679 READ32(lock->lk_reclaim);
680 READ64(lock->lk_offset);
681 READ64(lock->lk_length);
682 READ32(lock->lk_is_new);
683
684 if (lock->lk_is_new) {
Benny Halevye31a1b62008-08-12 20:46:18 +0300685 READ_BUF(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 READ32(lock->lk_new_open_seqid);
Benny Halevye31a1b62008-08-12 20:46:18 +0300687 status = nfsd4_decode_stateid(argp, &lock->lk_new_open_stateid);
688 if (status)
689 return status;
690 READ_BUF(8 + sizeof(clientid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 READ32(lock->lk_new_lock_seqid);
692 COPYMEM(&lock->lk_new_clientid, sizeof(clientid_t));
693 READ32(lock->lk_new_owner.len);
694 READ_BUF(lock->lk_new_owner.len);
695 READMEM(lock->lk_new_owner.data, lock->lk_new_owner.len);
696 } else {
Benny Halevye31a1b62008-08-12 20:46:18 +0300697 status = nfsd4_decode_stateid(argp, &lock->lk_old_lock_stateid);
698 if (status)
699 return status;
700 READ_BUF(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 READ32(lock->lk_old_lock_seqid);
702 }
703
704 DECODE_TAIL;
705}
706
Al Virob37ad282006-10-19 23:28:59 -0700707static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708nfsd4_decode_lockt(struct nfsd4_compoundargs *argp, struct nfsd4_lockt *lockt)
709{
710 DECODE_HEAD;
711
712 READ_BUF(32);
713 READ32(lockt->lt_type);
714 if((lockt->lt_type < NFS4_READ_LT) || (lockt->lt_type > NFS4_WRITEW_LT))
715 goto xdr_error;
716 READ64(lockt->lt_offset);
717 READ64(lockt->lt_length);
718 COPYMEM(&lockt->lt_clientid, 8);
719 READ32(lockt->lt_owner.len);
720 READ_BUF(lockt->lt_owner.len);
721 READMEM(lockt->lt_owner.data, lockt->lt_owner.len);
722
723 DECODE_TAIL;
724}
725
Al Virob37ad282006-10-19 23:28:59 -0700726static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727nfsd4_decode_locku(struct nfsd4_compoundargs *argp, struct nfsd4_locku *locku)
728{
729 DECODE_HEAD;
730
Benny Halevye31a1b62008-08-12 20:46:18 +0300731 READ_BUF(8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 READ32(locku->lu_type);
733 if ((locku->lu_type < NFS4_READ_LT) || (locku->lu_type > NFS4_WRITEW_LT))
734 goto xdr_error;
735 READ32(locku->lu_seqid);
Benny Halevye31a1b62008-08-12 20:46:18 +0300736 status = nfsd4_decode_stateid(argp, &locku->lu_stateid);
737 if (status)
738 return status;
739 READ_BUF(16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 READ64(locku->lu_offset);
741 READ64(locku->lu_length);
742
743 DECODE_TAIL;
744}
745
Al Virob37ad282006-10-19 23:28:59 -0700746static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747nfsd4_decode_lookup(struct nfsd4_compoundargs *argp, struct nfsd4_lookup *lookup)
748{
749 DECODE_HEAD;
750
751 READ_BUF(4);
752 READ32(lookup->lo_len);
753 READ_BUF(lookup->lo_len);
754 SAVEMEM(lookup->lo_name, lookup->lo_len);
J. Bruce Fieldsa36b1722012-11-25 16:31:00 -0500755 if ((status = check_filename(lookup->lo_name, lookup->lo_len)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 return status;
757
758 DECODE_TAIL;
759}
760
Benny Halevy2c8bd7e2012-02-16 20:57:09 +0200761static __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 -0400762{
763 __be32 *p;
764 u32 w;
765
766 READ_BUF(4);
767 READ32(w);
Benny Halevy2c8bd7e2012-02-16 20:57:09 +0200768 *share_access = w & NFS4_SHARE_ACCESS_MASK;
769 *deleg_want = w & NFS4_SHARE_WANT_MASK;
770 if (deleg_when)
771 *deleg_when = w & NFS4_SHARE_WHEN_MASK;
772
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400773 switch (w & NFS4_SHARE_ACCESS_MASK) {
774 case NFS4_SHARE_ACCESS_READ:
775 case NFS4_SHARE_ACCESS_WRITE:
776 case NFS4_SHARE_ACCESS_BOTH:
777 break;
778 default:
779 return nfserr_bad_xdr;
780 }
Benny Halevyfc0d14f2011-10-27 20:43:01 +0200781 w &= ~NFS4_SHARE_ACCESS_MASK;
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400782 if (!w)
783 return nfs_ok;
784 if (!argp->minorversion)
785 return nfserr_bad_xdr;
786 switch (w & NFS4_SHARE_WANT_MASK) {
787 case NFS4_SHARE_WANT_NO_PREFERENCE:
788 case NFS4_SHARE_WANT_READ_DELEG:
789 case NFS4_SHARE_WANT_WRITE_DELEG:
790 case NFS4_SHARE_WANT_ANY_DELEG:
791 case NFS4_SHARE_WANT_NO_DELEG:
792 case NFS4_SHARE_WANT_CANCEL:
793 break;
794 default:
795 return nfserr_bad_xdr;
796 }
Benny Halevy92bac8c2011-10-19 19:13:29 -0700797 w &= ~NFS4_SHARE_WANT_MASK;
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400798 if (!w)
799 return nfs_ok;
Benny Halevy2c8bd7e2012-02-16 20:57:09 +0200800
801 if (!deleg_when) /* open_downgrade */
802 return nfserr_inval;
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400803 switch (w) {
804 case NFS4_SHARE_SIGNAL_DELEG_WHEN_RESRC_AVAIL:
805 case NFS4_SHARE_PUSH_DELEG_WHEN_UNCONTENDED:
Benny Halevyc668fc62011-10-19 19:13:13 -0700806 case (NFS4_SHARE_SIGNAL_DELEG_WHEN_RESRC_AVAIL |
807 NFS4_SHARE_PUSH_DELEG_WHEN_UNCONTENDED):
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400808 return nfs_ok;
809 }
810xdr_error:
811 return nfserr_bad_xdr;
812}
813
814static __be32 nfsd4_decode_share_deny(struct nfsd4_compoundargs *argp, u32 *x)
815{
816 __be32 *p;
817
818 READ_BUF(4);
819 READ32(*x);
820 /* Note: unlinke access bits, deny bits may be zero. */
Dan Carpenter01cd4af2011-10-17 10:41:17 +0300821 if (*x & ~NFS4_SHARE_DENY_BOTH)
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400822 return nfserr_bad_xdr;
823 return nfs_ok;
824xdr_error:
825 return nfserr_bad_xdr;
826}
827
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -0400828static __be32 nfsd4_decode_opaque(struct nfsd4_compoundargs *argp, struct xdr_netobj *o)
829{
830 __be32 *p;
831
832 READ_BUF(4);
833 READ32(o->len);
834
835 if (o->len == 0 || o->len > NFS4_OPAQUE_LIMIT)
836 return nfserr_bad_xdr;
837
838 READ_BUF(o->len);
839 SAVEMEM(o->data, o->len);
840 return nfs_ok;
841xdr_error:
842 return nfserr_bad_xdr;
843}
844
Al Virob37ad282006-10-19 23:28:59 -0700845static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846nfsd4_decode_open(struct nfsd4_compoundargs *argp, struct nfsd4_open *open)
847{
848 DECODE_HEAD;
Benny Halevy2c8bd7e2012-02-16 20:57:09 +0200849 u32 dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
851 memset(open->op_bmval, 0, sizeof(open->op_bmval));
852 open->op_iattr.ia_valid = 0;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400853 open->op_openowner = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
J. Bruce Fields9d313b12013-02-28 12:51:49 -0800855 open->op_xdr_error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 /* seqid, share_access, share_deny, clientid, ownerlen */
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400857 READ_BUF(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 READ32(open->op_seqid);
Benny Halevy2c8bd7e2012-02-16 20:57:09 +0200859 /* decode, yet ignore deleg_when until supported */
860 status = nfsd4_decode_share_access(argp, &open->op_share_access,
861 &open->op_deleg_want, &dummy);
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400862 if (status)
863 goto xdr_error;
864 status = nfsd4_decode_share_deny(argp, &open->op_share_deny);
865 if (status)
866 goto xdr_error;
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -0400867 READ_BUF(sizeof(clientid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 COPYMEM(&open->op_clientid, sizeof(clientid_t));
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -0400869 status = nfsd4_decode_opaque(argp, &open->op_owner);
870 if (status)
871 goto xdr_error;
872 READ_BUF(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 READ32(open->op_create);
874 switch (open->op_create) {
875 case NFS4_OPEN_NOCREATE:
876 break;
877 case NFS4_OPEN_CREATE:
878 READ_BUF(4);
879 READ32(open->op_createmode);
880 switch (open->op_createmode) {
881 case NFS4_CREATE_UNCHECKED:
882 case NFS4_CREATE_GUARDED:
Benny Halevyc0d6fc82009-04-03 08:29:05 +0300883 status = nfsd4_decode_fattr(argp, open->op_bmval,
David Quigley18032ca2013-05-02 13:19:10 -0400884 &open->op_iattr, &open->op_acl, &open->op_label);
Benny Halevyc0d6fc82009-04-03 08:29:05 +0300885 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 goto out;
887 break;
888 case NFS4_CREATE_EXCLUSIVE:
Chuck Leverab4684d2012-03-02 17:13:50 -0500889 READ_BUF(NFS4_VERIFIER_SIZE);
890 COPYMEM(open->op_verf.data, NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 break;
Benny Halevy79fb54a2009-04-03 08:29:17 +0300892 case NFS4_CREATE_EXCLUSIVE4_1:
893 if (argp->minorversion < 1)
894 goto xdr_error;
Chuck Leverab4684d2012-03-02 17:13:50 -0500895 READ_BUF(NFS4_VERIFIER_SIZE);
896 COPYMEM(open->op_verf.data, NFS4_VERIFIER_SIZE);
Benny Halevy79fb54a2009-04-03 08:29:17 +0300897 status = nfsd4_decode_fattr(argp, open->op_bmval,
David Quigley18032ca2013-05-02 13:19:10 -0400898 &open->op_iattr, &open->op_acl, &open->op_label);
Benny Halevy79fb54a2009-04-03 08:29:17 +0300899 if (status)
900 goto out;
901 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 default:
903 goto xdr_error;
904 }
905 break;
906 default:
907 goto xdr_error;
908 }
909
910 /* open_claim */
911 READ_BUF(4);
912 READ32(open->op_claim_type);
913 switch (open->op_claim_type) {
914 case NFS4_OPEN_CLAIM_NULL:
915 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
916 READ_BUF(4);
917 READ32(open->op_fname.len);
918 READ_BUF(open->op_fname.len);
919 SAVEMEM(open->op_fname.data, open->op_fname.len);
J. Bruce Fieldsa36b1722012-11-25 16:31:00 -0500920 if ((status = check_filename(open->op_fname.data, open->op_fname.len)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 return status;
922 break;
923 case NFS4_OPEN_CLAIM_PREVIOUS:
924 READ_BUF(4);
925 READ32(open->op_delegate_type);
926 break;
927 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
Benny Halevye31a1b62008-08-12 20:46:18 +0300928 status = nfsd4_decode_stateid(argp, &open->op_delegate_stateid);
929 if (status)
930 return status;
931 READ_BUF(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 READ32(open->op_fname.len);
933 READ_BUF(open->op_fname.len);
934 SAVEMEM(open->op_fname.data, open->op_fname.len);
J. Bruce Fieldsa36b1722012-11-25 16:31:00 -0500935 if ((status = check_filename(open->op_fname.data, open->op_fname.len)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 return status;
937 break;
J. Bruce Fields8b289b22011-10-19 11:52:12 -0400938 case NFS4_OPEN_CLAIM_FH:
939 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
940 if (argp->minorversion < 1)
941 goto xdr_error;
942 /* void */
943 break;
944 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
945 if (argp->minorversion < 1)
946 goto xdr_error;
947 status = nfsd4_decode_stateid(argp, &open->op_delegate_stateid);
948 if (status)
949 return status;
950 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 default:
952 goto xdr_error;
953 }
954
955 DECODE_TAIL;
956}
957
Al Virob37ad282006-10-19 23:28:59 -0700958static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959nfsd4_decode_open_confirm(struct nfsd4_compoundargs *argp, struct nfsd4_open_confirm *open_conf)
960{
961 DECODE_HEAD;
Anna Schumakere1a90eb2013-10-30 10:00:20 -0400962
963 if (argp->minorversion >= 1)
964 return nfserr_notsupp;
965
Benny Halevye31a1b62008-08-12 20:46:18 +0300966 status = nfsd4_decode_stateid(argp, &open_conf->oc_req_stateid);
967 if (status)
968 return status;
969 READ_BUF(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 READ32(open_conf->oc_seqid);
Anna Schumakere1a90eb2013-10-30 10:00:20 -0400971
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 DECODE_TAIL;
973}
974
Al Virob37ad282006-10-19 23:28:59 -0700975static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976nfsd4_decode_open_downgrade(struct nfsd4_compoundargs *argp, struct nfsd4_open_downgrade *open_down)
977{
978 DECODE_HEAD;
979
Benny Halevye31a1b62008-08-12 20:46:18 +0300980 status = nfsd4_decode_stateid(argp, &open_down->od_stateid);
981 if (status)
982 return status;
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400983 READ_BUF(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 READ32(open_down->od_seqid);
Benny Halevy2c8bd7e2012-02-16 20:57:09 +0200985 status = nfsd4_decode_share_access(argp, &open_down->od_share_access,
986 &open_down->od_deleg_want, NULL);
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400987 if (status)
988 return status;
989 status = nfsd4_decode_share_deny(argp, &open_down->od_share_deny);
990 if (status)
991 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 DECODE_TAIL;
993}
994
Al Virob37ad282006-10-19 23:28:59 -0700995static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996nfsd4_decode_putfh(struct nfsd4_compoundargs *argp, struct nfsd4_putfh *putfh)
997{
998 DECODE_HEAD;
999
1000 READ_BUF(4);
1001 READ32(putfh->pf_fhlen);
1002 if (putfh->pf_fhlen > NFS4_FHSIZE)
1003 goto xdr_error;
1004 READ_BUF(putfh->pf_fhlen);
1005 SAVEMEM(putfh->pf_fhval, putfh->pf_fhlen);
1006
1007 DECODE_TAIL;
1008}
1009
Al Virob37ad282006-10-19 23:28:59 -07001010static __be32
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001011nfsd4_decode_putpubfh(struct nfsd4_compoundargs *argp, void *p)
1012{
1013 if (argp->minorversion == 0)
1014 return nfs_ok;
1015 return nfserr_notsupp;
1016}
1017
1018static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019nfsd4_decode_read(struct nfsd4_compoundargs *argp, struct nfsd4_read *read)
1020{
1021 DECODE_HEAD;
1022
Benny Halevye31a1b62008-08-12 20:46:18 +03001023 status = nfsd4_decode_stateid(argp, &read->rd_stateid);
1024 if (status)
1025 return status;
1026 READ_BUF(12);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 READ64(read->rd_offset);
1028 READ32(read->rd_length);
1029
1030 DECODE_TAIL;
1031}
1032
Al Virob37ad282006-10-19 23:28:59 -07001033static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034nfsd4_decode_readdir(struct nfsd4_compoundargs *argp, struct nfsd4_readdir *readdir)
1035{
1036 DECODE_HEAD;
1037
1038 READ_BUF(24);
1039 READ64(readdir->rd_cookie);
1040 COPYMEM(readdir->rd_verf.data, sizeof(readdir->rd_verf.data));
1041 READ32(readdir->rd_dircount); /* just in case you needed a useless field... */
1042 READ32(readdir->rd_maxcount);
1043 if ((status = nfsd4_decode_bitmap(argp, readdir->rd_bmval)))
1044 goto out;
1045
1046 DECODE_TAIL;
1047}
1048
Al Virob37ad282006-10-19 23:28:59 -07001049static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050nfsd4_decode_remove(struct nfsd4_compoundargs *argp, struct nfsd4_remove *remove)
1051{
1052 DECODE_HEAD;
1053
1054 READ_BUF(4);
1055 READ32(remove->rm_namelen);
1056 READ_BUF(remove->rm_namelen);
1057 SAVEMEM(remove->rm_name, remove->rm_namelen);
J. Bruce Fieldsa36b1722012-11-25 16:31:00 -05001058 if ((status = check_filename(remove->rm_name, remove->rm_namelen)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 return status;
1060
1061 DECODE_TAIL;
1062}
1063
Al Virob37ad282006-10-19 23:28:59 -07001064static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065nfsd4_decode_rename(struct nfsd4_compoundargs *argp, struct nfsd4_rename *rename)
1066{
1067 DECODE_HEAD;
1068
1069 READ_BUF(4);
1070 READ32(rename->rn_snamelen);
1071 READ_BUF(rename->rn_snamelen + 4);
1072 SAVEMEM(rename->rn_sname, rename->rn_snamelen);
1073 READ32(rename->rn_tnamelen);
1074 READ_BUF(rename->rn_tnamelen);
1075 SAVEMEM(rename->rn_tname, rename->rn_tnamelen);
J. Bruce Fieldsa36b1722012-11-25 16:31:00 -05001076 if ((status = check_filename(rename->rn_sname, rename->rn_snamelen)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 return status;
J. Bruce Fieldsa36b1722012-11-25 16:31:00 -05001078 if ((status = check_filename(rename->rn_tname, rename->rn_tnamelen)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 return status;
1080
1081 DECODE_TAIL;
1082}
1083
Al Virob37ad282006-10-19 23:28:59 -07001084static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085nfsd4_decode_renew(struct nfsd4_compoundargs *argp, clientid_t *clientid)
1086{
1087 DECODE_HEAD;
1088
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001089 if (argp->minorversion >= 1)
1090 return nfserr_notsupp;
1091
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 READ_BUF(sizeof(clientid_t));
1093 COPYMEM(clientid, sizeof(clientid_t));
1094
1095 DECODE_TAIL;
1096}
1097
Al Virob37ad282006-10-19 23:28:59 -07001098static __be32
Andy Adamsondcb488a32007-07-17 04:04:51 -07001099nfsd4_decode_secinfo(struct nfsd4_compoundargs *argp,
1100 struct nfsd4_secinfo *secinfo)
1101{
1102 DECODE_HEAD;
1103
1104 READ_BUF(4);
1105 READ32(secinfo->si_namelen);
1106 READ_BUF(secinfo->si_namelen);
1107 SAVEMEM(secinfo->si_name, secinfo->si_namelen);
J. Bruce Fieldsa36b1722012-11-25 16:31:00 -05001108 status = check_filename(secinfo->si_name, secinfo->si_namelen);
Andy Adamsondcb488a32007-07-17 04:04:51 -07001109 if (status)
1110 return status;
1111 DECODE_TAIL;
1112}
1113
1114static __be32
J. Bruce Fields04f4ad12010-12-16 09:51:13 -05001115nfsd4_decode_secinfo_no_name(struct nfsd4_compoundargs *argp,
1116 struct nfsd4_secinfo_no_name *sin)
1117{
1118 DECODE_HEAD;
1119
1120 READ_BUF(4);
1121 READ32(sin->sin_style);
1122 DECODE_TAIL;
1123}
1124
1125static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126nfsd4_decode_setattr(struct nfsd4_compoundargs *argp, struct nfsd4_setattr *setattr)
1127{
Benny Halevye31a1b62008-08-12 20:46:18 +03001128 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
Benny Halevye31a1b62008-08-12 20:46:18 +03001130 status = nfsd4_decode_stateid(argp, &setattr->sa_stateid);
1131 if (status)
1132 return status;
Yu Zhiguo3c8e0312009-05-16 16:22:31 +08001133 return nfsd4_decode_fattr(argp, setattr->sa_bmval, &setattr->sa_iattr,
David Quigley18032ca2013-05-02 13:19:10 -04001134 &setattr->sa_acl, &setattr->sa_label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135}
1136
Al Virob37ad282006-10-19 23:28:59 -07001137static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138nfsd4_decode_setclientid(struct nfsd4_compoundargs *argp, struct nfsd4_setclientid *setclientid)
1139{
1140 DECODE_HEAD;
1141
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001142 if (argp->minorversion >= 1)
1143 return nfserr_notsupp;
1144
Chuck Leverab4684d2012-03-02 17:13:50 -05001145 READ_BUF(NFS4_VERIFIER_SIZE);
1146 COPYMEM(setclientid->se_verf.data, NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04001148 status = nfsd4_decode_opaque(argp, &setclientid->se_name);
1149 if (status)
1150 return nfserr_bad_xdr;
1151 READ_BUF(8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 READ32(setclientid->se_callback_prog);
1153 READ32(setclientid->se_callback_netid_len);
1154
1155 READ_BUF(setclientid->se_callback_netid_len + 4);
1156 SAVEMEM(setclientid->se_callback_netid_val, setclientid->se_callback_netid_len);
1157 READ32(setclientid->se_callback_addr_len);
1158
1159 READ_BUF(setclientid->se_callback_addr_len + 4);
1160 SAVEMEM(setclientid->se_callback_addr_val, setclientid->se_callback_addr_len);
1161 READ32(setclientid->se_callback_ident);
1162
1163 DECODE_TAIL;
1164}
1165
Al Virob37ad282006-10-19 23:28:59 -07001166static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167nfsd4_decode_setclientid_confirm(struct nfsd4_compoundargs *argp, struct nfsd4_setclientid_confirm *scd_c)
1168{
1169 DECODE_HEAD;
1170
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001171 if (argp->minorversion >= 1)
1172 return nfserr_notsupp;
1173
Chuck Leverab4684d2012-03-02 17:13:50 -05001174 READ_BUF(8 + NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 COPYMEM(&scd_c->sc_clientid, 8);
Chuck Leverab4684d2012-03-02 17:13:50 -05001176 COPYMEM(&scd_c->sc_confirm, NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
1178 DECODE_TAIL;
1179}
1180
1181/* Also used for NVERIFY */
Al Virob37ad282006-10-19 23:28:59 -07001182static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183nfsd4_decode_verify(struct nfsd4_compoundargs *argp, struct nfsd4_verify *verify)
1184{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 DECODE_HEAD;
1186
1187 if ((status = nfsd4_decode_bitmap(argp, verify->ve_bmval)))
1188 goto out;
1189
1190 /* For convenience's sake, we compare raw xdr'd attributes in
J. Bruce Fieldse5f95702012-11-30 17:24:18 -05001191 * nfsd4_proc_verify */
1192
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 READ_BUF(4);
1194 READ32(verify->ve_attrlen);
1195 READ_BUF(verify->ve_attrlen);
1196 SAVEMEM(verify->ve_attrval, verify->ve_attrlen);
1197
1198 DECODE_TAIL;
1199}
1200
Al Virob37ad282006-10-19 23:28:59 -07001201static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202nfsd4_decode_write(struct nfsd4_compoundargs *argp, struct nfsd4_write *write)
1203{
1204 int avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 int len;
1206 DECODE_HEAD;
1207
Benny Halevye31a1b62008-08-12 20:46:18 +03001208 status = nfsd4_decode_stateid(argp, &write->wr_stateid);
1209 if (status)
1210 return status;
1211 READ_BUF(16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 READ64(write->wr_offset);
1213 READ32(write->wr_stable_how);
1214 if (write->wr_stable_how > 2)
1215 goto xdr_error;
1216 READ32(write->wr_buflen);
1217
1218 /* Sorry .. no magic macros for this.. *
1219 * READ_BUF(write->wr_buflen);
1220 * SAVEMEM(write->wr_buf, write->wr_buflen);
1221 */
1222 avail = (char*)argp->end - (char*)argp->p;
1223 if (avail + argp->pagelen < write->wr_buflen) {
Chuck Lever817cb9d2007-09-11 18:01:20 -04001224 dprintk("NFSD: xdr error (%s:%d)\n",
1225 __FILE__, __LINE__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 goto xdr_error;
1227 }
J. Bruce Fields70cc7f72012-11-16 14:16:46 -05001228 write->wr_head.iov_base = p;
1229 write->wr_head.iov_len = avail;
J. Bruce Fields5a80a542012-11-16 10:01:30 -05001230 WARN_ON(avail != (XDR_QUADLEN(avail) << 2));
J. Bruce Fields70cc7f72012-11-16 14:16:46 -05001231 write->wr_pagelist = argp->pagelist;
J. Bruce Fields5a80a542012-11-16 10:01:30 -05001232
1233 len = XDR_QUADLEN(write->wr_buflen) << 2;
1234 if (len >= avail) {
1235 int pages;
1236
1237 len -= avail;
1238
1239 pages = len >> PAGE_SHIFT;
1240 argp->pagelist += pages;
1241 argp->pagelen -= pages * PAGE_SIZE;
1242 len -= pages * PAGE_SIZE;
1243
1244 argp->p = (__be32 *)page_address(argp->pagelist[0]);
J. Bruce Fields365da4a2013-11-19 17:32:43 -05001245 argp->pagelist++;
J. Bruce Fields5a80a542012-11-16 10:01:30 -05001246 argp->end = argp->p + XDR_QUADLEN(PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 }
J. Bruce Fields5a80a542012-11-16 10:01:30 -05001248 argp->p += XDR_QUADLEN(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
1250 DECODE_TAIL;
1251}
1252
Al Virob37ad282006-10-19 23:28:59 -07001253static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254nfsd4_decode_release_lockowner(struct nfsd4_compoundargs *argp, struct nfsd4_release_lockowner *rlockowner)
1255{
1256 DECODE_HEAD;
1257
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001258 if (argp->minorversion >= 1)
1259 return nfserr_notsupp;
1260
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 READ_BUF(12);
1262 COPYMEM(&rlockowner->rl_clientid, sizeof(clientid_t));
1263 READ32(rlockowner->rl_owner.len);
1264 READ_BUF(rlockowner->rl_owner.len);
1265 READMEM(rlockowner->rl_owner.data, rlockowner->rl_owner.len);
1266
Andy Adamson60adfc52009-04-03 08:28:50 +03001267 if (argp->minorversion && !zero_clientid(&rlockowner->rl_clientid))
1268 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 DECODE_TAIL;
1270}
1271
Al Virob37ad282006-10-19 23:28:59 -07001272static __be32
Andy Adamson2db134e2009-04-03 08:27:55 +03001273nfsd4_decode_exchange_id(struct nfsd4_compoundargs *argp,
Andy Adamson0733d212009-04-03 08:28:01 +03001274 struct nfsd4_exchange_id *exid)
Andy Adamson2db134e2009-04-03 08:27:55 +03001275{
Mi Jinlong5afa0402010-11-09 09:39:23 +08001276 int dummy, tmp;
Andy Adamson0733d212009-04-03 08:28:01 +03001277 DECODE_HEAD;
1278
1279 READ_BUF(NFS4_VERIFIER_SIZE);
1280 COPYMEM(exid->verifier.data, NFS4_VERIFIER_SIZE);
1281
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04001282 status = nfsd4_decode_opaque(argp, &exid->clname);
1283 if (status)
1284 return nfserr_bad_xdr;
Andy Adamson0733d212009-04-03 08:28:01 +03001285
1286 READ_BUF(4);
1287 READ32(exid->flags);
1288
1289 /* Ignore state_protect4_a */
1290 READ_BUF(4);
1291 READ32(exid->spa_how);
1292 switch (exid->spa_how) {
1293 case SP4_NONE:
1294 break;
1295 case SP4_MACH_CRED:
1296 /* spo_must_enforce */
1297 READ_BUF(4);
1298 READ32(dummy);
1299 READ_BUF(dummy * 4);
1300 p += dummy;
1301
1302 /* spo_must_allow */
1303 READ_BUF(4);
1304 READ32(dummy);
1305 READ_BUF(dummy * 4);
1306 p += dummy;
1307 break;
1308 case SP4_SSV:
1309 /* ssp_ops */
1310 READ_BUF(4);
1311 READ32(dummy);
1312 READ_BUF(dummy * 4);
1313 p += dummy;
1314
1315 READ_BUF(4);
1316 READ32(dummy);
1317 READ_BUF(dummy * 4);
1318 p += dummy;
1319
1320 /* ssp_hash_algs<> */
1321 READ_BUF(4);
Mi Jinlong5afa0402010-11-09 09:39:23 +08001322 READ32(tmp);
1323 while (tmp--) {
1324 READ_BUF(4);
1325 READ32(dummy);
1326 READ_BUF(dummy);
1327 p += XDR_QUADLEN(dummy);
1328 }
Andy Adamson0733d212009-04-03 08:28:01 +03001329
1330 /* ssp_encr_algs<> */
1331 READ_BUF(4);
Mi Jinlong5afa0402010-11-09 09:39:23 +08001332 READ32(tmp);
1333 while (tmp--) {
1334 READ_BUF(4);
1335 READ32(dummy);
1336 READ_BUF(dummy);
1337 p += XDR_QUADLEN(dummy);
1338 }
Andy Adamson0733d212009-04-03 08:28:01 +03001339
1340 /* ssp_window and ssp_num_gss_handles */
1341 READ_BUF(8);
1342 READ32(dummy);
1343 READ32(dummy);
1344 break;
1345 default:
1346 goto xdr_error;
1347 }
1348
1349 /* Ignore Implementation ID */
1350 READ_BUF(4); /* nfs_impl_id4 array length */
1351 READ32(dummy);
1352
1353 if (dummy > 1)
1354 goto xdr_error;
1355
1356 if (dummy == 1) {
1357 /* nii_domain */
1358 READ_BUF(4);
1359 READ32(dummy);
1360 READ_BUF(dummy);
1361 p += XDR_QUADLEN(dummy);
1362
1363 /* nii_name */
1364 READ_BUF(4);
1365 READ32(dummy);
1366 READ_BUF(dummy);
1367 p += XDR_QUADLEN(dummy);
1368
1369 /* nii_date */
1370 READ_BUF(12);
1371 p += 3;
1372 }
1373 DECODE_TAIL;
Andy Adamson2db134e2009-04-03 08:27:55 +03001374}
1375
1376static __be32
1377nfsd4_decode_create_session(struct nfsd4_compoundargs *argp,
1378 struct nfsd4_create_session *sess)
1379{
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001380 DECODE_HEAD;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001381 u32 dummy;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001382
1383 READ_BUF(16);
1384 COPYMEM(&sess->clientid, 8);
1385 READ32(sess->seqid);
1386 READ32(sess->flags);
1387
1388 /* Fore channel attrs */
1389 READ_BUF(28);
1390 READ32(dummy); /* headerpadsz is always 0 */
1391 READ32(sess->fore_channel.maxreq_sz);
1392 READ32(sess->fore_channel.maxresp_sz);
1393 READ32(sess->fore_channel.maxresp_cached);
1394 READ32(sess->fore_channel.maxops);
1395 READ32(sess->fore_channel.maxreqs);
1396 READ32(sess->fore_channel.nr_rdma_attrs);
1397 if (sess->fore_channel.nr_rdma_attrs == 1) {
1398 READ_BUF(4);
1399 READ32(sess->fore_channel.rdma_attrs);
1400 } else if (sess->fore_channel.nr_rdma_attrs > 1) {
1401 dprintk("Too many fore channel attr bitmaps!\n");
1402 goto xdr_error;
1403 }
1404
1405 /* Back channel attrs */
1406 READ_BUF(28);
1407 READ32(dummy); /* headerpadsz is always 0 */
1408 READ32(sess->back_channel.maxreq_sz);
1409 READ32(sess->back_channel.maxresp_sz);
1410 READ32(sess->back_channel.maxresp_cached);
1411 READ32(sess->back_channel.maxops);
1412 READ32(sess->back_channel.maxreqs);
1413 READ32(sess->back_channel.nr_rdma_attrs);
1414 if (sess->back_channel.nr_rdma_attrs == 1) {
1415 READ_BUF(4);
1416 READ32(sess->back_channel.rdma_attrs);
1417 } else if (sess->back_channel.nr_rdma_attrs > 1) {
1418 dprintk("Too many back channel attr bitmaps!\n");
1419 goto xdr_error;
1420 }
1421
J. Bruce Fieldsacb28872012-03-27 14:50:26 -04001422 READ_BUF(4);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001423 READ32(sess->callback_prog);
J. Bruce Fieldsacb28872012-03-27 14:50:26 -04001424 nfsd4_decode_cb_sec(argp, &sess->cb_sec);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001425 DECODE_TAIL;
Andy Adamson2db134e2009-04-03 08:27:55 +03001426}
1427
1428static __be32
1429nfsd4_decode_destroy_session(struct nfsd4_compoundargs *argp,
1430 struct nfsd4_destroy_session *destroy_session)
1431{
Benny Halevye10e0cf2009-04-03 08:28:38 +03001432 DECODE_HEAD;
1433 READ_BUF(NFS4_MAX_SESSIONID_LEN);
1434 COPYMEM(destroy_session->sessionid.data, NFS4_MAX_SESSIONID_LEN);
1435
1436 DECODE_TAIL;
Andy Adamson2db134e2009-04-03 08:27:55 +03001437}
1438
1439static __be32
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04001440nfsd4_decode_free_stateid(struct nfsd4_compoundargs *argp,
1441 struct nfsd4_free_stateid *free_stateid)
1442{
1443 DECODE_HEAD;
1444
1445 READ_BUF(sizeof(stateid_t));
1446 READ32(free_stateid->fr_stateid.si_generation);
1447 COPYMEM(&free_stateid->fr_stateid.si_opaque, sizeof(stateid_opaque_t));
1448
1449 DECODE_TAIL;
1450}
1451
1452static __be32
Andy Adamson2db134e2009-04-03 08:27:55 +03001453nfsd4_decode_sequence(struct nfsd4_compoundargs *argp,
1454 struct nfsd4_sequence *seq)
1455{
Benny Halevyb85d4c02009-04-03 08:28:08 +03001456 DECODE_HEAD;
1457
1458 READ_BUF(NFS4_MAX_SESSIONID_LEN + 16);
1459 COPYMEM(seq->sessionid.data, NFS4_MAX_SESSIONID_LEN);
1460 READ32(seq->seqid);
1461 READ32(seq->slotid);
1462 READ32(seq->maxslots);
1463 READ32(seq->cachethis);
1464
1465 DECODE_TAIL;
Andy Adamson2db134e2009-04-03 08:27:55 +03001466}
1467
Bryan Schumaker17456802011-07-13 10:50:48 -04001468static __be32
1469nfsd4_decode_test_stateid(struct nfsd4_compoundargs *argp, struct nfsd4_test_stateid *test_stateid)
1470{
Bryan Schumaker17456802011-07-13 10:50:48 -04001471 int i;
Bryan Schumaker03cfb422012-01-27 10:22:49 -05001472 __be32 *p, status;
1473 struct nfsd4_test_stateid_id *stateid;
Bryan Schumaker17456802011-07-13 10:50:48 -04001474
1475 READ_BUF(4);
1476 test_stateid->ts_num_ids = ntohl(*p++);
1477
Bryan Schumaker03cfb422012-01-27 10:22:49 -05001478 INIT_LIST_HEAD(&test_stateid->ts_stateid_list);
Bryan Schumaker17456802011-07-13 10:50:48 -04001479
1480 for (i = 0; i < test_stateid->ts_num_ids; i++) {
Bryan Schumaker03cfb422012-01-27 10:22:49 -05001481 stateid = kmalloc(sizeof(struct nfsd4_test_stateid_id), GFP_KERNEL);
1482 if (!stateid) {
Al Viroafcf6792012-04-13 00:15:37 -04001483 status = nfserrno(-ENOMEM);
Bryan Schumaker03cfb422012-01-27 10:22:49 -05001484 goto out;
1485 }
1486
1487 defer_free(argp, kfree, stateid);
1488 INIT_LIST_HEAD(&stateid->ts_id_list);
1489 list_add_tail(&stateid->ts_id_list, &test_stateid->ts_stateid_list);
1490
1491 status = nfsd4_decode_stateid(argp, &stateid->ts_id_stateid);
Bryan Schumaker17456802011-07-13 10:50:48 -04001492 if (status)
Bryan Schumaker03cfb422012-01-27 10:22:49 -05001493 goto out;
Bryan Schumaker17456802011-07-13 10:50:48 -04001494 }
1495
1496 status = 0;
1497out:
1498 return status;
1499xdr_error:
1500 dprintk("NFSD: xdr error (%s:%d)\n", __FILE__, __LINE__);
1501 status = nfserr_bad_xdr;
1502 goto out;
1503}
1504
Mi Jinlong345c2842011-10-20 17:51:39 +08001505static __be32 nfsd4_decode_destroy_clientid(struct nfsd4_compoundargs *argp, struct nfsd4_destroy_clientid *dc)
1506{
1507 DECODE_HEAD;
1508
1509 READ_BUF(8);
1510 COPYMEM(&dc->clientid, 8);
1511
1512 DECODE_TAIL;
1513}
1514
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04001515static __be32 nfsd4_decode_reclaim_complete(struct nfsd4_compoundargs *argp, struct nfsd4_reclaim_complete *rc)
1516{
1517 DECODE_HEAD;
1518
1519 READ_BUF(4);
1520 READ32(rc->rca_one_fs);
1521
1522 DECODE_TAIL;
1523}
1524
Andy Adamson2db134e2009-04-03 08:27:55 +03001525static __be32
Benny Halevy347e0ad2008-07-02 11:13:41 +03001526nfsd4_decode_noop(struct nfsd4_compoundargs *argp, void *p)
1527{
1528 return nfs_ok;
1529}
1530
Benny Halevy3c375c62008-07-02 11:14:01 +03001531static __be32
1532nfsd4_decode_notsupp(struct nfsd4_compoundargs *argp, void *p)
1533{
Benny Halevy1e685ec2009-03-04 23:06:06 +02001534 return nfserr_notsupp;
Benny Halevy3c375c62008-07-02 11:14:01 +03001535}
1536
Benny Halevy347e0ad2008-07-02 11:13:41 +03001537typedef __be32(*nfsd4_dec)(struct nfsd4_compoundargs *argp, void *);
1538
1539static nfsd4_dec nfsd4_dec_ops[] = {
J. Bruce Fieldsad1060c2008-07-18 15:04:16 -04001540 [OP_ACCESS] = (nfsd4_dec)nfsd4_decode_access,
1541 [OP_CLOSE] = (nfsd4_dec)nfsd4_decode_close,
1542 [OP_COMMIT] = (nfsd4_dec)nfsd4_decode_commit,
1543 [OP_CREATE] = (nfsd4_dec)nfsd4_decode_create,
1544 [OP_DELEGPURGE] = (nfsd4_dec)nfsd4_decode_notsupp,
1545 [OP_DELEGRETURN] = (nfsd4_dec)nfsd4_decode_delegreturn,
1546 [OP_GETATTR] = (nfsd4_dec)nfsd4_decode_getattr,
1547 [OP_GETFH] = (nfsd4_dec)nfsd4_decode_noop,
1548 [OP_LINK] = (nfsd4_dec)nfsd4_decode_link,
1549 [OP_LOCK] = (nfsd4_dec)nfsd4_decode_lock,
1550 [OP_LOCKT] = (nfsd4_dec)nfsd4_decode_lockt,
1551 [OP_LOCKU] = (nfsd4_dec)nfsd4_decode_locku,
1552 [OP_LOOKUP] = (nfsd4_dec)nfsd4_decode_lookup,
1553 [OP_LOOKUPP] = (nfsd4_dec)nfsd4_decode_noop,
1554 [OP_NVERIFY] = (nfsd4_dec)nfsd4_decode_verify,
1555 [OP_OPEN] = (nfsd4_dec)nfsd4_decode_open,
1556 [OP_OPENATTR] = (nfsd4_dec)nfsd4_decode_notsupp,
1557 [OP_OPEN_CONFIRM] = (nfsd4_dec)nfsd4_decode_open_confirm,
1558 [OP_OPEN_DOWNGRADE] = (nfsd4_dec)nfsd4_decode_open_downgrade,
1559 [OP_PUTFH] = (nfsd4_dec)nfsd4_decode_putfh,
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001560 [OP_PUTPUBFH] = (nfsd4_dec)nfsd4_decode_putpubfh,
J. Bruce Fieldsad1060c2008-07-18 15:04:16 -04001561 [OP_PUTROOTFH] = (nfsd4_dec)nfsd4_decode_noop,
1562 [OP_READ] = (nfsd4_dec)nfsd4_decode_read,
1563 [OP_READDIR] = (nfsd4_dec)nfsd4_decode_readdir,
1564 [OP_READLINK] = (nfsd4_dec)nfsd4_decode_noop,
1565 [OP_REMOVE] = (nfsd4_dec)nfsd4_decode_remove,
1566 [OP_RENAME] = (nfsd4_dec)nfsd4_decode_rename,
1567 [OP_RENEW] = (nfsd4_dec)nfsd4_decode_renew,
1568 [OP_RESTOREFH] = (nfsd4_dec)nfsd4_decode_noop,
1569 [OP_SAVEFH] = (nfsd4_dec)nfsd4_decode_noop,
1570 [OP_SECINFO] = (nfsd4_dec)nfsd4_decode_secinfo,
1571 [OP_SETATTR] = (nfsd4_dec)nfsd4_decode_setattr,
1572 [OP_SETCLIENTID] = (nfsd4_dec)nfsd4_decode_setclientid,
1573 [OP_SETCLIENTID_CONFIRM] = (nfsd4_dec)nfsd4_decode_setclientid_confirm,
1574 [OP_VERIFY] = (nfsd4_dec)nfsd4_decode_verify,
1575 [OP_WRITE] = (nfsd4_dec)nfsd4_decode_write,
1576 [OP_RELEASE_LOCKOWNER] = (nfsd4_dec)nfsd4_decode_release_lockowner,
Andy Adamson2db134e2009-04-03 08:27:55 +03001577
1578 /* new operations for NFSv4.1 */
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04001579 [OP_BACKCHANNEL_CTL] = (nfsd4_dec)nfsd4_decode_backchannel_ctl,
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001580 [OP_BIND_CONN_TO_SESSION]= (nfsd4_dec)nfsd4_decode_bind_conn_to_session,
Randy Dunlap9064caa2009-04-28 16:48:25 -07001581 [OP_EXCHANGE_ID] = (nfsd4_dec)nfsd4_decode_exchange_id,
1582 [OP_CREATE_SESSION] = (nfsd4_dec)nfsd4_decode_create_session,
1583 [OP_DESTROY_SESSION] = (nfsd4_dec)nfsd4_decode_destroy_session,
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04001584 [OP_FREE_STATEID] = (nfsd4_dec)nfsd4_decode_free_stateid,
Randy Dunlap9064caa2009-04-28 16:48:25 -07001585 [OP_GET_DIR_DELEGATION] = (nfsd4_dec)nfsd4_decode_notsupp,
1586 [OP_GETDEVICEINFO] = (nfsd4_dec)nfsd4_decode_notsupp,
1587 [OP_GETDEVICELIST] = (nfsd4_dec)nfsd4_decode_notsupp,
1588 [OP_LAYOUTCOMMIT] = (nfsd4_dec)nfsd4_decode_notsupp,
1589 [OP_LAYOUTGET] = (nfsd4_dec)nfsd4_decode_notsupp,
1590 [OP_LAYOUTRETURN] = (nfsd4_dec)nfsd4_decode_notsupp,
J. Bruce Fields04f4ad12010-12-16 09:51:13 -05001591 [OP_SECINFO_NO_NAME] = (nfsd4_dec)nfsd4_decode_secinfo_no_name,
Randy Dunlap9064caa2009-04-28 16:48:25 -07001592 [OP_SEQUENCE] = (nfsd4_dec)nfsd4_decode_sequence,
1593 [OP_SET_SSV] = (nfsd4_dec)nfsd4_decode_notsupp,
Bryan Schumaker17456802011-07-13 10:50:48 -04001594 [OP_TEST_STATEID] = (nfsd4_dec)nfsd4_decode_test_stateid,
Randy Dunlap9064caa2009-04-28 16:48:25 -07001595 [OP_WANT_DELEGATION] = (nfsd4_dec)nfsd4_decode_notsupp,
Mi Jinlong345c2842011-10-20 17:51:39 +08001596 [OP_DESTROY_CLIENTID] = (nfsd4_dec)nfsd4_decode_destroy_clientid,
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04001597 [OP_RECLAIM_COMPLETE] = (nfsd4_dec)nfsd4_decode_reclaim_complete,
Andy Adamson2db134e2009-04-03 08:27:55 +03001598};
1599
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001600static inline bool
1601nfsd4_opnum_in_range(struct nfsd4_compoundargs *argp, struct nfsd4_op *op)
1602{
Anna Schumaker8217d142013-10-30 13:38:13 -04001603 if (op->opnum < FIRST_NFS4_OP)
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001604 return false;
Anna Schumaker8217d142013-10-30 13:38:13 -04001605 else if (argp->minorversion == 0 && op->opnum > LAST_NFS40_OP)
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001606 return false;
Anna Schumaker8217d142013-10-30 13:38:13 -04001607 else if (argp->minorversion == 1 && op->opnum > LAST_NFS41_OP)
1608 return false;
1609 else if (argp->minorversion == 2 && op->opnum > LAST_NFS42_OP)
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001610 return false;
1611 return true;
1612}
Benny Halevyf2feb962008-07-02 11:14:22 +03001613
J. Bruce Fields6ff40de2013-11-05 15:07:16 -05001614/*
1615 * Return a rough estimate of the maximum possible reply size. Note the
1616 * estimate includes rpc headers so is meant to be passed to
1617 * svc_reserve, not svc_reserve_auth.
1618 *
1619 * Also note the current compound encoding permits only one operation to
1620 * use pages beyond the first one, so the maximum possible length is the
1621 * maximum over these values, not the sum.
1622 */
1623static int nfsd4_max_reply(u32 opnum)
1624{
1625 switch (opnum) {
1626 case OP_READLINK:
1627 case OP_READDIR:
1628 /*
1629 * Both of these ops take a single page for data and put
1630 * the head and tail in another page:
1631 */
1632 return 2 * PAGE_SIZE;
1633 case OP_READ:
1634 return INT_MAX;
1635 default:
1636 return PAGE_SIZE;
1637 }
1638}
1639
Benny Halevy347e0ad2008-07-02 11:13:41 +03001640static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641nfsd4_decode_compound(struct nfsd4_compoundargs *argp)
1642{
1643 DECODE_HEAD;
1644 struct nfsd4_op *op;
J. Bruce Fields10910062011-01-24 12:11:02 -05001645 bool cachethis = false;
J. Bruce Fields6ff40de2013-11-05 15:07:16 -05001646 int max_reply = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 int i;
1648
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 READ_BUF(4);
1650 READ32(argp->taglen);
1651 READ_BUF(argp->taglen + 8);
1652 SAVEMEM(argp->tag, argp->taglen);
1653 READ32(argp->minorversion);
1654 READ32(argp->opcnt);
1655
1656 if (argp->taglen > NFSD4_MAX_TAGLEN)
1657 goto xdr_error;
1658 if (argp->opcnt > 100)
1659 goto xdr_error;
1660
Tobias Klausere8c96f82006-03-24 03:15:34 -08001661 if (argp->opcnt > ARRAY_SIZE(argp->iops)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 argp->ops = kmalloc(argp->opcnt * sizeof(*argp->ops), GFP_KERNEL);
1663 if (!argp->ops) {
1664 argp->ops = argp->iops;
Chuck Lever817cb9d2007-09-11 18:01:20 -04001665 dprintk("nfsd: couldn't allocate room for COMPOUND\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 goto xdr_error;
1667 }
1668 }
1669
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001670 if (argp->minorversion > NFSD_SUPPORTED_MINOR_VERSION)
Benny Halevy30cff1f2008-07-02 11:13:18 +03001671 argp->opcnt = 0;
1672
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 for (i = 0; i < argp->opcnt; i++) {
1674 op = &argp->ops[i];
1675 op->replay = NULL;
1676
J. Bruce Fields8a61b182012-11-16 22:28:38 -05001677 READ_BUF(4);
1678 READ32(op->opnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001680 if (nfsd4_opnum_in_range(argp, op))
1681 op->status = nfsd4_dec_ops[op->opnum](argp, &op->u);
Benny Halevy347e0ad2008-07-02 11:13:41 +03001682 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 op->opnum = OP_ILLEGAL;
1684 op->status = nfserr_op_illegal;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 }
1686
1687 if (op->status) {
1688 argp->opcnt = i+1;
1689 break;
1690 }
J. Bruce Fields10910062011-01-24 12:11:02 -05001691 /*
1692 * We'll try to cache the result in the DRC if any one
1693 * op in the compound wants to be cached:
1694 */
1695 cachethis |= nfsd4_cache_this_op(op);
J. Bruce Fields6ff40de2013-11-05 15:07:16 -05001696
1697 max_reply = max(max_reply, nfsd4_max_reply(op->opnum));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 }
J. Bruce Fields10910062011-01-24 12:11:02 -05001699 /* Sessions make the DRC unnecessary: */
1700 if (argp->minorversion)
1701 cachethis = false;
J. Bruce Fields6ff40de2013-11-05 15:07:16 -05001702 if (max_reply != INT_MAX)
1703 svc_reserve(argp->rqstp, max_reply);
J. Bruce Fields10910062011-01-24 12:11:02 -05001704 argp->rqstp->rq_cachetype = cachethis ? RC_REPLBUFF : RC_NOCACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705
1706 DECODE_TAIL;
1707}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709#define WRITE32(n) *p++ = htonl(n)
1710#define WRITE64(n) do { \
1711 *p++ = htonl((u32)((n) >> 32)); \
1712 *p++ = htonl((u32)(n)); \
1713} while (0)
Harvey Harrison5108b272008-07-17 21:33:04 -07001714#define WRITEMEM(ptr,nbytes) do { if (nbytes > 0) { \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 *(p + XDR_QUADLEN(nbytes) -1) = 0; \
1716 memcpy(p, ptr, nbytes); \
1717 p += XDR_QUADLEN(nbytes); \
Harvey Harrison5108b272008-07-17 21:33:04 -07001718}} while (0)
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04001719
1720static void write32(__be32 **p, u32 n)
1721{
J. Bruce Fields45eaa1c2012-04-25 18:11:04 -04001722 *(*p)++ = htonl(n);
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04001723}
1724
1725static void write64(__be32 **p, u64 n)
1726{
J. Bruce Fields45eaa1c2012-04-25 18:11:04 -04001727 write32(p, (n >> 32));
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04001728 write32(p, (u32)n);
1729}
1730
1731static void write_change(__be32 **p, struct kstat *stat, struct inode *inode)
1732{
1733 if (IS_I_VERSION(inode)) {
1734 write64(p, inode->i_version);
1735 } else {
1736 write32(p, stat->ctime.tv_sec);
1737 write32(p, stat->ctime.tv_nsec);
1738 }
1739}
1740
1741static void write_cinfo(__be32 **p, struct nfsd4_change_info *c)
1742{
1743 write32(p, c->atomic);
1744 if (c->change_supported) {
1745 write64(p, c->before_change);
1746 write64(p, c->after_change);
1747 } else {
1748 write32(p, c->before_ctime_sec);
1749 write32(p, c->before_ctime_nsec);
1750 write32(p, c->after_ctime_sec);
1751 write32(p, c->after_ctime_nsec);
1752 }
1753}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754
1755#define RESERVE_SPACE(nbytes) do { \
1756 p = resp->p; \
1757 BUG_ON(p + XDR_QUADLEN(nbytes) > resp->end); \
1758} while (0)
1759#define ADJUST_ARGS() resp->p = p
1760
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001761/* Encode as an array of strings the string given with components
Weston Andros Adamsone7a04442012-04-24 11:07:59 -04001762 * separated @sep, escaped with esc_enter and esc_exit.
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001763 */
Weston Andros Adamsone7a04442012-04-24 11:07:59 -04001764static __be32 nfsd4_encode_components_esc(char sep, char *components,
1765 __be32 **pp, int *buflen,
1766 char esc_enter, char esc_exit)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001767{
Al Viro2ebbc012006-10-19 23:28:58 -07001768 __be32 *p = *pp;
1769 __be32 *countp = p;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001770 int strlen, count=0;
Weston Andros Adamsone7a04442012-04-24 11:07:59 -04001771 char *str, *end, *next;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001772
1773 dprintk("nfsd4_encode_components(%s)\n", components);
1774 if ((*buflen -= 4) < 0)
1775 return nfserr_resource;
1776 WRITE32(0); /* We will fill this in with @count later */
1777 end = str = components;
1778 while (*end) {
Weston Andros Adamsone7a04442012-04-24 11:07:59 -04001779 bool found_esc = false;
1780
1781 /* try to parse as esc_start, ..., esc_end, sep */
1782 if (*str == esc_enter) {
1783 for (; *end && (*end != esc_exit); end++)
1784 /* find esc_exit or end of string */;
1785 next = end + 1;
1786 if (*end && (!*next || *next == sep)) {
1787 str++;
1788 found_esc = true;
1789 }
1790 }
1791
1792 if (!found_esc)
1793 for (; *end && (*end != sep); end++)
1794 /* find sep or end of string */;
1795
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001796 strlen = end - str;
1797 if (strlen) {
1798 if ((*buflen -= ((XDR_QUADLEN(strlen) << 2) + 4)) < 0)
1799 return nfserr_resource;
1800 WRITE32(strlen);
1801 WRITEMEM(str, strlen);
1802 count++;
1803 }
1804 else
1805 end++;
1806 str = end;
1807 }
1808 *pp = p;
1809 p = countp;
1810 WRITE32(count);
1811 return 0;
1812}
1813
Weston Andros Adamsone7a04442012-04-24 11:07:59 -04001814/* Encode as an array of strings the string given with components
1815 * separated @sep.
1816 */
1817static __be32 nfsd4_encode_components(char sep, char *components,
1818 __be32 **pp, int *buflen)
1819{
1820 return nfsd4_encode_components_esc(sep, components, pp, buflen, 0, 0);
1821}
1822
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001823/*
1824 * encode a location element of a fs_locations structure
1825 */
Al Virob37ad282006-10-19 23:28:59 -07001826static __be32 nfsd4_encode_fs_location4(struct nfsd4_fs_location *location,
Al Viro2ebbc012006-10-19 23:28:58 -07001827 __be32 **pp, int *buflen)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001828{
Al Virob37ad282006-10-19 23:28:59 -07001829 __be32 status;
Al Viro2ebbc012006-10-19 23:28:58 -07001830 __be32 *p = *pp;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001831
Weston Andros Adamsone7a04442012-04-24 11:07:59 -04001832 status = nfsd4_encode_components_esc(':', location->hosts, &p, buflen,
1833 '[', ']');
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001834 if (status)
1835 return status;
1836 status = nfsd4_encode_components('/', location->path, &p, buflen);
1837 if (status)
1838 return status;
1839 *pp = p;
1840 return 0;
1841}
1842
1843/*
Trond Myklebusted748aa2011-09-12 19:37:06 -04001844 * Encode a path in RFC3530 'pathname4' format
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001845 */
Trond Myklebusted748aa2011-09-12 19:37:06 -04001846static __be32 nfsd4_encode_path(const struct path *root,
1847 const struct path *path, __be32 **pp, int *buflen)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001848{
Al Viro301f0262013-09-01 16:06:39 -04001849 struct path cur = *path;
Trond Myklebusted748aa2011-09-12 19:37:06 -04001850 __be32 *p = *pp;
1851 struct dentry **components = NULL;
1852 unsigned int ncomponents = 0;
1853 __be32 err = nfserr_jukebox;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001854
Trond Myklebusted748aa2011-09-12 19:37:06 -04001855 dprintk("nfsd4_encode_components(");
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001856
Trond Myklebusted748aa2011-09-12 19:37:06 -04001857 path_get(&cur);
1858 /* First walk the path up to the nfsd root, and store the
1859 * dentries/path components in an array.
1860 */
1861 for (;;) {
1862 if (cur.dentry == root->dentry && cur.mnt == root->mnt)
1863 break;
1864 if (cur.dentry == cur.mnt->mnt_root) {
1865 if (follow_up(&cur))
1866 continue;
1867 goto out_free;
1868 }
1869 if ((ncomponents & 15) == 0) {
1870 struct dentry **new;
1871 new = krealloc(components,
1872 sizeof(*new) * (ncomponents + 16),
1873 GFP_KERNEL);
1874 if (!new)
1875 goto out_free;
1876 components = new;
1877 }
1878 components[ncomponents++] = cur.dentry;
1879 cur.dentry = dget_parent(cur.dentry);
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001880 }
Trond Myklebusted748aa2011-09-12 19:37:06 -04001881
1882 *buflen -= 4;
1883 if (*buflen < 0)
1884 goto out_free;
1885 WRITE32(ncomponents);
1886
1887 while (ncomponents) {
1888 struct dentry *dentry = components[ncomponents - 1];
Al Viro301f0262013-09-01 16:06:39 -04001889 unsigned int len;
Trond Myklebusted748aa2011-09-12 19:37:06 -04001890
Al Viro301f0262013-09-01 16:06:39 -04001891 spin_lock(&dentry->d_lock);
1892 len = dentry->d_name.len;
Trond Myklebusted748aa2011-09-12 19:37:06 -04001893 *buflen -= 4 + (XDR_QUADLEN(len) << 2);
Al Viro301f0262013-09-01 16:06:39 -04001894 if (*buflen < 0) {
1895 spin_unlock(&dentry->d_lock);
Trond Myklebusted748aa2011-09-12 19:37:06 -04001896 goto out_free;
Al Viro301f0262013-09-01 16:06:39 -04001897 }
Trond Myklebusted748aa2011-09-12 19:37:06 -04001898 WRITE32(len);
1899 WRITEMEM(dentry->d_name.name, len);
1900 dprintk("/%s", dentry->d_name.name);
Al Viro301f0262013-09-01 16:06:39 -04001901 spin_unlock(&dentry->d_lock);
Trond Myklebusted748aa2011-09-12 19:37:06 -04001902 dput(dentry);
1903 ncomponents--;
1904 }
1905
1906 *pp = p;
1907 err = 0;
1908out_free:
1909 dprintk(")\n");
1910 while (ncomponents)
1911 dput(components[--ncomponents]);
1912 kfree(components);
1913 path_put(&cur);
1914 return err;
1915}
1916
1917static __be32 nfsd4_encode_fsloc_fsroot(struct svc_rqst *rqstp,
1918 const struct path *path, __be32 **pp, int *buflen)
1919{
1920 struct svc_export *exp_ps;
1921 __be32 res;
1922
1923 exp_ps = rqst_find_fsidzero_export(rqstp);
1924 if (IS_ERR(exp_ps))
1925 return nfserrno(PTR_ERR(exp_ps));
1926 res = nfsd4_encode_path(&exp_ps->ex_path, path, pp, buflen);
1927 exp_put(exp_ps);
1928 return res;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001929}
1930
1931/*
1932 * encode a fs_locations structure
1933 */
Al Virob37ad282006-10-19 23:28:59 -07001934static __be32 nfsd4_encode_fs_locations(struct svc_rqst *rqstp,
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001935 struct svc_export *exp,
Al Viro2ebbc012006-10-19 23:28:58 -07001936 __be32 **pp, int *buflen)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001937{
Al Virob37ad282006-10-19 23:28:59 -07001938 __be32 status;
Al Virocc45f012006-10-19 23:28:44 -07001939 int i;
Al Viro2ebbc012006-10-19 23:28:58 -07001940 __be32 *p = *pp;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001941 struct nfsd4_fs_locations *fslocs = &exp->ex_fslocs;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001942
Trond Myklebusted748aa2011-09-12 19:37:06 -04001943 status = nfsd4_encode_fsloc_fsroot(rqstp, &exp->ex_path, &p, buflen);
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001944 if (status)
1945 return status;
1946 if ((*buflen -= 4) < 0)
1947 return nfserr_resource;
1948 WRITE32(fslocs->locations_count);
1949 for (i=0; i<fslocs->locations_count; i++) {
1950 status = nfsd4_encode_fs_location4(&fslocs->locations[i],
1951 &p, buflen);
1952 if (status)
1953 return status;
1954 }
1955 *pp = p;
1956 return 0;
1957}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958
J. Bruce Fields3d2544b2011-08-15 11:49:30 -04001959static u32 nfs4_file_type(umode_t mode)
1960{
1961 switch (mode & S_IFMT) {
1962 case S_IFIFO: return NF4FIFO;
1963 case S_IFCHR: return NF4CHR;
1964 case S_IFDIR: return NF4DIR;
1965 case S_IFBLK: return NF4BLK;
1966 case S_IFLNK: return NF4LNK;
1967 case S_IFREG: return NF4REG;
1968 case S_IFSOCK: return NF4SOCK;
1969 default: return NF4BAD;
1970 };
1971}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972
Al Virob37ad282006-10-19 23:28:59 -07001973static __be32
Eric W. Biedermanab8e4ae2013-02-02 05:18:08 -08001974nfsd4_encode_name(struct svc_rqst *rqstp, int whotype, kuid_t uid, kgid_t gid,
Al Viro2ebbc012006-10-19 23:28:58 -07001975 __be32 **p, int *buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976{
1977 int status;
1978
1979 if (*buflen < (XDR_QUADLEN(IDMAP_NAMESZ) << 2) + 4)
1980 return nfserr_resource;
1981 if (whotype != NFS4_ACL_WHO_NAMED)
1982 status = nfs4_acl_write_who(whotype, (u8 *)(*p + 1));
Eric W. Biedermanab8e4ae2013-02-02 05:18:08 -08001983 else if (gid_valid(gid))
1984 status = nfsd_map_gid_to_name(rqstp, gid, (u8 *)(*p + 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 else
Eric W. Biedermanab8e4ae2013-02-02 05:18:08 -08001986 status = nfsd_map_uid_to_name(rqstp, uid, (u8 *)(*p + 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 if (status < 0)
1988 return nfserrno(status);
1989 *p = xdr_encode_opaque(*p, NULL, status);
1990 *buflen -= (XDR_QUADLEN(status) << 2) + 4;
1991 BUG_ON(*buflen < 0);
1992 return 0;
1993}
1994
Al Virob37ad282006-10-19 23:28:59 -07001995static inline __be32
Eric W. Biedermanab8e4ae2013-02-02 05:18:08 -08001996nfsd4_encode_user(struct svc_rqst *rqstp, kuid_t user, __be32 **p, int *buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997{
Eric W. Biedermanab8e4ae2013-02-02 05:18:08 -08001998 return nfsd4_encode_name(rqstp, NFS4_ACL_WHO_NAMED, user, INVALID_GID,
1999 p, buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000}
2001
Al Virob37ad282006-10-19 23:28:59 -07002002static inline __be32
Eric W. Biedermanab8e4ae2013-02-02 05:18:08 -08002003nfsd4_encode_group(struct svc_rqst *rqstp, kgid_t group, __be32 **p, int *buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004{
Eric W. Biedermanab8e4ae2013-02-02 05:18:08 -08002005 return nfsd4_encode_name(rqstp, NFS4_ACL_WHO_NAMED, INVALID_UID, group,
2006 p, buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007}
2008
Al Virob37ad282006-10-19 23:28:59 -07002009static inline __be32
Eric W. Biedermanab8e4ae2013-02-02 05:18:08 -08002010nfsd4_encode_aclname(struct svc_rqst *rqstp, struct nfs4_ace *ace,
Al Viro2ebbc012006-10-19 23:28:58 -07002011 __be32 **p, int *buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012{
Eric W. Biedermanab8e4ae2013-02-02 05:18:08 -08002013 kuid_t uid = INVALID_UID;
2014 kgid_t gid = INVALID_GID;
2015
2016 if (ace->whotype == NFS4_ACL_WHO_NAMED) {
2017 if (ace->flag & NFS4_ACE_IDENTIFIER_GROUP)
2018 gid = ace->who_gid;
2019 else
2020 uid = ace->who_uid;
2021 }
2022 return nfsd4_encode_name(rqstp, ace->whotype, uid, gid, p, buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023}
2024
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002025#define WORD0_ABSENT_FS_ATTRS (FATTR4_WORD0_FS_LOCATIONS | FATTR4_WORD0_FSID | \
2026 FATTR4_WORD0_RDATTR_ERROR)
2027#define WORD1_ABSENT_FS_ATTRS FATTR4_WORD1_MOUNTED_ON_FILEID
2028
David Quigley18032ca2013-05-02 13:19:10 -04002029#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
2030static inline __be32
2031nfsd4_encode_security_label(struct svc_rqst *rqstp, void *context, int len, __be32 **pp, int *buflen)
2032{
2033 __be32 *p = *pp;
2034
2035 if (*buflen < ((XDR_QUADLEN(len) << 2) + 4 + 4 + 4))
2036 return nfserr_resource;
2037
2038 /*
2039 * For now we use a 0 here to indicate the null translation; in
2040 * the future we may place a call to translation code here.
2041 */
2042 if ((*buflen -= 8) < 0)
2043 return nfserr_resource;
2044
2045 WRITE32(0); /* lfs */
2046 WRITE32(0); /* pi */
2047 p = xdr_encode_opaque(p, context, len);
2048 *buflen -= (XDR_QUADLEN(len) << 2) + 4;
2049
2050 *pp = p;
2051 return 0;
2052}
2053#else
2054static inline __be32
J. Bruce Fieldsba4e55b2013-05-15 10:27:52 -04002055nfsd4_encode_security_label(struct svc_rqst *rqstp, void *context, int len, __be32 **pp, int *buflen)
David Quigley18032ca2013-05-02 13:19:10 -04002056{ return 0; }
2057#endif
2058
Al Virob37ad282006-10-19 23:28:59 -07002059static __be32 fattr_handle_absent_fs(u32 *bmval0, u32 *bmval1, u32 *rdattr_err)
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002060{
2061 /* As per referral draft: */
2062 if (*bmval0 & ~WORD0_ABSENT_FS_ATTRS ||
2063 *bmval1 & ~WORD1_ABSENT_FS_ATTRS) {
2064 if (*bmval0 & FATTR4_WORD0_RDATTR_ERROR ||
2065 *bmval0 & FATTR4_WORD0_FS_LOCATIONS)
2066 *rdattr_err = NFSERR_MOVED;
2067 else
2068 return nfserr_moved;
2069 }
2070 *bmval0 &= WORD0_ABSENT_FS_ATTRS;
2071 *bmval1 &= WORD1_ABSENT_FS_ATTRS;
2072 return 0;
2073}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074
J. Bruce Fieldsae7095a2012-10-01 17:50:56 -04002075
2076static int get_parent_attributes(struct svc_export *exp, struct kstat *stat)
2077{
2078 struct path path = exp->ex_path;
2079 int err;
2080
2081 path_get(&path);
2082 while (follow_up(&path)) {
2083 if (path.dentry != path.mnt->mnt_root)
2084 break;
2085 }
Al Viro3dadecc2013-01-24 02:18:08 -05002086 err = vfs_getattr(&path, stat);
J. Bruce Fieldsae7095a2012-10-01 17:50:56 -04002087 path_put(&path);
2088 return err;
2089}
2090
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091/*
2092 * Note: @fhp can be NULL; in this case, we might have to compose the filehandle
2093 * ourselves.
2094 *
J. Bruce Fields84822d02012-12-14 17:57:50 -05002095 * countp is the buffer size in _words_
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 */
Al Virob37ad282006-10-19 23:28:59 -07002097__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098nfsd4_encode_fattr(struct svc_fh *fhp, struct svc_export *exp,
J. Bruce Fields84822d02012-12-14 17:57:50 -05002099 struct dentry *dentry, __be32 **buffer, int count, u32 *bmval,
Frank Filz406a7ea2007-11-27 11:34:05 -08002100 struct svc_rqst *rqstp, int ignore_crossmnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101{
2102 u32 bmval0 = bmval[0];
2103 u32 bmval1 = bmval[1];
Andy Adamson7e705702009-04-03 08:29:11 +03002104 u32 bmval2 = bmval[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 struct kstat stat;
2106 struct svc_fh tempfh;
2107 struct kstatfs statfs;
J. Bruce Fields84822d02012-12-14 17:57:50 -05002108 int buflen = count << 2;
Al Viro2ebbc012006-10-19 23:28:58 -07002109 __be32 *attrlenp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 u32 dummy;
2111 u64 dummy64;
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002112 u32 rdattr_err = 0;
J. Bruce Fields84822d02012-12-14 17:57:50 -05002113 __be32 *p = *buffer;
Al Virob37ad282006-10-19 23:28:59 -07002114 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07002115 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 int aclsupport = 0;
2117 struct nfs4_acl *acl = NULL;
David Quigley18032ca2013-05-02 13:19:10 -04002118 void *context = NULL;
2119 int contextlen;
2120 bool contextsupport = false;
Andy Adamson7e705702009-04-03 08:29:11 +03002121 struct nfsd4_compoundres *resp = rqstp->rq_resp;
2122 u32 minorversion = resp->cstate.minorversion;
Christoph Hellwigebabe9a2010-07-07 18:53:11 +02002123 struct path path = {
2124 .mnt = exp->ex_path.mnt,
2125 .dentry = dentry,
2126 };
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +03002127 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128
2129 BUG_ON(bmval1 & NFSD_WRITEONLY_ATTRS_WORD1);
Andy Adamson7e705702009-04-03 08:29:11 +03002130 BUG_ON(bmval0 & ~nfsd_suppattrs0(minorversion));
2131 BUG_ON(bmval1 & ~nfsd_suppattrs1(minorversion));
2132 BUG_ON(bmval2 & ~nfsd_suppattrs2(minorversion));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002134 if (exp->ex_fslocs.migrated) {
Andy Adamson7e705702009-04-03 08:29:11 +03002135 BUG_ON(bmval[2]);
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002136 status = fattr_handle_absent_fs(&bmval0, &bmval1, &rdattr_err);
2137 if (status)
2138 goto out;
2139 }
2140
Al Viro3dadecc2013-01-24 02:18:08 -05002141 err = vfs_getattr(&path, &stat);
Al Virob8dd7b92006-10-19 23:29:01 -07002142 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 goto out_nfserr;
J. Bruce Fieldsa16e92e2007-09-28 16:45:51 -04002144 if ((bmval0 & (FATTR4_WORD0_FILES_FREE | FATTR4_WORD0_FILES_TOTAL |
2145 FATTR4_WORD0_MAXNAME)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 (bmval1 & (FATTR4_WORD1_SPACE_AVAIL | FATTR4_WORD1_SPACE_FREE |
2147 FATTR4_WORD1_SPACE_TOTAL))) {
Christoph Hellwigebabe9a2010-07-07 18:53:11 +02002148 err = vfs_statfs(&path, &statfs);
Al Virob8dd7b92006-10-19 23:29:01 -07002149 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 goto out_nfserr;
2151 }
2152 if ((bmval0 & (FATTR4_WORD0_FILEHANDLE | FATTR4_WORD0_FSID)) && !fhp) {
2153 fh_init(&tempfh, NFS4_FHSIZE);
2154 status = fh_compose(&tempfh, exp, dentry, NULL);
2155 if (status)
2156 goto out;
2157 fhp = &tempfh;
2158 }
2159 if (bmval0 & (FATTR4_WORD0_ACL | FATTR4_WORD0_ACLSUPPORT
2160 | FATTR4_WORD0_SUPPORTED_ATTRS)) {
Al Virob8dd7b92006-10-19 23:29:01 -07002161 err = nfsd4_get_nfs4_acl(rqstp, dentry, &acl);
2162 aclsupport = (err == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 if (bmval0 & FATTR4_WORD0_ACL) {
Al Virob8dd7b92006-10-19 23:29:01 -07002164 if (err == -EOPNOTSUPP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 bmval0 &= ~FATTR4_WORD0_ACL;
Al Virob8dd7b92006-10-19 23:29:01 -07002166 else if (err == -EINVAL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 status = nfserr_attrnotsupp;
2168 goto out;
Al Virob8dd7b92006-10-19 23:29:01 -07002169 } else if (err != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 goto out_nfserr;
2171 }
2172 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173
David Quigley18032ca2013-05-02 13:19:10 -04002174#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
2175 if ((bmval[2] & FATTR4_WORD2_SECURITY_LABEL) ||
2176 bmval[0] & FATTR4_WORD0_SUPPORTED_ATTRS) {
2177 err = security_inode_getsecctx(dentry->d_inode,
2178 &context, &contextlen);
2179 contextsupport = (err == 0);
2180 if (bmval2 & FATTR4_WORD2_SECURITY_LABEL) {
2181 if (err == -EOPNOTSUPP)
2182 bmval2 &= ~FATTR4_WORD2_SECURITY_LABEL;
2183 else if (err)
2184 goto out_nfserr;
2185 }
2186 }
2187#endif /* CONFIG_NFSD_V4_SECURITY_LABEL */
2188
Benny Halevy2b44f1b2010-09-30 20:47:46 +02002189 if (bmval2) {
2190 if ((buflen -= 16) < 0)
2191 goto out_resource;
Andy Adamson7e705702009-04-03 08:29:11 +03002192 WRITE32(3);
2193 WRITE32(bmval0);
2194 WRITE32(bmval1);
2195 WRITE32(bmval2);
Benny Halevy2b44f1b2010-09-30 20:47:46 +02002196 } else if (bmval1) {
2197 if ((buflen -= 12) < 0)
2198 goto out_resource;
Andy Adamson7e705702009-04-03 08:29:11 +03002199 WRITE32(2);
2200 WRITE32(bmval0);
2201 WRITE32(bmval1);
2202 } else {
Benny Halevy2b44f1b2010-09-30 20:47:46 +02002203 if ((buflen -= 8) < 0)
2204 goto out_resource;
Andy Adamson7e705702009-04-03 08:29:11 +03002205 WRITE32(1);
2206 WRITE32(bmval0);
2207 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 attrlenp = p++; /* to be backfilled later */
2209
2210 if (bmval0 & FATTR4_WORD0_SUPPORTED_ATTRS) {
Andy Adamson7e705702009-04-03 08:29:11 +03002211 u32 word0 = nfsd_suppattrs0(minorversion);
2212 u32 word1 = nfsd_suppattrs1(minorversion);
2213 u32 word2 = nfsd_suppattrs2(minorversion);
2214
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002215 if (!aclsupport)
2216 word0 &= ~FATTR4_WORD0_ACL;
David Quigley18032ca2013-05-02 13:19:10 -04002217 if (!contextsupport)
2218 word2 &= ~FATTR4_WORD2_SECURITY_LABEL;
Andy Adamson7e705702009-04-03 08:29:11 +03002219 if (!word2) {
Benny Halevy2b44f1b2010-09-30 20:47:46 +02002220 if ((buflen -= 12) < 0)
2221 goto out_resource;
Andy Adamson7e705702009-04-03 08:29:11 +03002222 WRITE32(2);
2223 WRITE32(word0);
2224 WRITE32(word1);
2225 } else {
Benny Halevy2b44f1b2010-09-30 20:47:46 +02002226 if ((buflen -= 16) < 0)
2227 goto out_resource;
Andy Adamson7e705702009-04-03 08:29:11 +03002228 WRITE32(3);
2229 WRITE32(word0);
2230 WRITE32(word1);
2231 WRITE32(word2);
2232 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 }
2234 if (bmval0 & FATTR4_WORD0_TYPE) {
2235 if ((buflen -= 4) < 0)
2236 goto out_resource;
J. Bruce Fields3d2544b2011-08-15 11:49:30 -04002237 dummy = nfs4_file_type(stat.mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 if (dummy == NF4BAD)
2239 goto out_serverfault;
2240 WRITE32(dummy);
2241 }
2242 if (bmval0 & FATTR4_WORD0_FH_EXPIRE_TYPE) {
2243 if ((buflen -= 4) < 0)
2244 goto out_resource;
NeilBrown49640002005-06-23 22:02:58 -07002245 if (exp->ex_flags & NFSEXP_NOSUBTREECHECK)
NeilBrowne34ac862005-07-07 17:59:30 -07002246 WRITE32(NFS4_FH_PERSISTENT);
NeilBrown49640002005-06-23 22:02:58 -07002247 else
NeilBrowne34ac862005-07-07 17:59:30 -07002248 WRITE32(NFS4_FH_PERSISTENT|NFS4_FH_VOL_RENAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 }
2250 if (bmval0 & FATTR4_WORD0_CHANGE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 if ((buflen -= 8) < 0)
2252 goto out_resource;
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002253 write_change(&p, &stat, dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 }
2255 if (bmval0 & FATTR4_WORD0_SIZE) {
2256 if ((buflen -= 8) < 0)
2257 goto out_resource;
2258 WRITE64(stat.size);
2259 }
2260 if (bmval0 & FATTR4_WORD0_LINK_SUPPORT) {
2261 if ((buflen -= 4) < 0)
2262 goto out_resource;
2263 WRITE32(1);
2264 }
2265 if (bmval0 & FATTR4_WORD0_SYMLINK_SUPPORT) {
2266 if ((buflen -= 4) < 0)
2267 goto out_resource;
2268 WRITE32(1);
2269 }
2270 if (bmval0 & FATTR4_WORD0_NAMED_ATTR) {
2271 if ((buflen -= 4) < 0)
2272 goto out_resource;
2273 WRITE32(0);
2274 }
2275 if (bmval0 & FATTR4_WORD0_FSID) {
2276 if ((buflen -= 16) < 0)
2277 goto out_resource;
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002278 if (exp->ex_fslocs.migrated) {
2279 WRITE64(NFS4_REFERRAL_FSID_MAJOR);
2280 WRITE64(NFS4_REFERRAL_FSID_MINOR);
NeilBrownaf6a4e22007-02-14 00:33:12 -08002281 } else switch(fsid_source(fhp)) {
2282 case FSIDSOURCE_FSID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 WRITE64((u64)exp->ex_fsid);
2284 WRITE64((u64)0);
NeilBrownaf6a4e22007-02-14 00:33:12 -08002285 break;
2286 case FSIDSOURCE_DEV:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 WRITE32(0);
2288 WRITE32(MAJOR(stat.dev));
2289 WRITE32(0);
2290 WRITE32(MINOR(stat.dev));
NeilBrownaf6a4e22007-02-14 00:33:12 -08002291 break;
2292 case FSIDSOURCE_UUID:
2293 WRITEMEM(exp->ex_uuid, 16);
2294 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 }
2296 }
2297 if (bmval0 & FATTR4_WORD0_UNIQUE_HANDLES) {
2298 if ((buflen -= 4) < 0)
2299 goto out_resource;
2300 WRITE32(0);
2301 }
2302 if (bmval0 & FATTR4_WORD0_LEASE_TIME) {
2303 if ((buflen -= 4) < 0)
2304 goto out_resource;
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +03002305 WRITE32(nn->nfsd4_lease);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 }
2307 if (bmval0 & FATTR4_WORD0_RDATTR_ERROR) {
2308 if ((buflen -= 4) < 0)
2309 goto out_resource;
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002310 WRITE32(rdattr_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 }
2312 if (bmval0 & FATTR4_WORD0_ACL) {
2313 struct nfs4_ace *ace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314
2315 if (acl == NULL) {
2316 if ((buflen -= 4) < 0)
2317 goto out_resource;
2318
2319 WRITE32(0);
2320 goto out_acl;
2321 }
2322 if ((buflen -= 4) < 0)
2323 goto out_resource;
2324 WRITE32(acl->naces);
2325
J. Bruce Fields28e05dd2007-02-16 01:28:30 -08002326 for (ace = acl->aces; ace < acl->aces + acl->naces; ace++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 if ((buflen -= 4*3) < 0)
2328 goto out_resource;
2329 WRITE32(ace->type);
2330 WRITE32(ace->flag);
2331 WRITE32(ace->access_mask & NFS4_ACE_MASK_ALL);
Eric W. Biedermanab8e4ae2013-02-02 05:18:08 -08002332 status = nfsd4_encode_aclname(rqstp, ace, &p, &buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 if (status == nfserr_resource)
2334 goto out_resource;
2335 if (status)
2336 goto out;
2337 }
2338 }
2339out_acl:
2340 if (bmval0 & FATTR4_WORD0_ACLSUPPORT) {
2341 if ((buflen -= 4) < 0)
2342 goto out_resource;
2343 WRITE32(aclsupport ?
2344 ACL4_SUPPORT_ALLOW_ACL|ACL4_SUPPORT_DENY_ACL : 0);
2345 }
2346 if (bmval0 & FATTR4_WORD0_CANSETTIME) {
2347 if ((buflen -= 4) < 0)
2348 goto out_resource;
2349 WRITE32(1);
2350 }
2351 if (bmval0 & FATTR4_WORD0_CASE_INSENSITIVE) {
2352 if ((buflen -= 4) < 0)
2353 goto out_resource;
J. Bruce Fields2930d382012-06-05 16:52:06 -04002354 WRITE32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 }
2356 if (bmval0 & FATTR4_WORD0_CASE_PRESERVING) {
2357 if ((buflen -= 4) < 0)
2358 goto out_resource;
2359 WRITE32(1);
2360 }
2361 if (bmval0 & FATTR4_WORD0_CHOWN_RESTRICTED) {
2362 if ((buflen -= 4) < 0)
2363 goto out_resource;
2364 WRITE32(1);
2365 }
2366 if (bmval0 & FATTR4_WORD0_FILEHANDLE) {
2367 buflen -= (XDR_QUADLEN(fhp->fh_handle.fh_size) << 2) + 4;
2368 if (buflen < 0)
2369 goto out_resource;
2370 WRITE32(fhp->fh_handle.fh_size);
2371 WRITEMEM(&fhp->fh_handle.fh_base, fhp->fh_handle.fh_size);
2372 }
2373 if (bmval0 & FATTR4_WORD0_FILEID) {
2374 if ((buflen -= 8) < 0)
2375 goto out_resource;
Peter Staubach40ee5dc2007-08-16 12:10:07 -04002376 WRITE64(stat.ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 }
2378 if (bmval0 & FATTR4_WORD0_FILES_AVAIL) {
2379 if ((buflen -= 8) < 0)
2380 goto out_resource;
2381 WRITE64((u64) statfs.f_ffree);
2382 }
2383 if (bmval0 & FATTR4_WORD0_FILES_FREE) {
2384 if ((buflen -= 8) < 0)
2385 goto out_resource;
2386 WRITE64((u64) statfs.f_ffree);
2387 }
2388 if (bmval0 & FATTR4_WORD0_FILES_TOTAL) {
2389 if ((buflen -= 8) < 0)
2390 goto out_resource;
2391 WRITE64((u64) statfs.f_files);
2392 }
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002393 if (bmval0 & FATTR4_WORD0_FS_LOCATIONS) {
2394 status = nfsd4_encode_fs_locations(rqstp, exp, &p, &buflen);
2395 if (status == nfserr_resource)
2396 goto out_resource;
2397 if (status)
2398 goto out;
2399 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 if (bmval0 & FATTR4_WORD0_HOMOGENEOUS) {
2401 if ((buflen -= 4) < 0)
2402 goto out_resource;
2403 WRITE32(1);
2404 }
2405 if (bmval0 & FATTR4_WORD0_MAXFILESIZE) {
2406 if ((buflen -= 8) < 0)
2407 goto out_resource;
Christoph Hellwigaea240f2013-11-14 05:50:10 -08002408 WRITE64(exp->ex_path.mnt->mnt_sb->s_maxbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 }
2410 if (bmval0 & FATTR4_WORD0_MAXLINK) {
2411 if ((buflen -= 4) < 0)
2412 goto out_resource;
2413 WRITE32(255);
2414 }
2415 if (bmval0 & FATTR4_WORD0_MAXNAME) {
2416 if ((buflen -= 4) < 0)
2417 goto out_resource;
J. Bruce Fieldsa16e92e2007-09-28 16:45:51 -04002418 WRITE32(statfs.f_namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 }
2420 if (bmval0 & FATTR4_WORD0_MAXREAD) {
2421 if ((buflen -= 8) < 0)
2422 goto out_resource;
Greg Banks7adae482006-10-04 02:15:47 -07002423 WRITE64((u64) svc_max_payload(rqstp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 }
2425 if (bmval0 & FATTR4_WORD0_MAXWRITE) {
2426 if ((buflen -= 8) < 0)
2427 goto out_resource;
Greg Banks7adae482006-10-04 02:15:47 -07002428 WRITE64((u64) svc_max_payload(rqstp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 }
2430 if (bmval1 & FATTR4_WORD1_MODE) {
2431 if ((buflen -= 4) < 0)
2432 goto out_resource;
2433 WRITE32(stat.mode & S_IALLUGO);
2434 }
2435 if (bmval1 & FATTR4_WORD1_NO_TRUNC) {
2436 if ((buflen -= 4) < 0)
2437 goto out_resource;
2438 WRITE32(1);
2439 }
2440 if (bmval1 & FATTR4_WORD1_NUMLINKS) {
2441 if ((buflen -= 4) < 0)
2442 goto out_resource;
2443 WRITE32(stat.nlink);
2444 }
2445 if (bmval1 & FATTR4_WORD1_OWNER) {
2446 status = nfsd4_encode_user(rqstp, stat.uid, &p, &buflen);
2447 if (status == nfserr_resource)
2448 goto out_resource;
2449 if (status)
2450 goto out;
2451 }
2452 if (bmval1 & FATTR4_WORD1_OWNER_GROUP) {
2453 status = nfsd4_encode_group(rqstp, stat.gid, &p, &buflen);
2454 if (status == nfserr_resource)
2455 goto out_resource;
2456 if (status)
2457 goto out;
2458 }
2459 if (bmval1 & FATTR4_WORD1_RAWDEV) {
2460 if ((buflen -= 8) < 0)
2461 goto out_resource;
2462 WRITE32((u32) MAJOR(stat.rdev));
2463 WRITE32((u32) MINOR(stat.rdev));
2464 }
2465 if (bmval1 & FATTR4_WORD1_SPACE_AVAIL) {
2466 if ((buflen -= 8) < 0)
2467 goto out_resource;
2468 dummy64 = (u64)statfs.f_bavail * (u64)statfs.f_bsize;
2469 WRITE64(dummy64);
2470 }
2471 if (bmval1 & FATTR4_WORD1_SPACE_FREE) {
2472 if ((buflen -= 8) < 0)
2473 goto out_resource;
2474 dummy64 = (u64)statfs.f_bfree * (u64)statfs.f_bsize;
2475 WRITE64(dummy64);
2476 }
2477 if (bmval1 & FATTR4_WORD1_SPACE_TOTAL) {
2478 if ((buflen -= 8) < 0)
2479 goto out_resource;
2480 dummy64 = (u64)statfs.f_blocks * (u64)statfs.f_bsize;
2481 WRITE64(dummy64);
2482 }
2483 if (bmval1 & FATTR4_WORD1_SPACE_USED) {
2484 if ((buflen -= 8) < 0)
2485 goto out_resource;
2486 dummy64 = (u64)stat.blocks << 9;
2487 WRITE64(dummy64);
2488 }
2489 if (bmval1 & FATTR4_WORD1_TIME_ACCESS) {
2490 if ((buflen -= 12) < 0)
2491 goto out_resource;
Bryan Schumakerbf8d9092013-04-19 16:09:38 -04002492 WRITE64((s64)stat.atime.tv_sec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 WRITE32(stat.atime.tv_nsec);
2494 }
2495 if (bmval1 & FATTR4_WORD1_TIME_DELTA) {
2496 if ((buflen -= 12) < 0)
2497 goto out_resource;
2498 WRITE32(0);
2499 WRITE32(1);
2500 WRITE32(0);
2501 }
2502 if (bmval1 & FATTR4_WORD1_TIME_METADATA) {
2503 if ((buflen -= 12) < 0)
2504 goto out_resource;
Bryan Schumakerbf8d9092013-04-19 16:09:38 -04002505 WRITE64((s64)stat.ctime.tv_sec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 WRITE32(stat.ctime.tv_nsec);
2507 }
2508 if (bmval1 & FATTR4_WORD1_TIME_MODIFY) {
2509 if ((buflen -= 12) < 0)
2510 goto out_resource;
Bryan Schumakerbf8d9092013-04-19 16:09:38 -04002511 WRITE64((s64)stat.mtime.tv_sec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 WRITE32(stat.mtime.tv_nsec);
2513 }
2514 if (bmval1 & FATTR4_WORD1_MOUNTED_ON_FILEID) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 if ((buflen -= 8) < 0)
2516 goto out_resource;
Frank Filz406a7ea2007-11-27 11:34:05 -08002517 /*
2518 * Get parent's attributes if not ignoring crossmount
2519 * and this is the root of a cross-mounted filesystem.
2520 */
2521 if (ignore_crossmnt == 0 &&
J. Bruce Fieldsae7095a2012-10-01 17:50:56 -04002522 dentry == exp->ex_path.mnt->mnt_root)
2523 get_parent_attributes(exp, &stat);
Peter Staubach40ee5dc2007-08-16 12:10:07 -04002524 WRITE64(stat.ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 }
David Quigley18032ca2013-05-02 13:19:10 -04002526 if (bmval2 & FATTR4_WORD2_SECURITY_LABEL) {
2527 status = nfsd4_encode_security_label(rqstp, context,
2528 contextlen, &p, &buflen);
2529 if (status)
2530 goto out;
2531 }
Benny Halevy8c18f202009-04-03 08:29:14 +03002532 if (bmval2 & FATTR4_WORD2_SUPPATTR_EXCLCREAT) {
2533 WRITE32(3);
2534 WRITE32(NFSD_SUPPATTR_EXCLCREAT_WORD0);
2535 WRITE32(NFSD_SUPPATTR_EXCLCREAT_WORD1);
2536 WRITE32(NFSD_SUPPATTR_EXCLCREAT_WORD2);
2537 }
Andy Adamson7e705702009-04-03 08:29:11 +03002538
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 *attrlenp = htonl((char *)p - (char *)attrlenp - 4);
J. Bruce Fields84822d02012-12-14 17:57:50 -05002540 *buffer = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 status = nfs_ok;
2542
2543out:
J. Bruce Fieldsba4e55b2013-05-15 10:27:52 -04002544#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
David Quigley18032ca2013-05-02 13:19:10 -04002545 if (context)
2546 security_release_secctx(context, contextlen);
J. Bruce Fieldsba4e55b2013-05-15 10:27:52 -04002547#endif /* CONFIG_NFSD_V4_SECURITY_LABEL */
J. Bruce Fields28e05dd2007-02-16 01:28:30 -08002548 kfree(acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 if (fhp == &tempfh)
2550 fh_put(&tempfh);
2551 return status;
2552out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07002553 status = nfserrno(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 goto out;
2555out_resource:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 status = nfserr_resource;
2557 goto out;
2558out_serverfault:
2559 status = nfserr_serverfault;
2560 goto out;
2561}
2562
J. Bruce Fieldsc0ce6ec2008-02-11 15:48:47 -05002563static inline int attributes_need_mount(u32 *bmval)
2564{
2565 if (bmval[0] & ~(FATTR4_WORD0_RDATTR_ERROR | FATTR4_WORD0_LEASE_TIME))
2566 return 1;
2567 if (bmval[1] & ~FATTR4_WORD1_MOUNTED_ON_FILEID)
2568 return 1;
2569 return 0;
2570}
2571
Al Virob37ad282006-10-19 23:28:59 -07002572static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573nfsd4_encode_dirent_fattr(struct nfsd4_readdir *cd,
J. Bruce Fields84822d02012-12-14 17:57:50 -05002574 const char *name, int namlen, __be32 **p, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575{
2576 struct svc_export *exp = cd->rd_fhp->fh_export;
2577 struct dentry *dentry;
Al Virob37ad282006-10-19 23:28:59 -07002578 __be32 nfserr;
Frank Filz406a7ea2007-11-27 11:34:05 -08002579 int ignore_crossmnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580
2581 dentry = lookup_one_len(name, cd->rd_fhp->fh_dentry, namlen);
2582 if (IS_ERR(dentry))
2583 return nfserrno(PTR_ERR(dentry));
J. Bruce Fieldsb2c0cea2009-05-05 19:04:29 -04002584 if (!dentry->d_inode) {
2585 /*
2586 * nfsd_buffered_readdir drops the i_mutex between
2587 * readdir and calling this callback, leaving a window
2588 * where this directory entry could have gone away.
2589 */
2590 dput(dentry);
2591 return nfserr_noent;
2592 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593
2594 exp_get(exp);
Frank Filz406a7ea2007-11-27 11:34:05 -08002595 /*
2596 * In the case of a mountpoint, the client may be asking for
2597 * attributes that are only properties of the underlying filesystem
2598 * as opposed to the cross-mounted file system. In such a case,
2599 * we will not follow the cross mount and will fill the attribtutes
2600 * directly from the mountpoint dentry.
2601 */
J. Bruce Fields3227fa42009-10-25 21:43:01 -04002602 if (nfsd_mountpoint(dentry, exp)) {
J.Bruce Fields021d3a72006-12-13 00:35:24 -08002603 int err;
2604
J. Bruce Fields3227fa42009-10-25 21:43:01 -04002605 if (!(exp->ex_flags & NFSEXP_V4ROOT)
2606 && !attributes_need_mount(cd->rd_bmval)) {
2607 ignore_crossmnt = 1;
2608 goto out_encode;
2609 }
Andy Adamsondcb488a32007-07-17 04:04:51 -07002610 /*
2611 * Why the heck aren't we just using nfsd_lookup??
2612 * Different "."/".." handling? Something else?
2613 * At least, add a comment here to explain....
2614 */
J.Bruce Fields021d3a72006-12-13 00:35:24 -08002615 err = nfsd_cross_mnt(cd->rd_rqstp, &dentry, &exp);
2616 if (err) {
2617 nfserr = nfserrno(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 goto out_put;
2619 }
Andy Adamsondcb488a32007-07-17 04:04:51 -07002620 nfserr = check_nfsd_access(exp, cd->rd_rqstp);
2621 if (nfserr)
2622 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623
2624 }
J. Bruce Fields3227fa42009-10-25 21:43:01 -04002625out_encode:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 nfserr = nfsd4_encode_fattr(NULL, exp, dentry, p, buflen, cd->rd_bmval,
Frank Filz406a7ea2007-11-27 11:34:05 -08002627 cd->rd_rqstp, ignore_crossmnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628out_put:
2629 dput(dentry);
2630 exp_put(exp);
2631 return nfserr;
2632}
2633
Al Viro2ebbc012006-10-19 23:28:58 -07002634static __be32 *
Al Virob37ad282006-10-19 23:28:59 -07002635nfsd4_encode_rdattr_error(__be32 *p, int buflen, __be32 nfserr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636{
Al Viro2ebbc012006-10-19 23:28:58 -07002637 __be32 *attrlenp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638
2639 if (buflen < 6)
2640 return NULL;
2641 *p++ = htonl(2);
2642 *p++ = htonl(FATTR4_WORD0_RDATTR_ERROR); /* bmval0 */
2643 *p++ = htonl(0); /* bmval1 */
2644
2645 attrlenp = p++;
2646 *p++ = nfserr; /* no htonl */
2647 *attrlenp = htonl((char *)p - (char *)attrlenp - 4);
2648 return p;
2649}
2650
2651static int
NeilBrowna0ad13e2007-01-26 00:57:10 -08002652nfsd4_encode_dirent(void *ccdv, const char *name, int namlen,
2653 loff_t offset, u64 ino, unsigned int d_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654{
NeilBrowna0ad13e2007-01-26 00:57:10 -08002655 struct readdir_cd *ccd = ccdv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 struct nfsd4_readdir *cd = container_of(ccd, struct nfsd4_readdir, common);
2657 int buflen;
Al Viro2ebbc012006-10-19 23:28:58 -07002658 __be32 *p = cd->buffer;
J. Bruce Fieldsb2c0cea2009-05-05 19:04:29 -04002659 __be32 *cookiep;
Al Virob37ad282006-10-19 23:28:59 -07002660 __be32 nfserr = nfserr_toosmall;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661
2662 /* In nfsv4, "." and ".." never make it onto the wire.. */
2663 if (name && isdotent(name, namlen)) {
2664 cd->common.err = nfs_ok;
2665 return 0;
2666 }
2667
2668 if (cd->offset)
2669 xdr_encode_hyper(cd->offset, (u64) offset);
2670
2671 buflen = cd->buflen - 4 - XDR_QUADLEN(namlen);
2672 if (buflen < 0)
2673 goto fail;
2674
2675 *p++ = xdr_one; /* mark entry present */
J. Bruce Fieldsb2c0cea2009-05-05 19:04:29 -04002676 cookiep = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 p = xdr_encode_hyper(p, NFS_OFFSET_MAX); /* offset of next entry */
2678 p = xdr_encode_array(p, name, namlen); /* name length & name */
2679
J. Bruce Fields84822d02012-12-14 17:57:50 -05002680 nfserr = nfsd4_encode_dirent_fattr(cd, name, namlen, &p, buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 switch (nfserr) {
2682 case nfs_ok:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 break;
2684 case nfserr_resource:
2685 nfserr = nfserr_toosmall;
2686 goto fail;
J. Bruce Fieldsb2c0cea2009-05-05 19:04:29 -04002687 case nfserr_noent:
2688 goto skip_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 default:
2690 /*
2691 * If the client requested the RDATTR_ERROR attribute,
2692 * we stuff the error code into this attribute
2693 * and continue. If this attribute was not requested,
2694 * then in accordance with the spec, we fail the
2695 * entire READDIR operation(!)
2696 */
2697 if (!(cd->rd_bmval[0] & FATTR4_WORD0_RDATTR_ERROR))
2698 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 p = nfsd4_encode_rdattr_error(p, buflen, nfserr);
Fred Isaman34081ef2006-01-18 17:43:40 -08002700 if (p == NULL) {
2701 nfserr = nfserr_toosmall;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 goto fail;
Fred Isaman34081ef2006-01-18 17:43:40 -08002703 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 }
2705 cd->buflen -= (p - cd->buffer);
2706 cd->buffer = p;
J. Bruce Fieldsb2c0cea2009-05-05 19:04:29 -04002707 cd->offset = cookiep;
2708skip_entry:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 cd->common.err = nfs_ok;
2710 return 0;
2711fail:
2712 cd->common.err = nfserr;
2713 return -EINVAL;
2714}
2715
Benny Halevye2f282b2008-08-12 20:45:07 +03002716static void
2717nfsd4_encode_stateid(struct nfsd4_compoundres *resp, stateid_t *sid)
2718{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002719 __be32 *p;
Benny Halevye2f282b2008-08-12 20:45:07 +03002720
2721 RESERVE_SPACE(sizeof(stateid_t));
2722 WRITE32(sid->si_generation);
2723 WRITEMEM(&sid->si_opaque, sizeof(stateid_opaque_t));
2724 ADJUST_ARGS();
2725}
2726
Benny Halevy695e12f2008-07-04 14:38:33 +03002727static __be32
Al Virob37ad282006-10-19 23:28:59 -07002728nfsd4_encode_access(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_access *access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002730 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731
2732 if (!nfserr) {
2733 RESERVE_SPACE(8);
2734 WRITE32(access->ac_supported);
2735 WRITE32(access->ac_resp_access);
2736 ADJUST_ARGS();
2737 }
Benny Halevy695e12f2008-07-04 14:38:33 +03002738 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739}
2740
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002741static __be32 nfsd4_encode_bind_conn_to_session(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_bind_conn_to_session *bcts)
2742{
2743 __be32 *p;
2744
2745 if (!nfserr) {
2746 RESERVE_SPACE(NFS4_MAX_SESSIONID_LEN + 8);
2747 WRITEMEM(bcts->sessionid.data, NFS4_MAX_SESSIONID_LEN);
2748 WRITE32(bcts->dir);
J. Bruce Fields6e67b5d2012-09-13 09:49:43 -04002749 /* Sorry, we do not yet support RDMA over 4.1: */
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002750 WRITE32(0);
2751 ADJUST_ARGS();
2752 }
2753 return nfserr;
2754}
2755
Benny Halevy695e12f2008-07-04 14:38:33 +03002756static __be32
Al Virob37ad282006-10-19 23:28:59 -07002757nfsd4_encode_close(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758{
Benny Halevye2f282b2008-08-12 20:45:07 +03002759 if (!nfserr)
2760 nfsd4_encode_stateid(resp, &close->cl_stateid);
2761
Benny Halevy695e12f2008-07-04 14:38:33 +03002762 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763}
2764
2765
Benny Halevy695e12f2008-07-04 14:38:33 +03002766static __be32
Al Virob37ad282006-10-19 23:28:59 -07002767nfsd4_encode_commit(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_commit *commit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002769 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770
2771 if (!nfserr) {
Chuck Leverab4684d2012-03-02 17:13:50 -05002772 RESERVE_SPACE(NFS4_VERIFIER_SIZE);
2773 WRITEMEM(commit->co_verf.data, NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 ADJUST_ARGS();
2775 }
Benny Halevy695e12f2008-07-04 14:38:33 +03002776 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777}
2778
Benny Halevy695e12f2008-07-04 14:38:33 +03002779static __be32
Al Virob37ad282006-10-19 23:28:59 -07002780nfsd4_encode_create(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_create *create)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002782 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783
2784 if (!nfserr) {
2785 RESERVE_SPACE(32);
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002786 write_cinfo(&p, &create->cr_cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 WRITE32(2);
2788 WRITE32(create->cr_bmval[0]);
2789 WRITE32(create->cr_bmval[1]);
2790 ADJUST_ARGS();
2791 }
Benny Halevy695e12f2008-07-04 14:38:33 +03002792 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793}
2794
Al Virob37ad282006-10-19 23:28:59 -07002795static __be32
2796nfsd4_encode_getattr(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_getattr *getattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797{
2798 struct svc_fh *fhp = getattr->ga_fhp;
2799 int buflen;
2800
2801 if (nfserr)
2802 return nfserr;
2803
2804 buflen = resp->end - resp->p - (COMPOUND_ERR_SLACK_SPACE >> 2);
2805 nfserr = nfsd4_encode_fattr(fhp, fhp->fh_export, fhp->fh_dentry,
J. Bruce Fields84822d02012-12-14 17:57:50 -05002806 &resp->p, buflen, getattr->ga_bmval,
Frank Filz406a7ea2007-11-27 11:34:05 -08002807 resp->rqstp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 return nfserr;
2809}
2810
Benny Halevy695e12f2008-07-04 14:38:33 +03002811static __be32
2812nfsd4_encode_getfh(struct nfsd4_compoundres *resp, __be32 nfserr, struct svc_fh **fhpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813{
Benny Halevy695e12f2008-07-04 14:38:33 +03002814 struct svc_fh *fhp = *fhpp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 unsigned int len;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002816 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817
2818 if (!nfserr) {
2819 len = fhp->fh_handle.fh_size;
2820 RESERVE_SPACE(len + 4);
2821 WRITE32(len);
2822 WRITEMEM(&fhp->fh_handle.fh_base, len);
2823 ADJUST_ARGS();
2824 }
Benny Halevy695e12f2008-07-04 14:38:33 +03002825 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826}
2827
2828/*
2829* Including all fields other than the name, a LOCK4denied structure requires
2830* 8(clientid) + 4(namelen) + 8(offset) + 8(length) + 4(type) = 32 bytes.
2831*/
2832static void
2833nfsd4_encode_lock_denied(struct nfsd4_compoundres *resp, struct nfsd4_lock_denied *ld)
2834{
J. Bruce Fields7c13f342011-08-30 22:15:47 -04002835 struct xdr_netobj *conf = &ld->ld_owner;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002836 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837
J. Bruce Fields7c13f342011-08-30 22:15:47 -04002838 RESERVE_SPACE(32 + XDR_LEN(conf->len));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 WRITE64(ld->ld_start);
2840 WRITE64(ld->ld_length);
2841 WRITE32(ld->ld_type);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04002842 if (conf->len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 WRITEMEM(&ld->ld_clientid, 8);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04002844 WRITE32(conf->len);
2845 WRITEMEM(conf->data, conf->len);
2846 kfree(conf->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 } else { /* non - nfsv4 lock in conflict, no clientid nor owner */
2848 WRITE64((u64)0); /* clientid */
2849 WRITE32(0); /* length of owner name */
2850 }
2851 ADJUST_ARGS();
2852}
2853
Benny Halevy695e12f2008-07-04 14:38:33 +03002854static __be32
Al Virob37ad282006-10-19 23:28:59 -07002855nfsd4_encode_lock(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856{
Benny Halevye2f282b2008-08-12 20:45:07 +03002857 if (!nfserr)
2858 nfsd4_encode_stateid(resp, &lock->lk_resp_stateid);
2859 else if (nfserr == nfserr_denied)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 nfsd4_encode_lock_denied(resp, &lock->lk_denied);
2861
Benny Halevy695e12f2008-07-04 14:38:33 +03002862 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863}
2864
Benny Halevy695e12f2008-07-04 14:38:33 +03002865static __be32
Al Virob37ad282006-10-19 23:28:59 -07002866nfsd4_encode_lockt(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867{
2868 if (nfserr == nfserr_denied)
2869 nfsd4_encode_lock_denied(resp, &lockt->lt_denied);
Benny Halevy695e12f2008-07-04 14:38:33 +03002870 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871}
2872
Benny Halevy695e12f2008-07-04 14:38:33 +03002873static __be32
Al Virob37ad282006-10-19 23:28:59 -07002874nfsd4_encode_locku(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875{
Benny Halevye2f282b2008-08-12 20:45:07 +03002876 if (!nfserr)
2877 nfsd4_encode_stateid(resp, &locku->lu_stateid);
2878
Benny Halevy695e12f2008-07-04 14:38:33 +03002879 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880}
2881
2882
Benny Halevy695e12f2008-07-04 14:38:33 +03002883static __be32
Al Virob37ad282006-10-19 23:28:59 -07002884nfsd4_encode_link(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_link *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002886 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887
2888 if (!nfserr) {
2889 RESERVE_SPACE(20);
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002890 write_cinfo(&p, &link->li_cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 ADJUST_ARGS();
2892 }
Benny Halevy695e12f2008-07-04 14:38:33 +03002893 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894}
2895
2896
Benny Halevy695e12f2008-07-04 14:38:33 +03002897static __be32
Al Virob37ad282006-10-19 23:28:59 -07002898nfsd4_encode_open(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002900 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901
2902 if (nfserr)
2903 goto out;
2904
Benny Halevye2f282b2008-08-12 20:45:07 +03002905 nfsd4_encode_stateid(resp, &open->op_stateid);
2906 RESERVE_SPACE(40);
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002907 write_cinfo(&p, &open->op_cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 WRITE32(open->op_rflags);
2909 WRITE32(2);
2910 WRITE32(open->op_bmval[0]);
2911 WRITE32(open->op_bmval[1]);
2912 WRITE32(open->op_delegate_type);
2913 ADJUST_ARGS();
2914
2915 switch (open->op_delegate_type) {
2916 case NFS4_OPEN_DELEGATE_NONE:
2917 break;
2918 case NFS4_OPEN_DELEGATE_READ:
Benny Halevye2f282b2008-08-12 20:45:07 +03002919 nfsd4_encode_stateid(resp, &open->op_delegate_stateid);
2920 RESERVE_SPACE(20);
NeilBrown7b190fe2005-06-23 22:03:23 -07002921 WRITE32(open->op_recall);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922
2923 /*
2924 * TODO: ACE's in delegations
2925 */
2926 WRITE32(NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
2927 WRITE32(0);
2928 WRITE32(0);
2929 WRITE32(0); /* XXX: is NULL principal ok? */
2930 ADJUST_ARGS();
2931 break;
2932 case NFS4_OPEN_DELEGATE_WRITE:
Benny Halevye2f282b2008-08-12 20:45:07 +03002933 nfsd4_encode_stateid(resp, &open->op_delegate_stateid);
2934 RESERVE_SPACE(32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 WRITE32(0);
2936
2937 /*
2938 * TODO: space_limit's in delegations
2939 */
2940 WRITE32(NFS4_LIMIT_SIZE);
2941 WRITE32(~(u32)0);
2942 WRITE32(~(u32)0);
2943
2944 /*
2945 * TODO: ACE's in delegations
2946 */
2947 WRITE32(NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
2948 WRITE32(0);
2949 WRITE32(0);
2950 WRITE32(0); /* XXX: is NULL principal ok? */
2951 ADJUST_ARGS();
2952 break;
Benny Halevyd24433c2012-02-16 20:57:17 +02002953 case NFS4_OPEN_DELEGATE_NONE_EXT: /* 4.1 */
2954 switch (open->op_why_no_deleg) {
2955 case WND4_CONTENTION:
2956 case WND4_RESOURCE:
2957 RESERVE_SPACE(8);
2958 WRITE32(open->op_why_no_deleg);
2959 WRITE32(0); /* deleg signaling not supported yet */
2960 break;
2961 default:
2962 RESERVE_SPACE(4);
2963 WRITE32(open->op_why_no_deleg);
2964 }
2965 ADJUST_ARGS();
2966 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 default:
2968 BUG();
2969 }
2970 /* XXX save filehandle here */
2971out:
Benny Halevy695e12f2008-07-04 14:38:33 +03002972 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973}
2974
Benny Halevy695e12f2008-07-04 14:38:33 +03002975static __be32
Al Virob37ad282006-10-19 23:28:59 -07002976nfsd4_encode_open_confirm(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977{
Benny Halevye2f282b2008-08-12 20:45:07 +03002978 if (!nfserr)
2979 nfsd4_encode_stateid(resp, &oc->oc_resp_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980
Benny Halevy695e12f2008-07-04 14:38:33 +03002981 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982}
2983
Benny Halevy695e12f2008-07-04 14:38:33 +03002984static __be32
Al Virob37ad282006-10-19 23:28:59 -07002985nfsd4_encode_open_downgrade(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986{
Benny Halevye2f282b2008-08-12 20:45:07 +03002987 if (!nfserr)
2988 nfsd4_encode_stateid(resp, &od->od_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989
Benny Halevy695e12f2008-07-04 14:38:33 +03002990 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991}
2992
Al Virob37ad282006-10-19 23:28:59 -07002993static __be32
2994nfsd4_encode_read(struct nfsd4_compoundres *resp, __be32 nfserr,
NeilBrown44524352006-10-04 02:15:46 -07002995 struct nfsd4_read *read)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996{
2997 u32 eof;
J. Bruce Fieldsafc59402012-12-10 18:01:37 -05002998 int v;
2999 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 unsigned long maxcount;
3001 long len;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003002 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003
3004 if (nfserr)
3005 return nfserr;
3006 if (resp->xbuf->page_len)
3007 return nfserr_resource;
3008
3009 RESERVE_SPACE(8); /* eof flag and byte count */
3010
Greg Banks7adae482006-10-04 02:15:47 -07003011 maxcount = svc_max_payload(resp->rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 if (maxcount > read->rd_length)
3013 maxcount = read->rd_length;
3014
3015 len = maxcount;
3016 v = 0;
3017 while (len > 0) {
J. Bruce Fieldsafc59402012-12-10 18:01:37 -05003018 page = *(resp->rqstp->rq_next_page);
3019 if (!page) { /* ran out of pages */
J. Bruce Fieldsd5f50b02012-12-04 18:25:10 -05003020 maxcount -= len;
3021 break;
3022 }
J. Bruce Fieldsafc59402012-12-10 18:01:37 -05003023 resp->rqstp->rq_vec[v].iov_base = page_address(page);
NeilBrown3cc03b12006-10-04 02:15:47 -07003024 resp->rqstp->rq_vec[v].iov_len =
NeilBrown44524352006-10-04 02:15:46 -07003025 len < PAGE_SIZE ? len : PAGE_SIZE;
J. Bruce Fieldsafc59402012-12-10 18:01:37 -05003026 resp->rqstp->rq_next_page++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 v++;
3028 len -= PAGE_SIZE;
3029 }
3030 read->rd_vlen = v;
3031
J. Bruce Fields039a87c2010-07-30 11:33:32 -04003032 nfserr = nfsd_read_file(read->rd_rqstp, read->rd_fhp, read->rd_filp,
NeilBrown3cc03b12006-10-04 02:15:47 -07003033 read->rd_offset, resp->rqstp->rq_vec, read->rd_vlen,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 &maxcount);
3035
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 if (nfserr)
3037 return nfserr;
NeilBrown44524352006-10-04 02:15:46 -07003038 eof = (read->rd_offset + maxcount >=
3039 read->rd_fhp->fh_dentry->d_inode->i_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040
3041 WRITE32(eof);
3042 WRITE32(maxcount);
3043 ADJUST_ARGS();
NeilBrown6ed6dec2006-04-10 22:55:32 -07003044 resp->xbuf->head[0].iov_len = (char*)p
3045 - (char*)resp->xbuf->head[0].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 resp->xbuf->page_len = maxcount;
3047
NeilBrown6ed6dec2006-04-10 22:55:32 -07003048 /* Use rest of head for padding and remaining ops: */
NeilBrown6ed6dec2006-04-10 22:55:32 -07003049 resp->xbuf->tail[0].iov_base = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 resp->xbuf->tail[0].iov_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 if (maxcount&3) {
NeilBrown6ed6dec2006-04-10 22:55:32 -07003052 RESERVE_SPACE(4);
3053 WRITE32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 resp->xbuf->tail[0].iov_base += maxcount&3;
3055 resp->xbuf->tail[0].iov_len = 4 - (maxcount&3);
NeilBrown6ed6dec2006-04-10 22:55:32 -07003056 ADJUST_ARGS();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 }
3058 return 0;
3059}
3060
Al Virob37ad282006-10-19 23:28:59 -07003061static __be32
3062nfsd4_encode_readlink(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_readlink *readlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063{
3064 int maxcount;
3065 char *page;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003066 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067
3068 if (nfserr)
3069 return nfserr;
3070 if (resp->xbuf->page_len)
3071 return nfserr_resource;
J. Bruce Fieldsafc59402012-12-10 18:01:37 -05003072 if (!*resp->rqstp->rq_next_page)
J. Bruce Fieldsd5f50b02012-12-04 18:25:10 -05003073 return nfserr_resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074
J. Bruce Fieldsafc59402012-12-10 18:01:37 -05003075 page = page_address(*(resp->rqstp->rq_next_page++));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076
3077 maxcount = PAGE_SIZE;
3078 RESERVE_SPACE(4);
3079
3080 /*
3081 * XXX: By default, the ->readlink() VFS op will truncate symlinks
3082 * if they would overflow the buffer. Is this kosher in NFSv4? If
3083 * not, one easy fix is: if ->readlink() precisely fills the buffer,
3084 * assume that truncation occurred, and return NFS4ERR_RESOURCE.
3085 */
3086 nfserr = nfsd_readlink(readlink->rl_rqstp, readlink->rl_fhp, page, &maxcount);
3087 if (nfserr == nfserr_isdir)
3088 return nfserr_inval;
3089 if (nfserr)
3090 return nfserr;
3091
3092 WRITE32(maxcount);
3093 ADJUST_ARGS();
NeilBrown6ed6dec2006-04-10 22:55:32 -07003094 resp->xbuf->head[0].iov_len = (char*)p
3095 - (char*)resp->xbuf->head[0].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 resp->xbuf->page_len = maxcount;
NeilBrown6ed6dec2006-04-10 22:55:32 -07003097
3098 /* Use rest of head for padding and remaining ops: */
NeilBrown6ed6dec2006-04-10 22:55:32 -07003099 resp->xbuf->tail[0].iov_base = p;
3100 resp->xbuf->tail[0].iov_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 if (maxcount&3) {
NeilBrown6ed6dec2006-04-10 22:55:32 -07003102 RESERVE_SPACE(4);
3103 WRITE32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 resp->xbuf->tail[0].iov_base += maxcount&3;
3105 resp->xbuf->tail[0].iov_len = 4 - (maxcount&3);
NeilBrown6ed6dec2006-04-10 22:55:32 -07003106 ADJUST_ARGS();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 }
3108 return 0;
3109}
3110
Al Virob37ad282006-10-19 23:28:59 -07003111static __be32
3112nfsd4_encode_readdir(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_readdir *readdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113{
3114 int maxcount;
3115 loff_t offset;
Al Viro2ebbc012006-10-19 23:28:58 -07003116 __be32 *page, *savep, *tailbase;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003117 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118
3119 if (nfserr)
3120 return nfserr;
3121 if (resp->xbuf->page_len)
3122 return nfserr_resource;
J. Bruce Fieldsafc59402012-12-10 18:01:37 -05003123 if (!*resp->rqstp->rq_next_page)
J. Bruce Fieldsd5f50b02012-12-04 18:25:10 -05003124 return nfserr_resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125
Chuck Leverab4684d2012-03-02 17:13:50 -05003126 RESERVE_SPACE(NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 savep = p;
3128
3129 /* XXX: Following NFSv3, we ignore the READDIR verifier for now. */
3130 WRITE32(0);
3131 WRITE32(0);
3132 ADJUST_ARGS();
3133 resp->xbuf->head[0].iov_len = ((char*)resp->p) - (char*)resp->xbuf->head[0].iov_base;
NeilBrownbb6e8a92006-04-10 22:55:33 -07003134 tailbase = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135
3136 maxcount = PAGE_SIZE;
3137 if (maxcount > readdir->rd_maxcount)
3138 maxcount = readdir->rd_maxcount;
3139
3140 /*
3141 * Convert from bytes to words, account for the two words already
3142 * written, make sure to leave two words at the end for the next
3143 * pointer and eof field.
3144 */
3145 maxcount = (maxcount >> 2) - 4;
3146 if (maxcount < 0) {
3147 nfserr = nfserr_toosmall;
3148 goto err_no_verf;
3149 }
3150
J. Bruce Fieldsafc59402012-12-10 18:01:37 -05003151 page = page_address(*(resp->rqstp->rq_next_page++));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 readdir->common.err = 0;
3153 readdir->buflen = maxcount;
3154 readdir->buffer = page;
3155 readdir->offset = NULL;
3156
3157 offset = readdir->rd_cookie;
3158 nfserr = nfsd_readdir(readdir->rd_rqstp, readdir->rd_fhp,
3159 &offset,
3160 &readdir->common, nfsd4_encode_dirent);
3161 if (nfserr == nfs_ok &&
3162 readdir->common.err == nfserr_toosmall &&
3163 readdir->buffer == page)
3164 nfserr = nfserr_toosmall;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165 if (nfserr)
3166 goto err_no_verf;
3167
3168 if (readdir->offset)
3169 xdr_encode_hyper(readdir->offset, offset);
3170
3171 p = readdir->buffer;
3172 *p++ = 0; /* no more entries */
3173 *p++ = htonl(readdir->common.err == nfserr_eof);
J. Bruce Fieldsafc59402012-12-10 18:01:37 -05003174 resp->xbuf->page_len = ((char*)p) -
3175 (char*)page_address(*(resp->rqstp->rq_next_page-1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176
NeilBrownbb6e8a92006-04-10 22:55:33 -07003177 /* Use rest of head for padding and remaining ops: */
NeilBrownbb6e8a92006-04-10 22:55:33 -07003178 resp->xbuf->tail[0].iov_base = tailbase;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179 resp->xbuf->tail[0].iov_len = 0;
3180 resp->p = resp->xbuf->tail[0].iov_base;
NeilBrownbb6e8a92006-04-10 22:55:33 -07003181 resp->end = resp->p + (PAGE_SIZE - resp->xbuf->head[0].iov_len)/4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182
3183 return 0;
3184err_no_verf:
3185 p = savep;
3186 ADJUST_ARGS();
3187 return nfserr;
3188}
3189
Benny Halevy695e12f2008-07-04 14:38:33 +03003190static __be32
Al Virob37ad282006-10-19 23:28:59 -07003191nfsd4_encode_remove(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_remove *remove)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003193 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194
3195 if (!nfserr) {
3196 RESERVE_SPACE(20);
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04003197 write_cinfo(&p, &remove->rm_cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 ADJUST_ARGS();
3199 }
Benny Halevy695e12f2008-07-04 14:38:33 +03003200 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201}
3202
Benny Halevy695e12f2008-07-04 14:38:33 +03003203static __be32
Al Virob37ad282006-10-19 23:28:59 -07003204nfsd4_encode_rename(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_rename *rename)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003206 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207
3208 if (!nfserr) {
3209 RESERVE_SPACE(40);
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04003210 write_cinfo(&p, &rename->rn_sinfo);
3211 write_cinfo(&p, &rename->rn_tinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212 ADJUST_ARGS();
3213 }
Benny Halevy695e12f2008-07-04 14:38:33 +03003214 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215}
3216
Benny Halevy695e12f2008-07-04 14:38:33 +03003217static __be32
Mi Jinlong22b6dee2010-12-27 14:29:57 +08003218nfsd4_do_encode_secinfo(struct nfsd4_compoundres *resp,
Chuck Levera77c8062013-03-16 15:55:10 -04003219 __be32 nfserr, struct svc_export *exp)
Andy Adamsondcb488a32007-07-17 04:04:51 -07003220{
Chuck Lever676e4eb2013-04-30 18:48:54 -04003221 u32 i, nflavs, supported;
J. Bruce Fields4796f452007-07-17 04:04:51 -07003222 struct exp_flavor_info *flavs;
3223 struct exp_flavor_info def_flavs[2];
Chuck Lever676e4eb2013-04-30 18:48:54 -04003224 __be32 *p, *flavorsp;
3225 static bool report = true;
Andy Adamsondcb488a32007-07-17 04:04:51 -07003226
3227 if (nfserr)
3228 goto out;
J. Bruce Fields4796f452007-07-17 04:04:51 -07003229 if (exp->ex_nflavors) {
3230 flavs = exp->ex_flavors;
3231 nflavs = exp->ex_nflavors;
3232 } else { /* Handling of some defaults in absence of real secinfo: */
3233 flavs = def_flavs;
3234 if (exp->ex_client->flavour->flavour == RPC_AUTH_UNIX) {
3235 nflavs = 2;
3236 flavs[0].pseudoflavor = RPC_AUTH_UNIX;
3237 flavs[1].pseudoflavor = RPC_AUTH_NULL;
3238 } else if (exp->ex_client->flavour->flavour == RPC_AUTH_GSS) {
3239 nflavs = 1;
3240 flavs[0].pseudoflavor
3241 = svcauth_gss_flavor(exp->ex_client);
3242 } else {
3243 nflavs = 1;
3244 flavs[0].pseudoflavor
3245 = exp->ex_client->flavour->flavour;
3246 }
3247 }
3248
Chuck Lever676e4eb2013-04-30 18:48:54 -04003249 supported = 0;
Andy Adamsondcb488a32007-07-17 04:04:51 -07003250 RESERVE_SPACE(4);
Chuck Lever676e4eb2013-04-30 18:48:54 -04003251 flavorsp = p++; /* to be backfilled later */
Andy Adamsondcb488a32007-07-17 04:04:51 -07003252 ADJUST_ARGS();
Chuck Lever676e4eb2013-04-30 18:48:54 -04003253
J. Bruce Fields4796f452007-07-17 04:04:51 -07003254 for (i = 0; i < nflavs; i++) {
Chuck Lever676e4eb2013-04-30 18:48:54 -04003255 rpc_authflavor_t pf = flavs[i].pseudoflavor;
Chuck Levera77c8062013-03-16 15:55:10 -04003256 struct rpcsec_gss_info info;
Andy Adamsondcb488a32007-07-17 04:04:51 -07003257
Chuck Lever676e4eb2013-04-30 18:48:54 -04003258 if (rpcauth_get_gssinfo(pf, &info) == 0) {
3259 supported++;
Kinglong Meea9f7b4a02013-12-09 19:31:21 +08003260 RESERVE_SPACE(4 + 4 + XDR_LEN(info.oid.len) + 4 + 4);
Andy Adamsondcb488a32007-07-17 04:04:51 -07003261 WRITE32(RPC_AUTH_GSS);
Chuck Levera77c8062013-03-16 15:55:10 -04003262 WRITE32(info.oid.len);
3263 WRITEMEM(info.oid.data, info.oid.len);
Chuck Levera77c8062013-03-16 15:55:10 -04003264 WRITE32(info.qop);
Chuck Levera77c8062013-03-16 15:55:10 -04003265 WRITE32(info.service);
Andy Adamsondcb488a32007-07-17 04:04:51 -07003266 ADJUST_ARGS();
Chuck Lever676e4eb2013-04-30 18:48:54 -04003267 } else if (pf < RPC_AUTH_MAXFLAVOR) {
3268 supported++;
Andy Adamsondcb488a32007-07-17 04:04:51 -07003269 RESERVE_SPACE(4);
Chuck Lever676e4eb2013-04-30 18:48:54 -04003270 WRITE32(pf);
Andy Adamsondcb488a32007-07-17 04:04:51 -07003271 ADJUST_ARGS();
Chuck Lever676e4eb2013-04-30 18:48:54 -04003272 } else {
3273 if (report)
3274 pr_warn("NFS: SECINFO: security flavor %u "
3275 "is not supported\n", pf);
Andy Adamsondcb488a32007-07-17 04:04:51 -07003276 }
3277 }
Chuck Levera77c8062013-03-16 15:55:10 -04003278
Chuck Lever676e4eb2013-04-30 18:48:54 -04003279 if (nflavs != supported)
3280 report = false;
3281 *flavorsp = htonl(supported);
3282
Andy Adamsondcb488a32007-07-17 04:04:51 -07003283out:
3284 if (exp)
3285 exp_put(exp);
Benny Halevy695e12f2008-07-04 14:38:33 +03003286 return nfserr;
Andy Adamsondcb488a32007-07-17 04:04:51 -07003287}
3288
Mi Jinlong22b6dee2010-12-27 14:29:57 +08003289static __be32
3290nfsd4_encode_secinfo(struct nfsd4_compoundres *resp, __be32 nfserr,
3291 struct nfsd4_secinfo *secinfo)
3292{
3293 return nfsd4_do_encode_secinfo(resp, nfserr, secinfo->si_exp);
3294}
3295
3296static __be32
3297nfsd4_encode_secinfo_no_name(struct nfsd4_compoundres *resp, __be32 nfserr,
3298 struct nfsd4_secinfo_no_name *secinfo)
3299{
3300 return nfsd4_do_encode_secinfo(resp, nfserr, secinfo->sin_exp);
3301}
3302
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303/*
3304 * The SETATTR encode routine is special -- it always encodes a bitmap,
3305 * regardless of the error status.
3306 */
Benny Halevy695e12f2008-07-04 14:38:33 +03003307static __be32
Al Virob37ad282006-10-19 23:28:59 -07003308nfsd4_encode_setattr(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_setattr *setattr)
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
David Quigley18032ca2013-05-02 13:19:10 -04003312 RESERVE_SPACE(16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 if (nfserr) {
David Quigley18032ca2013-05-02 13:19:10 -04003314 WRITE32(3);
3315 WRITE32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 WRITE32(0);
3317 WRITE32(0);
3318 }
3319 else {
David Quigley18032ca2013-05-02 13:19:10 -04003320 WRITE32(3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 WRITE32(setattr->sa_bmval[0]);
3322 WRITE32(setattr->sa_bmval[1]);
David Quigley18032ca2013-05-02 13:19:10 -04003323 WRITE32(setattr->sa_bmval[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324 }
3325 ADJUST_ARGS();
Benny Halevy695e12f2008-07-04 14:38:33 +03003326 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327}
3328
Benny Halevy695e12f2008-07-04 14:38:33 +03003329static __be32
Al Virob37ad282006-10-19 23:28:59 -07003330nfsd4_encode_setclientid(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_setclientid *scd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003332 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333
3334 if (!nfserr) {
Chuck Leverab4684d2012-03-02 17:13:50 -05003335 RESERVE_SPACE(8 + NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 WRITEMEM(&scd->se_clientid, 8);
Chuck Leverab4684d2012-03-02 17:13:50 -05003337 WRITEMEM(&scd->se_confirm, NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338 ADJUST_ARGS();
3339 }
3340 else if (nfserr == nfserr_clid_inuse) {
3341 RESERVE_SPACE(8);
3342 WRITE32(0);
3343 WRITE32(0);
3344 ADJUST_ARGS();
3345 }
Benny Halevy695e12f2008-07-04 14:38:33 +03003346 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347}
3348
Benny Halevy695e12f2008-07-04 14:38:33 +03003349static __be32
Al Virob37ad282006-10-19 23:28:59 -07003350nfsd4_encode_write(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_write *write)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003352 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353
3354 if (!nfserr) {
3355 RESERVE_SPACE(16);
3356 WRITE32(write->wr_bytes_written);
3357 WRITE32(write->wr_how_written);
Chuck Leverab4684d2012-03-02 17:13:50 -05003358 WRITEMEM(write->wr_verifier.data, NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359 ADJUST_ARGS();
3360 }
Benny Halevy695e12f2008-07-04 14:38:33 +03003361 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362}
3363
J. Bruce Fields57266a62013-04-13 14:27:29 -04003364static const u32 nfs4_minimal_spo_must_enforce[2] = {
3365 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
3366 1 << (OP_EXCHANGE_ID - 32) |
3367 1 << (OP_CREATE_SESSION - 32) |
3368 1 << (OP_DESTROY_SESSION - 32) |
3369 1 << (OP_DESTROY_CLIENTID - 32)
3370};
3371
Benny Halevy695e12f2008-07-04 14:38:33 +03003372static __be32
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003373nfsd4_encode_exchange_id(struct nfsd4_compoundres *resp, __be32 nfserr,
Andy Adamson2db134e2009-04-03 08:27:55 +03003374 struct nfsd4_exchange_id *exid)
3375{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003376 __be32 *p;
Andy Adamson0733d212009-04-03 08:28:01 +03003377 char *major_id;
3378 char *server_scope;
3379 int major_id_sz;
3380 int server_scope_sz;
3381 uint64_t minor_id = 0;
3382
3383 if (nfserr)
3384 return nfserr;
3385
3386 major_id = utsname()->nodename;
3387 major_id_sz = strlen(major_id);
3388 server_scope = utsname()->nodename;
3389 server_scope_sz = strlen(server_scope);
3390
3391 RESERVE_SPACE(
3392 8 /* eir_clientid */ +
3393 4 /* eir_sequenceid */ +
3394 4 /* eir_flags */ +
Kinglong Meea8bb84b2013-12-10 15:24:36 +08003395 4 /* spr_how */);
Andy Adamson0733d212009-04-03 08:28:01 +03003396
3397 WRITEMEM(&exid->clientid, 8);
3398 WRITE32(exid->seqid);
3399 WRITE32(exid->flags);
3400
Andy Adamson0733d212009-04-03 08:28:01 +03003401 WRITE32(exid->spa_how);
Kinglong Meea8bb84b2013-12-10 15:24:36 +08003402 ADJUST_ARGS();
3403
J. Bruce Fields57266a62013-04-13 14:27:29 -04003404 switch (exid->spa_how) {
3405 case SP4_NONE:
3406 break;
3407 case SP4_MACH_CRED:
Kinglong Meea8bb84b2013-12-10 15:24:36 +08003408 /* spo_must_enforce, spo_must_allow */
3409 RESERVE_SPACE(16);
3410
J. Bruce Fields57266a62013-04-13 14:27:29 -04003411 /* spo_must_enforce bitmap: */
3412 WRITE32(2);
3413 WRITE32(nfs4_minimal_spo_must_enforce[0]);
3414 WRITE32(nfs4_minimal_spo_must_enforce[1]);
3415 /* empty spo_must_allow bitmap: */
3416 WRITE32(0);
Kinglong Meea8bb84b2013-12-10 15:24:36 +08003417
3418 ADJUST_ARGS();
J. Bruce Fields57266a62013-04-13 14:27:29 -04003419 break;
3420 default:
3421 WARN_ON_ONCE(1);
3422 }
Andy Adamson0733d212009-04-03 08:28:01 +03003423
Kinglong Meea8bb84b2013-12-10 15:24:36 +08003424 RESERVE_SPACE(
3425 8 /* so_minor_id */ +
3426 4 /* so_major_id.len */ +
3427 (XDR_QUADLEN(major_id_sz) * 4) +
3428 4 /* eir_server_scope.len */ +
3429 (XDR_QUADLEN(server_scope_sz) * 4) +
3430 4 /* eir_server_impl_id.count (0) */);
3431
Andy Adamson0733d212009-04-03 08:28:01 +03003432 /* The server_owner struct */
3433 WRITE64(minor_id); /* Minor id */
3434 /* major id */
3435 WRITE32(major_id_sz);
3436 WRITEMEM(major_id, major_id_sz);
3437
3438 /* Server scope */
3439 WRITE32(server_scope_sz);
3440 WRITEMEM(server_scope, server_scope_sz);
3441
3442 /* Implementation id */
3443 WRITE32(0); /* zero length nfs_impl_id4 array */
3444 ADJUST_ARGS();
3445 return 0;
Andy Adamson2db134e2009-04-03 08:27:55 +03003446}
3447
3448static __be32
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003449nfsd4_encode_create_session(struct nfsd4_compoundres *resp, __be32 nfserr,
Andy Adamson2db134e2009-04-03 08:27:55 +03003450 struct nfsd4_create_session *sess)
3451{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003452 __be32 *p;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003453
3454 if (nfserr)
3455 return nfserr;
3456
3457 RESERVE_SPACE(24);
3458 WRITEMEM(sess->sessionid.data, NFS4_MAX_SESSIONID_LEN);
3459 WRITE32(sess->seqid);
3460 WRITE32(sess->flags);
3461 ADJUST_ARGS();
3462
3463 RESERVE_SPACE(28);
3464 WRITE32(0); /* headerpadsz */
3465 WRITE32(sess->fore_channel.maxreq_sz);
3466 WRITE32(sess->fore_channel.maxresp_sz);
3467 WRITE32(sess->fore_channel.maxresp_cached);
3468 WRITE32(sess->fore_channel.maxops);
3469 WRITE32(sess->fore_channel.maxreqs);
3470 WRITE32(sess->fore_channel.nr_rdma_attrs);
3471 ADJUST_ARGS();
3472
3473 if (sess->fore_channel.nr_rdma_attrs) {
3474 RESERVE_SPACE(4);
3475 WRITE32(sess->fore_channel.rdma_attrs);
3476 ADJUST_ARGS();
3477 }
3478
3479 RESERVE_SPACE(28);
3480 WRITE32(0); /* headerpadsz */
3481 WRITE32(sess->back_channel.maxreq_sz);
3482 WRITE32(sess->back_channel.maxresp_sz);
3483 WRITE32(sess->back_channel.maxresp_cached);
3484 WRITE32(sess->back_channel.maxops);
3485 WRITE32(sess->back_channel.maxreqs);
3486 WRITE32(sess->back_channel.nr_rdma_attrs);
3487 ADJUST_ARGS();
3488
3489 if (sess->back_channel.nr_rdma_attrs) {
3490 RESERVE_SPACE(4);
3491 WRITE32(sess->back_channel.rdma_attrs);
3492 ADJUST_ARGS();
3493 }
3494 return 0;
Andy Adamson2db134e2009-04-03 08:27:55 +03003495}
3496
3497static __be32
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003498nfsd4_encode_sequence(struct nfsd4_compoundres *resp, __be32 nfserr,
Andy Adamson2db134e2009-04-03 08:27:55 +03003499 struct nfsd4_sequence *seq)
3500{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003501 __be32 *p;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003502
3503 if (nfserr)
3504 return nfserr;
3505
3506 RESERVE_SPACE(NFS4_MAX_SESSIONID_LEN + 20);
3507 WRITEMEM(seq->sessionid.data, NFS4_MAX_SESSIONID_LEN);
3508 WRITE32(seq->seqid);
3509 WRITE32(seq->slotid);
J. Bruce Fieldsb7d7ca32011-08-31 15:39:30 -04003510 /* Note slotid's are numbered from zero: */
3511 WRITE32(seq->maxslots - 1); /* sr_highest_slotid */
3512 WRITE32(seq->maxslots - 1); /* sr_target_highest_slotid */
J. Bruce Fields0d7bb712010-11-18 08:30:33 -05003513 WRITE32(seq->status_flags);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003514
3515 ADJUST_ARGS();
Andy Adamson557ce262009-08-28 08:45:04 -04003516 resp->cstate.datap = p; /* DRC cache data pointer */
Benny Halevyb85d4c02009-04-03 08:28:08 +03003517 return 0;
Andy Adamson2db134e2009-04-03 08:27:55 +03003518}
3519
J. Bruce Fields2355c592012-04-25 16:56:22 -04003520static __be32
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003521nfsd4_encode_test_stateid(struct nfsd4_compoundres *resp, __be32 nfserr,
Bryan Schumaker17456802011-07-13 10:50:48 -04003522 struct nfsd4_test_stateid *test_stateid)
3523{
Bryan Schumaker03cfb422012-01-27 10:22:49 -05003524 struct nfsd4_test_stateid_id *stateid, *next;
Bryan Schumaker17456802011-07-13 10:50:48 -04003525 __be32 *p;
Bryan Schumaker17456802011-07-13 10:50:48 -04003526
Bryan Schumaker03cfb422012-01-27 10:22:49 -05003527 RESERVE_SPACE(4 + (4 * test_stateid->ts_num_ids));
Bryan Schumaker17456802011-07-13 10:50:48 -04003528 *p++ = htonl(test_stateid->ts_num_ids);
Bryan Schumaker17456802011-07-13 10:50:48 -04003529
Bryan Schumaker03cfb422012-01-27 10:22:49 -05003530 list_for_each_entry_safe(stateid, next, &test_stateid->ts_stateid_list, ts_id_list) {
Al Viro02f5fde2012-04-13 00:10:34 -04003531 *p++ = stateid->ts_id_status;
Bryan Schumaker17456802011-07-13 10:50:48 -04003532 }
Bryan Schumaker17456802011-07-13 10:50:48 -04003533
Bryan Schumaker03cfb422012-01-27 10:22:49 -05003534 ADJUST_ARGS();
Bryan Schumaker17456802011-07-13 10:50:48 -04003535 return nfserr;
3536}
3537
Andy Adamson2db134e2009-04-03 08:27:55 +03003538static __be32
Benny Halevy695e12f2008-07-04 14:38:33 +03003539nfsd4_encode_noop(struct nfsd4_compoundres *resp, __be32 nfserr, void *p)
3540{
3541 return nfserr;
3542}
3543
3544typedef __be32(* nfsd4_enc)(struct nfsd4_compoundres *, __be32, void *);
3545
Andy Adamson2db134e2009-04-03 08:27:55 +03003546/*
3547 * Note: nfsd4_enc_ops vector is shared for v4.0 and v4.1
3548 * since we don't need to filter out obsolete ops as this is
3549 * done in the decoding phase.
3550 */
Benny Halevy695e12f2008-07-04 14:38:33 +03003551static nfsd4_enc nfsd4_enc_ops[] = {
J. Bruce Fieldsad1060c2008-07-18 15:04:16 -04003552 [OP_ACCESS] = (nfsd4_enc)nfsd4_encode_access,
3553 [OP_CLOSE] = (nfsd4_enc)nfsd4_encode_close,
3554 [OP_COMMIT] = (nfsd4_enc)nfsd4_encode_commit,
3555 [OP_CREATE] = (nfsd4_enc)nfsd4_encode_create,
3556 [OP_DELEGPURGE] = (nfsd4_enc)nfsd4_encode_noop,
3557 [OP_DELEGRETURN] = (nfsd4_enc)nfsd4_encode_noop,
3558 [OP_GETATTR] = (nfsd4_enc)nfsd4_encode_getattr,
3559 [OP_GETFH] = (nfsd4_enc)nfsd4_encode_getfh,
3560 [OP_LINK] = (nfsd4_enc)nfsd4_encode_link,
3561 [OP_LOCK] = (nfsd4_enc)nfsd4_encode_lock,
3562 [OP_LOCKT] = (nfsd4_enc)nfsd4_encode_lockt,
3563 [OP_LOCKU] = (nfsd4_enc)nfsd4_encode_locku,
3564 [OP_LOOKUP] = (nfsd4_enc)nfsd4_encode_noop,
3565 [OP_LOOKUPP] = (nfsd4_enc)nfsd4_encode_noop,
3566 [OP_NVERIFY] = (nfsd4_enc)nfsd4_encode_noop,
3567 [OP_OPEN] = (nfsd4_enc)nfsd4_encode_open,
Benny Halevy84f09f42009-03-04 23:05:35 +02003568 [OP_OPENATTR] = (nfsd4_enc)nfsd4_encode_noop,
J. Bruce Fieldsad1060c2008-07-18 15:04:16 -04003569 [OP_OPEN_CONFIRM] = (nfsd4_enc)nfsd4_encode_open_confirm,
3570 [OP_OPEN_DOWNGRADE] = (nfsd4_enc)nfsd4_encode_open_downgrade,
3571 [OP_PUTFH] = (nfsd4_enc)nfsd4_encode_noop,
3572 [OP_PUTPUBFH] = (nfsd4_enc)nfsd4_encode_noop,
3573 [OP_PUTROOTFH] = (nfsd4_enc)nfsd4_encode_noop,
3574 [OP_READ] = (nfsd4_enc)nfsd4_encode_read,
3575 [OP_READDIR] = (nfsd4_enc)nfsd4_encode_readdir,
3576 [OP_READLINK] = (nfsd4_enc)nfsd4_encode_readlink,
3577 [OP_REMOVE] = (nfsd4_enc)nfsd4_encode_remove,
3578 [OP_RENAME] = (nfsd4_enc)nfsd4_encode_rename,
3579 [OP_RENEW] = (nfsd4_enc)nfsd4_encode_noop,
3580 [OP_RESTOREFH] = (nfsd4_enc)nfsd4_encode_noop,
3581 [OP_SAVEFH] = (nfsd4_enc)nfsd4_encode_noop,
3582 [OP_SECINFO] = (nfsd4_enc)nfsd4_encode_secinfo,
3583 [OP_SETATTR] = (nfsd4_enc)nfsd4_encode_setattr,
3584 [OP_SETCLIENTID] = (nfsd4_enc)nfsd4_encode_setclientid,
3585 [OP_SETCLIENTID_CONFIRM] = (nfsd4_enc)nfsd4_encode_noop,
3586 [OP_VERIFY] = (nfsd4_enc)nfsd4_encode_noop,
3587 [OP_WRITE] = (nfsd4_enc)nfsd4_encode_write,
3588 [OP_RELEASE_LOCKOWNER] = (nfsd4_enc)nfsd4_encode_noop,
Andy Adamson2db134e2009-04-03 08:27:55 +03003589
3590 /* NFSv4.1 operations */
3591 [OP_BACKCHANNEL_CTL] = (nfsd4_enc)nfsd4_encode_noop,
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003592 [OP_BIND_CONN_TO_SESSION] = (nfsd4_enc)nfsd4_encode_bind_conn_to_session,
Andy Adamson2db134e2009-04-03 08:27:55 +03003593 [OP_EXCHANGE_ID] = (nfsd4_enc)nfsd4_encode_exchange_id,
3594 [OP_CREATE_SESSION] = (nfsd4_enc)nfsd4_encode_create_session,
Kinglong Mee43212cc2013-12-09 19:04:23 +08003595 [OP_DESTROY_SESSION] = (nfsd4_enc)nfsd4_encode_noop,
3596 [OP_FREE_STATEID] = (nfsd4_enc)nfsd4_encode_noop,
Andy Adamson2db134e2009-04-03 08:27:55 +03003597 [OP_GET_DIR_DELEGATION] = (nfsd4_enc)nfsd4_encode_noop,
3598 [OP_GETDEVICEINFO] = (nfsd4_enc)nfsd4_encode_noop,
3599 [OP_GETDEVICELIST] = (nfsd4_enc)nfsd4_encode_noop,
3600 [OP_LAYOUTCOMMIT] = (nfsd4_enc)nfsd4_encode_noop,
3601 [OP_LAYOUTGET] = (nfsd4_enc)nfsd4_encode_noop,
3602 [OP_LAYOUTRETURN] = (nfsd4_enc)nfsd4_encode_noop,
Mi Jinlong22b6dee2010-12-27 14:29:57 +08003603 [OP_SECINFO_NO_NAME] = (nfsd4_enc)nfsd4_encode_secinfo_no_name,
Andy Adamson2db134e2009-04-03 08:27:55 +03003604 [OP_SEQUENCE] = (nfsd4_enc)nfsd4_encode_sequence,
3605 [OP_SET_SSV] = (nfsd4_enc)nfsd4_encode_noop,
Bryan Schumaker17456802011-07-13 10:50:48 -04003606 [OP_TEST_STATEID] = (nfsd4_enc)nfsd4_encode_test_stateid,
Andy Adamson2db134e2009-04-03 08:27:55 +03003607 [OP_WANT_DELEGATION] = (nfsd4_enc)nfsd4_encode_noop,
3608 [OP_DESTROY_CLIENTID] = (nfsd4_enc)nfsd4_encode_noop,
3609 [OP_RECLAIM_COMPLETE] = (nfsd4_enc)nfsd4_encode_noop,
Benny Halevy695e12f2008-07-04 14:38:33 +03003610};
3611
Andy Adamson496c2622009-04-03 08:28:48 +03003612/*
3613 * Calculate the total amount of memory that the compound response has taken
Mi Jinlong58e7b332011-08-28 18:18:56 +08003614 * after encoding the current operation with pad.
Andy Adamson496c2622009-04-03 08:28:48 +03003615 *
Mi Jinlong58e7b332011-08-28 18:18:56 +08003616 * pad: if operation is non-idempotent, pad was calculate by op_rsize_bop()
3617 * which was specified at nfsd4_operation, else pad is zero.
Andy Adamson496c2622009-04-03 08:28:48 +03003618 *
Mi Jinlong58e7b332011-08-28 18:18:56 +08003619 * Compare this length to the session se_fmaxresp_sz and se_fmaxresp_cached.
Andy Adamson496c2622009-04-03 08:28:48 +03003620 *
3621 * Our se_fmaxresp_cached will always be a multiple of PAGE_SIZE, and so
3622 * will be at least a page and will therefore hold the xdr_buf head.
3623 */
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003624__be32 nfsd4_check_resp_size(struct nfsd4_compoundres *resp, u32 pad)
Andy Adamson496c2622009-04-03 08:28:48 +03003625{
Andy Adamson496c2622009-04-03 08:28:48 +03003626 struct xdr_buf *xb = &resp->rqstp->rq_res;
Andy Adamson496c2622009-04-03 08:28:48 +03003627 struct nfsd4_session *session = NULL;
3628 struct nfsd4_slot *slot = resp->cstate.slot;
Mi Jinlong58e7b332011-08-28 18:18:56 +08003629 u32 length, tlen = 0;
Andy Adamson496c2622009-04-03 08:28:48 +03003630
3631 if (!nfsd4_has_session(&resp->cstate))
Mi Jinlong58e7b332011-08-28 18:18:56 +08003632 return 0;
Andy Adamson496c2622009-04-03 08:28:48 +03003633
3634 session = resp->cstate.session;
Mi Jinlong58e7b332011-08-28 18:18:56 +08003635 if (session == NULL)
3636 return 0;
Andy Adamson496c2622009-04-03 08:28:48 +03003637
3638 if (xb->page_len == 0) {
3639 length = (char *)resp->p - (char *)xb->head[0].iov_base + pad;
3640 } else {
3641 if (xb->tail[0].iov_base && xb->tail[0].iov_len > 0)
3642 tlen = (char *)resp->p - (char *)xb->tail[0].iov_base;
3643
3644 length = xb->head[0].iov_len + xb->page_len + tlen + pad;
3645 }
3646 dprintk("%s length %u, xb->page_len %u tlen %u pad %u\n", __func__,
3647 length, xb->page_len, tlen, pad);
3648
Mi Jinlong58e7b332011-08-28 18:18:56 +08003649 if (length > session->se_fchannel.maxresp_sz)
3650 return nfserr_rep_too_big;
3651
J. Bruce Fields73e79482012-02-13 16:39:00 -05003652 if ((slot->sl_flags & NFSD4_SLOT_CACHETHIS) &&
Mi Jinlong58e7b332011-08-28 18:18:56 +08003653 length > session->se_fchannel.maxresp_cached)
Andy Adamson496c2622009-04-03 08:28:48 +03003654 return nfserr_rep_too_big_to_cache;
Mi Jinlong58e7b332011-08-28 18:18:56 +08003655
3656 return 0;
Andy Adamson496c2622009-04-03 08:28:48 +03003657}
3658
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659void
3660nfsd4_encode_operation(struct nfsd4_compoundres *resp, struct nfsd4_op *op)
3661{
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04003662 struct nfs4_stateowner *so = resp->cstate.replay_owner;
Al Viro2ebbc012006-10-19 23:28:58 -07003663 __be32 *statp;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003664 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665
3666 RESERVE_SPACE(8);
3667 WRITE32(op->opnum);
3668 statp = p++; /* to be backfilled at the end */
3669 ADJUST_ARGS();
3670
Benny Halevy695e12f2008-07-04 14:38:33 +03003671 if (op->opnum == OP_ILLEGAL)
3672 goto status;
3673 BUG_ON(op->opnum < 0 || op->opnum >= ARRAY_SIZE(nfsd4_enc_ops) ||
3674 !nfsd4_enc_ops[op->opnum]);
3675 op->status = nfsd4_enc_ops[op->opnum](resp, op->status, &op->u);
Andy Adamson496c2622009-04-03 08:28:48 +03003676 /* nfsd4_check_drc_limit guarantees enough room for error status */
Mi Jinlong58e7b332011-08-28 18:18:56 +08003677 if (!op->status)
3678 op->status = nfsd4_check_resp_size(resp, 0);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04003679 if (so) {
3680 so->so_replay.rp_status = op->status;
3681 so->so_replay.rp_buflen = (char *)resp->p - (char *)(statp+1);
3682 memcpy(so->so_replay.rp_buf, statp+1, so->so_replay.rp_buflen);
3683 }
Benny Halevy695e12f2008-07-04 14:38:33 +03003684status:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685 /*
3686 * Note: We write the status directly, instead of using WRITE32(),
3687 * since it is already in network byte order.
3688 */
3689 *statp = op->status;
3690}
3691
3692/*
3693 * Encode the reply stored in the stateowner reply cache
3694 *
3695 * XDR note: do not encode rp->rp_buflen: the buffer contains the
3696 * previously sent already encoded operation.
3697 *
3698 * called with nfs4_lock_state() held
3699 */
3700void
3701nfsd4_encode_replay(struct nfsd4_compoundres *resp, struct nfsd4_op *op)
3702{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003703 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704 struct nfs4_replay *rp = op->replay;
3705
3706 BUG_ON(!rp);
3707
3708 RESERVE_SPACE(8);
3709 WRITE32(op->opnum);
3710 *p++ = rp->rp_status; /* already xdr'ed */
3711 ADJUST_ARGS();
3712
3713 RESERVE_SPACE(rp->rp_buflen);
3714 WRITEMEM(rp->rp_buf, rp->rp_buflen);
3715 ADJUST_ARGS();
3716}
3717
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718int
Al Viro2ebbc012006-10-19 23:28:58 -07003719nfs4svc_encode_voidres(struct svc_rqst *rqstp, __be32 *p, void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720{
3721 return xdr_ressize_check(rqstp, p);
3722}
3723
J. Bruce Fields3e98abf2011-07-16 17:15:10 -04003724int nfsd4_release_compoundargs(void *rq, __be32 *p, void *resp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725{
J. Bruce Fields3e98abf2011-07-16 17:15:10 -04003726 struct svc_rqst *rqstp = rq;
3727 struct nfsd4_compoundargs *args = rqstp->rq_argp;
3728
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729 if (args->ops != args->iops) {
3730 kfree(args->ops);
3731 args->ops = args->iops;
3732 }
Jesper Juhlf99d49a2005-11-07 01:01:34 -08003733 kfree(args->tmpp);
3734 args->tmpp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735 while (args->to_free) {
3736 struct tmpbuf *tb = args->to_free;
3737 args->to_free = tb->next;
3738 tb->release(tb->buf);
3739 kfree(tb);
3740 }
J. Bruce Fields3e98abf2011-07-16 17:15:10 -04003741 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742}
3743
3744int
Al Viro2ebbc012006-10-19 23:28:58 -07003745nfs4svc_decode_compoundargs(struct svc_rqst *rqstp, __be32 *p, struct nfsd4_compoundargs *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003746{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747 args->p = p;
3748 args->end = rqstp->rq_arg.head[0].iov_base + rqstp->rq_arg.head[0].iov_len;
3749 args->pagelist = rqstp->rq_arg.pages;
3750 args->pagelen = rqstp->rq_arg.page_len;
3751 args->tmpp = NULL;
3752 args->to_free = NULL;
3753 args->ops = args->iops;
3754 args->rqstp = rqstp;
3755
J. Bruce Fields3e98abf2011-07-16 17:15:10 -04003756 return !nfsd4_decode_compound(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757}
3758
3759int
Al Viro2ebbc012006-10-19 23:28:58 -07003760nfs4svc_encode_compoundres(struct svc_rqst *rqstp, __be32 *p, struct nfsd4_compoundres *resp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761{
3762 /*
3763 * All that remains is to write the tag and operation count...
3764 */
Andy Adamson557ce262009-08-28 08:45:04 -04003765 struct nfsd4_compound_state *cs = &resp->cstate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766 struct kvec *iov;
3767 p = resp->tagp;
3768 *p++ = htonl(resp->taglen);
3769 memcpy(p, resp->tag, resp->taglen);
3770 p += XDR_QUADLEN(resp->taglen);
3771 *p++ = htonl(resp->opcnt);
3772
3773 if (rqstp->rq_res.page_len)
3774 iov = &rqstp->rq_res.tail[0];
3775 else
3776 iov = &rqstp->rq_res.head[0];
3777 iov->iov_len = ((char*)resp->p) - (char*)iov->iov_base;
3778 BUG_ON(iov->iov_len > PAGE_SIZE);
J. Bruce Fields26c0c752010-04-24 15:35:43 -04003779 if (nfsd4_has_session(cs)) {
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003780 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3781 struct nfs4_client *clp = cs->session->se_client;
J. Bruce Fields26c0c752010-04-24 15:35:43 -04003782 if (cs->status != nfserr_replay_cache) {
3783 nfsd4_store_cache_entry(resp);
J. Bruce Fields73e79482012-02-13 16:39:00 -05003784 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
J. Bruce Fields26c0c752010-04-24 15:35:43 -04003785 }
Benny Halevyd7682982010-05-12 00:13:54 +03003786 /* Renew the clientid on success and on replay */
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003787 spin_lock(&nn->client_lock);
J. Bruce Fields221a6872013-04-01 22:23:49 -04003788 nfsd4_put_session(cs->session);
J. Bruce Fieldsf0f51f52013-06-18 14:26:02 -04003789 spin_unlock(&nn->client_lock);
3790 put_client_renew(clp);
Andy Adamsonda3846a2009-04-03 08:28:22 +03003791 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792 return 1;
3793}
3794
3795/*
3796 * Local variables:
3797 * c-basic-offset: 8
3798 * End:
3799 */