blob: d7e7c110246ef281db12f039fb60314111a43c8f [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 */
164 argp->p = page_address(argp->pagelist[0]);
165 argp->pagelist++;
166 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 Fields28e05dd2007-02-16 01:28:30 -0800266 int nace;
267 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;
346 READ32(dummy32);
347 if (dummy32)
348 return nfserr_inval;
349 READ32(iattr->ia_atime.tv_sec);
350 READ32(iattr->ia_atime.tv_nsec);
351 if (iattr->ia_atime.tv_nsec >= (u32)1000000000)
352 return nfserr_inval;
353 iattr->ia_valid |= (ATTR_ATIME | ATTR_ATIME_SET);
354 break;
355 case NFS4_SET_TO_SERVER_TIME:
356 iattr->ia_valid |= ATTR_ATIME;
357 break;
358 default:
359 goto xdr_error;
360 }
361 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 if (bmval[1] & FATTR4_WORD1_TIME_MODIFY_SET) {
363 READ_BUF(4);
364 len += 4;
365 READ32(dummy32);
366 switch (dummy32) {
367 case NFS4_SET_TO_CLIENT_TIME:
368 /* We require the high 32 bits of 'seconds' to be 0, and we ignore
369 all 32 bits of 'nseconds'. */
370 READ_BUF(12);
371 len += 12;
372 READ32(dummy32);
373 if (dummy32)
374 return nfserr_inval;
375 READ32(iattr->ia_mtime.tv_sec);
376 READ32(iattr->ia_mtime.tv_nsec);
377 if (iattr->ia_mtime.tv_nsec >= (u32)1000000000)
378 return nfserr_inval;
379 iattr->ia_valid |= (ATTR_MTIME | ATTR_MTIME_SET);
380 break;
381 case NFS4_SET_TO_SERVER_TIME:
382 iattr->ia_valid |= ATTR_MTIME;
383 break;
384 default:
385 goto xdr_error;
386 }
387 }
Yu Zhiguo3c8e0312009-05-16 16:22:31 +0800388 if (bmval[0] & ~NFSD_WRITEABLE_ATTRS_WORD0
389 || bmval[1] & ~NFSD_WRITEABLE_ATTRS_WORD1
390 || bmval[2] & ~NFSD_WRITEABLE_ATTRS_WORD2)
391 READ_BUF(expected_len - len);
392 else if (len != expected_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 goto xdr_error;
394
395 DECODE_TAIL;
396
397out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -0700398 status = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 goto out;
400}
401
Al Virob37ad282006-10-19 23:28:59 -0700402static __be32
Benny Halevye31a1b62008-08-12 20:46:18 +0300403nfsd4_decode_stateid(struct nfsd4_compoundargs *argp, stateid_t *sid)
404{
405 DECODE_HEAD;
406
407 READ_BUF(sizeof(stateid_t));
408 READ32(sid->si_generation);
409 COPYMEM(&sid->si_opaque, sizeof(stateid_opaque_t));
410
411 DECODE_TAIL;
412}
413
414static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415nfsd4_decode_access(struct nfsd4_compoundargs *argp, struct nfsd4_access *access)
416{
417 DECODE_HEAD;
418
419 READ_BUF(4);
420 READ32(access->ac_req_access);
421
422 DECODE_TAIL;
423}
424
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400425static __be32 nfsd4_decode_cb_sec(struct nfsd4_compoundargs *argp, struct nfsd4_cb_sec *cbs)
426{
427 DECODE_HEAD;
428 u32 dummy;
429 char *machine_name;
430 int i;
431 int nr_secflavs;
432
433 /* callback_sec_params4 */
434 READ_BUF(4);
435 READ32(nr_secflavs);
436 for (i = 0; i < nr_secflavs; ++i) {
437 READ_BUF(4);
438 READ32(dummy);
439 switch (dummy) {
440 case RPC_AUTH_NULL:
441 /* Nothing to read */
442 break;
443 case RPC_AUTH_UNIX:
444 READ_BUF(8);
445 /* stamp */
446 READ32(dummy);
447
448 /* machine name */
449 READ32(dummy);
450 READ_BUF(dummy);
451 SAVEMEM(machine_name, dummy);
452
453 /* uid, gid */
454 READ_BUF(8);
455 READ32(cbs->uid);
456 READ32(cbs->gid);
457
458 /* more gids */
459 READ_BUF(4);
460 READ32(dummy);
461 READ_BUF(dummy * 4);
462 break;
463 case RPC_AUTH_GSS:
464 dprintk("RPC_AUTH_GSS callback secflavor "
465 "not supported!\n");
466 READ_BUF(8);
467 /* gcbp_service */
468 READ32(dummy);
469 /* gcbp_handle_from_server */
470 READ32(dummy);
471 READ_BUF(dummy);
472 p += XDR_QUADLEN(dummy);
473 /* gcbp_handle_from_client */
474 READ_BUF(4);
475 READ32(dummy);
476 READ_BUF(dummy);
477 break;
478 default:
479 dprintk("Illegal callback secflavor\n");
480 return nfserr_inval;
481 }
482 }
483 DECODE_TAIL;
484}
485
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -0400486static __be32 nfsd4_decode_backchannel_ctl(struct nfsd4_compoundargs *argp, struct nfsd4_backchannel_ctl *bc)
487{
488 DECODE_HEAD;
489
490 READ_BUF(4);
491 READ32(bc->bc_cb_program);
492 nfsd4_decode_cb_sec(argp, &bc->bc_cb_sec);
493
494 DECODE_TAIL;
495}
496
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -0400497static __be32 nfsd4_decode_bind_conn_to_session(struct nfsd4_compoundargs *argp, struct nfsd4_bind_conn_to_session *bcts)
498{
499 DECODE_HEAD;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -0400500
501 READ_BUF(NFS4_MAX_SESSIONID_LEN + 8);
502 COPYMEM(bcts->sessionid.data, NFS4_MAX_SESSIONID_LEN);
503 READ32(bcts->dir);
Bryan Schumaker6ce23572011-04-27 15:47:16 -0400504 /* XXX: skipping ctsa_use_conn_in_rdma_mode. Perhaps Tom Tucker
505 * could help us figure out we should be using it. */
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -0400506 DECODE_TAIL;
507}
508
Al Virob37ad282006-10-19 23:28:59 -0700509static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510nfsd4_decode_close(struct nfsd4_compoundargs *argp, struct nfsd4_close *close)
511{
512 DECODE_HEAD;
513
Benny Halevye31a1b62008-08-12 20:46:18 +0300514 READ_BUF(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 READ32(close->cl_seqid);
Benny Halevye31a1b62008-08-12 20:46:18 +0300516 return nfsd4_decode_stateid(argp, &close->cl_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
518 DECODE_TAIL;
519}
520
521
Al Virob37ad282006-10-19 23:28:59 -0700522static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523nfsd4_decode_commit(struct nfsd4_compoundargs *argp, struct nfsd4_commit *commit)
524{
525 DECODE_HEAD;
526
527 READ_BUF(12);
528 READ64(commit->co_offset);
529 READ32(commit->co_count);
530
531 DECODE_TAIL;
532}
533
Al Virob37ad282006-10-19 23:28:59 -0700534static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535nfsd4_decode_create(struct nfsd4_compoundargs *argp, struct nfsd4_create *create)
536{
537 DECODE_HEAD;
538
539 READ_BUF(4);
540 READ32(create->cr_type);
541 switch (create->cr_type) {
542 case NF4LNK:
543 READ_BUF(4);
544 READ32(create->cr_linklen);
545 READ_BUF(create->cr_linklen);
546 SAVEMEM(create->cr_linkname, create->cr_linklen);
547 break;
548 case NF4BLK:
549 case NF4CHR:
550 READ_BUF(8);
551 READ32(create->cr_specdata1);
552 READ32(create->cr_specdata2);
553 break;
554 case NF4SOCK:
555 case NF4FIFO:
556 case NF4DIR:
557 default:
558 break;
559 }
560
561 READ_BUF(4);
562 READ32(create->cr_namelen);
563 READ_BUF(create->cr_namelen);
564 SAVEMEM(create->cr_name, create->cr_namelen);
565 if ((status = check_filename(create->cr_name, create->cr_namelen, nfserr_inval)))
566 return status;
567
Yu Zhiguo3c8e0312009-05-16 16:22:31 +0800568 status = nfsd4_decode_fattr(argp, create->cr_bmval, &create->cr_iattr,
569 &create->cr_acl);
Benny Halevyc0d6fc82009-04-03 08:29:05 +0300570 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 goto out;
572
573 DECODE_TAIL;
574}
575
Al Virob37ad282006-10-19 23:28:59 -0700576static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577nfsd4_decode_delegreturn(struct nfsd4_compoundargs *argp, struct nfsd4_delegreturn *dr)
578{
Benny Halevye31a1b62008-08-12 20:46:18 +0300579 return nfsd4_decode_stateid(argp, &dr->dr_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580}
581
Al Virob37ad282006-10-19 23:28:59 -0700582static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583nfsd4_decode_getattr(struct nfsd4_compoundargs *argp, struct nfsd4_getattr *getattr)
584{
585 return nfsd4_decode_bitmap(argp, getattr->ga_bmval);
586}
587
Al Virob37ad282006-10-19 23:28:59 -0700588static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589nfsd4_decode_link(struct nfsd4_compoundargs *argp, struct nfsd4_link *link)
590{
591 DECODE_HEAD;
592
593 READ_BUF(4);
594 READ32(link->li_namelen);
595 READ_BUF(link->li_namelen);
596 SAVEMEM(link->li_name, link->li_namelen);
597 if ((status = check_filename(link->li_name, link->li_namelen, nfserr_inval)))
598 return status;
599
600 DECODE_TAIL;
601}
602
Al Virob37ad282006-10-19 23:28:59 -0700603static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604nfsd4_decode_lock(struct nfsd4_compoundargs *argp, struct nfsd4_lock *lock)
605{
606 DECODE_HEAD;
607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 /*
609 * type, reclaim(boolean), offset, length, new_lock_owner(boolean)
610 */
611 READ_BUF(28);
612 READ32(lock->lk_type);
613 if ((lock->lk_type < NFS4_READ_LT) || (lock->lk_type > NFS4_WRITEW_LT))
614 goto xdr_error;
615 READ32(lock->lk_reclaim);
616 READ64(lock->lk_offset);
617 READ64(lock->lk_length);
618 READ32(lock->lk_is_new);
619
620 if (lock->lk_is_new) {
Benny Halevye31a1b62008-08-12 20:46:18 +0300621 READ_BUF(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 READ32(lock->lk_new_open_seqid);
Benny Halevye31a1b62008-08-12 20:46:18 +0300623 status = nfsd4_decode_stateid(argp, &lock->lk_new_open_stateid);
624 if (status)
625 return status;
626 READ_BUF(8 + sizeof(clientid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 READ32(lock->lk_new_lock_seqid);
628 COPYMEM(&lock->lk_new_clientid, sizeof(clientid_t));
629 READ32(lock->lk_new_owner.len);
630 READ_BUF(lock->lk_new_owner.len);
631 READMEM(lock->lk_new_owner.data, lock->lk_new_owner.len);
632 } else {
Benny Halevye31a1b62008-08-12 20:46:18 +0300633 status = nfsd4_decode_stateid(argp, &lock->lk_old_lock_stateid);
634 if (status)
635 return status;
636 READ_BUF(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 READ32(lock->lk_old_lock_seqid);
638 }
639
640 DECODE_TAIL;
641}
642
Al Virob37ad282006-10-19 23:28:59 -0700643static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644nfsd4_decode_lockt(struct nfsd4_compoundargs *argp, struct nfsd4_lockt *lockt)
645{
646 DECODE_HEAD;
647
648 READ_BUF(32);
649 READ32(lockt->lt_type);
650 if((lockt->lt_type < NFS4_READ_LT) || (lockt->lt_type > NFS4_WRITEW_LT))
651 goto xdr_error;
652 READ64(lockt->lt_offset);
653 READ64(lockt->lt_length);
654 COPYMEM(&lockt->lt_clientid, 8);
655 READ32(lockt->lt_owner.len);
656 READ_BUF(lockt->lt_owner.len);
657 READMEM(lockt->lt_owner.data, lockt->lt_owner.len);
658
659 DECODE_TAIL;
660}
661
Al Virob37ad282006-10-19 23:28:59 -0700662static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663nfsd4_decode_locku(struct nfsd4_compoundargs *argp, struct nfsd4_locku *locku)
664{
665 DECODE_HEAD;
666
Benny Halevye31a1b62008-08-12 20:46:18 +0300667 READ_BUF(8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 READ32(locku->lu_type);
669 if ((locku->lu_type < NFS4_READ_LT) || (locku->lu_type > NFS4_WRITEW_LT))
670 goto xdr_error;
671 READ32(locku->lu_seqid);
Benny Halevye31a1b62008-08-12 20:46:18 +0300672 status = nfsd4_decode_stateid(argp, &locku->lu_stateid);
673 if (status)
674 return status;
675 READ_BUF(16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 READ64(locku->lu_offset);
677 READ64(locku->lu_length);
678
679 DECODE_TAIL;
680}
681
Al Virob37ad282006-10-19 23:28:59 -0700682static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683nfsd4_decode_lookup(struct nfsd4_compoundargs *argp, struct nfsd4_lookup *lookup)
684{
685 DECODE_HEAD;
686
687 READ_BUF(4);
688 READ32(lookup->lo_len);
689 READ_BUF(lookup->lo_len);
690 SAVEMEM(lookup->lo_name, lookup->lo_len);
691 if ((status = check_filename(lookup->lo_name, lookup->lo_len, nfserr_noent)))
692 return status;
693
694 DECODE_TAIL;
695}
696
Benny Halevy2c8bd7e2012-02-16 20:57:09 +0200697static __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 -0400698{
699 __be32 *p;
700 u32 w;
701
702 READ_BUF(4);
703 READ32(w);
Benny Halevy2c8bd7e2012-02-16 20:57:09 +0200704 *share_access = w & NFS4_SHARE_ACCESS_MASK;
705 *deleg_want = w & NFS4_SHARE_WANT_MASK;
706 if (deleg_when)
707 *deleg_when = w & NFS4_SHARE_WHEN_MASK;
708
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400709 switch (w & NFS4_SHARE_ACCESS_MASK) {
710 case NFS4_SHARE_ACCESS_READ:
711 case NFS4_SHARE_ACCESS_WRITE:
712 case NFS4_SHARE_ACCESS_BOTH:
713 break;
714 default:
715 return nfserr_bad_xdr;
716 }
Benny Halevyfc0d14f2011-10-27 20:43:01 +0200717 w &= ~NFS4_SHARE_ACCESS_MASK;
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400718 if (!w)
719 return nfs_ok;
720 if (!argp->minorversion)
721 return nfserr_bad_xdr;
722 switch (w & NFS4_SHARE_WANT_MASK) {
723 case NFS4_SHARE_WANT_NO_PREFERENCE:
724 case NFS4_SHARE_WANT_READ_DELEG:
725 case NFS4_SHARE_WANT_WRITE_DELEG:
726 case NFS4_SHARE_WANT_ANY_DELEG:
727 case NFS4_SHARE_WANT_NO_DELEG:
728 case NFS4_SHARE_WANT_CANCEL:
729 break;
730 default:
731 return nfserr_bad_xdr;
732 }
Benny Halevy92bac8c2011-10-19 19:13:29 -0700733 w &= ~NFS4_SHARE_WANT_MASK;
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400734 if (!w)
735 return nfs_ok;
Benny Halevy2c8bd7e2012-02-16 20:57:09 +0200736
737 if (!deleg_when) /* open_downgrade */
738 return nfserr_inval;
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400739 switch (w) {
740 case NFS4_SHARE_SIGNAL_DELEG_WHEN_RESRC_AVAIL:
741 case NFS4_SHARE_PUSH_DELEG_WHEN_UNCONTENDED:
Benny Halevyc668fc62011-10-19 19:13:13 -0700742 case (NFS4_SHARE_SIGNAL_DELEG_WHEN_RESRC_AVAIL |
743 NFS4_SHARE_PUSH_DELEG_WHEN_UNCONTENDED):
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400744 return nfs_ok;
745 }
746xdr_error:
747 return nfserr_bad_xdr;
748}
749
750static __be32 nfsd4_decode_share_deny(struct nfsd4_compoundargs *argp, u32 *x)
751{
752 __be32 *p;
753
754 READ_BUF(4);
755 READ32(*x);
756 /* Note: unlinke access bits, deny bits may be zero. */
Dan Carpenter01cd4af2011-10-17 10:41:17 +0300757 if (*x & ~NFS4_SHARE_DENY_BOTH)
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400758 return nfserr_bad_xdr;
759 return nfs_ok;
760xdr_error:
761 return nfserr_bad_xdr;
762}
763
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -0400764static __be32 nfsd4_decode_opaque(struct nfsd4_compoundargs *argp, struct xdr_netobj *o)
765{
766 __be32 *p;
767
768 READ_BUF(4);
769 READ32(o->len);
770
771 if (o->len == 0 || o->len > NFS4_OPAQUE_LIMIT)
772 return nfserr_bad_xdr;
773
774 READ_BUF(o->len);
775 SAVEMEM(o->data, o->len);
776 return nfs_ok;
777xdr_error:
778 return nfserr_bad_xdr;
779}
780
Al Virob37ad282006-10-19 23:28:59 -0700781static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782nfsd4_decode_open(struct nfsd4_compoundargs *argp, struct nfsd4_open *open)
783{
784 DECODE_HEAD;
Benny Halevy2c8bd7e2012-02-16 20:57:09 +0200785 u32 dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
787 memset(open->op_bmval, 0, sizeof(open->op_bmval));
788 open->op_iattr.ia_valid = 0;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400789 open->op_openowner = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
791 /* seqid, share_access, share_deny, clientid, ownerlen */
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400792 READ_BUF(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 READ32(open->op_seqid);
Benny Halevy2c8bd7e2012-02-16 20:57:09 +0200794 /* decode, yet ignore deleg_when until supported */
795 status = nfsd4_decode_share_access(argp, &open->op_share_access,
796 &open->op_deleg_want, &dummy);
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400797 if (status)
798 goto xdr_error;
799 status = nfsd4_decode_share_deny(argp, &open->op_share_deny);
800 if (status)
801 goto xdr_error;
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -0400802 READ_BUF(sizeof(clientid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 COPYMEM(&open->op_clientid, sizeof(clientid_t));
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -0400804 status = nfsd4_decode_opaque(argp, &open->op_owner);
805 if (status)
806 goto xdr_error;
807 READ_BUF(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 READ32(open->op_create);
809 switch (open->op_create) {
810 case NFS4_OPEN_NOCREATE:
811 break;
812 case NFS4_OPEN_CREATE:
813 READ_BUF(4);
814 READ32(open->op_createmode);
815 switch (open->op_createmode) {
816 case NFS4_CREATE_UNCHECKED:
817 case NFS4_CREATE_GUARDED:
Benny Halevyc0d6fc82009-04-03 08:29:05 +0300818 status = nfsd4_decode_fattr(argp, open->op_bmval,
Yu Zhiguo3c8e0312009-05-16 16:22:31 +0800819 &open->op_iattr, &open->op_acl);
Benny Halevyc0d6fc82009-04-03 08:29:05 +0300820 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 goto out;
822 break;
823 case NFS4_CREATE_EXCLUSIVE:
Chuck Leverab4684d2012-03-02 17:13:50 -0500824 READ_BUF(NFS4_VERIFIER_SIZE);
825 COPYMEM(open->op_verf.data, NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 break;
Benny Halevy79fb54a2009-04-03 08:29:17 +0300827 case NFS4_CREATE_EXCLUSIVE4_1:
828 if (argp->minorversion < 1)
829 goto xdr_error;
Chuck Leverab4684d2012-03-02 17:13:50 -0500830 READ_BUF(NFS4_VERIFIER_SIZE);
831 COPYMEM(open->op_verf.data, NFS4_VERIFIER_SIZE);
Benny Halevy79fb54a2009-04-03 08:29:17 +0300832 status = nfsd4_decode_fattr(argp, open->op_bmval,
Yu Zhiguo3c8e0312009-05-16 16:22:31 +0800833 &open->op_iattr, &open->op_acl);
Benny Halevy79fb54a2009-04-03 08:29:17 +0300834 if (status)
835 goto out;
836 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 default:
838 goto xdr_error;
839 }
840 break;
841 default:
842 goto xdr_error;
843 }
844
845 /* open_claim */
846 READ_BUF(4);
847 READ32(open->op_claim_type);
848 switch (open->op_claim_type) {
849 case NFS4_OPEN_CLAIM_NULL:
850 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
851 READ_BUF(4);
852 READ32(open->op_fname.len);
853 READ_BUF(open->op_fname.len);
854 SAVEMEM(open->op_fname.data, open->op_fname.len);
855 if ((status = check_filename(open->op_fname.data, open->op_fname.len, nfserr_inval)))
856 return status;
857 break;
858 case NFS4_OPEN_CLAIM_PREVIOUS:
859 READ_BUF(4);
860 READ32(open->op_delegate_type);
861 break;
862 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
Benny Halevye31a1b62008-08-12 20:46:18 +0300863 status = nfsd4_decode_stateid(argp, &open->op_delegate_stateid);
864 if (status)
865 return status;
866 READ_BUF(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 READ32(open->op_fname.len);
868 READ_BUF(open->op_fname.len);
869 SAVEMEM(open->op_fname.data, open->op_fname.len);
870 if ((status = check_filename(open->op_fname.data, open->op_fname.len, nfserr_inval)))
871 return status;
872 break;
J. Bruce Fields8b289b22011-10-19 11:52:12 -0400873 case NFS4_OPEN_CLAIM_FH:
874 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
875 if (argp->minorversion < 1)
876 goto xdr_error;
877 /* void */
878 break;
879 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
880 if (argp->minorversion < 1)
881 goto xdr_error;
882 status = nfsd4_decode_stateid(argp, &open->op_delegate_stateid);
883 if (status)
884 return status;
885 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 default:
887 goto xdr_error;
888 }
889
890 DECODE_TAIL;
891}
892
Al Virob37ad282006-10-19 23:28:59 -0700893static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894nfsd4_decode_open_confirm(struct nfsd4_compoundargs *argp, struct nfsd4_open_confirm *open_conf)
895{
896 DECODE_HEAD;
897
Benny Halevye31a1b62008-08-12 20:46:18 +0300898 status = nfsd4_decode_stateid(argp, &open_conf->oc_req_stateid);
899 if (status)
900 return status;
901 READ_BUF(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 READ32(open_conf->oc_seqid);
903
904 DECODE_TAIL;
905}
906
Al Virob37ad282006-10-19 23:28:59 -0700907static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908nfsd4_decode_open_downgrade(struct nfsd4_compoundargs *argp, struct nfsd4_open_downgrade *open_down)
909{
910 DECODE_HEAD;
911
Benny Halevye31a1b62008-08-12 20:46:18 +0300912 status = nfsd4_decode_stateid(argp, &open_down->od_stateid);
913 if (status)
914 return status;
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400915 READ_BUF(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 READ32(open_down->od_seqid);
Benny Halevy2c8bd7e2012-02-16 20:57:09 +0200917 status = nfsd4_decode_share_access(argp, &open_down->od_share_access,
918 &open_down->od_deleg_want, NULL);
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400919 if (status)
920 return status;
921 status = nfsd4_decode_share_deny(argp, &open_down->od_share_deny);
922 if (status)
923 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 DECODE_TAIL;
925}
926
Al Virob37ad282006-10-19 23:28:59 -0700927static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928nfsd4_decode_putfh(struct nfsd4_compoundargs *argp, struct nfsd4_putfh *putfh)
929{
930 DECODE_HEAD;
931
932 READ_BUF(4);
933 READ32(putfh->pf_fhlen);
934 if (putfh->pf_fhlen > NFS4_FHSIZE)
935 goto xdr_error;
936 READ_BUF(putfh->pf_fhlen);
937 SAVEMEM(putfh->pf_fhval, putfh->pf_fhlen);
938
939 DECODE_TAIL;
940}
941
Al Virob37ad282006-10-19 23:28:59 -0700942static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943nfsd4_decode_read(struct nfsd4_compoundargs *argp, struct nfsd4_read *read)
944{
945 DECODE_HEAD;
946
Benny Halevye31a1b62008-08-12 20:46:18 +0300947 status = nfsd4_decode_stateid(argp, &read->rd_stateid);
948 if (status)
949 return status;
950 READ_BUF(12);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 READ64(read->rd_offset);
952 READ32(read->rd_length);
953
954 DECODE_TAIL;
955}
956
Al Virob37ad282006-10-19 23:28:59 -0700957static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958nfsd4_decode_readdir(struct nfsd4_compoundargs *argp, struct nfsd4_readdir *readdir)
959{
960 DECODE_HEAD;
961
962 READ_BUF(24);
963 READ64(readdir->rd_cookie);
964 COPYMEM(readdir->rd_verf.data, sizeof(readdir->rd_verf.data));
965 READ32(readdir->rd_dircount); /* just in case you needed a useless field... */
966 READ32(readdir->rd_maxcount);
967 if ((status = nfsd4_decode_bitmap(argp, readdir->rd_bmval)))
968 goto out;
969
970 DECODE_TAIL;
971}
972
Al Virob37ad282006-10-19 23:28:59 -0700973static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974nfsd4_decode_remove(struct nfsd4_compoundargs *argp, struct nfsd4_remove *remove)
975{
976 DECODE_HEAD;
977
978 READ_BUF(4);
979 READ32(remove->rm_namelen);
980 READ_BUF(remove->rm_namelen);
981 SAVEMEM(remove->rm_name, remove->rm_namelen);
982 if ((status = check_filename(remove->rm_name, remove->rm_namelen, nfserr_noent)))
983 return status;
984
985 DECODE_TAIL;
986}
987
Al Virob37ad282006-10-19 23:28:59 -0700988static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989nfsd4_decode_rename(struct nfsd4_compoundargs *argp, struct nfsd4_rename *rename)
990{
991 DECODE_HEAD;
992
993 READ_BUF(4);
994 READ32(rename->rn_snamelen);
995 READ_BUF(rename->rn_snamelen + 4);
996 SAVEMEM(rename->rn_sname, rename->rn_snamelen);
997 READ32(rename->rn_tnamelen);
998 READ_BUF(rename->rn_tnamelen);
999 SAVEMEM(rename->rn_tname, rename->rn_tnamelen);
1000 if ((status = check_filename(rename->rn_sname, rename->rn_snamelen, nfserr_noent)))
1001 return status;
1002 if ((status = check_filename(rename->rn_tname, rename->rn_tnamelen, nfserr_inval)))
1003 return status;
1004
1005 DECODE_TAIL;
1006}
1007
Al Virob37ad282006-10-19 23:28:59 -07001008static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009nfsd4_decode_renew(struct nfsd4_compoundargs *argp, clientid_t *clientid)
1010{
1011 DECODE_HEAD;
1012
1013 READ_BUF(sizeof(clientid_t));
1014 COPYMEM(clientid, sizeof(clientid_t));
1015
1016 DECODE_TAIL;
1017}
1018
Al Virob37ad282006-10-19 23:28:59 -07001019static __be32
Andy Adamsondcb488a32007-07-17 04:04:51 -07001020nfsd4_decode_secinfo(struct nfsd4_compoundargs *argp,
1021 struct nfsd4_secinfo *secinfo)
1022{
1023 DECODE_HEAD;
1024
1025 READ_BUF(4);
1026 READ32(secinfo->si_namelen);
1027 READ_BUF(secinfo->si_namelen);
1028 SAVEMEM(secinfo->si_name, secinfo->si_namelen);
1029 status = check_filename(secinfo->si_name, secinfo->si_namelen,
1030 nfserr_noent);
1031 if (status)
1032 return status;
1033 DECODE_TAIL;
1034}
1035
1036static __be32
J. Bruce Fields04f4ad12010-12-16 09:51:13 -05001037nfsd4_decode_secinfo_no_name(struct nfsd4_compoundargs *argp,
1038 struct nfsd4_secinfo_no_name *sin)
1039{
1040 DECODE_HEAD;
1041
1042 READ_BUF(4);
1043 READ32(sin->sin_style);
1044 DECODE_TAIL;
1045}
1046
1047static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048nfsd4_decode_setattr(struct nfsd4_compoundargs *argp, struct nfsd4_setattr *setattr)
1049{
Benny Halevye31a1b62008-08-12 20:46:18 +03001050 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
Benny Halevye31a1b62008-08-12 20:46:18 +03001052 status = nfsd4_decode_stateid(argp, &setattr->sa_stateid);
1053 if (status)
1054 return status;
Yu Zhiguo3c8e0312009-05-16 16:22:31 +08001055 return nfsd4_decode_fattr(argp, setattr->sa_bmval, &setattr->sa_iattr,
1056 &setattr->sa_acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057}
1058
Al Virob37ad282006-10-19 23:28:59 -07001059static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060nfsd4_decode_setclientid(struct nfsd4_compoundargs *argp, struct nfsd4_setclientid *setclientid)
1061{
1062 DECODE_HEAD;
1063
Chuck Leverab4684d2012-03-02 17:13:50 -05001064 READ_BUF(NFS4_VERIFIER_SIZE);
1065 COPYMEM(setclientid->se_verf.data, NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04001067 status = nfsd4_decode_opaque(argp, &setclientid->se_name);
1068 if (status)
1069 return nfserr_bad_xdr;
1070 READ_BUF(8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 READ32(setclientid->se_callback_prog);
1072 READ32(setclientid->se_callback_netid_len);
1073
1074 READ_BUF(setclientid->se_callback_netid_len + 4);
1075 SAVEMEM(setclientid->se_callback_netid_val, setclientid->se_callback_netid_len);
1076 READ32(setclientid->se_callback_addr_len);
1077
1078 READ_BUF(setclientid->se_callback_addr_len + 4);
1079 SAVEMEM(setclientid->se_callback_addr_val, setclientid->se_callback_addr_len);
1080 READ32(setclientid->se_callback_ident);
1081
1082 DECODE_TAIL;
1083}
1084
Al Virob37ad282006-10-19 23:28:59 -07001085static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086nfsd4_decode_setclientid_confirm(struct nfsd4_compoundargs *argp, struct nfsd4_setclientid_confirm *scd_c)
1087{
1088 DECODE_HEAD;
1089
Chuck Leverab4684d2012-03-02 17:13:50 -05001090 READ_BUF(8 + NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 COPYMEM(&scd_c->sc_clientid, 8);
Chuck Leverab4684d2012-03-02 17:13:50 -05001092 COPYMEM(&scd_c->sc_confirm, NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
1094 DECODE_TAIL;
1095}
1096
1097/* Also used for NVERIFY */
Al Virob37ad282006-10-19 23:28:59 -07001098static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099nfsd4_decode_verify(struct nfsd4_compoundargs *argp, struct nfsd4_verify *verify)
1100{
1101#if 0
1102 struct nfsd4_compoundargs save = {
1103 .p = argp->p,
1104 .end = argp->end,
1105 .rqstp = argp->rqstp,
1106 };
1107 u32 ve_bmval[2];
1108 struct iattr ve_iattr; /* request */
1109 struct nfs4_acl *ve_acl; /* request */
1110#endif
1111 DECODE_HEAD;
1112
1113 if ((status = nfsd4_decode_bitmap(argp, verify->ve_bmval)))
1114 goto out;
1115
1116 /* For convenience's sake, we compare raw xdr'd attributes in
1117 * nfsd4_proc_verify; however we still decode here just to return
1118 * correct error in case of bad xdr. */
1119#if 0
1120 status = nfsd4_decode_fattr(ve_bmval, &ve_iattr, &ve_acl);
1121 if (status == nfserr_inval) {
1122 status = nfserrno(status);
1123 goto out;
1124 }
1125#endif
1126 READ_BUF(4);
1127 READ32(verify->ve_attrlen);
1128 READ_BUF(verify->ve_attrlen);
1129 SAVEMEM(verify->ve_attrval, verify->ve_attrlen);
1130
1131 DECODE_TAIL;
1132}
1133
Al Virob37ad282006-10-19 23:28:59 -07001134static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135nfsd4_decode_write(struct nfsd4_compoundargs *argp, struct nfsd4_write *write)
1136{
1137 int avail;
1138 int v;
1139 int len;
1140 DECODE_HEAD;
1141
Benny Halevye31a1b62008-08-12 20:46:18 +03001142 status = nfsd4_decode_stateid(argp, &write->wr_stateid);
1143 if (status)
1144 return status;
1145 READ_BUF(16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 READ64(write->wr_offset);
1147 READ32(write->wr_stable_how);
1148 if (write->wr_stable_how > 2)
1149 goto xdr_error;
1150 READ32(write->wr_buflen);
1151
1152 /* Sorry .. no magic macros for this.. *
1153 * READ_BUF(write->wr_buflen);
1154 * SAVEMEM(write->wr_buf, write->wr_buflen);
1155 */
1156 avail = (char*)argp->end - (char*)argp->p;
1157 if (avail + argp->pagelen < write->wr_buflen) {
Chuck Lever817cb9d2007-09-11 18:01:20 -04001158 dprintk("NFSD: xdr error (%s:%d)\n",
1159 __FILE__, __LINE__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 goto xdr_error;
1161 }
NeilBrown3cc03b12006-10-04 02:15:47 -07001162 argp->rqstp->rq_vec[0].iov_base = p;
1163 argp->rqstp->rq_vec[0].iov_len = avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 v = 0;
1165 len = write->wr_buflen;
NeilBrown3cc03b12006-10-04 02:15:47 -07001166 while (len > argp->rqstp->rq_vec[v].iov_len) {
1167 len -= argp->rqstp->rq_vec[v].iov_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 v++;
NeilBrown3cc03b12006-10-04 02:15:47 -07001169 argp->rqstp->rq_vec[v].iov_base = page_address(argp->pagelist[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 argp->pagelist++;
1171 if (argp->pagelen >= PAGE_SIZE) {
NeilBrown3cc03b12006-10-04 02:15:47 -07001172 argp->rqstp->rq_vec[v].iov_len = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 argp->pagelen -= PAGE_SIZE;
1174 } else {
NeilBrown3cc03b12006-10-04 02:15:47 -07001175 argp->rqstp->rq_vec[v].iov_len = argp->pagelen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 argp->pagelen -= len;
1177 }
1178 }
Al Viro2ebbc012006-10-19 23:28:58 -07001179 argp->end = (__be32*) (argp->rqstp->rq_vec[v].iov_base + argp->rqstp->rq_vec[v].iov_len);
1180 argp->p = (__be32*) (argp->rqstp->rq_vec[v].iov_base + (XDR_QUADLEN(len) << 2));
NeilBrown3cc03b12006-10-04 02:15:47 -07001181 argp->rqstp->rq_vec[v].iov_len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 write->wr_vlen = v+1;
1183
1184 DECODE_TAIL;
1185}
1186
Al Virob37ad282006-10-19 23:28:59 -07001187static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188nfsd4_decode_release_lockowner(struct nfsd4_compoundargs *argp, struct nfsd4_release_lockowner *rlockowner)
1189{
1190 DECODE_HEAD;
1191
1192 READ_BUF(12);
1193 COPYMEM(&rlockowner->rl_clientid, sizeof(clientid_t));
1194 READ32(rlockowner->rl_owner.len);
1195 READ_BUF(rlockowner->rl_owner.len);
1196 READMEM(rlockowner->rl_owner.data, rlockowner->rl_owner.len);
1197
Andy Adamson60adfc52009-04-03 08:28:50 +03001198 if (argp->minorversion && !zero_clientid(&rlockowner->rl_clientid))
1199 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 DECODE_TAIL;
1201}
1202
Al Virob37ad282006-10-19 23:28:59 -07001203static __be32
Andy Adamson2db134e2009-04-03 08:27:55 +03001204nfsd4_decode_exchange_id(struct nfsd4_compoundargs *argp,
Andy Adamson0733d212009-04-03 08:28:01 +03001205 struct nfsd4_exchange_id *exid)
Andy Adamson2db134e2009-04-03 08:27:55 +03001206{
Mi Jinlong5afa0402010-11-09 09:39:23 +08001207 int dummy, tmp;
Andy Adamson0733d212009-04-03 08:28:01 +03001208 DECODE_HEAD;
1209
1210 READ_BUF(NFS4_VERIFIER_SIZE);
1211 COPYMEM(exid->verifier.data, NFS4_VERIFIER_SIZE);
1212
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04001213 status = nfsd4_decode_opaque(argp, &exid->clname);
1214 if (status)
1215 return nfserr_bad_xdr;
Andy Adamson0733d212009-04-03 08:28:01 +03001216
1217 READ_BUF(4);
1218 READ32(exid->flags);
1219
1220 /* Ignore state_protect4_a */
1221 READ_BUF(4);
1222 READ32(exid->spa_how);
1223 switch (exid->spa_how) {
1224 case SP4_NONE:
1225 break;
1226 case SP4_MACH_CRED:
1227 /* spo_must_enforce */
1228 READ_BUF(4);
1229 READ32(dummy);
1230 READ_BUF(dummy * 4);
1231 p += dummy;
1232
1233 /* spo_must_allow */
1234 READ_BUF(4);
1235 READ32(dummy);
1236 READ_BUF(dummy * 4);
1237 p += dummy;
1238 break;
1239 case SP4_SSV:
1240 /* ssp_ops */
1241 READ_BUF(4);
1242 READ32(dummy);
1243 READ_BUF(dummy * 4);
1244 p += dummy;
1245
1246 READ_BUF(4);
1247 READ32(dummy);
1248 READ_BUF(dummy * 4);
1249 p += dummy;
1250
1251 /* ssp_hash_algs<> */
1252 READ_BUF(4);
Mi Jinlong5afa0402010-11-09 09:39:23 +08001253 READ32(tmp);
1254 while (tmp--) {
1255 READ_BUF(4);
1256 READ32(dummy);
1257 READ_BUF(dummy);
1258 p += XDR_QUADLEN(dummy);
1259 }
Andy Adamson0733d212009-04-03 08:28:01 +03001260
1261 /* ssp_encr_algs<> */
1262 READ_BUF(4);
Mi Jinlong5afa0402010-11-09 09:39:23 +08001263 READ32(tmp);
1264 while (tmp--) {
1265 READ_BUF(4);
1266 READ32(dummy);
1267 READ_BUF(dummy);
1268 p += XDR_QUADLEN(dummy);
1269 }
Andy Adamson0733d212009-04-03 08:28:01 +03001270
1271 /* ssp_window and ssp_num_gss_handles */
1272 READ_BUF(8);
1273 READ32(dummy);
1274 READ32(dummy);
1275 break;
1276 default:
1277 goto xdr_error;
1278 }
1279
1280 /* Ignore Implementation ID */
1281 READ_BUF(4); /* nfs_impl_id4 array length */
1282 READ32(dummy);
1283
1284 if (dummy > 1)
1285 goto xdr_error;
1286
1287 if (dummy == 1) {
1288 /* nii_domain */
1289 READ_BUF(4);
1290 READ32(dummy);
1291 READ_BUF(dummy);
1292 p += XDR_QUADLEN(dummy);
1293
1294 /* nii_name */
1295 READ_BUF(4);
1296 READ32(dummy);
1297 READ_BUF(dummy);
1298 p += XDR_QUADLEN(dummy);
1299
1300 /* nii_date */
1301 READ_BUF(12);
1302 p += 3;
1303 }
1304 DECODE_TAIL;
Andy Adamson2db134e2009-04-03 08:27:55 +03001305}
1306
1307static __be32
1308nfsd4_decode_create_session(struct nfsd4_compoundargs *argp,
1309 struct nfsd4_create_session *sess)
1310{
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001311 DECODE_HEAD;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001312 u32 dummy;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001313
1314 READ_BUF(16);
1315 COPYMEM(&sess->clientid, 8);
1316 READ32(sess->seqid);
1317 READ32(sess->flags);
1318
1319 /* Fore channel attrs */
1320 READ_BUF(28);
1321 READ32(dummy); /* headerpadsz is always 0 */
1322 READ32(sess->fore_channel.maxreq_sz);
1323 READ32(sess->fore_channel.maxresp_sz);
1324 READ32(sess->fore_channel.maxresp_cached);
1325 READ32(sess->fore_channel.maxops);
1326 READ32(sess->fore_channel.maxreqs);
1327 READ32(sess->fore_channel.nr_rdma_attrs);
1328 if (sess->fore_channel.nr_rdma_attrs == 1) {
1329 READ_BUF(4);
1330 READ32(sess->fore_channel.rdma_attrs);
1331 } else if (sess->fore_channel.nr_rdma_attrs > 1) {
1332 dprintk("Too many fore channel attr bitmaps!\n");
1333 goto xdr_error;
1334 }
1335
1336 /* Back channel attrs */
1337 READ_BUF(28);
1338 READ32(dummy); /* headerpadsz is always 0 */
1339 READ32(sess->back_channel.maxreq_sz);
1340 READ32(sess->back_channel.maxresp_sz);
1341 READ32(sess->back_channel.maxresp_cached);
1342 READ32(sess->back_channel.maxops);
1343 READ32(sess->back_channel.maxreqs);
1344 READ32(sess->back_channel.nr_rdma_attrs);
1345 if (sess->back_channel.nr_rdma_attrs == 1) {
1346 READ_BUF(4);
1347 READ32(sess->back_channel.rdma_attrs);
1348 } else if (sess->back_channel.nr_rdma_attrs > 1) {
1349 dprintk("Too many back channel attr bitmaps!\n");
1350 goto xdr_error;
1351 }
1352
J. Bruce Fieldsacb28872012-03-27 14:50:26 -04001353 READ_BUF(4);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001354 READ32(sess->callback_prog);
J. Bruce Fieldsacb28872012-03-27 14:50:26 -04001355 nfsd4_decode_cb_sec(argp, &sess->cb_sec);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001356 DECODE_TAIL;
Andy Adamson2db134e2009-04-03 08:27:55 +03001357}
1358
1359static __be32
1360nfsd4_decode_destroy_session(struct nfsd4_compoundargs *argp,
1361 struct nfsd4_destroy_session *destroy_session)
1362{
Benny Halevye10e0cf2009-04-03 08:28:38 +03001363 DECODE_HEAD;
1364 READ_BUF(NFS4_MAX_SESSIONID_LEN);
1365 COPYMEM(destroy_session->sessionid.data, NFS4_MAX_SESSIONID_LEN);
1366
1367 DECODE_TAIL;
Andy Adamson2db134e2009-04-03 08:27:55 +03001368}
1369
1370static __be32
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04001371nfsd4_decode_free_stateid(struct nfsd4_compoundargs *argp,
1372 struct nfsd4_free_stateid *free_stateid)
1373{
1374 DECODE_HEAD;
1375
1376 READ_BUF(sizeof(stateid_t));
1377 READ32(free_stateid->fr_stateid.si_generation);
1378 COPYMEM(&free_stateid->fr_stateid.si_opaque, sizeof(stateid_opaque_t));
1379
1380 DECODE_TAIL;
1381}
1382
1383static __be32
Andy Adamson2db134e2009-04-03 08:27:55 +03001384nfsd4_decode_sequence(struct nfsd4_compoundargs *argp,
1385 struct nfsd4_sequence *seq)
1386{
Benny Halevyb85d4c02009-04-03 08:28:08 +03001387 DECODE_HEAD;
1388
1389 READ_BUF(NFS4_MAX_SESSIONID_LEN + 16);
1390 COPYMEM(seq->sessionid.data, NFS4_MAX_SESSIONID_LEN);
1391 READ32(seq->seqid);
1392 READ32(seq->slotid);
1393 READ32(seq->maxslots);
1394 READ32(seq->cachethis);
1395
1396 DECODE_TAIL;
Andy Adamson2db134e2009-04-03 08:27:55 +03001397}
1398
Bryan Schumaker17456802011-07-13 10:50:48 -04001399static __be32
1400nfsd4_decode_test_stateid(struct nfsd4_compoundargs *argp, struct nfsd4_test_stateid *test_stateid)
1401{
Bryan Schumaker17456802011-07-13 10:50:48 -04001402 int i;
Bryan Schumaker03cfb422012-01-27 10:22:49 -05001403 __be32 *p, status;
1404 struct nfsd4_test_stateid_id *stateid;
Bryan Schumaker17456802011-07-13 10:50:48 -04001405
1406 READ_BUF(4);
1407 test_stateid->ts_num_ids = ntohl(*p++);
1408
Bryan Schumaker03cfb422012-01-27 10:22:49 -05001409 INIT_LIST_HEAD(&test_stateid->ts_stateid_list);
Bryan Schumaker17456802011-07-13 10:50:48 -04001410
1411 for (i = 0; i < test_stateid->ts_num_ids; i++) {
Bryan Schumaker03cfb422012-01-27 10:22:49 -05001412 stateid = kmalloc(sizeof(struct nfsd4_test_stateid_id), GFP_KERNEL);
1413 if (!stateid) {
Al Viroafcf6792012-04-13 00:15:37 -04001414 status = nfserrno(-ENOMEM);
Bryan Schumaker03cfb422012-01-27 10:22:49 -05001415 goto out;
1416 }
1417
1418 defer_free(argp, kfree, stateid);
1419 INIT_LIST_HEAD(&stateid->ts_id_list);
1420 list_add_tail(&stateid->ts_id_list, &test_stateid->ts_stateid_list);
1421
1422 status = nfsd4_decode_stateid(argp, &stateid->ts_id_stateid);
Bryan Schumaker17456802011-07-13 10:50:48 -04001423 if (status)
Bryan Schumaker03cfb422012-01-27 10:22:49 -05001424 goto out;
Bryan Schumaker17456802011-07-13 10:50:48 -04001425 }
1426
1427 status = 0;
1428out:
1429 return status;
1430xdr_error:
1431 dprintk("NFSD: xdr error (%s:%d)\n", __FILE__, __LINE__);
1432 status = nfserr_bad_xdr;
1433 goto out;
1434}
1435
Mi Jinlong345c2842011-10-20 17:51:39 +08001436static __be32 nfsd4_decode_destroy_clientid(struct nfsd4_compoundargs *argp, struct nfsd4_destroy_clientid *dc)
1437{
1438 DECODE_HEAD;
1439
1440 READ_BUF(8);
1441 COPYMEM(&dc->clientid, 8);
1442
1443 DECODE_TAIL;
1444}
1445
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04001446static __be32 nfsd4_decode_reclaim_complete(struct nfsd4_compoundargs *argp, struct nfsd4_reclaim_complete *rc)
1447{
1448 DECODE_HEAD;
1449
1450 READ_BUF(4);
1451 READ32(rc->rca_one_fs);
1452
1453 DECODE_TAIL;
1454}
1455
Andy Adamson2db134e2009-04-03 08:27:55 +03001456static __be32
Benny Halevy347e0ad2008-07-02 11:13:41 +03001457nfsd4_decode_noop(struct nfsd4_compoundargs *argp, void *p)
1458{
1459 return nfs_ok;
1460}
1461
Benny Halevy3c375c62008-07-02 11:14:01 +03001462static __be32
1463nfsd4_decode_notsupp(struct nfsd4_compoundargs *argp, void *p)
1464{
Benny Halevy1e685ec2009-03-04 23:06:06 +02001465 return nfserr_notsupp;
Benny Halevy3c375c62008-07-02 11:14:01 +03001466}
1467
Benny Halevy347e0ad2008-07-02 11:13:41 +03001468typedef __be32(*nfsd4_dec)(struct nfsd4_compoundargs *argp, void *);
1469
1470static nfsd4_dec nfsd4_dec_ops[] = {
J. Bruce Fieldsad1060c2008-07-18 15:04:16 -04001471 [OP_ACCESS] = (nfsd4_dec)nfsd4_decode_access,
1472 [OP_CLOSE] = (nfsd4_dec)nfsd4_decode_close,
1473 [OP_COMMIT] = (nfsd4_dec)nfsd4_decode_commit,
1474 [OP_CREATE] = (nfsd4_dec)nfsd4_decode_create,
1475 [OP_DELEGPURGE] = (nfsd4_dec)nfsd4_decode_notsupp,
1476 [OP_DELEGRETURN] = (nfsd4_dec)nfsd4_decode_delegreturn,
1477 [OP_GETATTR] = (nfsd4_dec)nfsd4_decode_getattr,
1478 [OP_GETFH] = (nfsd4_dec)nfsd4_decode_noop,
1479 [OP_LINK] = (nfsd4_dec)nfsd4_decode_link,
1480 [OP_LOCK] = (nfsd4_dec)nfsd4_decode_lock,
1481 [OP_LOCKT] = (nfsd4_dec)nfsd4_decode_lockt,
1482 [OP_LOCKU] = (nfsd4_dec)nfsd4_decode_locku,
1483 [OP_LOOKUP] = (nfsd4_dec)nfsd4_decode_lookup,
1484 [OP_LOOKUPP] = (nfsd4_dec)nfsd4_decode_noop,
1485 [OP_NVERIFY] = (nfsd4_dec)nfsd4_decode_verify,
1486 [OP_OPEN] = (nfsd4_dec)nfsd4_decode_open,
1487 [OP_OPENATTR] = (nfsd4_dec)nfsd4_decode_notsupp,
1488 [OP_OPEN_CONFIRM] = (nfsd4_dec)nfsd4_decode_open_confirm,
1489 [OP_OPEN_DOWNGRADE] = (nfsd4_dec)nfsd4_decode_open_downgrade,
1490 [OP_PUTFH] = (nfsd4_dec)nfsd4_decode_putfh,
J. Bruce Fieldsa1c8c4d2009-03-09 12:17:29 -04001491 [OP_PUTPUBFH] = (nfsd4_dec)nfsd4_decode_noop,
J. Bruce Fieldsad1060c2008-07-18 15:04:16 -04001492 [OP_PUTROOTFH] = (nfsd4_dec)nfsd4_decode_noop,
1493 [OP_READ] = (nfsd4_dec)nfsd4_decode_read,
1494 [OP_READDIR] = (nfsd4_dec)nfsd4_decode_readdir,
1495 [OP_READLINK] = (nfsd4_dec)nfsd4_decode_noop,
1496 [OP_REMOVE] = (nfsd4_dec)nfsd4_decode_remove,
1497 [OP_RENAME] = (nfsd4_dec)nfsd4_decode_rename,
1498 [OP_RENEW] = (nfsd4_dec)nfsd4_decode_renew,
1499 [OP_RESTOREFH] = (nfsd4_dec)nfsd4_decode_noop,
1500 [OP_SAVEFH] = (nfsd4_dec)nfsd4_decode_noop,
1501 [OP_SECINFO] = (nfsd4_dec)nfsd4_decode_secinfo,
1502 [OP_SETATTR] = (nfsd4_dec)nfsd4_decode_setattr,
1503 [OP_SETCLIENTID] = (nfsd4_dec)nfsd4_decode_setclientid,
1504 [OP_SETCLIENTID_CONFIRM] = (nfsd4_dec)nfsd4_decode_setclientid_confirm,
1505 [OP_VERIFY] = (nfsd4_dec)nfsd4_decode_verify,
1506 [OP_WRITE] = (nfsd4_dec)nfsd4_decode_write,
1507 [OP_RELEASE_LOCKOWNER] = (nfsd4_dec)nfsd4_decode_release_lockowner,
Benny Halevy347e0ad2008-07-02 11:13:41 +03001508};
1509
Andy Adamson2db134e2009-04-03 08:27:55 +03001510static nfsd4_dec nfsd41_dec_ops[] = {
Randy Dunlap9064caa2009-04-28 16:48:25 -07001511 [OP_ACCESS] = (nfsd4_dec)nfsd4_decode_access,
1512 [OP_CLOSE] = (nfsd4_dec)nfsd4_decode_close,
1513 [OP_COMMIT] = (nfsd4_dec)nfsd4_decode_commit,
1514 [OP_CREATE] = (nfsd4_dec)nfsd4_decode_create,
1515 [OP_DELEGPURGE] = (nfsd4_dec)nfsd4_decode_notsupp,
1516 [OP_DELEGRETURN] = (nfsd4_dec)nfsd4_decode_delegreturn,
1517 [OP_GETATTR] = (nfsd4_dec)nfsd4_decode_getattr,
1518 [OP_GETFH] = (nfsd4_dec)nfsd4_decode_noop,
1519 [OP_LINK] = (nfsd4_dec)nfsd4_decode_link,
1520 [OP_LOCK] = (nfsd4_dec)nfsd4_decode_lock,
1521 [OP_LOCKT] = (nfsd4_dec)nfsd4_decode_lockt,
1522 [OP_LOCKU] = (nfsd4_dec)nfsd4_decode_locku,
1523 [OP_LOOKUP] = (nfsd4_dec)nfsd4_decode_lookup,
1524 [OP_LOOKUPP] = (nfsd4_dec)nfsd4_decode_noop,
1525 [OP_NVERIFY] = (nfsd4_dec)nfsd4_decode_verify,
1526 [OP_OPEN] = (nfsd4_dec)nfsd4_decode_open,
1527 [OP_OPENATTR] = (nfsd4_dec)nfsd4_decode_notsupp,
1528 [OP_OPEN_CONFIRM] = (nfsd4_dec)nfsd4_decode_notsupp,
1529 [OP_OPEN_DOWNGRADE] = (nfsd4_dec)nfsd4_decode_open_downgrade,
1530 [OP_PUTFH] = (nfsd4_dec)nfsd4_decode_putfh,
1531 [OP_PUTPUBFH] = (nfsd4_dec)nfsd4_decode_notsupp,
1532 [OP_PUTROOTFH] = (nfsd4_dec)nfsd4_decode_noop,
1533 [OP_READ] = (nfsd4_dec)nfsd4_decode_read,
1534 [OP_READDIR] = (nfsd4_dec)nfsd4_decode_readdir,
1535 [OP_READLINK] = (nfsd4_dec)nfsd4_decode_noop,
1536 [OP_REMOVE] = (nfsd4_dec)nfsd4_decode_remove,
1537 [OP_RENAME] = (nfsd4_dec)nfsd4_decode_rename,
1538 [OP_RENEW] = (nfsd4_dec)nfsd4_decode_notsupp,
1539 [OP_RESTOREFH] = (nfsd4_dec)nfsd4_decode_noop,
1540 [OP_SAVEFH] = (nfsd4_dec)nfsd4_decode_noop,
1541 [OP_SECINFO] = (nfsd4_dec)nfsd4_decode_secinfo,
1542 [OP_SETATTR] = (nfsd4_dec)nfsd4_decode_setattr,
1543 [OP_SETCLIENTID] = (nfsd4_dec)nfsd4_decode_notsupp,
1544 [OP_SETCLIENTID_CONFIRM]= (nfsd4_dec)nfsd4_decode_notsupp,
1545 [OP_VERIFY] = (nfsd4_dec)nfsd4_decode_verify,
1546 [OP_WRITE] = (nfsd4_dec)nfsd4_decode_write,
1547 [OP_RELEASE_LOCKOWNER] = (nfsd4_dec)nfsd4_decode_notsupp,
Andy Adamson2db134e2009-04-03 08:27:55 +03001548
1549 /* new operations for NFSv4.1 */
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04001550 [OP_BACKCHANNEL_CTL] = (nfsd4_dec)nfsd4_decode_backchannel_ctl,
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001551 [OP_BIND_CONN_TO_SESSION]= (nfsd4_dec)nfsd4_decode_bind_conn_to_session,
Randy Dunlap9064caa2009-04-28 16:48:25 -07001552 [OP_EXCHANGE_ID] = (nfsd4_dec)nfsd4_decode_exchange_id,
1553 [OP_CREATE_SESSION] = (nfsd4_dec)nfsd4_decode_create_session,
1554 [OP_DESTROY_SESSION] = (nfsd4_dec)nfsd4_decode_destroy_session,
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04001555 [OP_FREE_STATEID] = (nfsd4_dec)nfsd4_decode_free_stateid,
Randy Dunlap9064caa2009-04-28 16:48:25 -07001556 [OP_GET_DIR_DELEGATION] = (nfsd4_dec)nfsd4_decode_notsupp,
1557 [OP_GETDEVICEINFO] = (nfsd4_dec)nfsd4_decode_notsupp,
1558 [OP_GETDEVICELIST] = (nfsd4_dec)nfsd4_decode_notsupp,
1559 [OP_LAYOUTCOMMIT] = (nfsd4_dec)nfsd4_decode_notsupp,
1560 [OP_LAYOUTGET] = (nfsd4_dec)nfsd4_decode_notsupp,
1561 [OP_LAYOUTRETURN] = (nfsd4_dec)nfsd4_decode_notsupp,
J. Bruce Fields04f4ad12010-12-16 09:51:13 -05001562 [OP_SECINFO_NO_NAME] = (nfsd4_dec)nfsd4_decode_secinfo_no_name,
Randy Dunlap9064caa2009-04-28 16:48:25 -07001563 [OP_SEQUENCE] = (nfsd4_dec)nfsd4_decode_sequence,
1564 [OP_SET_SSV] = (nfsd4_dec)nfsd4_decode_notsupp,
Bryan Schumaker17456802011-07-13 10:50:48 -04001565 [OP_TEST_STATEID] = (nfsd4_dec)nfsd4_decode_test_stateid,
Randy Dunlap9064caa2009-04-28 16:48:25 -07001566 [OP_WANT_DELEGATION] = (nfsd4_dec)nfsd4_decode_notsupp,
Mi Jinlong345c2842011-10-20 17:51:39 +08001567 [OP_DESTROY_CLIENTID] = (nfsd4_dec)nfsd4_decode_destroy_clientid,
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04001568 [OP_RECLAIM_COMPLETE] = (nfsd4_dec)nfsd4_decode_reclaim_complete,
Andy Adamson2db134e2009-04-03 08:27:55 +03001569};
1570
Benny Halevyf2feb962008-07-02 11:14:22 +03001571struct nfsd4_minorversion_ops {
1572 nfsd4_dec *decoders;
1573 int nops;
1574};
1575
1576static struct nfsd4_minorversion_ops nfsd4_minorversion[] = {
J. Bruce Fieldsad1060c2008-07-18 15:04:16 -04001577 [0] = { nfsd4_dec_ops, ARRAY_SIZE(nfsd4_dec_ops) },
Andy Adamson2db134e2009-04-03 08:27:55 +03001578 [1] = { nfsd41_dec_ops, ARRAY_SIZE(nfsd41_dec_ops) },
Benny Halevyf2feb962008-07-02 11:14:22 +03001579};
1580
Benny Halevy347e0ad2008-07-02 11:13:41 +03001581static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582nfsd4_decode_compound(struct nfsd4_compoundargs *argp)
1583{
1584 DECODE_HEAD;
1585 struct nfsd4_op *op;
Benny Halevyf2feb962008-07-02 11:14:22 +03001586 struct nfsd4_minorversion_ops *ops;
J. Bruce Fields10910062011-01-24 12:11:02 -05001587 bool cachethis = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 int i;
1589
1590 /*
1591 * XXX: According to spec, we should check the tag
1592 * for UTF-8 compliance. I'm postponing this for
1593 * now because it seems that some clients do use
1594 * binary tags.
1595 */
1596 READ_BUF(4);
1597 READ32(argp->taglen);
1598 READ_BUF(argp->taglen + 8);
1599 SAVEMEM(argp->tag, argp->taglen);
1600 READ32(argp->minorversion);
1601 READ32(argp->opcnt);
1602
1603 if (argp->taglen > NFSD4_MAX_TAGLEN)
1604 goto xdr_error;
1605 if (argp->opcnt > 100)
1606 goto xdr_error;
1607
Tobias Klausere8c96f82006-03-24 03:15:34 -08001608 if (argp->opcnt > ARRAY_SIZE(argp->iops)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 argp->ops = kmalloc(argp->opcnt * sizeof(*argp->ops), GFP_KERNEL);
1610 if (!argp->ops) {
1611 argp->ops = argp->iops;
Chuck Lever817cb9d2007-09-11 18:01:20 -04001612 dprintk("nfsd: couldn't allocate room for COMPOUND\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 goto xdr_error;
1614 }
1615 }
1616
Benny Halevyf2feb962008-07-02 11:14:22 +03001617 if (argp->minorversion >= ARRAY_SIZE(nfsd4_minorversion))
Benny Halevy30cff1f2008-07-02 11:13:18 +03001618 argp->opcnt = 0;
1619
Benny Halevyf2feb962008-07-02 11:14:22 +03001620 ops = &nfsd4_minorversion[argp->minorversion];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 for (i = 0; i < argp->opcnt; i++) {
1622 op = &argp->ops[i];
1623 op->replay = NULL;
1624
1625 /*
1626 * We can't use READ_BUF() here because we need to handle
1627 * a missing opcode as an OP_WRITE + 1. So we need to check
1628 * to see if we're truly at the end of our buffer or if there
1629 * is another page we need to flip to.
1630 */
1631
1632 if (argp->p == argp->end) {
1633 if (argp->pagelen < 4) {
1634 /* There isn't an opcode still on the wire */
1635 op->opnum = OP_WRITE + 1;
1636 op->status = nfserr_bad_xdr;
1637 argp->opcnt = i+1;
1638 break;
1639 }
1640
1641 /*
1642 * False alarm. We just hit a page boundary, but there
1643 * is still data available. Move pointer across page
1644 * boundary. *snip from READ_BUF*
1645 */
1646 argp->p = page_address(argp->pagelist[0]);
1647 argp->pagelist++;
1648 if (argp->pagelen < PAGE_SIZE) {
Neil Brown2bc3c112010-04-20 12:16:52 +10001649 argp->end = argp->p + (argp->pagelen>>2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 argp->pagelen = 0;
1651 } else {
Neil Brown2bc3c112010-04-20 12:16:52 +10001652 argp->end = argp->p + (PAGE_SIZE>>2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 argp->pagelen -= PAGE_SIZE;
1654 }
1655 }
1656 op->opnum = ntohl(*argp->p++);
1657
Ricardo Labiagade3cab72009-12-11 20:03:27 -08001658 if (op->opnum >= FIRST_NFS4_OP && op->opnum <= LAST_NFS4_OP)
Benny Halevyf2feb962008-07-02 11:14:22 +03001659 op->status = ops->decoders[op->opnum](argp, &op->u);
Benny Halevy347e0ad2008-07-02 11:13:41 +03001660 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 op->opnum = OP_ILLEGAL;
1662 op->status = nfserr_op_illegal;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 }
1664
1665 if (op->status) {
1666 argp->opcnt = i+1;
1667 break;
1668 }
J. Bruce Fields10910062011-01-24 12:11:02 -05001669 /*
1670 * We'll try to cache the result in the DRC if any one
1671 * op in the compound wants to be cached:
1672 */
1673 cachethis |= nfsd4_cache_this_op(op);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 }
J. Bruce Fields10910062011-01-24 12:11:02 -05001675 /* Sessions make the DRC unnecessary: */
1676 if (argp->minorversion)
1677 cachethis = false;
1678 argp->rqstp->rq_cachetype = cachethis ? RC_REPLBUFF : RC_NOCACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679
1680 DECODE_TAIL;
1681}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683#define WRITE32(n) *p++ = htonl(n)
1684#define WRITE64(n) do { \
1685 *p++ = htonl((u32)((n) >> 32)); \
1686 *p++ = htonl((u32)(n)); \
1687} while (0)
Harvey Harrison5108b272008-07-17 21:33:04 -07001688#define WRITEMEM(ptr,nbytes) do { if (nbytes > 0) { \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 *(p + XDR_QUADLEN(nbytes) -1) = 0; \
1690 memcpy(p, ptr, nbytes); \
1691 p += XDR_QUADLEN(nbytes); \
Harvey Harrison5108b272008-07-17 21:33:04 -07001692}} while (0)
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04001693
1694static void write32(__be32 **p, u32 n)
1695{
J. Bruce Fields45eaa1c2012-04-25 18:11:04 -04001696 *(*p)++ = htonl(n);
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04001697}
1698
1699static void write64(__be32 **p, u64 n)
1700{
J. Bruce Fields45eaa1c2012-04-25 18:11:04 -04001701 write32(p, (n >> 32));
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04001702 write32(p, (u32)n);
1703}
1704
1705static void write_change(__be32 **p, struct kstat *stat, struct inode *inode)
1706{
1707 if (IS_I_VERSION(inode)) {
1708 write64(p, inode->i_version);
1709 } else {
1710 write32(p, stat->ctime.tv_sec);
1711 write32(p, stat->ctime.tv_nsec);
1712 }
1713}
1714
1715static void write_cinfo(__be32 **p, struct nfsd4_change_info *c)
1716{
1717 write32(p, c->atomic);
1718 if (c->change_supported) {
1719 write64(p, c->before_change);
1720 write64(p, c->after_change);
1721 } else {
1722 write32(p, c->before_ctime_sec);
1723 write32(p, c->before_ctime_nsec);
1724 write32(p, c->after_ctime_sec);
1725 write32(p, c->after_ctime_nsec);
1726 }
1727}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728
1729#define RESERVE_SPACE(nbytes) do { \
1730 p = resp->p; \
1731 BUG_ON(p + XDR_QUADLEN(nbytes) > resp->end); \
1732} while (0)
1733#define ADJUST_ARGS() resp->p = p
1734
1735/*
1736 * Header routine to setup seqid operation replay cache
1737 */
1738#define ENCODE_SEQID_OP_HEAD \
Al Viro2ebbc012006-10-19 23:28:58 -07001739 __be32 *save; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 \
1741 save = resp->p;
1742
1743/*
NeilBrown7fb64ce2005-07-07 17:59:20 -07001744 * Routine for encoding the result of a "seqid-mutating" NFSv4 operation. This
1745 * is where sequence id's are incremented, and the replay cache is filled.
1746 * Note that we increment sequence id's here, at the last moment, so we're sure
1747 * we know whether the error to be returned is a sequence id mutating error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 */
1749
J. Bruce Fieldsf3e42232011-08-24 12:27:31 -04001750static void encode_seqid_op_tail(struct nfsd4_compoundres *resp, __be32 *save, __be32 nfserr)
1751{
1752 struct nfs4_stateowner *stateowner = resp->cstate.replay_owner;
1753
1754 if (seqid_mutating_err(ntohl(nfserr)) && stateowner) {
1755 stateowner->so_seqid++;
1756 stateowner->so_replay.rp_status = nfserr;
1757 stateowner->so_replay.rp_buflen =
1758 (char *)resp->p - (char *)save;
1759 memcpy(stateowner->so_replay.rp_buf, save,
1760 stateowner->so_replay.rp_buflen);
J. Bruce Fields38c387b2011-09-16 17:42:48 -04001761 nfsd4_purge_closed_stateid(stateowner);
J. Bruce Fieldsf3e42232011-08-24 12:27:31 -04001762 }
1763}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001765/* Encode as an array of strings the string given with components
Weston Andros Adamsone7a04442012-04-24 11:07:59 -04001766 * separated @sep, escaped with esc_enter and esc_exit.
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001767 */
Weston Andros Adamsone7a04442012-04-24 11:07:59 -04001768static __be32 nfsd4_encode_components_esc(char sep, char *components,
1769 __be32 **pp, int *buflen,
1770 char esc_enter, char esc_exit)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001771{
Al Viro2ebbc012006-10-19 23:28:58 -07001772 __be32 *p = *pp;
1773 __be32 *countp = p;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001774 int strlen, count=0;
Weston Andros Adamsone7a04442012-04-24 11:07:59 -04001775 char *str, *end, *next;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001776
1777 dprintk("nfsd4_encode_components(%s)\n", components);
1778 if ((*buflen -= 4) < 0)
1779 return nfserr_resource;
1780 WRITE32(0); /* We will fill this in with @count later */
1781 end = str = components;
1782 while (*end) {
Weston Andros Adamsone7a04442012-04-24 11:07:59 -04001783 bool found_esc = false;
1784
1785 /* try to parse as esc_start, ..., esc_end, sep */
1786 if (*str == esc_enter) {
1787 for (; *end && (*end != esc_exit); end++)
1788 /* find esc_exit or end of string */;
1789 next = end + 1;
1790 if (*end && (!*next || *next == sep)) {
1791 str++;
1792 found_esc = true;
1793 }
1794 }
1795
1796 if (!found_esc)
1797 for (; *end && (*end != sep); end++)
1798 /* find sep or end of string */;
1799
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001800 strlen = end - str;
1801 if (strlen) {
1802 if ((*buflen -= ((XDR_QUADLEN(strlen) << 2) + 4)) < 0)
1803 return nfserr_resource;
1804 WRITE32(strlen);
1805 WRITEMEM(str, strlen);
1806 count++;
1807 }
1808 else
1809 end++;
1810 str = end;
1811 }
1812 *pp = p;
1813 p = countp;
1814 WRITE32(count);
1815 return 0;
1816}
1817
Weston Andros Adamsone7a04442012-04-24 11:07:59 -04001818/* Encode as an array of strings the string given with components
1819 * separated @sep.
1820 */
1821static __be32 nfsd4_encode_components(char sep, char *components,
1822 __be32 **pp, int *buflen)
1823{
1824 return nfsd4_encode_components_esc(sep, components, pp, buflen, 0, 0);
1825}
1826
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001827/*
1828 * encode a location element of a fs_locations structure
1829 */
Al Virob37ad282006-10-19 23:28:59 -07001830static __be32 nfsd4_encode_fs_location4(struct nfsd4_fs_location *location,
Al Viro2ebbc012006-10-19 23:28:58 -07001831 __be32 **pp, int *buflen)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001832{
Al Virob37ad282006-10-19 23:28:59 -07001833 __be32 status;
Al Viro2ebbc012006-10-19 23:28:58 -07001834 __be32 *p = *pp;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001835
Weston Andros Adamsone7a04442012-04-24 11:07:59 -04001836 status = nfsd4_encode_components_esc(':', location->hosts, &p, buflen,
1837 '[', ']');
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001838 if (status)
1839 return status;
1840 status = nfsd4_encode_components('/', location->path, &p, buflen);
1841 if (status)
1842 return status;
1843 *pp = p;
1844 return 0;
1845}
1846
1847/*
Trond Myklebusted748aa2011-09-12 19:37:06 -04001848 * Encode a path in RFC3530 'pathname4' format
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001849 */
Trond Myklebusted748aa2011-09-12 19:37:06 -04001850static __be32 nfsd4_encode_path(const struct path *root,
1851 const struct path *path, __be32 **pp, int *buflen)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001852{
Trond Myklebusted748aa2011-09-12 19:37:06 -04001853 struct path cur = {
1854 .mnt = path->mnt,
1855 .dentry = path->dentry,
1856 };
1857 __be32 *p = *pp;
1858 struct dentry **components = NULL;
1859 unsigned int ncomponents = 0;
1860 __be32 err = nfserr_jukebox;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001861
Trond Myklebusted748aa2011-09-12 19:37:06 -04001862 dprintk("nfsd4_encode_components(");
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001863
Trond Myklebusted748aa2011-09-12 19:37:06 -04001864 path_get(&cur);
1865 /* First walk the path up to the nfsd root, and store the
1866 * dentries/path components in an array.
1867 */
1868 for (;;) {
1869 if (cur.dentry == root->dentry && cur.mnt == root->mnt)
1870 break;
1871 if (cur.dentry == cur.mnt->mnt_root) {
1872 if (follow_up(&cur))
1873 continue;
1874 goto out_free;
1875 }
1876 if ((ncomponents & 15) == 0) {
1877 struct dentry **new;
1878 new = krealloc(components,
1879 sizeof(*new) * (ncomponents + 16),
1880 GFP_KERNEL);
1881 if (!new)
1882 goto out_free;
1883 components = new;
1884 }
1885 components[ncomponents++] = cur.dentry;
1886 cur.dentry = dget_parent(cur.dentry);
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001887 }
Trond Myklebusted748aa2011-09-12 19:37:06 -04001888
1889 *buflen -= 4;
1890 if (*buflen < 0)
1891 goto out_free;
1892 WRITE32(ncomponents);
1893
1894 while (ncomponents) {
1895 struct dentry *dentry = components[ncomponents - 1];
1896 unsigned int len = dentry->d_name.len;
1897
1898 *buflen -= 4 + (XDR_QUADLEN(len) << 2);
1899 if (*buflen < 0)
1900 goto out_free;
1901 WRITE32(len);
1902 WRITEMEM(dentry->d_name.name, len);
1903 dprintk("/%s", dentry->d_name.name);
1904 dput(dentry);
1905 ncomponents--;
1906 }
1907
1908 *pp = p;
1909 err = 0;
1910out_free:
1911 dprintk(")\n");
1912 while (ncomponents)
1913 dput(components[--ncomponents]);
1914 kfree(components);
1915 path_put(&cur);
1916 return err;
1917}
1918
1919static __be32 nfsd4_encode_fsloc_fsroot(struct svc_rqst *rqstp,
1920 const struct path *path, __be32 **pp, int *buflen)
1921{
1922 struct svc_export *exp_ps;
1923 __be32 res;
1924
1925 exp_ps = rqst_find_fsidzero_export(rqstp);
1926 if (IS_ERR(exp_ps))
1927 return nfserrno(PTR_ERR(exp_ps));
1928 res = nfsd4_encode_path(&exp_ps->ex_path, path, pp, buflen);
1929 exp_put(exp_ps);
1930 return res;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001931}
1932
1933/*
1934 * encode a fs_locations structure
1935 */
Al Virob37ad282006-10-19 23:28:59 -07001936static __be32 nfsd4_encode_fs_locations(struct svc_rqst *rqstp,
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001937 struct svc_export *exp,
Al Viro2ebbc012006-10-19 23:28:58 -07001938 __be32 **pp, int *buflen)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001939{
Al Virob37ad282006-10-19 23:28:59 -07001940 __be32 status;
Al Virocc45f012006-10-19 23:28:44 -07001941 int i;
Al Viro2ebbc012006-10-19 23:28:58 -07001942 __be32 *p = *pp;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001943 struct nfsd4_fs_locations *fslocs = &exp->ex_fslocs;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001944
Trond Myklebusted748aa2011-09-12 19:37:06 -04001945 status = nfsd4_encode_fsloc_fsroot(rqstp, &exp->ex_path, &p, buflen);
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001946 if (status)
1947 return status;
1948 if ((*buflen -= 4) < 0)
1949 return nfserr_resource;
1950 WRITE32(fslocs->locations_count);
1951 for (i=0; i<fslocs->locations_count; i++) {
1952 status = nfsd4_encode_fs_location4(&fslocs->locations[i],
1953 &p, buflen);
1954 if (status)
1955 return status;
1956 }
1957 *pp = p;
1958 return 0;
1959}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960
J. Bruce Fields3d2544b2011-08-15 11:49:30 -04001961static u32 nfs4_file_type(umode_t mode)
1962{
1963 switch (mode & S_IFMT) {
1964 case S_IFIFO: return NF4FIFO;
1965 case S_IFCHR: return NF4CHR;
1966 case S_IFDIR: return NF4DIR;
1967 case S_IFBLK: return NF4BLK;
1968 case S_IFLNK: return NF4LNK;
1969 case S_IFREG: return NF4REG;
1970 case S_IFSOCK: return NF4SOCK;
1971 default: return NF4BAD;
1972 };
1973}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974
Al Virob37ad282006-10-19 23:28:59 -07001975static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976nfsd4_encode_name(struct svc_rqst *rqstp, int whotype, uid_t id, int group,
Al Viro2ebbc012006-10-19 23:28:58 -07001977 __be32 **p, int *buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978{
1979 int status;
1980
1981 if (*buflen < (XDR_QUADLEN(IDMAP_NAMESZ) << 2) + 4)
1982 return nfserr_resource;
1983 if (whotype != NFS4_ACL_WHO_NAMED)
1984 status = nfs4_acl_write_who(whotype, (u8 *)(*p + 1));
1985 else if (group)
1986 status = nfsd_map_gid_to_name(rqstp, id, (u8 *)(*p + 1));
1987 else
1988 status = nfsd_map_uid_to_name(rqstp, id, (u8 *)(*p + 1));
1989 if (status < 0)
1990 return nfserrno(status);
1991 *p = xdr_encode_opaque(*p, NULL, status);
1992 *buflen -= (XDR_QUADLEN(status) << 2) + 4;
1993 BUG_ON(*buflen < 0);
1994 return 0;
1995}
1996
Al Virob37ad282006-10-19 23:28:59 -07001997static inline __be32
Al Viro2ebbc012006-10-19 23:28:58 -07001998nfsd4_encode_user(struct svc_rqst *rqstp, uid_t uid, __be32 **p, int *buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999{
2000 return nfsd4_encode_name(rqstp, NFS4_ACL_WHO_NAMED, uid, 0, p, buflen);
2001}
2002
Al Virob37ad282006-10-19 23:28:59 -07002003static inline __be32
Al Viro2ebbc012006-10-19 23:28:58 -07002004nfsd4_encode_group(struct svc_rqst *rqstp, uid_t gid, __be32 **p, int *buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005{
2006 return nfsd4_encode_name(rqstp, NFS4_ACL_WHO_NAMED, gid, 1, p, buflen);
2007}
2008
Al Virob37ad282006-10-19 23:28:59 -07002009static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010nfsd4_encode_aclname(struct svc_rqst *rqstp, int whotype, uid_t id, int group,
Al Viro2ebbc012006-10-19 23:28:58 -07002011 __be32 **p, int *buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012{
2013 return nfsd4_encode_name(rqstp, whotype, id, group, p, buflen);
2014}
2015
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002016#define WORD0_ABSENT_FS_ATTRS (FATTR4_WORD0_FS_LOCATIONS | FATTR4_WORD0_FSID | \
2017 FATTR4_WORD0_RDATTR_ERROR)
2018#define WORD1_ABSENT_FS_ATTRS FATTR4_WORD1_MOUNTED_ON_FILEID
2019
Al Virob37ad282006-10-19 23:28:59 -07002020static __be32 fattr_handle_absent_fs(u32 *bmval0, u32 *bmval1, u32 *rdattr_err)
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002021{
2022 /* As per referral draft: */
2023 if (*bmval0 & ~WORD0_ABSENT_FS_ATTRS ||
2024 *bmval1 & ~WORD1_ABSENT_FS_ATTRS) {
2025 if (*bmval0 & FATTR4_WORD0_RDATTR_ERROR ||
2026 *bmval0 & FATTR4_WORD0_FS_LOCATIONS)
2027 *rdattr_err = NFSERR_MOVED;
2028 else
2029 return nfserr_moved;
2030 }
2031 *bmval0 &= WORD0_ABSENT_FS_ATTRS;
2032 *bmval1 &= WORD1_ABSENT_FS_ATTRS;
2033 return 0;
2034}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035
J. Bruce Fieldsae7095a2012-10-01 17:50:56 -04002036
2037static int get_parent_attributes(struct svc_export *exp, struct kstat *stat)
2038{
2039 struct path path = exp->ex_path;
2040 int err;
2041
2042 path_get(&path);
2043 while (follow_up(&path)) {
2044 if (path.dentry != path.mnt->mnt_root)
2045 break;
2046 }
2047 err = vfs_getattr(path.mnt, path.dentry, stat);
2048 path_put(&path);
2049 return err;
2050}
2051
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052/*
2053 * Note: @fhp can be NULL; in this case, we might have to compose the filehandle
2054 * ourselves.
2055 *
2056 * @countp is the buffer size in _words_; upon successful return this becomes
2057 * replaced with the number of words written.
2058 */
Al Virob37ad282006-10-19 23:28:59 -07002059__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060nfsd4_encode_fattr(struct svc_fh *fhp, struct svc_export *exp,
Al Viro2ebbc012006-10-19 23:28:58 -07002061 struct dentry *dentry, __be32 *buffer, int *countp, u32 *bmval,
Frank Filz406a7ea2007-11-27 11:34:05 -08002062 struct svc_rqst *rqstp, int ignore_crossmnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063{
2064 u32 bmval0 = bmval[0];
2065 u32 bmval1 = bmval[1];
Andy Adamson7e705702009-04-03 08:29:11 +03002066 u32 bmval2 = bmval[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 struct kstat stat;
2068 struct svc_fh tempfh;
2069 struct kstatfs statfs;
2070 int buflen = *countp << 2;
Al Viro2ebbc012006-10-19 23:28:58 -07002071 __be32 *attrlenp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 u32 dummy;
2073 u64 dummy64;
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002074 u32 rdattr_err = 0;
Al Viro2ebbc012006-10-19 23:28:58 -07002075 __be32 *p = buffer;
Al Virob37ad282006-10-19 23:28:59 -07002076 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07002077 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 int aclsupport = 0;
2079 struct nfs4_acl *acl = NULL;
Andy Adamson7e705702009-04-03 08:29:11 +03002080 struct nfsd4_compoundres *resp = rqstp->rq_resp;
2081 u32 minorversion = resp->cstate.minorversion;
Christoph Hellwigebabe9a2010-07-07 18:53:11 +02002082 struct path path = {
2083 .mnt = exp->ex_path.mnt,
2084 .dentry = dentry,
2085 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086
2087 BUG_ON(bmval1 & NFSD_WRITEONLY_ATTRS_WORD1);
Andy Adamson7e705702009-04-03 08:29:11 +03002088 BUG_ON(bmval0 & ~nfsd_suppattrs0(minorversion));
2089 BUG_ON(bmval1 & ~nfsd_suppattrs1(minorversion));
2090 BUG_ON(bmval2 & ~nfsd_suppattrs2(minorversion));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002092 if (exp->ex_fslocs.migrated) {
Andy Adamson7e705702009-04-03 08:29:11 +03002093 BUG_ON(bmval[2]);
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002094 status = fattr_handle_absent_fs(&bmval0, &bmval1, &rdattr_err);
2095 if (status)
2096 goto out;
2097 }
2098
Jan Blunck54775492008-02-14 19:38:39 -08002099 err = vfs_getattr(exp->ex_path.mnt, dentry, &stat);
Al Virob8dd7b92006-10-19 23:29:01 -07002100 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 goto out_nfserr;
J. Bruce Fieldsa16e92e2007-09-28 16:45:51 -04002102 if ((bmval0 & (FATTR4_WORD0_FILES_FREE | FATTR4_WORD0_FILES_TOTAL |
2103 FATTR4_WORD0_MAXNAME)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 (bmval1 & (FATTR4_WORD1_SPACE_AVAIL | FATTR4_WORD1_SPACE_FREE |
2105 FATTR4_WORD1_SPACE_TOTAL))) {
Christoph Hellwigebabe9a2010-07-07 18:53:11 +02002106 err = vfs_statfs(&path, &statfs);
Al Virob8dd7b92006-10-19 23:29:01 -07002107 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 goto out_nfserr;
2109 }
2110 if ((bmval0 & (FATTR4_WORD0_FILEHANDLE | FATTR4_WORD0_FSID)) && !fhp) {
2111 fh_init(&tempfh, NFS4_FHSIZE);
2112 status = fh_compose(&tempfh, exp, dentry, NULL);
2113 if (status)
2114 goto out;
2115 fhp = &tempfh;
2116 }
2117 if (bmval0 & (FATTR4_WORD0_ACL | FATTR4_WORD0_ACLSUPPORT
2118 | FATTR4_WORD0_SUPPORTED_ATTRS)) {
Al Virob8dd7b92006-10-19 23:29:01 -07002119 err = nfsd4_get_nfs4_acl(rqstp, dentry, &acl);
2120 aclsupport = (err == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 if (bmval0 & FATTR4_WORD0_ACL) {
Al Virob8dd7b92006-10-19 23:29:01 -07002122 if (err == -EOPNOTSUPP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 bmval0 &= ~FATTR4_WORD0_ACL;
Al Virob8dd7b92006-10-19 23:29:01 -07002124 else if (err == -EINVAL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 status = nfserr_attrnotsupp;
2126 goto out;
Al Virob8dd7b92006-10-19 23:29:01 -07002127 } else if (err != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 goto out_nfserr;
2129 }
2130 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131
Benny Halevy2b44f1b2010-09-30 20:47:46 +02002132 if (bmval2) {
2133 if ((buflen -= 16) < 0)
2134 goto out_resource;
Andy Adamson7e705702009-04-03 08:29:11 +03002135 WRITE32(3);
2136 WRITE32(bmval0);
2137 WRITE32(bmval1);
2138 WRITE32(bmval2);
Benny Halevy2b44f1b2010-09-30 20:47:46 +02002139 } else if (bmval1) {
2140 if ((buflen -= 12) < 0)
2141 goto out_resource;
Andy Adamson7e705702009-04-03 08:29:11 +03002142 WRITE32(2);
2143 WRITE32(bmval0);
2144 WRITE32(bmval1);
2145 } else {
Benny Halevy2b44f1b2010-09-30 20:47:46 +02002146 if ((buflen -= 8) < 0)
2147 goto out_resource;
Andy Adamson7e705702009-04-03 08:29:11 +03002148 WRITE32(1);
2149 WRITE32(bmval0);
2150 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 attrlenp = p++; /* to be backfilled later */
2152
2153 if (bmval0 & FATTR4_WORD0_SUPPORTED_ATTRS) {
Andy Adamson7e705702009-04-03 08:29:11 +03002154 u32 word0 = nfsd_suppattrs0(minorversion);
2155 u32 word1 = nfsd_suppattrs1(minorversion);
2156 u32 word2 = nfsd_suppattrs2(minorversion);
2157
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002158 if (!aclsupport)
2159 word0 &= ~FATTR4_WORD0_ACL;
Andy Adamson7e705702009-04-03 08:29:11 +03002160 if (!word2) {
Benny Halevy2b44f1b2010-09-30 20:47:46 +02002161 if ((buflen -= 12) < 0)
2162 goto out_resource;
Andy Adamson7e705702009-04-03 08:29:11 +03002163 WRITE32(2);
2164 WRITE32(word0);
2165 WRITE32(word1);
2166 } else {
Benny Halevy2b44f1b2010-09-30 20:47:46 +02002167 if ((buflen -= 16) < 0)
2168 goto out_resource;
Andy Adamson7e705702009-04-03 08:29:11 +03002169 WRITE32(3);
2170 WRITE32(word0);
2171 WRITE32(word1);
2172 WRITE32(word2);
2173 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 }
2175 if (bmval0 & FATTR4_WORD0_TYPE) {
2176 if ((buflen -= 4) < 0)
2177 goto out_resource;
J. Bruce Fields3d2544b2011-08-15 11:49:30 -04002178 dummy = nfs4_file_type(stat.mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 if (dummy == NF4BAD)
2180 goto out_serverfault;
2181 WRITE32(dummy);
2182 }
2183 if (bmval0 & FATTR4_WORD0_FH_EXPIRE_TYPE) {
2184 if ((buflen -= 4) < 0)
2185 goto out_resource;
NeilBrown49640002005-06-23 22:02:58 -07002186 if (exp->ex_flags & NFSEXP_NOSUBTREECHECK)
NeilBrowne34ac862005-07-07 17:59:30 -07002187 WRITE32(NFS4_FH_PERSISTENT);
NeilBrown49640002005-06-23 22:02:58 -07002188 else
NeilBrowne34ac862005-07-07 17:59:30 -07002189 WRITE32(NFS4_FH_PERSISTENT|NFS4_FH_VOL_RENAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 }
2191 if (bmval0 & FATTR4_WORD0_CHANGE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 if ((buflen -= 8) < 0)
2193 goto out_resource;
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002194 write_change(&p, &stat, dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 }
2196 if (bmval0 & FATTR4_WORD0_SIZE) {
2197 if ((buflen -= 8) < 0)
2198 goto out_resource;
2199 WRITE64(stat.size);
2200 }
2201 if (bmval0 & FATTR4_WORD0_LINK_SUPPORT) {
2202 if ((buflen -= 4) < 0)
2203 goto out_resource;
2204 WRITE32(1);
2205 }
2206 if (bmval0 & FATTR4_WORD0_SYMLINK_SUPPORT) {
2207 if ((buflen -= 4) < 0)
2208 goto out_resource;
2209 WRITE32(1);
2210 }
2211 if (bmval0 & FATTR4_WORD0_NAMED_ATTR) {
2212 if ((buflen -= 4) < 0)
2213 goto out_resource;
2214 WRITE32(0);
2215 }
2216 if (bmval0 & FATTR4_WORD0_FSID) {
2217 if ((buflen -= 16) < 0)
2218 goto out_resource;
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002219 if (exp->ex_fslocs.migrated) {
2220 WRITE64(NFS4_REFERRAL_FSID_MAJOR);
2221 WRITE64(NFS4_REFERRAL_FSID_MINOR);
NeilBrownaf6a4e22007-02-14 00:33:12 -08002222 } else switch(fsid_source(fhp)) {
2223 case FSIDSOURCE_FSID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 WRITE64((u64)exp->ex_fsid);
2225 WRITE64((u64)0);
NeilBrownaf6a4e22007-02-14 00:33:12 -08002226 break;
2227 case FSIDSOURCE_DEV:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 WRITE32(0);
2229 WRITE32(MAJOR(stat.dev));
2230 WRITE32(0);
2231 WRITE32(MINOR(stat.dev));
NeilBrownaf6a4e22007-02-14 00:33:12 -08002232 break;
2233 case FSIDSOURCE_UUID:
2234 WRITEMEM(exp->ex_uuid, 16);
2235 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 }
2237 }
2238 if (bmval0 & FATTR4_WORD0_UNIQUE_HANDLES) {
2239 if ((buflen -= 4) < 0)
2240 goto out_resource;
2241 WRITE32(0);
2242 }
2243 if (bmval0 & FATTR4_WORD0_LEASE_TIME) {
2244 if ((buflen -= 4) < 0)
2245 goto out_resource;
J. Bruce Fieldscf07d2e2010-02-28 23:20:19 -05002246 WRITE32(nfsd4_lease);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 }
2248 if (bmval0 & FATTR4_WORD0_RDATTR_ERROR) {
2249 if ((buflen -= 4) < 0)
2250 goto out_resource;
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002251 WRITE32(rdattr_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 }
2253 if (bmval0 & FATTR4_WORD0_ACL) {
2254 struct nfs4_ace *ace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255
2256 if (acl == NULL) {
2257 if ((buflen -= 4) < 0)
2258 goto out_resource;
2259
2260 WRITE32(0);
2261 goto out_acl;
2262 }
2263 if ((buflen -= 4) < 0)
2264 goto out_resource;
2265 WRITE32(acl->naces);
2266
J. Bruce Fields28e05dd2007-02-16 01:28:30 -08002267 for (ace = acl->aces; ace < acl->aces + acl->naces; ace++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 if ((buflen -= 4*3) < 0)
2269 goto out_resource;
2270 WRITE32(ace->type);
2271 WRITE32(ace->flag);
2272 WRITE32(ace->access_mask & NFS4_ACE_MASK_ALL);
2273 status = nfsd4_encode_aclname(rqstp, ace->whotype,
2274 ace->who, ace->flag & NFS4_ACE_IDENTIFIER_GROUP,
2275 &p, &buflen);
2276 if (status == nfserr_resource)
2277 goto out_resource;
2278 if (status)
2279 goto out;
2280 }
2281 }
2282out_acl:
2283 if (bmval0 & FATTR4_WORD0_ACLSUPPORT) {
2284 if ((buflen -= 4) < 0)
2285 goto out_resource;
2286 WRITE32(aclsupport ?
2287 ACL4_SUPPORT_ALLOW_ACL|ACL4_SUPPORT_DENY_ACL : 0);
2288 }
2289 if (bmval0 & FATTR4_WORD0_CANSETTIME) {
2290 if ((buflen -= 4) < 0)
2291 goto out_resource;
2292 WRITE32(1);
2293 }
2294 if (bmval0 & FATTR4_WORD0_CASE_INSENSITIVE) {
2295 if ((buflen -= 4) < 0)
2296 goto out_resource;
J. Bruce Fields2930d382012-06-05 16:52:06 -04002297 WRITE32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 }
2299 if (bmval0 & FATTR4_WORD0_CASE_PRESERVING) {
2300 if ((buflen -= 4) < 0)
2301 goto out_resource;
2302 WRITE32(1);
2303 }
2304 if (bmval0 & FATTR4_WORD0_CHOWN_RESTRICTED) {
2305 if ((buflen -= 4) < 0)
2306 goto out_resource;
2307 WRITE32(1);
2308 }
2309 if (bmval0 & FATTR4_WORD0_FILEHANDLE) {
2310 buflen -= (XDR_QUADLEN(fhp->fh_handle.fh_size) << 2) + 4;
2311 if (buflen < 0)
2312 goto out_resource;
2313 WRITE32(fhp->fh_handle.fh_size);
2314 WRITEMEM(&fhp->fh_handle.fh_base, fhp->fh_handle.fh_size);
2315 }
2316 if (bmval0 & FATTR4_WORD0_FILEID) {
2317 if ((buflen -= 8) < 0)
2318 goto out_resource;
Peter Staubach40ee5dc2007-08-16 12:10:07 -04002319 WRITE64(stat.ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 }
2321 if (bmval0 & FATTR4_WORD0_FILES_AVAIL) {
2322 if ((buflen -= 8) < 0)
2323 goto out_resource;
2324 WRITE64((u64) statfs.f_ffree);
2325 }
2326 if (bmval0 & FATTR4_WORD0_FILES_FREE) {
2327 if ((buflen -= 8) < 0)
2328 goto out_resource;
2329 WRITE64((u64) statfs.f_ffree);
2330 }
2331 if (bmval0 & FATTR4_WORD0_FILES_TOTAL) {
2332 if ((buflen -= 8) < 0)
2333 goto out_resource;
2334 WRITE64((u64) statfs.f_files);
2335 }
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002336 if (bmval0 & FATTR4_WORD0_FS_LOCATIONS) {
2337 status = nfsd4_encode_fs_locations(rqstp, exp, &p, &buflen);
2338 if (status == nfserr_resource)
2339 goto out_resource;
2340 if (status)
2341 goto out;
2342 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 if (bmval0 & FATTR4_WORD0_HOMOGENEOUS) {
2344 if ((buflen -= 4) < 0)
2345 goto out_resource;
2346 WRITE32(1);
2347 }
2348 if (bmval0 & FATTR4_WORD0_MAXFILESIZE) {
2349 if ((buflen -= 8) < 0)
2350 goto out_resource;
2351 WRITE64(~(u64)0);
2352 }
2353 if (bmval0 & FATTR4_WORD0_MAXLINK) {
2354 if ((buflen -= 4) < 0)
2355 goto out_resource;
2356 WRITE32(255);
2357 }
2358 if (bmval0 & FATTR4_WORD0_MAXNAME) {
2359 if ((buflen -= 4) < 0)
2360 goto out_resource;
J. Bruce Fieldsa16e92e2007-09-28 16:45:51 -04002361 WRITE32(statfs.f_namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 }
2363 if (bmval0 & FATTR4_WORD0_MAXREAD) {
2364 if ((buflen -= 8) < 0)
2365 goto out_resource;
Greg Banks7adae482006-10-04 02:15:47 -07002366 WRITE64((u64) svc_max_payload(rqstp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 }
2368 if (bmval0 & FATTR4_WORD0_MAXWRITE) {
2369 if ((buflen -= 8) < 0)
2370 goto out_resource;
Greg Banks7adae482006-10-04 02:15:47 -07002371 WRITE64((u64) svc_max_payload(rqstp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 }
2373 if (bmval1 & FATTR4_WORD1_MODE) {
2374 if ((buflen -= 4) < 0)
2375 goto out_resource;
2376 WRITE32(stat.mode & S_IALLUGO);
2377 }
2378 if (bmval1 & FATTR4_WORD1_NO_TRUNC) {
2379 if ((buflen -= 4) < 0)
2380 goto out_resource;
2381 WRITE32(1);
2382 }
2383 if (bmval1 & FATTR4_WORD1_NUMLINKS) {
2384 if ((buflen -= 4) < 0)
2385 goto out_resource;
2386 WRITE32(stat.nlink);
2387 }
2388 if (bmval1 & FATTR4_WORD1_OWNER) {
2389 status = nfsd4_encode_user(rqstp, stat.uid, &p, &buflen);
2390 if (status == nfserr_resource)
2391 goto out_resource;
2392 if (status)
2393 goto out;
2394 }
2395 if (bmval1 & FATTR4_WORD1_OWNER_GROUP) {
2396 status = nfsd4_encode_group(rqstp, stat.gid, &p, &buflen);
2397 if (status == nfserr_resource)
2398 goto out_resource;
2399 if (status)
2400 goto out;
2401 }
2402 if (bmval1 & FATTR4_WORD1_RAWDEV) {
2403 if ((buflen -= 8) < 0)
2404 goto out_resource;
2405 WRITE32((u32) MAJOR(stat.rdev));
2406 WRITE32((u32) MINOR(stat.rdev));
2407 }
2408 if (bmval1 & FATTR4_WORD1_SPACE_AVAIL) {
2409 if ((buflen -= 8) < 0)
2410 goto out_resource;
2411 dummy64 = (u64)statfs.f_bavail * (u64)statfs.f_bsize;
2412 WRITE64(dummy64);
2413 }
2414 if (bmval1 & FATTR4_WORD1_SPACE_FREE) {
2415 if ((buflen -= 8) < 0)
2416 goto out_resource;
2417 dummy64 = (u64)statfs.f_bfree * (u64)statfs.f_bsize;
2418 WRITE64(dummy64);
2419 }
2420 if (bmval1 & FATTR4_WORD1_SPACE_TOTAL) {
2421 if ((buflen -= 8) < 0)
2422 goto out_resource;
2423 dummy64 = (u64)statfs.f_blocks * (u64)statfs.f_bsize;
2424 WRITE64(dummy64);
2425 }
2426 if (bmval1 & FATTR4_WORD1_SPACE_USED) {
2427 if ((buflen -= 8) < 0)
2428 goto out_resource;
2429 dummy64 = (u64)stat.blocks << 9;
2430 WRITE64(dummy64);
2431 }
2432 if (bmval1 & FATTR4_WORD1_TIME_ACCESS) {
2433 if ((buflen -= 12) < 0)
2434 goto out_resource;
2435 WRITE32(0);
2436 WRITE32(stat.atime.tv_sec);
2437 WRITE32(stat.atime.tv_nsec);
2438 }
2439 if (bmval1 & FATTR4_WORD1_TIME_DELTA) {
2440 if ((buflen -= 12) < 0)
2441 goto out_resource;
2442 WRITE32(0);
2443 WRITE32(1);
2444 WRITE32(0);
2445 }
2446 if (bmval1 & FATTR4_WORD1_TIME_METADATA) {
2447 if ((buflen -= 12) < 0)
2448 goto out_resource;
2449 WRITE32(0);
2450 WRITE32(stat.ctime.tv_sec);
2451 WRITE32(stat.ctime.tv_nsec);
2452 }
2453 if (bmval1 & FATTR4_WORD1_TIME_MODIFY) {
2454 if ((buflen -= 12) < 0)
2455 goto out_resource;
2456 WRITE32(0);
2457 WRITE32(stat.mtime.tv_sec);
2458 WRITE32(stat.mtime.tv_nsec);
2459 }
2460 if (bmval1 & FATTR4_WORD1_MOUNTED_ON_FILEID) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 if ((buflen -= 8) < 0)
2462 goto out_resource;
Frank Filz406a7ea2007-11-27 11:34:05 -08002463 /*
2464 * Get parent's attributes if not ignoring crossmount
2465 * and this is the root of a cross-mounted filesystem.
2466 */
2467 if (ignore_crossmnt == 0 &&
J. Bruce Fieldsae7095a2012-10-01 17:50:56 -04002468 dentry == exp->ex_path.mnt->mnt_root)
2469 get_parent_attributes(exp, &stat);
Peter Staubach40ee5dc2007-08-16 12:10:07 -04002470 WRITE64(stat.ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 }
Benny Halevy8c18f202009-04-03 08:29:14 +03002472 if (bmval2 & FATTR4_WORD2_SUPPATTR_EXCLCREAT) {
2473 WRITE32(3);
2474 WRITE32(NFSD_SUPPATTR_EXCLCREAT_WORD0);
2475 WRITE32(NFSD_SUPPATTR_EXCLCREAT_WORD1);
2476 WRITE32(NFSD_SUPPATTR_EXCLCREAT_WORD2);
2477 }
Andy Adamson7e705702009-04-03 08:29:11 +03002478
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 *attrlenp = htonl((char *)p - (char *)attrlenp - 4);
2480 *countp = p - buffer;
2481 status = nfs_ok;
2482
2483out:
J. Bruce Fields28e05dd2007-02-16 01:28:30 -08002484 kfree(acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 if (fhp == &tempfh)
2486 fh_put(&tempfh);
2487 return status;
2488out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07002489 status = nfserrno(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 goto out;
2491out_resource:
2492 *countp = 0;
2493 status = nfserr_resource;
2494 goto out;
2495out_serverfault:
2496 status = nfserr_serverfault;
2497 goto out;
2498}
2499
J. Bruce Fieldsc0ce6ec2008-02-11 15:48:47 -05002500static inline int attributes_need_mount(u32 *bmval)
2501{
2502 if (bmval[0] & ~(FATTR4_WORD0_RDATTR_ERROR | FATTR4_WORD0_LEASE_TIME))
2503 return 1;
2504 if (bmval[1] & ~FATTR4_WORD1_MOUNTED_ON_FILEID)
2505 return 1;
2506 return 0;
2507}
2508
Al Virob37ad282006-10-19 23:28:59 -07002509static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510nfsd4_encode_dirent_fattr(struct nfsd4_readdir *cd,
Al Viro2ebbc012006-10-19 23:28:58 -07002511 const char *name, int namlen, __be32 *p, int *buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512{
2513 struct svc_export *exp = cd->rd_fhp->fh_export;
2514 struct dentry *dentry;
Al Virob37ad282006-10-19 23:28:59 -07002515 __be32 nfserr;
Frank Filz406a7ea2007-11-27 11:34:05 -08002516 int ignore_crossmnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517
2518 dentry = lookup_one_len(name, cd->rd_fhp->fh_dentry, namlen);
2519 if (IS_ERR(dentry))
2520 return nfserrno(PTR_ERR(dentry));
J. Bruce Fieldsb2c0cea2009-05-05 19:04:29 -04002521 if (!dentry->d_inode) {
2522 /*
2523 * nfsd_buffered_readdir drops the i_mutex between
2524 * readdir and calling this callback, leaving a window
2525 * where this directory entry could have gone away.
2526 */
2527 dput(dentry);
2528 return nfserr_noent;
2529 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530
2531 exp_get(exp);
Frank Filz406a7ea2007-11-27 11:34:05 -08002532 /*
2533 * In the case of a mountpoint, the client may be asking for
2534 * attributes that are only properties of the underlying filesystem
2535 * as opposed to the cross-mounted file system. In such a case,
2536 * we will not follow the cross mount and will fill the attribtutes
2537 * directly from the mountpoint dentry.
2538 */
J. Bruce Fields3227fa42009-10-25 21:43:01 -04002539 if (nfsd_mountpoint(dentry, exp)) {
J.Bruce Fields021d3a72006-12-13 00:35:24 -08002540 int err;
2541
J. Bruce Fields3227fa42009-10-25 21:43:01 -04002542 if (!(exp->ex_flags & NFSEXP_V4ROOT)
2543 && !attributes_need_mount(cd->rd_bmval)) {
2544 ignore_crossmnt = 1;
2545 goto out_encode;
2546 }
Andy Adamsondcb488a32007-07-17 04:04:51 -07002547 /*
2548 * Why the heck aren't we just using nfsd_lookup??
2549 * Different "."/".." handling? Something else?
2550 * At least, add a comment here to explain....
2551 */
J.Bruce Fields021d3a72006-12-13 00:35:24 -08002552 err = nfsd_cross_mnt(cd->rd_rqstp, &dentry, &exp);
2553 if (err) {
2554 nfserr = nfserrno(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 goto out_put;
2556 }
Andy Adamsondcb488a32007-07-17 04:04:51 -07002557 nfserr = check_nfsd_access(exp, cd->rd_rqstp);
2558 if (nfserr)
2559 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560
2561 }
J. Bruce Fields3227fa42009-10-25 21:43:01 -04002562out_encode:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 nfserr = nfsd4_encode_fattr(NULL, exp, dentry, p, buflen, cd->rd_bmval,
Frank Filz406a7ea2007-11-27 11:34:05 -08002564 cd->rd_rqstp, ignore_crossmnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565out_put:
2566 dput(dentry);
2567 exp_put(exp);
2568 return nfserr;
2569}
2570
Al Viro2ebbc012006-10-19 23:28:58 -07002571static __be32 *
Al Virob37ad282006-10-19 23:28:59 -07002572nfsd4_encode_rdattr_error(__be32 *p, int buflen, __be32 nfserr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573{
Al Viro2ebbc012006-10-19 23:28:58 -07002574 __be32 *attrlenp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575
2576 if (buflen < 6)
2577 return NULL;
2578 *p++ = htonl(2);
2579 *p++ = htonl(FATTR4_WORD0_RDATTR_ERROR); /* bmval0 */
2580 *p++ = htonl(0); /* bmval1 */
2581
2582 attrlenp = p++;
2583 *p++ = nfserr; /* no htonl */
2584 *attrlenp = htonl((char *)p - (char *)attrlenp - 4);
2585 return p;
2586}
2587
2588static int
NeilBrowna0ad13e2007-01-26 00:57:10 -08002589nfsd4_encode_dirent(void *ccdv, const char *name, int namlen,
2590 loff_t offset, u64 ino, unsigned int d_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591{
NeilBrowna0ad13e2007-01-26 00:57:10 -08002592 struct readdir_cd *ccd = ccdv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 struct nfsd4_readdir *cd = container_of(ccd, struct nfsd4_readdir, common);
2594 int buflen;
Al Viro2ebbc012006-10-19 23:28:58 -07002595 __be32 *p = cd->buffer;
J. Bruce Fieldsb2c0cea2009-05-05 19:04:29 -04002596 __be32 *cookiep;
Al Virob37ad282006-10-19 23:28:59 -07002597 __be32 nfserr = nfserr_toosmall;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598
2599 /* In nfsv4, "." and ".." never make it onto the wire.. */
2600 if (name && isdotent(name, namlen)) {
2601 cd->common.err = nfs_ok;
2602 return 0;
2603 }
2604
2605 if (cd->offset)
2606 xdr_encode_hyper(cd->offset, (u64) offset);
2607
2608 buflen = cd->buflen - 4 - XDR_QUADLEN(namlen);
2609 if (buflen < 0)
2610 goto fail;
2611
2612 *p++ = xdr_one; /* mark entry present */
J. Bruce Fieldsb2c0cea2009-05-05 19:04:29 -04002613 cookiep = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 p = xdr_encode_hyper(p, NFS_OFFSET_MAX); /* offset of next entry */
2615 p = xdr_encode_array(p, name, namlen); /* name length & name */
2616
2617 nfserr = nfsd4_encode_dirent_fattr(cd, name, namlen, p, &buflen);
2618 switch (nfserr) {
2619 case nfs_ok:
2620 p += buflen;
2621 break;
2622 case nfserr_resource:
2623 nfserr = nfserr_toosmall;
2624 goto fail;
J. Bruce Fieldsb2c0cea2009-05-05 19:04:29 -04002625 case nfserr_noent:
2626 goto skip_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 default:
2628 /*
2629 * If the client requested the RDATTR_ERROR attribute,
2630 * we stuff the error code into this attribute
2631 * and continue. If this attribute was not requested,
2632 * then in accordance with the spec, we fail the
2633 * entire READDIR operation(!)
2634 */
2635 if (!(cd->rd_bmval[0] & FATTR4_WORD0_RDATTR_ERROR))
2636 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 p = nfsd4_encode_rdattr_error(p, buflen, nfserr);
Fred Isaman34081ef2006-01-18 17:43:40 -08002638 if (p == NULL) {
2639 nfserr = nfserr_toosmall;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 goto fail;
Fred Isaman34081ef2006-01-18 17:43:40 -08002641 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 }
2643 cd->buflen -= (p - cd->buffer);
2644 cd->buffer = p;
J. Bruce Fieldsb2c0cea2009-05-05 19:04:29 -04002645 cd->offset = cookiep;
2646skip_entry:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 cd->common.err = nfs_ok;
2648 return 0;
2649fail:
2650 cd->common.err = nfserr;
2651 return -EINVAL;
2652}
2653
Benny Halevye2f282b2008-08-12 20:45:07 +03002654static void
2655nfsd4_encode_stateid(struct nfsd4_compoundres *resp, stateid_t *sid)
2656{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002657 __be32 *p;
Benny Halevye2f282b2008-08-12 20:45:07 +03002658
2659 RESERVE_SPACE(sizeof(stateid_t));
2660 WRITE32(sid->si_generation);
2661 WRITEMEM(&sid->si_opaque, sizeof(stateid_opaque_t));
2662 ADJUST_ARGS();
2663}
2664
Benny Halevy695e12f2008-07-04 14:38:33 +03002665static __be32
Al Virob37ad282006-10-19 23:28:59 -07002666nfsd4_encode_access(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_access *access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002668 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669
2670 if (!nfserr) {
2671 RESERVE_SPACE(8);
2672 WRITE32(access->ac_supported);
2673 WRITE32(access->ac_resp_access);
2674 ADJUST_ARGS();
2675 }
Benny Halevy695e12f2008-07-04 14:38:33 +03002676 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677}
2678
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002679static __be32 nfsd4_encode_bind_conn_to_session(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_bind_conn_to_session *bcts)
2680{
2681 __be32 *p;
2682
2683 if (!nfserr) {
2684 RESERVE_SPACE(NFS4_MAX_SESSIONID_LEN + 8);
2685 WRITEMEM(bcts->sessionid.data, NFS4_MAX_SESSIONID_LEN);
2686 WRITE32(bcts->dir);
J. Bruce Fields6e67b5d2012-09-13 09:49:43 -04002687 /* Sorry, we do not yet support RDMA over 4.1: */
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002688 WRITE32(0);
2689 ADJUST_ARGS();
2690 }
2691 return nfserr;
2692}
2693
Benny Halevy695e12f2008-07-04 14:38:33 +03002694static __be32
Al Virob37ad282006-10-19 23:28:59 -07002695nfsd4_encode_close(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696{
2697 ENCODE_SEQID_OP_HEAD;
2698
Benny Halevye2f282b2008-08-12 20:45:07 +03002699 if (!nfserr)
2700 nfsd4_encode_stateid(resp, &close->cl_stateid);
2701
J. Bruce Fieldsf3e42232011-08-24 12:27:31 -04002702 encode_seqid_op_tail(resp, save, nfserr);
Benny Halevy695e12f2008-07-04 14:38:33 +03002703 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704}
2705
2706
Benny Halevy695e12f2008-07-04 14:38:33 +03002707static __be32
Al Virob37ad282006-10-19 23:28:59 -07002708nfsd4_encode_commit(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_commit *commit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002710 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711
2712 if (!nfserr) {
Chuck Leverab4684d2012-03-02 17:13:50 -05002713 RESERVE_SPACE(NFS4_VERIFIER_SIZE);
2714 WRITEMEM(commit->co_verf.data, NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 ADJUST_ARGS();
2716 }
Benny Halevy695e12f2008-07-04 14:38:33 +03002717 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718}
2719
Benny Halevy695e12f2008-07-04 14:38:33 +03002720static __be32
Al Virob37ad282006-10-19 23:28:59 -07002721nfsd4_encode_create(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_create *create)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002723 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724
2725 if (!nfserr) {
2726 RESERVE_SPACE(32);
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002727 write_cinfo(&p, &create->cr_cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 WRITE32(2);
2729 WRITE32(create->cr_bmval[0]);
2730 WRITE32(create->cr_bmval[1]);
2731 ADJUST_ARGS();
2732 }
Benny Halevy695e12f2008-07-04 14:38:33 +03002733 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734}
2735
Al Virob37ad282006-10-19 23:28:59 -07002736static __be32
2737nfsd4_encode_getattr(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_getattr *getattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738{
2739 struct svc_fh *fhp = getattr->ga_fhp;
2740 int buflen;
2741
2742 if (nfserr)
2743 return nfserr;
2744
2745 buflen = resp->end - resp->p - (COMPOUND_ERR_SLACK_SPACE >> 2);
2746 nfserr = nfsd4_encode_fattr(fhp, fhp->fh_export, fhp->fh_dentry,
2747 resp->p, &buflen, getattr->ga_bmval,
Frank Filz406a7ea2007-11-27 11:34:05 -08002748 resp->rqstp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 if (!nfserr)
2750 resp->p += buflen;
2751 return nfserr;
2752}
2753
Benny Halevy695e12f2008-07-04 14:38:33 +03002754static __be32
2755nfsd4_encode_getfh(struct nfsd4_compoundres *resp, __be32 nfserr, struct svc_fh **fhpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756{
Benny Halevy695e12f2008-07-04 14:38:33 +03002757 struct svc_fh *fhp = *fhpp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 unsigned int len;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002759 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760
2761 if (!nfserr) {
2762 len = fhp->fh_handle.fh_size;
2763 RESERVE_SPACE(len + 4);
2764 WRITE32(len);
2765 WRITEMEM(&fhp->fh_handle.fh_base, len);
2766 ADJUST_ARGS();
2767 }
Benny Halevy695e12f2008-07-04 14:38:33 +03002768 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769}
2770
2771/*
2772* Including all fields other than the name, a LOCK4denied structure requires
2773* 8(clientid) + 4(namelen) + 8(offset) + 8(length) + 4(type) = 32 bytes.
2774*/
2775static void
2776nfsd4_encode_lock_denied(struct nfsd4_compoundres *resp, struct nfsd4_lock_denied *ld)
2777{
J. Bruce Fields7c13f342011-08-30 22:15:47 -04002778 struct xdr_netobj *conf = &ld->ld_owner;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002779 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780
J. Bruce Fields7c13f342011-08-30 22:15:47 -04002781 RESERVE_SPACE(32 + XDR_LEN(conf->len));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 WRITE64(ld->ld_start);
2783 WRITE64(ld->ld_length);
2784 WRITE32(ld->ld_type);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04002785 if (conf->len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 WRITEMEM(&ld->ld_clientid, 8);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04002787 WRITE32(conf->len);
2788 WRITEMEM(conf->data, conf->len);
2789 kfree(conf->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 } else { /* non - nfsv4 lock in conflict, no clientid nor owner */
2791 WRITE64((u64)0); /* clientid */
2792 WRITE32(0); /* length of owner name */
2793 }
2794 ADJUST_ARGS();
2795}
2796
Benny Halevy695e12f2008-07-04 14:38:33 +03002797static __be32
Al Virob37ad282006-10-19 23:28:59 -07002798nfsd4_encode_lock(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 ENCODE_SEQID_OP_HEAD;
2801
Benny Halevye2f282b2008-08-12 20:45:07 +03002802 if (!nfserr)
2803 nfsd4_encode_stateid(resp, &lock->lk_resp_stateid);
2804 else if (nfserr == nfserr_denied)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 nfsd4_encode_lock_denied(resp, &lock->lk_denied);
2806
J. Bruce Fieldsf3e42232011-08-24 12:27:31 -04002807 encode_seqid_op_tail(resp, save, nfserr);
Benny Halevy695e12f2008-07-04 14:38:33 +03002808 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809}
2810
Benny Halevy695e12f2008-07-04 14:38:33 +03002811static __be32
Al Virob37ad282006-10-19 23:28:59 -07002812nfsd4_encode_lockt(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813{
2814 if (nfserr == nfserr_denied)
2815 nfsd4_encode_lock_denied(resp, &lockt->lt_denied);
Benny Halevy695e12f2008-07-04 14:38:33 +03002816 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817}
2818
Benny Halevy695e12f2008-07-04 14:38:33 +03002819static __be32
Al Virob37ad282006-10-19 23:28:59 -07002820nfsd4_encode_locku(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821{
2822 ENCODE_SEQID_OP_HEAD;
2823
Benny Halevye2f282b2008-08-12 20:45:07 +03002824 if (!nfserr)
2825 nfsd4_encode_stateid(resp, &locku->lu_stateid);
2826
J. Bruce Fieldsf3e42232011-08-24 12:27:31 -04002827 encode_seqid_op_tail(resp, save, nfserr);
Benny Halevy695e12f2008-07-04 14:38:33 +03002828 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829}
2830
2831
Benny Halevy695e12f2008-07-04 14:38:33 +03002832static __be32
Al Virob37ad282006-10-19 23:28:59 -07002833nfsd4_encode_link(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_link *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002835 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836
2837 if (!nfserr) {
2838 RESERVE_SPACE(20);
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002839 write_cinfo(&p, &link->li_cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 ADJUST_ARGS();
2841 }
Benny Halevy695e12f2008-07-04 14:38:33 +03002842 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843}
2844
2845
Benny Halevy695e12f2008-07-04 14:38:33 +03002846static __be32
Al Virob37ad282006-10-19 23:28:59 -07002847nfsd4_encode_open(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002849 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 ENCODE_SEQID_OP_HEAD;
2851
2852 if (nfserr)
2853 goto out;
2854
Benny Halevye2f282b2008-08-12 20:45:07 +03002855 nfsd4_encode_stateid(resp, &open->op_stateid);
2856 RESERVE_SPACE(40);
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002857 write_cinfo(&p, &open->op_cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 WRITE32(open->op_rflags);
2859 WRITE32(2);
2860 WRITE32(open->op_bmval[0]);
2861 WRITE32(open->op_bmval[1]);
2862 WRITE32(open->op_delegate_type);
2863 ADJUST_ARGS();
2864
2865 switch (open->op_delegate_type) {
2866 case NFS4_OPEN_DELEGATE_NONE:
2867 break;
2868 case NFS4_OPEN_DELEGATE_READ:
Benny Halevye2f282b2008-08-12 20:45:07 +03002869 nfsd4_encode_stateid(resp, &open->op_delegate_stateid);
2870 RESERVE_SPACE(20);
NeilBrown7b190fe2005-06-23 22:03:23 -07002871 WRITE32(open->op_recall);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872
2873 /*
2874 * TODO: ACE's in delegations
2875 */
2876 WRITE32(NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
2877 WRITE32(0);
2878 WRITE32(0);
2879 WRITE32(0); /* XXX: is NULL principal ok? */
2880 ADJUST_ARGS();
2881 break;
2882 case NFS4_OPEN_DELEGATE_WRITE:
Benny Halevye2f282b2008-08-12 20:45:07 +03002883 nfsd4_encode_stateid(resp, &open->op_delegate_stateid);
2884 RESERVE_SPACE(32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 WRITE32(0);
2886
2887 /*
2888 * TODO: space_limit's in delegations
2889 */
2890 WRITE32(NFS4_LIMIT_SIZE);
2891 WRITE32(~(u32)0);
2892 WRITE32(~(u32)0);
2893
2894 /*
2895 * TODO: ACE's in delegations
2896 */
2897 WRITE32(NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
2898 WRITE32(0);
2899 WRITE32(0);
2900 WRITE32(0); /* XXX: is NULL principal ok? */
2901 ADJUST_ARGS();
2902 break;
Benny Halevyd24433c2012-02-16 20:57:17 +02002903 case NFS4_OPEN_DELEGATE_NONE_EXT: /* 4.1 */
2904 switch (open->op_why_no_deleg) {
2905 case WND4_CONTENTION:
2906 case WND4_RESOURCE:
2907 RESERVE_SPACE(8);
2908 WRITE32(open->op_why_no_deleg);
2909 WRITE32(0); /* deleg signaling not supported yet */
2910 break;
2911 default:
2912 RESERVE_SPACE(4);
2913 WRITE32(open->op_why_no_deleg);
2914 }
2915 ADJUST_ARGS();
2916 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 default:
2918 BUG();
2919 }
2920 /* XXX save filehandle here */
2921out:
J. Bruce Fieldsf3e42232011-08-24 12:27:31 -04002922 encode_seqid_op_tail(resp, save, nfserr);
Benny Halevy695e12f2008-07-04 14:38:33 +03002923 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924}
2925
Benny Halevy695e12f2008-07-04 14:38:33 +03002926static __be32
Al Virob37ad282006-10-19 23:28:59 -07002927nfsd4_encode_open_confirm(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928{
2929 ENCODE_SEQID_OP_HEAD;
Benny Halevye2f282b2008-08-12 20:45:07 +03002930
2931 if (!nfserr)
2932 nfsd4_encode_stateid(resp, &oc->oc_resp_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933
J. Bruce Fieldsf3e42232011-08-24 12:27:31 -04002934 encode_seqid_op_tail(resp, save, nfserr);
Benny Halevy695e12f2008-07-04 14:38:33 +03002935 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936}
2937
Benny Halevy695e12f2008-07-04 14:38:33 +03002938static __be32
Al Virob37ad282006-10-19 23:28:59 -07002939nfsd4_encode_open_downgrade(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940{
2941 ENCODE_SEQID_OP_HEAD;
Benny Halevye2f282b2008-08-12 20:45:07 +03002942
2943 if (!nfserr)
2944 nfsd4_encode_stateid(resp, &od->od_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945
J. Bruce Fieldsf3e42232011-08-24 12:27:31 -04002946 encode_seqid_op_tail(resp, save, nfserr);
Benny Halevy695e12f2008-07-04 14:38:33 +03002947 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948}
2949
Al Virob37ad282006-10-19 23:28:59 -07002950static __be32
2951nfsd4_encode_read(struct nfsd4_compoundres *resp, __be32 nfserr,
NeilBrown44524352006-10-04 02:15:46 -07002952 struct nfsd4_read *read)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953{
2954 u32 eof;
2955 int v, pn;
2956 unsigned long maxcount;
2957 long len;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002958 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959
2960 if (nfserr)
2961 return nfserr;
2962 if (resp->xbuf->page_len)
2963 return nfserr_resource;
2964
2965 RESERVE_SPACE(8); /* eof flag and byte count */
2966
Greg Banks7adae482006-10-04 02:15:47 -07002967 maxcount = svc_max_payload(resp->rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 if (maxcount > read->rd_length)
2969 maxcount = read->rd_length;
2970
2971 len = maxcount;
2972 v = 0;
2973 while (len > 0) {
NeilBrown44524352006-10-04 02:15:46 -07002974 pn = resp->rqstp->rq_resused++;
NeilBrown3cc03b12006-10-04 02:15:47 -07002975 resp->rqstp->rq_vec[v].iov_base =
NeilBrown44524352006-10-04 02:15:46 -07002976 page_address(resp->rqstp->rq_respages[pn]);
NeilBrown3cc03b12006-10-04 02:15:47 -07002977 resp->rqstp->rq_vec[v].iov_len =
NeilBrown44524352006-10-04 02:15:46 -07002978 len < PAGE_SIZE ? len : PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 v++;
2980 len -= PAGE_SIZE;
2981 }
2982 read->rd_vlen = v;
2983
J. Bruce Fields039a87c2010-07-30 11:33:32 -04002984 nfserr = nfsd_read_file(read->rd_rqstp, read->rd_fhp, read->rd_filp,
NeilBrown3cc03b12006-10-04 02:15:47 -07002985 read->rd_offset, resp->rqstp->rq_vec, read->rd_vlen,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 &maxcount);
2987
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 if (nfserr)
2989 return nfserr;
NeilBrown44524352006-10-04 02:15:46 -07002990 eof = (read->rd_offset + maxcount >=
2991 read->rd_fhp->fh_dentry->d_inode->i_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992
2993 WRITE32(eof);
2994 WRITE32(maxcount);
2995 ADJUST_ARGS();
NeilBrown6ed6dec2006-04-10 22:55:32 -07002996 resp->xbuf->head[0].iov_len = (char*)p
2997 - (char*)resp->xbuf->head[0].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 resp->xbuf->page_len = maxcount;
2999
NeilBrown6ed6dec2006-04-10 22:55:32 -07003000 /* Use rest of head for padding and remaining ops: */
NeilBrown6ed6dec2006-04-10 22:55:32 -07003001 resp->xbuf->tail[0].iov_base = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 resp->xbuf->tail[0].iov_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 if (maxcount&3) {
NeilBrown6ed6dec2006-04-10 22:55:32 -07003004 RESERVE_SPACE(4);
3005 WRITE32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 resp->xbuf->tail[0].iov_base += maxcount&3;
3007 resp->xbuf->tail[0].iov_len = 4 - (maxcount&3);
NeilBrown6ed6dec2006-04-10 22:55:32 -07003008 ADJUST_ARGS();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 }
3010 return 0;
3011}
3012
Al Virob37ad282006-10-19 23:28:59 -07003013static __be32
3014nfsd4_encode_readlink(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_readlink *readlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015{
3016 int maxcount;
3017 char *page;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003018 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019
3020 if (nfserr)
3021 return nfserr;
3022 if (resp->xbuf->page_len)
3023 return nfserr_resource;
3024
NeilBrown44524352006-10-04 02:15:46 -07003025 page = page_address(resp->rqstp->rq_respages[resp->rqstp->rq_resused++]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026
3027 maxcount = PAGE_SIZE;
3028 RESERVE_SPACE(4);
3029
3030 /*
3031 * XXX: By default, the ->readlink() VFS op will truncate symlinks
3032 * if they would overflow the buffer. Is this kosher in NFSv4? If
3033 * not, one easy fix is: if ->readlink() precisely fills the buffer,
3034 * assume that truncation occurred, and return NFS4ERR_RESOURCE.
3035 */
3036 nfserr = nfsd_readlink(readlink->rl_rqstp, readlink->rl_fhp, page, &maxcount);
3037 if (nfserr == nfserr_isdir)
3038 return nfserr_inval;
3039 if (nfserr)
3040 return nfserr;
3041
3042 WRITE32(maxcount);
3043 ADJUST_ARGS();
NeilBrown6ed6dec2006-04-10 22:55:32 -07003044 resp->xbuf->head[0].iov_len = (char*)p
3045 - (char*)resp->xbuf->head[0].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 resp->xbuf->page_len = maxcount;
NeilBrown6ed6dec2006-04-10 22:55:32 -07003047
3048 /* Use rest of head for padding and remaining ops: */
NeilBrown6ed6dec2006-04-10 22:55:32 -07003049 resp->xbuf->tail[0].iov_base = p;
3050 resp->xbuf->tail[0].iov_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 if (maxcount&3) {
NeilBrown6ed6dec2006-04-10 22:55:32 -07003052 RESERVE_SPACE(4);
3053 WRITE32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 resp->xbuf->tail[0].iov_base += maxcount&3;
3055 resp->xbuf->tail[0].iov_len = 4 - (maxcount&3);
NeilBrown6ed6dec2006-04-10 22:55:32 -07003056 ADJUST_ARGS();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 }
3058 return 0;
3059}
3060
Al Virob37ad282006-10-19 23:28:59 -07003061static __be32
3062nfsd4_encode_readdir(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_readdir *readdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063{
3064 int maxcount;
3065 loff_t offset;
Al Viro2ebbc012006-10-19 23:28:58 -07003066 __be32 *page, *savep, *tailbase;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003067 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068
3069 if (nfserr)
3070 return nfserr;
3071 if (resp->xbuf->page_len)
3072 return nfserr_resource;
3073
Chuck Leverab4684d2012-03-02 17:13:50 -05003074 RESERVE_SPACE(NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 savep = p;
3076
3077 /* XXX: Following NFSv3, we ignore the READDIR verifier for now. */
3078 WRITE32(0);
3079 WRITE32(0);
3080 ADJUST_ARGS();
3081 resp->xbuf->head[0].iov_len = ((char*)resp->p) - (char*)resp->xbuf->head[0].iov_base;
NeilBrownbb6e8a92006-04-10 22:55:33 -07003082 tailbase = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083
3084 maxcount = PAGE_SIZE;
3085 if (maxcount > readdir->rd_maxcount)
3086 maxcount = readdir->rd_maxcount;
3087
3088 /*
3089 * Convert from bytes to words, account for the two words already
3090 * written, make sure to leave two words at the end for the next
3091 * pointer and eof field.
3092 */
3093 maxcount = (maxcount >> 2) - 4;
3094 if (maxcount < 0) {
3095 nfserr = nfserr_toosmall;
3096 goto err_no_verf;
3097 }
3098
NeilBrown44524352006-10-04 02:15:46 -07003099 page = page_address(resp->rqstp->rq_respages[resp->rqstp->rq_resused++]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 readdir->common.err = 0;
3101 readdir->buflen = maxcount;
3102 readdir->buffer = page;
3103 readdir->offset = NULL;
3104
3105 offset = readdir->rd_cookie;
3106 nfserr = nfsd_readdir(readdir->rd_rqstp, readdir->rd_fhp,
3107 &offset,
3108 &readdir->common, nfsd4_encode_dirent);
3109 if (nfserr == nfs_ok &&
3110 readdir->common.err == nfserr_toosmall &&
3111 readdir->buffer == page)
3112 nfserr = nfserr_toosmall;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 if (nfserr)
3114 goto err_no_verf;
3115
3116 if (readdir->offset)
3117 xdr_encode_hyper(readdir->offset, offset);
3118
3119 p = readdir->buffer;
3120 *p++ = 0; /* no more entries */
3121 *p++ = htonl(readdir->common.err == nfserr_eof);
NeilBrown44524352006-10-04 02:15:46 -07003122 resp->xbuf->page_len = ((char*)p) - (char*)page_address(
3123 resp->rqstp->rq_respages[resp->rqstp->rq_resused-1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124
NeilBrownbb6e8a92006-04-10 22:55:33 -07003125 /* Use rest of head for padding and remaining ops: */
NeilBrownbb6e8a92006-04-10 22:55:33 -07003126 resp->xbuf->tail[0].iov_base = tailbase;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 resp->xbuf->tail[0].iov_len = 0;
3128 resp->p = resp->xbuf->tail[0].iov_base;
NeilBrownbb6e8a92006-04-10 22:55:33 -07003129 resp->end = resp->p + (PAGE_SIZE - resp->xbuf->head[0].iov_len)/4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130
3131 return 0;
3132err_no_verf:
3133 p = savep;
3134 ADJUST_ARGS();
3135 return nfserr;
3136}
3137
Benny Halevy695e12f2008-07-04 14:38:33 +03003138static __be32
Al Virob37ad282006-10-19 23:28:59 -07003139nfsd4_encode_remove(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_remove *remove)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003141 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142
3143 if (!nfserr) {
3144 RESERVE_SPACE(20);
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04003145 write_cinfo(&p, &remove->rm_cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 ADJUST_ARGS();
3147 }
Benny Halevy695e12f2008-07-04 14:38:33 +03003148 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149}
3150
Benny Halevy695e12f2008-07-04 14:38:33 +03003151static __be32
Al Virob37ad282006-10-19 23:28:59 -07003152nfsd4_encode_rename(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_rename *rename)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003154 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155
3156 if (!nfserr) {
3157 RESERVE_SPACE(40);
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04003158 write_cinfo(&p, &rename->rn_sinfo);
3159 write_cinfo(&p, &rename->rn_tinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160 ADJUST_ARGS();
3161 }
Benny Halevy695e12f2008-07-04 14:38:33 +03003162 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163}
3164
Benny Halevy695e12f2008-07-04 14:38:33 +03003165static __be32
Mi Jinlong22b6dee2010-12-27 14:29:57 +08003166nfsd4_do_encode_secinfo(struct nfsd4_compoundres *resp,
3167 __be32 nfserr,struct svc_export *exp)
Andy Adamsondcb488a32007-07-17 04:04:51 -07003168{
3169 int i = 0;
J. Bruce Fields4796f452007-07-17 04:04:51 -07003170 u32 nflavs;
3171 struct exp_flavor_info *flavs;
3172 struct exp_flavor_info def_flavs[2];
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003173 __be32 *p;
Andy Adamsondcb488a32007-07-17 04:04:51 -07003174
3175 if (nfserr)
3176 goto out;
J. Bruce Fields4796f452007-07-17 04:04:51 -07003177 if (exp->ex_nflavors) {
3178 flavs = exp->ex_flavors;
3179 nflavs = exp->ex_nflavors;
3180 } else { /* Handling of some defaults in absence of real secinfo: */
3181 flavs = def_flavs;
3182 if (exp->ex_client->flavour->flavour == RPC_AUTH_UNIX) {
3183 nflavs = 2;
3184 flavs[0].pseudoflavor = RPC_AUTH_UNIX;
3185 flavs[1].pseudoflavor = RPC_AUTH_NULL;
3186 } else if (exp->ex_client->flavour->flavour == RPC_AUTH_GSS) {
3187 nflavs = 1;
3188 flavs[0].pseudoflavor
3189 = svcauth_gss_flavor(exp->ex_client);
3190 } else {
3191 nflavs = 1;
3192 flavs[0].pseudoflavor
3193 = exp->ex_client->flavour->flavour;
3194 }
3195 }
3196
Andy Adamsondcb488a32007-07-17 04:04:51 -07003197 RESERVE_SPACE(4);
J. Bruce Fields4796f452007-07-17 04:04:51 -07003198 WRITE32(nflavs);
Andy Adamsondcb488a32007-07-17 04:04:51 -07003199 ADJUST_ARGS();
J. Bruce Fields4796f452007-07-17 04:04:51 -07003200 for (i = 0; i < nflavs; i++) {
3201 u32 flav = flavs[i].pseudoflavor;
Andy Adamsondcb488a32007-07-17 04:04:51 -07003202 struct gss_api_mech *gm = gss_mech_get_by_pseudoflavor(flav);
3203
3204 if (gm) {
3205 RESERVE_SPACE(4);
3206 WRITE32(RPC_AUTH_GSS);
3207 ADJUST_ARGS();
3208 RESERVE_SPACE(4 + gm->gm_oid.len);
3209 WRITE32(gm->gm_oid.len);
3210 WRITEMEM(gm->gm_oid.data, gm->gm_oid.len);
3211 ADJUST_ARGS();
3212 RESERVE_SPACE(4);
3213 WRITE32(0); /* qop */
3214 ADJUST_ARGS();
3215 RESERVE_SPACE(4);
3216 WRITE32(gss_pseudoflavor_to_service(gm, flav));
3217 ADJUST_ARGS();
3218 gss_mech_put(gm);
3219 } else {
3220 RESERVE_SPACE(4);
3221 WRITE32(flav);
3222 ADJUST_ARGS();
3223 }
3224 }
3225out:
3226 if (exp)
3227 exp_put(exp);
Benny Halevy695e12f2008-07-04 14:38:33 +03003228 return nfserr;
Andy Adamsondcb488a32007-07-17 04:04:51 -07003229}
3230
Mi Jinlong22b6dee2010-12-27 14:29:57 +08003231static __be32
3232nfsd4_encode_secinfo(struct nfsd4_compoundres *resp, __be32 nfserr,
3233 struct nfsd4_secinfo *secinfo)
3234{
3235 return nfsd4_do_encode_secinfo(resp, nfserr, secinfo->si_exp);
3236}
3237
3238static __be32
3239nfsd4_encode_secinfo_no_name(struct nfsd4_compoundres *resp, __be32 nfserr,
3240 struct nfsd4_secinfo_no_name *secinfo)
3241{
3242 return nfsd4_do_encode_secinfo(resp, nfserr, secinfo->sin_exp);
3243}
3244
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245/*
3246 * The SETATTR encode routine is special -- it always encodes a bitmap,
3247 * regardless of the error status.
3248 */
Benny Halevy695e12f2008-07-04 14:38:33 +03003249static __be32
Al Virob37ad282006-10-19 23:28:59 -07003250nfsd4_encode_setattr(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_setattr *setattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003252 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253
3254 RESERVE_SPACE(12);
3255 if (nfserr) {
3256 WRITE32(2);
3257 WRITE32(0);
3258 WRITE32(0);
3259 }
3260 else {
3261 WRITE32(2);
3262 WRITE32(setattr->sa_bmval[0]);
3263 WRITE32(setattr->sa_bmval[1]);
3264 }
3265 ADJUST_ARGS();
Benny Halevy695e12f2008-07-04 14:38:33 +03003266 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267}
3268
Benny Halevy695e12f2008-07-04 14:38:33 +03003269static __be32
Al Virob37ad282006-10-19 23:28:59 -07003270nfsd4_encode_setclientid(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_setclientid *scd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003272 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273
3274 if (!nfserr) {
Chuck Leverab4684d2012-03-02 17:13:50 -05003275 RESERVE_SPACE(8 + NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 WRITEMEM(&scd->se_clientid, 8);
Chuck Leverab4684d2012-03-02 17:13:50 -05003277 WRITEMEM(&scd->se_confirm, NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 ADJUST_ARGS();
3279 }
3280 else if (nfserr == nfserr_clid_inuse) {
3281 RESERVE_SPACE(8);
3282 WRITE32(0);
3283 WRITE32(0);
3284 ADJUST_ARGS();
3285 }
Benny Halevy695e12f2008-07-04 14:38:33 +03003286 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287}
3288
Benny Halevy695e12f2008-07-04 14:38:33 +03003289static __be32
Al Virob37ad282006-10-19 23:28:59 -07003290nfsd4_encode_write(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_write *write)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003292 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293
3294 if (!nfserr) {
3295 RESERVE_SPACE(16);
3296 WRITE32(write->wr_bytes_written);
3297 WRITE32(write->wr_how_written);
Chuck Leverab4684d2012-03-02 17:13:50 -05003298 WRITEMEM(write->wr_verifier.data, NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 ADJUST_ARGS();
3300 }
Benny Halevy695e12f2008-07-04 14:38:33 +03003301 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302}
3303
Benny Halevy695e12f2008-07-04 14:38:33 +03003304static __be32
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003305nfsd4_encode_exchange_id(struct nfsd4_compoundres *resp, __be32 nfserr,
Andy Adamson2db134e2009-04-03 08:27:55 +03003306 struct nfsd4_exchange_id *exid)
3307{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003308 __be32 *p;
Andy Adamson0733d212009-04-03 08:28:01 +03003309 char *major_id;
3310 char *server_scope;
3311 int major_id_sz;
3312 int server_scope_sz;
3313 uint64_t minor_id = 0;
3314
3315 if (nfserr)
3316 return nfserr;
3317
3318 major_id = utsname()->nodename;
3319 major_id_sz = strlen(major_id);
3320 server_scope = utsname()->nodename;
3321 server_scope_sz = strlen(server_scope);
3322
3323 RESERVE_SPACE(
3324 8 /* eir_clientid */ +
3325 4 /* eir_sequenceid */ +
3326 4 /* eir_flags */ +
3327 4 /* spr_how (SP4_NONE) */ +
3328 8 /* so_minor_id */ +
3329 4 /* so_major_id.len */ +
3330 (XDR_QUADLEN(major_id_sz) * 4) +
3331 4 /* eir_server_scope.len */ +
3332 (XDR_QUADLEN(server_scope_sz) * 4) +
3333 4 /* eir_server_impl_id.count (0) */);
3334
3335 WRITEMEM(&exid->clientid, 8);
3336 WRITE32(exid->seqid);
3337 WRITE32(exid->flags);
3338
3339 /* state_protect4_r. Currently only support SP4_NONE */
3340 BUG_ON(exid->spa_how != SP4_NONE);
3341 WRITE32(exid->spa_how);
3342
3343 /* The server_owner struct */
3344 WRITE64(minor_id); /* Minor id */
3345 /* major id */
3346 WRITE32(major_id_sz);
3347 WRITEMEM(major_id, major_id_sz);
3348
3349 /* Server scope */
3350 WRITE32(server_scope_sz);
3351 WRITEMEM(server_scope, server_scope_sz);
3352
3353 /* Implementation id */
3354 WRITE32(0); /* zero length nfs_impl_id4 array */
3355 ADJUST_ARGS();
3356 return 0;
Andy Adamson2db134e2009-04-03 08:27:55 +03003357}
3358
3359static __be32
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003360nfsd4_encode_create_session(struct nfsd4_compoundres *resp, __be32 nfserr,
Andy Adamson2db134e2009-04-03 08:27:55 +03003361 struct nfsd4_create_session *sess)
3362{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003363 __be32 *p;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003364
3365 if (nfserr)
3366 return nfserr;
3367
3368 RESERVE_SPACE(24);
3369 WRITEMEM(sess->sessionid.data, NFS4_MAX_SESSIONID_LEN);
3370 WRITE32(sess->seqid);
3371 WRITE32(sess->flags);
3372 ADJUST_ARGS();
3373
3374 RESERVE_SPACE(28);
3375 WRITE32(0); /* headerpadsz */
3376 WRITE32(sess->fore_channel.maxreq_sz);
3377 WRITE32(sess->fore_channel.maxresp_sz);
3378 WRITE32(sess->fore_channel.maxresp_cached);
3379 WRITE32(sess->fore_channel.maxops);
3380 WRITE32(sess->fore_channel.maxreqs);
3381 WRITE32(sess->fore_channel.nr_rdma_attrs);
3382 ADJUST_ARGS();
3383
3384 if (sess->fore_channel.nr_rdma_attrs) {
3385 RESERVE_SPACE(4);
3386 WRITE32(sess->fore_channel.rdma_attrs);
3387 ADJUST_ARGS();
3388 }
3389
3390 RESERVE_SPACE(28);
3391 WRITE32(0); /* headerpadsz */
3392 WRITE32(sess->back_channel.maxreq_sz);
3393 WRITE32(sess->back_channel.maxresp_sz);
3394 WRITE32(sess->back_channel.maxresp_cached);
3395 WRITE32(sess->back_channel.maxops);
3396 WRITE32(sess->back_channel.maxreqs);
3397 WRITE32(sess->back_channel.nr_rdma_attrs);
3398 ADJUST_ARGS();
3399
3400 if (sess->back_channel.nr_rdma_attrs) {
3401 RESERVE_SPACE(4);
3402 WRITE32(sess->back_channel.rdma_attrs);
3403 ADJUST_ARGS();
3404 }
3405 return 0;
Andy Adamson2db134e2009-04-03 08:27:55 +03003406}
3407
3408static __be32
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003409nfsd4_encode_destroy_session(struct nfsd4_compoundres *resp, __be32 nfserr,
Andy Adamson2db134e2009-04-03 08:27:55 +03003410 struct nfsd4_destroy_session *destroy_session)
3411{
Andy Adamson2db134e2009-04-03 08:27:55 +03003412 return nfserr;
3413}
3414
Daniel Mackc47d8322011-05-16 16:38:14 +02003415static __be32
J. Bruce Fieldsd1829b32012-04-25 18:04:54 -04003416nfsd4_encode_free_stateid(struct nfsd4_compoundres *resp, __be32 nfserr,
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003417 struct nfsd4_free_stateid *free_stateid)
3418{
3419 __be32 *p;
3420
3421 if (nfserr)
3422 return nfserr;
3423
3424 RESERVE_SPACE(4);
J. Bruce Fieldsd1829b32012-04-25 18:04:54 -04003425 *p++ = nfserr;
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003426 ADJUST_ARGS();
3427 return nfserr;
3428}
3429
3430static __be32
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003431nfsd4_encode_sequence(struct nfsd4_compoundres *resp, __be32 nfserr,
Andy Adamson2db134e2009-04-03 08:27:55 +03003432 struct nfsd4_sequence *seq)
3433{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003434 __be32 *p;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003435
3436 if (nfserr)
3437 return nfserr;
3438
3439 RESERVE_SPACE(NFS4_MAX_SESSIONID_LEN + 20);
3440 WRITEMEM(seq->sessionid.data, NFS4_MAX_SESSIONID_LEN);
3441 WRITE32(seq->seqid);
3442 WRITE32(seq->slotid);
J. Bruce Fieldsb7d7ca32011-08-31 15:39:30 -04003443 /* Note slotid's are numbered from zero: */
3444 WRITE32(seq->maxslots - 1); /* sr_highest_slotid */
3445 WRITE32(seq->maxslots - 1); /* sr_target_highest_slotid */
J. Bruce Fields0d7bb712010-11-18 08:30:33 -05003446 WRITE32(seq->status_flags);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003447
3448 ADJUST_ARGS();
Andy Adamson557ce262009-08-28 08:45:04 -04003449 resp->cstate.datap = p; /* DRC cache data pointer */
Benny Halevyb85d4c02009-04-03 08:28:08 +03003450 return 0;
Andy Adamson2db134e2009-04-03 08:27:55 +03003451}
3452
J. Bruce Fields2355c592012-04-25 16:56:22 -04003453static __be32
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003454nfsd4_encode_test_stateid(struct nfsd4_compoundres *resp, __be32 nfserr,
Bryan Schumaker17456802011-07-13 10:50:48 -04003455 struct nfsd4_test_stateid *test_stateid)
3456{
Bryan Schumaker03cfb422012-01-27 10:22:49 -05003457 struct nfsd4_test_stateid_id *stateid, *next;
Bryan Schumaker17456802011-07-13 10:50:48 -04003458 __be32 *p;
Bryan Schumaker17456802011-07-13 10:50:48 -04003459
Bryan Schumaker03cfb422012-01-27 10:22:49 -05003460 RESERVE_SPACE(4 + (4 * test_stateid->ts_num_ids));
Bryan Schumaker17456802011-07-13 10:50:48 -04003461 *p++ = htonl(test_stateid->ts_num_ids);
Bryan Schumaker17456802011-07-13 10:50:48 -04003462
Bryan Schumaker03cfb422012-01-27 10:22:49 -05003463 list_for_each_entry_safe(stateid, next, &test_stateid->ts_stateid_list, ts_id_list) {
Al Viro02f5fde2012-04-13 00:10:34 -04003464 *p++ = stateid->ts_id_status;
Bryan Schumaker17456802011-07-13 10:50:48 -04003465 }
Bryan Schumaker17456802011-07-13 10:50:48 -04003466
Bryan Schumaker03cfb422012-01-27 10:22:49 -05003467 ADJUST_ARGS();
Bryan Schumaker17456802011-07-13 10:50:48 -04003468 return nfserr;
3469}
3470
Andy Adamson2db134e2009-04-03 08:27:55 +03003471static __be32
Benny Halevy695e12f2008-07-04 14:38:33 +03003472nfsd4_encode_noop(struct nfsd4_compoundres *resp, __be32 nfserr, void *p)
3473{
3474 return nfserr;
3475}
3476
3477typedef __be32(* nfsd4_enc)(struct nfsd4_compoundres *, __be32, void *);
3478
Andy Adamson2db134e2009-04-03 08:27:55 +03003479/*
3480 * Note: nfsd4_enc_ops vector is shared for v4.0 and v4.1
3481 * since we don't need to filter out obsolete ops as this is
3482 * done in the decoding phase.
3483 */
Benny Halevy695e12f2008-07-04 14:38:33 +03003484static nfsd4_enc nfsd4_enc_ops[] = {
J. Bruce Fieldsad1060c2008-07-18 15:04:16 -04003485 [OP_ACCESS] = (nfsd4_enc)nfsd4_encode_access,
3486 [OP_CLOSE] = (nfsd4_enc)nfsd4_encode_close,
3487 [OP_COMMIT] = (nfsd4_enc)nfsd4_encode_commit,
3488 [OP_CREATE] = (nfsd4_enc)nfsd4_encode_create,
3489 [OP_DELEGPURGE] = (nfsd4_enc)nfsd4_encode_noop,
3490 [OP_DELEGRETURN] = (nfsd4_enc)nfsd4_encode_noop,
3491 [OP_GETATTR] = (nfsd4_enc)nfsd4_encode_getattr,
3492 [OP_GETFH] = (nfsd4_enc)nfsd4_encode_getfh,
3493 [OP_LINK] = (nfsd4_enc)nfsd4_encode_link,
3494 [OP_LOCK] = (nfsd4_enc)nfsd4_encode_lock,
3495 [OP_LOCKT] = (nfsd4_enc)nfsd4_encode_lockt,
3496 [OP_LOCKU] = (nfsd4_enc)nfsd4_encode_locku,
3497 [OP_LOOKUP] = (nfsd4_enc)nfsd4_encode_noop,
3498 [OP_LOOKUPP] = (nfsd4_enc)nfsd4_encode_noop,
3499 [OP_NVERIFY] = (nfsd4_enc)nfsd4_encode_noop,
3500 [OP_OPEN] = (nfsd4_enc)nfsd4_encode_open,
Benny Halevy84f09f42009-03-04 23:05:35 +02003501 [OP_OPENATTR] = (nfsd4_enc)nfsd4_encode_noop,
J. Bruce Fieldsad1060c2008-07-18 15:04:16 -04003502 [OP_OPEN_CONFIRM] = (nfsd4_enc)nfsd4_encode_open_confirm,
3503 [OP_OPEN_DOWNGRADE] = (nfsd4_enc)nfsd4_encode_open_downgrade,
3504 [OP_PUTFH] = (nfsd4_enc)nfsd4_encode_noop,
3505 [OP_PUTPUBFH] = (nfsd4_enc)nfsd4_encode_noop,
3506 [OP_PUTROOTFH] = (nfsd4_enc)nfsd4_encode_noop,
3507 [OP_READ] = (nfsd4_enc)nfsd4_encode_read,
3508 [OP_READDIR] = (nfsd4_enc)nfsd4_encode_readdir,
3509 [OP_READLINK] = (nfsd4_enc)nfsd4_encode_readlink,
3510 [OP_REMOVE] = (nfsd4_enc)nfsd4_encode_remove,
3511 [OP_RENAME] = (nfsd4_enc)nfsd4_encode_rename,
3512 [OP_RENEW] = (nfsd4_enc)nfsd4_encode_noop,
3513 [OP_RESTOREFH] = (nfsd4_enc)nfsd4_encode_noop,
3514 [OP_SAVEFH] = (nfsd4_enc)nfsd4_encode_noop,
3515 [OP_SECINFO] = (nfsd4_enc)nfsd4_encode_secinfo,
3516 [OP_SETATTR] = (nfsd4_enc)nfsd4_encode_setattr,
3517 [OP_SETCLIENTID] = (nfsd4_enc)nfsd4_encode_setclientid,
3518 [OP_SETCLIENTID_CONFIRM] = (nfsd4_enc)nfsd4_encode_noop,
3519 [OP_VERIFY] = (nfsd4_enc)nfsd4_encode_noop,
3520 [OP_WRITE] = (nfsd4_enc)nfsd4_encode_write,
3521 [OP_RELEASE_LOCKOWNER] = (nfsd4_enc)nfsd4_encode_noop,
Andy Adamson2db134e2009-04-03 08:27:55 +03003522
3523 /* NFSv4.1 operations */
3524 [OP_BACKCHANNEL_CTL] = (nfsd4_enc)nfsd4_encode_noop,
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003525 [OP_BIND_CONN_TO_SESSION] = (nfsd4_enc)nfsd4_encode_bind_conn_to_session,
Andy Adamson2db134e2009-04-03 08:27:55 +03003526 [OP_EXCHANGE_ID] = (nfsd4_enc)nfsd4_encode_exchange_id,
3527 [OP_CREATE_SESSION] = (nfsd4_enc)nfsd4_encode_create_session,
3528 [OP_DESTROY_SESSION] = (nfsd4_enc)nfsd4_encode_destroy_session,
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003529 [OP_FREE_STATEID] = (nfsd4_enc)nfsd4_encode_free_stateid,
Andy Adamson2db134e2009-04-03 08:27:55 +03003530 [OP_GET_DIR_DELEGATION] = (nfsd4_enc)nfsd4_encode_noop,
3531 [OP_GETDEVICEINFO] = (nfsd4_enc)nfsd4_encode_noop,
3532 [OP_GETDEVICELIST] = (nfsd4_enc)nfsd4_encode_noop,
3533 [OP_LAYOUTCOMMIT] = (nfsd4_enc)nfsd4_encode_noop,
3534 [OP_LAYOUTGET] = (nfsd4_enc)nfsd4_encode_noop,
3535 [OP_LAYOUTRETURN] = (nfsd4_enc)nfsd4_encode_noop,
Mi Jinlong22b6dee2010-12-27 14:29:57 +08003536 [OP_SECINFO_NO_NAME] = (nfsd4_enc)nfsd4_encode_secinfo_no_name,
Andy Adamson2db134e2009-04-03 08:27:55 +03003537 [OP_SEQUENCE] = (nfsd4_enc)nfsd4_encode_sequence,
3538 [OP_SET_SSV] = (nfsd4_enc)nfsd4_encode_noop,
Bryan Schumaker17456802011-07-13 10:50:48 -04003539 [OP_TEST_STATEID] = (nfsd4_enc)nfsd4_encode_test_stateid,
Andy Adamson2db134e2009-04-03 08:27:55 +03003540 [OP_WANT_DELEGATION] = (nfsd4_enc)nfsd4_encode_noop,
3541 [OP_DESTROY_CLIENTID] = (nfsd4_enc)nfsd4_encode_noop,
3542 [OP_RECLAIM_COMPLETE] = (nfsd4_enc)nfsd4_encode_noop,
Benny Halevy695e12f2008-07-04 14:38:33 +03003543};
3544
Andy Adamson496c2622009-04-03 08:28:48 +03003545/*
3546 * Calculate the total amount of memory that the compound response has taken
Mi Jinlong58e7b332011-08-28 18:18:56 +08003547 * after encoding the current operation with pad.
Andy Adamson496c2622009-04-03 08:28:48 +03003548 *
Mi Jinlong58e7b332011-08-28 18:18:56 +08003549 * pad: if operation is non-idempotent, pad was calculate by op_rsize_bop()
3550 * which was specified at nfsd4_operation, else pad is zero.
Andy Adamson496c2622009-04-03 08:28:48 +03003551 *
Mi Jinlong58e7b332011-08-28 18:18:56 +08003552 * Compare this length to the session se_fmaxresp_sz and se_fmaxresp_cached.
Andy Adamson496c2622009-04-03 08:28:48 +03003553 *
3554 * Our se_fmaxresp_cached will always be a multiple of PAGE_SIZE, and so
3555 * will be at least a page and will therefore hold the xdr_buf head.
3556 */
J. Bruce Fields57b7b432012-04-25 17:58:50 -04003557__be32 nfsd4_check_resp_size(struct nfsd4_compoundres *resp, u32 pad)
Andy Adamson496c2622009-04-03 08:28:48 +03003558{
Andy Adamson496c2622009-04-03 08:28:48 +03003559 struct xdr_buf *xb = &resp->rqstp->rq_res;
Andy Adamson496c2622009-04-03 08:28:48 +03003560 struct nfsd4_session *session = NULL;
3561 struct nfsd4_slot *slot = resp->cstate.slot;
Mi Jinlong58e7b332011-08-28 18:18:56 +08003562 u32 length, tlen = 0;
Andy Adamson496c2622009-04-03 08:28:48 +03003563
3564 if (!nfsd4_has_session(&resp->cstate))
Mi Jinlong58e7b332011-08-28 18:18:56 +08003565 return 0;
Andy Adamson496c2622009-04-03 08:28:48 +03003566
3567 session = resp->cstate.session;
Mi Jinlong58e7b332011-08-28 18:18:56 +08003568 if (session == NULL)
3569 return 0;
Andy Adamson496c2622009-04-03 08:28:48 +03003570
3571 if (xb->page_len == 0) {
3572 length = (char *)resp->p - (char *)xb->head[0].iov_base + pad;
3573 } else {
3574 if (xb->tail[0].iov_base && xb->tail[0].iov_len > 0)
3575 tlen = (char *)resp->p - (char *)xb->tail[0].iov_base;
3576
3577 length = xb->head[0].iov_len + xb->page_len + tlen + pad;
3578 }
3579 dprintk("%s length %u, xb->page_len %u tlen %u pad %u\n", __func__,
3580 length, xb->page_len, tlen, pad);
3581
Mi Jinlong58e7b332011-08-28 18:18:56 +08003582 if (length > session->se_fchannel.maxresp_sz)
3583 return nfserr_rep_too_big;
3584
J. Bruce Fields73e79482012-02-13 16:39:00 -05003585 if ((slot->sl_flags & NFSD4_SLOT_CACHETHIS) &&
Mi Jinlong58e7b332011-08-28 18:18:56 +08003586 length > session->se_fchannel.maxresp_cached)
Andy Adamson496c2622009-04-03 08:28:48 +03003587 return nfserr_rep_too_big_to_cache;
Mi Jinlong58e7b332011-08-28 18:18:56 +08003588
3589 return 0;
Andy Adamson496c2622009-04-03 08:28:48 +03003590}
3591
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592void
3593nfsd4_encode_operation(struct nfsd4_compoundres *resp, struct nfsd4_op *op)
3594{
Al Viro2ebbc012006-10-19 23:28:58 -07003595 __be32 *statp;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003596 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597
3598 RESERVE_SPACE(8);
3599 WRITE32(op->opnum);
3600 statp = p++; /* to be backfilled at the end */
3601 ADJUST_ARGS();
3602
Benny Halevy695e12f2008-07-04 14:38:33 +03003603 if (op->opnum == OP_ILLEGAL)
3604 goto status;
3605 BUG_ON(op->opnum < 0 || op->opnum >= ARRAY_SIZE(nfsd4_enc_ops) ||
3606 !nfsd4_enc_ops[op->opnum]);
3607 op->status = nfsd4_enc_ops[op->opnum](resp, op->status, &op->u);
Andy Adamson496c2622009-04-03 08:28:48 +03003608 /* nfsd4_check_drc_limit guarantees enough room for error status */
Mi Jinlong58e7b332011-08-28 18:18:56 +08003609 if (!op->status)
3610 op->status = nfsd4_check_resp_size(resp, 0);
Benny Halevy695e12f2008-07-04 14:38:33 +03003611status:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612 /*
3613 * Note: We write the status directly, instead of using WRITE32(),
3614 * since it is already in network byte order.
3615 */
3616 *statp = op->status;
3617}
3618
3619/*
3620 * Encode the reply stored in the stateowner reply cache
3621 *
3622 * XDR note: do not encode rp->rp_buflen: the buffer contains the
3623 * previously sent already encoded operation.
3624 *
3625 * called with nfs4_lock_state() held
3626 */
3627void
3628nfsd4_encode_replay(struct nfsd4_compoundres *resp, struct nfsd4_op *op)
3629{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003630 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631 struct nfs4_replay *rp = op->replay;
3632
3633 BUG_ON(!rp);
3634
3635 RESERVE_SPACE(8);
3636 WRITE32(op->opnum);
3637 *p++ = rp->rp_status; /* already xdr'ed */
3638 ADJUST_ARGS();
3639
3640 RESERVE_SPACE(rp->rp_buflen);
3641 WRITEMEM(rp->rp_buf, rp->rp_buflen);
3642 ADJUST_ARGS();
3643}
3644
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645int
Al Viro2ebbc012006-10-19 23:28:58 -07003646nfs4svc_encode_voidres(struct svc_rqst *rqstp, __be32 *p, void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647{
3648 return xdr_ressize_check(rqstp, p);
3649}
3650
J. Bruce Fields3e98abf2011-07-16 17:15:10 -04003651int nfsd4_release_compoundargs(void *rq, __be32 *p, void *resp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652{
J. Bruce Fields3e98abf2011-07-16 17:15:10 -04003653 struct svc_rqst *rqstp = rq;
3654 struct nfsd4_compoundargs *args = rqstp->rq_argp;
3655
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656 if (args->ops != args->iops) {
3657 kfree(args->ops);
3658 args->ops = args->iops;
3659 }
Jesper Juhlf99d49a2005-11-07 01:01:34 -08003660 kfree(args->tmpp);
3661 args->tmpp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662 while (args->to_free) {
3663 struct tmpbuf *tb = args->to_free;
3664 args->to_free = tb->next;
3665 tb->release(tb->buf);
3666 kfree(tb);
3667 }
J. Bruce Fields3e98abf2011-07-16 17:15:10 -04003668 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669}
3670
3671int
Al Viro2ebbc012006-10-19 23:28:58 -07003672nfs4svc_decode_compoundargs(struct svc_rqst *rqstp, __be32 *p, struct nfsd4_compoundargs *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674 args->p = p;
3675 args->end = rqstp->rq_arg.head[0].iov_base + rqstp->rq_arg.head[0].iov_len;
3676 args->pagelist = rqstp->rq_arg.pages;
3677 args->pagelen = rqstp->rq_arg.page_len;
3678 args->tmpp = NULL;
3679 args->to_free = NULL;
3680 args->ops = args->iops;
3681 args->rqstp = rqstp;
3682
J. Bruce Fields3e98abf2011-07-16 17:15:10 -04003683 return !nfsd4_decode_compound(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684}
3685
3686int
Al Viro2ebbc012006-10-19 23:28:58 -07003687nfs4svc_encode_compoundres(struct svc_rqst *rqstp, __be32 *p, struct nfsd4_compoundres *resp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688{
3689 /*
3690 * All that remains is to write the tag and operation count...
3691 */
Andy Adamson557ce262009-08-28 08:45:04 -04003692 struct nfsd4_compound_state *cs = &resp->cstate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693 struct kvec *iov;
3694 p = resp->tagp;
3695 *p++ = htonl(resp->taglen);
3696 memcpy(p, resp->tag, resp->taglen);
3697 p += XDR_QUADLEN(resp->taglen);
3698 *p++ = htonl(resp->opcnt);
3699
3700 if (rqstp->rq_res.page_len)
3701 iov = &rqstp->rq_res.tail[0];
3702 else
3703 iov = &rqstp->rq_res.head[0];
3704 iov->iov_len = ((char*)resp->p) - (char*)iov->iov_base;
3705 BUG_ON(iov->iov_len > PAGE_SIZE);
J. Bruce Fields26c0c752010-04-24 15:35:43 -04003706 if (nfsd4_has_session(cs)) {
3707 if (cs->status != nfserr_replay_cache) {
3708 nfsd4_store_cache_entry(resp);
J. Bruce Fields73e79482012-02-13 16:39:00 -05003709 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
J. Bruce Fields26c0c752010-04-24 15:35:43 -04003710 }
Benny Halevyd7682982010-05-12 00:13:54 +03003711 /* Renew the clientid on success and on replay */
3712 release_session_client(cs->session);
J. Bruce Fields76407f72010-06-22 14:10:14 -04003713 nfsd4_put_session(cs->session);
Andy Adamsonda3846a2009-04-03 08:28:22 +03003714 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715 return 1;
3716}
3717
3718/*
3719 * Local variables:
3720 * c-basic-offset: 8
3721 * End:
3722 */