blob: ba7bf4a118551b7e007a821c8f29b707c1279a25 [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"
J. Bruce Fields2ca72e12011-01-04 17:37:15 -050056
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#define NFSDDBG_FACILITY NFSDDBG_XDR
58
J.Bruce Fields42ca0992006-10-04 02:16:20 -070059/*
60 * As per referral draft, the fsid for a referral MUST be different from the fsid of the containing
61 * directory in order to indicate to the client that a filesystem boundary is present
62 * We use a fixed fsid for a referral
63 */
64#define NFS4_REFERRAL_FSID_MAJOR 0x8000000ULL
65#define NFS4_REFERRAL_FSID_MINOR 0x8000000ULL
66
Al Virob37ad282006-10-19 23:28:59 -070067static __be32
68check_filename(char *str, int len, __be32 err)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
70 int i;
71
72 if (len == 0)
73 return nfserr_inval;
74 if (isdotent(str, len))
75 return err;
76 for (i = 0; i < len; i++)
77 if (str[i] == '/')
78 return err;
79 return 0;
80}
81
Linus Torvalds1da177e2005-04-16 15:20:36 -070082#define DECODE_HEAD \
Al Viro2ebbc012006-10-19 23:28:58 -070083 __be32 *p; \
Al Virob37ad282006-10-19 23:28:59 -070084 __be32 status
Linus Torvalds1da177e2005-04-16 15:20:36 -070085#define DECODE_TAIL \
86 status = 0; \
87out: \
88 return status; \
89xdr_error: \
Chuck Lever817cb9d2007-09-11 18:01:20 -040090 dprintk("NFSD: xdr error (%s:%d)\n", \
91 __FILE__, __LINE__); \
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 status = nfserr_bad_xdr; \
93 goto out
94
95#define READ32(x) (x) = ntohl(*p++)
96#define READ64(x) do { \
97 (x) = (u64)ntohl(*p++) << 32; \
98 (x) |= ntohl(*p++); \
99} while (0)
100#define READTIME(x) do { \
101 p++; \
102 (x) = ntohl(*p++); \
103 p++; \
104} while (0)
105#define READMEM(x,nbytes) do { \
106 x = (char *)p; \
107 p += XDR_QUADLEN(nbytes); \
108} while (0)
109#define SAVEMEM(x,nbytes) do { \
110 if (!(x = (p==argp->tmp || p == argp->tmpp) ? \
111 savemem(argp, p, nbytes) : \
112 (char *)p)) { \
Chuck Lever817cb9d2007-09-11 18:01:20 -0400113 dprintk("NFSD: xdr error (%s:%d)\n", \
114 __FILE__, __LINE__); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 goto xdr_error; \
116 } \
117 p += XDR_QUADLEN(nbytes); \
118} while (0)
119#define COPYMEM(x,nbytes) do { \
120 memcpy((x), p, nbytes); \
121 p += XDR_QUADLEN(nbytes); \
122} while (0)
123
124/* READ_BUF, read_buf(): nbytes must be <= PAGE_SIZE */
125#define READ_BUF(nbytes) do { \
126 if (nbytes <= (u32)((char *)argp->end - (char *)argp->p)) { \
127 p = argp->p; \
128 argp->p += XDR_QUADLEN(nbytes); \
129 } else if (!(p = read_buf(argp, nbytes))) { \
Chuck Lever817cb9d2007-09-11 18:01:20 -0400130 dprintk("NFSD: xdr error (%s:%d)\n", \
131 __FILE__, __LINE__); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 goto xdr_error; \
133 } \
134} while (0)
135
J. Bruce Fieldsca2a05a2007-11-11 15:43:12 -0500136static __be32 *read_buf(struct nfsd4_compoundargs *argp, u32 nbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137{
138 /* We want more bytes than seem to be available.
139 * Maybe we need a new page, maybe we have just run out
140 */
J. Bruce Fieldsca2a05a2007-11-11 15:43:12 -0500141 unsigned int avail = (char *)argp->end - (char *)argp->p;
Al Viro2ebbc012006-10-19 23:28:58 -0700142 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 if (avail + argp->pagelen < nbytes)
144 return NULL;
145 if (avail + PAGE_SIZE < nbytes) /* need more than a page !! */
146 return NULL;
147 /* ok, we can do it with the current plus the next page */
148 if (nbytes <= sizeof(argp->tmp))
149 p = argp->tmp;
150 else {
Jesper Juhlf99d49a2005-11-07 01:01:34 -0800151 kfree(argp->tmpp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 p = argp->tmpp = kmalloc(nbytes, GFP_KERNEL);
153 if (!p)
154 return NULL;
155
156 }
J. Bruce Fieldsca2a05a2007-11-11 15:43:12 -0500157 /*
158 * The following memcpy is safe because read_buf is always
159 * called with nbytes > avail, and the two cases above both
160 * guarantee p points to at least nbytes bytes.
161 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 memcpy(p, argp->p, avail);
163 /* step to next page */
J. Bruce Fieldsffdae5d2013-06-21 11:48:11 -0400164 argp->p = page_address(argp->pagelist[0]);
J. Bruce Fieldsca36e742013-11-19 17:32:43 -0500165 argp->pagelist++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 if (argp->pagelen < PAGE_SIZE) {
Neil Brown2bc3c112010-04-20 12:16:52 +1000167 argp->end = argp->p + (argp->pagelen>>2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 argp->pagelen = 0;
169 } else {
Neil Brown2bc3c112010-04-20 12:16:52 +1000170 argp->end = argp->p + (PAGE_SIZE>>2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 argp->pagelen -= PAGE_SIZE;
172 }
173 memcpy(((char*)p)+avail, argp->p, (nbytes - avail));
174 argp->p += XDR_QUADLEN(nbytes - avail);
175 return p;
176}
177
Andy Adamson60adfc52009-04-03 08:28:50 +0300178static int zero_clientid(clientid_t *clid)
179{
180 return (clid->cl_boot == 0) && (clid->cl_id == 0);
181}
182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183static int
184defer_free(struct nfsd4_compoundargs *argp,
185 void (*release)(const void *), void *p)
186{
187 struct tmpbuf *tb;
188
189 tb = kmalloc(sizeof(*tb), GFP_KERNEL);
190 if (!tb)
191 return -ENOMEM;
192 tb->buf = p;
193 tb->release = release;
194 tb->next = argp->to_free;
195 argp->to_free = tb;
196 return 0;
197}
198
Al Viro2ebbc012006-10-19 23:28:58 -0700199static char *savemem(struct nfsd4_compoundargs *argp, __be32 *p, int nbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 if (p == argp->tmp) {
Thomas Meyer67114fe2011-11-17 23:43:40 +0100202 p = kmemdup(argp->tmp, nbytes, GFP_KERNEL);
J. Bruce Fieldsa4db5fe2007-02-16 01:28:30 -0800203 if (!p)
204 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 } else {
Eric Sesterhenn73dff8b2006-10-03 23:37:14 +0200206 BUG_ON(p != argp->tmpp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 argp->tmpp = NULL;
208 }
209 if (defer_free(argp, kfree, p)) {
J. Bruce Fieldsa4db5fe2007-02-16 01:28:30 -0800210 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 return NULL;
212 } else
213 return (char *)p;
214}
215
Al Virob37ad282006-10-19 23:28:59 -0700216static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217nfsd4_decode_bitmap(struct nfsd4_compoundargs *argp, u32 *bmval)
218{
219 u32 bmlen;
220 DECODE_HEAD;
221
222 bmval[0] = 0;
223 bmval[1] = 0;
Andy Adamson7e705702009-04-03 08:29:11 +0300224 bmval[2] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
226 READ_BUF(4);
227 READ32(bmlen);
228 if (bmlen > 1000)
229 goto xdr_error;
230
231 READ_BUF(bmlen << 2);
232 if (bmlen > 0)
233 READ32(bmval[0]);
234 if (bmlen > 1)
235 READ32(bmval[1]);
Andy Adamson7e705702009-04-03 08:29:11 +0300236 if (bmlen > 2)
237 READ32(bmval[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
239 DECODE_TAIL;
240}
241
Al Virob37ad282006-10-19 23:28:59 -0700242static __be32
Yu Zhiguo3c8e0312009-05-16 16:22:31 +0800243nfsd4_decode_fattr(struct nfsd4_compoundargs *argp, u32 *bmval,
Benny Halevyc0d6fc82009-04-03 08:29:05 +0300244 struct iattr *iattr, struct nfs4_acl **acl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245{
246 int expected_len, len = 0;
247 u32 dummy32;
248 char *buf;
Al Virob8dd7b92006-10-19 23:29:01 -0700249 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251 DECODE_HEAD;
252 iattr->ia_valid = 0;
253 if ((status = nfsd4_decode_bitmap(argp, bmval)))
254 return status;
255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 READ_BUF(4);
257 READ32(expected_len);
258
259 if (bmval[0] & FATTR4_WORD0_SIZE) {
260 READ_BUF(8);
261 len += 8;
262 READ64(iattr->ia_size);
263 iattr->ia_valid |= ATTR_SIZE;
264 }
265 if (bmval[0] & FATTR4_WORD0_ACL) {
J. Bruce Fields49e67242013-03-26 14:11:13 -0400266 u32 nace;
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800267 struct nfs4_ace *ace;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
269 READ_BUF(4); len += 4;
270 READ32(nace);
271
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800272 if (nace > NFS4_ACL_MAX)
273 return nfserr_resource;
274
275 *acl = nfs4_acl_new(nace);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 if (*acl == NULL) {
Al Virob8dd7b92006-10-19 23:29:01 -0700277 host_err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 goto out_nfserr;
279 }
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800280 defer_free(argp, kfree, *acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800282 (*acl)->naces = nace;
283 for (ace = (*acl)->aces; ace < (*acl)->aces + nace; ace++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 READ_BUF(16); len += 16;
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800285 READ32(ace->type);
286 READ32(ace->flag);
287 READ32(ace->access_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 READ32(dummy32);
289 READ_BUF(dummy32);
290 len += XDR_QUADLEN(dummy32) << 2;
291 READMEM(buf, dummy32);
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800292 ace->whotype = nfs4_acl_get_whotype(buf, dummy32);
J. Bruce Fields3c726022011-01-04 17:53:52 -0500293 status = nfs_ok;
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800294 if (ace->whotype != NFS4_ACL_WHO_NAMED)
295 ace->who = 0;
296 else if (ace->flag & NFS4_ACE_IDENTIFIER_GROUP)
J. Bruce Fields3c726022011-01-04 17:53:52 -0500297 status = nfsd_map_name_to_gid(argp->rqstp,
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800298 buf, dummy32, &ace->who);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 else
J. Bruce Fields3c726022011-01-04 17:53:52 -0500300 status = nfsd_map_name_to_uid(argp->rqstp,
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800301 buf, dummy32, &ace->who);
J. Bruce Fields3c726022011-01-04 17:53:52 -0500302 if (status)
303 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 }
305 } else
306 *acl = NULL;
307 if (bmval[1] & FATTR4_WORD1_MODE) {
308 READ_BUF(4);
309 len += 4;
310 READ32(iattr->ia_mode);
311 iattr->ia_mode &= (S_IFMT | S_IALLUGO);
312 iattr->ia_valid |= ATTR_MODE;
313 }
314 if (bmval[1] & FATTR4_WORD1_OWNER) {
315 READ_BUF(4);
316 len += 4;
317 READ32(dummy32);
318 READ_BUF(dummy32);
319 len += (XDR_QUADLEN(dummy32) << 2);
320 READMEM(buf, dummy32);
NeilBrown47c85292011-02-16 13:08:35 +1100321 if ((status = nfsd_map_name_to_uid(argp->rqstp, buf, dummy32, &iattr->ia_uid)))
322 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 iattr->ia_valid |= ATTR_UID;
324 }
325 if (bmval[1] & FATTR4_WORD1_OWNER_GROUP) {
326 READ_BUF(4);
327 len += 4;
328 READ32(dummy32);
329 READ_BUF(dummy32);
330 len += (XDR_QUADLEN(dummy32) << 2);
331 READMEM(buf, dummy32);
NeilBrown47c85292011-02-16 13:08:35 +1100332 if ((status = nfsd_map_name_to_gid(argp->rqstp, buf, dummy32, &iattr->ia_gid)))
333 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 iattr->ia_valid |= ATTR_GID;
335 }
336 if (bmval[1] & FATTR4_WORD1_TIME_ACCESS_SET) {
337 READ_BUF(4);
338 len += 4;
339 READ32(dummy32);
340 switch (dummy32) {
341 case NFS4_SET_TO_CLIENT_TIME:
342 /* We require the high 32 bits of 'seconds' to be 0, and we ignore
343 all 32 bits of 'nseconds'. */
344 READ_BUF(12);
345 len += 12;
Bryan Schumaker2111a772013-04-19 16:09:38 -0400346 READ64(iattr->ia_atime.tv_sec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 READ32(iattr->ia_atime.tv_nsec);
348 if (iattr->ia_atime.tv_nsec >= (u32)1000000000)
349 return nfserr_inval;
350 iattr->ia_valid |= (ATTR_ATIME | ATTR_ATIME_SET);
351 break;
352 case NFS4_SET_TO_SERVER_TIME:
353 iattr->ia_valid |= ATTR_ATIME;
354 break;
355 default:
356 goto xdr_error;
357 }
358 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 if (bmval[1] & FATTR4_WORD1_TIME_MODIFY_SET) {
360 READ_BUF(4);
361 len += 4;
362 READ32(dummy32);
363 switch (dummy32) {
364 case NFS4_SET_TO_CLIENT_TIME:
365 /* We require the high 32 bits of 'seconds' to be 0, and we ignore
366 all 32 bits of 'nseconds'. */
367 READ_BUF(12);
368 len += 12;
Bryan Schumaker2111a772013-04-19 16:09:38 -0400369 READ64(iattr->ia_mtime.tv_sec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 READ32(iattr->ia_mtime.tv_nsec);
371 if (iattr->ia_mtime.tv_nsec >= (u32)1000000000)
372 return nfserr_inval;
373 iattr->ia_valid |= (ATTR_MTIME | ATTR_MTIME_SET);
374 break;
375 case NFS4_SET_TO_SERVER_TIME:
376 iattr->ia_valid |= ATTR_MTIME;
377 break;
378 default:
379 goto xdr_error;
380 }
381 }
Yu Zhiguo3c8e0312009-05-16 16:22:31 +0800382 if (bmval[0] & ~NFSD_WRITEABLE_ATTRS_WORD0
383 || bmval[1] & ~NFSD_WRITEABLE_ATTRS_WORD1
384 || bmval[2] & ~NFSD_WRITEABLE_ATTRS_WORD2)
385 READ_BUF(expected_len - len);
386 else if (len != expected_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 goto xdr_error;
388
389 DECODE_TAIL;
390
391out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -0700392 status = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 goto out;
394}
395
Al Virob37ad282006-10-19 23:28:59 -0700396static __be32
Benny Halevye31a1b62008-08-12 20:46:18 +0300397nfsd4_decode_stateid(struct nfsd4_compoundargs *argp, stateid_t *sid)
398{
399 DECODE_HEAD;
400
401 READ_BUF(sizeof(stateid_t));
402 READ32(sid->si_generation);
403 COPYMEM(&sid->si_opaque, sizeof(stateid_opaque_t));
404
405 DECODE_TAIL;
406}
407
408static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409nfsd4_decode_access(struct nfsd4_compoundargs *argp, struct nfsd4_access *access)
410{
411 DECODE_HEAD;
412
413 READ_BUF(4);
414 READ32(access->ac_req_access);
415
416 DECODE_TAIL;
417}
418
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -0400419static __be32 nfsd4_decode_bind_conn_to_session(struct nfsd4_compoundargs *argp, struct nfsd4_bind_conn_to_session *bcts)
420{
421 DECODE_HEAD;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -0400422
423 READ_BUF(NFS4_MAX_SESSIONID_LEN + 8);
424 COPYMEM(bcts->sessionid.data, NFS4_MAX_SESSIONID_LEN);
425 READ32(bcts->dir);
Bryan Schumaker6ce23572011-04-27 15:47:16 -0400426 /* XXX: skipping ctsa_use_conn_in_rdma_mode. Perhaps Tom Tucker
427 * could help us figure out we should be using it. */
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -0400428 DECODE_TAIL;
429}
430
Al Virob37ad282006-10-19 23:28:59 -0700431static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432nfsd4_decode_close(struct nfsd4_compoundargs *argp, struct nfsd4_close *close)
433{
434 DECODE_HEAD;
435
Benny Halevye31a1b62008-08-12 20:46:18 +0300436 READ_BUF(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 READ32(close->cl_seqid);
Benny Halevye31a1b62008-08-12 20:46:18 +0300438 return nfsd4_decode_stateid(argp, &close->cl_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
440 DECODE_TAIL;
441}
442
443
Al Virob37ad282006-10-19 23:28:59 -0700444static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445nfsd4_decode_commit(struct nfsd4_compoundargs *argp, struct nfsd4_commit *commit)
446{
447 DECODE_HEAD;
448
449 READ_BUF(12);
450 READ64(commit->co_offset);
451 READ32(commit->co_count);
452
453 DECODE_TAIL;
454}
455
Al Virob37ad282006-10-19 23:28:59 -0700456static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457nfsd4_decode_create(struct nfsd4_compoundargs *argp, struct nfsd4_create *create)
458{
459 DECODE_HEAD;
460
461 READ_BUF(4);
462 READ32(create->cr_type);
463 switch (create->cr_type) {
464 case NF4LNK:
465 READ_BUF(4);
466 READ32(create->cr_linklen);
467 READ_BUF(create->cr_linklen);
468 SAVEMEM(create->cr_linkname, create->cr_linklen);
469 break;
470 case NF4BLK:
471 case NF4CHR:
472 READ_BUF(8);
473 READ32(create->cr_specdata1);
474 READ32(create->cr_specdata2);
475 break;
476 case NF4SOCK:
477 case NF4FIFO:
478 case NF4DIR:
479 default:
480 break;
481 }
482
483 READ_BUF(4);
484 READ32(create->cr_namelen);
485 READ_BUF(create->cr_namelen);
486 SAVEMEM(create->cr_name, create->cr_namelen);
487 if ((status = check_filename(create->cr_name, create->cr_namelen, nfserr_inval)))
488 return status;
489
Yu Zhiguo3c8e0312009-05-16 16:22:31 +0800490 status = nfsd4_decode_fattr(argp, create->cr_bmval, &create->cr_iattr,
491 &create->cr_acl);
Benny Halevyc0d6fc82009-04-03 08:29:05 +0300492 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 goto out;
494
495 DECODE_TAIL;
496}
497
Al Virob37ad282006-10-19 23:28:59 -0700498static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499nfsd4_decode_delegreturn(struct nfsd4_compoundargs *argp, struct nfsd4_delegreturn *dr)
500{
Benny Halevye31a1b62008-08-12 20:46:18 +0300501 return nfsd4_decode_stateid(argp, &dr->dr_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502}
503
Al Virob37ad282006-10-19 23:28:59 -0700504static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505nfsd4_decode_getattr(struct nfsd4_compoundargs *argp, struct nfsd4_getattr *getattr)
506{
507 return nfsd4_decode_bitmap(argp, getattr->ga_bmval);
508}
509
Al Virob37ad282006-10-19 23:28:59 -0700510static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511nfsd4_decode_link(struct nfsd4_compoundargs *argp, struct nfsd4_link *link)
512{
513 DECODE_HEAD;
514
515 READ_BUF(4);
516 READ32(link->li_namelen);
517 READ_BUF(link->li_namelen);
518 SAVEMEM(link->li_name, link->li_namelen);
519 if ((status = check_filename(link->li_name, link->li_namelen, nfserr_inval)))
520 return status;
521
522 DECODE_TAIL;
523}
524
Al Virob37ad282006-10-19 23:28:59 -0700525static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526nfsd4_decode_lock(struct nfsd4_compoundargs *argp, struct nfsd4_lock *lock)
527{
528 DECODE_HEAD;
529
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 /*
531 * type, reclaim(boolean), offset, length, new_lock_owner(boolean)
532 */
533 READ_BUF(28);
534 READ32(lock->lk_type);
535 if ((lock->lk_type < NFS4_READ_LT) || (lock->lk_type > NFS4_WRITEW_LT))
536 goto xdr_error;
537 READ32(lock->lk_reclaim);
538 READ64(lock->lk_offset);
539 READ64(lock->lk_length);
540 READ32(lock->lk_is_new);
541
542 if (lock->lk_is_new) {
Benny Halevye31a1b62008-08-12 20:46:18 +0300543 READ_BUF(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 READ32(lock->lk_new_open_seqid);
Benny Halevye31a1b62008-08-12 20:46:18 +0300545 status = nfsd4_decode_stateid(argp, &lock->lk_new_open_stateid);
546 if (status)
547 return status;
548 READ_BUF(8 + sizeof(clientid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 READ32(lock->lk_new_lock_seqid);
550 COPYMEM(&lock->lk_new_clientid, sizeof(clientid_t));
551 READ32(lock->lk_new_owner.len);
552 READ_BUF(lock->lk_new_owner.len);
553 READMEM(lock->lk_new_owner.data, lock->lk_new_owner.len);
554 } else {
Benny Halevye31a1b62008-08-12 20:46:18 +0300555 status = nfsd4_decode_stateid(argp, &lock->lk_old_lock_stateid);
556 if (status)
557 return status;
558 READ_BUF(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 READ32(lock->lk_old_lock_seqid);
560 }
561
562 DECODE_TAIL;
563}
564
Al Virob37ad282006-10-19 23:28:59 -0700565static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566nfsd4_decode_lockt(struct nfsd4_compoundargs *argp, struct nfsd4_lockt *lockt)
567{
568 DECODE_HEAD;
569
570 READ_BUF(32);
571 READ32(lockt->lt_type);
572 if((lockt->lt_type < NFS4_READ_LT) || (lockt->lt_type > NFS4_WRITEW_LT))
573 goto xdr_error;
574 READ64(lockt->lt_offset);
575 READ64(lockt->lt_length);
576 COPYMEM(&lockt->lt_clientid, 8);
577 READ32(lockt->lt_owner.len);
578 READ_BUF(lockt->lt_owner.len);
579 READMEM(lockt->lt_owner.data, lockt->lt_owner.len);
580
581 DECODE_TAIL;
582}
583
Al Virob37ad282006-10-19 23:28:59 -0700584static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585nfsd4_decode_locku(struct nfsd4_compoundargs *argp, struct nfsd4_locku *locku)
586{
587 DECODE_HEAD;
588
Benny Halevye31a1b62008-08-12 20:46:18 +0300589 READ_BUF(8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 READ32(locku->lu_type);
591 if ((locku->lu_type < NFS4_READ_LT) || (locku->lu_type > NFS4_WRITEW_LT))
592 goto xdr_error;
593 READ32(locku->lu_seqid);
Benny Halevye31a1b62008-08-12 20:46:18 +0300594 status = nfsd4_decode_stateid(argp, &locku->lu_stateid);
595 if (status)
596 return status;
597 READ_BUF(16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 READ64(locku->lu_offset);
599 READ64(locku->lu_length);
600
601 DECODE_TAIL;
602}
603
Al Virob37ad282006-10-19 23:28:59 -0700604static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605nfsd4_decode_lookup(struct nfsd4_compoundargs *argp, struct nfsd4_lookup *lookup)
606{
607 DECODE_HEAD;
608
609 READ_BUF(4);
610 READ32(lookup->lo_len);
611 READ_BUF(lookup->lo_len);
612 SAVEMEM(lookup->lo_name, lookup->lo_len);
613 if ((status = check_filename(lookup->lo_name, lookup->lo_len, nfserr_noent)))
614 return status;
615
616 DECODE_TAIL;
617}
618
Benny Halevy2c8bd7e2012-02-16 20:57:09 +0200619static __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 -0400620{
621 __be32 *p;
622 u32 w;
623
624 READ_BUF(4);
625 READ32(w);
Benny Halevy2c8bd7e2012-02-16 20:57:09 +0200626 *share_access = w & NFS4_SHARE_ACCESS_MASK;
627 *deleg_want = w & NFS4_SHARE_WANT_MASK;
628 if (deleg_when)
629 *deleg_when = w & NFS4_SHARE_WHEN_MASK;
630
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400631 switch (w & NFS4_SHARE_ACCESS_MASK) {
632 case NFS4_SHARE_ACCESS_READ:
633 case NFS4_SHARE_ACCESS_WRITE:
634 case NFS4_SHARE_ACCESS_BOTH:
635 break;
636 default:
637 return nfserr_bad_xdr;
638 }
Benny Halevyfc0d14f2011-10-27 20:43:01 +0200639 w &= ~NFS4_SHARE_ACCESS_MASK;
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400640 if (!w)
641 return nfs_ok;
642 if (!argp->minorversion)
643 return nfserr_bad_xdr;
644 switch (w & NFS4_SHARE_WANT_MASK) {
645 case NFS4_SHARE_WANT_NO_PREFERENCE:
646 case NFS4_SHARE_WANT_READ_DELEG:
647 case NFS4_SHARE_WANT_WRITE_DELEG:
648 case NFS4_SHARE_WANT_ANY_DELEG:
649 case NFS4_SHARE_WANT_NO_DELEG:
650 case NFS4_SHARE_WANT_CANCEL:
651 break;
652 default:
653 return nfserr_bad_xdr;
654 }
Benny Halevy92bac8c2011-10-19 19:13:29 -0700655 w &= ~NFS4_SHARE_WANT_MASK;
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400656 if (!w)
657 return nfs_ok;
Benny Halevy2c8bd7e2012-02-16 20:57:09 +0200658
659 if (!deleg_when) /* open_downgrade */
660 return nfserr_inval;
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400661 switch (w) {
662 case NFS4_SHARE_SIGNAL_DELEG_WHEN_RESRC_AVAIL:
663 case NFS4_SHARE_PUSH_DELEG_WHEN_UNCONTENDED:
Benny Halevyc668fc62011-10-19 19:13:13 -0700664 case (NFS4_SHARE_SIGNAL_DELEG_WHEN_RESRC_AVAIL |
665 NFS4_SHARE_PUSH_DELEG_WHEN_UNCONTENDED):
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400666 return nfs_ok;
667 }
668xdr_error:
669 return nfserr_bad_xdr;
670}
671
672static __be32 nfsd4_decode_share_deny(struct nfsd4_compoundargs *argp, u32 *x)
673{
674 __be32 *p;
675
676 READ_BUF(4);
677 READ32(*x);
678 /* Note: unlinke access bits, deny bits may be zero. */
Dan Carpenter01cd4af2011-10-17 10:41:17 +0300679 if (*x & ~NFS4_SHARE_DENY_BOTH)
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400680 return nfserr_bad_xdr;
681 return nfs_ok;
682xdr_error:
683 return nfserr_bad_xdr;
684}
685
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -0400686static __be32 nfsd4_decode_opaque(struct nfsd4_compoundargs *argp, struct xdr_netobj *o)
687{
688 __be32 *p;
689
690 READ_BUF(4);
691 READ32(o->len);
692
693 if (o->len == 0 || o->len > NFS4_OPAQUE_LIMIT)
694 return nfserr_bad_xdr;
695
696 READ_BUF(o->len);
697 SAVEMEM(o->data, o->len);
698 return nfs_ok;
699xdr_error:
700 return nfserr_bad_xdr;
701}
702
Al Virob37ad282006-10-19 23:28:59 -0700703static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704nfsd4_decode_open(struct nfsd4_compoundargs *argp, struct nfsd4_open *open)
705{
706 DECODE_HEAD;
Benny Halevy2c8bd7e2012-02-16 20:57:09 +0200707 u32 dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
709 memset(open->op_bmval, 0, sizeof(open->op_bmval));
710 open->op_iattr.ia_valid = 0;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400711 open->op_openowner = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
713 /* seqid, share_access, share_deny, clientid, ownerlen */
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400714 READ_BUF(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 READ32(open->op_seqid);
Benny Halevy2c8bd7e2012-02-16 20:57:09 +0200716 /* decode, yet ignore deleg_when until supported */
717 status = nfsd4_decode_share_access(argp, &open->op_share_access,
718 &open->op_deleg_want, &dummy);
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400719 if (status)
720 goto xdr_error;
721 status = nfsd4_decode_share_deny(argp, &open->op_share_deny);
722 if (status)
723 goto xdr_error;
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -0400724 READ_BUF(sizeof(clientid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 COPYMEM(&open->op_clientid, sizeof(clientid_t));
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -0400726 status = nfsd4_decode_opaque(argp, &open->op_owner);
727 if (status)
728 goto xdr_error;
729 READ_BUF(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 READ32(open->op_create);
731 switch (open->op_create) {
732 case NFS4_OPEN_NOCREATE:
733 break;
734 case NFS4_OPEN_CREATE:
735 READ_BUF(4);
736 READ32(open->op_createmode);
737 switch (open->op_createmode) {
738 case NFS4_CREATE_UNCHECKED:
739 case NFS4_CREATE_GUARDED:
Benny Halevyc0d6fc82009-04-03 08:29:05 +0300740 status = nfsd4_decode_fattr(argp, open->op_bmval,
Yu Zhiguo3c8e0312009-05-16 16:22:31 +0800741 &open->op_iattr, &open->op_acl);
Benny Halevyc0d6fc82009-04-03 08:29:05 +0300742 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 goto out;
744 break;
745 case NFS4_CREATE_EXCLUSIVE:
Chuck Leverab4684d2012-03-02 17:13:50 -0500746 READ_BUF(NFS4_VERIFIER_SIZE);
747 COPYMEM(open->op_verf.data, NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 break;
Benny Halevy79fb54a2009-04-03 08:29:17 +0300749 case NFS4_CREATE_EXCLUSIVE4_1:
750 if (argp->minorversion < 1)
751 goto xdr_error;
Chuck Leverab4684d2012-03-02 17:13:50 -0500752 READ_BUF(NFS4_VERIFIER_SIZE);
753 COPYMEM(open->op_verf.data, NFS4_VERIFIER_SIZE);
Benny Halevy79fb54a2009-04-03 08:29:17 +0300754 status = nfsd4_decode_fattr(argp, open->op_bmval,
Yu Zhiguo3c8e0312009-05-16 16:22:31 +0800755 &open->op_iattr, &open->op_acl);
Benny Halevy79fb54a2009-04-03 08:29:17 +0300756 if (status)
757 goto out;
758 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 default:
760 goto xdr_error;
761 }
762 break;
763 default:
764 goto xdr_error;
765 }
766
767 /* open_claim */
768 READ_BUF(4);
769 READ32(open->op_claim_type);
770 switch (open->op_claim_type) {
771 case NFS4_OPEN_CLAIM_NULL:
772 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
773 READ_BUF(4);
774 READ32(open->op_fname.len);
775 READ_BUF(open->op_fname.len);
776 SAVEMEM(open->op_fname.data, open->op_fname.len);
777 if ((status = check_filename(open->op_fname.data, open->op_fname.len, nfserr_inval)))
778 return status;
779 break;
780 case NFS4_OPEN_CLAIM_PREVIOUS:
781 READ_BUF(4);
782 READ32(open->op_delegate_type);
783 break;
784 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
Benny Halevye31a1b62008-08-12 20:46:18 +0300785 status = nfsd4_decode_stateid(argp, &open->op_delegate_stateid);
786 if (status)
787 return status;
788 READ_BUF(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 READ32(open->op_fname.len);
790 READ_BUF(open->op_fname.len);
791 SAVEMEM(open->op_fname.data, open->op_fname.len);
792 if ((status = check_filename(open->op_fname.data, open->op_fname.len, nfserr_inval)))
793 return status;
794 break;
J. Bruce Fields8b289b22011-10-19 11:52:12 -0400795 case NFS4_OPEN_CLAIM_FH:
796 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
797 if (argp->minorversion < 1)
798 goto xdr_error;
799 /* void */
800 break;
801 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
802 if (argp->minorversion < 1)
803 goto xdr_error;
804 status = nfsd4_decode_stateid(argp, &open->op_delegate_stateid);
805 if (status)
806 return status;
807 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 default:
809 goto xdr_error;
810 }
811
812 DECODE_TAIL;
813}
814
Al Virob37ad282006-10-19 23:28:59 -0700815static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816nfsd4_decode_open_confirm(struct nfsd4_compoundargs *argp, struct nfsd4_open_confirm *open_conf)
817{
818 DECODE_HEAD;
819
Benny Halevye31a1b62008-08-12 20:46:18 +0300820 status = nfsd4_decode_stateid(argp, &open_conf->oc_req_stateid);
821 if (status)
822 return status;
823 READ_BUF(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 READ32(open_conf->oc_seqid);
825
826 DECODE_TAIL;
827}
828
Al Virob37ad282006-10-19 23:28:59 -0700829static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830nfsd4_decode_open_downgrade(struct nfsd4_compoundargs *argp, struct nfsd4_open_downgrade *open_down)
831{
832 DECODE_HEAD;
833
Benny Halevye31a1b62008-08-12 20:46:18 +0300834 status = nfsd4_decode_stateid(argp, &open_down->od_stateid);
835 if (status)
836 return status;
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400837 READ_BUF(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 READ32(open_down->od_seqid);
Benny Halevy2c8bd7e2012-02-16 20:57:09 +0200839 status = nfsd4_decode_share_access(argp, &open_down->od_share_access,
840 &open_down->od_deleg_want, NULL);
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400841 if (status)
842 return status;
843 status = nfsd4_decode_share_deny(argp, &open_down->od_share_deny);
844 if (status)
845 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 DECODE_TAIL;
847}
848
Al Virob37ad282006-10-19 23:28:59 -0700849static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850nfsd4_decode_putfh(struct nfsd4_compoundargs *argp, struct nfsd4_putfh *putfh)
851{
852 DECODE_HEAD;
853
854 READ_BUF(4);
855 READ32(putfh->pf_fhlen);
856 if (putfh->pf_fhlen > NFS4_FHSIZE)
857 goto xdr_error;
858 READ_BUF(putfh->pf_fhlen);
859 SAVEMEM(putfh->pf_fhval, putfh->pf_fhlen);
860
861 DECODE_TAIL;
862}
863
Al Virob37ad282006-10-19 23:28:59 -0700864static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865nfsd4_decode_read(struct nfsd4_compoundargs *argp, struct nfsd4_read *read)
866{
867 DECODE_HEAD;
868
Benny Halevye31a1b62008-08-12 20:46:18 +0300869 status = nfsd4_decode_stateid(argp, &read->rd_stateid);
870 if (status)
871 return status;
872 READ_BUF(12);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 READ64(read->rd_offset);
874 READ32(read->rd_length);
875
876 DECODE_TAIL;
877}
878
Al Virob37ad282006-10-19 23:28:59 -0700879static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880nfsd4_decode_readdir(struct nfsd4_compoundargs *argp, struct nfsd4_readdir *readdir)
881{
882 DECODE_HEAD;
883
884 READ_BUF(24);
885 READ64(readdir->rd_cookie);
886 COPYMEM(readdir->rd_verf.data, sizeof(readdir->rd_verf.data));
887 READ32(readdir->rd_dircount); /* just in case you needed a useless field... */
888 READ32(readdir->rd_maxcount);
889 if ((status = nfsd4_decode_bitmap(argp, readdir->rd_bmval)))
890 goto out;
891
892 DECODE_TAIL;
893}
894
Al Virob37ad282006-10-19 23:28:59 -0700895static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896nfsd4_decode_remove(struct nfsd4_compoundargs *argp, struct nfsd4_remove *remove)
897{
898 DECODE_HEAD;
899
900 READ_BUF(4);
901 READ32(remove->rm_namelen);
902 READ_BUF(remove->rm_namelen);
903 SAVEMEM(remove->rm_name, remove->rm_namelen);
904 if ((status = check_filename(remove->rm_name, remove->rm_namelen, nfserr_noent)))
905 return status;
906
907 DECODE_TAIL;
908}
909
Al Virob37ad282006-10-19 23:28:59 -0700910static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911nfsd4_decode_rename(struct nfsd4_compoundargs *argp, struct nfsd4_rename *rename)
912{
913 DECODE_HEAD;
914
915 READ_BUF(4);
916 READ32(rename->rn_snamelen);
917 READ_BUF(rename->rn_snamelen + 4);
918 SAVEMEM(rename->rn_sname, rename->rn_snamelen);
919 READ32(rename->rn_tnamelen);
920 READ_BUF(rename->rn_tnamelen);
921 SAVEMEM(rename->rn_tname, rename->rn_tnamelen);
922 if ((status = check_filename(rename->rn_sname, rename->rn_snamelen, nfserr_noent)))
923 return status;
924 if ((status = check_filename(rename->rn_tname, rename->rn_tnamelen, nfserr_inval)))
925 return status;
926
927 DECODE_TAIL;
928}
929
Al Virob37ad282006-10-19 23:28:59 -0700930static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931nfsd4_decode_renew(struct nfsd4_compoundargs *argp, clientid_t *clientid)
932{
933 DECODE_HEAD;
934
935 READ_BUF(sizeof(clientid_t));
936 COPYMEM(clientid, sizeof(clientid_t));
937
938 DECODE_TAIL;
939}
940
Al Virob37ad282006-10-19 23:28:59 -0700941static __be32
Andy Adamsondcb488a2007-07-17 04:04:51 -0700942nfsd4_decode_secinfo(struct nfsd4_compoundargs *argp,
943 struct nfsd4_secinfo *secinfo)
944{
945 DECODE_HEAD;
946
947 READ_BUF(4);
948 READ32(secinfo->si_namelen);
949 READ_BUF(secinfo->si_namelen);
950 SAVEMEM(secinfo->si_name, secinfo->si_namelen);
951 status = check_filename(secinfo->si_name, secinfo->si_namelen,
952 nfserr_noent);
953 if (status)
954 return status;
955 DECODE_TAIL;
956}
957
958static __be32
J. Bruce Fields04f4ad12010-12-16 09:51:13 -0500959nfsd4_decode_secinfo_no_name(struct nfsd4_compoundargs *argp,
960 struct nfsd4_secinfo_no_name *sin)
961{
962 DECODE_HEAD;
963
964 READ_BUF(4);
965 READ32(sin->sin_style);
966 DECODE_TAIL;
967}
968
969static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970nfsd4_decode_setattr(struct nfsd4_compoundargs *argp, struct nfsd4_setattr *setattr)
971{
Benny Halevye31a1b62008-08-12 20:46:18 +0300972 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
Benny Halevye31a1b62008-08-12 20:46:18 +0300974 status = nfsd4_decode_stateid(argp, &setattr->sa_stateid);
975 if (status)
976 return status;
Yu Zhiguo3c8e0312009-05-16 16:22:31 +0800977 return nfsd4_decode_fattr(argp, setattr->sa_bmval, &setattr->sa_iattr,
978 &setattr->sa_acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979}
980
Al Virob37ad282006-10-19 23:28:59 -0700981static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982nfsd4_decode_setclientid(struct nfsd4_compoundargs *argp, struct nfsd4_setclientid *setclientid)
983{
984 DECODE_HEAD;
985
Chuck Leverab4684d2012-03-02 17:13:50 -0500986 READ_BUF(NFS4_VERIFIER_SIZE);
987 COPYMEM(setclientid->se_verf.data, NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -0400989 status = nfsd4_decode_opaque(argp, &setclientid->se_name);
990 if (status)
991 return nfserr_bad_xdr;
992 READ_BUF(8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 READ32(setclientid->se_callback_prog);
994 READ32(setclientid->se_callback_netid_len);
995
996 READ_BUF(setclientid->se_callback_netid_len + 4);
997 SAVEMEM(setclientid->se_callback_netid_val, setclientid->se_callback_netid_len);
998 READ32(setclientid->se_callback_addr_len);
999
1000 READ_BUF(setclientid->se_callback_addr_len + 4);
1001 SAVEMEM(setclientid->se_callback_addr_val, setclientid->se_callback_addr_len);
1002 READ32(setclientid->se_callback_ident);
1003
1004 DECODE_TAIL;
1005}
1006
Al Virob37ad282006-10-19 23:28:59 -07001007static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008nfsd4_decode_setclientid_confirm(struct nfsd4_compoundargs *argp, struct nfsd4_setclientid_confirm *scd_c)
1009{
1010 DECODE_HEAD;
1011
Chuck Leverab4684d2012-03-02 17:13:50 -05001012 READ_BUF(8 + NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 COPYMEM(&scd_c->sc_clientid, 8);
Chuck Leverab4684d2012-03-02 17:13:50 -05001014 COPYMEM(&scd_c->sc_confirm, NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
1016 DECODE_TAIL;
1017}
1018
1019/* Also used for NVERIFY */
Al Virob37ad282006-10-19 23:28:59 -07001020static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021nfsd4_decode_verify(struct nfsd4_compoundargs *argp, struct nfsd4_verify *verify)
1022{
1023#if 0
1024 struct nfsd4_compoundargs save = {
1025 .p = argp->p,
1026 .end = argp->end,
1027 .rqstp = argp->rqstp,
1028 };
1029 u32 ve_bmval[2];
1030 struct iattr ve_iattr; /* request */
1031 struct nfs4_acl *ve_acl; /* request */
1032#endif
1033 DECODE_HEAD;
1034
1035 if ((status = nfsd4_decode_bitmap(argp, verify->ve_bmval)))
1036 goto out;
1037
1038 /* For convenience's sake, we compare raw xdr'd attributes in
1039 * nfsd4_proc_verify; however we still decode here just to return
1040 * correct error in case of bad xdr. */
1041#if 0
1042 status = nfsd4_decode_fattr(ve_bmval, &ve_iattr, &ve_acl);
1043 if (status == nfserr_inval) {
1044 status = nfserrno(status);
1045 goto out;
1046 }
1047#endif
1048 READ_BUF(4);
1049 READ32(verify->ve_attrlen);
1050 READ_BUF(verify->ve_attrlen);
1051 SAVEMEM(verify->ve_attrval, verify->ve_attrlen);
1052
1053 DECODE_TAIL;
1054}
1055
Al Virob37ad282006-10-19 23:28:59 -07001056static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057nfsd4_decode_write(struct nfsd4_compoundargs *argp, struct nfsd4_write *write)
1058{
1059 int avail;
1060 int v;
1061 int len;
1062 DECODE_HEAD;
1063
Benny Halevye31a1b62008-08-12 20:46:18 +03001064 status = nfsd4_decode_stateid(argp, &write->wr_stateid);
1065 if (status)
1066 return status;
1067 READ_BUF(16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 READ64(write->wr_offset);
1069 READ32(write->wr_stable_how);
1070 if (write->wr_stable_how > 2)
1071 goto xdr_error;
1072 READ32(write->wr_buflen);
1073
1074 /* Sorry .. no magic macros for this.. *
1075 * READ_BUF(write->wr_buflen);
1076 * SAVEMEM(write->wr_buf, write->wr_buflen);
1077 */
1078 avail = (char*)argp->end - (char*)argp->p;
1079 if (avail + argp->pagelen < write->wr_buflen) {
Chuck Lever817cb9d2007-09-11 18:01:20 -04001080 dprintk("NFSD: xdr error (%s:%d)\n",
1081 __FILE__, __LINE__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 goto xdr_error;
1083 }
NeilBrown3cc03b12006-10-04 02:15:47 -07001084 argp->rqstp->rq_vec[0].iov_base = p;
1085 argp->rqstp->rq_vec[0].iov_len = avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 v = 0;
1087 len = write->wr_buflen;
NeilBrown3cc03b12006-10-04 02:15:47 -07001088 while (len > argp->rqstp->rq_vec[v].iov_len) {
1089 len -= argp->rqstp->rq_vec[v].iov_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 v++;
NeilBrown3cc03b12006-10-04 02:15:47 -07001091 argp->rqstp->rq_vec[v].iov_base = page_address(argp->pagelist[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 argp->pagelist++;
1093 if (argp->pagelen >= PAGE_SIZE) {
NeilBrown3cc03b12006-10-04 02:15:47 -07001094 argp->rqstp->rq_vec[v].iov_len = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 argp->pagelen -= PAGE_SIZE;
1096 } else {
NeilBrown3cc03b12006-10-04 02:15:47 -07001097 argp->rqstp->rq_vec[v].iov_len = argp->pagelen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 argp->pagelen -= len;
1099 }
1100 }
Al Viro2ebbc012006-10-19 23:28:58 -07001101 argp->end = (__be32*) (argp->rqstp->rq_vec[v].iov_base + argp->rqstp->rq_vec[v].iov_len);
1102 argp->p = (__be32*) (argp->rqstp->rq_vec[v].iov_base + (XDR_QUADLEN(len) << 2));
NeilBrown3cc03b12006-10-04 02:15:47 -07001103 argp->rqstp->rq_vec[v].iov_len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 write->wr_vlen = v+1;
1105
1106 DECODE_TAIL;
1107}
1108
Al Virob37ad282006-10-19 23:28:59 -07001109static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110nfsd4_decode_release_lockowner(struct nfsd4_compoundargs *argp, struct nfsd4_release_lockowner *rlockowner)
1111{
1112 DECODE_HEAD;
1113
1114 READ_BUF(12);
1115 COPYMEM(&rlockowner->rl_clientid, sizeof(clientid_t));
1116 READ32(rlockowner->rl_owner.len);
1117 READ_BUF(rlockowner->rl_owner.len);
1118 READMEM(rlockowner->rl_owner.data, rlockowner->rl_owner.len);
1119
Andy Adamson60adfc52009-04-03 08:28:50 +03001120 if (argp->minorversion && !zero_clientid(&rlockowner->rl_clientid))
1121 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 DECODE_TAIL;
1123}
1124
Al Virob37ad282006-10-19 23:28:59 -07001125static __be32
Andy Adamson2db134e2009-04-03 08:27:55 +03001126nfsd4_decode_exchange_id(struct nfsd4_compoundargs *argp,
Andy Adamson0733d212009-04-03 08:28:01 +03001127 struct nfsd4_exchange_id *exid)
Andy Adamson2db134e2009-04-03 08:27:55 +03001128{
Mi Jinlong5afa0402010-11-09 09:39:23 +08001129 int dummy, tmp;
Andy Adamson0733d212009-04-03 08:28:01 +03001130 DECODE_HEAD;
1131
1132 READ_BUF(NFS4_VERIFIER_SIZE);
1133 COPYMEM(exid->verifier.data, NFS4_VERIFIER_SIZE);
1134
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04001135 status = nfsd4_decode_opaque(argp, &exid->clname);
1136 if (status)
1137 return nfserr_bad_xdr;
Andy Adamson0733d212009-04-03 08:28:01 +03001138
1139 READ_BUF(4);
1140 READ32(exid->flags);
1141
1142 /* Ignore state_protect4_a */
1143 READ_BUF(4);
1144 READ32(exid->spa_how);
1145 switch (exid->spa_how) {
1146 case SP4_NONE:
1147 break;
1148 case SP4_MACH_CRED:
1149 /* spo_must_enforce */
1150 READ_BUF(4);
1151 READ32(dummy);
1152 READ_BUF(dummy * 4);
1153 p += dummy;
1154
1155 /* spo_must_allow */
1156 READ_BUF(4);
1157 READ32(dummy);
1158 READ_BUF(dummy * 4);
1159 p += dummy;
1160 break;
1161 case SP4_SSV:
1162 /* ssp_ops */
1163 READ_BUF(4);
1164 READ32(dummy);
1165 READ_BUF(dummy * 4);
1166 p += dummy;
1167
1168 READ_BUF(4);
1169 READ32(dummy);
1170 READ_BUF(dummy * 4);
1171 p += dummy;
1172
1173 /* ssp_hash_algs<> */
1174 READ_BUF(4);
Mi Jinlong5afa0402010-11-09 09:39:23 +08001175 READ32(tmp);
1176 while (tmp--) {
1177 READ_BUF(4);
1178 READ32(dummy);
1179 READ_BUF(dummy);
1180 p += XDR_QUADLEN(dummy);
1181 }
Andy Adamson0733d212009-04-03 08:28:01 +03001182
1183 /* ssp_encr_algs<> */
1184 READ_BUF(4);
Mi Jinlong5afa0402010-11-09 09:39:23 +08001185 READ32(tmp);
1186 while (tmp--) {
1187 READ_BUF(4);
1188 READ32(dummy);
1189 READ_BUF(dummy);
1190 p += XDR_QUADLEN(dummy);
1191 }
Andy Adamson0733d212009-04-03 08:28:01 +03001192
1193 /* ssp_window and ssp_num_gss_handles */
1194 READ_BUF(8);
1195 READ32(dummy);
1196 READ32(dummy);
1197 break;
1198 default:
1199 goto xdr_error;
1200 }
1201
1202 /* Ignore Implementation ID */
1203 READ_BUF(4); /* nfs_impl_id4 array length */
1204 READ32(dummy);
1205
1206 if (dummy > 1)
1207 goto xdr_error;
1208
1209 if (dummy == 1) {
1210 /* nii_domain */
1211 READ_BUF(4);
1212 READ32(dummy);
1213 READ_BUF(dummy);
1214 p += XDR_QUADLEN(dummy);
1215
1216 /* nii_name */
1217 READ_BUF(4);
1218 READ32(dummy);
1219 READ_BUF(dummy);
1220 p += XDR_QUADLEN(dummy);
1221
1222 /* nii_date */
1223 READ_BUF(12);
1224 p += 3;
1225 }
1226 DECODE_TAIL;
Andy Adamson2db134e2009-04-03 08:27:55 +03001227}
1228
1229static __be32
1230nfsd4_decode_create_session(struct nfsd4_compoundargs *argp,
1231 struct nfsd4_create_session *sess)
1232{
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001233 DECODE_HEAD;
1234
1235 u32 dummy;
1236 char *machine_name;
Mi Jinlong5a02ab72011-03-11 12:13:55 +08001237 int i;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001238 int nr_secflavs;
1239
1240 READ_BUF(16);
1241 COPYMEM(&sess->clientid, 8);
1242 READ32(sess->seqid);
1243 READ32(sess->flags);
1244
1245 /* Fore channel attrs */
1246 READ_BUF(28);
1247 READ32(dummy); /* headerpadsz is always 0 */
1248 READ32(sess->fore_channel.maxreq_sz);
1249 READ32(sess->fore_channel.maxresp_sz);
1250 READ32(sess->fore_channel.maxresp_cached);
1251 READ32(sess->fore_channel.maxops);
1252 READ32(sess->fore_channel.maxreqs);
1253 READ32(sess->fore_channel.nr_rdma_attrs);
1254 if (sess->fore_channel.nr_rdma_attrs == 1) {
1255 READ_BUF(4);
1256 READ32(sess->fore_channel.rdma_attrs);
1257 } else if (sess->fore_channel.nr_rdma_attrs > 1) {
1258 dprintk("Too many fore channel attr bitmaps!\n");
1259 goto xdr_error;
1260 }
1261
1262 /* Back channel attrs */
1263 READ_BUF(28);
1264 READ32(dummy); /* headerpadsz is always 0 */
1265 READ32(sess->back_channel.maxreq_sz);
1266 READ32(sess->back_channel.maxresp_sz);
1267 READ32(sess->back_channel.maxresp_cached);
1268 READ32(sess->back_channel.maxops);
1269 READ32(sess->back_channel.maxreqs);
1270 READ32(sess->back_channel.nr_rdma_attrs);
1271 if (sess->back_channel.nr_rdma_attrs == 1) {
1272 READ_BUF(4);
1273 READ32(sess->back_channel.rdma_attrs);
1274 } else if (sess->back_channel.nr_rdma_attrs > 1) {
1275 dprintk("Too many back channel attr bitmaps!\n");
1276 goto xdr_error;
1277 }
1278
1279 READ_BUF(8);
1280 READ32(sess->callback_prog);
1281
1282 /* callback_sec_params4 */
1283 READ32(nr_secflavs);
1284 for (i = 0; i < nr_secflavs; ++i) {
1285 READ_BUF(4);
1286 READ32(dummy);
1287 switch (dummy) {
1288 case RPC_AUTH_NULL:
1289 /* Nothing to read */
1290 break;
1291 case RPC_AUTH_UNIX:
1292 READ_BUF(8);
1293 /* stamp */
1294 READ32(dummy);
1295
1296 /* machine name */
1297 READ32(dummy);
1298 READ_BUF(dummy);
1299 SAVEMEM(machine_name, dummy);
1300
1301 /* uid, gid */
1302 READ_BUF(8);
1303 READ32(sess->uid);
1304 READ32(sess->gid);
1305
1306 /* more gids */
1307 READ_BUF(4);
1308 READ32(dummy);
1309 READ_BUF(dummy * 4);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001310 break;
1311 case RPC_AUTH_GSS:
1312 dprintk("RPC_AUTH_GSS callback secflavor "
1313 "not supported!\n");
1314 READ_BUF(8);
1315 /* gcbp_service */
1316 READ32(dummy);
1317 /* gcbp_handle_from_server */
1318 READ32(dummy);
1319 READ_BUF(dummy);
1320 p += XDR_QUADLEN(dummy);
1321 /* gcbp_handle_from_client */
1322 READ_BUF(4);
1323 READ32(dummy);
1324 READ_BUF(dummy);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001325 break;
1326 default:
1327 dprintk("Illegal callback secflavor\n");
1328 return nfserr_inval;
1329 }
1330 }
1331 DECODE_TAIL;
Andy Adamson2db134e2009-04-03 08:27:55 +03001332}
1333
1334static __be32
1335nfsd4_decode_destroy_session(struct nfsd4_compoundargs *argp,
1336 struct nfsd4_destroy_session *destroy_session)
1337{
Benny Halevye10e0cf2009-04-03 08:28:38 +03001338 DECODE_HEAD;
1339 READ_BUF(NFS4_MAX_SESSIONID_LEN);
1340 COPYMEM(destroy_session->sessionid.data, NFS4_MAX_SESSIONID_LEN);
1341
1342 DECODE_TAIL;
Andy Adamson2db134e2009-04-03 08:27:55 +03001343}
1344
1345static __be32
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04001346nfsd4_decode_free_stateid(struct nfsd4_compoundargs *argp,
1347 struct nfsd4_free_stateid *free_stateid)
1348{
1349 DECODE_HEAD;
1350
1351 READ_BUF(sizeof(stateid_t));
1352 READ32(free_stateid->fr_stateid.si_generation);
1353 COPYMEM(&free_stateid->fr_stateid.si_opaque, sizeof(stateid_opaque_t));
1354
1355 DECODE_TAIL;
1356}
1357
1358static __be32
Andy Adamson2db134e2009-04-03 08:27:55 +03001359nfsd4_decode_sequence(struct nfsd4_compoundargs *argp,
1360 struct nfsd4_sequence *seq)
1361{
Benny Halevyb85d4c02009-04-03 08:28:08 +03001362 DECODE_HEAD;
1363
1364 READ_BUF(NFS4_MAX_SESSIONID_LEN + 16);
1365 COPYMEM(seq->sessionid.data, NFS4_MAX_SESSIONID_LEN);
1366 READ32(seq->seqid);
1367 READ32(seq->slotid);
1368 READ32(seq->maxslots);
1369 READ32(seq->cachethis);
1370
1371 DECODE_TAIL;
Andy Adamson2db134e2009-04-03 08:27:55 +03001372}
1373
Bryan Schumaker17456802011-07-13 10:50:48 -04001374static __be32
1375nfsd4_decode_test_stateid(struct nfsd4_compoundargs *argp, struct nfsd4_test_stateid *test_stateid)
1376{
Bryan Schumaker17456802011-07-13 10:50:48 -04001377 int i;
Bryan Schumaker03cfb422012-01-27 10:22:49 -05001378 __be32 *p, status;
1379 struct nfsd4_test_stateid_id *stateid;
Bryan Schumaker17456802011-07-13 10:50:48 -04001380
1381 READ_BUF(4);
1382 test_stateid->ts_num_ids = ntohl(*p++);
1383
Bryan Schumaker03cfb422012-01-27 10:22:49 -05001384 INIT_LIST_HEAD(&test_stateid->ts_stateid_list);
Bryan Schumaker17456802011-07-13 10:50:48 -04001385
1386 for (i = 0; i < test_stateid->ts_num_ids; i++) {
Bryan Schumaker03cfb422012-01-27 10:22:49 -05001387 stateid = kmalloc(sizeof(struct nfsd4_test_stateid_id), GFP_KERNEL);
1388 if (!stateid) {
Al Viroafcf6792012-04-13 00:15:37 -04001389 status = nfserrno(-ENOMEM);
Bryan Schumaker03cfb422012-01-27 10:22:49 -05001390 goto out;
1391 }
1392
1393 defer_free(argp, kfree, stateid);
1394 INIT_LIST_HEAD(&stateid->ts_id_list);
1395 list_add_tail(&stateid->ts_id_list, &test_stateid->ts_stateid_list);
1396
1397 status = nfsd4_decode_stateid(argp, &stateid->ts_id_stateid);
Bryan Schumaker17456802011-07-13 10:50:48 -04001398 if (status)
Bryan Schumaker03cfb422012-01-27 10:22:49 -05001399 goto out;
Bryan Schumaker17456802011-07-13 10:50:48 -04001400 }
1401
1402 status = 0;
1403out:
1404 return status;
1405xdr_error:
1406 dprintk("NFSD: xdr error (%s:%d)\n", __FILE__, __LINE__);
1407 status = nfserr_bad_xdr;
1408 goto out;
1409}
1410
Mi Jinlong345c2842011-10-20 17:51:39 +08001411static __be32 nfsd4_decode_destroy_clientid(struct nfsd4_compoundargs *argp, struct nfsd4_destroy_clientid *dc)
1412{
1413 DECODE_HEAD;
1414
1415 READ_BUF(8);
1416 COPYMEM(&dc->clientid, 8);
1417
1418 DECODE_TAIL;
1419}
1420
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04001421static __be32 nfsd4_decode_reclaim_complete(struct nfsd4_compoundargs *argp, struct nfsd4_reclaim_complete *rc)
1422{
1423 DECODE_HEAD;
1424
1425 READ_BUF(4);
1426 READ32(rc->rca_one_fs);
1427
1428 DECODE_TAIL;
1429}
1430
Andy Adamson2db134e2009-04-03 08:27:55 +03001431static __be32
Benny Halevy347e0ad2008-07-02 11:13:41 +03001432nfsd4_decode_noop(struct nfsd4_compoundargs *argp, void *p)
1433{
1434 return nfs_ok;
1435}
1436
Benny Halevy3c375c62008-07-02 11:14:01 +03001437static __be32
1438nfsd4_decode_notsupp(struct nfsd4_compoundargs *argp, void *p)
1439{
Benny Halevy1e685ec2009-03-04 23:06:06 +02001440 return nfserr_notsupp;
Benny Halevy3c375c62008-07-02 11:14:01 +03001441}
1442
Benny Halevy347e0ad2008-07-02 11:13:41 +03001443typedef __be32(*nfsd4_dec)(struct nfsd4_compoundargs *argp, void *);
1444
1445static nfsd4_dec nfsd4_dec_ops[] = {
J. Bruce Fieldsad1060c2008-07-18 15:04:16 -04001446 [OP_ACCESS] = (nfsd4_dec)nfsd4_decode_access,
1447 [OP_CLOSE] = (nfsd4_dec)nfsd4_decode_close,
1448 [OP_COMMIT] = (nfsd4_dec)nfsd4_decode_commit,
1449 [OP_CREATE] = (nfsd4_dec)nfsd4_decode_create,
1450 [OP_DELEGPURGE] = (nfsd4_dec)nfsd4_decode_notsupp,
1451 [OP_DELEGRETURN] = (nfsd4_dec)nfsd4_decode_delegreturn,
1452 [OP_GETATTR] = (nfsd4_dec)nfsd4_decode_getattr,
1453 [OP_GETFH] = (nfsd4_dec)nfsd4_decode_noop,
1454 [OP_LINK] = (nfsd4_dec)nfsd4_decode_link,
1455 [OP_LOCK] = (nfsd4_dec)nfsd4_decode_lock,
1456 [OP_LOCKT] = (nfsd4_dec)nfsd4_decode_lockt,
1457 [OP_LOCKU] = (nfsd4_dec)nfsd4_decode_locku,
1458 [OP_LOOKUP] = (nfsd4_dec)nfsd4_decode_lookup,
1459 [OP_LOOKUPP] = (nfsd4_dec)nfsd4_decode_noop,
1460 [OP_NVERIFY] = (nfsd4_dec)nfsd4_decode_verify,
1461 [OP_OPEN] = (nfsd4_dec)nfsd4_decode_open,
1462 [OP_OPENATTR] = (nfsd4_dec)nfsd4_decode_notsupp,
1463 [OP_OPEN_CONFIRM] = (nfsd4_dec)nfsd4_decode_open_confirm,
1464 [OP_OPEN_DOWNGRADE] = (nfsd4_dec)nfsd4_decode_open_downgrade,
1465 [OP_PUTFH] = (nfsd4_dec)nfsd4_decode_putfh,
J. Bruce Fieldsa1c8c4d2009-03-09 12:17:29 -04001466 [OP_PUTPUBFH] = (nfsd4_dec)nfsd4_decode_noop,
J. Bruce Fieldsad1060c2008-07-18 15:04:16 -04001467 [OP_PUTROOTFH] = (nfsd4_dec)nfsd4_decode_noop,
1468 [OP_READ] = (nfsd4_dec)nfsd4_decode_read,
1469 [OP_READDIR] = (nfsd4_dec)nfsd4_decode_readdir,
1470 [OP_READLINK] = (nfsd4_dec)nfsd4_decode_noop,
1471 [OP_REMOVE] = (nfsd4_dec)nfsd4_decode_remove,
1472 [OP_RENAME] = (nfsd4_dec)nfsd4_decode_rename,
1473 [OP_RENEW] = (nfsd4_dec)nfsd4_decode_renew,
1474 [OP_RESTOREFH] = (nfsd4_dec)nfsd4_decode_noop,
1475 [OP_SAVEFH] = (nfsd4_dec)nfsd4_decode_noop,
1476 [OP_SECINFO] = (nfsd4_dec)nfsd4_decode_secinfo,
1477 [OP_SETATTR] = (nfsd4_dec)nfsd4_decode_setattr,
1478 [OP_SETCLIENTID] = (nfsd4_dec)nfsd4_decode_setclientid,
1479 [OP_SETCLIENTID_CONFIRM] = (nfsd4_dec)nfsd4_decode_setclientid_confirm,
1480 [OP_VERIFY] = (nfsd4_dec)nfsd4_decode_verify,
1481 [OP_WRITE] = (nfsd4_dec)nfsd4_decode_write,
1482 [OP_RELEASE_LOCKOWNER] = (nfsd4_dec)nfsd4_decode_release_lockowner,
Benny Halevy347e0ad2008-07-02 11:13:41 +03001483};
1484
Andy Adamson2db134e2009-04-03 08:27:55 +03001485static nfsd4_dec nfsd41_dec_ops[] = {
Randy Dunlap9064caa2009-04-28 16:48:25 -07001486 [OP_ACCESS] = (nfsd4_dec)nfsd4_decode_access,
1487 [OP_CLOSE] = (nfsd4_dec)nfsd4_decode_close,
1488 [OP_COMMIT] = (nfsd4_dec)nfsd4_decode_commit,
1489 [OP_CREATE] = (nfsd4_dec)nfsd4_decode_create,
1490 [OP_DELEGPURGE] = (nfsd4_dec)nfsd4_decode_notsupp,
1491 [OP_DELEGRETURN] = (nfsd4_dec)nfsd4_decode_delegreturn,
1492 [OP_GETATTR] = (nfsd4_dec)nfsd4_decode_getattr,
1493 [OP_GETFH] = (nfsd4_dec)nfsd4_decode_noop,
1494 [OP_LINK] = (nfsd4_dec)nfsd4_decode_link,
1495 [OP_LOCK] = (nfsd4_dec)nfsd4_decode_lock,
1496 [OP_LOCKT] = (nfsd4_dec)nfsd4_decode_lockt,
1497 [OP_LOCKU] = (nfsd4_dec)nfsd4_decode_locku,
1498 [OP_LOOKUP] = (nfsd4_dec)nfsd4_decode_lookup,
1499 [OP_LOOKUPP] = (nfsd4_dec)nfsd4_decode_noop,
1500 [OP_NVERIFY] = (nfsd4_dec)nfsd4_decode_verify,
1501 [OP_OPEN] = (nfsd4_dec)nfsd4_decode_open,
1502 [OP_OPENATTR] = (nfsd4_dec)nfsd4_decode_notsupp,
1503 [OP_OPEN_CONFIRM] = (nfsd4_dec)nfsd4_decode_notsupp,
1504 [OP_OPEN_DOWNGRADE] = (nfsd4_dec)nfsd4_decode_open_downgrade,
1505 [OP_PUTFH] = (nfsd4_dec)nfsd4_decode_putfh,
1506 [OP_PUTPUBFH] = (nfsd4_dec)nfsd4_decode_notsupp,
1507 [OP_PUTROOTFH] = (nfsd4_dec)nfsd4_decode_noop,
1508 [OP_READ] = (nfsd4_dec)nfsd4_decode_read,
1509 [OP_READDIR] = (nfsd4_dec)nfsd4_decode_readdir,
1510 [OP_READLINK] = (nfsd4_dec)nfsd4_decode_noop,
1511 [OP_REMOVE] = (nfsd4_dec)nfsd4_decode_remove,
1512 [OP_RENAME] = (nfsd4_dec)nfsd4_decode_rename,
1513 [OP_RENEW] = (nfsd4_dec)nfsd4_decode_notsupp,
1514 [OP_RESTOREFH] = (nfsd4_dec)nfsd4_decode_noop,
1515 [OP_SAVEFH] = (nfsd4_dec)nfsd4_decode_noop,
1516 [OP_SECINFO] = (nfsd4_dec)nfsd4_decode_secinfo,
1517 [OP_SETATTR] = (nfsd4_dec)nfsd4_decode_setattr,
1518 [OP_SETCLIENTID] = (nfsd4_dec)nfsd4_decode_notsupp,
1519 [OP_SETCLIENTID_CONFIRM]= (nfsd4_dec)nfsd4_decode_notsupp,
1520 [OP_VERIFY] = (nfsd4_dec)nfsd4_decode_verify,
1521 [OP_WRITE] = (nfsd4_dec)nfsd4_decode_write,
1522 [OP_RELEASE_LOCKOWNER] = (nfsd4_dec)nfsd4_decode_notsupp,
Andy Adamson2db134e2009-04-03 08:27:55 +03001523
1524 /* new operations for NFSv4.1 */
Randy Dunlap9064caa2009-04-28 16:48:25 -07001525 [OP_BACKCHANNEL_CTL] = (nfsd4_dec)nfsd4_decode_notsupp,
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001526 [OP_BIND_CONN_TO_SESSION]= (nfsd4_dec)nfsd4_decode_bind_conn_to_session,
Randy Dunlap9064caa2009-04-28 16:48:25 -07001527 [OP_EXCHANGE_ID] = (nfsd4_dec)nfsd4_decode_exchange_id,
1528 [OP_CREATE_SESSION] = (nfsd4_dec)nfsd4_decode_create_session,
1529 [OP_DESTROY_SESSION] = (nfsd4_dec)nfsd4_decode_destroy_session,
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04001530 [OP_FREE_STATEID] = (nfsd4_dec)nfsd4_decode_free_stateid,
Randy Dunlap9064caa2009-04-28 16:48:25 -07001531 [OP_GET_DIR_DELEGATION] = (nfsd4_dec)nfsd4_decode_notsupp,
1532 [OP_GETDEVICEINFO] = (nfsd4_dec)nfsd4_decode_notsupp,
1533 [OP_GETDEVICELIST] = (nfsd4_dec)nfsd4_decode_notsupp,
1534 [OP_LAYOUTCOMMIT] = (nfsd4_dec)nfsd4_decode_notsupp,
1535 [OP_LAYOUTGET] = (nfsd4_dec)nfsd4_decode_notsupp,
1536 [OP_LAYOUTRETURN] = (nfsd4_dec)nfsd4_decode_notsupp,
J. Bruce Fields04f4ad12010-12-16 09:51:13 -05001537 [OP_SECINFO_NO_NAME] = (nfsd4_dec)nfsd4_decode_secinfo_no_name,
Randy Dunlap9064caa2009-04-28 16:48:25 -07001538 [OP_SEQUENCE] = (nfsd4_dec)nfsd4_decode_sequence,
1539 [OP_SET_SSV] = (nfsd4_dec)nfsd4_decode_notsupp,
Bryan Schumaker17456802011-07-13 10:50:48 -04001540 [OP_TEST_STATEID] = (nfsd4_dec)nfsd4_decode_test_stateid,
Randy Dunlap9064caa2009-04-28 16:48:25 -07001541 [OP_WANT_DELEGATION] = (nfsd4_dec)nfsd4_decode_notsupp,
Mi Jinlong345c2842011-10-20 17:51:39 +08001542 [OP_DESTROY_CLIENTID] = (nfsd4_dec)nfsd4_decode_destroy_clientid,
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04001543 [OP_RECLAIM_COMPLETE] = (nfsd4_dec)nfsd4_decode_reclaim_complete,
Andy Adamson2db134e2009-04-03 08:27:55 +03001544};
1545
Benny Halevyf2feb962008-07-02 11:14:22 +03001546struct nfsd4_minorversion_ops {
1547 nfsd4_dec *decoders;
1548 int nops;
1549};
1550
1551static struct nfsd4_minorversion_ops nfsd4_minorversion[] = {
J. Bruce Fieldsad1060c2008-07-18 15:04:16 -04001552 [0] = { nfsd4_dec_ops, ARRAY_SIZE(nfsd4_dec_ops) },
Andy Adamson2db134e2009-04-03 08:27:55 +03001553 [1] = { nfsd41_dec_ops, ARRAY_SIZE(nfsd41_dec_ops) },
Benny Halevyf2feb962008-07-02 11:14:22 +03001554};
1555
Benny Halevy347e0ad2008-07-02 11:13:41 +03001556static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557nfsd4_decode_compound(struct nfsd4_compoundargs *argp)
1558{
1559 DECODE_HEAD;
1560 struct nfsd4_op *op;
Benny Halevyf2feb962008-07-02 11:14:22 +03001561 struct nfsd4_minorversion_ops *ops;
J. Bruce Fields10910062011-01-24 12:11:02 -05001562 bool cachethis = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 int i;
1564
1565 /*
1566 * XXX: According to spec, we should check the tag
1567 * for UTF-8 compliance. I'm postponing this for
1568 * now because it seems that some clients do use
1569 * binary tags.
1570 */
1571 READ_BUF(4);
1572 READ32(argp->taglen);
1573 READ_BUF(argp->taglen + 8);
1574 SAVEMEM(argp->tag, argp->taglen);
1575 READ32(argp->minorversion);
1576 READ32(argp->opcnt);
1577
1578 if (argp->taglen > NFSD4_MAX_TAGLEN)
1579 goto xdr_error;
1580 if (argp->opcnt > 100)
1581 goto xdr_error;
1582
Tobias Klausere8c96f82006-03-24 03:15:34 -08001583 if (argp->opcnt > ARRAY_SIZE(argp->iops)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 argp->ops = kmalloc(argp->opcnt * sizeof(*argp->ops), GFP_KERNEL);
1585 if (!argp->ops) {
1586 argp->ops = argp->iops;
Chuck Lever817cb9d2007-09-11 18:01:20 -04001587 dprintk("nfsd: couldn't allocate room for COMPOUND\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 goto xdr_error;
1589 }
1590 }
1591
Benny Halevyf2feb962008-07-02 11:14:22 +03001592 if (argp->minorversion >= ARRAY_SIZE(nfsd4_minorversion))
Benny Halevy30cff1f2008-07-02 11:13:18 +03001593 argp->opcnt = 0;
1594
Benny Halevyf2feb962008-07-02 11:14:22 +03001595 ops = &nfsd4_minorversion[argp->minorversion];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 for (i = 0; i < argp->opcnt; i++) {
1597 op = &argp->ops[i];
1598 op->replay = NULL;
1599
1600 /*
1601 * We can't use READ_BUF() here because we need to handle
1602 * a missing opcode as an OP_WRITE + 1. So we need to check
1603 * to see if we're truly at the end of our buffer or if there
1604 * is another page we need to flip to.
1605 */
1606
1607 if (argp->p == argp->end) {
1608 if (argp->pagelen < 4) {
1609 /* There isn't an opcode still on the wire */
1610 op->opnum = OP_WRITE + 1;
1611 op->status = nfserr_bad_xdr;
1612 argp->opcnt = i+1;
1613 break;
1614 }
1615
1616 /*
1617 * False alarm. We just hit a page boundary, but there
1618 * is still data available. Move pointer across page
1619 * boundary. *snip from READ_BUF*
1620 */
1621 argp->p = page_address(argp->pagelist[0]);
1622 argp->pagelist++;
1623 if (argp->pagelen < PAGE_SIZE) {
Neil Brown2bc3c112010-04-20 12:16:52 +10001624 argp->end = argp->p + (argp->pagelen>>2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 argp->pagelen = 0;
1626 } else {
Neil Brown2bc3c112010-04-20 12:16:52 +10001627 argp->end = argp->p + (PAGE_SIZE>>2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 argp->pagelen -= PAGE_SIZE;
1629 }
1630 }
1631 op->opnum = ntohl(*argp->p++);
1632
Ricardo Labiagade3cab72009-12-11 20:03:27 -08001633 if (op->opnum >= FIRST_NFS4_OP && op->opnum <= LAST_NFS4_OP)
Benny Halevyf2feb962008-07-02 11:14:22 +03001634 op->status = ops->decoders[op->opnum](argp, &op->u);
Benny Halevy347e0ad2008-07-02 11:13:41 +03001635 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 op->opnum = OP_ILLEGAL;
1637 op->status = nfserr_op_illegal;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 }
1639
1640 if (op->status) {
1641 argp->opcnt = i+1;
1642 break;
1643 }
J. Bruce Fields10910062011-01-24 12:11:02 -05001644 /*
1645 * We'll try to cache the result in the DRC if any one
1646 * op in the compound wants to be cached:
1647 */
1648 cachethis |= nfsd4_cache_this_op(op);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 }
J. Bruce Fields10910062011-01-24 12:11:02 -05001650 /* Sessions make the DRC unnecessary: */
1651 if (argp->minorversion)
1652 cachethis = false;
1653 argp->rqstp->rq_cachetype = cachethis ? RC_REPLBUFF : RC_NOCACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654
1655 DECODE_TAIL;
1656}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658#define WRITE32(n) *p++ = htonl(n)
1659#define WRITE64(n) do { \
1660 *p++ = htonl((u32)((n) >> 32)); \
1661 *p++ = htonl((u32)(n)); \
1662} while (0)
Harvey Harrison5108b272008-07-17 21:33:04 -07001663#define WRITEMEM(ptr,nbytes) do { if (nbytes > 0) { \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 *(p + XDR_QUADLEN(nbytes) -1) = 0; \
1665 memcpy(p, ptr, nbytes); \
1666 p += XDR_QUADLEN(nbytes); \
Harvey Harrison5108b272008-07-17 21:33:04 -07001667}} while (0)
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04001668
1669static void write32(__be32 **p, u32 n)
1670{
1671 *(*p)++ = n;
1672}
1673
1674static void write64(__be32 **p, u64 n)
1675{
1676 write32(p, (u32)(n >> 32));
1677 write32(p, (u32)n);
1678}
1679
1680static void write_change(__be32 **p, struct kstat *stat, struct inode *inode)
1681{
1682 if (IS_I_VERSION(inode)) {
1683 write64(p, inode->i_version);
1684 } else {
1685 write32(p, stat->ctime.tv_sec);
1686 write32(p, stat->ctime.tv_nsec);
1687 }
1688}
1689
1690static void write_cinfo(__be32 **p, struct nfsd4_change_info *c)
1691{
1692 write32(p, c->atomic);
1693 if (c->change_supported) {
1694 write64(p, c->before_change);
1695 write64(p, c->after_change);
1696 } else {
1697 write32(p, c->before_ctime_sec);
1698 write32(p, c->before_ctime_nsec);
1699 write32(p, c->after_ctime_sec);
1700 write32(p, c->after_ctime_nsec);
1701 }
1702}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
1704#define RESERVE_SPACE(nbytes) do { \
1705 p = resp->p; \
1706 BUG_ON(p + XDR_QUADLEN(nbytes) > resp->end); \
1707} while (0)
1708#define ADJUST_ARGS() resp->p = p
1709
1710/*
1711 * Header routine to setup seqid operation replay cache
1712 */
1713#define ENCODE_SEQID_OP_HEAD \
Al Viro2ebbc012006-10-19 23:28:58 -07001714 __be32 *save; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 \
1716 save = resp->p;
1717
1718/*
NeilBrown7fb64ce2005-07-07 17:59:20 -07001719 * Routine for encoding the result of a "seqid-mutating" NFSv4 operation. This
1720 * is where sequence id's are incremented, and the replay cache is filled.
1721 * Note that we increment sequence id's here, at the last moment, so we're sure
1722 * we know whether the error to be returned is a sequence id mutating error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 */
1724
J. Bruce Fieldsf3e42232011-08-24 12:27:31 -04001725static void encode_seqid_op_tail(struct nfsd4_compoundres *resp, __be32 *save, __be32 nfserr)
1726{
1727 struct nfs4_stateowner *stateowner = resp->cstate.replay_owner;
1728
1729 if (seqid_mutating_err(ntohl(nfserr)) && stateowner) {
1730 stateowner->so_seqid++;
1731 stateowner->so_replay.rp_status = nfserr;
1732 stateowner->so_replay.rp_buflen =
1733 (char *)resp->p - (char *)save;
1734 memcpy(stateowner->so_replay.rp_buf, save,
1735 stateowner->so_replay.rp_buflen);
J. Bruce Fields38c387b2011-09-16 17:42:48 -04001736 nfsd4_purge_closed_stateid(stateowner);
J. Bruce Fieldsf3e42232011-08-24 12:27:31 -04001737 }
1738}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001740/* Encode as an array of strings the string given with components
Daniel Mack3ad2f3f2010-02-03 08:01:28 +08001741 * separated @sep.
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001742 */
Al Virob37ad282006-10-19 23:28:59 -07001743static __be32 nfsd4_encode_components(char sep, char *components,
Al Viro2ebbc012006-10-19 23:28:58 -07001744 __be32 **pp, int *buflen)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001745{
Al Viro2ebbc012006-10-19 23:28:58 -07001746 __be32 *p = *pp;
1747 __be32 *countp = p;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001748 int strlen, count=0;
1749 char *str, *end;
1750
1751 dprintk("nfsd4_encode_components(%s)\n", components);
1752 if ((*buflen -= 4) < 0)
1753 return nfserr_resource;
1754 WRITE32(0); /* We will fill this in with @count later */
1755 end = str = components;
1756 while (*end) {
1757 for (; *end && (*end != sep); end++)
1758 ; /* Point to end of component */
1759 strlen = end - str;
1760 if (strlen) {
1761 if ((*buflen -= ((XDR_QUADLEN(strlen) << 2) + 4)) < 0)
1762 return nfserr_resource;
1763 WRITE32(strlen);
1764 WRITEMEM(str, strlen);
1765 count++;
1766 }
1767 else
1768 end++;
1769 str = end;
1770 }
1771 *pp = p;
1772 p = countp;
1773 WRITE32(count);
1774 return 0;
1775}
1776
1777/*
1778 * encode a location element of a fs_locations structure
1779 */
Al Virob37ad282006-10-19 23:28:59 -07001780static __be32 nfsd4_encode_fs_location4(struct nfsd4_fs_location *location,
Al Viro2ebbc012006-10-19 23:28:58 -07001781 __be32 **pp, int *buflen)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001782{
Al Virob37ad282006-10-19 23:28:59 -07001783 __be32 status;
Al Viro2ebbc012006-10-19 23:28:58 -07001784 __be32 *p = *pp;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001785
1786 status = nfsd4_encode_components(':', location->hosts, &p, buflen);
1787 if (status)
1788 return status;
1789 status = nfsd4_encode_components('/', location->path, &p, buflen);
1790 if (status)
1791 return status;
1792 *pp = p;
1793 return 0;
1794}
1795
1796/*
Trond Myklebusted748aa2011-09-12 19:37:06 -04001797 * Encode a path in RFC3530 'pathname4' format
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001798 */
Trond Myklebusted748aa2011-09-12 19:37:06 -04001799static __be32 nfsd4_encode_path(const struct path *root,
1800 const struct path *path, __be32 **pp, int *buflen)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001801{
Trond Myklebusted748aa2011-09-12 19:37:06 -04001802 struct path cur = {
1803 .mnt = path->mnt,
1804 .dentry = path->dentry,
1805 };
1806 __be32 *p = *pp;
1807 struct dentry **components = NULL;
1808 unsigned int ncomponents = 0;
1809 __be32 err = nfserr_jukebox;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001810
Trond Myklebusted748aa2011-09-12 19:37:06 -04001811 dprintk("nfsd4_encode_components(");
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001812
Trond Myklebusted748aa2011-09-12 19:37:06 -04001813 path_get(&cur);
1814 /* First walk the path up to the nfsd root, and store the
1815 * dentries/path components in an array.
1816 */
1817 for (;;) {
1818 if (cur.dentry == root->dentry && cur.mnt == root->mnt)
1819 break;
1820 if (cur.dentry == cur.mnt->mnt_root) {
1821 if (follow_up(&cur))
1822 continue;
1823 goto out_free;
1824 }
1825 if ((ncomponents & 15) == 0) {
1826 struct dentry **new;
1827 new = krealloc(components,
1828 sizeof(*new) * (ncomponents + 16),
1829 GFP_KERNEL);
1830 if (!new)
1831 goto out_free;
1832 components = new;
1833 }
1834 components[ncomponents++] = cur.dentry;
1835 cur.dentry = dget_parent(cur.dentry);
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001836 }
Trond Myklebusted748aa2011-09-12 19:37:06 -04001837
1838 *buflen -= 4;
1839 if (*buflen < 0)
1840 goto out_free;
1841 WRITE32(ncomponents);
1842
1843 while (ncomponents) {
1844 struct dentry *dentry = components[ncomponents - 1];
1845 unsigned int len = dentry->d_name.len;
1846
1847 *buflen -= 4 + (XDR_QUADLEN(len) << 2);
1848 if (*buflen < 0)
1849 goto out_free;
1850 WRITE32(len);
1851 WRITEMEM(dentry->d_name.name, len);
1852 dprintk("/%s", dentry->d_name.name);
1853 dput(dentry);
1854 ncomponents--;
1855 }
1856
1857 *pp = p;
1858 err = 0;
1859out_free:
1860 dprintk(")\n");
1861 while (ncomponents)
1862 dput(components[--ncomponents]);
1863 kfree(components);
1864 path_put(&cur);
1865 return err;
1866}
1867
1868static __be32 nfsd4_encode_fsloc_fsroot(struct svc_rqst *rqstp,
1869 const struct path *path, __be32 **pp, int *buflen)
1870{
1871 struct svc_export *exp_ps;
1872 __be32 res;
1873
1874 exp_ps = rqst_find_fsidzero_export(rqstp);
1875 if (IS_ERR(exp_ps))
1876 return nfserrno(PTR_ERR(exp_ps));
1877 res = nfsd4_encode_path(&exp_ps->ex_path, path, pp, buflen);
1878 exp_put(exp_ps);
1879 return res;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001880}
1881
1882/*
1883 * encode a fs_locations structure
1884 */
Al Virob37ad282006-10-19 23:28:59 -07001885static __be32 nfsd4_encode_fs_locations(struct svc_rqst *rqstp,
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001886 struct svc_export *exp,
Al Viro2ebbc012006-10-19 23:28:58 -07001887 __be32 **pp, int *buflen)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001888{
Al Virob37ad282006-10-19 23:28:59 -07001889 __be32 status;
Al Virocc45f012006-10-19 23:28:44 -07001890 int i;
Al Viro2ebbc012006-10-19 23:28:58 -07001891 __be32 *p = *pp;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001892 struct nfsd4_fs_locations *fslocs = &exp->ex_fslocs;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001893
Trond Myklebusted748aa2011-09-12 19:37:06 -04001894 status = nfsd4_encode_fsloc_fsroot(rqstp, &exp->ex_path, &p, buflen);
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001895 if (status)
1896 return status;
1897 if ((*buflen -= 4) < 0)
1898 return nfserr_resource;
1899 WRITE32(fslocs->locations_count);
1900 for (i=0; i<fslocs->locations_count; i++) {
1901 status = nfsd4_encode_fs_location4(&fslocs->locations[i],
1902 &p, buflen);
1903 if (status)
1904 return status;
1905 }
1906 *pp = p;
1907 return 0;
1908}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909
J. Bruce Fields3d2544b2011-08-15 11:49:30 -04001910static u32 nfs4_file_type(umode_t mode)
1911{
1912 switch (mode & S_IFMT) {
1913 case S_IFIFO: return NF4FIFO;
1914 case S_IFCHR: return NF4CHR;
1915 case S_IFDIR: return NF4DIR;
1916 case S_IFBLK: return NF4BLK;
1917 case S_IFLNK: return NF4LNK;
1918 case S_IFREG: return NF4REG;
1919 case S_IFSOCK: return NF4SOCK;
1920 default: return NF4BAD;
1921 };
1922}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923
Al Virob37ad282006-10-19 23:28:59 -07001924static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925nfsd4_encode_name(struct svc_rqst *rqstp, int whotype, uid_t id, int group,
Al Viro2ebbc012006-10-19 23:28:58 -07001926 __be32 **p, int *buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927{
1928 int status;
1929
1930 if (*buflen < (XDR_QUADLEN(IDMAP_NAMESZ) << 2) + 4)
1931 return nfserr_resource;
1932 if (whotype != NFS4_ACL_WHO_NAMED)
1933 status = nfs4_acl_write_who(whotype, (u8 *)(*p + 1));
1934 else if (group)
1935 status = nfsd_map_gid_to_name(rqstp, id, (u8 *)(*p + 1));
1936 else
1937 status = nfsd_map_uid_to_name(rqstp, id, (u8 *)(*p + 1));
1938 if (status < 0)
1939 return nfserrno(status);
1940 *p = xdr_encode_opaque(*p, NULL, status);
1941 *buflen -= (XDR_QUADLEN(status) << 2) + 4;
1942 BUG_ON(*buflen < 0);
1943 return 0;
1944}
1945
Al Virob37ad282006-10-19 23:28:59 -07001946static inline __be32
Al Viro2ebbc012006-10-19 23:28:58 -07001947nfsd4_encode_user(struct svc_rqst *rqstp, uid_t uid, __be32 **p, int *buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948{
1949 return nfsd4_encode_name(rqstp, NFS4_ACL_WHO_NAMED, uid, 0, p, buflen);
1950}
1951
Al Virob37ad282006-10-19 23:28:59 -07001952static inline __be32
Al Viro2ebbc012006-10-19 23:28:58 -07001953nfsd4_encode_group(struct svc_rqst *rqstp, uid_t gid, __be32 **p, int *buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954{
1955 return nfsd4_encode_name(rqstp, NFS4_ACL_WHO_NAMED, gid, 1, p, buflen);
1956}
1957
Al Virob37ad282006-10-19 23:28:59 -07001958static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959nfsd4_encode_aclname(struct svc_rqst *rqstp, int whotype, uid_t id, int group,
Al Viro2ebbc012006-10-19 23:28:58 -07001960 __be32 **p, int *buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961{
1962 return nfsd4_encode_name(rqstp, whotype, id, group, p, buflen);
1963}
1964
J.Bruce Fields42ca0992006-10-04 02:16:20 -07001965#define WORD0_ABSENT_FS_ATTRS (FATTR4_WORD0_FS_LOCATIONS | FATTR4_WORD0_FSID | \
1966 FATTR4_WORD0_RDATTR_ERROR)
1967#define WORD1_ABSENT_FS_ATTRS FATTR4_WORD1_MOUNTED_ON_FILEID
1968
Al Virob37ad282006-10-19 23:28:59 -07001969static __be32 fattr_handle_absent_fs(u32 *bmval0, u32 *bmval1, u32 *rdattr_err)
J.Bruce Fields42ca0992006-10-04 02:16:20 -07001970{
1971 /* As per referral draft: */
1972 if (*bmval0 & ~WORD0_ABSENT_FS_ATTRS ||
1973 *bmval1 & ~WORD1_ABSENT_FS_ATTRS) {
1974 if (*bmval0 & FATTR4_WORD0_RDATTR_ERROR ||
1975 *bmval0 & FATTR4_WORD0_FS_LOCATIONS)
1976 *rdattr_err = NFSERR_MOVED;
1977 else
1978 return nfserr_moved;
1979 }
1980 *bmval0 &= WORD0_ABSENT_FS_ATTRS;
1981 *bmval1 &= WORD1_ABSENT_FS_ATTRS;
1982 return 0;
1983}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984
1985/*
1986 * Note: @fhp can be NULL; in this case, we might have to compose the filehandle
1987 * ourselves.
1988 *
1989 * @countp is the buffer size in _words_; upon successful return this becomes
1990 * replaced with the number of words written.
1991 */
Al Virob37ad282006-10-19 23:28:59 -07001992__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993nfsd4_encode_fattr(struct svc_fh *fhp, struct svc_export *exp,
Al Viro2ebbc012006-10-19 23:28:58 -07001994 struct dentry *dentry, __be32 *buffer, int *countp, u32 *bmval,
Frank Filz406a7ea2007-11-27 11:34:05 -08001995 struct svc_rqst *rqstp, int ignore_crossmnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996{
1997 u32 bmval0 = bmval[0];
1998 u32 bmval1 = bmval[1];
Andy Adamson7e705702009-04-03 08:29:11 +03001999 u32 bmval2 = bmval[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 struct kstat stat;
2001 struct svc_fh tempfh;
2002 struct kstatfs statfs;
2003 int buflen = *countp << 2;
Al Viro2ebbc012006-10-19 23:28:58 -07002004 __be32 *attrlenp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 u32 dummy;
2006 u64 dummy64;
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002007 u32 rdattr_err = 0;
Al Viro2ebbc012006-10-19 23:28:58 -07002008 __be32 *p = buffer;
Al Virob37ad282006-10-19 23:28:59 -07002009 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07002010 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 int aclsupport = 0;
2012 struct nfs4_acl *acl = NULL;
Andy Adamson7e705702009-04-03 08:29:11 +03002013 struct nfsd4_compoundres *resp = rqstp->rq_resp;
2014 u32 minorversion = resp->cstate.minorversion;
Christoph Hellwigebabe9a2010-07-07 18:53:11 +02002015 struct path path = {
2016 .mnt = exp->ex_path.mnt,
2017 .dentry = dentry,
2018 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019
2020 BUG_ON(bmval1 & NFSD_WRITEONLY_ATTRS_WORD1);
Andy Adamson7e705702009-04-03 08:29:11 +03002021 BUG_ON(bmval0 & ~nfsd_suppattrs0(minorversion));
2022 BUG_ON(bmval1 & ~nfsd_suppattrs1(minorversion));
2023 BUG_ON(bmval2 & ~nfsd_suppattrs2(minorversion));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002025 if (exp->ex_fslocs.migrated) {
Andy Adamson7e705702009-04-03 08:29:11 +03002026 BUG_ON(bmval[2]);
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002027 status = fattr_handle_absent_fs(&bmval0, &bmval1, &rdattr_err);
2028 if (status)
2029 goto out;
2030 }
2031
Jan Blunck54775492008-02-14 19:38:39 -08002032 err = vfs_getattr(exp->ex_path.mnt, dentry, &stat);
Al Virob8dd7b92006-10-19 23:29:01 -07002033 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 goto out_nfserr;
Christoph Hellwigbdef8302014-05-28 10:46:13 +02002035 if ((bmval0 & (FATTR4_WORD0_FILES_AVAIL | FATTR4_WORD0_FILES_FREE |
2036 FATTR4_WORD0_FILES_TOTAL | FATTR4_WORD0_MAXNAME)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 (bmval1 & (FATTR4_WORD1_SPACE_AVAIL | FATTR4_WORD1_SPACE_FREE |
2038 FATTR4_WORD1_SPACE_TOTAL))) {
Christoph Hellwigebabe9a2010-07-07 18:53:11 +02002039 err = vfs_statfs(&path, &statfs);
Al Virob8dd7b92006-10-19 23:29:01 -07002040 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 goto out_nfserr;
2042 }
2043 if ((bmval0 & (FATTR4_WORD0_FILEHANDLE | FATTR4_WORD0_FSID)) && !fhp) {
2044 fh_init(&tempfh, NFS4_FHSIZE);
2045 status = fh_compose(&tempfh, exp, dentry, NULL);
2046 if (status)
2047 goto out;
2048 fhp = &tempfh;
2049 }
2050 if (bmval0 & (FATTR4_WORD0_ACL | FATTR4_WORD0_ACLSUPPORT
2051 | FATTR4_WORD0_SUPPORTED_ATTRS)) {
Al Virob8dd7b92006-10-19 23:29:01 -07002052 err = nfsd4_get_nfs4_acl(rqstp, dentry, &acl);
2053 aclsupport = (err == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 if (bmval0 & FATTR4_WORD0_ACL) {
Al Virob8dd7b92006-10-19 23:29:01 -07002055 if (err == -EOPNOTSUPP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 bmval0 &= ~FATTR4_WORD0_ACL;
Al Virob8dd7b92006-10-19 23:29:01 -07002057 else if (err == -EINVAL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 status = nfserr_attrnotsupp;
2059 goto out;
Al Virob8dd7b92006-10-19 23:29:01 -07002060 } else if (err != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 goto out_nfserr;
2062 }
2063 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064
Benny Halevy2b44f1b2010-09-30 20:47:46 +02002065 if (bmval2) {
2066 if ((buflen -= 16) < 0)
2067 goto out_resource;
Andy Adamson7e705702009-04-03 08:29:11 +03002068 WRITE32(3);
2069 WRITE32(bmval0);
2070 WRITE32(bmval1);
2071 WRITE32(bmval2);
Benny Halevy2b44f1b2010-09-30 20:47:46 +02002072 } else if (bmval1) {
2073 if ((buflen -= 12) < 0)
2074 goto out_resource;
Andy Adamson7e705702009-04-03 08:29:11 +03002075 WRITE32(2);
2076 WRITE32(bmval0);
2077 WRITE32(bmval1);
2078 } else {
Benny Halevy2b44f1b2010-09-30 20:47:46 +02002079 if ((buflen -= 8) < 0)
2080 goto out_resource;
Andy Adamson7e705702009-04-03 08:29:11 +03002081 WRITE32(1);
2082 WRITE32(bmval0);
2083 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 attrlenp = p++; /* to be backfilled later */
2085
2086 if (bmval0 & FATTR4_WORD0_SUPPORTED_ATTRS) {
Andy Adamson7e705702009-04-03 08:29:11 +03002087 u32 word0 = nfsd_suppattrs0(minorversion);
2088 u32 word1 = nfsd_suppattrs1(minorversion);
2089 u32 word2 = nfsd_suppattrs2(minorversion);
2090
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002091 if (!aclsupport)
2092 word0 &= ~FATTR4_WORD0_ACL;
Andy Adamson7e705702009-04-03 08:29:11 +03002093 if (!word2) {
Benny Halevy2b44f1b2010-09-30 20:47:46 +02002094 if ((buflen -= 12) < 0)
2095 goto out_resource;
Andy Adamson7e705702009-04-03 08:29:11 +03002096 WRITE32(2);
2097 WRITE32(word0);
2098 WRITE32(word1);
2099 } else {
Benny Halevy2b44f1b2010-09-30 20:47:46 +02002100 if ((buflen -= 16) < 0)
2101 goto out_resource;
Andy Adamson7e705702009-04-03 08:29:11 +03002102 WRITE32(3);
2103 WRITE32(word0);
2104 WRITE32(word1);
2105 WRITE32(word2);
2106 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 }
2108 if (bmval0 & FATTR4_WORD0_TYPE) {
2109 if ((buflen -= 4) < 0)
2110 goto out_resource;
J. Bruce Fields3d2544b2011-08-15 11:49:30 -04002111 dummy = nfs4_file_type(stat.mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 if (dummy == NF4BAD)
2113 goto out_serverfault;
2114 WRITE32(dummy);
2115 }
2116 if (bmval0 & FATTR4_WORD0_FH_EXPIRE_TYPE) {
2117 if ((buflen -= 4) < 0)
2118 goto out_resource;
NeilBrown49640002005-06-23 22:02:58 -07002119 if (exp->ex_flags & NFSEXP_NOSUBTREECHECK)
NeilBrowne34ac862005-07-07 17:59:30 -07002120 WRITE32(NFS4_FH_PERSISTENT);
NeilBrown49640002005-06-23 22:02:58 -07002121 else
NeilBrowne34ac862005-07-07 17:59:30 -07002122 WRITE32(NFS4_FH_PERSISTENT|NFS4_FH_VOL_RENAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 }
2124 if (bmval0 & FATTR4_WORD0_CHANGE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 if ((buflen -= 8) < 0)
2126 goto out_resource;
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002127 write_change(&p, &stat, dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 }
2129 if (bmval0 & FATTR4_WORD0_SIZE) {
2130 if ((buflen -= 8) < 0)
2131 goto out_resource;
2132 WRITE64(stat.size);
2133 }
2134 if (bmval0 & FATTR4_WORD0_LINK_SUPPORT) {
2135 if ((buflen -= 4) < 0)
2136 goto out_resource;
2137 WRITE32(1);
2138 }
2139 if (bmval0 & FATTR4_WORD0_SYMLINK_SUPPORT) {
2140 if ((buflen -= 4) < 0)
2141 goto out_resource;
2142 WRITE32(1);
2143 }
2144 if (bmval0 & FATTR4_WORD0_NAMED_ATTR) {
2145 if ((buflen -= 4) < 0)
2146 goto out_resource;
2147 WRITE32(0);
2148 }
2149 if (bmval0 & FATTR4_WORD0_FSID) {
2150 if ((buflen -= 16) < 0)
2151 goto out_resource;
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002152 if (exp->ex_fslocs.migrated) {
2153 WRITE64(NFS4_REFERRAL_FSID_MAJOR);
2154 WRITE64(NFS4_REFERRAL_FSID_MINOR);
NeilBrownaf6a4e22007-02-14 00:33:12 -08002155 } else switch(fsid_source(fhp)) {
2156 case FSIDSOURCE_FSID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 WRITE64((u64)exp->ex_fsid);
2158 WRITE64((u64)0);
NeilBrownaf6a4e22007-02-14 00:33:12 -08002159 break;
2160 case FSIDSOURCE_DEV:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 WRITE32(0);
2162 WRITE32(MAJOR(stat.dev));
2163 WRITE32(0);
2164 WRITE32(MINOR(stat.dev));
NeilBrownaf6a4e22007-02-14 00:33:12 -08002165 break;
2166 case FSIDSOURCE_UUID:
2167 WRITEMEM(exp->ex_uuid, 16);
2168 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 }
2170 }
2171 if (bmval0 & FATTR4_WORD0_UNIQUE_HANDLES) {
2172 if ((buflen -= 4) < 0)
2173 goto out_resource;
2174 WRITE32(0);
2175 }
2176 if (bmval0 & FATTR4_WORD0_LEASE_TIME) {
2177 if ((buflen -= 4) < 0)
2178 goto out_resource;
J. Bruce Fieldscf07d2e2010-02-28 23:20:19 -05002179 WRITE32(nfsd4_lease);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 }
2181 if (bmval0 & FATTR4_WORD0_RDATTR_ERROR) {
2182 if ((buflen -= 4) < 0)
2183 goto out_resource;
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002184 WRITE32(rdattr_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 }
2186 if (bmval0 & FATTR4_WORD0_ACL) {
2187 struct nfs4_ace *ace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188
2189 if (acl == NULL) {
2190 if ((buflen -= 4) < 0)
2191 goto out_resource;
2192
2193 WRITE32(0);
2194 goto out_acl;
2195 }
2196 if ((buflen -= 4) < 0)
2197 goto out_resource;
2198 WRITE32(acl->naces);
2199
J. Bruce Fields28e05dd2007-02-16 01:28:30 -08002200 for (ace = acl->aces; ace < acl->aces + acl->naces; ace++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 if ((buflen -= 4*3) < 0)
2202 goto out_resource;
2203 WRITE32(ace->type);
2204 WRITE32(ace->flag);
2205 WRITE32(ace->access_mask & NFS4_ACE_MASK_ALL);
2206 status = nfsd4_encode_aclname(rqstp, ace->whotype,
2207 ace->who, ace->flag & NFS4_ACE_IDENTIFIER_GROUP,
2208 &p, &buflen);
2209 if (status == nfserr_resource)
2210 goto out_resource;
2211 if (status)
2212 goto out;
2213 }
2214 }
2215out_acl:
2216 if (bmval0 & FATTR4_WORD0_ACLSUPPORT) {
2217 if ((buflen -= 4) < 0)
2218 goto out_resource;
2219 WRITE32(aclsupport ?
2220 ACL4_SUPPORT_ALLOW_ACL|ACL4_SUPPORT_DENY_ACL : 0);
2221 }
2222 if (bmval0 & FATTR4_WORD0_CANSETTIME) {
2223 if ((buflen -= 4) < 0)
2224 goto out_resource;
2225 WRITE32(1);
2226 }
2227 if (bmval0 & FATTR4_WORD0_CASE_INSENSITIVE) {
2228 if ((buflen -= 4) < 0)
2229 goto out_resource;
J. Bruce Fieldsf0f5dcc2012-06-05 16:52:06 -04002230 WRITE32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 }
2232 if (bmval0 & FATTR4_WORD0_CASE_PRESERVING) {
2233 if ((buflen -= 4) < 0)
2234 goto out_resource;
2235 WRITE32(1);
2236 }
2237 if (bmval0 & FATTR4_WORD0_CHOWN_RESTRICTED) {
2238 if ((buflen -= 4) < 0)
2239 goto out_resource;
2240 WRITE32(1);
2241 }
2242 if (bmval0 & FATTR4_WORD0_FILEHANDLE) {
2243 buflen -= (XDR_QUADLEN(fhp->fh_handle.fh_size) << 2) + 4;
2244 if (buflen < 0)
2245 goto out_resource;
2246 WRITE32(fhp->fh_handle.fh_size);
2247 WRITEMEM(&fhp->fh_handle.fh_base, fhp->fh_handle.fh_size);
2248 }
2249 if (bmval0 & FATTR4_WORD0_FILEID) {
2250 if ((buflen -= 8) < 0)
2251 goto out_resource;
Peter Staubach40ee5dc2007-08-16 12:10:07 -04002252 WRITE64(stat.ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 }
2254 if (bmval0 & FATTR4_WORD0_FILES_AVAIL) {
2255 if ((buflen -= 8) < 0)
2256 goto out_resource;
2257 WRITE64((u64) statfs.f_ffree);
2258 }
2259 if (bmval0 & FATTR4_WORD0_FILES_FREE) {
2260 if ((buflen -= 8) < 0)
2261 goto out_resource;
2262 WRITE64((u64) statfs.f_ffree);
2263 }
2264 if (bmval0 & FATTR4_WORD0_FILES_TOTAL) {
2265 if ((buflen -= 8) < 0)
2266 goto out_resource;
2267 WRITE64((u64) statfs.f_files);
2268 }
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002269 if (bmval0 & FATTR4_WORD0_FS_LOCATIONS) {
2270 status = nfsd4_encode_fs_locations(rqstp, exp, &p, &buflen);
2271 if (status == nfserr_resource)
2272 goto out_resource;
2273 if (status)
2274 goto out;
2275 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 if (bmval0 & FATTR4_WORD0_HOMOGENEOUS) {
2277 if ((buflen -= 4) < 0)
2278 goto out_resource;
2279 WRITE32(1);
2280 }
2281 if (bmval0 & FATTR4_WORD0_MAXFILESIZE) {
2282 if ((buflen -= 8) < 0)
2283 goto out_resource;
2284 WRITE64(~(u64)0);
2285 }
2286 if (bmval0 & FATTR4_WORD0_MAXLINK) {
2287 if ((buflen -= 4) < 0)
2288 goto out_resource;
2289 WRITE32(255);
2290 }
2291 if (bmval0 & FATTR4_WORD0_MAXNAME) {
2292 if ((buflen -= 4) < 0)
2293 goto out_resource;
J. Bruce Fieldsa16e92e2007-09-28 16:45:51 -04002294 WRITE32(statfs.f_namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 }
2296 if (bmval0 & FATTR4_WORD0_MAXREAD) {
2297 if ((buflen -= 8) < 0)
2298 goto out_resource;
Greg Banks7adae482006-10-04 02:15:47 -07002299 WRITE64((u64) svc_max_payload(rqstp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 }
2301 if (bmval0 & FATTR4_WORD0_MAXWRITE) {
2302 if ((buflen -= 8) < 0)
2303 goto out_resource;
Greg Banks7adae482006-10-04 02:15:47 -07002304 WRITE64((u64) svc_max_payload(rqstp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 }
2306 if (bmval1 & FATTR4_WORD1_MODE) {
2307 if ((buflen -= 4) < 0)
2308 goto out_resource;
2309 WRITE32(stat.mode & S_IALLUGO);
2310 }
2311 if (bmval1 & FATTR4_WORD1_NO_TRUNC) {
2312 if ((buflen -= 4) < 0)
2313 goto out_resource;
2314 WRITE32(1);
2315 }
2316 if (bmval1 & FATTR4_WORD1_NUMLINKS) {
2317 if ((buflen -= 4) < 0)
2318 goto out_resource;
2319 WRITE32(stat.nlink);
2320 }
2321 if (bmval1 & FATTR4_WORD1_OWNER) {
2322 status = nfsd4_encode_user(rqstp, stat.uid, &p, &buflen);
2323 if (status == nfserr_resource)
2324 goto out_resource;
2325 if (status)
2326 goto out;
2327 }
2328 if (bmval1 & FATTR4_WORD1_OWNER_GROUP) {
2329 status = nfsd4_encode_group(rqstp, stat.gid, &p, &buflen);
2330 if (status == nfserr_resource)
2331 goto out_resource;
2332 if (status)
2333 goto out;
2334 }
2335 if (bmval1 & FATTR4_WORD1_RAWDEV) {
2336 if ((buflen -= 8) < 0)
2337 goto out_resource;
2338 WRITE32((u32) MAJOR(stat.rdev));
2339 WRITE32((u32) MINOR(stat.rdev));
2340 }
2341 if (bmval1 & FATTR4_WORD1_SPACE_AVAIL) {
2342 if ((buflen -= 8) < 0)
2343 goto out_resource;
2344 dummy64 = (u64)statfs.f_bavail * (u64)statfs.f_bsize;
2345 WRITE64(dummy64);
2346 }
2347 if (bmval1 & FATTR4_WORD1_SPACE_FREE) {
2348 if ((buflen -= 8) < 0)
2349 goto out_resource;
2350 dummy64 = (u64)statfs.f_bfree * (u64)statfs.f_bsize;
2351 WRITE64(dummy64);
2352 }
2353 if (bmval1 & FATTR4_WORD1_SPACE_TOTAL) {
2354 if ((buflen -= 8) < 0)
2355 goto out_resource;
2356 dummy64 = (u64)statfs.f_blocks * (u64)statfs.f_bsize;
2357 WRITE64(dummy64);
2358 }
2359 if (bmval1 & FATTR4_WORD1_SPACE_USED) {
2360 if ((buflen -= 8) < 0)
2361 goto out_resource;
2362 dummy64 = (u64)stat.blocks << 9;
2363 WRITE64(dummy64);
2364 }
2365 if (bmval1 & FATTR4_WORD1_TIME_ACCESS) {
2366 if ((buflen -= 12) < 0)
2367 goto out_resource;
Bryan Schumaker2111a772013-04-19 16:09:38 -04002368 WRITE64((s64)stat.atime.tv_sec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 WRITE32(stat.atime.tv_nsec);
2370 }
2371 if (bmval1 & FATTR4_WORD1_TIME_DELTA) {
2372 if ((buflen -= 12) < 0)
2373 goto out_resource;
2374 WRITE32(0);
2375 WRITE32(1);
2376 WRITE32(0);
2377 }
2378 if (bmval1 & FATTR4_WORD1_TIME_METADATA) {
2379 if ((buflen -= 12) < 0)
2380 goto out_resource;
Bryan Schumaker2111a772013-04-19 16:09:38 -04002381 WRITE64((s64)stat.ctime.tv_sec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 WRITE32(stat.ctime.tv_nsec);
2383 }
2384 if (bmval1 & FATTR4_WORD1_TIME_MODIFY) {
2385 if ((buflen -= 12) < 0)
2386 goto out_resource;
Bryan Schumaker2111a772013-04-19 16:09:38 -04002387 WRITE64((s64)stat.mtime.tv_sec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 WRITE32(stat.mtime.tv_nsec);
2389 }
2390 if (bmval1 & FATTR4_WORD1_MOUNTED_ON_FILEID) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 if ((buflen -= 8) < 0)
2392 goto out_resource;
Frank Filz406a7ea2007-11-27 11:34:05 -08002393 /*
2394 * Get parent's attributes if not ignoring crossmount
2395 * and this is the root of a cross-mounted filesystem.
2396 */
2397 if (ignore_crossmnt == 0 &&
Al Viro462d6052010-01-30 16:11:21 -05002398 dentry == exp->ex_path.mnt->mnt_root) {
2399 struct path path = exp->ex_path;
2400 path_get(&path);
2401 while (follow_up(&path)) {
2402 if (path.dentry != path.mnt->mnt_root)
2403 break;
2404 }
2405 err = vfs_getattr(path.mnt, path.dentry, &stat);
2406 path_put(&path);
Peter Staubach40ee5dc2007-08-16 12:10:07 -04002407 if (err)
2408 goto out_nfserr;
2409 }
2410 WRITE64(stat.ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 }
Benny Halevy8c18f202009-04-03 08:29:14 +03002412 if (bmval2 & FATTR4_WORD2_SUPPATTR_EXCLCREAT) {
J. Bruce Fields9acc5312014-01-28 16:05:15 -05002413 if ((buflen -= 16) < 0)
2414 goto out_resource;
Benny Halevy8c18f202009-04-03 08:29:14 +03002415 WRITE32(3);
2416 WRITE32(NFSD_SUPPATTR_EXCLCREAT_WORD0);
2417 WRITE32(NFSD_SUPPATTR_EXCLCREAT_WORD1);
2418 WRITE32(NFSD_SUPPATTR_EXCLCREAT_WORD2);
2419 }
Andy Adamson7e705702009-04-03 08:29:11 +03002420
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 *attrlenp = htonl((char *)p - (char *)attrlenp - 4);
2422 *countp = p - buffer;
2423 status = nfs_ok;
2424
2425out:
J. Bruce Fields28e05dd2007-02-16 01:28:30 -08002426 kfree(acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 if (fhp == &tempfh)
2428 fh_put(&tempfh);
2429 return status;
2430out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07002431 status = nfserrno(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 goto out;
2433out_resource:
2434 *countp = 0;
2435 status = nfserr_resource;
2436 goto out;
2437out_serverfault:
2438 status = nfserr_serverfault;
2439 goto out;
2440}
2441
J. Bruce Fieldsc0ce6ec2008-02-11 15:48:47 -05002442static inline int attributes_need_mount(u32 *bmval)
2443{
2444 if (bmval[0] & ~(FATTR4_WORD0_RDATTR_ERROR | FATTR4_WORD0_LEASE_TIME))
2445 return 1;
2446 if (bmval[1] & ~FATTR4_WORD1_MOUNTED_ON_FILEID)
2447 return 1;
2448 return 0;
2449}
2450
Al Virob37ad282006-10-19 23:28:59 -07002451static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452nfsd4_encode_dirent_fattr(struct nfsd4_readdir *cd,
Al Viro2ebbc012006-10-19 23:28:58 -07002453 const char *name, int namlen, __be32 *p, int *buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454{
2455 struct svc_export *exp = cd->rd_fhp->fh_export;
2456 struct dentry *dentry;
Al Virob37ad282006-10-19 23:28:59 -07002457 __be32 nfserr;
Frank Filz406a7ea2007-11-27 11:34:05 -08002458 int ignore_crossmnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459
2460 dentry = lookup_one_len(name, cd->rd_fhp->fh_dentry, namlen);
2461 if (IS_ERR(dentry))
2462 return nfserrno(PTR_ERR(dentry));
J. Bruce Fieldsb2c0cea2009-05-05 19:04:29 -04002463 if (!dentry->d_inode) {
2464 /*
2465 * nfsd_buffered_readdir drops the i_mutex between
2466 * readdir and calling this callback, leaving a window
2467 * where this directory entry could have gone away.
2468 */
2469 dput(dentry);
2470 return nfserr_noent;
2471 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472
2473 exp_get(exp);
Frank Filz406a7ea2007-11-27 11:34:05 -08002474 /*
2475 * In the case of a mountpoint, the client may be asking for
2476 * attributes that are only properties of the underlying filesystem
2477 * as opposed to the cross-mounted file system. In such a case,
2478 * we will not follow the cross mount and will fill the attribtutes
2479 * directly from the mountpoint dentry.
2480 */
J. Bruce Fields3227fa42009-10-25 21:43:01 -04002481 if (nfsd_mountpoint(dentry, exp)) {
J.Bruce Fields021d3a72006-12-13 00:35:24 -08002482 int err;
2483
J. Bruce Fields3227fa42009-10-25 21:43:01 -04002484 if (!(exp->ex_flags & NFSEXP_V4ROOT)
2485 && !attributes_need_mount(cd->rd_bmval)) {
2486 ignore_crossmnt = 1;
2487 goto out_encode;
2488 }
Andy Adamsondcb488a2007-07-17 04:04:51 -07002489 /*
2490 * Why the heck aren't we just using nfsd_lookup??
2491 * Different "."/".." handling? Something else?
2492 * At least, add a comment here to explain....
2493 */
J.Bruce Fields021d3a72006-12-13 00:35:24 -08002494 err = nfsd_cross_mnt(cd->rd_rqstp, &dentry, &exp);
2495 if (err) {
2496 nfserr = nfserrno(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 goto out_put;
2498 }
Andy Adamsondcb488a2007-07-17 04:04:51 -07002499 nfserr = check_nfsd_access(exp, cd->rd_rqstp);
2500 if (nfserr)
2501 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502
2503 }
J. Bruce Fields3227fa42009-10-25 21:43:01 -04002504out_encode:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 nfserr = nfsd4_encode_fattr(NULL, exp, dentry, p, buflen, cd->rd_bmval,
Frank Filz406a7ea2007-11-27 11:34:05 -08002506 cd->rd_rqstp, ignore_crossmnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507out_put:
2508 dput(dentry);
2509 exp_put(exp);
2510 return nfserr;
2511}
2512
Al Viro2ebbc012006-10-19 23:28:58 -07002513static __be32 *
Al Virob37ad282006-10-19 23:28:59 -07002514nfsd4_encode_rdattr_error(__be32 *p, int buflen, __be32 nfserr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515{
Al Viro2ebbc012006-10-19 23:28:58 -07002516 __be32 *attrlenp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517
2518 if (buflen < 6)
2519 return NULL;
2520 *p++ = htonl(2);
2521 *p++ = htonl(FATTR4_WORD0_RDATTR_ERROR); /* bmval0 */
2522 *p++ = htonl(0); /* bmval1 */
2523
2524 attrlenp = p++;
2525 *p++ = nfserr; /* no htonl */
2526 *attrlenp = htonl((char *)p - (char *)attrlenp - 4);
2527 return p;
2528}
2529
2530static int
NeilBrowna0ad13e2007-01-26 00:57:10 -08002531nfsd4_encode_dirent(void *ccdv, const char *name, int namlen,
2532 loff_t offset, u64 ino, unsigned int d_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533{
NeilBrowna0ad13e2007-01-26 00:57:10 -08002534 struct readdir_cd *ccd = ccdv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 struct nfsd4_readdir *cd = container_of(ccd, struct nfsd4_readdir, common);
2536 int buflen;
Al Viro2ebbc012006-10-19 23:28:58 -07002537 __be32 *p = cd->buffer;
J. Bruce Fieldsb2c0cea2009-05-05 19:04:29 -04002538 __be32 *cookiep;
Al Virob37ad282006-10-19 23:28:59 -07002539 __be32 nfserr = nfserr_toosmall;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540
2541 /* In nfsv4, "." and ".." never make it onto the wire.. */
2542 if (name && isdotent(name, namlen)) {
2543 cd->common.err = nfs_ok;
2544 return 0;
2545 }
2546
2547 if (cd->offset)
2548 xdr_encode_hyper(cd->offset, (u64) offset);
2549
2550 buflen = cd->buflen - 4 - XDR_QUADLEN(namlen);
2551 if (buflen < 0)
2552 goto fail;
2553
2554 *p++ = xdr_one; /* mark entry present */
J. Bruce Fieldsb2c0cea2009-05-05 19:04:29 -04002555 cookiep = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 p = xdr_encode_hyper(p, NFS_OFFSET_MAX); /* offset of next entry */
2557 p = xdr_encode_array(p, name, namlen); /* name length & name */
2558
2559 nfserr = nfsd4_encode_dirent_fattr(cd, name, namlen, p, &buflen);
2560 switch (nfserr) {
2561 case nfs_ok:
2562 p += buflen;
2563 break;
2564 case nfserr_resource:
2565 nfserr = nfserr_toosmall;
2566 goto fail;
J. Bruce Fieldsb2c0cea2009-05-05 19:04:29 -04002567 case nfserr_noent:
2568 goto skip_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 default:
2570 /*
2571 * If the client requested the RDATTR_ERROR attribute,
2572 * we stuff the error code into this attribute
2573 * and continue. If this attribute was not requested,
2574 * then in accordance with the spec, we fail the
2575 * entire READDIR operation(!)
2576 */
2577 if (!(cd->rd_bmval[0] & FATTR4_WORD0_RDATTR_ERROR))
2578 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 p = nfsd4_encode_rdattr_error(p, buflen, nfserr);
Fred Isaman34081ef2006-01-18 17:43:40 -08002580 if (p == NULL) {
2581 nfserr = nfserr_toosmall;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 goto fail;
Fred Isaman34081ef2006-01-18 17:43:40 -08002583 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 }
2585 cd->buflen -= (p - cd->buffer);
2586 cd->buffer = p;
J. Bruce Fieldsb2c0cea2009-05-05 19:04:29 -04002587 cd->offset = cookiep;
2588skip_entry:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 cd->common.err = nfs_ok;
2590 return 0;
2591fail:
2592 cd->common.err = nfserr;
2593 return -EINVAL;
2594}
2595
Benny Halevye2f282b2008-08-12 20:45:07 +03002596static void
2597nfsd4_encode_stateid(struct nfsd4_compoundres *resp, stateid_t *sid)
2598{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002599 __be32 *p;
Benny Halevye2f282b2008-08-12 20:45:07 +03002600
2601 RESERVE_SPACE(sizeof(stateid_t));
2602 WRITE32(sid->si_generation);
2603 WRITEMEM(&sid->si_opaque, sizeof(stateid_opaque_t));
2604 ADJUST_ARGS();
2605}
2606
Benny Halevy695e12f2008-07-04 14:38:33 +03002607static __be32
Al Virob37ad282006-10-19 23:28:59 -07002608nfsd4_encode_access(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_access *access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002610 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611
2612 if (!nfserr) {
2613 RESERVE_SPACE(8);
2614 WRITE32(access->ac_supported);
2615 WRITE32(access->ac_resp_access);
2616 ADJUST_ARGS();
2617 }
Benny Halevy695e12f2008-07-04 14:38:33 +03002618 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619}
2620
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002621static __be32 nfsd4_encode_bind_conn_to_session(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_bind_conn_to_session *bcts)
2622{
2623 __be32 *p;
2624
2625 if (!nfserr) {
2626 RESERVE_SPACE(NFS4_MAX_SESSIONID_LEN + 8);
2627 WRITEMEM(bcts->sessionid.data, NFS4_MAX_SESSIONID_LEN);
2628 WRITE32(bcts->dir);
2629 /* XXX: ? */
2630 WRITE32(0);
2631 ADJUST_ARGS();
2632 }
2633 return nfserr;
2634}
2635
Benny Halevy695e12f2008-07-04 14:38:33 +03002636static __be32
Al Virob37ad282006-10-19 23:28:59 -07002637nfsd4_encode_close(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638{
2639 ENCODE_SEQID_OP_HEAD;
2640
Benny Halevye2f282b2008-08-12 20:45:07 +03002641 if (!nfserr)
2642 nfsd4_encode_stateid(resp, &close->cl_stateid);
2643
J. Bruce Fieldsf3e42232011-08-24 12:27:31 -04002644 encode_seqid_op_tail(resp, save, nfserr);
Benny Halevy695e12f2008-07-04 14:38:33 +03002645 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646}
2647
2648
Benny Halevy695e12f2008-07-04 14:38:33 +03002649static __be32
Al Virob37ad282006-10-19 23:28:59 -07002650nfsd4_encode_commit(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_commit *commit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002652 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653
2654 if (!nfserr) {
Chuck Leverab4684d2012-03-02 17:13:50 -05002655 RESERVE_SPACE(NFS4_VERIFIER_SIZE);
2656 WRITEMEM(commit->co_verf.data, NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 ADJUST_ARGS();
2658 }
Benny Halevy695e12f2008-07-04 14:38:33 +03002659 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660}
2661
Benny Halevy695e12f2008-07-04 14:38:33 +03002662static __be32
Al Virob37ad282006-10-19 23:28:59 -07002663nfsd4_encode_create(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_create *create)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002665 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666
2667 if (!nfserr) {
2668 RESERVE_SPACE(32);
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002669 write_cinfo(&p, &create->cr_cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 WRITE32(2);
2671 WRITE32(create->cr_bmval[0]);
2672 WRITE32(create->cr_bmval[1]);
2673 ADJUST_ARGS();
2674 }
Benny Halevy695e12f2008-07-04 14:38:33 +03002675 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676}
2677
Al Virob37ad282006-10-19 23:28:59 -07002678static __be32
2679nfsd4_encode_getattr(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_getattr *getattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680{
2681 struct svc_fh *fhp = getattr->ga_fhp;
2682 int buflen;
2683
2684 if (nfserr)
2685 return nfserr;
2686
2687 buflen = resp->end - resp->p - (COMPOUND_ERR_SLACK_SPACE >> 2);
2688 nfserr = nfsd4_encode_fattr(fhp, fhp->fh_export, fhp->fh_dentry,
2689 resp->p, &buflen, getattr->ga_bmval,
Frank Filz406a7ea2007-11-27 11:34:05 -08002690 resp->rqstp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 if (!nfserr)
2692 resp->p += buflen;
2693 return nfserr;
2694}
2695
Benny Halevy695e12f2008-07-04 14:38:33 +03002696static __be32
2697nfsd4_encode_getfh(struct nfsd4_compoundres *resp, __be32 nfserr, struct svc_fh **fhpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698{
Benny Halevy695e12f2008-07-04 14:38:33 +03002699 struct svc_fh *fhp = *fhpp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 unsigned int len;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002701 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702
2703 if (!nfserr) {
2704 len = fhp->fh_handle.fh_size;
2705 RESERVE_SPACE(len + 4);
2706 WRITE32(len);
2707 WRITEMEM(&fhp->fh_handle.fh_base, len);
2708 ADJUST_ARGS();
2709 }
Benny Halevy695e12f2008-07-04 14:38:33 +03002710 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711}
2712
2713/*
2714* Including all fields other than the name, a LOCK4denied structure requires
2715* 8(clientid) + 4(namelen) + 8(offset) + 8(length) + 4(type) = 32 bytes.
2716*/
2717static void
2718nfsd4_encode_lock_denied(struct nfsd4_compoundres *resp, struct nfsd4_lock_denied *ld)
2719{
J. Bruce Fields7c13f342011-08-30 22:15:47 -04002720 struct xdr_netobj *conf = &ld->ld_owner;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002721 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722
J. Bruce Fields7c13f342011-08-30 22:15:47 -04002723 RESERVE_SPACE(32 + XDR_LEN(conf->len));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 WRITE64(ld->ld_start);
2725 WRITE64(ld->ld_length);
2726 WRITE32(ld->ld_type);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04002727 if (conf->len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 WRITEMEM(&ld->ld_clientid, 8);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04002729 WRITE32(conf->len);
2730 WRITEMEM(conf->data, conf->len);
2731 kfree(conf->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 } else { /* non - nfsv4 lock in conflict, no clientid nor owner */
2733 WRITE64((u64)0); /* clientid */
2734 WRITE32(0); /* length of owner name */
2735 }
2736 ADJUST_ARGS();
2737}
2738
Benny Halevy695e12f2008-07-04 14:38:33 +03002739static __be32
Al Virob37ad282006-10-19 23:28:59 -07002740nfsd4_encode_lock(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 ENCODE_SEQID_OP_HEAD;
2743
Benny Halevye2f282b2008-08-12 20:45:07 +03002744 if (!nfserr)
2745 nfsd4_encode_stateid(resp, &lock->lk_resp_stateid);
2746 else if (nfserr == nfserr_denied)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 nfsd4_encode_lock_denied(resp, &lock->lk_denied);
2748
J. Bruce Fieldsf3e42232011-08-24 12:27:31 -04002749 encode_seqid_op_tail(resp, save, nfserr);
Benny Halevy695e12f2008-07-04 14:38:33 +03002750 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751}
2752
Benny Halevy695e12f2008-07-04 14:38:33 +03002753static __be32
Al Virob37ad282006-10-19 23:28:59 -07002754nfsd4_encode_lockt(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755{
2756 if (nfserr == nfserr_denied)
2757 nfsd4_encode_lock_denied(resp, &lockt->lt_denied);
Benny Halevy695e12f2008-07-04 14:38:33 +03002758 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759}
2760
Benny Halevy695e12f2008-07-04 14:38:33 +03002761static __be32
Al Virob37ad282006-10-19 23:28:59 -07002762nfsd4_encode_locku(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763{
2764 ENCODE_SEQID_OP_HEAD;
2765
Benny Halevye2f282b2008-08-12 20:45:07 +03002766 if (!nfserr)
2767 nfsd4_encode_stateid(resp, &locku->lu_stateid);
2768
J. Bruce Fieldsf3e42232011-08-24 12:27:31 -04002769 encode_seqid_op_tail(resp, save, nfserr);
Benny Halevy695e12f2008-07-04 14:38:33 +03002770 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771}
2772
2773
Benny Halevy695e12f2008-07-04 14:38:33 +03002774static __be32
Al Virob37ad282006-10-19 23:28:59 -07002775nfsd4_encode_link(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_link *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002777 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778
2779 if (!nfserr) {
2780 RESERVE_SPACE(20);
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002781 write_cinfo(&p, &link->li_cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 ADJUST_ARGS();
2783 }
Benny Halevy695e12f2008-07-04 14:38:33 +03002784 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785}
2786
2787
Benny Halevy695e12f2008-07-04 14:38:33 +03002788static __be32
Al Virob37ad282006-10-19 23:28:59 -07002789nfsd4_encode_open(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002791 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 ENCODE_SEQID_OP_HEAD;
2793
2794 if (nfserr)
2795 goto out;
2796
Benny Halevye2f282b2008-08-12 20:45:07 +03002797 nfsd4_encode_stateid(resp, &open->op_stateid);
2798 RESERVE_SPACE(40);
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002799 write_cinfo(&p, &open->op_cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 WRITE32(open->op_rflags);
2801 WRITE32(2);
2802 WRITE32(open->op_bmval[0]);
2803 WRITE32(open->op_bmval[1]);
2804 WRITE32(open->op_delegate_type);
2805 ADJUST_ARGS();
2806
2807 switch (open->op_delegate_type) {
2808 case NFS4_OPEN_DELEGATE_NONE:
2809 break;
2810 case NFS4_OPEN_DELEGATE_READ:
Benny Halevye2f282b2008-08-12 20:45:07 +03002811 nfsd4_encode_stateid(resp, &open->op_delegate_stateid);
2812 RESERVE_SPACE(20);
NeilBrown7b190fe2005-06-23 22:03:23 -07002813 WRITE32(open->op_recall);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814
2815 /*
2816 * TODO: ACE's in delegations
2817 */
2818 WRITE32(NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
2819 WRITE32(0);
2820 WRITE32(0);
2821 WRITE32(0); /* XXX: is NULL principal ok? */
2822 ADJUST_ARGS();
2823 break;
2824 case NFS4_OPEN_DELEGATE_WRITE:
Benny Halevye2f282b2008-08-12 20:45:07 +03002825 nfsd4_encode_stateid(resp, &open->op_delegate_stateid);
2826 RESERVE_SPACE(32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 WRITE32(0);
2828
2829 /*
2830 * TODO: space_limit's in delegations
2831 */
2832 WRITE32(NFS4_LIMIT_SIZE);
2833 WRITE32(~(u32)0);
2834 WRITE32(~(u32)0);
2835
2836 /*
2837 * TODO: ACE's in delegations
2838 */
2839 WRITE32(NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
2840 WRITE32(0);
2841 WRITE32(0);
2842 WRITE32(0); /* XXX: is NULL principal ok? */
2843 ADJUST_ARGS();
2844 break;
Benny Halevyd24433c2012-02-16 20:57:17 +02002845 case NFS4_OPEN_DELEGATE_NONE_EXT: /* 4.1 */
2846 switch (open->op_why_no_deleg) {
2847 case WND4_CONTENTION:
2848 case WND4_RESOURCE:
2849 RESERVE_SPACE(8);
2850 WRITE32(open->op_why_no_deleg);
2851 WRITE32(0); /* deleg signaling not supported yet */
2852 break;
2853 default:
2854 RESERVE_SPACE(4);
2855 WRITE32(open->op_why_no_deleg);
2856 }
2857 ADJUST_ARGS();
2858 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 default:
2860 BUG();
2861 }
2862 /* XXX save filehandle here */
2863out:
J. Bruce Fieldsf3e42232011-08-24 12:27:31 -04002864 encode_seqid_op_tail(resp, save, nfserr);
Benny Halevy695e12f2008-07-04 14:38:33 +03002865 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866}
2867
Benny Halevy695e12f2008-07-04 14:38:33 +03002868static __be32
Al Virob37ad282006-10-19 23:28:59 -07002869nfsd4_encode_open_confirm(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870{
2871 ENCODE_SEQID_OP_HEAD;
Benny Halevye2f282b2008-08-12 20:45:07 +03002872
2873 if (!nfserr)
2874 nfsd4_encode_stateid(resp, &oc->oc_resp_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875
J. Bruce Fieldsf3e42232011-08-24 12:27:31 -04002876 encode_seqid_op_tail(resp, save, nfserr);
Benny Halevy695e12f2008-07-04 14:38:33 +03002877 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878}
2879
Benny Halevy695e12f2008-07-04 14:38:33 +03002880static __be32
Al Virob37ad282006-10-19 23:28:59 -07002881nfsd4_encode_open_downgrade(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882{
2883 ENCODE_SEQID_OP_HEAD;
Benny Halevye2f282b2008-08-12 20:45:07 +03002884
2885 if (!nfserr)
2886 nfsd4_encode_stateid(resp, &od->od_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887
J. Bruce Fieldsf3e42232011-08-24 12:27:31 -04002888 encode_seqid_op_tail(resp, save, nfserr);
Benny Halevy695e12f2008-07-04 14:38:33 +03002889 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890}
2891
Al Virob37ad282006-10-19 23:28:59 -07002892static __be32
2893nfsd4_encode_read(struct nfsd4_compoundres *resp, __be32 nfserr,
NeilBrown44524352006-10-04 02:15:46 -07002894 struct nfsd4_read *read)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895{
2896 u32 eof;
2897 int v, pn;
2898 unsigned long maxcount;
2899 long len;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002900 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901
2902 if (nfserr)
2903 return nfserr;
2904 if (resp->xbuf->page_len)
2905 return nfserr_resource;
2906
2907 RESERVE_SPACE(8); /* eof flag and byte count */
2908
Greg Banks7adae482006-10-04 02:15:47 -07002909 maxcount = svc_max_payload(resp->rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 if (maxcount > read->rd_length)
2911 maxcount = read->rd_length;
2912
2913 len = maxcount;
2914 v = 0;
2915 while (len > 0) {
J. Bruce Fields4cbb3d02012-12-04 18:25:10 -05002916 pn = resp->rqstp->rq_resused;
2917 if (!resp->rqstp->rq_respages[pn]) { /* ran out of pages */
2918 maxcount -= len;
2919 break;
2920 }
NeilBrown3cc03b12006-10-04 02:15:47 -07002921 resp->rqstp->rq_vec[v].iov_base =
NeilBrown44524352006-10-04 02:15:46 -07002922 page_address(resp->rqstp->rq_respages[pn]);
NeilBrown3cc03b12006-10-04 02:15:47 -07002923 resp->rqstp->rq_vec[v].iov_len =
NeilBrown44524352006-10-04 02:15:46 -07002924 len < PAGE_SIZE ? len : PAGE_SIZE;
J. Bruce Fields4cbb3d02012-12-04 18:25:10 -05002925 resp->rqstp->rq_resused++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 v++;
2927 len -= PAGE_SIZE;
2928 }
2929 read->rd_vlen = v;
2930
J. Bruce Fields039a87c2010-07-30 11:33:32 -04002931 nfserr = nfsd_read_file(read->rd_rqstp, read->rd_fhp, read->rd_filp,
NeilBrown3cc03b12006-10-04 02:15:47 -07002932 read->rd_offset, resp->rqstp->rq_vec, read->rd_vlen,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 &maxcount);
2934
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 if (nfserr)
2936 return nfserr;
NeilBrown44524352006-10-04 02:15:46 -07002937 eof = (read->rd_offset + maxcount >=
2938 read->rd_fhp->fh_dentry->d_inode->i_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939
2940 WRITE32(eof);
2941 WRITE32(maxcount);
2942 ADJUST_ARGS();
NeilBrown6ed6dec2006-04-10 22:55:32 -07002943 resp->xbuf->head[0].iov_len = (char*)p
2944 - (char*)resp->xbuf->head[0].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 resp->xbuf->page_len = maxcount;
2946
NeilBrown6ed6dec2006-04-10 22:55:32 -07002947 /* Use rest of head for padding and remaining ops: */
NeilBrown6ed6dec2006-04-10 22:55:32 -07002948 resp->xbuf->tail[0].iov_base = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 resp->xbuf->tail[0].iov_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 if (maxcount&3) {
NeilBrown6ed6dec2006-04-10 22:55:32 -07002951 RESERVE_SPACE(4);
2952 WRITE32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 resp->xbuf->tail[0].iov_base += maxcount&3;
2954 resp->xbuf->tail[0].iov_len = 4 - (maxcount&3);
NeilBrown6ed6dec2006-04-10 22:55:32 -07002955 ADJUST_ARGS();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 }
2957 return 0;
2958}
2959
Al Virob37ad282006-10-19 23:28:59 -07002960static __be32
2961nfsd4_encode_readlink(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_readlink *readlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962{
2963 int maxcount;
2964 char *page;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002965 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966
2967 if (nfserr)
2968 return nfserr;
2969 if (resp->xbuf->page_len)
2970 return nfserr_resource;
J. Bruce Fields4cbb3d02012-12-04 18:25:10 -05002971 if (!resp->rqstp->rq_respages[resp->rqstp->rq_resused])
2972 return nfserr_resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973
NeilBrown44524352006-10-04 02:15:46 -07002974 page = page_address(resp->rqstp->rq_respages[resp->rqstp->rq_resused++]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975
2976 maxcount = PAGE_SIZE;
2977 RESERVE_SPACE(4);
2978
2979 /*
2980 * XXX: By default, the ->readlink() VFS op will truncate symlinks
2981 * if they would overflow the buffer. Is this kosher in NFSv4? If
2982 * not, one easy fix is: if ->readlink() precisely fills the buffer,
2983 * assume that truncation occurred, and return NFS4ERR_RESOURCE.
2984 */
2985 nfserr = nfsd_readlink(readlink->rl_rqstp, readlink->rl_fhp, page, &maxcount);
2986 if (nfserr == nfserr_isdir)
2987 return nfserr_inval;
2988 if (nfserr)
2989 return nfserr;
2990
2991 WRITE32(maxcount);
2992 ADJUST_ARGS();
NeilBrown6ed6dec2006-04-10 22:55:32 -07002993 resp->xbuf->head[0].iov_len = (char*)p
2994 - (char*)resp->xbuf->head[0].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 resp->xbuf->page_len = maxcount;
NeilBrown6ed6dec2006-04-10 22:55:32 -07002996
2997 /* Use rest of head for padding and remaining ops: */
NeilBrown6ed6dec2006-04-10 22:55:32 -07002998 resp->xbuf->tail[0].iov_base = p;
2999 resp->xbuf->tail[0].iov_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 if (maxcount&3) {
NeilBrown6ed6dec2006-04-10 22:55:32 -07003001 RESERVE_SPACE(4);
3002 WRITE32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 resp->xbuf->tail[0].iov_base += maxcount&3;
3004 resp->xbuf->tail[0].iov_len = 4 - (maxcount&3);
NeilBrown6ed6dec2006-04-10 22:55:32 -07003005 ADJUST_ARGS();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 }
3007 return 0;
3008}
3009
Al Virob37ad282006-10-19 23:28:59 -07003010static __be32
3011nfsd4_encode_readdir(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_readdir *readdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012{
3013 int maxcount;
3014 loff_t offset;
Al Viro2ebbc012006-10-19 23:28:58 -07003015 __be32 *page, *savep, *tailbase;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003016 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017
3018 if (nfserr)
3019 return nfserr;
3020 if (resp->xbuf->page_len)
3021 return nfserr_resource;
J. Bruce Fields4cbb3d02012-12-04 18:25:10 -05003022 if (!resp->rqstp->rq_respages[resp->rqstp->rq_resused])
3023 return nfserr_resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024
Chuck Leverab4684d2012-03-02 17:13:50 -05003025 RESERVE_SPACE(NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 savep = p;
3027
3028 /* XXX: Following NFSv3, we ignore the READDIR verifier for now. */
3029 WRITE32(0);
3030 WRITE32(0);
3031 ADJUST_ARGS();
3032 resp->xbuf->head[0].iov_len = ((char*)resp->p) - (char*)resp->xbuf->head[0].iov_base;
NeilBrownbb6e8a92006-04-10 22:55:33 -07003033 tailbase = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034
3035 maxcount = PAGE_SIZE;
3036 if (maxcount > readdir->rd_maxcount)
3037 maxcount = readdir->rd_maxcount;
3038
3039 /*
3040 * Convert from bytes to words, account for the two words already
3041 * written, make sure to leave two words at the end for the next
3042 * pointer and eof field.
3043 */
3044 maxcount = (maxcount >> 2) - 4;
3045 if (maxcount < 0) {
3046 nfserr = nfserr_toosmall;
3047 goto err_no_verf;
3048 }
3049
NeilBrown44524352006-10-04 02:15:46 -07003050 page = page_address(resp->rqstp->rq_respages[resp->rqstp->rq_resused++]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 readdir->common.err = 0;
3052 readdir->buflen = maxcount;
3053 readdir->buffer = page;
3054 readdir->offset = NULL;
3055
3056 offset = readdir->rd_cookie;
3057 nfserr = nfsd_readdir(readdir->rd_rqstp, readdir->rd_fhp,
3058 &offset,
3059 &readdir->common, nfsd4_encode_dirent);
3060 if (nfserr == nfs_ok &&
3061 readdir->common.err == nfserr_toosmall &&
3062 readdir->buffer == page)
3063 nfserr = nfserr_toosmall;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 if (nfserr)
3065 goto err_no_verf;
3066
3067 if (readdir->offset)
3068 xdr_encode_hyper(readdir->offset, offset);
3069
3070 p = readdir->buffer;
3071 *p++ = 0; /* no more entries */
3072 *p++ = htonl(readdir->common.err == nfserr_eof);
NeilBrown44524352006-10-04 02:15:46 -07003073 resp->xbuf->page_len = ((char*)p) - (char*)page_address(
3074 resp->rqstp->rq_respages[resp->rqstp->rq_resused-1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075
NeilBrownbb6e8a92006-04-10 22:55:33 -07003076 /* Use rest of head for padding and remaining ops: */
NeilBrownbb6e8a92006-04-10 22:55:33 -07003077 resp->xbuf->tail[0].iov_base = tailbase;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 resp->xbuf->tail[0].iov_len = 0;
3079 resp->p = resp->xbuf->tail[0].iov_base;
NeilBrownbb6e8a92006-04-10 22:55:33 -07003080 resp->end = resp->p + (PAGE_SIZE - resp->xbuf->head[0].iov_len)/4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081
3082 return 0;
3083err_no_verf:
3084 p = savep;
3085 ADJUST_ARGS();
3086 return nfserr;
3087}
3088
Benny Halevy695e12f2008-07-04 14:38:33 +03003089static __be32
Al Virob37ad282006-10-19 23:28:59 -07003090nfsd4_encode_remove(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_remove *remove)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003092 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093
3094 if (!nfserr) {
3095 RESERVE_SPACE(20);
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04003096 write_cinfo(&p, &remove->rm_cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 ADJUST_ARGS();
3098 }
Benny Halevy695e12f2008-07-04 14:38:33 +03003099 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100}
3101
Benny Halevy695e12f2008-07-04 14:38:33 +03003102static __be32
Al Virob37ad282006-10-19 23:28:59 -07003103nfsd4_encode_rename(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_rename *rename)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003105 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106
3107 if (!nfserr) {
3108 RESERVE_SPACE(40);
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04003109 write_cinfo(&p, &rename->rn_sinfo);
3110 write_cinfo(&p, &rename->rn_tinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111 ADJUST_ARGS();
3112 }
Benny Halevy695e12f2008-07-04 14:38:33 +03003113 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114}
3115
Benny Halevy695e12f2008-07-04 14:38:33 +03003116static __be32
Mi Jinlong22b6dee2010-12-27 14:29:57 +08003117nfsd4_do_encode_secinfo(struct nfsd4_compoundres *resp,
3118 __be32 nfserr,struct svc_export *exp)
Andy Adamsondcb488a2007-07-17 04:04:51 -07003119{
3120 int i = 0;
J. Bruce Fields4796f452007-07-17 04:04:51 -07003121 u32 nflavs;
3122 struct exp_flavor_info *flavs;
3123 struct exp_flavor_info def_flavs[2];
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003124 __be32 *p;
Andy Adamsondcb488a2007-07-17 04:04:51 -07003125
3126 if (nfserr)
3127 goto out;
J. Bruce Fields4796f452007-07-17 04:04:51 -07003128 if (exp->ex_nflavors) {
3129 flavs = exp->ex_flavors;
3130 nflavs = exp->ex_nflavors;
3131 } else { /* Handling of some defaults in absence of real secinfo: */
3132 flavs = def_flavs;
3133 if (exp->ex_client->flavour->flavour == RPC_AUTH_UNIX) {
3134 nflavs = 2;
3135 flavs[0].pseudoflavor = RPC_AUTH_UNIX;
3136 flavs[1].pseudoflavor = RPC_AUTH_NULL;
3137 } else if (exp->ex_client->flavour->flavour == RPC_AUTH_GSS) {
3138 nflavs = 1;
3139 flavs[0].pseudoflavor
3140 = svcauth_gss_flavor(exp->ex_client);
3141 } else {
3142 nflavs = 1;
3143 flavs[0].pseudoflavor
3144 = exp->ex_client->flavour->flavour;
3145 }
3146 }
3147
Andy Adamsondcb488a2007-07-17 04:04:51 -07003148 RESERVE_SPACE(4);
J. Bruce Fields4796f452007-07-17 04:04:51 -07003149 WRITE32(nflavs);
Andy Adamsondcb488a2007-07-17 04:04:51 -07003150 ADJUST_ARGS();
J. Bruce Fields4796f452007-07-17 04:04:51 -07003151 for (i = 0; i < nflavs; i++) {
3152 u32 flav = flavs[i].pseudoflavor;
Andy Adamsondcb488a2007-07-17 04:04:51 -07003153 struct gss_api_mech *gm = gss_mech_get_by_pseudoflavor(flav);
3154
3155 if (gm) {
3156 RESERVE_SPACE(4);
3157 WRITE32(RPC_AUTH_GSS);
3158 ADJUST_ARGS();
3159 RESERVE_SPACE(4 + gm->gm_oid.len);
3160 WRITE32(gm->gm_oid.len);
3161 WRITEMEM(gm->gm_oid.data, gm->gm_oid.len);
3162 ADJUST_ARGS();
3163 RESERVE_SPACE(4);
3164 WRITE32(0); /* qop */
3165 ADJUST_ARGS();
3166 RESERVE_SPACE(4);
3167 WRITE32(gss_pseudoflavor_to_service(gm, flav));
3168 ADJUST_ARGS();
3169 gss_mech_put(gm);
3170 } else {
3171 RESERVE_SPACE(4);
3172 WRITE32(flav);
3173 ADJUST_ARGS();
3174 }
3175 }
3176out:
3177 if (exp)
3178 exp_put(exp);
Benny Halevy695e12f2008-07-04 14:38:33 +03003179 return nfserr;
Andy Adamsondcb488a2007-07-17 04:04:51 -07003180}
3181
Mi Jinlong22b6dee2010-12-27 14:29:57 +08003182static __be32
3183nfsd4_encode_secinfo(struct nfsd4_compoundres *resp, __be32 nfserr,
3184 struct nfsd4_secinfo *secinfo)
3185{
3186 return nfsd4_do_encode_secinfo(resp, nfserr, secinfo->si_exp);
3187}
3188
3189static __be32
3190nfsd4_encode_secinfo_no_name(struct nfsd4_compoundres *resp, __be32 nfserr,
3191 struct nfsd4_secinfo_no_name *secinfo)
3192{
3193 return nfsd4_do_encode_secinfo(resp, nfserr, secinfo->sin_exp);
3194}
3195
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196/*
3197 * The SETATTR encode routine is special -- it always encodes a bitmap,
3198 * regardless of the error status.
3199 */
Benny Halevy695e12f2008-07-04 14:38:33 +03003200static __be32
Al Virob37ad282006-10-19 23:28:59 -07003201nfsd4_encode_setattr(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_setattr *setattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003203 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204
3205 RESERVE_SPACE(12);
3206 if (nfserr) {
3207 WRITE32(2);
3208 WRITE32(0);
3209 WRITE32(0);
3210 }
3211 else {
3212 WRITE32(2);
3213 WRITE32(setattr->sa_bmval[0]);
3214 WRITE32(setattr->sa_bmval[1]);
3215 }
3216 ADJUST_ARGS();
Benny Halevy695e12f2008-07-04 14:38:33 +03003217 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218}
3219
Benny Halevy695e12f2008-07-04 14:38:33 +03003220static __be32
Al Virob37ad282006-10-19 23:28:59 -07003221nfsd4_encode_setclientid(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_setclientid *scd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003223 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224
3225 if (!nfserr) {
Chuck Leverab4684d2012-03-02 17:13:50 -05003226 RESERVE_SPACE(8 + NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227 WRITEMEM(&scd->se_clientid, 8);
Chuck Leverab4684d2012-03-02 17:13:50 -05003228 WRITEMEM(&scd->se_confirm, NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 ADJUST_ARGS();
3230 }
3231 else if (nfserr == nfserr_clid_inuse) {
3232 RESERVE_SPACE(8);
3233 WRITE32(0);
3234 WRITE32(0);
3235 ADJUST_ARGS();
3236 }
Benny Halevy695e12f2008-07-04 14:38:33 +03003237 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238}
3239
Benny Halevy695e12f2008-07-04 14:38:33 +03003240static __be32
Al Virob37ad282006-10-19 23:28:59 -07003241nfsd4_encode_write(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_write *write)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003243 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244
3245 if (!nfserr) {
3246 RESERVE_SPACE(16);
3247 WRITE32(write->wr_bytes_written);
3248 WRITE32(write->wr_how_written);
Chuck Leverab4684d2012-03-02 17:13:50 -05003249 WRITEMEM(write->wr_verifier.data, NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 ADJUST_ARGS();
3251 }
Benny Halevy695e12f2008-07-04 14:38:33 +03003252 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253}
3254
Benny Halevy695e12f2008-07-04 14:38:33 +03003255static __be32
Andy Adamson2db134e2009-04-03 08:27:55 +03003256nfsd4_encode_exchange_id(struct nfsd4_compoundres *resp, int nfserr,
3257 struct nfsd4_exchange_id *exid)
3258{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003259 __be32 *p;
Andy Adamson0733d212009-04-03 08:28:01 +03003260 char *major_id;
3261 char *server_scope;
3262 int major_id_sz;
3263 int server_scope_sz;
3264 uint64_t minor_id = 0;
3265
3266 if (nfserr)
3267 return nfserr;
3268
3269 major_id = utsname()->nodename;
3270 major_id_sz = strlen(major_id);
3271 server_scope = utsname()->nodename;
3272 server_scope_sz = strlen(server_scope);
3273
3274 RESERVE_SPACE(
3275 8 /* eir_clientid */ +
3276 4 /* eir_sequenceid */ +
3277 4 /* eir_flags */ +
3278 4 /* spr_how (SP4_NONE) */ +
3279 8 /* so_minor_id */ +
3280 4 /* so_major_id.len */ +
3281 (XDR_QUADLEN(major_id_sz) * 4) +
3282 4 /* eir_server_scope.len */ +
3283 (XDR_QUADLEN(server_scope_sz) * 4) +
3284 4 /* eir_server_impl_id.count (0) */);
3285
3286 WRITEMEM(&exid->clientid, 8);
3287 WRITE32(exid->seqid);
3288 WRITE32(exid->flags);
3289
3290 /* state_protect4_r. Currently only support SP4_NONE */
3291 BUG_ON(exid->spa_how != SP4_NONE);
3292 WRITE32(exid->spa_how);
3293
3294 /* The server_owner struct */
3295 WRITE64(minor_id); /* Minor id */
3296 /* major id */
3297 WRITE32(major_id_sz);
3298 WRITEMEM(major_id, major_id_sz);
3299
3300 /* Server scope */
3301 WRITE32(server_scope_sz);
3302 WRITEMEM(server_scope, server_scope_sz);
3303
3304 /* Implementation id */
3305 WRITE32(0); /* zero length nfs_impl_id4 array */
3306 ADJUST_ARGS();
3307 return 0;
Andy Adamson2db134e2009-04-03 08:27:55 +03003308}
3309
3310static __be32
3311nfsd4_encode_create_session(struct nfsd4_compoundres *resp, int nfserr,
3312 struct nfsd4_create_session *sess)
3313{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003314 __be32 *p;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003315
3316 if (nfserr)
3317 return nfserr;
3318
3319 RESERVE_SPACE(24);
3320 WRITEMEM(sess->sessionid.data, NFS4_MAX_SESSIONID_LEN);
3321 WRITE32(sess->seqid);
3322 WRITE32(sess->flags);
3323 ADJUST_ARGS();
3324
3325 RESERVE_SPACE(28);
3326 WRITE32(0); /* headerpadsz */
3327 WRITE32(sess->fore_channel.maxreq_sz);
3328 WRITE32(sess->fore_channel.maxresp_sz);
3329 WRITE32(sess->fore_channel.maxresp_cached);
3330 WRITE32(sess->fore_channel.maxops);
3331 WRITE32(sess->fore_channel.maxreqs);
3332 WRITE32(sess->fore_channel.nr_rdma_attrs);
3333 ADJUST_ARGS();
3334
3335 if (sess->fore_channel.nr_rdma_attrs) {
3336 RESERVE_SPACE(4);
3337 WRITE32(sess->fore_channel.rdma_attrs);
3338 ADJUST_ARGS();
3339 }
3340
3341 RESERVE_SPACE(28);
3342 WRITE32(0); /* headerpadsz */
3343 WRITE32(sess->back_channel.maxreq_sz);
3344 WRITE32(sess->back_channel.maxresp_sz);
3345 WRITE32(sess->back_channel.maxresp_cached);
3346 WRITE32(sess->back_channel.maxops);
3347 WRITE32(sess->back_channel.maxreqs);
3348 WRITE32(sess->back_channel.nr_rdma_attrs);
3349 ADJUST_ARGS();
3350
3351 if (sess->back_channel.nr_rdma_attrs) {
3352 RESERVE_SPACE(4);
3353 WRITE32(sess->back_channel.rdma_attrs);
3354 ADJUST_ARGS();
3355 }
3356 return 0;
Andy Adamson2db134e2009-04-03 08:27:55 +03003357}
3358
3359static __be32
3360nfsd4_encode_destroy_session(struct nfsd4_compoundres *resp, int nfserr,
3361 struct nfsd4_destroy_session *destroy_session)
3362{
Andy Adamson2db134e2009-04-03 08:27:55 +03003363 return nfserr;
3364}
3365
Daniel Mackc47d8322011-05-16 16:38:14 +02003366static __be32
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003367nfsd4_encode_free_stateid(struct nfsd4_compoundres *resp, int nfserr,
3368 struct nfsd4_free_stateid *free_stateid)
3369{
3370 __be32 *p;
3371
3372 if (nfserr)
3373 return nfserr;
3374
3375 RESERVE_SPACE(4);
3376 WRITE32(nfserr);
3377 ADJUST_ARGS();
3378 return nfserr;
3379}
3380
3381static __be32
Andy Adamson2db134e2009-04-03 08:27:55 +03003382nfsd4_encode_sequence(struct nfsd4_compoundres *resp, int nfserr,
3383 struct nfsd4_sequence *seq)
3384{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003385 __be32 *p;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003386
3387 if (nfserr)
3388 return nfserr;
3389
3390 RESERVE_SPACE(NFS4_MAX_SESSIONID_LEN + 20);
3391 WRITEMEM(seq->sessionid.data, NFS4_MAX_SESSIONID_LEN);
3392 WRITE32(seq->seqid);
3393 WRITE32(seq->slotid);
J. Bruce Fieldsb7d7ca32011-08-31 15:39:30 -04003394 /* Note slotid's are numbered from zero: */
3395 WRITE32(seq->maxslots - 1); /* sr_highest_slotid */
3396 WRITE32(seq->maxslots - 1); /* sr_target_highest_slotid */
J. Bruce Fields0d7bb712010-11-18 08:30:33 -05003397 WRITE32(seq->status_flags);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003398
3399 ADJUST_ARGS();
Andy Adamson557ce262009-08-28 08:45:04 -04003400 resp->cstate.datap = p; /* DRC cache data pointer */
Benny Halevyb85d4c02009-04-03 08:28:08 +03003401 return 0;
Andy Adamson2db134e2009-04-03 08:27:55 +03003402}
3403
Bryan Schumaker17456802011-07-13 10:50:48 -04003404__be32
3405nfsd4_encode_test_stateid(struct nfsd4_compoundres *resp, int nfserr,
3406 struct nfsd4_test_stateid *test_stateid)
3407{
Bryan Schumaker03cfb422012-01-27 10:22:49 -05003408 struct nfsd4_test_stateid_id *stateid, *next;
Bryan Schumaker17456802011-07-13 10:50:48 -04003409 __be32 *p;
Bryan Schumaker17456802011-07-13 10:50:48 -04003410
J. Bruce Fieldsc2189c72014-02-03 16:31:42 -05003411 if (nfserr)
3412 return nfserr;
3413
Bryan Schumaker03cfb422012-01-27 10:22:49 -05003414 RESERVE_SPACE(4 + (4 * test_stateid->ts_num_ids));
Bryan Schumaker17456802011-07-13 10:50:48 -04003415 *p++ = htonl(test_stateid->ts_num_ids);
Bryan Schumaker17456802011-07-13 10:50:48 -04003416
Bryan Schumaker03cfb422012-01-27 10:22:49 -05003417 list_for_each_entry_safe(stateid, next, &test_stateid->ts_stateid_list, ts_id_list) {
Al Viro02f5fde2012-04-13 00:10:34 -04003418 *p++ = stateid->ts_id_status;
Bryan Schumaker17456802011-07-13 10:50:48 -04003419 }
Bryan Schumaker17456802011-07-13 10:50:48 -04003420
Bryan Schumaker03cfb422012-01-27 10:22:49 -05003421 ADJUST_ARGS();
Bryan Schumaker17456802011-07-13 10:50:48 -04003422 return nfserr;
3423}
3424
Andy Adamson2db134e2009-04-03 08:27:55 +03003425static __be32
Benny Halevy695e12f2008-07-04 14:38:33 +03003426nfsd4_encode_noop(struct nfsd4_compoundres *resp, __be32 nfserr, void *p)
3427{
3428 return nfserr;
3429}
3430
3431typedef __be32(* nfsd4_enc)(struct nfsd4_compoundres *, __be32, void *);
3432
Andy Adamson2db134e2009-04-03 08:27:55 +03003433/*
3434 * Note: nfsd4_enc_ops vector is shared for v4.0 and v4.1
3435 * since we don't need to filter out obsolete ops as this is
3436 * done in the decoding phase.
3437 */
Benny Halevy695e12f2008-07-04 14:38:33 +03003438static nfsd4_enc nfsd4_enc_ops[] = {
J. Bruce Fieldsad1060c2008-07-18 15:04:16 -04003439 [OP_ACCESS] = (nfsd4_enc)nfsd4_encode_access,
3440 [OP_CLOSE] = (nfsd4_enc)nfsd4_encode_close,
3441 [OP_COMMIT] = (nfsd4_enc)nfsd4_encode_commit,
3442 [OP_CREATE] = (nfsd4_enc)nfsd4_encode_create,
3443 [OP_DELEGPURGE] = (nfsd4_enc)nfsd4_encode_noop,
3444 [OP_DELEGRETURN] = (nfsd4_enc)nfsd4_encode_noop,
3445 [OP_GETATTR] = (nfsd4_enc)nfsd4_encode_getattr,
3446 [OP_GETFH] = (nfsd4_enc)nfsd4_encode_getfh,
3447 [OP_LINK] = (nfsd4_enc)nfsd4_encode_link,
3448 [OP_LOCK] = (nfsd4_enc)nfsd4_encode_lock,
3449 [OP_LOCKT] = (nfsd4_enc)nfsd4_encode_lockt,
3450 [OP_LOCKU] = (nfsd4_enc)nfsd4_encode_locku,
3451 [OP_LOOKUP] = (nfsd4_enc)nfsd4_encode_noop,
3452 [OP_LOOKUPP] = (nfsd4_enc)nfsd4_encode_noop,
3453 [OP_NVERIFY] = (nfsd4_enc)nfsd4_encode_noop,
3454 [OP_OPEN] = (nfsd4_enc)nfsd4_encode_open,
Benny Halevy84f09f42009-03-04 23:05:35 +02003455 [OP_OPENATTR] = (nfsd4_enc)nfsd4_encode_noop,
J. Bruce Fieldsad1060c2008-07-18 15:04:16 -04003456 [OP_OPEN_CONFIRM] = (nfsd4_enc)nfsd4_encode_open_confirm,
3457 [OP_OPEN_DOWNGRADE] = (nfsd4_enc)nfsd4_encode_open_downgrade,
3458 [OP_PUTFH] = (nfsd4_enc)nfsd4_encode_noop,
3459 [OP_PUTPUBFH] = (nfsd4_enc)nfsd4_encode_noop,
3460 [OP_PUTROOTFH] = (nfsd4_enc)nfsd4_encode_noop,
3461 [OP_READ] = (nfsd4_enc)nfsd4_encode_read,
3462 [OP_READDIR] = (nfsd4_enc)nfsd4_encode_readdir,
3463 [OP_READLINK] = (nfsd4_enc)nfsd4_encode_readlink,
3464 [OP_REMOVE] = (nfsd4_enc)nfsd4_encode_remove,
3465 [OP_RENAME] = (nfsd4_enc)nfsd4_encode_rename,
3466 [OP_RENEW] = (nfsd4_enc)nfsd4_encode_noop,
3467 [OP_RESTOREFH] = (nfsd4_enc)nfsd4_encode_noop,
3468 [OP_SAVEFH] = (nfsd4_enc)nfsd4_encode_noop,
3469 [OP_SECINFO] = (nfsd4_enc)nfsd4_encode_secinfo,
3470 [OP_SETATTR] = (nfsd4_enc)nfsd4_encode_setattr,
3471 [OP_SETCLIENTID] = (nfsd4_enc)nfsd4_encode_setclientid,
3472 [OP_SETCLIENTID_CONFIRM] = (nfsd4_enc)nfsd4_encode_noop,
3473 [OP_VERIFY] = (nfsd4_enc)nfsd4_encode_noop,
3474 [OP_WRITE] = (nfsd4_enc)nfsd4_encode_write,
3475 [OP_RELEASE_LOCKOWNER] = (nfsd4_enc)nfsd4_encode_noop,
Andy Adamson2db134e2009-04-03 08:27:55 +03003476
3477 /* NFSv4.1 operations */
3478 [OP_BACKCHANNEL_CTL] = (nfsd4_enc)nfsd4_encode_noop,
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003479 [OP_BIND_CONN_TO_SESSION] = (nfsd4_enc)nfsd4_encode_bind_conn_to_session,
Andy Adamson2db134e2009-04-03 08:27:55 +03003480 [OP_EXCHANGE_ID] = (nfsd4_enc)nfsd4_encode_exchange_id,
3481 [OP_CREATE_SESSION] = (nfsd4_enc)nfsd4_encode_create_session,
3482 [OP_DESTROY_SESSION] = (nfsd4_enc)nfsd4_encode_destroy_session,
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003483 [OP_FREE_STATEID] = (nfsd4_enc)nfsd4_encode_free_stateid,
Andy Adamson2db134e2009-04-03 08:27:55 +03003484 [OP_GET_DIR_DELEGATION] = (nfsd4_enc)nfsd4_encode_noop,
3485 [OP_GETDEVICEINFO] = (nfsd4_enc)nfsd4_encode_noop,
3486 [OP_GETDEVICELIST] = (nfsd4_enc)nfsd4_encode_noop,
3487 [OP_LAYOUTCOMMIT] = (nfsd4_enc)nfsd4_encode_noop,
3488 [OP_LAYOUTGET] = (nfsd4_enc)nfsd4_encode_noop,
3489 [OP_LAYOUTRETURN] = (nfsd4_enc)nfsd4_encode_noop,
Mi Jinlong22b6dee2010-12-27 14:29:57 +08003490 [OP_SECINFO_NO_NAME] = (nfsd4_enc)nfsd4_encode_secinfo_no_name,
Andy Adamson2db134e2009-04-03 08:27:55 +03003491 [OP_SEQUENCE] = (nfsd4_enc)nfsd4_encode_sequence,
3492 [OP_SET_SSV] = (nfsd4_enc)nfsd4_encode_noop,
Bryan Schumaker17456802011-07-13 10:50:48 -04003493 [OP_TEST_STATEID] = (nfsd4_enc)nfsd4_encode_test_stateid,
Andy Adamson2db134e2009-04-03 08:27:55 +03003494 [OP_WANT_DELEGATION] = (nfsd4_enc)nfsd4_encode_noop,
3495 [OP_DESTROY_CLIENTID] = (nfsd4_enc)nfsd4_encode_noop,
3496 [OP_RECLAIM_COMPLETE] = (nfsd4_enc)nfsd4_encode_noop,
Benny Halevy695e12f2008-07-04 14:38:33 +03003497};
3498
Andy Adamson496c2622009-04-03 08:28:48 +03003499/*
3500 * Calculate the total amount of memory that the compound response has taken
Mi Jinlong58e7b332011-08-28 18:18:56 +08003501 * after encoding the current operation with pad.
Andy Adamson496c2622009-04-03 08:28:48 +03003502 *
Mi Jinlong58e7b332011-08-28 18:18:56 +08003503 * pad: if operation is non-idempotent, pad was calculate by op_rsize_bop()
3504 * which was specified at nfsd4_operation, else pad is zero.
Andy Adamson496c2622009-04-03 08:28:48 +03003505 *
Mi Jinlong58e7b332011-08-28 18:18:56 +08003506 * Compare this length to the session se_fmaxresp_sz and se_fmaxresp_cached.
Andy Adamson496c2622009-04-03 08:28:48 +03003507 *
3508 * Our se_fmaxresp_cached will always be a multiple of PAGE_SIZE, and so
3509 * will be at least a page and will therefore hold the xdr_buf head.
3510 */
Mi Jinlong58e7b332011-08-28 18:18:56 +08003511int nfsd4_check_resp_size(struct nfsd4_compoundres *resp, u32 pad)
Andy Adamson496c2622009-04-03 08:28:48 +03003512{
Andy Adamson496c2622009-04-03 08:28:48 +03003513 struct xdr_buf *xb = &resp->rqstp->rq_res;
Andy Adamson496c2622009-04-03 08:28:48 +03003514 struct nfsd4_session *session = NULL;
3515 struct nfsd4_slot *slot = resp->cstate.slot;
Mi Jinlong58e7b332011-08-28 18:18:56 +08003516 u32 length, tlen = 0;
Andy Adamson496c2622009-04-03 08:28:48 +03003517
3518 if (!nfsd4_has_session(&resp->cstate))
Mi Jinlong58e7b332011-08-28 18:18:56 +08003519 return 0;
Andy Adamson496c2622009-04-03 08:28:48 +03003520
3521 session = resp->cstate.session;
Mi Jinlong58e7b332011-08-28 18:18:56 +08003522 if (session == NULL)
3523 return 0;
Andy Adamson496c2622009-04-03 08:28:48 +03003524
3525 if (xb->page_len == 0) {
3526 length = (char *)resp->p - (char *)xb->head[0].iov_base + pad;
3527 } else {
3528 if (xb->tail[0].iov_base && xb->tail[0].iov_len > 0)
3529 tlen = (char *)resp->p - (char *)xb->tail[0].iov_base;
3530
3531 length = xb->head[0].iov_len + xb->page_len + tlen + pad;
3532 }
3533 dprintk("%s length %u, xb->page_len %u tlen %u pad %u\n", __func__,
3534 length, xb->page_len, tlen, pad);
3535
Mi Jinlong58e7b332011-08-28 18:18:56 +08003536 if (length > session->se_fchannel.maxresp_sz)
3537 return nfserr_rep_too_big;
3538
J. Bruce Fields73e79482012-02-13 16:39:00 -05003539 if ((slot->sl_flags & NFSD4_SLOT_CACHETHIS) &&
Mi Jinlong58e7b332011-08-28 18:18:56 +08003540 length > session->se_fchannel.maxresp_cached)
Andy Adamson496c2622009-04-03 08:28:48 +03003541 return nfserr_rep_too_big_to_cache;
Mi Jinlong58e7b332011-08-28 18:18:56 +08003542
3543 return 0;
Andy Adamson496c2622009-04-03 08:28:48 +03003544}
3545
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546void
3547nfsd4_encode_operation(struct nfsd4_compoundres *resp, struct nfsd4_op *op)
3548{
Al Viro2ebbc012006-10-19 23:28:58 -07003549 __be32 *statp;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003550 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551
3552 RESERVE_SPACE(8);
3553 WRITE32(op->opnum);
3554 statp = p++; /* to be backfilled at the end */
3555 ADJUST_ARGS();
3556
Benny Halevy695e12f2008-07-04 14:38:33 +03003557 if (op->opnum == OP_ILLEGAL)
3558 goto status;
3559 BUG_ON(op->opnum < 0 || op->opnum >= ARRAY_SIZE(nfsd4_enc_ops) ||
3560 !nfsd4_enc_ops[op->opnum]);
3561 op->status = nfsd4_enc_ops[op->opnum](resp, op->status, &op->u);
Andy Adamson496c2622009-04-03 08:28:48 +03003562 /* nfsd4_check_drc_limit guarantees enough room for error status */
Mi Jinlong58e7b332011-08-28 18:18:56 +08003563 if (!op->status)
3564 op->status = nfsd4_check_resp_size(resp, 0);
Benny Halevy695e12f2008-07-04 14:38:33 +03003565status:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566 /*
3567 * Note: We write the status directly, instead of using WRITE32(),
3568 * since it is already in network byte order.
3569 */
3570 *statp = op->status;
3571}
3572
3573/*
3574 * Encode the reply stored in the stateowner reply cache
3575 *
3576 * XDR note: do not encode rp->rp_buflen: the buffer contains the
3577 * previously sent already encoded operation.
3578 *
3579 * called with nfs4_lock_state() held
3580 */
3581void
3582nfsd4_encode_replay(struct nfsd4_compoundres *resp, struct nfsd4_op *op)
3583{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003584 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585 struct nfs4_replay *rp = op->replay;
3586
3587 BUG_ON(!rp);
3588
3589 RESERVE_SPACE(8);
3590 WRITE32(op->opnum);
3591 *p++ = rp->rp_status; /* already xdr'ed */
3592 ADJUST_ARGS();
3593
3594 RESERVE_SPACE(rp->rp_buflen);
3595 WRITEMEM(rp->rp_buf, rp->rp_buflen);
3596 ADJUST_ARGS();
3597}
3598
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599int
Al Viro2ebbc012006-10-19 23:28:58 -07003600nfs4svc_encode_voidres(struct svc_rqst *rqstp, __be32 *p, void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601{
3602 return xdr_ressize_check(rqstp, p);
3603}
3604
J. Bruce Fields3e98abf2011-07-16 17:15:10 -04003605int nfsd4_release_compoundargs(void *rq, __be32 *p, void *resp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606{
J. Bruce Fields3e98abf2011-07-16 17:15:10 -04003607 struct svc_rqst *rqstp = rq;
3608 struct nfsd4_compoundargs *args = rqstp->rq_argp;
3609
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610 if (args->ops != args->iops) {
3611 kfree(args->ops);
3612 args->ops = args->iops;
3613 }
Jesper Juhlf99d49a2005-11-07 01:01:34 -08003614 kfree(args->tmpp);
3615 args->tmpp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616 while (args->to_free) {
3617 struct tmpbuf *tb = args->to_free;
3618 args->to_free = tb->next;
3619 tb->release(tb->buf);
3620 kfree(tb);
3621 }
J. Bruce Fields3e98abf2011-07-16 17:15:10 -04003622 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623}
3624
3625int
Al Viro2ebbc012006-10-19 23:28:58 -07003626nfs4svc_decode_compoundargs(struct svc_rqst *rqstp, __be32 *p, struct nfsd4_compoundargs *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628 args->p = p;
3629 args->end = rqstp->rq_arg.head[0].iov_base + rqstp->rq_arg.head[0].iov_len;
3630 args->pagelist = rqstp->rq_arg.pages;
3631 args->pagelen = rqstp->rq_arg.page_len;
3632 args->tmpp = NULL;
3633 args->to_free = NULL;
3634 args->ops = args->iops;
3635 args->rqstp = rqstp;
3636
J. Bruce Fields3e98abf2011-07-16 17:15:10 -04003637 return !nfsd4_decode_compound(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638}
3639
3640int
Al Viro2ebbc012006-10-19 23:28:58 -07003641nfs4svc_encode_compoundres(struct svc_rqst *rqstp, __be32 *p, struct nfsd4_compoundres *resp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642{
3643 /*
3644 * All that remains is to write the tag and operation count...
3645 */
Andy Adamson557ce262009-08-28 08:45:04 -04003646 struct nfsd4_compound_state *cs = &resp->cstate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647 struct kvec *iov;
3648 p = resp->tagp;
3649 *p++ = htonl(resp->taglen);
3650 memcpy(p, resp->tag, resp->taglen);
3651 p += XDR_QUADLEN(resp->taglen);
3652 *p++ = htonl(resp->opcnt);
3653
3654 if (rqstp->rq_res.page_len)
3655 iov = &rqstp->rq_res.tail[0];
3656 else
3657 iov = &rqstp->rq_res.head[0];
3658 iov->iov_len = ((char*)resp->p) - (char*)iov->iov_base;
3659 BUG_ON(iov->iov_len > PAGE_SIZE);
J. Bruce Fields26c0c752010-04-24 15:35:43 -04003660 if (nfsd4_has_session(cs)) {
3661 if (cs->status != nfserr_replay_cache) {
3662 nfsd4_store_cache_entry(resp);
J. Bruce Fields73e79482012-02-13 16:39:00 -05003663 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
J. Bruce Fields26c0c752010-04-24 15:35:43 -04003664 }
Benny Halevyd7682982010-05-12 00:13:54 +03003665 /* Renew the clientid on success and on replay */
3666 release_session_client(cs->session);
J. Bruce Fields76407f72010-06-22 14:10:14 -04003667 nfsd4_put_session(cs->session);
Andy Adamsonda3846a2009-04-03 08:28:22 +03003668 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669 return 1;
3670}
3671
3672/*
3673 * Local variables:
3674 * c-basic-offset: 8
3675 * End:
3676 */