blob: a0402c745ed1c7e300061de83fd8e83d2c5aff74 [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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 argp->pagelist++;
J. Bruce Fieldsffdae5d2013-06-21 11:48:11 -0400165 argp->p = page_address(argp->pagelist[0]);
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
J. Bruce Fieldse3d0b282011-08-10 19:16:22 -04001718static bool seqid_mutating_err(__be32 err)
1719{
1720 /* rfc 3530 section 8.1.5: */
1721 return err != nfserr_stale_clientid &&
1722 err != nfserr_stale_stateid &&
1723 err != nfserr_bad_stateid &&
1724 err != nfserr_bad_seqid &&
1725 err != nfserr_bad_xdr &&
1726 err != nfserr_resource &&
1727 err != nfserr_nofilehandle;
1728}
1729
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730/*
NeilBrown7fb64ce2005-07-07 17:59:20 -07001731 * Routine for encoding the result of a "seqid-mutating" NFSv4 operation. This
1732 * is where sequence id's are incremented, and the replay cache is filled.
1733 * Note that we increment sequence id's here, at the last moment, so we're sure
1734 * we know whether the error to be returned is a sequence id mutating error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 */
1736
J. Bruce Fieldsf3e42232011-08-24 12:27:31 -04001737static void encode_seqid_op_tail(struct nfsd4_compoundres *resp, __be32 *save, __be32 nfserr)
1738{
1739 struct nfs4_stateowner *stateowner = resp->cstate.replay_owner;
1740
1741 if (seqid_mutating_err(ntohl(nfserr)) && stateowner) {
1742 stateowner->so_seqid++;
1743 stateowner->so_replay.rp_status = nfserr;
1744 stateowner->so_replay.rp_buflen =
1745 (char *)resp->p - (char *)save;
1746 memcpy(stateowner->so_replay.rp_buf, save,
1747 stateowner->so_replay.rp_buflen);
J. Bruce Fields38c387b2011-09-16 17:42:48 -04001748 nfsd4_purge_closed_stateid(stateowner);
J. Bruce Fieldsf3e42232011-08-24 12:27:31 -04001749 }
1750}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001752/* Encode as an array of strings the string given with components
Daniel Mack3ad2f3f2010-02-03 08:01:28 +08001753 * separated @sep.
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001754 */
Al Virob37ad282006-10-19 23:28:59 -07001755static __be32 nfsd4_encode_components(char sep, char *components,
Al Viro2ebbc012006-10-19 23:28:58 -07001756 __be32 **pp, int *buflen)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001757{
Al Viro2ebbc012006-10-19 23:28:58 -07001758 __be32 *p = *pp;
1759 __be32 *countp = p;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001760 int strlen, count=0;
1761 char *str, *end;
1762
1763 dprintk("nfsd4_encode_components(%s)\n", components);
1764 if ((*buflen -= 4) < 0)
1765 return nfserr_resource;
1766 WRITE32(0); /* We will fill this in with @count later */
1767 end = str = components;
1768 while (*end) {
1769 for (; *end && (*end != sep); end++)
1770 ; /* Point to end of component */
1771 strlen = end - str;
1772 if (strlen) {
1773 if ((*buflen -= ((XDR_QUADLEN(strlen) << 2) + 4)) < 0)
1774 return nfserr_resource;
1775 WRITE32(strlen);
1776 WRITEMEM(str, strlen);
1777 count++;
1778 }
1779 else
1780 end++;
1781 str = end;
1782 }
1783 *pp = p;
1784 p = countp;
1785 WRITE32(count);
1786 return 0;
1787}
1788
1789/*
1790 * encode a location element of a fs_locations structure
1791 */
Al Virob37ad282006-10-19 23:28:59 -07001792static __be32 nfsd4_encode_fs_location4(struct nfsd4_fs_location *location,
Al Viro2ebbc012006-10-19 23:28:58 -07001793 __be32 **pp, int *buflen)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001794{
Al Virob37ad282006-10-19 23:28:59 -07001795 __be32 status;
Al Viro2ebbc012006-10-19 23:28:58 -07001796 __be32 *p = *pp;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001797
1798 status = nfsd4_encode_components(':', location->hosts, &p, buflen);
1799 if (status)
1800 return status;
1801 status = nfsd4_encode_components('/', location->path, &p, buflen);
1802 if (status)
1803 return status;
1804 *pp = p;
1805 return 0;
1806}
1807
1808/*
Trond Myklebusted748aa2011-09-12 19:37:06 -04001809 * Encode a path in RFC3530 'pathname4' format
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001810 */
Trond Myklebusted748aa2011-09-12 19:37:06 -04001811static __be32 nfsd4_encode_path(const struct path *root,
1812 const struct path *path, __be32 **pp, int *buflen)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001813{
Trond Myklebusted748aa2011-09-12 19:37:06 -04001814 struct path cur = {
1815 .mnt = path->mnt,
1816 .dentry = path->dentry,
1817 };
1818 __be32 *p = *pp;
1819 struct dentry **components = NULL;
1820 unsigned int ncomponents = 0;
1821 __be32 err = nfserr_jukebox;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001822
Trond Myklebusted748aa2011-09-12 19:37:06 -04001823 dprintk("nfsd4_encode_components(");
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001824
Trond Myklebusted748aa2011-09-12 19:37:06 -04001825 path_get(&cur);
1826 /* First walk the path up to the nfsd root, and store the
1827 * dentries/path components in an array.
1828 */
1829 for (;;) {
1830 if (cur.dentry == root->dentry && cur.mnt == root->mnt)
1831 break;
1832 if (cur.dentry == cur.mnt->mnt_root) {
1833 if (follow_up(&cur))
1834 continue;
1835 goto out_free;
1836 }
1837 if ((ncomponents & 15) == 0) {
1838 struct dentry **new;
1839 new = krealloc(components,
1840 sizeof(*new) * (ncomponents + 16),
1841 GFP_KERNEL);
1842 if (!new)
1843 goto out_free;
1844 components = new;
1845 }
1846 components[ncomponents++] = cur.dentry;
1847 cur.dentry = dget_parent(cur.dentry);
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001848 }
Trond Myklebusted748aa2011-09-12 19:37:06 -04001849
1850 *buflen -= 4;
1851 if (*buflen < 0)
1852 goto out_free;
1853 WRITE32(ncomponents);
1854
1855 while (ncomponents) {
1856 struct dentry *dentry = components[ncomponents - 1];
1857 unsigned int len = dentry->d_name.len;
1858
1859 *buflen -= 4 + (XDR_QUADLEN(len) << 2);
1860 if (*buflen < 0)
1861 goto out_free;
1862 WRITE32(len);
1863 WRITEMEM(dentry->d_name.name, len);
1864 dprintk("/%s", dentry->d_name.name);
1865 dput(dentry);
1866 ncomponents--;
1867 }
1868
1869 *pp = p;
1870 err = 0;
1871out_free:
1872 dprintk(")\n");
1873 while (ncomponents)
1874 dput(components[--ncomponents]);
1875 kfree(components);
1876 path_put(&cur);
1877 return err;
1878}
1879
1880static __be32 nfsd4_encode_fsloc_fsroot(struct svc_rqst *rqstp,
1881 const struct path *path, __be32 **pp, int *buflen)
1882{
1883 struct svc_export *exp_ps;
1884 __be32 res;
1885
1886 exp_ps = rqst_find_fsidzero_export(rqstp);
1887 if (IS_ERR(exp_ps))
1888 return nfserrno(PTR_ERR(exp_ps));
1889 res = nfsd4_encode_path(&exp_ps->ex_path, path, pp, buflen);
1890 exp_put(exp_ps);
1891 return res;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001892}
1893
1894/*
1895 * encode a fs_locations structure
1896 */
Al Virob37ad282006-10-19 23:28:59 -07001897static __be32 nfsd4_encode_fs_locations(struct svc_rqst *rqstp,
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001898 struct svc_export *exp,
Al Viro2ebbc012006-10-19 23:28:58 -07001899 __be32 **pp, int *buflen)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001900{
Al Virob37ad282006-10-19 23:28:59 -07001901 __be32 status;
Al Virocc45f012006-10-19 23:28:44 -07001902 int i;
Al Viro2ebbc012006-10-19 23:28:58 -07001903 __be32 *p = *pp;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001904 struct nfsd4_fs_locations *fslocs = &exp->ex_fslocs;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001905
Trond Myklebusted748aa2011-09-12 19:37:06 -04001906 status = nfsd4_encode_fsloc_fsroot(rqstp, &exp->ex_path, &p, buflen);
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001907 if (status)
1908 return status;
1909 if ((*buflen -= 4) < 0)
1910 return nfserr_resource;
1911 WRITE32(fslocs->locations_count);
1912 for (i=0; i<fslocs->locations_count; i++) {
1913 status = nfsd4_encode_fs_location4(&fslocs->locations[i],
1914 &p, buflen);
1915 if (status)
1916 return status;
1917 }
1918 *pp = p;
1919 return 0;
1920}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921
J. Bruce Fields3d2544b2011-08-15 11:49:30 -04001922static u32 nfs4_file_type(umode_t mode)
1923{
1924 switch (mode & S_IFMT) {
1925 case S_IFIFO: return NF4FIFO;
1926 case S_IFCHR: return NF4CHR;
1927 case S_IFDIR: return NF4DIR;
1928 case S_IFBLK: return NF4BLK;
1929 case S_IFLNK: return NF4LNK;
1930 case S_IFREG: return NF4REG;
1931 case S_IFSOCK: return NF4SOCK;
1932 default: return NF4BAD;
1933 };
1934}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935
Al Virob37ad282006-10-19 23:28:59 -07001936static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937nfsd4_encode_name(struct svc_rqst *rqstp, int whotype, uid_t id, int group,
Al Viro2ebbc012006-10-19 23:28:58 -07001938 __be32 **p, int *buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939{
1940 int status;
1941
1942 if (*buflen < (XDR_QUADLEN(IDMAP_NAMESZ) << 2) + 4)
1943 return nfserr_resource;
1944 if (whotype != NFS4_ACL_WHO_NAMED)
1945 status = nfs4_acl_write_who(whotype, (u8 *)(*p + 1));
1946 else if (group)
1947 status = nfsd_map_gid_to_name(rqstp, id, (u8 *)(*p + 1));
1948 else
1949 status = nfsd_map_uid_to_name(rqstp, id, (u8 *)(*p + 1));
1950 if (status < 0)
1951 return nfserrno(status);
1952 *p = xdr_encode_opaque(*p, NULL, status);
1953 *buflen -= (XDR_QUADLEN(status) << 2) + 4;
1954 BUG_ON(*buflen < 0);
1955 return 0;
1956}
1957
Al Virob37ad282006-10-19 23:28:59 -07001958static inline __be32
Al Viro2ebbc012006-10-19 23:28:58 -07001959nfsd4_encode_user(struct svc_rqst *rqstp, uid_t uid, __be32 **p, int *buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960{
1961 return nfsd4_encode_name(rqstp, NFS4_ACL_WHO_NAMED, uid, 0, p, buflen);
1962}
1963
Al Virob37ad282006-10-19 23:28:59 -07001964static inline __be32
Al Viro2ebbc012006-10-19 23:28:58 -07001965nfsd4_encode_group(struct svc_rqst *rqstp, uid_t gid, __be32 **p, int *buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966{
1967 return nfsd4_encode_name(rqstp, NFS4_ACL_WHO_NAMED, gid, 1, p, buflen);
1968}
1969
Al Virob37ad282006-10-19 23:28:59 -07001970static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971nfsd4_encode_aclname(struct svc_rqst *rqstp, int whotype, uid_t id, int group,
Al Viro2ebbc012006-10-19 23:28:58 -07001972 __be32 **p, int *buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973{
1974 return nfsd4_encode_name(rqstp, whotype, id, group, p, buflen);
1975}
1976
J.Bruce Fields42ca0992006-10-04 02:16:20 -07001977#define WORD0_ABSENT_FS_ATTRS (FATTR4_WORD0_FS_LOCATIONS | FATTR4_WORD0_FSID | \
1978 FATTR4_WORD0_RDATTR_ERROR)
1979#define WORD1_ABSENT_FS_ATTRS FATTR4_WORD1_MOUNTED_ON_FILEID
1980
Al Virob37ad282006-10-19 23:28:59 -07001981static __be32 fattr_handle_absent_fs(u32 *bmval0, u32 *bmval1, u32 *rdattr_err)
J.Bruce Fields42ca0992006-10-04 02:16:20 -07001982{
1983 /* As per referral draft: */
1984 if (*bmval0 & ~WORD0_ABSENT_FS_ATTRS ||
1985 *bmval1 & ~WORD1_ABSENT_FS_ATTRS) {
1986 if (*bmval0 & FATTR4_WORD0_RDATTR_ERROR ||
1987 *bmval0 & FATTR4_WORD0_FS_LOCATIONS)
1988 *rdattr_err = NFSERR_MOVED;
1989 else
1990 return nfserr_moved;
1991 }
1992 *bmval0 &= WORD0_ABSENT_FS_ATTRS;
1993 *bmval1 &= WORD1_ABSENT_FS_ATTRS;
1994 return 0;
1995}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996
1997/*
1998 * Note: @fhp can be NULL; in this case, we might have to compose the filehandle
1999 * ourselves.
2000 *
2001 * @countp is the buffer size in _words_; upon successful return this becomes
2002 * replaced with the number of words written.
2003 */
Al Virob37ad282006-10-19 23:28:59 -07002004__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005nfsd4_encode_fattr(struct svc_fh *fhp, struct svc_export *exp,
Al Viro2ebbc012006-10-19 23:28:58 -07002006 struct dentry *dentry, __be32 *buffer, int *countp, u32 *bmval,
Frank Filz406a7ea2007-11-27 11:34:05 -08002007 struct svc_rqst *rqstp, int ignore_crossmnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008{
2009 u32 bmval0 = bmval[0];
2010 u32 bmval1 = bmval[1];
Andy Adamson7e705702009-04-03 08:29:11 +03002011 u32 bmval2 = bmval[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 struct kstat stat;
2013 struct svc_fh tempfh;
2014 struct kstatfs statfs;
2015 int buflen = *countp << 2;
Al Viro2ebbc012006-10-19 23:28:58 -07002016 __be32 *attrlenp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 u32 dummy;
2018 u64 dummy64;
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002019 u32 rdattr_err = 0;
Al Viro2ebbc012006-10-19 23:28:58 -07002020 __be32 *p = buffer;
Al Virob37ad282006-10-19 23:28:59 -07002021 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07002022 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 int aclsupport = 0;
2024 struct nfs4_acl *acl = NULL;
Andy Adamson7e705702009-04-03 08:29:11 +03002025 struct nfsd4_compoundres *resp = rqstp->rq_resp;
2026 u32 minorversion = resp->cstate.minorversion;
Christoph Hellwigebabe9a2010-07-07 18:53:11 +02002027 struct path path = {
2028 .mnt = exp->ex_path.mnt,
2029 .dentry = dentry,
2030 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031
2032 BUG_ON(bmval1 & NFSD_WRITEONLY_ATTRS_WORD1);
Andy Adamson7e705702009-04-03 08:29:11 +03002033 BUG_ON(bmval0 & ~nfsd_suppattrs0(minorversion));
2034 BUG_ON(bmval1 & ~nfsd_suppattrs1(minorversion));
2035 BUG_ON(bmval2 & ~nfsd_suppattrs2(minorversion));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002037 if (exp->ex_fslocs.migrated) {
Andy Adamson7e705702009-04-03 08:29:11 +03002038 BUG_ON(bmval[2]);
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002039 status = fattr_handle_absent_fs(&bmval0, &bmval1, &rdattr_err);
2040 if (status)
2041 goto out;
2042 }
2043
Jan Blunck54775492008-02-14 19:38:39 -08002044 err = vfs_getattr(exp->ex_path.mnt, dentry, &stat);
Al Virob8dd7b92006-10-19 23:29:01 -07002045 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 goto out_nfserr;
J. Bruce Fieldsa16e92e2007-09-28 16:45:51 -04002047 if ((bmval0 & (FATTR4_WORD0_FILES_FREE | FATTR4_WORD0_FILES_TOTAL |
2048 FATTR4_WORD0_MAXNAME)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 (bmval1 & (FATTR4_WORD1_SPACE_AVAIL | FATTR4_WORD1_SPACE_FREE |
2050 FATTR4_WORD1_SPACE_TOTAL))) {
Christoph Hellwigebabe9a2010-07-07 18:53:11 +02002051 err = vfs_statfs(&path, &statfs);
Al Virob8dd7b92006-10-19 23:29:01 -07002052 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 goto out_nfserr;
2054 }
2055 if ((bmval0 & (FATTR4_WORD0_FILEHANDLE | FATTR4_WORD0_FSID)) && !fhp) {
2056 fh_init(&tempfh, NFS4_FHSIZE);
2057 status = fh_compose(&tempfh, exp, dentry, NULL);
2058 if (status)
2059 goto out;
2060 fhp = &tempfh;
2061 }
2062 if (bmval0 & (FATTR4_WORD0_ACL | FATTR4_WORD0_ACLSUPPORT
2063 | FATTR4_WORD0_SUPPORTED_ATTRS)) {
Al Virob8dd7b92006-10-19 23:29:01 -07002064 err = nfsd4_get_nfs4_acl(rqstp, dentry, &acl);
2065 aclsupport = (err == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 if (bmval0 & FATTR4_WORD0_ACL) {
Al Virob8dd7b92006-10-19 23:29:01 -07002067 if (err == -EOPNOTSUPP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 bmval0 &= ~FATTR4_WORD0_ACL;
Al Virob8dd7b92006-10-19 23:29:01 -07002069 else if (err == -EINVAL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 status = nfserr_attrnotsupp;
2071 goto out;
Al Virob8dd7b92006-10-19 23:29:01 -07002072 } else if (err != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 goto out_nfserr;
2074 }
2075 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076
Benny Halevy2b44f1b2010-09-30 20:47:46 +02002077 if (bmval2) {
2078 if ((buflen -= 16) < 0)
2079 goto out_resource;
Andy Adamson7e705702009-04-03 08:29:11 +03002080 WRITE32(3);
2081 WRITE32(bmval0);
2082 WRITE32(bmval1);
2083 WRITE32(bmval2);
Benny Halevy2b44f1b2010-09-30 20:47:46 +02002084 } else if (bmval1) {
2085 if ((buflen -= 12) < 0)
2086 goto out_resource;
Andy Adamson7e705702009-04-03 08:29:11 +03002087 WRITE32(2);
2088 WRITE32(bmval0);
2089 WRITE32(bmval1);
2090 } else {
Benny Halevy2b44f1b2010-09-30 20:47:46 +02002091 if ((buflen -= 8) < 0)
2092 goto out_resource;
Andy Adamson7e705702009-04-03 08:29:11 +03002093 WRITE32(1);
2094 WRITE32(bmval0);
2095 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 attrlenp = p++; /* to be backfilled later */
2097
2098 if (bmval0 & FATTR4_WORD0_SUPPORTED_ATTRS) {
Andy Adamson7e705702009-04-03 08:29:11 +03002099 u32 word0 = nfsd_suppattrs0(minorversion);
2100 u32 word1 = nfsd_suppattrs1(minorversion);
2101 u32 word2 = nfsd_suppattrs2(minorversion);
2102
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002103 if (!aclsupport)
2104 word0 &= ~FATTR4_WORD0_ACL;
Andy Adamson7e705702009-04-03 08:29:11 +03002105 if (!word2) {
Benny Halevy2b44f1b2010-09-30 20:47:46 +02002106 if ((buflen -= 12) < 0)
2107 goto out_resource;
Andy Adamson7e705702009-04-03 08:29:11 +03002108 WRITE32(2);
2109 WRITE32(word0);
2110 WRITE32(word1);
2111 } else {
Benny Halevy2b44f1b2010-09-30 20:47:46 +02002112 if ((buflen -= 16) < 0)
2113 goto out_resource;
Andy Adamson7e705702009-04-03 08:29:11 +03002114 WRITE32(3);
2115 WRITE32(word0);
2116 WRITE32(word1);
2117 WRITE32(word2);
2118 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 }
2120 if (bmval0 & FATTR4_WORD0_TYPE) {
2121 if ((buflen -= 4) < 0)
2122 goto out_resource;
J. Bruce Fields3d2544b2011-08-15 11:49:30 -04002123 dummy = nfs4_file_type(stat.mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 if (dummy == NF4BAD)
2125 goto out_serverfault;
2126 WRITE32(dummy);
2127 }
2128 if (bmval0 & FATTR4_WORD0_FH_EXPIRE_TYPE) {
2129 if ((buflen -= 4) < 0)
2130 goto out_resource;
NeilBrown49640002005-06-23 22:02:58 -07002131 if (exp->ex_flags & NFSEXP_NOSUBTREECHECK)
NeilBrowne34ac862005-07-07 17:59:30 -07002132 WRITE32(NFS4_FH_PERSISTENT);
NeilBrown49640002005-06-23 22:02:58 -07002133 else
NeilBrowne34ac862005-07-07 17:59:30 -07002134 WRITE32(NFS4_FH_PERSISTENT|NFS4_FH_VOL_RENAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 }
2136 if (bmval0 & FATTR4_WORD0_CHANGE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 if ((buflen -= 8) < 0)
2138 goto out_resource;
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002139 write_change(&p, &stat, dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 }
2141 if (bmval0 & FATTR4_WORD0_SIZE) {
2142 if ((buflen -= 8) < 0)
2143 goto out_resource;
2144 WRITE64(stat.size);
2145 }
2146 if (bmval0 & FATTR4_WORD0_LINK_SUPPORT) {
2147 if ((buflen -= 4) < 0)
2148 goto out_resource;
2149 WRITE32(1);
2150 }
2151 if (bmval0 & FATTR4_WORD0_SYMLINK_SUPPORT) {
2152 if ((buflen -= 4) < 0)
2153 goto out_resource;
2154 WRITE32(1);
2155 }
2156 if (bmval0 & FATTR4_WORD0_NAMED_ATTR) {
2157 if ((buflen -= 4) < 0)
2158 goto out_resource;
2159 WRITE32(0);
2160 }
2161 if (bmval0 & FATTR4_WORD0_FSID) {
2162 if ((buflen -= 16) < 0)
2163 goto out_resource;
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002164 if (exp->ex_fslocs.migrated) {
2165 WRITE64(NFS4_REFERRAL_FSID_MAJOR);
2166 WRITE64(NFS4_REFERRAL_FSID_MINOR);
NeilBrownaf6a4e22007-02-14 00:33:12 -08002167 } else switch(fsid_source(fhp)) {
2168 case FSIDSOURCE_FSID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 WRITE64((u64)exp->ex_fsid);
2170 WRITE64((u64)0);
NeilBrownaf6a4e22007-02-14 00:33:12 -08002171 break;
2172 case FSIDSOURCE_DEV:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 WRITE32(0);
2174 WRITE32(MAJOR(stat.dev));
2175 WRITE32(0);
2176 WRITE32(MINOR(stat.dev));
NeilBrownaf6a4e22007-02-14 00:33:12 -08002177 break;
2178 case FSIDSOURCE_UUID:
2179 WRITEMEM(exp->ex_uuid, 16);
2180 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 }
2182 }
2183 if (bmval0 & FATTR4_WORD0_UNIQUE_HANDLES) {
2184 if ((buflen -= 4) < 0)
2185 goto out_resource;
2186 WRITE32(0);
2187 }
2188 if (bmval0 & FATTR4_WORD0_LEASE_TIME) {
2189 if ((buflen -= 4) < 0)
2190 goto out_resource;
J. Bruce Fieldscf07d2e2010-02-28 23:20:19 -05002191 WRITE32(nfsd4_lease);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 }
2193 if (bmval0 & FATTR4_WORD0_RDATTR_ERROR) {
2194 if ((buflen -= 4) < 0)
2195 goto out_resource;
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002196 WRITE32(rdattr_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 }
2198 if (bmval0 & FATTR4_WORD0_ACL) {
2199 struct nfs4_ace *ace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200
2201 if (acl == NULL) {
2202 if ((buflen -= 4) < 0)
2203 goto out_resource;
2204
2205 WRITE32(0);
2206 goto out_acl;
2207 }
2208 if ((buflen -= 4) < 0)
2209 goto out_resource;
2210 WRITE32(acl->naces);
2211
J. Bruce Fields28e05dd2007-02-16 01:28:30 -08002212 for (ace = acl->aces; ace < acl->aces + acl->naces; ace++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 if ((buflen -= 4*3) < 0)
2214 goto out_resource;
2215 WRITE32(ace->type);
2216 WRITE32(ace->flag);
2217 WRITE32(ace->access_mask & NFS4_ACE_MASK_ALL);
2218 status = nfsd4_encode_aclname(rqstp, ace->whotype,
2219 ace->who, ace->flag & NFS4_ACE_IDENTIFIER_GROUP,
2220 &p, &buflen);
2221 if (status == nfserr_resource)
2222 goto out_resource;
2223 if (status)
2224 goto out;
2225 }
2226 }
2227out_acl:
2228 if (bmval0 & FATTR4_WORD0_ACLSUPPORT) {
2229 if ((buflen -= 4) < 0)
2230 goto out_resource;
2231 WRITE32(aclsupport ?
2232 ACL4_SUPPORT_ALLOW_ACL|ACL4_SUPPORT_DENY_ACL : 0);
2233 }
2234 if (bmval0 & FATTR4_WORD0_CANSETTIME) {
2235 if ((buflen -= 4) < 0)
2236 goto out_resource;
2237 WRITE32(1);
2238 }
2239 if (bmval0 & FATTR4_WORD0_CASE_INSENSITIVE) {
2240 if ((buflen -= 4) < 0)
2241 goto out_resource;
J. Bruce Fieldsf0f5dcc2012-06-05 16:52:06 -04002242 WRITE32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 }
2244 if (bmval0 & FATTR4_WORD0_CASE_PRESERVING) {
2245 if ((buflen -= 4) < 0)
2246 goto out_resource;
2247 WRITE32(1);
2248 }
2249 if (bmval0 & FATTR4_WORD0_CHOWN_RESTRICTED) {
2250 if ((buflen -= 4) < 0)
2251 goto out_resource;
2252 WRITE32(1);
2253 }
2254 if (bmval0 & FATTR4_WORD0_FILEHANDLE) {
2255 buflen -= (XDR_QUADLEN(fhp->fh_handle.fh_size) << 2) + 4;
2256 if (buflen < 0)
2257 goto out_resource;
2258 WRITE32(fhp->fh_handle.fh_size);
2259 WRITEMEM(&fhp->fh_handle.fh_base, fhp->fh_handle.fh_size);
2260 }
2261 if (bmval0 & FATTR4_WORD0_FILEID) {
2262 if ((buflen -= 8) < 0)
2263 goto out_resource;
Peter Staubach40ee5dc2007-08-16 12:10:07 -04002264 WRITE64(stat.ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 }
2266 if (bmval0 & FATTR4_WORD0_FILES_AVAIL) {
2267 if ((buflen -= 8) < 0)
2268 goto out_resource;
2269 WRITE64((u64) statfs.f_ffree);
2270 }
2271 if (bmval0 & FATTR4_WORD0_FILES_FREE) {
2272 if ((buflen -= 8) < 0)
2273 goto out_resource;
2274 WRITE64((u64) statfs.f_ffree);
2275 }
2276 if (bmval0 & FATTR4_WORD0_FILES_TOTAL) {
2277 if ((buflen -= 8) < 0)
2278 goto out_resource;
2279 WRITE64((u64) statfs.f_files);
2280 }
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002281 if (bmval0 & FATTR4_WORD0_FS_LOCATIONS) {
2282 status = nfsd4_encode_fs_locations(rqstp, exp, &p, &buflen);
2283 if (status == nfserr_resource)
2284 goto out_resource;
2285 if (status)
2286 goto out;
2287 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 if (bmval0 & FATTR4_WORD0_HOMOGENEOUS) {
2289 if ((buflen -= 4) < 0)
2290 goto out_resource;
2291 WRITE32(1);
2292 }
2293 if (bmval0 & FATTR4_WORD0_MAXFILESIZE) {
2294 if ((buflen -= 8) < 0)
2295 goto out_resource;
2296 WRITE64(~(u64)0);
2297 }
2298 if (bmval0 & FATTR4_WORD0_MAXLINK) {
2299 if ((buflen -= 4) < 0)
2300 goto out_resource;
2301 WRITE32(255);
2302 }
2303 if (bmval0 & FATTR4_WORD0_MAXNAME) {
2304 if ((buflen -= 4) < 0)
2305 goto out_resource;
J. Bruce Fieldsa16e92e2007-09-28 16:45:51 -04002306 WRITE32(statfs.f_namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 }
2308 if (bmval0 & FATTR4_WORD0_MAXREAD) {
2309 if ((buflen -= 8) < 0)
2310 goto out_resource;
Greg Banks7adae482006-10-04 02:15:47 -07002311 WRITE64((u64) svc_max_payload(rqstp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 }
2313 if (bmval0 & FATTR4_WORD0_MAXWRITE) {
2314 if ((buflen -= 8) < 0)
2315 goto out_resource;
Greg Banks7adae482006-10-04 02:15:47 -07002316 WRITE64((u64) svc_max_payload(rqstp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 }
2318 if (bmval1 & FATTR4_WORD1_MODE) {
2319 if ((buflen -= 4) < 0)
2320 goto out_resource;
2321 WRITE32(stat.mode & S_IALLUGO);
2322 }
2323 if (bmval1 & FATTR4_WORD1_NO_TRUNC) {
2324 if ((buflen -= 4) < 0)
2325 goto out_resource;
2326 WRITE32(1);
2327 }
2328 if (bmval1 & FATTR4_WORD1_NUMLINKS) {
2329 if ((buflen -= 4) < 0)
2330 goto out_resource;
2331 WRITE32(stat.nlink);
2332 }
2333 if (bmval1 & FATTR4_WORD1_OWNER) {
2334 status = nfsd4_encode_user(rqstp, stat.uid, &p, &buflen);
2335 if (status == nfserr_resource)
2336 goto out_resource;
2337 if (status)
2338 goto out;
2339 }
2340 if (bmval1 & FATTR4_WORD1_OWNER_GROUP) {
2341 status = nfsd4_encode_group(rqstp, stat.gid, &p, &buflen);
2342 if (status == nfserr_resource)
2343 goto out_resource;
2344 if (status)
2345 goto out;
2346 }
2347 if (bmval1 & FATTR4_WORD1_RAWDEV) {
2348 if ((buflen -= 8) < 0)
2349 goto out_resource;
2350 WRITE32((u32) MAJOR(stat.rdev));
2351 WRITE32((u32) MINOR(stat.rdev));
2352 }
2353 if (bmval1 & FATTR4_WORD1_SPACE_AVAIL) {
2354 if ((buflen -= 8) < 0)
2355 goto out_resource;
2356 dummy64 = (u64)statfs.f_bavail * (u64)statfs.f_bsize;
2357 WRITE64(dummy64);
2358 }
2359 if (bmval1 & FATTR4_WORD1_SPACE_FREE) {
2360 if ((buflen -= 8) < 0)
2361 goto out_resource;
2362 dummy64 = (u64)statfs.f_bfree * (u64)statfs.f_bsize;
2363 WRITE64(dummy64);
2364 }
2365 if (bmval1 & FATTR4_WORD1_SPACE_TOTAL) {
2366 if ((buflen -= 8) < 0)
2367 goto out_resource;
2368 dummy64 = (u64)statfs.f_blocks * (u64)statfs.f_bsize;
2369 WRITE64(dummy64);
2370 }
2371 if (bmval1 & FATTR4_WORD1_SPACE_USED) {
2372 if ((buflen -= 8) < 0)
2373 goto out_resource;
2374 dummy64 = (u64)stat.blocks << 9;
2375 WRITE64(dummy64);
2376 }
2377 if (bmval1 & FATTR4_WORD1_TIME_ACCESS) {
2378 if ((buflen -= 12) < 0)
2379 goto out_resource;
Bryan Schumaker2111a772013-04-19 16:09:38 -04002380 WRITE64((s64)stat.atime.tv_sec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 WRITE32(stat.atime.tv_nsec);
2382 }
2383 if (bmval1 & FATTR4_WORD1_TIME_DELTA) {
2384 if ((buflen -= 12) < 0)
2385 goto out_resource;
2386 WRITE32(0);
2387 WRITE32(1);
2388 WRITE32(0);
2389 }
2390 if (bmval1 & FATTR4_WORD1_TIME_METADATA) {
2391 if ((buflen -= 12) < 0)
2392 goto out_resource;
Bryan Schumaker2111a772013-04-19 16:09:38 -04002393 WRITE64((s64)stat.ctime.tv_sec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 WRITE32(stat.ctime.tv_nsec);
2395 }
2396 if (bmval1 & FATTR4_WORD1_TIME_MODIFY) {
2397 if ((buflen -= 12) < 0)
2398 goto out_resource;
Bryan Schumaker2111a772013-04-19 16:09:38 -04002399 WRITE64((s64)stat.mtime.tv_sec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 WRITE32(stat.mtime.tv_nsec);
2401 }
2402 if (bmval1 & FATTR4_WORD1_MOUNTED_ON_FILEID) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 if ((buflen -= 8) < 0)
2404 goto out_resource;
Frank Filz406a7ea2007-11-27 11:34:05 -08002405 /*
2406 * Get parent's attributes if not ignoring crossmount
2407 * and this is the root of a cross-mounted filesystem.
2408 */
2409 if (ignore_crossmnt == 0 &&
Al Viro462d6052010-01-30 16:11:21 -05002410 dentry == exp->ex_path.mnt->mnt_root) {
2411 struct path path = exp->ex_path;
2412 path_get(&path);
2413 while (follow_up(&path)) {
2414 if (path.dentry != path.mnt->mnt_root)
2415 break;
2416 }
2417 err = vfs_getattr(path.mnt, path.dentry, &stat);
2418 path_put(&path);
Peter Staubach40ee5dc2007-08-16 12:10:07 -04002419 if (err)
2420 goto out_nfserr;
2421 }
2422 WRITE64(stat.ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 }
Benny Halevy8c18f202009-04-03 08:29:14 +03002424 if (bmval2 & FATTR4_WORD2_SUPPATTR_EXCLCREAT) {
2425 WRITE32(3);
2426 WRITE32(NFSD_SUPPATTR_EXCLCREAT_WORD0);
2427 WRITE32(NFSD_SUPPATTR_EXCLCREAT_WORD1);
2428 WRITE32(NFSD_SUPPATTR_EXCLCREAT_WORD2);
2429 }
Andy Adamson7e705702009-04-03 08:29:11 +03002430
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 *attrlenp = htonl((char *)p - (char *)attrlenp - 4);
2432 *countp = p - buffer;
2433 status = nfs_ok;
2434
2435out:
J. Bruce Fields28e05dd2007-02-16 01:28:30 -08002436 kfree(acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 if (fhp == &tempfh)
2438 fh_put(&tempfh);
2439 return status;
2440out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07002441 status = nfserrno(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 goto out;
2443out_resource:
2444 *countp = 0;
2445 status = nfserr_resource;
2446 goto out;
2447out_serverfault:
2448 status = nfserr_serverfault;
2449 goto out;
2450}
2451
J. Bruce Fieldsc0ce6ec2008-02-11 15:48:47 -05002452static inline int attributes_need_mount(u32 *bmval)
2453{
2454 if (bmval[0] & ~(FATTR4_WORD0_RDATTR_ERROR | FATTR4_WORD0_LEASE_TIME))
2455 return 1;
2456 if (bmval[1] & ~FATTR4_WORD1_MOUNTED_ON_FILEID)
2457 return 1;
2458 return 0;
2459}
2460
Al Virob37ad282006-10-19 23:28:59 -07002461static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462nfsd4_encode_dirent_fattr(struct nfsd4_readdir *cd,
Al Viro2ebbc012006-10-19 23:28:58 -07002463 const char *name, int namlen, __be32 *p, int *buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464{
2465 struct svc_export *exp = cd->rd_fhp->fh_export;
2466 struct dentry *dentry;
Al Virob37ad282006-10-19 23:28:59 -07002467 __be32 nfserr;
Frank Filz406a7ea2007-11-27 11:34:05 -08002468 int ignore_crossmnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469
2470 dentry = lookup_one_len(name, cd->rd_fhp->fh_dentry, namlen);
2471 if (IS_ERR(dentry))
2472 return nfserrno(PTR_ERR(dentry));
J. Bruce Fieldsb2c0cea2009-05-05 19:04:29 -04002473 if (!dentry->d_inode) {
2474 /*
2475 * nfsd_buffered_readdir drops the i_mutex between
2476 * readdir and calling this callback, leaving a window
2477 * where this directory entry could have gone away.
2478 */
2479 dput(dentry);
2480 return nfserr_noent;
2481 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482
2483 exp_get(exp);
Frank Filz406a7ea2007-11-27 11:34:05 -08002484 /*
2485 * In the case of a mountpoint, the client may be asking for
2486 * attributes that are only properties of the underlying filesystem
2487 * as opposed to the cross-mounted file system. In such a case,
2488 * we will not follow the cross mount and will fill the attribtutes
2489 * directly from the mountpoint dentry.
2490 */
J. Bruce Fields3227fa42009-10-25 21:43:01 -04002491 if (nfsd_mountpoint(dentry, exp)) {
J.Bruce Fields021d3a72006-12-13 00:35:24 -08002492 int err;
2493
J. Bruce Fields3227fa42009-10-25 21:43:01 -04002494 if (!(exp->ex_flags & NFSEXP_V4ROOT)
2495 && !attributes_need_mount(cd->rd_bmval)) {
2496 ignore_crossmnt = 1;
2497 goto out_encode;
2498 }
Andy Adamsondcb488a2007-07-17 04:04:51 -07002499 /*
2500 * Why the heck aren't we just using nfsd_lookup??
2501 * Different "."/".." handling? Something else?
2502 * At least, add a comment here to explain....
2503 */
J.Bruce Fields021d3a72006-12-13 00:35:24 -08002504 err = nfsd_cross_mnt(cd->rd_rqstp, &dentry, &exp);
2505 if (err) {
2506 nfserr = nfserrno(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 goto out_put;
2508 }
Andy Adamsondcb488a2007-07-17 04:04:51 -07002509 nfserr = check_nfsd_access(exp, cd->rd_rqstp);
2510 if (nfserr)
2511 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512
2513 }
J. Bruce Fields3227fa42009-10-25 21:43:01 -04002514out_encode:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 nfserr = nfsd4_encode_fattr(NULL, exp, dentry, p, buflen, cd->rd_bmval,
Frank Filz406a7ea2007-11-27 11:34:05 -08002516 cd->rd_rqstp, ignore_crossmnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517out_put:
2518 dput(dentry);
2519 exp_put(exp);
2520 return nfserr;
2521}
2522
Al Viro2ebbc012006-10-19 23:28:58 -07002523static __be32 *
Al Virob37ad282006-10-19 23:28:59 -07002524nfsd4_encode_rdattr_error(__be32 *p, int buflen, __be32 nfserr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525{
Al Viro2ebbc012006-10-19 23:28:58 -07002526 __be32 *attrlenp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527
2528 if (buflen < 6)
2529 return NULL;
2530 *p++ = htonl(2);
2531 *p++ = htonl(FATTR4_WORD0_RDATTR_ERROR); /* bmval0 */
2532 *p++ = htonl(0); /* bmval1 */
2533
2534 attrlenp = p++;
2535 *p++ = nfserr; /* no htonl */
2536 *attrlenp = htonl((char *)p - (char *)attrlenp - 4);
2537 return p;
2538}
2539
2540static int
NeilBrowna0ad13e2007-01-26 00:57:10 -08002541nfsd4_encode_dirent(void *ccdv, const char *name, int namlen,
2542 loff_t offset, u64 ino, unsigned int d_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543{
NeilBrowna0ad13e2007-01-26 00:57:10 -08002544 struct readdir_cd *ccd = ccdv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 struct nfsd4_readdir *cd = container_of(ccd, struct nfsd4_readdir, common);
2546 int buflen;
Al Viro2ebbc012006-10-19 23:28:58 -07002547 __be32 *p = cd->buffer;
J. Bruce Fieldsb2c0cea2009-05-05 19:04:29 -04002548 __be32 *cookiep;
Al Virob37ad282006-10-19 23:28:59 -07002549 __be32 nfserr = nfserr_toosmall;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550
2551 /* In nfsv4, "." and ".." never make it onto the wire.. */
2552 if (name && isdotent(name, namlen)) {
2553 cd->common.err = nfs_ok;
2554 return 0;
2555 }
2556
2557 if (cd->offset)
2558 xdr_encode_hyper(cd->offset, (u64) offset);
2559
2560 buflen = cd->buflen - 4 - XDR_QUADLEN(namlen);
2561 if (buflen < 0)
2562 goto fail;
2563
2564 *p++ = xdr_one; /* mark entry present */
J. Bruce Fieldsb2c0cea2009-05-05 19:04:29 -04002565 cookiep = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 p = xdr_encode_hyper(p, NFS_OFFSET_MAX); /* offset of next entry */
2567 p = xdr_encode_array(p, name, namlen); /* name length & name */
2568
2569 nfserr = nfsd4_encode_dirent_fattr(cd, name, namlen, p, &buflen);
2570 switch (nfserr) {
2571 case nfs_ok:
2572 p += buflen;
2573 break;
2574 case nfserr_resource:
2575 nfserr = nfserr_toosmall;
2576 goto fail;
J. Bruce Fieldsb2c0cea2009-05-05 19:04:29 -04002577 case nfserr_noent:
2578 goto skip_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 default:
2580 /*
2581 * If the client requested the RDATTR_ERROR attribute,
2582 * we stuff the error code into this attribute
2583 * and continue. If this attribute was not requested,
2584 * then in accordance with the spec, we fail the
2585 * entire READDIR operation(!)
2586 */
2587 if (!(cd->rd_bmval[0] & FATTR4_WORD0_RDATTR_ERROR))
2588 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 p = nfsd4_encode_rdattr_error(p, buflen, nfserr);
Fred Isaman34081ef2006-01-18 17:43:40 -08002590 if (p == NULL) {
2591 nfserr = nfserr_toosmall;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 goto fail;
Fred Isaman34081ef2006-01-18 17:43:40 -08002593 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 }
2595 cd->buflen -= (p - cd->buffer);
2596 cd->buffer = p;
J. Bruce Fieldsb2c0cea2009-05-05 19:04:29 -04002597 cd->offset = cookiep;
2598skip_entry:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 cd->common.err = nfs_ok;
2600 return 0;
2601fail:
2602 cd->common.err = nfserr;
2603 return -EINVAL;
2604}
2605
Benny Halevye2f282b2008-08-12 20:45:07 +03002606static void
2607nfsd4_encode_stateid(struct nfsd4_compoundres *resp, stateid_t *sid)
2608{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002609 __be32 *p;
Benny Halevye2f282b2008-08-12 20:45:07 +03002610
2611 RESERVE_SPACE(sizeof(stateid_t));
2612 WRITE32(sid->si_generation);
2613 WRITEMEM(&sid->si_opaque, sizeof(stateid_opaque_t));
2614 ADJUST_ARGS();
2615}
2616
Benny Halevy695e12f2008-07-04 14:38:33 +03002617static __be32
Al Virob37ad282006-10-19 23:28:59 -07002618nfsd4_encode_access(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_access *access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002620 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621
2622 if (!nfserr) {
2623 RESERVE_SPACE(8);
2624 WRITE32(access->ac_supported);
2625 WRITE32(access->ac_resp_access);
2626 ADJUST_ARGS();
2627 }
Benny Halevy695e12f2008-07-04 14:38:33 +03002628 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629}
2630
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002631static __be32 nfsd4_encode_bind_conn_to_session(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_bind_conn_to_session *bcts)
2632{
2633 __be32 *p;
2634
2635 if (!nfserr) {
2636 RESERVE_SPACE(NFS4_MAX_SESSIONID_LEN + 8);
2637 WRITEMEM(bcts->sessionid.data, NFS4_MAX_SESSIONID_LEN);
2638 WRITE32(bcts->dir);
2639 /* XXX: ? */
2640 WRITE32(0);
2641 ADJUST_ARGS();
2642 }
2643 return nfserr;
2644}
2645
Benny Halevy695e12f2008-07-04 14:38:33 +03002646static __be32
Al Virob37ad282006-10-19 23:28:59 -07002647nfsd4_encode_close(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648{
2649 ENCODE_SEQID_OP_HEAD;
2650
Benny Halevye2f282b2008-08-12 20:45:07 +03002651 if (!nfserr)
2652 nfsd4_encode_stateid(resp, &close->cl_stateid);
2653
J. Bruce Fieldsf3e42232011-08-24 12:27:31 -04002654 encode_seqid_op_tail(resp, save, nfserr);
Benny Halevy695e12f2008-07-04 14:38:33 +03002655 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656}
2657
2658
Benny Halevy695e12f2008-07-04 14:38:33 +03002659static __be32
Al Virob37ad282006-10-19 23:28:59 -07002660nfsd4_encode_commit(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_commit *commit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002662 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663
2664 if (!nfserr) {
Chuck Leverab4684d2012-03-02 17:13:50 -05002665 RESERVE_SPACE(NFS4_VERIFIER_SIZE);
2666 WRITEMEM(commit->co_verf.data, NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 ADJUST_ARGS();
2668 }
Benny Halevy695e12f2008-07-04 14:38:33 +03002669 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670}
2671
Benny Halevy695e12f2008-07-04 14:38:33 +03002672static __be32
Al Virob37ad282006-10-19 23:28:59 -07002673nfsd4_encode_create(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_create *create)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002675 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676
2677 if (!nfserr) {
2678 RESERVE_SPACE(32);
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002679 write_cinfo(&p, &create->cr_cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 WRITE32(2);
2681 WRITE32(create->cr_bmval[0]);
2682 WRITE32(create->cr_bmval[1]);
2683 ADJUST_ARGS();
2684 }
Benny Halevy695e12f2008-07-04 14:38:33 +03002685 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686}
2687
Al Virob37ad282006-10-19 23:28:59 -07002688static __be32
2689nfsd4_encode_getattr(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_getattr *getattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690{
2691 struct svc_fh *fhp = getattr->ga_fhp;
2692 int buflen;
2693
2694 if (nfserr)
2695 return nfserr;
2696
2697 buflen = resp->end - resp->p - (COMPOUND_ERR_SLACK_SPACE >> 2);
2698 nfserr = nfsd4_encode_fattr(fhp, fhp->fh_export, fhp->fh_dentry,
2699 resp->p, &buflen, getattr->ga_bmval,
Frank Filz406a7ea2007-11-27 11:34:05 -08002700 resp->rqstp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 if (!nfserr)
2702 resp->p += buflen;
2703 return nfserr;
2704}
2705
Benny Halevy695e12f2008-07-04 14:38:33 +03002706static __be32
2707nfsd4_encode_getfh(struct nfsd4_compoundres *resp, __be32 nfserr, struct svc_fh **fhpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708{
Benny Halevy695e12f2008-07-04 14:38:33 +03002709 struct svc_fh *fhp = *fhpp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 unsigned int len;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002711 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712
2713 if (!nfserr) {
2714 len = fhp->fh_handle.fh_size;
2715 RESERVE_SPACE(len + 4);
2716 WRITE32(len);
2717 WRITEMEM(&fhp->fh_handle.fh_base, len);
2718 ADJUST_ARGS();
2719 }
Benny Halevy695e12f2008-07-04 14:38:33 +03002720 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721}
2722
2723/*
2724* Including all fields other than the name, a LOCK4denied structure requires
2725* 8(clientid) + 4(namelen) + 8(offset) + 8(length) + 4(type) = 32 bytes.
2726*/
2727static void
2728nfsd4_encode_lock_denied(struct nfsd4_compoundres *resp, struct nfsd4_lock_denied *ld)
2729{
J. Bruce Fields7c13f342011-08-30 22:15:47 -04002730 struct xdr_netobj *conf = &ld->ld_owner;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002731 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732
J. Bruce Fields7c13f342011-08-30 22:15:47 -04002733 RESERVE_SPACE(32 + XDR_LEN(conf->len));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 WRITE64(ld->ld_start);
2735 WRITE64(ld->ld_length);
2736 WRITE32(ld->ld_type);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04002737 if (conf->len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 WRITEMEM(&ld->ld_clientid, 8);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04002739 WRITE32(conf->len);
2740 WRITEMEM(conf->data, conf->len);
2741 kfree(conf->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 } else { /* non - nfsv4 lock in conflict, no clientid nor owner */
2743 WRITE64((u64)0); /* clientid */
2744 WRITE32(0); /* length of owner name */
2745 }
2746 ADJUST_ARGS();
2747}
2748
Benny Halevy695e12f2008-07-04 14:38:33 +03002749static __be32
Al Virob37ad282006-10-19 23:28:59 -07002750nfsd4_encode_lock(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 ENCODE_SEQID_OP_HEAD;
2753
Benny Halevye2f282b2008-08-12 20:45:07 +03002754 if (!nfserr)
2755 nfsd4_encode_stateid(resp, &lock->lk_resp_stateid);
2756 else if (nfserr == nfserr_denied)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 nfsd4_encode_lock_denied(resp, &lock->lk_denied);
2758
J. Bruce Fieldsf3e42232011-08-24 12:27:31 -04002759 encode_seqid_op_tail(resp, save, nfserr);
Benny Halevy695e12f2008-07-04 14:38:33 +03002760 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761}
2762
Benny Halevy695e12f2008-07-04 14:38:33 +03002763static __be32
Al Virob37ad282006-10-19 23:28:59 -07002764nfsd4_encode_lockt(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765{
2766 if (nfserr == nfserr_denied)
2767 nfsd4_encode_lock_denied(resp, &lockt->lt_denied);
Benny Halevy695e12f2008-07-04 14:38:33 +03002768 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769}
2770
Benny Halevy695e12f2008-07-04 14:38:33 +03002771static __be32
Al Virob37ad282006-10-19 23:28:59 -07002772nfsd4_encode_locku(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773{
2774 ENCODE_SEQID_OP_HEAD;
2775
Benny Halevye2f282b2008-08-12 20:45:07 +03002776 if (!nfserr)
2777 nfsd4_encode_stateid(resp, &locku->lu_stateid);
2778
J. Bruce Fieldsf3e42232011-08-24 12:27:31 -04002779 encode_seqid_op_tail(resp, save, nfserr);
Benny Halevy695e12f2008-07-04 14:38:33 +03002780 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781}
2782
2783
Benny Halevy695e12f2008-07-04 14:38:33 +03002784static __be32
Al Virob37ad282006-10-19 23:28:59 -07002785nfsd4_encode_link(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_link *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002787 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788
2789 if (!nfserr) {
2790 RESERVE_SPACE(20);
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002791 write_cinfo(&p, &link->li_cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 ADJUST_ARGS();
2793 }
Benny Halevy695e12f2008-07-04 14:38:33 +03002794 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795}
2796
2797
Benny Halevy695e12f2008-07-04 14:38:33 +03002798static __be32
Al Virob37ad282006-10-19 23:28:59 -07002799nfsd4_encode_open(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002801 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 ENCODE_SEQID_OP_HEAD;
2803
2804 if (nfserr)
2805 goto out;
2806
Benny Halevye2f282b2008-08-12 20:45:07 +03002807 nfsd4_encode_stateid(resp, &open->op_stateid);
2808 RESERVE_SPACE(40);
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002809 write_cinfo(&p, &open->op_cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 WRITE32(open->op_rflags);
2811 WRITE32(2);
2812 WRITE32(open->op_bmval[0]);
2813 WRITE32(open->op_bmval[1]);
2814 WRITE32(open->op_delegate_type);
2815 ADJUST_ARGS();
2816
2817 switch (open->op_delegate_type) {
2818 case NFS4_OPEN_DELEGATE_NONE:
2819 break;
2820 case NFS4_OPEN_DELEGATE_READ:
Benny Halevye2f282b2008-08-12 20:45:07 +03002821 nfsd4_encode_stateid(resp, &open->op_delegate_stateid);
2822 RESERVE_SPACE(20);
NeilBrown7b190fe2005-06-23 22:03:23 -07002823 WRITE32(open->op_recall);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824
2825 /*
2826 * TODO: ACE's in delegations
2827 */
2828 WRITE32(NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
2829 WRITE32(0);
2830 WRITE32(0);
2831 WRITE32(0); /* XXX: is NULL principal ok? */
2832 ADJUST_ARGS();
2833 break;
2834 case NFS4_OPEN_DELEGATE_WRITE:
Benny Halevye2f282b2008-08-12 20:45:07 +03002835 nfsd4_encode_stateid(resp, &open->op_delegate_stateid);
2836 RESERVE_SPACE(32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 WRITE32(0);
2838
2839 /*
2840 * TODO: space_limit's in delegations
2841 */
2842 WRITE32(NFS4_LIMIT_SIZE);
2843 WRITE32(~(u32)0);
2844 WRITE32(~(u32)0);
2845
2846 /*
2847 * TODO: ACE's in delegations
2848 */
2849 WRITE32(NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
2850 WRITE32(0);
2851 WRITE32(0);
2852 WRITE32(0); /* XXX: is NULL principal ok? */
2853 ADJUST_ARGS();
2854 break;
Benny Halevyd24433c2012-02-16 20:57:17 +02002855 case NFS4_OPEN_DELEGATE_NONE_EXT: /* 4.1 */
2856 switch (open->op_why_no_deleg) {
2857 case WND4_CONTENTION:
2858 case WND4_RESOURCE:
2859 RESERVE_SPACE(8);
2860 WRITE32(open->op_why_no_deleg);
2861 WRITE32(0); /* deleg signaling not supported yet */
2862 break;
2863 default:
2864 RESERVE_SPACE(4);
2865 WRITE32(open->op_why_no_deleg);
2866 }
2867 ADJUST_ARGS();
2868 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 default:
2870 BUG();
2871 }
2872 /* XXX save filehandle here */
2873out:
J. Bruce Fieldsf3e42232011-08-24 12:27:31 -04002874 encode_seqid_op_tail(resp, save, nfserr);
Benny Halevy695e12f2008-07-04 14:38:33 +03002875 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876}
2877
Benny Halevy695e12f2008-07-04 14:38:33 +03002878static __be32
Al Virob37ad282006-10-19 23:28:59 -07002879nfsd4_encode_open_confirm(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880{
2881 ENCODE_SEQID_OP_HEAD;
Benny Halevye2f282b2008-08-12 20:45:07 +03002882
2883 if (!nfserr)
2884 nfsd4_encode_stateid(resp, &oc->oc_resp_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885
J. Bruce Fieldsf3e42232011-08-24 12:27:31 -04002886 encode_seqid_op_tail(resp, save, nfserr);
Benny Halevy695e12f2008-07-04 14:38:33 +03002887 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888}
2889
Benny Halevy695e12f2008-07-04 14:38:33 +03002890static __be32
Al Virob37ad282006-10-19 23:28:59 -07002891nfsd4_encode_open_downgrade(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892{
2893 ENCODE_SEQID_OP_HEAD;
Benny Halevye2f282b2008-08-12 20:45:07 +03002894
2895 if (!nfserr)
2896 nfsd4_encode_stateid(resp, &od->od_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897
J. Bruce Fieldsf3e42232011-08-24 12:27:31 -04002898 encode_seqid_op_tail(resp, save, nfserr);
Benny Halevy695e12f2008-07-04 14:38:33 +03002899 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900}
2901
Al Virob37ad282006-10-19 23:28:59 -07002902static __be32
2903nfsd4_encode_read(struct nfsd4_compoundres *resp, __be32 nfserr,
NeilBrown44524352006-10-04 02:15:46 -07002904 struct nfsd4_read *read)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905{
2906 u32 eof;
2907 int v, pn;
2908 unsigned long maxcount;
2909 long len;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002910 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911
2912 if (nfserr)
2913 return nfserr;
2914 if (resp->xbuf->page_len)
2915 return nfserr_resource;
2916
2917 RESERVE_SPACE(8); /* eof flag and byte count */
2918
Greg Banks7adae482006-10-04 02:15:47 -07002919 maxcount = svc_max_payload(resp->rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 if (maxcount > read->rd_length)
2921 maxcount = read->rd_length;
2922
2923 len = maxcount;
2924 v = 0;
2925 while (len > 0) {
J. Bruce Fields4cbb3d02012-12-04 18:25:10 -05002926 pn = resp->rqstp->rq_resused;
2927 if (!resp->rqstp->rq_respages[pn]) { /* ran out of pages */
2928 maxcount -= len;
2929 break;
2930 }
NeilBrown3cc03b12006-10-04 02:15:47 -07002931 resp->rqstp->rq_vec[v].iov_base =
NeilBrown44524352006-10-04 02:15:46 -07002932 page_address(resp->rqstp->rq_respages[pn]);
NeilBrown3cc03b12006-10-04 02:15:47 -07002933 resp->rqstp->rq_vec[v].iov_len =
NeilBrown44524352006-10-04 02:15:46 -07002934 len < PAGE_SIZE ? len : PAGE_SIZE;
J. Bruce Fields4cbb3d02012-12-04 18:25:10 -05002935 resp->rqstp->rq_resused++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 v++;
2937 len -= PAGE_SIZE;
2938 }
2939 read->rd_vlen = v;
2940
J. Bruce Fields039a87c2010-07-30 11:33:32 -04002941 nfserr = nfsd_read_file(read->rd_rqstp, read->rd_fhp, read->rd_filp,
NeilBrown3cc03b12006-10-04 02:15:47 -07002942 read->rd_offset, resp->rqstp->rq_vec, read->rd_vlen,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 &maxcount);
2944
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 if (nfserr)
2946 return nfserr;
NeilBrown44524352006-10-04 02:15:46 -07002947 eof = (read->rd_offset + maxcount >=
2948 read->rd_fhp->fh_dentry->d_inode->i_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949
2950 WRITE32(eof);
2951 WRITE32(maxcount);
2952 ADJUST_ARGS();
NeilBrown6ed6dec2006-04-10 22:55:32 -07002953 resp->xbuf->head[0].iov_len = (char*)p
2954 - (char*)resp->xbuf->head[0].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 resp->xbuf->page_len = maxcount;
2956
NeilBrown6ed6dec2006-04-10 22:55:32 -07002957 /* Use rest of head for padding and remaining ops: */
NeilBrown6ed6dec2006-04-10 22:55:32 -07002958 resp->xbuf->tail[0].iov_base = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 resp->xbuf->tail[0].iov_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 if (maxcount&3) {
NeilBrown6ed6dec2006-04-10 22:55:32 -07002961 RESERVE_SPACE(4);
2962 WRITE32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 resp->xbuf->tail[0].iov_base += maxcount&3;
2964 resp->xbuf->tail[0].iov_len = 4 - (maxcount&3);
NeilBrown6ed6dec2006-04-10 22:55:32 -07002965 ADJUST_ARGS();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 }
2967 return 0;
2968}
2969
Al Virob37ad282006-10-19 23:28:59 -07002970static __be32
2971nfsd4_encode_readlink(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_readlink *readlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972{
2973 int maxcount;
2974 char *page;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002975 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976
2977 if (nfserr)
2978 return nfserr;
2979 if (resp->xbuf->page_len)
2980 return nfserr_resource;
J. Bruce Fields4cbb3d02012-12-04 18:25:10 -05002981 if (!resp->rqstp->rq_respages[resp->rqstp->rq_resused])
2982 return nfserr_resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983
NeilBrown44524352006-10-04 02:15:46 -07002984 page = page_address(resp->rqstp->rq_respages[resp->rqstp->rq_resused++]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985
2986 maxcount = PAGE_SIZE;
2987 RESERVE_SPACE(4);
2988
2989 /*
2990 * XXX: By default, the ->readlink() VFS op will truncate symlinks
2991 * if they would overflow the buffer. Is this kosher in NFSv4? If
2992 * not, one easy fix is: if ->readlink() precisely fills the buffer,
2993 * assume that truncation occurred, and return NFS4ERR_RESOURCE.
2994 */
2995 nfserr = nfsd_readlink(readlink->rl_rqstp, readlink->rl_fhp, page, &maxcount);
2996 if (nfserr == nfserr_isdir)
2997 return nfserr_inval;
2998 if (nfserr)
2999 return nfserr;
3000
3001 WRITE32(maxcount);
3002 ADJUST_ARGS();
NeilBrown6ed6dec2006-04-10 22:55:32 -07003003 resp->xbuf->head[0].iov_len = (char*)p
3004 - (char*)resp->xbuf->head[0].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 resp->xbuf->page_len = maxcount;
NeilBrown6ed6dec2006-04-10 22:55:32 -07003006
3007 /* Use rest of head for padding and remaining ops: */
NeilBrown6ed6dec2006-04-10 22:55:32 -07003008 resp->xbuf->tail[0].iov_base = p;
3009 resp->xbuf->tail[0].iov_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 if (maxcount&3) {
NeilBrown6ed6dec2006-04-10 22:55:32 -07003011 RESERVE_SPACE(4);
3012 WRITE32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 resp->xbuf->tail[0].iov_base += maxcount&3;
3014 resp->xbuf->tail[0].iov_len = 4 - (maxcount&3);
NeilBrown6ed6dec2006-04-10 22:55:32 -07003015 ADJUST_ARGS();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 }
3017 return 0;
3018}
3019
Al Virob37ad282006-10-19 23:28:59 -07003020static __be32
3021nfsd4_encode_readdir(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_readdir *readdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022{
3023 int maxcount;
3024 loff_t offset;
Al Viro2ebbc012006-10-19 23:28:58 -07003025 __be32 *page, *savep, *tailbase;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003026 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027
3028 if (nfserr)
3029 return nfserr;
3030 if (resp->xbuf->page_len)
3031 return nfserr_resource;
J. Bruce Fields4cbb3d02012-12-04 18:25:10 -05003032 if (!resp->rqstp->rq_respages[resp->rqstp->rq_resused])
3033 return nfserr_resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034
Chuck Leverab4684d2012-03-02 17:13:50 -05003035 RESERVE_SPACE(NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 savep = p;
3037
3038 /* XXX: Following NFSv3, we ignore the READDIR verifier for now. */
3039 WRITE32(0);
3040 WRITE32(0);
3041 ADJUST_ARGS();
3042 resp->xbuf->head[0].iov_len = ((char*)resp->p) - (char*)resp->xbuf->head[0].iov_base;
NeilBrownbb6e8a92006-04-10 22:55:33 -07003043 tailbase = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044
3045 maxcount = PAGE_SIZE;
3046 if (maxcount > readdir->rd_maxcount)
3047 maxcount = readdir->rd_maxcount;
3048
3049 /*
3050 * Convert from bytes to words, account for the two words already
3051 * written, make sure to leave two words at the end for the next
3052 * pointer and eof field.
3053 */
3054 maxcount = (maxcount >> 2) - 4;
3055 if (maxcount < 0) {
3056 nfserr = nfserr_toosmall;
3057 goto err_no_verf;
3058 }
3059
NeilBrown44524352006-10-04 02:15:46 -07003060 page = page_address(resp->rqstp->rq_respages[resp->rqstp->rq_resused++]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 readdir->common.err = 0;
3062 readdir->buflen = maxcount;
3063 readdir->buffer = page;
3064 readdir->offset = NULL;
3065
3066 offset = readdir->rd_cookie;
3067 nfserr = nfsd_readdir(readdir->rd_rqstp, readdir->rd_fhp,
3068 &offset,
3069 &readdir->common, nfsd4_encode_dirent);
3070 if (nfserr == nfs_ok &&
3071 readdir->common.err == nfserr_toosmall &&
3072 readdir->buffer == page)
3073 nfserr = nfserr_toosmall;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 if (nfserr)
3075 goto err_no_verf;
3076
3077 if (readdir->offset)
3078 xdr_encode_hyper(readdir->offset, offset);
3079
3080 p = readdir->buffer;
3081 *p++ = 0; /* no more entries */
3082 *p++ = htonl(readdir->common.err == nfserr_eof);
NeilBrown44524352006-10-04 02:15:46 -07003083 resp->xbuf->page_len = ((char*)p) - (char*)page_address(
3084 resp->rqstp->rq_respages[resp->rqstp->rq_resused-1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085
NeilBrownbb6e8a92006-04-10 22:55:33 -07003086 /* Use rest of head for padding and remaining ops: */
NeilBrownbb6e8a92006-04-10 22:55:33 -07003087 resp->xbuf->tail[0].iov_base = tailbase;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 resp->xbuf->tail[0].iov_len = 0;
3089 resp->p = resp->xbuf->tail[0].iov_base;
NeilBrownbb6e8a92006-04-10 22:55:33 -07003090 resp->end = resp->p + (PAGE_SIZE - resp->xbuf->head[0].iov_len)/4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091
3092 return 0;
3093err_no_verf:
3094 p = savep;
3095 ADJUST_ARGS();
3096 return nfserr;
3097}
3098
Benny Halevy695e12f2008-07-04 14:38:33 +03003099static __be32
Al Virob37ad282006-10-19 23:28:59 -07003100nfsd4_encode_remove(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_remove *remove)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003102 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103
3104 if (!nfserr) {
3105 RESERVE_SPACE(20);
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04003106 write_cinfo(&p, &remove->rm_cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 ADJUST_ARGS();
3108 }
Benny Halevy695e12f2008-07-04 14:38:33 +03003109 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110}
3111
Benny Halevy695e12f2008-07-04 14:38:33 +03003112static __be32
Al Virob37ad282006-10-19 23:28:59 -07003113nfsd4_encode_rename(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_rename *rename)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003115 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116
3117 if (!nfserr) {
3118 RESERVE_SPACE(40);
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04003119 write_cinfo(&p, &rename->rn_sinfo);
3120 write_cinfo(&p, &rename->rn_tinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 ADJUST_ARGS();
3122 }
Benny Halevy695e12f2008-07-04 14:38:33 +03003123 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124}
3125
Benny Halevy695e12f2008-07-04 14:38:33 +03003126static __be32
Mi Jinlong22b6dee2010-12-27 14:29:57 +08003127nfsd4_do_encode_secinfo(struct nfsd4_compoundres *resp,
3128 __be32 nfserr,struct svc_export *exp)
Andy Adamsondcb488a2007-07-17 04:04:51 -07003129{
3130 int i = 0;
J. Bruce Fields4796f452007-07-17 04:04:51 -07003131 u32 nflavs;
3132 struct exp_flavor_info *flavs;
3133 struct exp_flavor_info def_flavs[2];
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003134 __be32 *p;
Andy Adamsondcb488a2007-07-17 04:04:51 -07003135
3136 if (nfserr)
3137 goto out;
J. Bruce Fields4796f452007-07-17 04:04:51 -07003138 if (exp->ex_nflavors) {
3139 flavs = exp->ex_flavors;
3140 nflavs = exp->ex_nflavors;
3141 } else { /* Handling of some defaults in absence of real secinfo: */
3142 flavs = def_flavs;
3143 if (exp->ex_client->flavour->flavour == RPC_AUTH_UNIX) {
3144 nflavs = 2;
3145 flavs[0].pseudoflavor = RPC_AUTH_UNIX;
3146 flavs[1].pseudoflavor = RPC_AUTH_NULL;
3147 } else if (exp->ex_client->flavour->flavour == RPC_AUTH_GSS) {
3148 nflavs = 1;
3149 flavs[0].pseudoflavor
3150 = svcauth_gss_flavor(exp->ex_client);
3151 } else {
3152 nflavs = 1;
3153 flavs[0].pseudoflavor
3154 = exp->ex_client->flavour->flavour;
3155 }
3156 }
3157
Andy Adamsondcb488a2007-07-17 04:04:51 -07003158 RESERVE_SPACE(4);
J. Bruce Fields4796f452007-07-17 04:04:51 -07003159 WRITE32(nflavs);
Andy Adamsondcb488a2007-07-17 04:04:51 -07003160 ADJUST_ARGS();
J. Bruce Fields4796f452007-07-17 04:04:51 -07003161 for (i = 0; i < nflavs; i++) {
3162 u32 flav = flavs[i].pseudoflavor;
Andy Adamsondcb488a2007-07-17 04:04:51 -07003163 struct gss_api_mech *gm = gss_mech_get_by_pseudoflavor(flav);
3164
3165 if (gm) {
3166 RESERVE_SPACE(4);
3167 WRITE32(RPC_AUTH_GSS);
3168 ADJUST_ARGS();
3169 RESERVE_SPACE(4 + gm->gm_oid.len);
3170 WRITE32(gm->gm_oid.len);
3171 WRITEMEM(gm->gm_oid.data, gm->gm_oid.len);
3172 ADJUST_ARGS();
3173 RESERVE_SPACE(4);
3174 WRITE32(0); /* qop */
3175 ADJUST_ARGS();
3176 RESERVE_SPACE(4);
3177 WRITE32(gss_pseudoflavor_to_service(gm, flav));
3178 ADJUST_ARGS();
3179 gss_mech_put(gm);
3180 } else {
3181 RESERVE_SPACE(4);
3182 WRITE32(flav);
3183 ADJUST_ARGS();
3184 }
3185 }
3186out:
3187 if (exp)
3188 exp_put(exp);
Benny Halevy695e12f2008-07-04 14:38:33 +03003189 return nfserr;
Andy Adamsondcb488a2007-07-17 04:04:51 -07003190}
3191
Mi Jinlong22b6dee2010-12-27 14:29:57 +08003192static __be32
3193nfsd4_encode_secinfo(struct nfsd4_compoundres *resp, __be32 nfserr,
3194 struct nfsd4_secinfo *secinfo)
3195{
3196 return nfsd4_do_encode_secinfo(resp, nfserr, secinfo->si_exp);
3197}
3198
3199static __be32
3200nfsd4_encode_secinfo_no_name(struct nfsd4_compoundres *resp, __be32 nfserr,
3201 struct nfsd4_secinfo_no_name *secinfo)
3202{
3203 return nfsd4_do_encode_secinfo(resp, nfserr, secinfo->sin_exp);
3204}
3205
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206/*
3207 * The SETATTR encode routine is special -- it always encodes a bitmap,
3208 * regardless of the error status.
3209 */
Benny Halevy695e12f2008-07-04 14:38:33 +03003210static __be32
Al Virob37ad282006-10-19 23:28:59 -07003211nfsd4_encode_setattr(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_setattr *setattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003213 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214
3215 RESERVE_SPACE(12);
3216 if (nfserr) {
3217 WRITE32(2);
3218 WRITE32(0);
3219 WRITE32(0);
3220 }
3221 else {
3222 WRITE32(2);
3223 WRITE32(setattr->sa_bmval[0]);
3224 WRITE32(setattr->sa_bmval[1]);
3225 }
3226 ADJUST_ARGS();
Benny Halevy695e12f2008-07-04 14:38:33 +03003227 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228}
3229
Benny Halevy695e12f2008-07-04 14:38:33 +03003230static __be32
Al Virob37ad282006-10-19 23:28:59 -07003231nfsd4_encode_setclientid(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_setclientid *scd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003233 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234
3235 if (!nfserr) {
Chuck Leverab4684d2012-03-02 17:13:50 -05003236 RESERVE_SPACE(8 + NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 WRITEMEM(&scd->se_clientid, 8);
Chuck Leverab4684d2012-03-02 17:13:50 -05003238 WRITEMEM(&scd->se_confirm, NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239 ADJUST_ARGS();
3240 }
3241 else if (nfserr == nfserr_clid_inuse) {
3242 RESERVE_SPACE(8);
3243 WRITE32(0);
3244 WRITE32(0);
3245 ADJUST_ARGS();
3246 }
Benny Halevy695e12f2008-07-04 14:38:33 +03003247 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248}
3249
Benny Halevy695e12f2008-07-04 14:38:33 +03003250static __be32
Al Virob37ad282006-10-19 23:28:59 -07003251nfsd4_encode_write(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_write *write)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003253 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254
3255 if (!nfserr) {
3256 RESERVE_SPACE(16);
3257 WRITE32(write->wr_bytes_written);
3258 WRITE32(write->wr_how_written);
Chuck Leverab4684d2012-03-02 17:13:50 -05003259 WRITEMEM(write->wr_verifier.data, NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260 ADJUST_ARGS();
3261 }
Benny Halevy695e12f2008-07-04 14:38:33 +03003262 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263}
3264
Benny Halevy695e12f2008-07-04 14:38:33 +03003265static __be32
Andy Adamson2db134e2009-04-03 08:27:55 +03003266nfsd4_encode_exchange_id(struct nfsd4_compoundres *resp, int nfserr,
3267 struct nfsd4_exchange_id *exid)
3268{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003269 __be32 *p;
Andy Adamson0733d212009-04-03 08:28:01 +03003270 char *major_id;
3271 char *server_scope;
3272 int major_id_sz;
3273 int server_scope_sz;
3274 uint64_t minor_id = 0;
3275
3276 if (nfserr)
3277 return nfserr;
3278
3279 major_id = utsname()->nodename;
3280 major_id_sz = strlen(major_id);
3281 server_scope = utsname()->nodename;
3282 server_scope_sz = strlen(server_scope);
3283
3284 RESERVE_SPACE(
3285 8 /* eir_clientid */ +
3286 4 /* eir_sequenceid */ +
3287 4 /* eir_flags */ +
3288 4 /* spr_how (SP4_NONE) */ +
3289 8 /* so_minor_id */ +
3290 4 /* so_major_id.len */ +
3291 (XDR_QUADLEN(major_id_sz) * 4) +
3292 4 /* eir_server_scope.len */ +
3293 (XDR_QUADLEN(server_scope_sz) * 4) +
3294 4 /* eir_server_impl_id.count (0) */);
3295
3296 WRITEMEM(&exid->clientid, 8);
3297 WRITE32(exid->seqid);
3298 WRITE32(exid->flags);
3299
3300 /* state_protect4_r. Currently only support SP4_NONE */
3301 BUG_ON(exid->spa_how != SP4_NONE);
3302 WRITE32(exid->spa_how);
3303
3304 /* The server_owner struct */
3305 WRITE64(minor_id); /* Minor id */
3306 /* major id */
3307 WRITE32(major_id_sz);
3308 WRITEMEM(major_id, major_id_sz);
3309
3310 /* Server scope */
3311 WRITE32(server_scope_sz);
3312 WRITEMEM(server_scope, server_scope_sz);
3313
3314 /* Implementation id */
3315 WRITE32(0); /* zero length nfs_impl_id4 array */
3316 ADJUST_ARGS();
3317 return 0;
Andy Adamson2db134e2009-04-03 08:27:55 +03003318}
3319
3320static __be32
3321nfsd4_encode_create_session(struct nfsd4_compoundres *resp, int nfserr,
3322 struct nfsd4_create_session *sess)
3323{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003324 __be32 *p;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003325
3326 if (nfserr)
3327 return nfserr;
3328
3329 RESERVE_SPACE(24);
3330 WRITEMEM(sess->sessionid.data, NFS4_MAX_SESSIONID_LEN);
3331 WRITE32(sess->seqid);
3332 WRITE32(sess->flags);
3333 ADJUST_ARGS();
3334
3335 RESERVE_SPACE(28);
3336 WRITE32(0); /* headerpadsz */
3337 WRITE32(sess->fore_channel.maxreq_sz);
3338 WRITE32(sess->fore_channel.maxresp_sz);
3339 WRITE32(sess->fore_channel.maxresp_cached);
3340 WRITE32(sess->fore_channel.maxops);
3341 WRITE32(sess->fore_channel.maxreqs);
3342 WRITE32(sess->fore_channel.nr_rdma_attrs);
3343 ADJUST_ARGS();
3344
3345 if (sess->fore_channel.nr_rdma_attrs) {
3346 RESERVE_SPACE(4);
3347 WRITE32(sess->fore_channel.rdma_attrs);
3348 ADJUST_ARGS();
3349 }
3350
3351 RESERVE_SPACE(28);
3352 WRITE32(0); /* headerpadsz */
3353 WRITE32(sess->back_channel.maxreq_sz);
3354 WRITE32(sess->back_channel.maxresp_sz);
3355 WRITE32(sess->back_channel.maxresp_cached);
3356 WRITE32(sess->back_channel.maxops);
3357 WRITE32(sess->back_channel.maxreqs);
3358 WRITE32(sess->back_channel.nr_rdma_attrs);
3359 ADJUST_ARGS();
3360
3361 if (sess->back_channel.nr_rdma_attrs) {
3362 RESERVE_SPACE(4);
3363 WRITE32(sess->back_channel.rdma_attrs);
3364 ADJUST_ARGS();
3365 }
3366 return 0;
Andy Adamson2db134e2009-04-03 08:27:55 +03003367}
3368
3369static __be32
3370nfsd4_encode_destroy_session(struct nfsd4_compoundres *resp, int nfserr,
3371 struct nfsd4_destroy_session *destroy_session)
3372{
Andy Adamson2db134e2009-04-03 08:27:55 +03003373 return nfserr;
3374}
3375
Daniel Mackc47d8322011-05-16 16:38:14 +02003376static __be32
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003377nfsd4_encode_free_stateid(struct nfsd4_compoundres *resp, int nfserr,
3378 struct nfsd4_free_stateid *free_stateid)
3379{
3380 __be32 *p;
3381
3382 if (nfserr)
3383 return nfserr;
3384
3385 RESERVE_SPACE(4);
3386 WRITE32(nfserr);
3387 ADJUST_ARGS();
3388 return nfserr;
3389}
3390
3391static __be32
Andy Adamson2db134e2009-04-03 08:27:55 +03003392nfsd4_encode_sequence(struct nfsd4_compoundres *resp, int nfserr,
3393 struct nfsd4_sequence *seq)
3394{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003395 __be32 *p;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003396
3397 if (nfserr)
3398 return nfserr;
3399
3400 RESERVE_SPACE(NFS4_MAX_SESSIONID_LEN + 20);
3401 WRITEMEM(seq->sessionid.data, NFS4_MAX_SESSIONID_LEN);
3402 WRITE32(seq->seqid);
3403 WRITE32(seq->slotid);
J. Bruce Fieldsb7d7ca32011-08-31 15:39:30 -04003404 /* Note slotid's are numbered from zero: */
3405 WRITE32(seq->maxslots - 1); /* sr_highest_slotid */
3406 WRITE32(seq->maxslots - 1); /* sr_target_highest_slotid */
J. Bruce Fields0d7bb712010-11-18 08:30:33 -05003407 WRITE32(seq->status_flags);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003408
3409 ADJUST_ARGS();
Andy Adamson557ce262009-08-28 08:45:04 -04003410 resp->cstate.datap = p; /* DRC cache data pointer */
Benny Halevyb85d4c02009-04-03 08:28:08 +03003411 return 0;
Andy Adamson2db134e2009-04-03 08:27:55 +03003412}
3413
Bryan Schumaker17456802011-07-13 10:50:48 -04003414__be32
3415nfsd4_encode_test_stateid(struct nfsd4_compoundres *resp, int nfserr,
3416 struct nfsd4_test_stateid *test_stateid)
3417{
Bryan Schumaker03cfb422012-01-27 10:22:49 -05003418 struct nfsd4_test_stateid_id *stateid, *next;
Bryan Schumaker17456802011-07-13 10:50:48 -04003419 __be32 *p;
Bryan Schumaker17456802011-07-13 10:50:48 -04003420
Bryan Schumaker03cfb422012-01-27 10:22:49 -05003421 RESERVE_SPACE(4 + (4 * test_stateid->ts_num_ids));
Bryan Schumaker17456802011-07-13 10:50:48 -04003422 *p++ = htonl(test_stateid->ts_num_ids);
Bryan Schumaker17456802011-07-13 10:50:48 -04003423
Bryan Schumaker03cfb422012-01-27 10:22:49 -05003424 list_for_each_entry_safe(stateid, next, &test_stateid->ts_stateid_list, ts_id_list) {
Al Viro02f5fde2012-04-13 00:10:34 -04003425 *p++ = stateid->ts_id_status;
Bryan Schumaker17456802011-07-13 10:50:48 -04003426 }
Bryan Schumaker17456802011-07-13 10:50:48 -04003427
Bryan Schumaker03cfb422012-01-27 10:22:49 -05003428 ADJUST_ARGS();
Bryan Schumaker17456802011-07-13 10:50:48 -04003429 return nfserr;
3430}
3431
Andy Adamson2db134e2009-04-03 08:27:55 +03003432static __be32
Benny Halevy695e12f2008-07-04 14:38:33 +03003433nfsd4_encode_noop(struct nfsd4_compoundres *resp, __be32 nfserr, void *p)
3434{
3435 return nfserr;
3436}
3437
3438typedef __be32(* nfsd4_enc)(struct nfsd4_compoundres *, __be32, void *);
3439
Andy Adamson2db134e2009-04-03 08:27:55 +03003440/*
3441 * Note: nfsd4_enc_ops vector is shared for v4.0 and v4.1
3442 * since we don't need to filter out obsolete ops as this is
3443 * done in the decoding phase.
3444 */
Benny Halevy695e12f2008-07-04 14:38:33 +03003445static nfsd4_enc nfsd4_enc_ops[] = {
J. Bruce Fieldsad1060c2008-07-18 15:04:16 -04003446 [OP_ACCESS] = (nfsd4_enc)nfsd4_encode_access,
3447 [OP_CLOSE] = (nfsd4_enc)nfsd4_encode_close,
3448 [OP_COMMIT] = (nfsd4_enc)nfsd4_encode_commit,
3449 [OP_CREATE] = (nfsd4_enc)nfsd4_encode_create,
3450 [OP_DELEGPURGE] = (nfsd4_enc)nfsd4_encode_noop,
3451 [OP_DELEGRETURN] = (nfsd4_enc)nfsd4_encode_noop,
3452 [OP_GETATTR] = (nfsd4_enc)nfsd4_encode_getattr,
3453 [OP_GETFH] = (nfsd4_enc)nfsd4_encode_getfh,
3454 [OP_LINK] = (nfsd4_enc)nfsd4_encode_link,
3455 [OP_LOCK] = (nfsd4_enc)nfsd4_encode_lock,
3456 [OP_LOCKT] = (nfsd4_enc)nfsd4_encode_lockt,
3457 [OP_LOCKU] = (nfsd4_enc)nfsd4_encode_locku,
3458 [OP_LOOKUP] = (nfsd4_enc)nfsd4_encode_noop,
3459 [OP_LOOKUPP] = (nfsd4_enc)nfsd4_encode_noop,
3460 [OP_NVERIFY] = (nfsd4_enc)nfsd4_encode_noop,
3461 [OP_OPEN] = (nfsd4_enc)nfsd4_encode_open,
Benny Halevy84f09f42009-03-04 23:05:35 +02003462 [OP_OPENATTR] = (nfsd4_enc)nfsd4_encode_noop,
J. Bruce Fieldsad1060c2008-07-18 15:04:16 -04003463 [OP_OPEN_CONFIRM] = (nfsd4_enc)nfsd4_encode_open_confirm,
3464 [OP_OPEN_DOWNGRADE] = (nfsd4_enc)nfsd4_encode_open_downgrade,
3465 [OP_PUTFH] = (nfsd4_enc)nfsd4_encode_noop,
3466 [OP_PUTPUBFH] = (nfsd4_enc)nfsd4_encode_noop,
3467 [OP_PUTROOTFH] = (nfsd4_enc)nfsd4_encode_noop,
3468 [OP_READ] = (nfsd4_enc)nfsd4_encode_read,
3469 [OP_READDIR] = (nfsd4_enc)nfsd4_encode_readdir,
3470 [OP_READLINK] = (nfsd4_enc)nfsd4_encode_readlink,
3471 [OP_REMOVE] = (nfsd4_enc)nfsd4_encode_remove,
3472 [OP_RENAME] = (nfsd4_enc)nfsd4_encode_rename,
3473 [OP_RENEW] = (nfsd4_enc)nfsd4_encode_noop,
3474 [OP_RESTOREFH] = (nfsd4_enc)nfsd4_encode_noop,
3475 [OP_SAVEFH] = (nfsd4_enc)nfsd4_encode_noop,
3476 [OP_SECINFO] = (nfsd4_enc)nfsd4_encode_secinfo,
3477 [OP_SETATTR] = (nfsd4_enc)nfsd4_encode_setattr,
3478 [OP_SETCLIENTID] = (nfsd4_enc)nfsd4_encode_setclientid,
3479 [OP_SETCLIENTID_CONFIRM] = (nfsd4_enc)nfsd4_encode_noop,
3480 [OP_VERIFY] = (nfsd4_enc)nfsd4_encode_noop,
3481 [OP_WRITE] = (nfsd4_enc)nfsd4_encode_write,
3482 [OP_RELEASE_LOCKOWNER] = (nfsd4_enc)nfsd4_encode_noop,
Andy Adamson2db134e2009-04-03 08:27:55 +03003483
3484 /* NFSv4.1 operations */
3485 [OP_BACKCHANNEL_CTL] = (nfsd4_enc)nfsd4_encode_noop,
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003486 [OP_BIND_CONN_TO_SESSION] = (nfsd4_enc)nfsd4_encode_bind_conn_to_session,
Andy Adamson2db134e2009-04-03 08:27:55 +03003487 [OP_EXCHANGE_ID] = (nfsd4_enc)nfsd4_encode_exchange_id,
3488 [OP_CREATE_SESSION] = (nfsd4_enc)nfsd4_encode_create_session,
3489 [OP_DESTROY_SESSION] = (nfsd4_enc)nfsd4_encode_destroy_session,
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003490 [OP_FREE_STATEID] = (nfsd4_enc)nfsd4_encode_free_stateid,
Andy Adamson2db134e2009-04-03 08:27:55 +03003491 [OP_GET_DIR_DELEGATION] = (nfsd4_enc)nfsd4_encode_noop,
3492 [OP_GETDEVICEINFO] = (nfsd4_enc)nfsd4_encode_noop,
3493 [OP_GETDEVICELIST] = (nfsd4_enc)nfsd4_encode_noop,
3494 [OP_LAYOUTCOMMIT] = (nfsd4_enc)nfsd4_encode_noop,
3495 [OP_LAYOUTGET] = (nfsd4_enc)nfsd4_encode_noop,
3496 [OP_LAYOUTRETURN] = (nfsd4_enc)nfsd4_encode_noop,
Mi Jinlong22b6dee2010-12-27 14:29:57 +08003497 [OP_SECINFO_NO_NAME] = (nfsd4_enc)nfsd4_encode_secinfo_no_name,
Andy Adamson2db134e2009-04-03 08:27:55 +03003498 [OP_SEQUENCE] = (nfsd4_enc)nfsd4_encode_sequence,
3499 [OP_SET_SSV] = (nfsd4_enc)nfsd4_encode_noop,
Bryan Schumaker17456802011-07-13 10:50:48 -04003500 [OP_TEST_STATEID] = (nfsd4_enc)nfsd4_encode_test_stateid,
Andy Adamson2db134e2009-04-03 08:27:55 +03003501 [OP_WANT_DELEGATION] = (nfsd4_enc)nfsd4_encode_noop,
3502 [OP_DESTROY_CLIENTID] = (nfsd4_enc)nfsd4_encode_noop,
3503 [OP_RECLAIM_COMPLETE] = (nfsd4_enc)nfsd4_encode_noop,
Benny Halevy695e12f2008-07-04 14:38:33 +03003504};
3505
Andy Adamson496c2622009-04-03 08:28:48 +03003506/*
3507 * Calculate the total amount of memory that the compound response has taken
Mi Jinlong58e7b332011-08-28 18:18:56 +08003508 * after encoding the current operation with pad.
Andy Adamson496c2622009-04-03 08:28:48 +03003509 *
Mi Jinlong58e7b332011-08-28 18:18:56 +08003510 * pad: if operation is non-idempotent, pad was calculate by op_rsize_bop()
3511 * which was specified at nfsd4_operation, else pad is zero.
Andy Adamson496c2622009-04-03 08:28:48 +03003512 *
Mi Jinlong58e7b332011-08-28 18:18:56 +08003513 * Compare this length to the session se_fmaxresp_sz and se_fmaxresp_cached.
Andy Adamson496c2622009-04-03 08:28:48 +03003514 *
3515 * Our se_fmaxresp_cached will always be a multiple of PAGE_SIZE, and so
3516 * will be at least a page and will therefore hold the xdr_buf head.
3517 */
Mi Jinlong58e7b332011-08-28 18:18:56 +08003518int nfsd4_check_resp_size(struct nfsd4_compoundres *resp, u32 pad)
Andy Adamson496c2622009-04-03 08:28:48 +03003519{
Andy Adamson496c2622009-04-03 08:28:48 +03003520 struct xdr_buf *xb = &resp->rqstp->rq_res;
Andy Adamson496c2622009-04-03 08:28:48 +03003521 struct nfsd4_session *session = NULL;
3522 struct nfsd4_slot *slot = resp->cstate.slot;
Mi Jinlong58e7b332011-08-28 18:18:56 +08003523 u32 length, tlen = 0;
Andy Adamson496c2622009-04-03 08:28:48 +03003524
3525 if (!nfsd4_has_session(&resp->cstate))
Mi Jinlong58e7b332011-08-28 18:18:56 +08003526 return 0;
Andy Adamson496c2622009-04-03 08:28:48 +03003527
3528 session = resp->cstate.session;
Mi Jinlong58e7b332011-08-28 18:18:56 +08003529 if (session == NULL)
3530 return 0;
Andy Adamson496c2622009-04-03 08:28:48 +03003531
3532 if (xb->page_len == 0) {
3533 length = (char *)resp->p - (char *)xb->head[0].iov_base + pad;
3534 } else {
3535 if (xb->tail[0].iov_base && xb->tail[0].iov_len > 0)
3536 tlen = (char *)resp->p - (char *)xb->tail[0].iov_base;
3537
3538 length = xb->head[0].iov_len + xb->page_len + tlen + pad;
3539 }
3540 dprintk("%s length %u, xb->page_len %u tlen %u pad %u\n", __func__,
3541 length, xb->page_len, tlen, pad);
3542
Mi Jinlong58e7b332011-08-28 18:18:56 +08003543 if (length > session->se_fchannel.maxresp_sz)
3544 return nfserr_rep_too_big;
3545
J. Bruce Fields73e79482012-02-13 16:39:00 -05003546 if ((slot->sl_flags & NFSD4_SLOT_CACHETHIS) &&
Mi Jinlong58e7b332011-08-28 18:18:56 +08003547 length > session->se_fchannel.maxresp_cached)
Andy Adamson496c2622009-04-03 08:28:48 +03003548 return nfserr_rep_too_big_to_cache;
Mi Jinlong58e7b332011-08-28 18:18:56 +08003549
3550 return 0;
Andy Adamson496c2622009-04-03 08:28:48 +03003551}
3552
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553void
3554nfsd4_encode_operation(struct nfsd4_compoundres *resp, struct nfsd4_op *op)
3555{
Al Viro2ebbc012006-10-19 23:28:58 -07003556 __be32 *statp;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003557 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558
3559 RESERVE_SPACE(8);
3560 WRITE32(op->opnum);
3561 statp = p++; /* to be backfilled at the end */
3562 ADJUST_ARGS();
3563
Benny Halevy695e12f2008-07-04 14:38:33 +03003564 if (op->opnum == OP_ILLEGAL)
3565 goto status;
3566 BUG_ON(op->opnum < 0 || op->opnum >= ARRAY_SIZE(nfsd4_enc_ops) ||
3567 !nfsd4_enc_ops[op->opnum]);
3568 op->status = nfsd4_enc_ops[op->opnum](resp, op->status, &op->u);
Andy Adamson496c2622009-04-03 08:28:48 +03003569 /* nfsd4_check_drc_limit guarantees enough room for error status */
Mi Jinlong58e7b332011-08-28 18:18:56 +08003570 if (!op->status)
3571 op->status = nfsd4_check_resp_size(resp, 0);
Benny Halevy695e12f2008-07-04 14:38:33 +03003572status:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573 /*
3574 * Note: We write the status directly, instead of using WRITE32(),
3575 * since it is already in network byte order.
3576 */
3577 *statp = op->status;
3578}
3579
3580/*
3581 * Encode the reply stored in the stateowner reply cache
3582 *
3583 * XDR note: do not encode rp->rp_buflen: the buffer contains the
3584 * previously sent already encoded operation.
3585 *
3586 * called with nfs4_lock_state() held
3587 */
3588void
3589nfsd4_encode_replay(struct nfsd4_compoundres *resp, struct nfsd4_op *op)
3590{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003591 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592 struct nfs4_replay *rp = op->replay;
3593
3594 BUG_ON(!rp);
3595
3596 RESERVE_SPACE(8);
3597 WRITE32(op->opnum);
3598 *p++ = rp->rp_status; /* already xdr'ed */
3599 ADJUST_ARGS();
3600
3601 RESERVE_SPACE(rp->rp_buflen);
3602 WRITEMEM(rp->rp_buf, rp->rp_buflen);
3603 ADJUST_ARGS();
3604}
3605
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606int
Al Viro2ebbc012006-10-19 23:28:58 -07003607nfs4svc_encode_voidres(struct svc_rqst *rqstp, __be32 *p, void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608{
3609 return xdr_ressize_check(rqstp, p);
3610}
3611
J. Bruce Fields3e98abf2011-07-16 17:15:10 -04003612int nfsd4_release_compoundargs(void *rq, __be32 *p, void *resp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613{
J. Bruce Fields3e98abf2011-07-16 17:15:10 -04003614 struct svc_rqst *rqstp = rq;
3615 struct nfsd4_compoundargs *args = rqstp->rq_argp;
3616
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617 if (args->ops != args->iops) {
3618 kfree(args->ops);
3619 args->ops = args->iops;
3620 }
Jesper Juhlf99d49a2005-11-07 01:01:34 -08003621 kfree(args->tmpp);
3622 args->tmpp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623 while (args->to_free) {
3624 struct tmpbuf *tb = args->to_free;
3625 args->to_free = tb->next;
3626 tb->release(tb->buf);
3627 kfree(tb);
3628 }
J. Bruce Fields3e98abf2011-07-16 17:15:10 -04003629 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630}
3631
3632int
Al Viro2ebbc012006-10-19 23:28:58 -07003633nfs4svc_decode_compoundargs(struct svc_rqst *rqstp, __be32 *p, struct nfsd4_compoundargs *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635 args->p = p;
3636 args->end = rqstp->rq_arg.head[0].iov_base + rqstp->rq_arg.head[0].iov_len;
3637 args->pagelist = rqstp->rq_arg.pages;
3638 args->pagelen = rqstp->rq_arg.page_len;
3639 args->tmpp = NULL;
3640 args->to_free = NULL;
3641 args->ops = args->iops;
3642 args->rqstp = rqstp;
3643
J. Bruce Fields3e98abf2011-07-16 17:15:10 -04003644 return !nfsd4_decode_compound(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645}
3646
3647int
Al Viro2ebbc012006-10-19 23:28:58 -07003648nfs4svc_encode_compoundres(struct svc_rqst *rqstp, __be32 *p, struct nfsd4_compoundres *resp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649{
3650 /*
3651 * All that remains is to write the tag and operation count...
3652 */
Andy Adamson557ce262009-08-28 08:45:04 -04003653 struct nfsd4_compound_state *cs = &resp->cstate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654 struct kvec *iov;
3655 p = resp->tagp;
3656 *p++ = htonl(resp->taglen);
3657 memcpy(p, resp->tag, resp->taglen);
3658 p += XDR_QUADLEN(resp->taglen);
3659 *p++ = htonl(resp->opcnt);
3660
3661 if (rqstp->rq_res.page_len)
3662 iov = &rqstp->rq_res.tail[0];
3663 else
3664 iov = &rqstp->rq_res.head[0];
3665 iov->iov_len = ((char*)resp->p) - (char*)iov->iov_base;
3666 BUG_ON(iov->iov_len > PAGE_SIZE);
J. Bruce Fields26c0c752010-04-24 15:35:43 -04003667 if (nfsd4_has_session(cs)) {
3668 if (cs->status != nfserr_replay_cache) {
3669 nfsd4_store_cache_entry(resp);
J. Bruce Fields73e79482012-02-13 16:39:00 -05003670 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
J. Bruce Fields26c0c752010-04-24 15:35:43 -04003671 }
Benny Halevyd7682982010-05-12 00:13:54 +03003672 /* Renew the clientid on success and on replay */
3673 release_session_client(cs->session);
J. Bruce Fields76407f72010-06-22 14:10:14 -04003674 nfsd4_put_session(cs->session);
Andy Adamsonda3846a2009-04-03 08:28:22 +03003675 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676 return 1;
3677}
3678
3679/*
3680 * Local variables:
3681 * c-basic-offset: 8
3682 * End:
3683 */