blob: 3c342960b712192552ca833330a99c9bfc5810c9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Server-side XDR for NFSv4
3 *
4 * Copyright (c) 2002 The Regents of the University of Michigan.
5 * All rights reserved.
6 *
7 * Kendrick Smith <kmsmith@umich.edu>
8 * Andy Adamson <andros@umich.edu>
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 *
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
24 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
30 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *
35 * TODO: Neil Brown made the following observation: We currently
36 * initially reserve NFSD_BUFSIZE space on the transmit queue and
37 * never release any of that until the request is complete.
38 * It would be good to calculate a new maximum response size while
39 * decoding the COMPOUND, and call svc_reserve with this number
40 * at the end of nfs4svc_decode_compoundargs.
41 */
42
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090043#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/namei.h>
Boaz Harrosh341eb182009-12-03 20:29:12 +020045#include <linux/statfs.h>
Andy Adamson0733d212009-04-03 08:28:01 +030046#include <linux/utsname.h>
Bryan Schumaker17456802011-07-13 10:50:48 -040047#include <linux/pagemap.h>
J. Bruce Fields4796f452007-07-17 04:04:51 -070048#include <linux/sunrpc/svcauth_gss.h>
Boaz Harrosh9a74af22009-12-03 20:30:56 +020049
J. Bruce Fields2ca72e12011-01-04 17:37:15 -050050#include "idmap.h"
51#include "acl.h"
Boaz Harrosh9a74af22009-12-03 20:30:56 +020052#include "xdr4.h"
J. Bruce Fields0a3adad2009-11-04 18:12:35 -050053#include "vfs.h"
Bryan Schumaker17456802011-07-13 10:50:48 -040054#include "state.h"
J. Bruce Fields10910062011-01-24 12:11:02 -050055#include "cache.h"
J. Bruce Fields2ca72e12011-01-04 17:37:15 -050056
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#define NFSDDBG_FACILITY NFSDDBG_XDR
58
J.Bruce Fields42ca0992006-10-04 02:16:20 -070059/*
60 * As per referral draft, the fsid for a referral MUST be different from the fsid of the containing
61 * directory in order to indicate to the client that a filesystem boundary is present
62 * We use a fixed fsid for a referral
63 */
64#define NFS4_REFERRAL_FSID_MAJOR 0x8000000ULL
65#define NFS4_REFERRAL_FSID_MINOR 0x8000000ULL
66
Al Virob37ad282006-10-19 23:28:59 -070067static __be32
68check_filename(char *str, int len, __be32 err)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
70 int i;
71
72 if (len == 0)
73 return nfserr_inval;
74 if (isdotent(str, len))
75 return err;
76 for (i = 0; i < len; i++)
77 if (str[i] == '/')
78 return err;
79 return 0;
80}
81
Linus Torvalds1da177e2005-04-16 15:20:36 -070082#define DECODE_HEAD \
Al Viro2ebbc012006-10-19 23:28:58 -070083 __be32 *p; \
Al Virob37ad282006-10-19 23:28:59 -070084 __be32 status
Linus Torvalds1da177e2005-04-16 15:20:36 -070085#define DECODE_TAIL \
86 status = 0; \
87out: \
88 return status; \
89xdr_error: \
Chuck Lever817cb9d2007-09-11 18:01:20 -040090 dprintk("NFSD: xdr error (%s:%d)\n", \
91 __FILE__, __LINE__); \
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 status = nfserr_bad_xdr; \
93 goto out
94
95#define READ32(x) (x) = ntohl(*p++)
96#define READ64(x) do { \
97 (x) = (u64)ntohl(*p++) << 32; \
98 (x) |= ntohl(*p++); \
99} while (0)
100#define READTIME(x) do { \
101 p++; \
102 (x) = ntohl(*p++); \
103 p++; \
104} while (0)
105#define READMEM(x,nbytes) do { \
106 x = (char *)p; \
107 p += XDR_QUADLEN(nbytes); \
108} while (0)
109#define SAVEMEM(x,nbytes) do { \
110 if (!(x = (p==argp->tmp || p == argp->tmpp) ? \
111 savemem(argp, p, nbytes) : \
112 (char *)p)) { \
Chuck Lever817cb9d2007-09-11 18:01:20 -0400113 dprintk("NFSD: xdr error (%s:%d)\n", \
114 __FILE__, __LINE__); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 goto xdr_error; \
116 } \
117 p += XDR_QUADLEN(nbytes); \
118} while (0)
119#define COPYMEM(x,nbytes) do { \
120 memcpy((x), p, nbytes); \
121 p += XDR_QUADLEN(nbytes); \
122} while (0)
123
124/* READ_BUF, read_buf(): nbytes must be <= PAGE_SIZE */
125#define READ_BUF(nbytes) do { \
126 if (nbytes <= (u32)((char *)argp->end - (char *)argp->p)) { \
127 p = argp->p; \
128 argp->p += XDR_QUADLEN(nbytes); \
129 } else if (!(p = read_buf(argp, nbytes))) { \
Chuck Lever817cb9d2007-09-11 18:01:20 -0400130 dprintk("NFSD: xdr error (%s:%d)\n", \
131 __FILE__, __LINE__); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 goto xdr_error; \
133 } \
134} while (0)
135
J. Bruce Fieldsca2a05a2007-11-11 15:43:12 -0500136static __be32 *read_buf(struct nfsd4_compoundargs *argp, u32 nbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137{
138 /* We want more bytes than seem to be available.
139 * Maybe we need a new page, maybe we have just run out
140 */
J. Bruce Fieldsca2a05a2007-11-11 15:43:12 -0500141 unsigned int avail = (char *)argp->end - (char *)argp->p;
Al Viro2ebbc012006-10-19 23:28:58 -0700142 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 if (avail + argp->pagelen < nbytes)
144 return NULL;
145 if (avail + PAGE_SIZE < nbytes) /* need more than a page !! */
146 return NULL;
147 /* ok, we can do it with the current plus the next page */
148 if (nbytes <= sizeof(argp->tmp))
149 p = argp->tmp;
150 else {
Jesper Juhlf99d49a2005-11-07 01:01:34 -0800151 kfree(argp->tmpp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 p = argp->tmpp = kmalloc(nbytes, GFP_KERNEL);
153 if (!p)
154 return NULL;
155
156 }
J. Bruce Fieldsca2a05a2007-11-11 15:43:12 -0500157 /*
158 * The following memcpy is safe because read_buf is always
159 * called with nbytes > avail, and the two cases above both
160 * guarantee p points to at least nbytes bytes.
161 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 memcpy(p, argp->p, avail);
163 /* step to next page */
J. Bruce Fieldsffdae5d2013-06-21 11:48:11 -0400164 argp->p = page_address(argp->pagelist[0]);
J. Bruce Fieldsca36e742013-11-19 17:32:43 -0500165 argp->pagelist++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 if (argp->pagelen < PAGE_SIZE) {
Neil Brown2bc3c112010-04-20 12:16:52 +1000167 argp->end = argp->p + (argp->pagelen>>2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 argp->pagelen = 0;
169 } else {
Neil Brown2bc3c112010-04-20 12:16:52 +1000170 argp->end = argp->p + (PAGE_SIZE>>2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 argp->pagelen -= PAGE_SIZE;
172 }
173 memcpy(((char*)p)+avail, argp->p, (nbytes - avail));
174 argp->p += XDR_QUADLEN(nbytes - avail);
175 return p;
176}
177
Andy Adamson60adfc52009-04-03 08:28:50 +0300178static int zero_clientid(clientid_t *clid)
179{
180 return (clid->cl_boot == 0) && (clid->cl_id == 0);
181}
182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183static int
184defer_free(struct nfsd4_compoundargs *argp,
185 void (*release)(const void *), void *p)
186{
187 struct tmpbuf *tb;
188
189 tb = kmalloc(sizeof(*tb), GFP_KERNEL);
190 if (!tb)
191 return -ENOMEM;
192 tb->buf = p;
193 tb->release = release;
194 tb->next = argp->to_free;
195 argp->to_free = tb;
196 return 0;
197}
198
Al Viro2ebbc012006-10-19 23:28:58 -0700199static char *savemem(struct nfsd4_compoundargs *argp, __be32 *p, int nbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 if (p == argp->tmp) {
Thomas Meyer67114fe2011-11-17 23:43:40 +0100202 p = kmemdup(argp->tmp, nbytes, GFP_KERNEL);
J. Bruce Fieldsa4db5fe2007-02-16 01:28:30 -0800203 if (!p)
204 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 } else {
Eric Sesterhenn73dff8b2006-10-03 23:37:14 +0200206 BUG_ON(p != argp->tmpp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 argp->tmpp = NULL;
208 }
209 if (defer_free(argp, kfree, p)) {
J. Bruce Fieldsa4db5fe2007-02-16 01:28:30 -0800210 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 return NULL;
212 } else
213 return (char *)p;
214}
215
Al Virob37ad282006-10-19 23:28:59 -0700216static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217nfsd4_decode_bitmap(struct nfsd4_compoundargs *argp, u32 *bmval)
218{
219 u32 bmlen;
220 DECODE_HEAD;
221
222 bmval[0] = 0;
223 bmval[1] = 0;
Andy Adamson7e705702009-04-03 08:29:11 +0300224 bmval[2] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
226 READ_BUF(4);
227 READ32(bmlen);
228 if (bmlen > 1000)
229 goto xdr_error;
230
231 READ_BUF(bmlen << 2);
232 if (bmlen > 0)
233 READ32(bmval[0]);
234 if (bmlen > 1)
235 READ32(bmval[1]);
Andy Adamson7e705702009-04-03 08:29:11 +0300236 if (bmlen > 2)
237 READ32(bmval[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
239 DECODE_TAIL;
240}
241
Al Virob37ad282006-10-19 23:28:59 -0700242static __be32
Yu Zhiguo3c8e0312009-05-16 16:22:31 +0800243nfsd4_decode_fattr(struct nfsd4_compoundargs *argp, u32 *bmval,
Benny Halevyc0d6fc82009-04-03 08:29:05 +0300244 struct iattr *iattr, struct nfs4_acl **acl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245{
246 int expected_len, len = 0;
247 u32 dummy32;
248 char *buf;
Al Virob8dd7b92006-10-19 23:29:01 -0700249 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251 DECODE_HEAD;
252 iattr->ia_valid = 0;
253 if ((status = nfsd4_decode_bitmap(argp, bmval)))
254 return status;
255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 READ_BUF(4);
257 READ32(expected_len);
258
259 if (bmval[0] & FATTR4_WORD0_SIZE) {
260 READ_BUF(8);
261 len += 8;
262 READ64(iattr->ia_size);
263 iattr->ia_valid |= ATTR_SIZE;
264 }
265 if (bmval[0] & FATTR4_WORD0_ACL) {
J. Bruce Fields49e67242013-03-26 14:11:13 -0400266 u32 nace;
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800267 struct nfs4_ace *ace;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
269 READ_BUF(4); len += 4;
270 READ32(nace);
271
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800272 if (nace > NFS4_ACL_MAX)
273 return nfserr_resource;
274
275 *acl = nfs4_acl_new(nace);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 if (*acl == NULL) {
Al Virob8dd7b92006-10-19 23:29:01 -0700277 host_err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 goto out_nfserr;
279 }
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800280 defer_free(argp, kfree, *acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800282 (*acl)->naces = nace;
283 for (ace = (*acl)->aces; ace < (*acl)->aces + nace; ace++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 READ_BUF(16); len += 16;
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800285 READ32(ace->type);
286 READ32(ace->flag);
287 READ32(ace->access_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 READ32(dummy32);
289 READ_BUF(dummy32);
290 len += XDR_QUADLEN(dummy32) << 2;
291 READMEM(buf, dummy32);
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800292 ace->whotype = nfs4_acl_get_whotype(buf, dummy32);
J. Bruce Fields3c726022011-01-04 17:53:52 -0500293 status = nfs_ok;
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800294 if (ace->whotype != NFS4_ACL_WHO_NAMED)
295 ace->who = 0;
296 else if (ace->flag & NFS4_ACE_IDENTIFIER_GROUP)
J. Bruce Fields3c726022011-01-04 17:53:52 -0500297 status = nfsd_map_name_to_gid(argp->rqstp,
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800298 buf, dummy32, &ace->who);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 else
J. Bruce Fields3c726022011-01-04 17:53:52 -0500300 status = nfsd_map_name_to_uid(argp->rqstp,
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800301 buf, dummy32, &ace->who);
J. Bruce Fields3c726022011-01-04 17:53:52 -0500302 if (status)
303 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 }
305 } else
306 *acl = NULL;
307 if (bmval[1] & FATTR4_WORD1_MODE) {
308 READ_BUF(4);
309 len += 4;
310 READ32(iattr->ia_mode);
311 iattr->ia_mode &= (S_IFMT | S_IALLUGO);
312 iattr->ia_valid |= ATTR_MODE;
313 }
314 if (bmval[1] & FATTR4_WORD1_OWNER) {
315 READ_BUF(4);
316 len += 4;
317 READ32(dummy32);
318 READ_BUF(dummy32);
319 len += (XDR_QUADLEN(dummy32) << 2);
320 READMEM(buf, dummy32);
NeilBrown47c85292011-02-16 13:08:35 +1100321 if ((status = nfsd_map_name_to_uid(argp->rqstp, buf, dummy32, &iattr->ia_uid)))
322 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 iattr->ia_valid |= ATTR_UID;
324 }
325 if (bmval[1] & FATTR4_WORD1_OWNER_GROUP) {
326 READ_BUF(4);
327 len += 4;
328 READ32(dummy32);
329 READ_BUF(dummy32);
330 len += (XDR_QUADLEN(dummy32) << 2);
331 READMEM(buf, dummy32);
NeilBrown47c85292011-02-16 13:08:35 +1100332 if ((status = nfsd_map_name_to_gid(argp->rqstp, buf, dummy32, &iattr->ia_gid)))
333 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 iattr->ia_valid |= ATTR_GID;
335 }
336 if (bmval[1] & FATTR4_WORD1_TIME_ACCESS_SET) {
337 READ_BUF(4);
338 len += 4;
339 READ32(dummy32);
340 switch (dummy32) {
341 case NFS4_SET_TO_CLIENT_TIME:
342 /* We require the high 32 bits of 'seconds' to be 0, and we ignore
343 all 32 bits of 'nseconds'. */
344 READ_BUF(12);
345 len += 12;
Bryan Schumaker2111a772013-04-19 16:09:38 -0400346 READ64(iattr->ia_atime.tv_sec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 READ32(iattr->ia_atime.tv_nsec);
348 if (iattr->ia_atime.tv_nsec >= (u32)1000000000)
349 return nfserr_inval;
350 iattr->ia_valid |= (ATTR_ATIME | ATTR_ATIME_SET);
351 break;
352 case NFS4_SET_TO_SERVER_TIME:
353 iattr->ia_valid |= ATTR_ATIME;
354 break;
355 default:
356 goto xdr_error;
357 }
358 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 if (bmval[1] & FATTR4_WORD1_TIME_MODIFY_SET) {
360 READ_BUF(4);
361 len += 4;
362 READ32(dummy32);
363 switch (dummy32) {
364 case NFS4_SET_TO_CLIENT_TIME:
365 /* We require the high 32 bits of 'seconds' to be 0, and we ignore
366 all 32 bits of 'nseconds'. */
367 READ_BUF(12);
368 len += 12;
Bryan Schumaker2111a772013-04-19 16:09:38 -0400369 READ64(iattr->ia_mtime.tv_sec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 READ32(iattr->ia_mtime.tv_nsec);
371 if (iattr->ia_mtime.tv_nsec >= (u32)1000000000)
372 return nfserr_inval;
373 iattr->ia_valid |= (ATTR_MTIME | ATTR_MTIME_SET);
374 break;
375 case NFS4_SET_TO_SERVER_TIME:
376 iattr->ia_valid |= ATTR_MTIME;
377 break;
378 default:
379 goto xdr_error;
380 }
381 }
Yu Zhiguo3c8e0312009-05-16 16:22:31 +0800382 if (bmval[0] & ~NFSD_WRITEABLE_ATTRS_WORD0
383 || bmval[1] & ~NFSD_WRITEABLE_ATTRS_WORD1
384 || bmval[2] & ~NFSD_WRITEABLE_ATTRS_WORD2)
385 READ_BUF(expected_len - len);
386 else if (len != expected_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 goto xdr_error;
388
389 DECODE_TAIL;
390
391out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -0700392 status = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 goto out;
394}
395
Al Virob37ad282006-10-19 23:28:59 -0700396static __be32
Benny Halevye31a1b62008-08-12 20:46:18 +0300397nfsd4_decode_stateid(struct nfsd4_compoundargs *argp, stateid_t *sid)
398{
399 DECODE_HEAD;
400
401 READ_BUF(sizeof(stateid_t));
402 READ32(sid->si_generation);
403 COPYMEM(&sid->si_opaque, sizeof(stateid_opaque_t));
404
405 DECODE_TAIL;
406}
407
408static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409nfsd4_decode_access(struct nfsd4_compoundargs *argp, struct nfsd4_access *access)
410{
411 DECODE_HEAD;
412
413 READ_BUF(4);
414 READ32(access->ac_req_access);
415
416 DECODE_TAIL;
417}
418
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -0400419static __be32 nfsd4_decode_bind_conn_to_session(struct nfsd4_compoundargs *argp, struct nfsd4_bind_conn_to_session *bcts)
420{
421 DECODE_HEAD;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -0400422
423 READ_BUF(NFS4_MAX_SESSIONID_LEN + 8);
424 COPYMEM(bcts->sessionid.data, NFS4_MAX_SESSIONID_LEN);
425 READ32(bcts->dir);
Bryan Schumaker6ce23572011-04-27 15:47:16 -0400426 /* XXX: skipping ctsa_use_conn_in_rdma_mode. Perhaps Tom Tucker
427 * could help us figure out we should be using it. */
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -0400428 DECODE_TAIL;
429}
430
Al Virob37ad282006-10-19 23:28:59 -0700431static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432nfsd4_decode_close(struct nfsd4_compoundargs *argp, struct nfsd4_close *close)
433{
434 DECODE_HEAD;
435
Benny Halevye31a1b62008-08-12 20:46:18 +0300436 READ_BUF(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 READ32(close->cl_seqid);
Benny Halevye31a1b62008-08-12 20:46:18 +0300438 return nfsd4_decode_stateid(argp, &close->cl_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
440 DECODE_TAIL;
441}
442
443
Al Virob37ad282006-10-19 23:28:59 -0700444static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445nfsd4_decode_commit(struct nfsd4_compoundargs *argp, struct nfsd4_commit *commit)
446{
447 DECODE_HEAD;
448
449 READ_BUF(12);
450 READ64(commit->co_offset);
451 READ32(commit->co_count);
452
453 DECODE_TAIL;
454}
455
Al Virob37ad282006-10-19 23:28:59 -0700456static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457nfsd4_decode_create(struct nfsd4_compoundargs *argp, struct nfsd4_create *create)
458{
459 DECODE_HEAD;
460
461 READ_BUF(4);
462 READ32(create->cr_type);
463 switch (create->cr_type) {
464 case NF4LNK:
465 READ_BUF(4);
466 READ32(create->cr_linklen);
467 READ_BUF(create->cr_linklen);
J. Bruce Fieldsa22a7e72014-06-19 16:44:48 -0400468 /*
469 * The VFS will want a null-terminated string, and
470 * null-terminating in place isn't safe since this might
471 * end on a page boundary:
472 */
473 create->cr_linkname =
474 kmalloc(create->cr_linklen + 1, GFP_KERNEL);
475 if (!create->cr_linkname)
476 return nfserr_jukebox;
477 memcpy(create->cr_linkname, p, create->cr_linklen);
478 create->cr_linkname[create->cr_linklen] = '\0';
479 defer_free(argp, kfree, create->cr_linkname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 break;
481 case NF4BLK:
482 case NF4CHR:
483 READ_BUF(8);
484 READ32(create->cr_specdata1);
485 READ32(create->cr_specdata2);
486 break;
487 case NF4SOCK:
488 case NF4FIFO:
489 case NF4DIR:
490 default:
491 break;
492 }
493
494 READ_BUF(4);
495 READ32(create->cr_namelen);
496 READ_BUF(create->cr_namelen);
497 SAVEMEM(create->cr_name, create->cr_namelen);
498 if ((status = check_filename(create->cr_name, create->cr_namelen, nfserr_inval)))
499 return status;
500
Yu Zhiguo3c8e0312009-05-16 16:22:31 +0800501 status = nfsd4_decode_fattr(argp, create->cr_bmval, &create->cr_iattr,
502 &create->cr_acl);
Benny Halevyc0d6fc82009-04-03 08:29:05 +0300503 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 goto out;
505
506 DECODE_TAIL;
507}
508
Al Virob37ad282006-10-19 23:28:59 -0700509static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510nfsd4_decode_delegreturn(struct nfsd4_compoundargs *argp, struct nfsd4_delegreturn *dr)
511{
Benny Halevye31a1b62008-08-12 20:46:18 +0300512 return nfsd4_decode_stateid(argp, &dr->dr_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513}
514
Al Virob37ad282006-10-19 23:28:59 -0700515static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516nfsd4_decode_getattr(struct nfsd4_compoundargs *argp, struct nfsd4_getattr *getattr)
517{
518 return nfsd4_decode_bitmap(argp, getattr->ga_bmval);
519}
520
Al Virob37ad282006-10-19 23:28:59 -0700521static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522nfsd4_decode_link(struct nfsd4_compoundargs *argp, struct nfsd4_link *link)
523{
524 DECODE_HEAD;
525
526 READ_BUF(4);
527 READ32(link->li_namelen);
528 READ_BUF(link->li_namelen);
529 SAVEMEM(link->li_name, link->li_namelen);
530 if ((status = check_filename(link->li_name, link->li_namelen, nfserr_inval)))
531 return status;
532
533 DECODE_TAIL;
534}
535
Al Virob37ad282006-10-19 23:28:59 -0700536static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537nfsd4_decode_lock(struct nfsd4_compoundargs *argp, struct nfsd4_lock *lock)
538{
539 DECODE_HEAD;
540
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 /*
542 * type, reclaim(boolean), offset, length, new_lock_owner(boolean)
543 */
544 READ_BUF(28);
545 READ32(lock->lk_type);
546 if ((lock->lk_type < NFS4_READ_LT) || (lock->lk_type > NFS4_WRITEW_LT))
547 goto xdr_error;
548 READ32(lock->lk_reclaim);
549 READ64(lock->lk_offset);
550 READ64(lock->lk_length);
551 READ32(lock->lk_is_new);
552
553 if (lock->lk_is_new) {
Benny Halevye31a1b62008-08-12 20:46:18 +0300554 READ_BUF(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 READ32(lock->lk_new_open_seqid);
Benny Halevye31a1b62008-08-12 20:46:18 +0300556 status = nfsd4_decode_stateid(argp, &lock->lk_new_open_stateid);
557 if (status)
558 return status;
559 READ_BUF(8 + sizeof(clientid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 READ32(lock->lk_new_lock_seqid);
561 COPYMEM(&lock->lk_new_clientid, sizeof(clientid_t));
562 READ32(lock->lk_new_owner.len);
563 READ_BUF(lock->lk_new_owner.len);
564 READMEM(lock->lk_new_owner.data, lock->lk_new_owner.len);
565 } else {
Benny Halevye31a1b62008-08-12 20:46:18 +0300566 status = nfsd4_decode_stateid(argp, &lock->lk_old_lock_stateid);
567 if (status)
568 return status;
569 READ_BUF(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 READ32(lock->lk_old_lock_seqid);
571 }
572
573 DECODE_TAIL;
574}
575
Al Virob37ad282006-10-19 23:28:59 -0700576static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577nfsd4_decode_lockt(struct nfsd4_compoundargs *argp, struct nfsd4_lockt *lockt)
578{
579 DECODE_HEAD;
580
581 READ_BUF(32);
582 READ32(lockt->lt_type);
583 if((lockt->lt_type < NFS4_READ_LT) || (lockt->lt_type > NFS4_WRITEW_LT))
584 goto xdr_error;
585 READ64(lockt->lt_offset);
586 READ64(lockt->lt_length);
587 COPYMEM(&lockt->lt_clientid, 8);
588 READ32(lockt->lt_owner.len);
589 READ_BUF(lockt->lt_owner.len);
590 READMEM(lockt->lt_owner.data, lockt->lt_owner.len);
591
592 DECODE_TAIL;
593}
594
Al Virob37ad282006-10-19 23:28:59 -0700595static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596nfsd4_decode_locku(struct nfsd4_compoundargs *argp, struct nfsd4_locku *locku)
597{
598 DECODE_HEAD;
599
Benny Halevye31a1b62008-08-12 20:46:18 +0300600 READ_BUF(8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 READ32(locku->lu_type);
602 if ((locku->lu_type < NFS4_READ_LT) || (locku->lu_type > NFS4_WRITEW_LT))
603 goto xdr_error;
604 READ32(locku->lu_seqid);
Benny Halevye31a1b62008-08-12 20:46:18 +0300605 status = nfsd4_decode_stateid(argp, &locku->lu_stateid);
606 if (status)
607 return status;
608 READ_BUF(16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 READ64(locku->lu_offset);
610 READ64(locku->lu_length);
611
612 DECODE_TAIL;
613}
614
Al Virob37ad282006-10-19 23:28:59 -0700615static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616nfsd4_decode_lookup(struct nfsd4_compoundargs *argp, struct nfsd4_lookup *lookup)
617{
618 DECODE_HEAD;
619
620 READ_BUF(4);
621 READ32(lookup->lo_len);
622 READ_BUF(lookup->lo_len);
623 SAVEMEM(lookup->lo_name, lookup->lo_len);
624 if ((status = check_filename(lookup->lo_name, lookup->lo_len, nfserr_noent)))
625 return status;
626
627 DECODE_TAIL;
628}
629
Benny Halevy2c8bd7e2012-02-16 20:57:09 +0200630static __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 -0400631{
632 __be32 *p;
633 u32 w;
634
635 READ_BUF(4);
636 READ32(w);
Benny Halevy2c8bd7e2012-02-16 20:57:09 +0200637 *share_access = w & NFS4_SHARE_ACCESS_MASK;
638 *deleg_want = w & NFS4_SHARE_WANT_MASK;
639 if (deleg_when)
640 *deleg_when = w & NFS4_SHARE_WHEN_MASK;
641
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400642 switch (w & NFS4_SHARE_ACCESS_MASK) {
643 case NFS4_SHARE_ACCESS_READ:
644 case NFS4_SHARE_ACCESS_WRITE:
645 case NFS4_SHARE_ACCESS_BOTH:
646 break;
647 default:
648 return nfserr_bad_xdr;
649 }
Benny Halevyfc0d14f2011-10-27 20:43:01 +0200650 w &= ~NFS4_SHARE_ACCESS_MASK;
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400651 if (!w)
652 return nfs_ok;
653 if (!argp->minorversion)
654 return nfserr_bad_xdr;
655 switch (w & NFS4_SHARE_WANT_MASK) {
656 case NFS4_SHARE_WANT_NO_PREFERENCE:
657 case NFS4_SHARE_WANT_READ_DELEG:
658 case NFS4_SHARE_WANT_WRITE_DELEG:
659 case NFS4_SHARE_WANT_ANY_DELEG:
660 case NFS4_SHARE_WANT_NO_DELEG:
661 case NFS4_SHARE_WANT_CANCEL:
662 break;
663 default:
664 return nfserr_bad_xdr;
665 }
Benny Halevy92bac8c2011-10-19 19:13:29 -0700666 w &= ~NFS4_SHARE_WANT_MASK;
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400667 if (!w)
668 return nfs_ok;
Benny Halevy2c8bd7e2012-02-16 20:57:09 +0200669
670 if (!deleg_when) /* open_downgrade */
671 return nfserr_inval;
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400672 switch (w) {
673 case NFS4_SHARE_SIGNAL_DELEG_WHEN_RESRC_AVAIL:
674 case NFS4_SHARE_PUSH_DELEG_WHEN_UNCONTENDED:
Benny Halevyc668fc62011-10-19 19:13:13 -0700675 case (NFS4_SHARE_SIGNAL_DELEG_WHEN_RESRC_AVAIL |
676 NFS4_SHARE_PUSH_DELEG_WHEN_UNCONTENDED):
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400677 return nfs_ok;
678 }
679xdr_error:
680 return nfserr_bad_xdr;
681}
682
683static __be32 nfsd4_decode_share_deny(struct nfsd4_compoundargs *argp, u32 *x)
684{
685 __be32 *p;
686
687 READ_BUF(4);
688 READ32(*x);
689 /* Note: unlinke access bits, deny bits may be zero. */
Dan Carpenter01cd4af2011-10-17 10:41:17 +0300690 if (*x & ~NFS4_SHARE_DENY_BOTH)
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400691 return nfserr_bad_xdr;
692 return nfs_ok;
693xdr_error:
694 return nfserr_bad_xdr;
695}
696
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -0400697static __be32 nfsd4_decode_opaque(struct nfsd4_compoundargs *argp, struct xdr_netobj *o)
698{
699 __be32 *p;
700
701 READ_BUF(4);
702 READ32(o->len);
703
704 if (o->len == 0 || o->len > NFS4_OPAQUE_LIMIT)
705 return nfserr_bad_xdr;
706
707 READ_BUF(o->len);
708 SAVEMEM(o->data, o->len);
709 return nfs_ok;
710xdr_error:
711 return nfserr_bad_xdr;
712}
713
Al Virob37ad282006-10-19 23:28:59 -0700714static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715nfsd4_decode_open(struct nfsd4_compoundargs *argp, struct nfsd4_open *open)
716{
717 DECODE_HEAD;
Benny Halevy2c8bd7e2012-02-16 20:57:09 +0200718 u32 dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
720 memset(open->op_bmval, 0, sizeof(open->op_bmval));
721 open->op_iattr.ia_valid = 0;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400722 open->op_openowner = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
724 /* seqid, share_access, share_deny, clientid, ownerlen */
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400725 READ_BUF(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 READ32(open->op_seqid);
Benny Halevy2c8bd7e2012-02-16 20:57:09 +0200727 /* decode, yet ignore deleg_when until supported */
728 status = nfsd4_decode_share_access(argp, &open->op_share_access,
729 &open->op_deleg_want, &dummy);
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400730 if (status)
731 goto xdr_error;
732 status = nfsd4_decode_share_deny(argp, &open->op_share_deny);
733 if (status)
734 goto xdr_error;
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -0400735 READ_BUF(sizeof(clientid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 COPYMEM(&open->op_clientid, sizeof(clientid_t));
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -0400737 status = nfsd4_decode_opaque(argp, &open->op_owner);
738 if (status)
739 goto xdr_error;
740 READ_BUF(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 READ32(open->op_create);
742 switch (open->op_create) {
743 case NFS4_OPEN_NOCREATE:
744 break;
745 case NFS4_OPEN_CREATE:
746 READ_BUF(4);
747 READ32(open->op_createmode);
748 switch (open->op_createmode) {
749 case NFS4_CREATE_UNCHECKED:
750 case NFS4_CREATE_GUARDED:
Benny Halevyc0d6fc82009-04-03 08:29:05 +0300751 status = nfsd4_decode_fattr(argp, open->op_bmval,
Yu Zhiguo3c8e0312009-05-16 16:22:31 +0800752 &open->op_iattr, &open->op_acl);
Benny Halevyc0d6fc82009-04-03 08:29:05 +0300753 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 goto out;
755 break;
756 case NFS4_CREATE_EXCLUSIVE:
Chuck Leverab4684d2012-03-02 17:13:50 -0500757 READ_BUF(NFS4_VERIFIER_SIZE);
758 COPYMEM(open->op_verf.data, NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 break;
Benny Halevy79fb54a2009-04-03 08:29:17 +0300760 case NFS4_CREATE_EXCLUSIVE4_1:
761 if (argp->minorversion < 1)
762 goto xdr_error;
Chuck Leverab4684d2012-03-02 17:13:50 -0500763 READ_BUF(NFS4_VERIFIER_SIZE);
764 COPYMEM(open->op_verf.data, NFS4_VERIFIER_SIZE);
Benny Halevy79fb54a2009-04-03 08:29:17 +0300765 status = nfsd4_decode_fattr(argp, open->op_bmval,
Yu Zhiguo3c8e0312009-05-16 16:22:31 +0800766 &open->op_iattr, &open->op_acl);
Benny Halevy79fb54a2009-04-03 08:29:17 +0300767 if (status)
768 goto out;
769 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 default:
771 goto xdr_error;
772 }
773 break;
774 default:
775 goto xdr_error;
776 }
777
778 /* open_claim */
779 READ_BUF(4);
780 READ32(open->op_claim_type);
781 switch (open->op_claim_type) {
782 case NFS4_OPEN_CLAIM_NULL:
783 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
784 READ_BUF(4);
785 READ32(open->op_fname.len);
786 READ_BUF(open->op_fname.len);
787 SAVEMEM(open->op_fname.data, open->op_fname.len);
788 if ((status = check_filename(open->op_fname.data, open->op_fname.len, nfserr_inval)))
789 return status;
790 break;
791 case NFS4_OPEN_CLAIM_PREVIOUS:
792 READ_BUF(4);
793 READ32(open->op_delegate_type);
794 break;
795 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
Benny Halevye31a1b62008-08-12 20:46:18 +0300796 status = nfsd4_decode_stateid(argp, &open->op_delegate_stateid);
797 if (status)
798 return status;
799 READ_BUF(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 READ32(open->op_fname.len);
801 READ_BUF(open->op_fname.len);
802 SAVEMEM(open->op_fname.data, open->op_fname.len);
803 if ((status = check_filename(open->op_fname.data, open->op_fname.len, nfserr_inval)))
804 return status;
805 break;
J. Bruce Fields8b289b22011-10-19 11:52:12 -0400806 case NFS4_OPEN_CLAIM_FH:
807 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
808 if (argp->minorversion < 1)
809 goto xdr_error;
810 /* void */
811 break;
812 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
813 if (argp->minorversion < 1)
814 goto xdr_error;
815 status = nfsd4_decode_stateid(argp, &open->op_delegate_stateid);
816 if (status)
817 return status;
818 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 default:
820 goto xdr_error;
821 }
822
823 DECODE_TAIL;
824}
825
Al Virob37ad282006-10-19 23:28:59 -0700826static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827nfsd4_decode_open_confirm(struct nfsd4_compoundargs *argp, struct nfsd4_open_confirm *open_conf)
828{
829 DECODE_HEAD;
830
Benny Halevye31a1b62008-08-12 20:46:18 +0300831 status = nfsd4_decode_stateid(argp, &open_conf->oc_req_stateid);
832 if (status)
833 return status;
834 READ_BUF(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 READ32(open_conf->oc_seqid);
836
837 DECODE_TAIL;
838}
839
Al Virob37ad282006-10-19 23:28:59 -0700840static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841nfsd4_decode_open_downgrade(struct nfsd4_compoundargs *argp, struct nfsd4_open_downgrade *open_down)
842{
843 DECODE_HEAD;
844
Benny Halevye31a1b62008-08-12 20:46:18 +0300845 status = nfsd4_decode_stateid(argp, &open_down->od_stateid);
846 if (status)
847 return status;
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400848 READ_BUF(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 READ32(open_down->od_seqid);
Benny Halevy2c8bd7e2012-02-16 20:57:09 +0200850 status = nfsd4_decode_share_access(argp, &open_down->od_share_access,
851 &open_down->od_deleg_want, NULL);
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400852 if (status)
853 return status;
854 status = nfsd4_decode_share_deny(argp, &open_down->od_share_deny);
855 if (status)
856 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 DECODE_TAIL;
858}
859
Al Virob37ad282006-10-19 23:28:59 -0700860static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861nfsd4_decode_putfh(struct nfsd4_compoundargs *argp, struct nfsd4_putfh *putfh)
862{
863 DECODE_HEAD;
864
865 READ_BUF(4);
866 READ32(putfh->pf_fhlen);
867 if (putfh->pf_fhlen > NFS4_FHSIZE)
868 goto xdr_error;
869 READ_BUF(putfh->pf_fhlen);
870 SAVEMEM(putfh->pf_fhval, putfh->pf_fhlen);
871
872 DECODE_TAIL;
873}
874
Al Virob37ad282006-10-19 23:28:59 -0700875static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876nfsd4_decode_read(struct nfsd4_compoundargs *argp, struct nfsd4_read *read)
877{
878 DECODE_HEAD;
879
Benny Halevye31a1b62008-08-12 20:46:18 +0300880 status = nfsd4_decode_stateid(argp, &read->rd_stateid);
881 if (status)
882 return status;
883 READ_BUF(12);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 READ64(read->rd_offset);
885 READ32(read->rd_length);
886
887 DECODE_TAIL;
888}
889
Al Virob37ad282006-10-19 23:28:59 -0700890static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891nfsd4_decode_readdir(struct nfsd4_compoundargs *argp, struct nfsd4_readdir *readdir)
892{
893 DECODE_HEAD;
894
895 READ_BUF(24);
896 READ64(readdir->rd_cookie);
897 COPYMEM(readdir->rd_verf.data, sizeof(readdir->rd_verf.data));
898 READ32(readdir->rd_dircount); /* just in case you needed a useless field... */
899 READ32(readdir->rd_maxcount);
900 if ((status = nfsd4_decode_bitmap(argp, readdir->rd_bmval)))
901 goto out;
902
903 DECODE_TAIL;
904}
905
Al Virob37ad282006-10-19 23:28:59 -0700906static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907nfsd4_decode_remove(struct nfsd4_compoundargs *argp, struct nfsd4_remove *remove)
908{
909 DECODE_HEAD;
910
911 READ_BUF(4);
912 READ32(remove->rm_namelen);
913 READ_BUF(remove->rm_namelen);
914 SAVEMEM(remove->rm_name, remove->rm_namelen);
915 if ((status = check_filename(remove->rm_name, remove->rm_namelen, nfserr_noent)))
916 return status;
917
918 DECODE_TAIL;
919}
920
Al Virob37ad282006-10-19 23:28:59 -0700921static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922nfsd4_decode_rename(struct nfsd4_compoundargs *argp, struct nfsd4_rename *rename)
923{
924 DECODE_HEAD;
925
926 READ_BUF(4);
927 READ32(rename->rn_snamelen);
928 READ_BUF(rename->rn_snamelen + 4);
929 SAVEMEM(rename->rn_sname, rename->rn_snamelen);
930 READ32(rename->rn_tnamelen);
931 READ_BUF(rename->rn_tnamelen);
932 SAVEMEM(rename->rn_tname, rename->rn_tnamelen);
933 if ((status = check_filename(rename->rn_sname, rename->rn_snamelen, nfserr_noent)))
934 return status;
935 if ((status = check_filename(rename->rn_tname, rename->rn_tnamelen, nfserr_inval)))
936 return status;
937
938 DECODE_TAIL;
939}
940
Al Virob37ad282006-10-19 23:28:59 -0700941static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942nfsd4_decode_renew(struct nfsd4_compoundargs *argp, clientid_t *clientid)
943{
944 DECODE_HEAD;
945
946 READ_BUF(sizeof(clientid_t));
947 COPYMEM(clientid, sizeof(clientid_t));
948
949 DECODE_TAIL;
950}
951
Al Virob37ad282006-10-19 23:28:59 -0700952static __be32
Andy Adamsondcb488a2007-07-17 04:04:51 -0700953nfsd4_decode_secinfo(struct nfsd4_compoundargs *argp,
954 struct nfsd4_secinfo *secinfo)
955{
956 DECODE_HEAD;
957
958 READ_BUF(4);
959 READ32(secinfo->si_namelen);
960 READ_BUF(secinfo->si_namelen);
961 SAVEMEM(secinfo->si_name, secinfo->si_namelen);
962 status = check_filename(secinfo->si_name, secinfo->si_namelen,
963 nfserr_noent);
964 if (status)
965 return status;
966 DECODE_TAIL;
967}
968
969static __be32
J. Bruce Fields04f4ad12010-12-16 09:51:13 -0500970nfsd4_decode_secinfo_no_name(struct nfsd4_compoundargs *argp,
971 struct nfsd4_secinfo_no_name *sin)
972{
973 DECODE_HEAD;
974
975 READ_BUF(4);
976 READ32(sin->sin_style);
977 DECODE_TAIL;
978}
979
980static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981nfsd4_decode_setattr(struct nfsd4_compoundargs *argp, struct nfsd4_setattr *setattr)
982{
Benny Halevye31a1b62008-08-12 20:46:18 +0300983 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
Benny Halevye31a1b62008-08-12 20:46:18 +0300985 status = nfsd4_decode_stateid(argp, &setattr->sa_stateid);
986 if (status)
987 return status;
Yu Zhiguo3c8e0312009-05-16 16:22:31 +0800988 return nfsd4_decode_fattr(argp, setattr->sa_bmval, &setattr->sa_iattr,
989 &setattr->sa_acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990}
991
Al Virob37ad282006-10-19 23:28:59 -0700992static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993nfsd4_decode_setclientid(struct nfsd4_compoundargs *argp, struct nfsd4_setclientid *setclientid)
994{
995 DECODE_HEAD;
996
Chuck Leverab4684d2012-03-02 17:13:50 -0500997 READ_BUF(NFS4_VERIFIER_SIZE);
998 COPYMEM(setclientid->se_verf.data, NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04001000 status = nfsd4_decode_opaque(argp, &setclientid->se_name);
1001 if (status)
1002 return nfserr_bad_xdr;
1003 READ_BUF(8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 READ32(setclientid->se_callback_prog);
1005 READ32(setclientid->se_callback_netid_len);
1006
1007 READ_BUF(setclientid->se_callback_netid_len + 4);
1008 SAVEMEM(setclientid->se_callback_netid_val, setclientid->se_callback_netid_len);
1009 READ32(setclientid->se_callback_addr_len);
1010
1011 READ_BUF(setclientid->se_callback_addr_len + 4);
1012 SAVEMEM(setclientid->se_callback_addr_val, setclientid->se_callback_addr_len);
1013 READ32(setclientid->se_callback_ident);
1014
1015 DECODE_TAIL;
1016}
1017
Al Virob37ad282006-10-19 23:28:59 -07001018static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019nfsd4_decode_setclientid_confirm(struct nfsd4_compoundargs *argp, struct nfsd4_setclientid_confirm *scd_c)
1020{
1021 DECODE_HEAD;
1022
Chuck Leverab4684d2012-03-02 17:13:50 -05001023 READ_BUF(8 + NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 COPYMEM(&scd_c->sc_clientid, 8);
Chuck Leverab4684d2012-03-02 17:13:50 -05001025 COPYMEM(&scd_c->sc_confirm, NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026
1027 DECODE_TAIL;
1028}
1029
1030/* Also used for NVERIFY */
Al Virob37ad282006-10-19 23:28:59 -07001031static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032nfsd4_decode_verify(struct nfsd4_compoundargs *argp, struct nfsd4_verify *verify)
1033{
1034#if 0
1035 struct nfsd4_compoundargs save = {
1036 .p = argp->p,
1037 .end = argp->end,
1038 .rqstp = argp->rqstp,
1039 };
1040 u32 ve_bmval[2];
1041 struct iattr ve_iattr; /* request */
1042 struct nfs4_acl *ve_acl; /* request */
1043#endif
1044 DECODE_HEAD;
1045
1046 if ((status = nfsd4_decode_bitmap(argp, verify->ve_bmval)))
1047 goto out;
1048
1049 /* For convenience's sake, we compare raw xdr'd attributes in
1050 * nfsd4_proc_verify; however we still decode here just to return
1051 * correct error in case of bad xdr. */
1052#if 0
1053 status = nfsd4_decode_fattr(ve_bmval, &ve_iattr, &ve_acl);
1054 if (status == nfserr_inval) {
1055 status = nfserrno(status);
1056 goto out;
1057 }
1058#endif
1059 READ_BUF(4);
1060 READ32(verify->ve_attrlen);
1061 READ_BUF(verify->ve_attrlen);
1062 SAVEMEM(verify->ve_attrval, verify->ve_attrlen);
1063
1064 DECODE_TAIL;
1065}
1066
Al Virob37ad282006-10-19 23:28:59 -07001067static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068nfsd4_decode_write(struct nfsd4_compoundargs *argp, struct nfsd4_write *write)
1069{
1070 int avail;
1071 int v;
1072 int len;
1073 DECODE_HEAD;
1074
Benny Halevye31a1b62008-08-12 20:46:18 +03001075 status = nfsd4_decode_stateid(argp, &write->wr_stateid);
1076 if (status)
1077 return status;
1078 READ_BUF(16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 READ64(write->wr_offset);
1080 READ32(write->wr_stable_how);
1081 if (write->wr_stable_how > 2)
1082 goto xdr_error;
1083 READ32(write->wr_buflen);
1084
1085 /* Sorry .. no magic macros for this.. *
1086 * READ_BUF(write->wr_buflen);
1087 * SAVEMEM(write->wr_buf, write->wr_buflen);
1088 */
1089 avail = (char*)argp->end - (char*)argp->p;
1090 if (avail + argp->pagelen < write->wr_buflen) {
Chuck Lever817cb9d2007-09-11 18:01:20 -04001091 dprintk("NFSD: xdr error (%s:%d)\n",
1092 __FILE__, __LINE__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 goto xdr_error;
1094 }
NeilBrown3cc03b12006-10-04 02:15:47 -07001095 argp->rqstp->rq_vec[0].iov_base = p;
1096 argp->rqstp->rq_vec[0].iov_len = avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 v = 0;
1098 len = write->wr_buflen;
NeilBrown3cc03b12006-10-04 02:15:47 -07001099 while (len > argp->rqstp->rq_vec[v].iov_len) {
1100 len -= argp->rqstp->rq_vec[v].iov_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 v++;
NeilBrown3cc03b12006-10-04 02:15:47 -07001102 argp->rqstp->rq_vec[v].iov_base = page_address(argp->pagelist[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 argp->pagelist++;
1104 if (argp->pagelen >= PAGE_SIZE) {
NeilBrown3cc03b12006-10-04 02:15:47 -07001105 argp->rqstp->rq_vec[v].iov_len = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 argp->pagelen -= PAGE_SIZE;
1107 } else {
NeilBrown3cc03b12006-10-04 02:15:47 -07001108 argp->rqstp->rq_vec[v].iov_len = argp->pagelen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 argp->pagelen -= len;
1110 }
1111 }
Al Viro2ebbc012006-10-19 23:28:58 -07001112 argp->end = (__be32*) (argp->rqstp->rq_vec[v].iov_base + argp->rqstp->rq_vec[v].iov_len);
1113 argp->p = (__be32*) (argp->rqstp->rq_vec[v].iov_base + (XDR_QUADLEN(len) << 2));
NeilBrown3cc03b12006-10-04 02:15:47 -07001114 argp->rqstp->rq_vec[v].iov_len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 write->wr_vlen = v+1;
1116
1117 DECODE_TAIL;
1118}
1119
Al Virob37ad282006-10-19 23:28:59 -07001120static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121nfsd4_decode_release_lockowner(struct nfsd4_compoundargs *argp, struct nfsd4_release_lockowner *rlockowner)
1122{
1123 DECODE_HEAD;
1124
1125 READ_BUF(12);
1126 COPYMEM(&rlockowner->rl_clientid, sizeof(clientid_t));
1127 READ32(rlockowner->rl_owner.len);
1128 READ_BUF(rlockowner->rl_owner.len);
1129 READMEM(rlockowner->rl_owner.data, rlockowner->rl_owner.len);
1130
Andy Adamson60adfc52009-04-03 08:28:50 +03001131 if (argp->minorversion && !zero_clientid(&rlockowner->rl_clientid))
1132 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 DECODE_TAIL;
1134}
1135
Al Virob37ad282006-10-19 23:28:59 -07001136static __be32
Andy Adamson2db134e2009-04-03 08:27:55 +03001137nfsd4_decode_exchange_id(struct nfsd4_compoundargs *argp,
Andy Adamson0733d212009-04-03 08:28:01 +03001138 struct nfsd4_exchange_id *exid)
Andy Adamson2db134e2009-04-03 08:27:55 +03001139{
Mi Jinlong5afa0402010-11-09 09:39:23 +08001140 int dummy, tmp;
Andy Adamson0733d212009-04-03 08:28:01 +03001141 DECODE_HEAD;
1142
1143 READ_BUF(NFS4_VERIFIER_SIZE);
1144 COPYMEM(exid->verifier.data, NFS4_VERIFIER_SIZE);
1145
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04001146 status = nfsd4_decode_opaque(argp, &exid->clname);
1147 if (status)
1148 return nfserr_bad_xdr;
Andy Adamson0733d212009-04-03 08:28:01 +03001149
1150 READ_BUF(4);
1151 READ32(exid->flags);
1152
1153 /* Ignore state_protect4_a */
1154 READ_BUF(4);
1155 READ32(exid->spa_how);
1156 switch (exid->spa_how) {
1157 case SP4_NONE:
1158 break;
1159 case SP4_MACH_CRED:
1160 /* spo_must_enforce */
1161 READ_BUF(4);
1162 READ32(dummy);
1163 READ_BUF(dummy * 4);
1164 p += dummy;
1165
1166 /* spo_must_allow */
1167 READ_BUF(4);
1168 READ32(dummy);
1169 READ_BUF(dummy * 4);
1170 p += dummy;
1171 break;
1172 case SP4_SSV:
1173 /* ssp_ops */
1174 READ_BUF(4);
1175 READ32(dummy);
1176 READ_BUF(dummy * 4);
1177 p += dummy;
1178
1179 READ_BUF(4);
1180 READ32(dummy);
1181 READ_BUF(dummy * 4);
1182 p += dummy;
1183
1184 /* ssp_hash_algs<> */
1185 READ_BUF(4);
Mi Jinlong5afa0402010-11-09 09:39:23 +08001186 READ32(tmp);
1187 while (tmp--) {
1188 READ_BUF(4);
1189 READ32(dummy);
1190 READ_BUF(dummy);
1191 p += XDR_QUADLEN(dummy);
1192 }
Andy Adamson0733d212009-04-03 08:28:01 +03001193
1194 /* ssp_encr_algs<> */
1195 READ_BUF(4);
Mi Jinlong5afa0402010-11-09 09:39:23 +08001196 READ32(tmp);
1197 while (tmp--) {
1198 READ_BUF(4);
1199 READ32(dummy);
1200 READ_BUF(dummy);
1201 p += XDR_QUADLEN(dummy);
1202 }
Andy Adamson0733d212009-04-03 08:28:01 +03001203
1204 /* ssp_window and ssp_num_gss_handles */
1205 READ_BUF(8);
1206 READ32(dummy);
1207 READ32(dummy);
1208 break;
1209 default:
1210 goto xdr_error;
1211 }
1212
1213 /* Ignore Implementation ID */
1214 READ_BUF(4); /* nfs_impl_id4 array length */
1215 READ32(dummy);
1216
1217 if (dummy > 1)
1218 goto xdr_error;
1219
1220 if (dummy == 1) {
1221 /* nii_domain */
1222 READ_BUF(4);
1223 READ32(dummy);
1224 READ_BUF(dummy);
1225 p += XDR_QUADLEN(dummy);
1226
1227 /* nii_name */
1228 READ_BUF(4);
1229 READ32(dummy);
1230 READ_BUF(dummy);
1231 p += XDR_QUADLEN(dummy);
1232
1233 /* nii_date */
1234 READ_BUF(12);
1235 p += 3;
1236 }
1237 DECODE_TAIL;
Andy Adamson2db134e2009-04-03 08:27:55 +03001238}
1239
1240static __be32
1241nfsd4_decode_create_session(struct nfsd4_compoundargs *argp,
1242 struct nfsd4_create_session *sess)
1243{
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001244 DECODE_HEAD;
1245
1246 u32 dummy;
1247 char *machine_name;
Mi Jinlong5a02ab72011-03-11 12:13:55 +08001248 int i;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001249 int nr_secflavs;
1250
1251 READ_BUF(16);
1252 COPYMEM(&sess->clientid, 8);
1253 READ32(sess->seqid);
1254 READ32(sess->flags);
1255
1256 /* Fore channel attrs */
1257 READ_BUF(28);
1258 READ32(dummy); /* headerpadsz is always 0 */
1259 READ32(sess->fore_channel.maxreq_sz);
1260 READ32(sess->fore_channel.maxresp_sz);
1261 READ32(sess->fore_channel.maxresp_cached);
1262 READ32(sess->fore_channel.maxops);
1263 READ32(sess->fore_channel.maxreqs);
1264 READ32(sess->fore_channel.nr_rdma_attrs);
1265 if (sess->fore_channel.nr_rdma_attrs == 1) {
1266 READ_BUF(4);
1267 READ32(sess->fore_channel.rdma_attrs);
1268 } else if (sess->fore_channel.nr_rdma_attrs > 1) {
1269 dprintk("Too many fore channel attr bitmaps!\n");
1270 goto xdr_error;
1271 }
1272
1273 /* Back channel attrs */
1274 READ_BUF(28);
1275 READ32(dummy); /* headerpadsz is always 0 */
1276 READ32(sess->back_channel.maxreq_sz);
1277 READ32(sess->back_channel.maxresp_sz);
1278 READ32(sess->back_channel.maxresp_cached);
1279 READ32(sess->back_channel.maxops);
1280 READ32(sess->back_channel.maxreqs);
1281 READ32(sess->back_channel.nr_rdma_attrs);
1282 if (sess->back_channel.nr_rdma_attrs == 1) {
1283 READ_BUF(4);
1284 READ32(sess->back_channel.rdma_attrs);
1285 } else if (sess->back_channel.nr_rdma_attrs > 1) {
1286 dprintk("Too many back channel attr bitmaps!\n");
1287 goto xdr_error;
1288 }
1289
1290 READ_BUF(8);
1291 READ32(sess->callback_prog);
1292
1293 /* callback_sec_params4 */
1294 READ32(nr_secflavs);
1295 for (i = 0; i < nr_secflavs; ++i) {
1296 READ_BUF(4);
1297 READ32(dummy);
1298 switch (dummy) {
1299 case RPC_AUTH_NULL:
1300 /* Nothing to read */
1301 break;
1302 case RPC_AUTH_UNIX:
1303 READ_BUF(8);
1304 /* stamp */
1305 READ32(dummy);
1306
1307 /* machine name */
1308 READ32(dummy);
1309 READ_BUF(dummy);
1310 SAVEMEM(machine_name, dummy);
1311
1312 /* uid, gid */
1313 READ_BUF(8);
1314 READ32(sess->uid);
1315 READ32(sess->gid);
1316
1317 /* more gids */
1318 READ_BUF(4);
1319 READ32(dummy);
1320 READ_BUF(dummy * 4);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001321 break;
1322 case RPC_AUTH_GSS:
1323 dprintk("RPC_AUTH_GSS callback secflavor "
1324 "not supported!\n");
1325 READ_BUF(8);
1326 /* gcbp_service */
1327 READ32(dummy);
1328 /* gcbp_handle_from_server */
1329 READ32(dummy);
1330 READ_BUF(dummy);
1331 p += XDR_QUADLEN(dummy);
1332 /* gcbp_handle_from_client */
1333 READ_BUF(4);
1334 READ32(dummy);
1335 READ_BUF(dummy);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001336 break;
1337 default:
1338 dprintk("Illegal callback secflavor\n");
1339 return nfserr_inval;
1340 }
1341 }
1342 DECODE_TAIL;
Andy Adamson2db134e2009-04-03 08:27:55 +03001343}
1344
1345static __be32
1346nfsd4_decode_destroy_session(struct nfsd4_compoundargs *argp,
1347 struct nfsd4_destroy_session *destroy_session)
1348{
Benny Halevye10e0cf2009-04-03 08:28:38 +03001349 DECODE_HEAD;
1350 READ_BUF(NFS4_MAX_SESSIONID_LEN);
1351 COPYMEM(destroy_session->sessionid.data, NFS4_MAX_SESSIONID_LEN);
1352
1353 DECODE_TAIL;
Andy Adamson2db134e2009-04-03 08:27:55 +03001354}
1355
1356static __be32
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04001357nfsd4_decode_free_stateid(struct nfsd4_compoundargs *argp,
1358 struct nfsd4_free_stateid *free_stateid)
1359{
1360 DECODE_HEAD;
1361
1362 READ_BUF(sizeof(stateid_t));
1363 READ32(free_stateid->fr_stateid.si_generation);
1364 COPYMEM(&free_stateid->fr_stateid.si_opaque, sizeof(stateid_opaque_t));
1365
1366 DECODE_TAIL;
1367}
1368
1369static __be32
Andy Adamson2db134e2009-04-03 08:27:55 +03001370nfsd4_decode_sequence(struct nfsd4_compoundargs *argp,
1371 struct nfsd4_sequence *seq)
1372{
Benny Halevyb85d4c02009-04-03 08:28:08 +03001373 DECODE_HEAD;
1374
1375 READ_BUF(NFS4_MAX_SESSIONID_LEN + 16);
1376 COPYMEM(seq->sessionid.data, NFS4_MAX_SESSIONID_LEN);
1377 READ32(seq->seqid);
1378 READ32(seq->slotid);
1379 READ32(seq->maxslots);
1380 READ32(seq->cachethis);
1381
1382 DECODE_TAIL;
Andy Adamson2db134e2009-04-03 08:27:55 +03001383}
1384
Bryan Schumaker17456802011-07-13 10:50:48 -04001385static __be32
1386nfsd4_decode_test_stateid(struct nfsd4_compoundargs *argp, struct nfsd4_test_stateid *test_stateid)
1387{
Bryan Schumaker17456802011-07-13 10:50:48 -04001388 int i;
Bryan Schumaker03cfb422012-01-27 10:22:49 -05001389 __be32 *p, status;
1390 struct nfsd4_test_stateid_id *stateid;
Bryan Schumaker17456802011-07-13 10:50:48 -04001391
1392 READ_BUF(4);
1393 test_stateid->ts_num_ids = ntohl(*p++);
1394
Bryan Schumaker03cfb422012-01-27 10:22:49 -05001395 INIT_LIST_HEAD(&test_stateid->ts_stateid_list);
Bryan Schumaker17456802011-07-13 10:50:48 -04001396
1397 for (i = 0; i < test_stateid->ts_num_ids; i++) {
Bryan Schumaker03cfb422012-01-27 10:22:49 -05001398 stateid = kmalloc(sizeof(struct nfsd4_test_stateid_id), GFP_KERNEL);
1399 if (!stateid) {
Al Viroafcf6792012-04-13 00:15:37 -04001400 status = nfserrno(-ENOMEM);
Bryan Schumaker03cfb422012-01-27 10:22:49 -05001401 goto out;
1402 }
1403
1404 defer_free(argp, kfree, stateid);
1405 INIT_LIST_HEAD(&stateid->ts_id_list);
1406 list_add_tail(&stateid->ts_id_list, &test_stateid->ts_stateid_list);
1407
1408 status = nfsd4_decode_stateid(argp, &stateid->ts_id_stateid);
Bryan Schumaker17456802011-07-13 10:50:48 -04001409 if (status)
Bryan Schumaker03cfb422012-01-27 10:22:49 -05001410 goto out;
Bryan Schumaker17456802011-07-13 10:50:48 -04001411 }
1412
1413 status = 0;
1414out:
1415 return status;
1416xdr_error:
1417 dprintk("NFSD: xdr error (%s:%d)\n", __FILE__, __LINE__);
1418 status = nfserr_bad_xdr;
1419 goto out;
1420}
1421
Mi Jinlong345c2842011-10-20 17:51:39 +08001422static __be32 nfsd4_decode_destroy_clientid(struct nfsd4_compoundargs *argp, struct nfsd4_destroy_clientid *dc)
1423{
1424 DECODE_HEAD;
1425
1426 READ_BUF(8);
1427 COPYMEM(&dc->clientid, 8);
1428
1429 DECODE_TAIL;
1430}
1431
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04001432static __be32 nfsd4_decode_reclaim_complete(struct nfsd4_compoundargs *argp, struct nfsd4_reclaim_complete *rc)
1433{
1434 DECODE_HEAD;
1435
1436 READ_BUF(4);
1437 READ32(rc->rca_one_fs);
1438
1439 DECODE_TAIL;
1440}
1441
Andy Adamson2db134e2009-04-03 08:27:55 +03001442static __be32
Benny Halevy347e0ad2008-07-02 11:13:41 +03001443nfsd4_decode_noop(struct nfsd4_compoundargs *argp, void *p)
1444{
1445 return nfs_ok;
1446}
1447
Benny Halevy3c375c62008-07-02 11:14:01 +03001448static __be32
1449nfsd4_decode_notsupp(struct nfsd4_compoundargs *argp, void *p)
1450{
Benny Halevy1e685ec2009-03-04 23:06:06 +02001451 return nfserr_notsupp;
Benny Halevy3c375c62008-07-02 11:14:01 +03001452}
1453
Benny Halevy347e0ad2008-07-02 11:13:41 +03001454typedef __be32(*nfsd4_dec)(struct nfsd4_compoundargs *argp, void *);
1455
1456static nfsd4_dec nfsd4_dec_ops[] = {
J. Bruce Fieldsad1060c2008-07-18 15:04:16 -04001457 [OP_ACCESS] = (nfsd4_dec)nfsd4_decode_access,
1458 [OP_CLOSE] = (nfsd4_dec)nfsd4_decode_close,
1459 [OP_COMMIT] = (nfsd4_dec)nfsd4_decode_commit,
1460 [OP_CREATE] = (nfsd4_dec)nfsd4_decode_create,
1461 [OP_DELEGPURGE] = (nfsd4_dec)nfsd4_decode_notsupp,
1462 [OP_DELEGRETURN] = (nfsd4_dec)nfsd4_decode_delegreturn,
1463 [OP_GETATTR] = (nfsd4_dec)nfsd4_decode_getattr,
1464 [OP_GETFH] = (nfsd4_dec)nfsd4_decode_noop,
1465 [OP_LINK] = (nfsd4_dec)nfsd4_decode_link,
1466 [OP_LOCK] = (nfsd4_dec)nfsd4_decode_lock,
1467 [OP_LOCKT] = (nfsd4_dec)nfsd4_decode_lockt,
1468 [OP_LOCKU] = (nfsd4_dec)nfsd4_decode_locku,
1469 [OP_LOOKUP] = (nfsd4_dec)nfsd4_decode_lookup,
1470 [OP_LOOKUPP] = (nfsd4_dec)nfsd4_decode_noop,
1471 [OP_NVERIFY] = (nfsd4_dec)nfsd4_decode_verify,
1472 [OP_OPEN] = (nfsd4_dec)nfsd4_decode_open,
1473 [OP_OPENATTR] = (nfsd4_dec)nfsd4_decode_notsupp,
1474 [OP_OPEN_CONFIRM] = (nfsd4_dec)nfsd4_decode_open_confirm,
1475 [OP_OPEN_DOWNGRADE] = (nfsd4_dec)nfsd4_decode_open_downgrade,
1476 [OP_PUTFH] = (nfsd4_dec)nfsd4_decode_putfh,
J. Bruce Fieldsa1c8c4d2009-03-09 12:17:29 -04001477 [OP_PUTPUBFH] = (nfsd4_dec)nfsd4_decode_noop,
J. Bruce Fieldsad1060c2008-07-18 15:04:16 -04001478 [OP_PUTROOTFH] = (nfsd4_dec)nfsd4_decode_noop,
1479 [OP_READ] = (nfsd4_dec)nfsd4_decode_read,
1480 [OP_READDIR] = (nfsd4_dec)nfsd4_decode_readdir,
1481 [OP_READLINK] = (nfsd4_dec)nfsd4_decode_noop,
1482 [OP_REMOVE] = (nfsd4_dec)nfsd4_decode_remove,
1483 [OP_RENAME] = (nfsd4_dec)nfsd4_decode_rename,
1484 [OP_RENEW] = (nfsd4_dec)nfsd4_decode_renew,
1485 [OP_RESTOREFH] = (nfsd4_dec)nfsd4_decode_noop,
1486 [OP_SAVEFH] = (nfsd4_dec)nfsd4_decode_noop,
1487 [OP_SECINFO] = (nfsd4_dec)nfsd4_decode_secinfo,
1488 [OP_SETATTR] = (nfsd4_dec)nfsd4_decode_setattr,
1489 [OP_SETCLIENTID] = (nfsd4_dec)nfsd4_decode_setclientid,
1490 [OP_SETCLIENTID_CONFIRM] = (nfsd4_dec)nfsd4_decode_setclientid_confirm,
1491 [OP_VERIFY] = (nfsd4_dec)nfsd4_decode_verify,
1492 [OP_WRITE] = (nfsd4_dec)nfsd4_decode_write,
1493 [OP_RELEASE_LOCKOWNER] = (nfsd4_dec)nfsd4_decode_release_lockowner,
Benny Halevy347e0ad2008-07-02 11:13:41 +03001494};
1495
Andy Adamson2db134e2009-04-03 08:27:55 +03001496static nfsd4_dec nfsd41_dec_ops[] = {
Randy Dunlap9064caa2009-04-28 16:48:25 -07001497 [OP_ACCESS] = (nfsd4_dec)nfsd4_decode_access,
1498 [OP_CLOSE] = (nfsd4_dec)nfsd4_decode_close,
1499 [OP_COMMIT] = (nfsd4_dec)nfsd4_decode_commit,
1500 [OP_CREATE] = (nfsd4_dec)nfsd4_decode_create,
1501 [OP_DELEGPURGE] = (nfsd4_dec)nfsd4_decode_notsupp,
1502 [OP_DELEGRETURN] = (nfsd4_dec)nfsd4_decode_delegreturn,
1503 [OP_GETATTR] = (nfsd4_dec)nfsd4_decode_getattr,
1504 [OP_GETFH] = (nfsd4_dec)nfsd4_decode_noop,
1505 [OP_LINK] = (nfsd4_dec)nfsd4_decode_link,
1506 [OP_LOCK] = (nfsd4_dec)nfsd4_decode_lock,
1507 [OP_LOCKT] = (nfsd4_dec)nfsd4_decode_lockt,
1508 [OP_LOCKU] = (nfsd4_dec)nfsd4_decode_locku,
1509 [OP_LOOKUP] = (nfsd4_dec)nfsd4_decode_lookup,
1510 [OP_LOOKUPP] = (nfsd4_dec)nfsd4_decode_noop,
1511 [OP_NVERIFY] = (nfsd4_dec)nfsd4_decode_verify,
1512 [OP_OPEN] = (nfsd4_dec)nfsd4_decode_open,
1513 [OP_OPENATTR] = (nfsd4_dec)nfsd4_decode_notsupp,
1514 [OP_OPEN_CONFIRM] = (nfsd4_dec)nfsd4_decode_notsupp,
1515 [OP_OPEN_DOWNGRADE] = (nfsd4_dec)nfsd4_decode_open_downgrade,
1516 [OP_PUTFH] = (nfsd4_dec)nfsd4_decode_putfh,
1517 [OP_PUTPUBFH] = (nfsd4_dec)nfsd4_decode_notsupp,
1518 [OP_PUTROOTFH] = (nfsd4_dec)nfsd4_decode_noop,
1519 [OP_READ] = (nfsd4_dec)nfsd4_decode_read,
1520 [OP_READDIR] = (nfsd4_dec)nfsd4_decode_readdir,
1521 [OP_READLINK] = (nfsd4_dec)nfsd4_decode_noop,
1522 [OP_REMOVE] = (nfsd4_dec)nfsd4_decode_remove,
1523 [OP_RENAME] = (nfsd4_dec)nfsd4_decode_rename,
1524 [OP_RENEW] = (nfsd4_dec)nfsd4_decode_notsupp,
1525 [OP_RESTOREFH] = (nfsd4_dec)nfsd4_decode_noop,
1526 [OP_SAVEFH] = (nfsd4_dec)nfsd4_decode_noop,
1527 [OP_SECINFO] = (nfsd4_dec)nfsd4_decode_secinfo,
1528 [OP_SETATTR] = (nfsd4_dec)nfsd4_decode_setattr,
1529 [OP_SETCLIENTID] = (nfsd4_dec)nfsd4_decode_notsupp,
1530 [OP_SETCLIENTID_CONFIRM]= (nfsd4_dec)nfsd4_decode_notsupp,
1531 [OP_VERIFY] = (nfsd4_dec)nfsd4_decode_verify,
1532 [OP_WRITE] = (nfsd4_dec)nfsd4_decode_write,
1533 [OP_RELEASE_LOCKOWNER] = (nfsd4_dec)nfsd4_decode_notsupp,
Andy Adamson2db134e2009-04-03 08:27:55 +03001534
1535 /* new operations for NFSv4.1 */
Randy Dunlap9064caa2009-04-28 16:48:25 -07001536 [OP_BACKCHANNEL_CTL] = (nfsd4_dec)nfsd4_decode_notsupp,
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001537 [OP_BIND_CONN_TO_SESSION]= (nfsd4_dec)nfsd4_decode_bind_conn_to_session,
Randy Dunlap9064caa2009-04-28 16:48:25 -07001538 [OP_EXCHANGE_ID] = (nfsd4_dec)nfsd4_decode_exchange_id,
1539 [OP_CREATE_SESSION] = (nfsd4_dec)nfsd4_decode_create_session,
1540 [OP_DESTROY_SESSION] = (nfsd4_dec)nfsd4_decode_destroy_session,
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04001541 [OP_FREE_STATEID] = (nfsd4_dec)nfsd4_decode_free_stateid,
Randy Dunlap9064caa2009-04-28 16:48:25 -07001542 [OP_GET_DIR_DELEGATION] = (nfsd4_dec)nfsd4_decode_notsupp,
1543 [OP_GETDEVICEINFO] = (nfsd4_dec)nfsd4_decode_notsupp,
1544 [OP_GETDEVICELIST] = (nfsd4_dec)nfsd4_decode_notsupp,
1545 [OP_LAYOUTCOMMIT] = (nfsd4_dec)nfsd4_decode_notsupp,
1546 [OP_LAYOUTGET] = (nfsd4_dec)nfsd4_decode_notsupp,
1547 [OP_LAYOUTRETURN] = (nfsd4_dec)nfsd4_decode_notsupp,
J. Bruce Fields04f4ad12010-12-16 09:51:13 -05001548 [OP_SECINFO_NO_NAME] = (nfsd4_dec)nfsd4_decode_secinfo_no_name,
Randy Dunlap9064caa2009-04-28 16:48:25 -07001549 [OP_SEQUENCE] = (nfsd4_dec)nfsd4_decode_sequence,
1550 [OP_SET_SSV] = (nfsd4_dec)nfsd4_decode_notsupp,
Bryan Schumaker17456802011-07-13 10:50:48 -04001551 [OP_TEST_STATEID] = (nfsd4_dec)nfsd4_decode_test_stateid,
Randy Dunlap9064caa2009-04-28 16:48:25 -07001552 [OP_WANT_DELEGATION] = (nfsd4_dec)nfsd4_decode_notsupp,
Mi Jinlong345c2842011-10-20 17:51:39 +08001553 [OP_DESTROY_CLIENTID] = (nfsd4_dec)nfsd4_decode_destroy_clientid,
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04001554 [OP_RECLAIM_COMPLETE] = (nfsd4_dec)nfsd4_decode_reclaim_complete,
Andy Adamson2db134e2009-04-03 08:27:55 +03001555};
1556
Benny Halevyf2feb962008-07-02 11:14:22 +03001557struct nfsd4_minorversion_ops {
1558 nfsd4_dec *decoders;
1559 int nops;
1560};
1561
1562static struct nfsd4_minorversion_ops nfsd4_minorversion[] = {
J. Bruce Fieldsad1060c2008-07-18 15:04:16 -04001563 [0] = { nfsd4_dec_ops, ARRAY_SIZE(nfsd4_dec_ops) },
Andy Adamson2db134e2009-04-03 08:27:55 +03001564 [1] = { nfsd41_dec_ops, ARRAY_SIZE(nfsd41_dec_ops) },
Benny Halevyf2feb962008-07-02 11:14:22 +03001565};
1566
Benny Halevy347e0ad2008-07-02 11:13:41 +03001567static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568nfsd4_decode_compound(struct nfsd4_compoundargs *argp)
1569{
1570 DECODE_HEAD;
1571 struct nfsd4_op *op;
Benny Halevyf2feb962008-07-02 11:14:22 +03001572 struct nfsd4_minorversion_ops *ops;
J. Bruce Fields10910062011-01-24 12:11:02 -05001573 bool cachethis = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 int i;
1575
1576 /*
1577 * XXX: According to spec, we should check the tag
1578 * for UTF-8 compliance. I'm postponing this for
1579 * now because it seems that some clients do use
1580 * binary tags.
1581 */
1582 READ_BUF(4);
1583 READ32(argp->taglen);
1584 READ_BUF(argp->taglen + 8);
1585 SAVEMEM(argp->tag, argp->taglen);
1586 READ32(argp->minorversion);
1587 READ32(argp->opcnt);
1588
1589 if (argp->taglen > NFSD4_MAX_TAGLEN)
1590 goto xdr_error;
1591 if (argp->opcnt > 100)
1592 goto xdr_error;
1593
Tobias Klausere8c96f82006-03-24 03:15:34 -08001594 if (argp->opcnt > ARRAY_SIZE(argp->iops)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 argp->ops = kmalloc(argp->opcnt * sizeof(*argp->ops), GFP_KERNEL);
1596 if (!argp->ops) {
1597 argp->ops = argp->iops;
Chuck Lever817cb9d2007-09-11 18:01:20 -04001598 dprintk("nfsd: couldn't allocate room for COMPOUND\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 goto xdr_error;
1600 }
1601 }
1602
Benny Halevyf2feb962008-07-02 11:14:22 +03001603 if (argp->minorversion >= ARRAY_SIZE(nfsd4_minorversion))
Benny Halevy30cff1f2008-07-02 11:13:18 +03001604 argp->opcnt = 0;
1605
Benny Halevyf2feb962008-07-02 11:14:22 +03001606 ops = &nfsd4_minorversion[argp->minorversion];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 for (i = 0; i < argp->opcnt; i++) {
1608 op = &argp->ops[i];
1609 op->replay = NULL;
1610
1611 /*
1612 * We can't use READ_BUF() here because we need to handle
1613 * a missing opcode as an OP_WRITE + 1. So we need to check
1614 * to see if we're truly at the end of our buffer or if there
1615 * is another page we need to flip to.
1616 */
1617
1618 if (argp->p == argp->end) {
1619 if (argp->pagelen < 4) {
1620 /* There isn't an opcode still on the wire */
1621 op->opnum = OP_WRITE + 1;
1622 op->status = nfserr_bad_xdr;
1623 argp->opcnt = i+1;
1624 break;
1625 }
1626
1627 /*
1628 * False alarm. We just hit a page boundary, but there
1629 * is still data available. Move pointer across page
1630 * boundary. *snip from READ_BUF*
1631 */
1632 argp->p = page_address(argp->pagelist[0]);
1633 argp->pagelist++;
1634 if (argp->pagelen < PAGE_SIZE) {
Neil Brown2bc3c112010-04-20 12:16:52 +10001635 argp->end = argp->p + (argp->pagelen>>2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 argp->pagelen = 0;
1637 } else {
Neil Brown2bc3c112010-04-20 12:16:52 +10001638 argp->end = argp->p + (PAGE_SIZE>>2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 argp->pagelen -= PAGE_SIZE;
1640 }
1641 }
1642 op->opnum = ntohl(*argp->p++);
1643
Ricardo Labiagade3cab72009-12-11 20:03:27 -08001644 if (op->opnum >= FIRST_NFS4_OP && op->opnum <= LAST_NFS4_OP)
Benny Halevyf2feb962008-07-02 11:14:22 +03001645 op->status = ops->decoders[op->opnum](argp, &op->u);
Benny Halevy347e0ad2008-07-02 11:13:41 +03001646 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 op->opnum = OP_ILLEGAL;
1648 op->status = nfserr_op_illegal;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 }
1650
1651 if (op->status) {
1652 argp->opcnt = i+1;
1653 break;
1654 }
J. Bruce Fields10910062011-01-24 12:11:02 -05001655 /*
1656 * We'll try to cache the result in the DRC if any one
1657 * op in the compound wants to be cached:
1658 */
1659 cachethis |= nfsd4_cache_this_op(op);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 }
J. Bruce Fields10910062011-01-24 12:11:02 -05001661 /* Sessions make the DRC unnecessary: */
1662 if (argp->minorversion)
1663 cachethis = false;
1664 argp->rqstp->rq_cachetype = cachethis ? RC_REPLBUFF : RC_NOCACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665
1666 DECODE_TAIL;
1667}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669#define WRITE32(n) *p++ = htonl(n)
1670#define WRITE64(n) do { \
1671 *p++ = htonl((u32)((n) >> 32)); \
1672 *p++ = htonl((u32)(n)); \
1673} while (0)
Harvey Harrison5108b272008-07-17 21:33:04 -07001674#define WRITEMEM(ptr,nbytes) do { if (nbytes > 0) { \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 *(p + XDR_QUADLEN(nbytes) -1) = 0; \
1676 memcpy(p, ptr, nbytes); \
1677 p += XDR_QUADLEN(nbytes); \
Harvey Harrison5108b272008-07-17 21:33:04 -07001678}} while (0)
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04001679
1680static void write32(__be32 **p, u32 n)
1681{
1682 *(*p)++ = n;
1683}
1684
1685static void write64(__be32 **p, u64 n)
1686{
1687 write32(p, (u32)(n >> 32));
1688 write32(p, (u32)n);
1689}
1690
1691static void write_change(__be32 **p, struct kstat *stat, struct inode *inode)
1692{
1693 if (IS_I_VERSION(inode)) {
1694 write64(p, inode->i_version);
1695 } else {
1696 write32(p, stat->ctime.tv_sec);
1697 write32(p, stat->ctime.tv_nsec);
1698 }
1699}
1700
1701static void write_cinfo(__be32 **p, struct nfsd4_change_info *c)
1702{
1703 write32(p, c->atomic);
1704 if (c->change_supported) {
1705 write64(p, c->before_change);
1706 write64(p, c->after_change);
1707 } else {
1708 write32(p, c->before_ctime_sec);
1709 write32(p, c->before_ctime_nsec);
1710 write32(p, c->after_ctime_sec);
1711 write32(p, c->after_ctime_nsec);
1712 }
1713}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714
1715#define RESERVE_SPACE(nbytes) do { \
1716 p = resp->p; \
1717 BUG_ON(p + XDR_QUADLEN(nbytes) > resp->end); \
1718} while (0)
1719#define ADJUST_ARGS() resp->p = p
1720
1721/*
1722 * Header routine to setup seqid operation replay cache
1723 */
1724#define ENCODE_SEQID_OP_HEAD \
Al Viro2ebbc012006-10-19 23:28:58 -07001725 __be32 *save; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 \
1727 save = resp->p;
1728
J. Bruce Fieldse3d0b282011-08-10 19:16:22 -04001729static bool seqid_mutating_err(__be32 err)
1730{
1731 /* rfc 3530 section 8.1.5: */
1732 return err != nfserr_stale_clientid &&
1733 err != nfserr_stale_stateid &&
1734 err != nfserr_bad_stateid &&
1735 err != nfserr_bad_seqid &&
1736 err != nfserr_bad_xdr &&
1737 err != nfserr_resource &&
1738 err != nfserr_nofilehandle;
1739}
1740
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741/*
NeilBrown7fb64ce2005-07-07 17:59:20 -07001742 * Routine for encoding the result of a "seqid-mutating" NFSv4 operation. This
1743 * is where sequence id's are incremented, and the replay cache is filled.
1744 * Note that we increment sequence id's here, at the last moment, so we're sure
1745 * we know whether the error to be returned is a sequence id mutating error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 */
1747
J. Bruce Fieldsf3e42232011-08-24 12:27:31 -04001748static void encode_seqid_op_tail(struct nfsd4_compoundres *resp, __be32 *save, __be32 nfserr)
1749{
1750 struct nfs4_stateowner *stateowner = resp->cstate.replay_owner;
1751
1752 if (seqid_mutating_err(ntohl(nfserr)) && stateowner) {
1753 stateowner->so_seqid++;
1754 stateowner->so_replay.rp_status = nfserr;
1755 stateowner->so_replay.rp_buflen =
1756 (char *)resp->p - (char *)save;
1757 memcpy(stateowner->so_replay.rp_buf, save,
1758 stateowner->so_replay.rp_buflen);
J. Bruce Fields38c387b2011-09-16 17:42:48 -04001759 nfsd4_purge_closed_stateid(stateowner);
J. Bruce Fieldsf3e42232011-08-24 12:27:31 -04001760 }
1761}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001763/* Encode as an array of strings the string given with components
Daniel Mack3ad2f3f2010-02-03 08:01:28 +08001764 * separated @sep.
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001765 */
Al Virob37ad282006-10-19 23:28:59 -07001766static __be32 nfsd4_encode_components(char sep, char *components,
Al Viro2ebbc012006-10-19 23:28:58 -07001767 __be32 **pp, int *buflen)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001768{
Al Viro2ebbc012006-10-19 23:28:58 -07001769 __be32 *p = *pp;
1770 __be32 *countp = p;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001771 int strlen, count=0;
1772 char *str, *end;
1773
1774 dprintk("nfsd4_encode_components(%s)\n", components);
1775 if ((*buflen -= 4) < 0)
1776 return nfserr_resource;
1777 WRITE32(0); /* We will fill this in with @count later */
1778 end = str = components;
1779 while (*end) {
1780 for (; *end && (*end != sep); end++)
1781 ; /* Point to end of component */
1782 strlen = end - str;
1783 if (strlen) {
1784 if ((*buflen -= ((XDR_QUADLEN(strlen) << 2) + 4)) < 0)
1785 return nfserr_resource;
1786 WRITE32(strlen);
1787 WRITEMEM(str, strlen);
1788 count++;
1789 }
1790 else
1791 end++;
1792 str = end;
1793 }
1794 *pp = p;
1795 p = countp;
1796 WRITE32(count);
1797 return 0;
1798}
1799
1800/*
1801 * encode a location element of a fs_locations structure
1802 */
Al Virob37ad282006-10-19 23:28:59 -07001803static __be32 nfsd4_encode_fs_location4(struct nfsd4_fs_location *location,
Al Viro2ebbc012006-10-19 23:28:58 -07001804 __be32 **pp, int *buflen)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001805{
Al Virob37ad282006-10-19 23:28:59 -07001806 __be32 status;
Al Viro2ebbc012006-10-19 23:28:58 -07001807 __be32 *p = *pp;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001808
1809 status = nfsd4_encode_components(':', location->hosts, &p, buflen);
1810 if (status)
1811 return status;
1812 status = nfsd4_encode_components('/', location->path, &p, buflen);
1813 if (status)
1814 return status;
1815 *pp = p;
1816 return 0;
1817}
1818
1819/*
Trond Myklebusted748aa2011-09-12 19:37:06 -04001820 * Encode a path in RFC3530 'pathname4' format
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001821 */
Trond Myklebusted748aa2011-09-12 19:37:06 -04001822static __be32 nfsd4_encode_path(const struct path *root,
1823 const struct path *path, __be32 **pp, int *buflen)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001824{
Trond Myklebusted748aa2011-09-12 19:37:06 -04001825 struct path cur = {
1826 .mnt = path->mnt,
1827 .dentry = path->dentry,
1828 };
1829 __be32 *p = *pp;
1830 struct dentry **components = NULL;
1831 unsigned int ncomponents = 0;
1832 __be32 err = nfserr_jukebox;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001833
Trond Myklebusted748aa2011-09-12 19:37:06 -04001834 dprintk("nfsd4_encode_components(");
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001835
Trond Myklebusted748aa2011-09-12 19:37:06 -04001836 path_get(&cur);
1837 /* First walk the path up to the nfsd root, and store the
1838 * dentries/path components in an array.
1839 */
1840 for (;;) {
1841 if (cur.dentry == root->dentry && cur.mnt == root->mnt)
1842 break;
1843 if (cur.dentry == cur.mnt->mnt_root) {
1844 if (follow_up(&cur))
1845 continue;
1846 goto out_free;
1847 }
1848 if ((ncomponents & 15) == 0) {
1849 struct dentry **new;
1850 new = krealloc(components,
1851 sizeof(*new) * (ncomponents + 16),
1852 GFP_KERNEL);
1853 if (!new)
1854 goto out_free;
1855 components = new;
1856 }
1857 components[ncomponents++] = cur.dentry;
1858 cur.dentry = dget_parent(cur.dentry);
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001859 }
Trond Myklebusted748aa2011-09-12 19:37:06 -04001860
1861 *buflen -= 4;
1862 if (*buflen < 0)
1863 goto out_free;
1864 WRITE32(ncomponents);
1865
1866 while (ncomponents) {
1867 struct dentry *dentry = components[ncomponents - 1];
1868 unsigned int len = dentry->d_name.len;
1869
1870 *buflen -= 4 + (XDR_QUADLEN(len) << 2);
1871 if (*buflen < 0)
1872 goto out_free;
1873 WRITE32(len);
1874 WRITEMEM(dentry->d_name.name, len);
1875 dprintk("/%s", dentry->d_name.name);
1876 dput(dentry);
1877 ncomponents--;
1878 }
1879
1880 *pp = p;
1881 err = 0;
1882out_free:
1883 dprintk(")\n");
1884 while (ncomponents)
1885 dput(components[--ncomponents]);
1886 kfree(components);
1887 path_put(&cur);
1888 return err;
1889}
1890
1891static __be32 nfsd4_encode_fsloc_fsroot(struct svc_rqst *rqstp,
1892 const struct path *path, __be32 **pp, int *buflen)
1893{
1894 struct svc_export *exp_ps;
1895 __be32 res;
1896
1897 exp_ps = rqst_find_fsidzero_export(rqstp);
1898 if (IS_ERR(exp_ps))
1899 return nfserrno(PTR_ERR(exp_ps));
1900 res = nfsd4_encode_path(&exp_ps->ex_path, path, pp, buflen);
1901 exp_put(exp_ps);
1902 return res;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001903}
1904
1905/*
1906 * encode a fs_locations structure
1907 */
Al Virob37ad282006-10-19 23:28:59 -07001908static __be32 nfsd4_encode_fs_locations(struct svc_rqst *rqstp,
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001909 struct svc_export *exp,
Al Viro2ebbc012006-10-19 23:28:58 -07001910 __be32 **pp, int *buflen)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001911{
Al Virob37ad282006-10-19 23:28:59 -07001912 __be32 status;
Al Virocc45f012006-10-19 23:28:44 -07001913 int i;
Al Viro2ebbc012006-10-19 23:28:58 -07001914 __be32 *p = *pp;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001915 struct nfsd4_fs_locations *fslocs = &exp->ex_fslocs;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001916
Trond Myklebusted748aa2011-09-12 19:37:06 -04001917 status = nfsd4_encode_fsloc_fsroot(rqstp, &exp->ex_path, &p, buflen);
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001918 if (status)
1919 return status;
1920 if ((*buflen -= 4) < 0)
1921 return nfserr_resource;
1922 WRITE32(fslocs->locations_count);
1923 for (i=0; i<fslocs->locations_count; i++) {
1924 status = nfsd4_encode_fs_location4(&fslocs->locations[i],
1925 &p, buflen);
1926 if (status)
1927 return status;
1928 }
1929 *pp = p;
1930 return 0;
1931}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932
J. Bruce Fields3d2544b2011-08-15 11:49:30 -04001933static u32 nfs4_file_type(umode_t mode)
1934{
1935 switch (mode & S_IFMT) {
1936 case S_IFIFO: return NF4FIFO;
1937 case S_IFCHR: return NF4CHR;
1938 case S_IFDIR: return NF4DIR;
1939 case S_IFBLK: return NF4BLK;
1940 case S_IFLNK: return NF4LNK;
1941 case S_IFREG: return NF4REG;
1942 case S_IFSOCK: return NF4SOCK;
1943 default: return NF4BAD;
1944 };
1945}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946
Al Virob37ad282006-10-19 23:28:59 -07001947static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948nfsd4_encode_name(struct svc_rqst *rqstp, int whotype, uid_t id, int group,
Al Viro2ebbc012006-10-19 23:28:58 -07001949 __be32 **p, int *buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950{
1951 int status;
1952
1953 if (*buflen < (XDR_QUADLEN(IDMAP_NAMESZ) << 2) + 4)
1954 return nfserr_resource;
1955 if (whotype != NFS4_ACL_WHO_NAMED)
1956 status = nfs4_acl_write_who(whotype, (u8 *)(*p + 1));
1957 else if (group)
1958 status = nfsd_map_gid_to_name(rqstp, id, (u8 *)(*p + 1));
1959 else
1960 status = nfsd_map_uid_to_name(rqstp, id, (u8 *)(*p + 1));
1961 if (status < 0)
1962 return nfserrno(status);
1963 *p = xdr_encode_opaque(*p, NULL, status);
1964 *buflen -= (XDR_QUADLEN(status) << 2) + 4;
1965 BUG_ON(*buflen < 0);
1966 return 0;
1967}
1968
Al Virob37ad282006-10-19 23:28:59 -07001969static inline __be32
Al Viro2ebbc012006-10-19 23:28:58 -07001970nfsd4_encode_user(struct svc_rqst *rqstp, uid_t uid, __be32 **p, int *buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971{
1972 return nfsd4_encode_name(rqstp, NFS4_ACL_WHO_NAMED, uid, 0, p, buflen);
1973}
1974
Al Virob37ad282006-10-19 23:28:59 -07001975static inline __be32
Al Viro2ebbc012006-10-19 23:28:58 -07001976nfsd4_encode_group(struct svc_rqst *rqstp, uid_t gid, __be32 **p, int *buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977{
1978 return nfsd4_encode_name(rqstp, NFS4_ACL_WHO_NAMED, gid, 1, p, buflen);
1979}
1980
Al Virob37ad282006-10-19 23:28:59 -07001981static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982nfsd4_encode_aclname(struct svc_rqst *rqstp, int whotype, uid_t id, int group,
Al Viro2ebbc012006-10-19 23:28:58 -07001983 __be32 **p, int *buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984{
1985 return nfsd4_encode_name(rqstp, whotype, id, group, p, buflen);
1986}
1987
J.Bruce Fields42ca0992006-10-04 02:16:20 -07001988#define WORD0_ABSENT_FS_ATTRS (FATTR4_WORD0_FS_LOCATIONS | FATTR4_WORD0_FSID | \
1989 FATTR4_WORD0_RDATTR_ERROR)
1990#define WORD1_ABSENT_FS_ATTRS FATTR4_WORD1_MOUNTED_ON_FILEID
1991
Al Virob37ad282006-10-19 23:28:59 -07001992static __be32 fattr_handle_absent_fs(u32 *bmval0, u32 *bmval1, u32 *rdattr_err)
J.Bruce Fields42ca0992006-10-04 02:16:20 -07001993{
1994 /* As per referral draft: */
1995 if (*bmval0 & ~WORD0_ABSENT_FS_ATTRS ||
1996 *bmval1 & ~WORD1_ABSENT_FS_ATTRS) {
1997 if (*bmval0 & FATTR4_WORD0_RDATTR_ERROR ||
1998 *bmval0 & FATTR4_WORD0_FS_LOCATIONS)
1999 *rdattr_err = NFSERR_MOVED;
2000 else
2001 return nfserr_moved;
2002 }
2003 *bmval0 &= WORD0_ABSENT_FS_ATTRS;
2004 *bmval1 &= WORD1_ABSENT_FS_ATTRS;
2005 return 0;
2006}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007
2008/*
2009 * Note: @fhp can be NULL; in this case, we might have to compose the filehandle
2010 * ourselves.
2011 *
2012 * @countp is the buffer size in _words_; upon successful return this becomes
2013 * replaced with the number of words written.
2014 */
Al Virob37ad282006-10-19 23:28:59 -07002015__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016nfsd4_encode_fattr(struct svc_fh *fhp, struct svc_export *exp,
Al Viro2ebbc012006-10-19 23:28:58 -07002017 struct dentry *dentry, __be32 *buffer, int *countp, u32 *bmval,
Frank Filz406a7ea2007-11-27 11:34:05 -08002018 struct svc_rqst *rqstp, int ignore_crossmnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019{
2020 u32 bmval0 = bmval[0];
2021 u32 bmval1 = bmval[1];
Andy Adamson7e705702009-04-03 08:29:11 +03002022 u32 bmval2 = bmval[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 struct kstat stat;
2024 struct svc_fh tempfh;
2025 struct kstatfs statfs;
2026 int buflen = *countp << 2;
Al Viro2ebbc012006-10-19 23:28:58 -07002027 __be32 *attrlenp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 u32 dummy;
2029 u64 dummy64;
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002030 u32 rdattr_err = 0;
Al Viro2ebbc012006-10-19 23:28:58 -07002031 __be32 *p = buffer;
Al Virob37ad282006-10-19 23:28:59 -07002032 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07002033 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 int aclsupport = 0;
2035 struct nfs4_acl *acl = NULL;
Andy Adamson7e705702009-04-03 08:29:11 +03002036 struct nfsd4_compoundres *resp = rqstp->rq_resp;
2037 u32 minorversion = resp->cstate.minorversion;
Christoph Hellwigebabe9a2010-07-07 18:53:11 +02002038 struct path path = {
2039 .mnt = exp->ex_path.mnt,
2040 .dentry = dentry,
2041 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042
2043 BUG_ON(bmval1 & NFSD_WRITEONLY_ATTRS_WORD1);
Andy Adamson7e705702009-04-03 08:29:11 +03002044 BUG_ON(bmval0 & ~nfsd_suppattrs0(minorversion));
2045 BUG_ON(bmval1 & ~nfsd_suppattrs1(minorversion));
2046 BUG_ON(bmval2 & ~nfsd_suppattrs2(minorversion));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002048 if (exp->ex_fslocs.migrated) {
Andy Adamson7e705702009-04-03 08:29:11 +03002049 BUG_ON(bmval[2]);
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002050 status = fattr_handle_absent_fs(&bmval0, &bmval1, &rdattr_err);
2051 if (status)
2052 goto out;
2053 }
2054
Jan Blunck54775492008-02-14 19:38:39 -08002055 err = vfs_getattr(exp->ex_path.mnt, dentry, &stat);
Al Virob8dd7b92006-10-19 23:29:01 -07002056 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 goto out_nfserr;
Christoph Hellwigbdef8302014-05-28 10:46:13 +02002058 if ((bmval0 & (FATTR4_WORD0_FILES_AVAIL | FATTR4_WORD0_FILES_FREE |
2059 FATTR4_WORD0_FILES_TOTAL | FATTR4_WORD0_MAXNAME)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 (bmval1 & (FATTR4_WORD1_SPACE_AVAIL | FATTR4_WORD1_SPACE_FREE |
2061 FATTR4_WORD1_SPACE_TOTAL))) {
Christoph Hellwigebabe9a2010-07-07 18:53:11 +02002062 err = vfs_statfs(&path, &statfs);
Al Virob8dd7b92006-10-19 23:29:01 -07002063 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 goto out_nfserr;
2065 }
2066 if ((bmval0 & (FATTR4_WORD0_FILEHANDLE | FATTR4_WORD0_FSID)) && !fhp) {
2067 fh_init(&tempfh, NFS4_FHSIZE);
2068 status = fh_compose(&tempfh, exp, dentry, NULL);
2069 if (status)
2070 goto out;
2071 fhp = &tempfh;
2072 }
2073 if (bmval0 & (FATTR4_WORD0_ACL | FATTR4_WORD0_ACLSUPPORT
2074 | FATTR4_WORD0_SUPPORTED_ATTRS)) {
Al Virob8dd7b92006-10-19 23:29:01 -07002075 err = nfsd4_get_nfs4_acl(rqstp, dentry, &acl);
2076 aclsupport = (err == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 if (bmval0 & FATTR4_WORD0_ACL) {
Al Virob8dd7b92006-10-19 23:29:01 -07002078 if (err == -EOPNOTSUPP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 bmval0 &= ~FATTR4_WORD0_ACL;
Al Virob8dd7b92006-10-19 23:29:01 -07002080 else if (err == -EINVAL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 status = nfserr_attrnotsupp;
2082 goto out;
Al Virob8dd7b92006-10-19 23:29:01 -07002083 } else if (err != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 goto out_nfserr;
2085 }
2086 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087
Benny Halevy2b44f1b2010-09-30 20:47:46 +02002088 if (bmval2) {
2089 if ((buflen -= 16) < 0)
2090 goto out_resource;
Andy Adamson7e705702009-04-03 08:29:11 +03002091 WRITE32(3);
2092 WRITE32(bmval0);
2093 WRITE32(bmval1);
2094 WRITE32(bmval2);
Benny Halevy2b44f1b2010-09-30 20:47:46 +02002095 } else if (bmval1) {
2096 if ((buflen -= 12) < 0)
2097 goto out_resource;
Andy Adamson7e705702009-04-03 08:29:11 +03002098 WRITE32(2);
2099 WRITE32(bmval0);
2100 WRITE32(bmval1);
2101 } else {
Benny Halevy2b44f1b2010-09-30 20:47:46 +02002102 if ((buflen -= 8) < 0)
2103 goto out_resource;
Andy Adamson7e705702009-04-03 08:29:11 +03002104 WRITE32(1);
2105 WRITE32(bmval0);
2106 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 attrlenp = p++; /* to be backfilled later */
2108
2109 if (bmval0 & FATTR4_WORD0_SUPPORTED_ATTRS) {
Andy Adamson7e705702009-04-03 08:29:11 +03002110 u32 word0 = nfsd_suppattrs0(minorversion);
2111 u32 word1 = nfsd_suppattrs1(minorversion);
2112 u32 word2 = nfsd_suppattrs2(minorversion);
2113
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002114 if (!aclsupport)
2115 word0 &= ~FATTR4_WORD0_ACL;
Andy Adamson7e705702009-04-03 08:29:11 +03002116 if (!word2) {
Benny Halevy2b44f1b2010-09-30 20:47:46 +02002117 if ((buflen -= 12) < 0)
2118 goto out_resource;
Andy Adamson7e705702009-04-03 08:29:11 +03002119 WRITE32(2);
2120 WRITE32(word0);
2121 WRITE32(word1);
2122 } else {
Benny Halevy2b44f1b2010-09-30 20:47:46 +02002123 if ((buflen -= 16) < 0)
2124 goto out_resource;
Andy Adamson7e705702009-04-03 08:29:11 +03002125 WRITE32(3);
2126 WRITE32(word0);
2127 WRITE32(word1);
2128 WRITE32(word2);
2129 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 }
2131 if (bmval0 & FATTR4_WORD0_TYPE) {
2132 if ((buflen -= 4) < 0)
2133 goto out_resource;
J. Bruce Fields3d2544b2011-08-15 11:49:30 -04002134 dummy = nfs4_file_type(stat.mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 if (dummy == NF4BAD)
2136 goto out_serverfault;
2137 WRITE32(dummy);
2138 }
2139 if (bmval0 & FATTR4_WORD0_FH_EXPIRE_TYPE) {
2140 if ((buflen -= 4) < 0)
2141 goto out_resource;
NeilBrown49640002005-06-23 22:02:58 -07002142 if (exp->ex_flags & NFSEXP_NOSUBTREECHECK)
NeilBrowne34ac862005-07-07 17:59:30 -07002143 WRITE32(NFS4_FH_PERSISTENT);
NeilBrown49640002005-06-23 22:02:58 -07002144 else
NeilBrowne34ac862005-07-07 17:59:30 -07002145 WRITE32(NFS4_FH_PERSISTENT|NFS4_FH_VOL_RENAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 }
2147 if (bmval0 & FATTR4_WORD0_CHANGE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 if ((buflen -= 8) < 0)
2149 goto out_resource;
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002150 write_change(&p, &stat, dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 }
2152 if (bmval0 & FATTR4_WORD0_SIZE) {
2153 if ((buflen -= 8) < 0)
2154 goto out_resource;
2155 WRITE64(stat.size);
2156 }
2157 if (bmval0 & FATTR4_WORD0_LINK_SUPPORT) {
2158 if ((buflen -= 4) < 0)
2159 goto out_resource;
2160 WRITE32(1);
2161 }
2162 if (bmval0 & FATTR4_WORD0_SYMLINK_SUPPORT) {
2163 if ((buflen -= 4) < 0)
2164 goto out_resource;
2165 WRITE32(1);
2166 }
2167 if (bmval0 & FATTR4_WORD0_NAMED_ATTR) {
2168 if ((buflen -= 4) < 0)
2169 goto out_resource;
2170 WRITE32(0);
2171 }
2172 if (bmval0 & FATTR4_WORD0_FSID) {
2173 if ((buflen -= 16) < 0)
2174 goto out_resource;
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002175 if (exp->ex_fslocs.migrated) {
2176 WRITE64(NFS4_REFERRAL_FSID_MAJOR);
2177 WRITE64(NFS4_REFERRAL_FSID_MINOR);
NeilBrownaf6a4e22007-02-14 00:33:12 -08002178 } else switch(fsid_source(fhp)) {
2179 case FSIDSOURCE_FSID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 WRITE64((u64)exp->ex_fsid);
2181 WRITE64((u64)0);
NeilBrownaf6a4e22007-02-14 00:33:12 -08002182 break;
2183 case FSIDSOURCE_DEV:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 WRITE32(0);
2185 WRITE32(MAJOR(stat.dev));
2186 WRITE32(0);
2187 WRITE32(MINOR(stat.dev));
NeilBrownaf6a4e22007-02-14 00:33:12 -08002188 break;
2189 case FSIDSOURCE_UUID:
2190 WRITEMEM(exp->ex_uuid, 16);
2191 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 }
2193 }
2194 if (bmval0 & FATTR4_WORD0_UNIQUE_HANDLES) {
2195 if ((buflen -= 4) < 0)
2196 goto out_resource;
2197 WRITE32(0);
2198 }
2199 if (bmval0 & FATTR4_WORD0_LEASE_TIME) {
2200 if ((buflen -= 4) < 0)
2201 goto out_resource;
J. Bruce Fieldscf07d2e2010-02-28 23:20:19 -05002202 WRITE32(nfsd4_lease);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 }
2204 if (bmval0 & FATTR4_WORD0_RDATTR_ERROR) {
2205 if ((buflen -= 4) < 0)
2206 goto out_resource;
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002207 WRITE32(rdattr_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 }
2209 if (bmval0 & FATTR4_WORD0_ACL) {
2210 struct nfs4_ace *ace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211
2212 if (acl == NULL) {
2213 if ((buflen -= 4) < 0)
2214 goto out_resource;
2215
2216 WRITE32(0);
2217 goto out_acl;
2218 }
2219 if ((buflen -= 4) < 0)
2220 goto out_resource;
2221 WRITE32(acl->naces);
2222
J. Bruce Fields28e05dd2007-02-16 01:28:30 -08002223 for (ace = acl->aces; ace < acl->aces + acl->naces; ace++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 if ((buflen -= 4*3) < 0)
2225 goto out_resource;
2226 WRITE32(ace->type);
2227 WRITE32(ace->flag);
2228 WRITE32(ace->access_mask & NFS4_ACE_MASK_ALL);
2229 status = nfsd4_encode_aclname(rqstp, ace->whotype,
2230 ace->who, ace->flag & NFS4_ACE_IDENTIFIER_GROUP,
2231 &p, &buflen);
2232 if (status == nfserr_resource)
2233 goto out_resource;
2234 if (status)
2235 goto out;
2236 }
2237 }
2238out_acl:
2239 if (bmval0 & FATTR4_WORD0_ACLSUPPORT) {
2240 if ((buflen -= 4) < 0)
2241 goto out_resource;
2242 WRITE32(aclsupport ?
2243 ACL4_SUPPORT_ALLOW_ACL|ACL4_SUPPORT_DENY_ACL : 0);
2244 }
2245 if (bmval0 & FATTR4_WORD0_CANSETTIME) {
2246 if ((buflen -= 4) < 0)
2247 goto out_resource;
2248 WRITE32(1);
2249 }
2250 if (bmval0 & FATTR4_WORD0_CASE_INSENSITIVE) {
2251 if ((buflen -= 4) < 0)
2252 goto out_resource;
J. Bruce Fieldsf0f5dcc2012-06-05 16:52:06 -04002253 WRITE32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 }
2255 if (bmval0 & FATTR4_WORD0_CASE_PRESERVING) {
2256 if ((buflen -= 4) < 0)
2257 goto out_resource;
2258 WRITE32(1);
2259 }
2260 if (bmval0 & FATTR4_WORD0_CHOWN_RESTRICTED) {
2261 if ((buflen -= 4) < 0)
2262 goto out_resource;
2263 WRITE32(1);
2264 }
2265 if (bmval0 & FATTR4_WORD0_FILEHANDLE) {
2266 buflen -= (XDR_QUADLEN(fhp->fh_handle.fh_size) << 2) + 4;
2267 if (buflen < 0)
2268 goto out_resource;
2269 WRITE32(fhp->fh_handle.fh_size);
2270 WRITEMEM(&fhp->fh_handle.fh_base, fhp->fh_handle.fh_size);
2271 }
2272 if (bmval0 & FATTR4_WORD0_FILEID) {
2273 if ((buflen -= 8) < 0)
2274 goto out_resource;
Peter Staubach40ee5dc2007-08-16 12:10:07 -04002275 WRITE64(stat.ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 }
2277 if (bmval0 & FATTR4_WORD0_FILES_AVAIL) {
2278 if ((buflen -= 8) < 0)
2279 goto out_resource;
2280 WRITE64((u64) statfs.f_ffree);
2281 }
2282 if (bmval0 & FATTR4_WORD0_FILES_FREE) {
2283 if ((buflen -= 8) < 0)
2284 goto out_resource;
2285 WRITE64((u64) statfs.f_ffree);
2286 }
2287 if (bmval0 & FATTR4_WORD0_FILES_TOTAL) {
2288 if ((buflen -= 8) < 0)
2289 goto out_resource;
2290 WRITE64((u64) statfs.f_files);
2291 }
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002292 if (bmval0 & FATTR4_WORD0_FS_LOCATIONS) {
2293 status = nfsd4_encode_fs_locations(rqstp, exp, &p, &buflen);
2294 if (status == nfserr_resource)
2295 goto out_resource;
2296 if (status)
2297 goto out;
2298 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 if (bmval0 & FATTR4_WORD0_HOMOGENEOUS) {
2300 if ((buflen -= 4) < 0)
2301 goto out_resource;
2302 WRITE32(1);
2303 }
2304 if (bmval0 & FATTR4_WORD0_MAXFILESIZE) {
2305 if ((buflen -= 8) < 0)
2306 goto out_resource;
2307 WRITE64(~(u64)0);
2308 }
2309 if (bmval0 & FATTR4_WORD0_MAXLINK) {
2310 if ((buflen -= 4) < 0)
2311 goto out_resource;
2312 WRITE32(255);
2313 }
2314 if (bmval0 & FATTR4_WORD0_MAXNAME) {
2315 if ((buflen -= 4) < 0)
2316 goto out_resource;
J. Bruce Fieldsa16e92e2007-09-28 16:45:51 -04002317 WRITE32(statfs.f_namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 }
2319 if (bmval0 & FATTR4_WORD0_MAXREAD) {
2320 if ((buflen -= 8) < 0)
2321 goto out_resource;
Greg Banks7adae482006-10-04 02:15:47 -07002322 WRITE64((u64) svc_max_payload(rqstp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 }
2324 if (bmval0 & FATTR4_WORD0_MAXWRITE) {
2325 if ((buflen -= 8) < 0)
2326 goto out_resource;
Greg Banks7adae482006-10-04 02:15:47 -07002327 WRITE64((u64) svc_max_payload(rqstp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 }
2329 if (bmval1 & FATTR4_WORD1_MODE) {
2330 if ((buflen -= 4) < 0)
2331 goto out_resource;
2332 WRITE32(stat.mode & S_IALLUGO);
2333 }
2334 if (bmval1 & FATTR4_WORD1_NO_TRUNC) {
2335 if ((buflen -= 4) < 0)
2336 goto out_resource;
2337 WRITE32(1);
2338 }
2339 if (bmval1 & FATTR4_WORD1_NUMLINKS) {
2340 if ((buflen -= 4) < 0)
2341 goto out_resource;
2342 WRITE32(stat.nlink);
2343 }
2344 if (bmval1 & FATTR4_WORD1_OWNER) {
2345 status = nfsd4_encode_user(rqstp, stat.uid, &p, &buflen);
2346 if (status == nfserr_resource)
2347 goto out_resource;
2348 if (status)
2349 goto out;
2350 }
2351 if (bmval1 & FATTR4_WORD1_OWNER_GROUP) {
2352 status = nfsd4_encode_group(rqstp, stat.gid, &p, &buflen);
2353 if (status == nfserr_resource)
2354 goto out_resource;
2355 if (status)
2356 goto out;
2357 }
2358 if (bmval1 & FATTR4_WORD1_RAWDEV) {
2359 if ((buflen -= 8) < 0)
2360 goto out_resource;
2361 WRITE32((u32) MAJOR(stat.rdev));
2362 WRITE32((u32) MINOR(stat.rdev));
2363 }
2364 if (bmval1 & FATTR4_WORD1_SPACE_AVAIL) {
2365 if ((buflen -= 8) < 0)
2366 goto out_resource;
2367 dummy64 = (u64)statfs.f_bavail * (u64)statfs.f_bsize;
2368 WRITE64(dummy64);
2369 }
2370 if (bmval1 & FATTR4_WORD1_SPACE_FREE) {
2371 if ((buflen -= 8) < 0)
2372 goto out_resource;
2373 dummy64 = (u64)statfs.f_bfree * (u64)statfs.f_bsize;
2374 WRITE64(dummy64);
2375 }
2376 if (bmval1 & FATTR4_WORD1_SPACE_TOTAL) {
2377 if ((buflen -= 8) < 0)
2378 goto out_resource;
2379 dummy64 = (u64)statfs.f_blocks * (u64)statfs.f_bsize;
2380 WRITE64(dummy64);
2381 }
2382 if (bmval1 & FATTR4_WORD1_SPACE_USED) {
2383 if ((buflen -= 8) < 0)
2384 goto out_resource;
2385 dummy64 = (u64)stat.blocks << 9;
2386 WRITE64(dummy64);
2387 }
2388 if (bmval1 & FATTR4_WORD1_TIME_ACCESS) {
2389 if ((buflen -= 12) < 0)
2390 goto out_resource;
Bryan Schumaker2111a772013-04-19 16:09:38 -04002391 WRITE64((s64)stat.atime.tv_sec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 WRITE32(stat.atime.tv_nsec);
2393 }
2394 if (bmval1 & FATTR4_WORD1_TIME_DELTA) {
2395 if ((buflen -= 12) < 0)
2396 goto out_resource;
2397 WRITE32(0);
2398 WRITE32(1);
2399 WRITE32(0);
2400 }
2401 if (bmval1 & FATTR4_WORD1_TIME_METADATA) {
2402 if ((buflen -= 12) < 0)
2403 goto out_resource;
Bryan Schumaker2111a772013-04-19 16:09:38 -04002404 WRITE64((s64)stat.ctime.tv_sec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 WRITE32(stat.ctime.tv_nsec);
2406 }
2407 if (bmval1 & FATTR4_WORD1_TIME_MODIFY) {
2408 if ((buflen -= 12) < 0)
2409 goto out_resource;
Bryan Schumaker2111a772013-04-19 16:09:38 -04002410 WRITE64((s64)stat.mtime.tv_sec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 WRITE32(stat.mtime.tv_nsec);
2412 }
2413 if (bmval1 & FATTR4_WORD1_MOUNTED_ON_FILEID) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 if ((buflen -= 8) < 0)
2415 goto out_resource;
Frank Filz406a7ea2007-11-27 11:34:05 -08002416 /*
2417 * Get parent's attributes if not ignoring crossmount
2418 * and this is the root of a cross-mounted filesystem.
2419 */
2420 if (ignore_crossmnt == 0 &&
Al Viro462d6052010-01-30 16:11:21 -05002421 dentry == exp->ex_path.mnt->mnt_root) {
2422 struct path path = exp->ex_path;
2423 path_get(&path);
2424 while (follow_up(&path)) {
2425 if (path.dentry != path.mnt->mnt_root)
2426 break;
2427 }
2428 err = vfs_getattr(path.mnt, path.dentry, &stat);
2429 path_put(&path);
Peter Staubach40ee5dc2007-08-16 12:10:07 -04002430 if (err)
2431 goto out_nfserr;
2432 }
2433 WRITE64(stat.ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 }
Benny Halevy8c18f202009-04-03 08:29:14 +03002435 if (bmval2 & FATTR4_WORD2_SUPPATTR_EXCLCREAT) {
J. Bruce Fields9acc5312014-01-28 16:05:15 -05002436 if ((buflen -= 16) < 0)
2437 goto out_resource;
Benny Halevy8c18f202009-04-03 08:29:14 +03002438 WRITE32(3);
2439 WRITE32(NFSD_SUPPATTR_EXCLCREAT_WORD0);
2440 WRITE32(NFSD_SUPPATTR_EXCLCREAT_WORD1);
2441 WRITE32(NFSD_SUPPATTR_EXCLCREAT_WORD2);
2442 }
Andy Adamson7e705702009-04-03 08:29:11 +03002443
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 *attrlenp = htonl((char *)p - (char *)attrlenp - 4);
2445 *countp = p - buffer;
2446 status = nfs_ok;
2447
2448out:
J. Bruce Fields28e05dd2007-02-16 01:28:30 -08002449 kfree(acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 if (fhp == &tempfh)
2451 fh_put(&tempfh);
2452 return status;
2453out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07002454 status = nfserrno(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 goto out;
2456out_resource:
2457 *countp = 0;
2458 status = nfserr_resource;
2459 goto out;
2460out_serverfault:
2461 status = nfserr_serverfault;
2462 goto out;
2463}
2464
J. Bruce Fieldsc0ce6ec2008-02-11 15:48:47 -05002465static inline int attributes_need_mount(u32 *bmval)
2466{
2467 if (bmval[0] & ~(FATTR4_WORD0_RDATTR_ERROR | FATTR4_WORD0_LEASE_TIME))
2468 return 1;
2469 if (bmval[1] & ~FATTR4_WORD1_MOUNTED_ON_FILEID)
2470 return 1;
2471 return 0;
2472}
2473
Al Virob37ad282006-10-19 23:28:59 -07002474static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475nfsd4_encode_dirent_fattr(struct nfsd4_readdir *cd,
Al Viro2ebbc012006-10-19 23:28:58 -07002476 const char *name, int namlen, __be32 *p, int *buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477{
2478 struct svc_export *exp = cd->rd_fhp->fh_export;
2479 struct dentry *dentry;
Al Virob37ad282006-10-19 23:28:59 -07002480 __be32 nfserr;
Frank Filz406a7ea2007-11-27 11:34:05 -08002481 int ignore_crossmnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482
2483 dentry = lookup_one_len(name, cd->rd_fhp->fh_dentry, namlen);
2484 if (IS_ERR(dentry))
2485 return nfserrno(PTR_ERR(dentry));
J. Bruce Fieldsb2c0cea2009-05-05 19:04:29 -04002486 if (!dentry->d_inode) {
2487 /*
2488 * nfsd_buffered_readdir drops the i_mutex between
2489 * readdir and calling this callback, leaving a window
2490 * where this directory entry could have gone away.
2491 */
2492 dput(dentry);
2493 return nfserr_noent;
2494 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495
2496 exp_get(exp);
Frank Filz406a7ea2007-11-27 11:34:05 -08002497 /*
2498 * In the case of a mountpoint, the client may be asking for
2499 * attributes that are only properties of the underlying filesystem
2500 * as opposed to the cross-mounted file system. In such a case,
2501 * we will not follow the cross mount and will fill the attribtutes
2502 * directly from the mountpoint dentry.
2503 */
J. Bruce Fields3227fa42009-10-25 21:43:01 -04002504 if (nfsd_mountpoint(dentry, exp)) {
J.Bruce Fields021d3a72006-12-13 00:35:24 -08002505 int err;
2506
J. Bruce Fields3227fa42009-10-25 21:43:01 -04002507 if (!(exp->ex_flags & NFSEXP_V4ROOT)
2508 && !attributes_need_mount(cd->rd_bmval)) {
2509 ignore_crossmnt = 1;
2510 goto out_encode;
2511 }
Andy Adamsondcb488a2007-07-17 04:04:51 -07002512 /*
2513 * Why the heck aren't we just using nfsd_lookup??
2514 * Different "."/".." handling? Something else?
2515 * At least, add a comment here to explain....
2516 */
J.Bruce Fields021d3a72006-12-13 00:35:24 -08002517 err = nfsd_cross_mnt(cd->rd_rqstp, &dentry, &exp);
2518 if (err) {
2519 nfserr = nfserrno(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 goto out_put;
2521 }
Andy Adamsondcb488a2007-07-17 04:04:51 -07002522 nfserr = check_nfsd_access(exp, cd->rd_rqstp);
2523 if (nfserr)
2524 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525
2526 }
J. Bruce Fields3227fa42009-10-25 21:43:01 -04002527out_encode:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 nfserr = nfsd4_encode_fattr(NULL, exp, dentry, p, buflen, cd->rd_bmval,
Frank Filz406a7ea2007-11-27 11:34:05 -08002529 cd->rd_rqstp, ignore_crossmnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530out_put:
2531 dput(dentry);
2532 exp_put(exp);
2533 return nfserr;
2534}
2535
Al Viro2ebbc012006-10-19 23:28:58 -07002536static __be32 *
Al Virob37ad282006-10-19 23:28:59 -07002537nfsd4_encode_rdattr_error(__be32 *p, int buflen, __be32 nfserr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538{
Al Viro2ebbc012006-10-19 23:28:58 -07002539 __be32 *attrlenp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540
2541 if (buflen < 6)
2542 return NULL;
2543 *p++ = htonl(2);
2544 *p++ = htonl(FATTR4_WORD0_RDATTR_ERROR); /* bmval0 */
2545 *p++ = htonl(0); /* bmval1 */
2546
2547 attrlenp = p++;
2548 *p++ = nfserr; /* no htonl */
2549 *attrlenp = htonl((char *)p - (char *)attrlenp - 4);
2550 return p;
2551}
2552
2553static int
NeilBrowna0ad13e2007-01-26 00:57:10 -08002554nfsd4_encode_dirent(void *ccdv, const char *name, int namlen,
2555 loff_t offset, u64 ino, unsigned int d_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556{
NeilBrowna0ad13e2007-01-26 00:57:10 -08002557 struct readdir_cd *ccd = ccdv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 struct nfsd4_readdir *cd = container_of(ccd, struct nfsd4_readdir, common);
2559 int buflen;
Al Viro2ebbc012006-10-19 23:28:58 -07002560 __be32 *p = cd->buffer;
J. Bruce Fieldsb2c0cea2009-05-05 19:04:29 -04002561 __be32 *cookiep;
Al Virob37ad282006-10-19 23:28:59 -07002562 __be32 nfserr = nfserr_toosmall;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563
2564 /* In nfsv4, "." and ".." never make it onto the wire.. */
2565 if (name && isdotent(name, namlen)) {
2566 cd->common.err = nfs_ok;
2567 return 0;
2568 }
2569
2570 if (cd->offset)
2571 xdr_encode_hyper(cd->offset, (u64) offset);
2572
2573 buflen = cd->buflen - 4 - XDR_QUADLEN(namlen);
2574 if (buflen < 0)
2575 goto fail;
2576
2577 *p++ = xdr_one; /* mark entry present */
J. Bruce Fieldsb2c0cea2009-05-05 19:04:29 -04002578 cookiep = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 p = xdr_encode_hyper(p, NFS_OFFSET_MAX); /* offset of next entry */
2580 p = xdr_encode_array(p, name, namlen); /* name length & name */
2581
2582 nfserr = nfsd4_encode_dirent_fattr(cd, name, namlen, p, &buflen);
2583 switch (nfserr) {
2584 case nfs_ok:
2585 p += buflen;
2586 break;
2587 case nfserr_resource:
2588 nfserr = nfserr_toosmall;
2589 goto fail;
J. Bruce Fieldsb2c0cea2009-05-05 19:04:29 -04002590 case nfserr_noent:
2591 goto skip_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 default:
2593 /*
2594 * If the client requested the RDATTR_ERROR attribute,
2595 * we stuff the error code into this attribute
2596 * and continue. If this attribute was not requested,
2597 * then in accordance with the spec, we fail the
2598 * entire READDIR operation(!)
2599 */
2600 if (!(cd->rd_bmval[0] & FATTR4_WORD0_RDATTR_ERROR))
2601 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 p = nfsd4_encode_rdattr_error(p, buflen, nfserr);
Fred Isaman34081ef2006-01-18 17:43:40 -08002603 if (p == NULL) {
2604 nfserr = nfserr_toosmall;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 goto fail;
Fred Isaman34081ef2006-01-18 17:43:40 -08002606 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 }
2608 cd->buflen -= (p - cd->buffer);
2609 cd->buffer = p;
J. Bruce Fieldsb2c0cea2009-05-05 19:04:29 -04002610 cd->offset = cookiep;
2611skip_entry:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 cd->common.err = nfs_ok;
2613 return 0;
2614fail:
2615 cd->common.err = nfserr;
2616 return -EINVAL;
2617}
2618
Benny Halevye2f282b2008-08-12 20:45:07 +03002619static void
2620nfsd4_encode_stateid(struct nfsd4_compoundres *resp, stateid_t *sid)
2621{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002622 __be32 *p;
Benny Halevye2f282b2008-08-12 20:45:07 +03002623
2624 RESERVE_SPACE(sizeof(stateid_t));
2625 WRITE32(sid->si_generation);
2626 WRITEMEM(&sid->si_opaque, sizeof(stateid_opaque_t));
2627 ADJUST_ARGS();
2628}
2629
Benny Halevy695e12f2008-07-04 14:38:33 +03002630static __be32
Al Virob37ad282006-10-19 23:28:59 -07002631nfsd4_encode_access(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_access *access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002633 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634
2635 if (!nfserr) {
2636 RESERVE_SPACE(8);
2637 WRITE32(access->ac_supported);
2638 WRITE32(access->ac_resp_access);
2639 ADJUST_ARGS();
2640 }
Benny Halevy695e12f2008-07-04 14:38:33 +03002641 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642}
2643
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002644static __be32 nfsd4_encode_bind_conn_to_session(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_bind_conn_to_session *bcts)
2645{
2646 __be32 *p;
2647
2648 if (!nfserr) {
2649 RESERVE_SPACE(NFS4_MAX_SESSIONID_LEN + 8);
2650 WRITEMEM(bcts->sessionid.data, NFS4_MAX_SESSIONID_LEN);
2651 WRITE32(bcts->dir);
2652 /* XXX: ? */
2653 WRITE32(0);
2654 ADJUST_ARGS();
2655 }
2656 return nfserr;
2657}
2658
Benny Halevy695e12f2008-07-04 14:38:33 +03002659static __be32
Al Virob37ad282006-10-19 23:28:59 -07002660nfsd4_encode_close(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661{
2662 ENCODE_SEQID_OP_HEAD;
2663
Benny Halevye2f282b2008-08-12 20:45:07 +03002664 if (!nfserr)
2665 nfsd4_encode_stateid(resp, &close->cl_stateid);
2666
J. Bruce Fieldsf3e42232011-08-24 12:27:31 -04002667 encode_seqid_op_tail(resp, save, nfserr);
Benny Halevy695e12f2008-07-04 14:38:33 +03002668 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669}
2670
2671
Benny Halevy695e12f2008-07-04 14:38:33 +03002672static __be32
Al Virob37ad282006-10-19 23:28:59 -07002673nfsd4_encode_commit(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_commit *commit)
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) {
Chuck Leverab4684d2012-03-02 17:13:50 -05002678 RESERVE_SPACE(NFS4_VERIFIER_SIZE);
2679 WRITEMEM(commit->co_verf.data, NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 ADJUST_ARGS();
2681 }
Benny Halevy695e12f2008-07-04 14:38:33 +03002682 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683}
2684
Benny Halevy695e12f2008-07-04 14:38:33 +03002685static __be32
Al Virob37ad282006-10-19 23:28:59 -07002686nfsd4_encode_create(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_create *create)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002688 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689
2690 if (!nfserr) {
2691 RESERVE_SPACE(32);
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002692 write_cinfo(&p, &create->cr_cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 WRITE32(2);
2694 WRITE32(create->cr_bmval[0]);
2695 WRITE32(create->cr_bmval[1]);
2696 ADJUST_ARGS();
2697 }
Benny Halevy695e12f2008-07-04 14:38:33 +03002698 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699}
2700
Al Virob37ad282006-10-19 23:28:59 -07002701static __be32
2702nfsd4_encode_getattr(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_getattr *getattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703{
2704 struct svc_fh *fhp = getattr->ga_fhp;
2705 int buflen;
2706
2707 if (nfserr)
2708 return nfserr;
2709
2710 buflen = resp->end - resp->p - (COMPOUND_ERR_SLACK_SPACE >> 2);
2711 nfserr = nfsd4_encode_fattr(fhp, fhp->fh_export, fhp->fh_dentry,
2712 resp->p, &buflen, getattr->ga_bmval,
Frank Filz406a7ea2007-11-27 11:34:05 -08002713 resp->rqstp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 if (!nfserr)
2715 resp->p += buflen;
2716 return nfserr;
2717}
2718
Benny Halevy695e12f2008-07-04 14:38:33 +03002719static __be32
2720nfsd4_encode_getfh(struct nfsd4_compoundres *resp, __be32 nfserr, struct svc_fh **fhpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721{
Benny Halevy695e12f2008-07-04 14:38:33 +03002722 struct svc_fh *fhp = *fhpp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 unsigned int len;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002724 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725
2726 if (!nfserr) {
2727 len = fhp->fh_handle.fh_size;
2728 RESERVE_SPACE(len + 4);
2729 WRITE32(len);
2730 WRITEMEM(&fhp->fh_handle.fh_base, len);
2731 ADJUST_ARGS();
2732 }
Benny Halevy695e12f2008-07-04 14:38:33 +03002733 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734}
2735
2736/*
2737* Including all fields other than the name, a LOCK4denied structure requires
2738* 8(clientid) + 4(namelen) + 8(offset) + 8(length) + 4(type) = 32 bytes.
2739*/
2740static void
2741nfsd4_encode_lock_denied(struct nfsd4_compoundres *resp, struct nfsd4_lock_denied *ld)
2742{
J. Bruce Fields7c13f342011-08-30 22:15:47 -04002743 struct xdr_netobj *conf = &ld->ld_owner;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002744 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745
J. Bruce Fields7c13f342011-08-30 22:15:47 -04002746 RESERVE_SPACE(32 + XDR_LEN(conf->len));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 WRITE64(ld->ld_start);
2748 WRITE64(ld->ld_length);
2749 WRITE32(ld->ld_type);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04002750 if (conf->len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 WRITEMEM(&ld->ld_clientid, 8);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04002752 WRITE32(conf->len);
2753 WRITEMEM(conf->data, conf->len);
2754 kfree(conf->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 } else { /* non - nfsv4 lock in conflict, no clientid nor owner */
2756 WRITE64((u64)0); /* clientid */
2757 WRITE32(0); /* length of owner name */
2758 }
2759 ADJUST_ARGS();
2760}
2761
Benny Halevy695e12f2008-07-04 14:38:33 +03002762static __be32
Al Virob37ad282006-10-19 23:28:59 -07002763nfsd4_encode_lock(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 ENCODE_SEQID_OP_HEAD;
2766
Benny Halevye2f282b2008-08-12 20:45:07 +03002767 if (!nfserr)
2768 nfsd4_encode_stateid(resp, &lock->lk_resp_stateid);
2769 else if (nfserr == nfserr_denied)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 nfsd4_encode_lock_denied(resp, &lock->lk_denied);
2771
J. Bruce Fieldsf3e42232011-08-24 12:27:31 -04002772 encode_seqid_op_tail(resp, save, nfserr);
Benny Halevy695e12f2008-07-04 14:38:33 +03002773 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774}
2775
Benny Halevy695e12f2008-07-04 14:38:33 +03002776static __be32
Al Virob37ad282006-10-19 23:28:59 -07002777nfsd4_encode_lockt(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778{
2779 if (nfserr == nfserr_denied)
2780 nfsd4_encode_lock_denied(resp, &lockt->lt_denied);
Benny Halevy695e12f2008-07-04 14:38:33 +03002781 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782}
2783
Benny Halevy695e12f2008-07-04 14:38:33 +03002784static __be32
Al Virob37ad282006-10-19 23:28:59 -07002785nfsd4_encode_locku(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786{
2787 ENCODE_SEQID_OP_HEAD;
2788
Benny Halevye2f282b2008-08-12 20:45:07 +03002789 if (!nfserr)
2790 nfsd4_encode_stateid(resp, &locku->lu_stateid);
2791
J. Bruce Fieldsf3e42232011-08-24 12:27:31 -04002792 encode_seqid_op_tail(resp, save, nfserr);
Benny Halevy695e12f2008-07-04 14:38:33 +03002793 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794}
2795
2796
Benny Halevy695e12f2008-07-04 14:38:33 +03002797static __be32
Al Virob37ad282006-10-19 23:28:59 -07002798nfsd4_encode_link(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_link *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002800 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801
2802 if (!nfserr) {
2803 RESERVE_SPACE(20);
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002804 write_cinfo(&p, &link->li_cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 ADJUST_ARGS();
2806 }
Benny Halevy695e12f2008-07-04 14:38:33 +03002807 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808}
2809
2810
Benny Halevy695e12f2008-07-04 14:38:33 +03002811static __be32
Al Virob37ad282006-10-19 23:28:59 -07002812nfsd4_encode_open(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002814 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 ENCODE_SEQID_OP_HEAD;
2816
2817 if (nfserr)
2818 goto out;
2819
Benny Halevye2f282b2008-08-12 20:45:07 +03002820 nfsd4_encode_stateid(resp, &open->op_stateid);
2821 RESERVE_SPACE(40);
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002822 write_cinfo(&p, &open->op_cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 WRITE32(open->op_rflags);
2824 WRITE32(2);
2825 WRITE32(open->op_bmval[0]);
2826 WRITE32(open->op_bmval[1]);
2827 WRITE32(open->op_delegate_type);
2828 ADJUST_ARGS();
2829
2830 switch (open->op_delegate_type) {
2831 case NFS4_OPEN_DELEGATE_NONE:
2832 break;
2833 case NFS4_OPEN_DELEGATE_READ:
Benny Halevye2f282b2008-08-12 20:45:07 +03002834 nfsd4_encode_stateid(resp, &open->op_delegate_stateid);
2835 RESERVE_SPACE(20);
NeilBrown7b190fe2005-06-23 22:03:23 -07002836 WRITE32(open->op_recall);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837
2838 /*
2839 * TODO: ACE's in delegations
2840 */
2841 WRITE32(NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
2842 WRITE32(0);
2843 WRITE32(0);
2844 WRITE32(0); /* XXX: is NULL principal ok? */
2845 ADJUST_ARGS();
2846 break;
2847 case NFS4_OPEN_DELEGATE_WRITE:
Benny Halevye2f282b2008-08-12 20:45:07 +03002848 nfsd4_encode_stateid(resp, &open->op_delegate_stateid);
2849 RESERVE_SPACE(32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 WRITE32(0);
2851
2852 /*
2853 * TODO: space_limit's in delegations
2854 */
2855 WRITE32(NFS4_LIMIT_SIZE);
2856 WRITE32(~(u32)0);
2857 WRITE32(~(u32)0);
2858
2859 /*
2860 * TODO: ACE's in delegations
2861 */
2862 WRITE32(NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
2863 WRITE32(0);
2864 WRITE32(0);
2865 WRITE32(0); /* XXX: is NULL principal ok? */
2866 ADJUST_ARGS();
2867 break;
Benny Halevyd24433c2012-02-16 20:57:17 +02002868 case NFS4_OPEN_DELEGATE_NONE_EXT: /* 4.1 */
2869 switch (open->op_why_no_deleg) {
2870 case WND4_CONTENTION:
2871 case WND4_RESOURCE:
2872 RESERVE_SPACE(8);
2873 WRITE32(open->op_why_no_deleg);
2874 WRITE32(0); /* deleg signaling not supported yet */
2875 break;
2876 default:
2877 RESERVE_SPACE(4);
2878 WRITE32(open->op_why_no_deleg);
2879 }
2880 ADJUST_ARGS();
2881 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 default:
2883 BUG();
2884 }
2885 /* XXX save filehandle here */
2886out:
J. Bruce Fieldsf3e42232011-08-24 12:27:31 -04002887 encode_seqid_op_tail(resp, save, nfserr);
Benny Halevy695e12f2008-07-04 14:38:33 +03002888 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889}
2890
Benny Halevy695e12f2008-07-04 14:38:33 +03002891static __be32
Al Virob37ad282006-10-19 23:28:59 -07002892nfsd4_encode_open_confirm(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893{
2894 ENCODE_SEQID_OP_HEAD;
Benny Halevye2f282b2008-08-12 20:45:07 +03002895
2896 if (!nfserr)
2897 nfsd4_encode_stateid(resp, &oc->oc_resp_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898
J. Bruce Fieldsf3e42232011-08-24 12:27:31 -04002899 encode_seqid_op_tail(resp, save, nfserr);
Benny Halevy695e12f2008-07-04 14:38:33 +03002900 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901}
2902
Benny Halevy695e12f2008-07-04 14:38:33 +03002903static __be32
Al Virob37ad282006-10-19 23:28:59 -07002904nfsd4_encode_open_downgrade(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905{
2906 ENCODE_SEQID_OP_HEAD;
Benny Halevye2f282b2008-08-12 20:45:07 +03002907
2908 if (!nfserr)
2909 nfsd4_encode_stateid(resp, &od->od_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910
J. Bruce Fieldsf3e42232011-08-24 12:27:31 -04002911 encode_seqid_op_tail(resp, save, nfserr);
Benny Halevy695e12f2008-07-04 14:38:33 +03002912 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913}
2914
Al Virob37ad282006-10-19 23:28:59 -07002915static __be32
2916nfsd4_encode_read(struct nfsd4_compoundres *resp, __be32 nfserr,
NeilBrown44524352006-10-04 02:15:46 -07002917 struct nfsd4_read *read)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918{
2919 u32 eof;
2920 int v, pn;
2921 unsigned long maxcount;
2922 long len;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002923 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924
2925 if (nfserr)
2926 return nfserr;
2927 if (resp->xbuf->page_len)
2928 return nfserr_resource;
2929
2930 RESERVE_SPACE(8); /* eof flag and byte count */
2931
Greg Banks7adae482006-10-04 02:15:47 -07002932 maxcount = svc_max_payload(resp->rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 if (maxcount > read->rd_length)
2934 maxcount = read->rd_length;
2935
2936 len = maxcount;
2937 v = 0;
2938 while (len > 0) {
J. Bruce Fields4cbb3d02012-12-04 18:25:10 -05002939 pn = resp->rqstp->rq_resused;
2940 if (!resp->rqstp->rq_respages[pn]) { /* ran out of pages */
2941 maxcount -= len;
2942 break;
2943 }
NeilBrown3cc03b12006-10-04 02:15:47 -07002944 resp->rqstp->rq_vec[v].iov_base =
NeilBrown44524352006-10-04 02:15:46 -07002945 page_address(resp->rqstp->rq_respages[pn]);
NeilBrown3cc03b12006-10-04 02:15:47 -07002946 resp->rqstp->rq_vec[v].iov_len =
NeilBrown44524352006-10-04 02:15:46 -07002947 len < PAGE_SIZE ? len : PAGE_SIZE;
J. Bruce Fields4cbb3d02012-12-04 18:25:10 -05002948 resp->rqstp->rq_resused++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 v++;
2950 len -= PAGE_SIZE;
2951 }
2952 read->rd_vlen = v;
2953
J. Bruce Fields039a87c2010-07-30 11:33:32 -04002954 nfserr = nfsd_read_file(read->rd_rqstp, read->rd_fhp, read->rd_filp,
NeilBrown3cc03b12006-10-04 02:15:47 -07002955 read->rd_offset, resp->rqstp->rq_vec, read->rd_vlen,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 &maxcount);
2957
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 if (nfserr)
2959 return nfserr;
NeilBrown44524352006-10-04 02:15:46 -07002960 eof = (read->rd_offset + maxcount >=
2961 read->rd_fhp->fh_dentry->d_inode->i_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962
2963 WRITE32(eof);
2964 WRITE32(maxcount);
2965 ADJUST_ARGS();
NeilBrown6ed6dec2006-04-10 22:55:32 -07002966 resp->xbuf->head[0].iov_len = (char*)p
2967 - (char*)resp->xbuf->head[0].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 resp->xbuf->page_len = maxcount;
2969
NeilBrown6ed6dec2006-04-10 22:55:32 -07002970 /* Use rest of head for padding and remaining ops: */
NeilBrown6ed6dec2006-04-10 22:55:32 -07002971 resp->xbuf->tail[0].iov_base = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 resp->xbuf->tail[0].iov_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 if (maxcount&3) {
NeilBrown6ed6dec2006-04-10 22:55:32 -07002974 RESERVE_SPACE(4);
2975 WRITE32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 resp->xbuf->tail[0].iov_base += maxcount&3;
2977 resp->xbuf->tail[0].iov_len = 4 - (maxcount&3);
NeilBrown6ed6dec2006-04-10 22:55:32 -07002978 ADJUST_ARGS();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 }
2980 return 0;
2981}
2982
Al Virob37ad282006-10-19 23:28:59 -07002983static __be32
2984nfsd4_encode_readlink(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_readlink *readlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985{
2986 int maxcount;
2987 char *page;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002988 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989
2990 if (nfserr)
2991 return nfserr;
2992 if (resp->xbuf->page_len)
2993 return nfserr_resource;
J. Bruce Fields4cbb3d02012-12-04 18:25:10 -05002994 if (!resp->rqstp->rq_respages[resp->rqstp->rq_resused])
2995 return nfserr_resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996
NeilBrown44524352006-10-04 02:15:46 -07002997 page = page_address(resp->rqstp->rq_respages[resp->rqstp->rq_resused++]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998
2999 maxcount = PAGE_SIZE;
3000 RESERVE_SPACE(4);
3001
3002 /*
3003 * XXX: By default, the ->readlink() VFS op will truncate symlinks
3004 * if they would overflow the buffer. Is this kosher in NFSv4? If
3005 * not, one easy fix is: if ->readlink() precisely fills the buffer,
3006 * assume that truncation occurred, and return NFS4ERR_RESOURCE.
3007 */
3008 nfserr = nfsd_readlink(readlink->rl_rqstp, readlink->rl_fhp, page, &maxcount);
3009 if (nfserr == nfserr_isdir)
3010 return nfserr_inval;
3011 if (nfserr)
3012 return nfserr;
3013
3014 WRITE32(maxcount);
3015 ADJUST_ARGS();
NeilBrown6ed6dec2006-04-10 22:55:32 -07003016 resp->xbuf->head[0].iov_len = (char*)p
3017 - (char*)resp->xbuf->head[0].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018 resp->xbuf->page_len = maxcount;
NeilBrown6ed6dec2006-04-10 22:55:32 -07003019
3020 /* Use rest of head for padding and remaining ops: */
NeilBrown6ed6dec2006-04-10 22:55:32 -07003021 resp->xbuf->tail[0].iov_base = p;
3022 resp->xbuf->tail[0].iov_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023 if (maxcount&3) {
NeilBrown6ed6dec2006-04-10 22:55:32 -07003024 RESERVE_SPACE(4);
3025 WRITE32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 resp->xbuf->tail[0].iov_base += maxcount&3;
3027 resp->xbuf->tail[0].iov_len = 4 - (maxcount&3);
NeilBrown6ed6dec2006-04-10 22:55:32 -07003028 ADJUST_ARGS();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 }
3030 return 0;
3031}
3032
Al Virob37ad282006-10-19 23:28:59 -07003033static __be32
3034nfsd4_encode_readdir(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_readdir *readdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035{
3036 int maxcount;
3037 loff_t offset;
Al Viro2ebbc012006-10-19 23:28:58 -07003038 __be32 *page, *savep, *tailbase;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003039 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040
3041 if (nfserr)
3042 return nfserr;
3043 if (resp->xbuf->page_len)
3044 return nfserr_resource;
J. Bruce Fields4cbb3d02012-12-04 18:25:10 -05003045 if (!resp->rqstp->rq_respages[resp->rqstp->rq_resused])
3046 return nfserr_resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047
Chuck Leverab4684d2012-03-02 17:13:50 -05003048 RESERVE_SPACE(NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049 savep = p;
3050
3051 /* XXX: Following NFSv3, we ignore the READDIR verifier for now. */
3052 WRITE32(0);
3053 WRITE32(0);
3054 ADJUST_ARGS();
3055 resp->xbuf->head[0].iov_len = ((char*)resp->p) - (char*)resp->xbuf->head[0].iov_base;
NeilBrownbb6e8a92006-04-10 22:55:33 -07003056 tailbase = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057
3058 maxcount = PAGE_SIZE;
3059 if (maxcount > readdir->rd_maxcount)
3060 maxcount = readdir->rd_maxcount;
3061
3062 /*
3063 * Convert from bytes to words, account for the two words already
3064 * written, make sure to leave two words at the end for the next
3065 * pointer and eof field.
3066 */
3067 maxcount = (maxcount >> 2) - 4;
3068 if (maxcount < 0) {
3069 nfserr = nfserr_toosmall;
3070 goto err_no_verf;
3071 }
3072
NeilBrown44524352006-10-04 02:15:46 -07003073 page = page_address(resp->rqstp->rq_respages[resp->rqstp->rq_resused++]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 readdir->common.err = 0;
3075 readdir->buflen = maxcount;
3076 readdir->buffer = page;
3077 readdir->offset = NULL;
3078
3079 offset = readdir->rd_cookie;
3080 nfserr = nfsd_readdir(readdir->rd_rqstp, readdir->rd_fhp,
3081 &offset,
3082 &readdir->common, nfsd4_encode_dirent);
3083 if (nfserr == nfs_ok &&
3084 readdir->common.err == nfserr_toosmall &&
3085 readdir->buffer == page)
3086 nfserr = nfserr_toosmall;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 if (nfserr)
3088 goto err_no_verf;
3089
3090 if (readdir->offset)
3091 xdr_encode_hyper(readdir->offset, offset);
3092
3093 p = readdir->buffer;
3094 *p++ = 0; /* no more entries */
3095 *p++ = htonl(readdir->common.err == nfserr_eof);
NeilBrown44524352006-10-04 02:15:46 -07003096 resp->xbuf->page_len = ((char*)p) - (char*)page_address(
3097 resp->rqstp->rq_respages[resp->rqstp->rq_resused-1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098
NeilBrownbb6e8a92006-04-10 22:55:33 -07003099 /* Use rest of head for padding and remaining ops: */
NeilBrownbb6e8a92006-04-10 22:55:33 -07003100 resp->xbuf->tail[0].iov_base = tailbase;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 resp->xbuf->tail[0].iov_len = 0;
3102 resp->p = resp->xbuf->tail[0].iov_base;
NeilBrownbb6e8a92006-04-10 22:55:33 -07003103 resp->end = resp->p + (PAGE_SIZE - resp->xbuf->head[0].iov_len)/4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104
3105 return 0;
3106err_no_verf:
3107 p = savep;
3108 ADJUST_ARGS();
3109 return nfserr;
3110}
3111
Benny Halevy695e12f2008-07-04 14:38:33 +03003112static __be32
Al Virob37ad282006-10-19 23:28:59 -07003113nfsd4_encode_remove(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_remove *remove)
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(20);
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04003119 write_cinfo(&p, &remove->rm_cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 ADJUST_ARGS();
3121 }
Benny Halevy695e12f2008-07-04 14:38:33 +03003122 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123}
3124
Benny Halevy695e12f2008-07-04 14:38:33 +03003125static __be32
Al Virob37ad282006-10-19 23:28:59 -07003126nfsd4_encode_rename(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_rename *rename)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003128 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129
3130 if (!nfserr) {
3131 RESERVE_SPACE(40);
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04003132 write_cinfo(&p, &rename->rn_sinfo);
3133 write_cinfo(&p, &rename->rn_tinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 ADJUST_ARGS();
3135 }
Benny Halevy695e12f2008-07-04 14:38:33 +03003136 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137}
3138
Benny Halevy695e12f2008-07-04 14:38:33 +03003139static __be32
Mi Jinlong22b6dee2010-12-27 14:29:57 +08003140nfsd4_do_encode_secinfo(struct nfsd4_compoundres *resp,
3141 __be32 nfserr,struct svc_export *exp)
Andy Adamsondcb488a2007-07-17 04:04:51 -07003142{
3143 int i = 0;
J. Bruce Fields4796f452007-07-17 04:04:51 -07003144 u32 nflavs;
3145 struct exp_flavor_info *flavs;
3146 struct exp_flavor_info def_flavs[2];
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003147 __be32 *p;
Andy Adamsondcb488a2007-07-17 04:04:51 -07003148
3149 if (nfserr)
3150 goto out;
J. Bruce Fields4796f452007-07-17 04:04:51 -07003151 if (exp->ex_nflavors) {
3152 flavs = exp->ex_flavors;
3153 nflavs = exp->ex_nflavors;
3154 } else { /* Handling of some defaults in absence of real secinfo: */
3155 flavs = def_flavs;
3156 if (exp->ex_client->flavour->flavour == RPC_AUTH_UNIX) {
3157 nflavs = 2;
3158 flavs[0].pseudoflavor = RPC_AUTH_UNIX;
3159 flavs[1].pseudoflavor = RPC_AUTH_NULL;
3160 } else if (exp->ex_client->flavour->flavour == RPC_AUTH_GSS) {
3161 nflavs = 1;
3162 flavs[0].pseudoflavor
3163 = svcauth_gss_flavor(exp->ex_client);
3164 } else {
3165 nflavs = 1;
3166 flavs[0].pseudoflavor
3167 = exp->ex_client->flavour->flavour;
3168 }
3169 }
3170
Andy Adamsondcb488a2007-07-17 04:04:51 -07003171 RESERVE_SPACE(4);
J. Bruce Fields4796f452007-07-17 04:04:51 -07003172 WRITE32(nflavs);
Andy Adamsondcb488a2007-07-17 04:04:51 -07003173 ADJUST_ARGS();
J. Bruce Fields4796f452007-07-17 04:04:51 -07003174 for (i = 0; i < nflavs; i++) {
3175 u32 flav = flavs[i].pseudoflavor;
Andy Adamsondcb488a2007-07-17 04:04:51 -07003176 struct gss_api_mech *gm = gss_mech_get_by_pseudoflavor(flav);
3177
3178 if (gm) {
3179 RESERVE_SPACE(4);
3180 WRITE32(RPC_AUTH_GSS);
3181 ADJUST_ARGS();
3182 RESERVE_SPACE(4 + gm->gm_oid.len);
3183 WRITE32(gm->gm_oid.len);
3184 WRITEMEM(gm->gm_oid.data, gm->gm_oid.len);
3185 ADJUST_ARGS();
3186 RESERVE_SPACE(4);
3187 WRITE32(0); /* qop */
3188 ADJUST_ARGS();
3189 RESERVE_SPACE(4);
3190 WRITE32(gss_pseudoflavor_to_service(gm, flav));
3191 ADJUST_ARGS();
3192 gss_mech_put(gm);
3193 } else {
3194 RESERVE_SPACE(4);
3195 WRITE32(flav);
3196 ADJUST_ARGS();
3197 }
3198 }
3199out:
3200 if (exp)
3201 exp_put(exp);
Benny Halevy695e12f2008-07-04 14:38:33 +03003202 return nfserr;
Andy Adamsondcb488a2007-07-17 04:04:51 -07003203}
3204
Mi Jinlong22b6dee2010-12-27 14:29:57 +08003205static __be32
3206nfsd4_encode_secinfo(struct nfsd4_compoundres *resp, __be32 nfserr,
3207 struct nfsd4_secinfo *secinfo)
3208{
3209 return nfsd4_do_encode_secinfo(resp, nfserr, secinfo->si_exp);
3210}
3211
3212static __be32
3213nfsd4_encode_secinfo_no_name(struct nfsd4_compoundres *resp, __be32 nfserr,
3214 struct nfsd4_secinfo_no_name *secinfo)
3215{
3216 return nfsd4_do_encode_secinfo(resp, nfserr, secinfo->sin_exp);
3217}
3218
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219/*
3220 * The SETATTR encode routine is special -- it always encodes a bitmap,
3221 * regardless of the error status.
3222 */
Benny Halevy695e12f2008-07-04 14:38:33 +03003223static __be32
Al Virob37ad282006-10-19 23:28:59 -07003224nfsd4_encode_setattr(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_setattr *setattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003226 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227
3228 RESERVE_SPACE(12);
3229 if (nfserr) {
3230 WRITE32(2);
3231 WRITE32(0);
3232 WRITE32(0);
3233 }
3234 else {
3235 WRITE32(2);
3236 WRITE32(setattr->sa_bmval[0]);
3237 WRITE32(setattr->sa_bmval[1]);
3238 }
3239 ADJUST_ARGS();
Benny Halevy695e12f2008-07-04 14:38:33 +03003240 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241}
3242
Benny Halevy695e12f2008-07-04 14:38:33 +03003243static __be32
Al Virob37ad282006-10-19 23:28:59 -07003244nfsd4_encode_setclientid(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_setclientid *scd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003246 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247
3248 if (!nfserr) {
Chuck Leverab4684d2012-03-02 17:13:50 -05003249 RESERVE_SPACE(8 + NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 WRITEMEM(&scd->se_clientid, 8);
Chuck Leverab4684d2012-03-02 17:13:50 -05003251 WRITEMEM(&scd->se_confirm, NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 ADJUST_ARGS();
3253 }
3254 else if (nfserr == nfserr_clid_inuse) {
3255 RESERVE_SPACE(8);
3256 WRITE32(0);
3257 WRITE32(0);
3258 ADJUST_ARGS();
3259 }
Benny Halevy695e12f2008-07-04 14:38:33 +03003260 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261}
3262
Benny Halevy695e12f2008-07-04 14:38:33 +03003263static __be32
Al Virob37ad282006-10-19 23:28:59 -07003264nfsd4_encode_write(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_write *write)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003266 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267
3268 if (!nfserr) {
3269 RESERVE_SPACE(16);
3270 WRITE32(write->wr_bytes_written);
3271 WRITE32(write->wr_how_written);
Chuck Leverab4684d2012-03-02 17:13:50 -05003272 WRITEMEM(write->wr_verifier.data, NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273 ADJUST_ARGS();
3274 }
Benny Halevy695e12f2008-07-04 14:38:33 +03003275 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276}
3277
Benny Halevy695e12f2008-07-04 14:38:33 +03003278static __be32
Andy Adamson2db134e2009-04-03 08:27:55 +03003279nfsd4_encode_exchange_id(struct nfsd4_compoundres *resp, int nfserr,
3280 struct nfsd4_exchange_id *exid)
3281{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003282 __be32 *p;
Andy Adamson0733d212009-04-03 08:28:01 +03003283 char *major_id;
3284 char *server_scope;
3285 int major_id_sz;
3286 int server_scope_sz;
3287 uint64_t minor_id = 0;
3288
3289 if (nfserr)
3290 return nfserr;
3291
3292 major_id = utsname()->nodename;
3293 major_id_sz = strlen(major_id);
3294 server_scope = utsname()->nodename;
3295 server_scope_sz = strlen(server_scope);
3296
3297 RESERVE_SPACE(
3298 8 /* eir_clientid */ +
3299 4 /* eir_sequenceid */ +
3300 4 /* eir_flags */ +
3301 4 /* spr_how (SP4_NONE) */ +
3302 8 /* so_minor_id */ +
3303 4 /* so_major_id.len */ +
3304 (XDR_QUADLEN(major_id_sz) * 4) +
3305 4 /* eir_server_scope.len */ +
3306 (XDR_QUADLEN(server_scope_sz) * 4) +
3307 4 /* eir_server_impl_id.count (0) */);
3308
3309 WRITEMEM(&exid->clientid, 8);
3310 WRITE32(exid->seqid);
3311 WRITE32(exid->flags);
3312
3313 /* state_protect4_r. Currently only support SP4_NONE */
3314 BUG_ON(exid->spa_how != SP4_NONE);
3315 WRITE32(exid->spa_how);
3316
3317 /* The server_owner struct */
3318 WRITE64(minor_id); /* Minor id */
3319 /* major id */
3320 WRITE32(major_id_sz);
3321 WRITEMEM(major_id, major_id_sz);
3322
3323 /* Server scope */
3324 WRITE32(server_scope_sz);
3325 WRITEMEM(server_scope, server_scope_sz);
3326
3327 /* Implementation id */
3328 WRITE32(0); /* zero length nfs_impl_id4 array */
3329 ADJUST_ARGS();
3330 return 0;
Andy Adamson2db134e2009-04-03 08:27:55 +03003331}
3332
3333static __be32
3334nfsd4_encode_create_session(struct nfsd4_compoundres *resp, int nfserr,
3335 struct nfsd4_create_session *sess)
3336{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003337 __be32 *p;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003338
3339 if (nfserr)
3340 return nfserr;
3341
3342 RESERVE_SPACE(24);
3343 WRITEMEM(sess->sessionid.data, NFS4_MAX_SESSIONID_LEN);
3344 WRITE32(sess->seqid);
3345 WRITE32(sess->flags);
3346 ADJUST_ARGS();
3347
3348 RESERVE_SPACE(28);
3349 WRITE32(0); /* headerpadsz */
3350 WRITE32(sess->fore_channel.maxreq_sz);
3351 WRITE32(sess->fore_channel.maxresp_sz);
3352 WRITE32(sess->fore_channel.maxresp_cached);
3353 WRITE32(sess->fore_channel.maxops);
3354 WRITE32(sess->fore_channel.maxreqs);
3355 WRITE32(sess->fore_channel.nr_rdma_attrs);
3356 ADJUST_ARGS();
3357
3358 if (sess->fore_channel.nr_rdma_attrs) {
3359 RESERVE_SPACE(4);
3360 WRITE32(sess->fore_channel.rdma_attrs);
3361 ADJUST_ARGS();
3362 }
3363
3364 RESERVE_SPACE(28);
3365 WRITE32(0); /* headerpadsz */
3366 WRITE32(sess->back_channel.maxreq_sz);
3367 WRITE32(sess->back_channel.maxresp_sz);
3368 WRITE32(sess->back_channel.maxresp_cached);
3369 WRITE32(sess->back_channel.maxops);
3370 WRITE32(sess->back_channel.maxreqs);
3371 WRITE32(sess->back_channel.nr_rdma_attrs);
3372 ADJUST_ARGS();
3373
3374 if (sess->back_channel.nr_rdma_attrs) {
3375 RESERVE_SPACE(4);
3376 WRITE32(sess->back_channel.rdma_attrs);
3377 ADJUST_ARGS();
3378 }
3379 return 0;
Andy Adamson2db134e2009-04-03 08:27:55 +03003380}
3381
3382static __be32
3383nfsd4_encode_destroy_session(struct nfsd4_compoundres *resp, int nfserr,
3384 struct nfsd4_destroy_session *destroy_session)
3385{
Andy Adamson2db134e2009-04-03 08:27:55 +03003386 return nfserr;
3387}
3388
Daniel Mackc47d8322011-05-16 16:38:14 +02003389static __be32
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003390nfsd4_encode_free_stateid(struct nfsd4_compoundres *resp, int nfserr,
3391 struct nfsd4_free_stateid *free_stateid)
3392{
3393 __be32 *p;
3394
3395 if (nfserr)
3396 return nfserr;
3397
3398 RESERVE_SPACE(4);
3399 WRITE32(nfserr);
3400 ADJUST_ARGS();
3401 return nfserr;
3402}
3403
3404static __be32
Andy Adamson2db134e2009-04-03 08:27:55 +03003405nfsd4_encode_sequence(struct nfsd4_compoundres *resp, int nfserr,
3406 struct nfsd4_sequence *seq)
3407{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003408 __be32 *p;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003409
3410 if (nfserr)
3411 return nfserr;
3412
3413 RESERVE_SPACE(NFS4_MAX_SESSIONID_LEN + 20);
3414 WRITEMEM(seq->sessionid.data, NFS4_MAX_SESSIONID_LEN);
3415 WRITE32(seq->seqid);
3416 WRITE32(seq->slotid);
J. Bruce Fieldsb7d7ca32011-08-31 15:39:30 -04003417 /* Note slotid's are numbered from zero: */
3418 WRITE32(seq->maxslots - 1); /* sr_highest_slotid */
3419 WRITE32(seq->maxslots - 1); /* sr_target_highest_slotid */
J. Bruce Fields0d7bb712010-11-18 08:30:33 -05003420 WRITE32(seq->status_flags);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003421
3422 ADJUST_ARGS();
Andy Adamson557ce262009-08-28 08:45:04 -04003423 resp->cstate.datap = p; /* DRC cache data pointer */
Benny Halevyb85d4c02009-04-03 08:28:08 +03003424 return 0;
Andy Adamson2db134e2009-04-03 08:27:55 +03003425}
3426
Bryan Schumaker17456802011-07-13 10:50:48 -04003427__be32
3428nfsd4_encode_test_stateid(struct nfsd4_compoundres *resp, int nfserr,
3429 struct nfsd4_test_stateid *test_stateid)
3430{
Bryan Schumaker03cfb422012-01-27 10:22:49 -05003431 struct nfsd4_test_stateid_id *stateid, *next;
Bryan Schumaker17456802011-07-13 10:50:48 -04003432 __be32 *p;
Bryan Schumaker17456802011-07-13 10:50:48 -04003433
J. Bruce Fieldsc2189c72014-02-03 16:31:42 -05003434 if (nfserr)
3435 return nfserr;
3436
Bryan Schumaker03cfb422012-01-27 10:22:49 -05003437 RESERVE_SPACE(4 + (4 * test_stateid->ts_num_ids));
Bryan Schumaker17456802011-07-13 10:50:48 -04003438 *p++ = htonl(test_stateid->ts_num_ids);
Bryan Schumaker17456802011-07-13 10:50:48 -04003439
Bryan Schumaker03cfb422012-01-27 10:22:49 -05003440 list_for_each_entry_safe(stateid, next, &test_stateid->ts_stateid_list, ts_id_list) {
Al Viro02f5fde2012-04-13 00:10:34 -04003441 *p++ = stateid->ts_id_status;
Bryan Schumaker17456802011-07-13 10:50:48 -04003442 }
Bryan Schumaker17456802011-07-13 10:50:48 -04003443
Bryan Schumaker03cfb422012-01-27 10:22:49 -05003444 ADJUST_ARGS();
Bryan Schumaker17456802011-07-13 10:50:48 -04003445 return nfserr;
3446}
3447
Andy Adamson2db134e2009-04-03 08:27:55 +03003448static __be32
Benny Halevy695e12f2008-07-04 14:38:33 +03003449nfsd4_encode_noop(struct nfsd4_compoundres *resp, __be32 nfserr, void *p)
3450{
3451 return nfserr;
3452}
3453
3454typedef __be32(* nfsd4_enc)(struct nfsd4_compoundres *, __be32, void *);
3455
Andy Adamson2db134e2009-04-03 08:27:55 +03003456/*
3457 * Note: nfsd4_enc_ops vector is shared for v4.0 and v4.1
3458 * since we don't need to filter out obsolete ops as this is
3459 * done in the decoding phase.
3460 */
Benny Halevy695e12f2008-07-04 14:38:33 +03003461static nfsd4_enc nfsd4_enc_ops[] = {
J. Bruce Fieldsad1060c2008-07-18 15:04:16 -04003462 [OP_ACCESS] = (nfsd4_enc)nfsd4_encode_access,
3463 [OP_CLOSE] = (nfsd4_enc)nfsd4_encode_close,
3464 [OP_COMMIT] = (nfsd4_enc)nfsd4_encode_commit,
3465 [OP_CREATE] = (nfsd4_enc)nfsd4_encode_create,
3466 [OP_DELEGPURGE] = (nfsd4_enc)nfsd4_encode_noop,
3467 [OP_DELEGRETURN] = (nfsd4_enc)nfsd4_encode_noop,
3468 [OP_GETATTR] = (nfsd4_enc)nfsd4_encode_getattr,
3469 [OP_GETFH] = (nfsd4_enc)nfsd4_encode_getfh,
3470 [OP_LINK] = (nfsd4_enc)nfsd4_encode_link,
3471 [OP_LOCK] = (nfsd4_enc)nfsd4_encode_lock,
3472 [OP_LOCKT] = (nfsd4_enc)nfsd4_encode_lockt,
3473 [OP_LOCKU] = (nfsd4_enc)nfsd4_encode_locku,
3474 [OP_LOOKUP] = (nfsd4_enc)nfsd4_encode_noop,
3475 [OP_LOOKUPP] = (nfsd4_enc)nfsd4_encode_noop,
3476 [OP_NVERIFY] = (nfsd4_enc)nfsd4_encode_noop,
3477 [OP_OPEN] = (nfsd4_enc)nfsd4_encode_open,
Benny Halevy84f09f42009-03-04 23:05:35 +02003478 [OP_OPENATTR] = (nfsd4_enc)nfsd4_encode_noop,
J. Bruce Fieldsad1060c2008-07-18 15:04:16 -04003479 [OP_OPEN_CONFIRM] = (nfsd4_enc)nfsd4_encode_open_confirm,
3480 [OP_OPEN_DOWNGRADE] = (nfsd4_enc)nfsd4_encode_open_downgrade,
3481 [OP_PUTFH] = (nfsd4_enc)nfsd4_encode_noop,
3482 [OP_PUTPUBFH] = (nfsd4_enc)nfsd4_encode_noop,
3483 [OP_PUTROOTFH] = (nfsd4_enc)nfsd4_encode_noop,
3484 [OP_READ] = (nfsd4_enc)nfsd4_encode_read,
3485 [OP_READDIR] = (nfsd4_enc)nfsd4_encode_readdir,
3486 [OP_READLINK] = (nfsd4_enc)nfsd4_encode_readlink,
3487 [OP_REMOVE] = (nfsd4_enc)nfsd4_encode_remove,
3488 [OP_RENAME] = (nfsd4_enc)nfsd4_encode_rename,
3489 [OP_RENEW] = (nfsd4_enc)nfsd4_encode_noop,
3490 [OP_RESTOREFH] = (nfsd4_enc)nfsd4_encode_noop,
3491 [OP_SAVEFH] = (nfsd4_enc)nfsd4_encode_noop,
3492 [OP_SECINFO] = (nfsd4_enc)nfsd4_encode_secinfo,
3493 [OP_SETATTR] = (nfsd4_enc)nfsd4_encode_setattr,
3494 [OP_SETCLIENTID] = (nfsd4_enc)nfsd4_encode_setclientid,
3495 [OP_SETCLIENTID_CONFIRM] = (nfsd4_enc)nfsd4_encode_noop,
3496 [OP_VERIFY] = (nfsd4_enc)nfsd4_encode_noop,
3497 [OP_WRITE] = (nfsd4_enc)nfsd4_encode_write,
3498 [OP_RELEASE_LOCKOWNER] = (nfsd4_enc)nfsd4_encode_noop,
Andy Adamson2db134e2009-04-03 08:27:55 +03003499
3500 /* NFSv4.1 operations */
3501 [OP_BACKCHANNEL_CTL] = (nfsd4_enc)nfsd4_encode_noop,
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003502 [OP_BIND_CONN_TO_SESSION] = (nfsd4_enc)nfsd4_encode_bind_conn_to_session,
Andy Adamson2db134e2009-04-03 08:27:55 +03003503 [OP_EXCHANGE_ID] = (nfsd4_enc)nfsd4_encode_exchange_id,
3504 [OP_CREATE_SESSION] = (nfsd4_enc)nfsd4_encode_create_session,
3505 [OP_DESTROY_SESSION] = (nfsd4_enc)nfsd4_encode_destroy_session,
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003506 [OP_FREE_STATEID] = (nfsd4_enc)nfsd4_encode_free_stateid,
Andy Adamson2db134e2009-04-03 08:27:55 +03003507 [OP_GET_DIR_DELEGATION] = (nfsd4_enc)nfsd4_encode_noop,
3508 [OP_GETDEVICEINFO] = (nfsd4_enc)nfsd4_encode_noop,
3509 [OP_GETDEVICELIST] = (nfsd4_enc)nfsd4_encode_noop,
3510 [OP_LAYOUTCOMMIT] = (nfsd4_enc)nfsd4_encode_noop,
3511 [OP_LAYOUTGET] = (nfsd4_enc)nfsd4_encode_noop,
3512 [OP_LAYOUTRETURN] = (nfsd4_enc)nfsd4_encode_noop,
Mi Jinlong22b6dee2010-12-27 14:29:57 +08003513 [OP_SECINFO_NO_NAME] = (nfsd4_enc)nfsd4_encode_secinfo_no_name,
Andy Adamson2db134e2009-04-03 08:27:55 +03003514 [OP_SEQUENCE] = (nfsd4_enc)nfsd4_encode_sequence,
3515 [OP_SET_SSV] = (nfsd4_enc)nfsd4_encode_noop,
Bryan Schumaker17456802011-07-13 10:50:48 -04003516 [OP_TEST_STATEID] = (nfsd4_enc)nfsd4_encode_test_stateid,
Andy Adamson2db134e2009-04-03 08:27:55 +03003517 [OP_WANT_DELEGATION] = (nfsd4_enc)nfsd4_encode_noop,
3518 [OP_DESTROY_CLIENTID] = (nfsd4_enc)nfsd4_encode_noop,
3519 [OP_RECLAIM_COMPLETE] = (nfsd4_enc)nfsd4_encode_noop,
Benny Halevy695e12f2008-07-04 14:38:33 +03003520};
3521
Andy Adamson496c2622009-04-03 08:28:48 +03003522/*
3523 * Calculate the total amount of memory that the compound response has taken
Mi Jinlong58e7b332011-08-28 18:18:56 +08003524 * after encoding the current operation with pad.
Andy Adamson496c2622009-04-03 08:28:48 +03003525 *
Mi Jinlong58e7b332011-08-28 18:18:56 +08003526 * pad: if operation is non-idempotent, pad was calculate by op_rsize_bop()
3527 * which was specified at nfsd4_operation, else pad is zero.
Andy Adamson496c2622009-04-03 08:28:48 +03003528 *
Mi Jinlong58e7b332011-08-28 18:18:56 +08003529 * Compare this length to the session se_fmaxresp_sz and se_fmaxresp_cached.
Andy Adamson496c2622009-04-03 08:28:48 +03003530 *
3531 * Our se_fmaxresp_cached will always be a multiple of PAGE_SIZE, and so
3532 * will be at least a page and will therefore hold the xdr_buf head.
3533 */
Mi Jinlong58e7b332011-08-28 18:18:56 +08003534int nfsd4_check_resp_size(struct nfsd4_compoundres *resp, u32 pad)
Andy Adamson496c2622009-04-03 08:28:48 +03003535{
Andy Adamson496c2622009-04-03 08:28:48 +03003536 struct xdr_buf *xb = &resp->rqstp->rq_res;
Andy Adamson496c2622009-04-03 08:28:48 +03003537 struct nfsd4_session *session = NULL;
3538 struct nfsd4_slot *slot = resp->cstate.slot;
Mi Jinlong58e7b332011-08-28 18:18:56 +08003539 u32 length, tlen = 0;
Andy Adamson496c2622009-04-03 08:28:48 +03003540
3541 if (!nfsd4_has_session(&resp->cstate))
Mi Jinlong58e7b332011-08-28 18:18:56 +08003542 return 0;
Andy Adamson496c2622009-04-03 08:28:48 +03003543
3544 session = resp->cstate.session;
Mi Jinlong58e7b332011-08-28 18:18:56 +08003545 if (session == NULL)
3546 return 0;
Andy Adamson496c2622009-04-03 08:28:48 +03003547
3548 if (xb->page_len == 0) {
3549 length = (char *)resp->p - (char *)xb->head[0].iov_base + pad;
3550 } else {
3551 if (xb->tail[0].iov_base && xb->tail[0].iov_len > 0)
3552 tlen = (char *)resp->p - (char *)xb->tail[0].iov_base;
3553
3554 length = xb->head[0].iov_len + xb->page_len + tlen + pad;
3555 }
3556 dprintk("%s length %u, xb->page_len %u tlen %u pad %u\n", __func__,
3557 length, xb->page_len, tlen, pad);
3558
Mi Jinlong58e7b332011-08-28 18:18:56 +08003559 if (length > session->se_fchannel.maxresp_sz)
3560 return nfserr_rep_too_big;
3561
J. Bruce Fields73e79482012-02-13 16:39:00 -05003562 if ((slot->sl_flags & NFSD4_SLOT_CACHETHIS) &&
Mi Jinlong58e7b332011-08-28 18:18:56 +08003563 length > session->se_fchannel.maxresp_cached)
Andy Adamson496c2622009-04-03 08:28:48 +03003564 return nfserr_rep_too_big_to_cache;
Mi Jinlong58e7b332011-08-28 18:18:56 +08003565
3566 return 0;
Andy Adamson496c2622009-04-03 08:28:48 +03003567}
3568
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569void
3570nfsd4_encode_operation(struct nfsd4_compoundres *resp, struct nfsd4_op *op)
3571{
Al Viro2ebbc012006-10-19 23:28:58 -07003572 __be32 *statp;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003573 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574
3575 RESERVE_SPACE(8);
3576 WRITE32(op->opnum);
3577 statp = p++; /* to be backfilled at the end */
3578 ADJUST_ARGS();
3579
Benny Halevy695e12f2008-07-04 14:38:33 +03003580 if (op->opnum == OP_ILLEGAL)
3581 goto status;
3582 BUG_ON(op->opnum < 0 || op->opnum >= ARRAY_SIZE(nfsd4_enc_ops) ||
3583 !nfsd4_enc_ops[op->opnum]);
3584 op->status = nfsd4_enc_ops[op->opnum](resp, op->status, &op->u);
Andy Adamson496c2622009-04-03 08:28:48 +03003585 /* nfsd4_check_drc_limit guarantees enough room for error status */
Mi Jinlong58e7b332011-08-28 18:18:56 +08003586 if (!op->status)
3587 op->status = nfsd4_check_resp_size(resp, 0);
Benny Halevy695e12f2008-07-04 14:38:33 +03003588status:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589 /*
3590 * Note: We write the status directly, instead of using WRITE32(),
3591 * since it is already in network byte order.
3592 */
3593 *statp = op->status;
3594}
3595
3596/*
3597 * Encode the reply stored in the stateowner reply cache
3598 *
3599 * XDR note: do not encode rp->rp_buflen: the buffer contains the
3600 * previously sent already encoded operation.
3601 *
3602 * called with nfs4_lock_state() held
3603 */
3604void
3605nfsd4_encode_replay(struct nfsd4_compoundres *resp, struct nfsd4_op *op)
3606{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003607 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608 struct nfs4_replay *rp = op->replay;
3609
3610 BUG_ON(!rp);
3611
3612 RESERVE_SPACE(8);
3613 WRITE32(op->opnum);
3614 *p++ = rp->rp_status; /* already xdr'ed */
3615 ADJUST_ARGS();
3616
3617 RESERVE_SPACE(rp->rp_buflen);
3618 WRITEMEM(rp->rp_buf, rp->rp_buflen);
3619 ADJUST_ARGS();
3620}
3621
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622int
Al Viro2ebbc012006-10-19 23:28:58 -07003623nfs4svc_encode_voidres(struct svc_rqst *rqstp, __be32 *p, void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624{
3625 return xdr_ressize_check(rqstp, p);
3626}
3627
J. Bruce Fields3e98abf2011-07-16 17:15:10 -04003628int nfsd4_release_compoundargs(void *rq, __be32 *p, void *resp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629{
J. Bruce Fields3e98abf2011-07-16 17:15:10 -04003630 struct svc_rqst *rqstp = rq;
3631 struct nfsd4_compoundargs *args = rqstp->rq_argp;
3632
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633 if (args->ops != args->iops) {
3634 kfree(args->ops);
3635 args->ops = args->iops;
3636 }
Jesper Juhlf99d49a2005-11-07 01:01:34 -08003637 kfree(args->tmpp);
3638 args->tmpp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639 while (args->to_free) {
3640 struct tmpbuf *tb = args->to_free;
3641 args->to_free = tb->next;
3642 tb->release(tb->buf);
3643 kfree(tb);
3644 }
J. Bruce Fields3e98abf2011-07-16 17:15:10 -04003645 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646}
3647
3648int
Al Viro2ebbc012006-10-19 23:28:58 -07003649nfs4svc_decode_compoundargs(struct svc_rqst *rqstp, __be32 *p, struct nfsd4_compoundargs *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651 args->p = p;
3652 args->end = rqstp->rq_arg.head[0].iov_base + rqstp->rq_arg.head[0].iov_len;
3653 args->pagelist = rqstp->rq_arg.pages;
3654 args->pagelen = rqstp->rq_arg.page_len;
3655 args->tmpp = NULL;
3656 args->to_free = NULL;
3657 args->ops = args->iops;
3658 args->rqstp = rqstp;
3659
J. Bruce Fields3e98abf2011-07-16 17:15:10 -04003660 return !nfsd4_decode_compound(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661}
3662
3663int
Al Viro2ebbc012006-10-19 23:28:58 -07003664nfs4svc_encode_compoundres(struct svc_rqst *rqstp, __be32 *p, struct nfsd4_compoundres *resp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665{
3666 /*
3667 * All that remains is to write the tag and operation count...
3668 */
Andy Adamson557ce262009-08-28 08:45:04 -04003669 struct nfsd4_compound_state *cs = &resp->cstate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670 struct kvec *iov;
3671 p = resp->tagp;
3672 *p++ = htonl(resp->taglen);
3673 memcpy(p, resp->tag, resp->taglen);
3674 p += XDR_QUADLEN(resp->taglen);
3675 *p++ = htonl(resp->opcnt);
3676
3677 if (rqstp->rq_res.page_len)
3678 iov = &rqstp->rq_res.tail[0];
3679 else
3680 iov = &rqstp->rq_res.head[0];
3681 iov->iov_len = ((char*)resp->p) - (char*)iov->iov_base;
3682 BUG_ON(iov->iov_len > PAGE_SIZE);
J. Bruce Fields26c0c752010-04-24 15:35:43 -04003683 if (nfsd4_has_session(cs)) {
3684 if (cs->status != nfserr_replay_cache) {
3685 nfsd4_store_cache_entry(resp);
J. Bruce Fields73e79482012-02-13 16:39:00 -05003686 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
J. Bruce Fields26c0c752010-04-24 15:35:43 -04003687 }
Benny Halevyd7682982010-05-12 00:13:54 +03003688 /* Renew the clientid on success and on replay */
3689 release_session_client(cs->session);
J. Bruce Fields76407f72010-06-22 14:10:14 -04003690 nfsd4_put_session(cs->session);
Andy Adamsonda3846a2009-04-03 08:28:22 +03003691 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692 return 1;
3693}
3694
3695/*
3696 * Local variables:
3697 * c-basic-offset: 8
3698 * End:
3699 */