blob: d241a8571ddfd957d894fcfe6563c6ecc46beb6a [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
Bryan Schumaker17456802011-07-13 10:50:48 -0400136static void save_buf(struct nfsd4_compoundargs *argp, struct nfsd4_saved_compoundargs *savep)
137{
138 savep->p = argp->p;
139 savep->end = argp->end;
140 savep->pagelen = argp->pagelen;
141 savep->pagelist = argp->pagelist;
142}
143
144static void restore_buf(struct nfsd4_compoundargs *argp, struct nfsd4_saved_compoundargs *savep)
145{
146 argp->p = savep->p;
147 argp->end = savep->end;
148 argp->pagelen = savep->pagelen;
149 argp->pagelist = savep->pagelist;
150}
151
J. Bruce Fieldsca2a05a2007-11-11 15:43:12 -0500152static __be32 *read_buf(struct nfsd4_compoundargs *argp, u32 nbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
154 /* We want more bytes than seem to be available.
155 * Maybe we need a new page, maybe we have just run out
156 */
J. Bruce Fieldsca2a05a2007-11-11 15:43:12 -0500157 unsigned int avail = (char *)argp->end - (char *)argp->p;
Al Viro2ebbc012006-10-19 23:28:58 -0700158 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 if (avail + argp->pagelen < nbytes)
160 return NULL;
161 if (avail + PAGE_SIZE < nbytes) /* need more than a page !! */
162 return NULL;
163 /* ok, we can do it with the current plus the next page */
164 if (nbytes <= sizeof(argp->tmp))
165 p = argp->tmp;
166 else {
Jesper Juhlf99d49a2005-11-07 01:01:34 -0800167 kfree(argp->tmpp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 p = argp->tmpp = kmalloc(nbytes, GFP_KERNEL);
169 if (!p)
170 return NULL;
171
172 }
J. Bruce Fieldsca2a05a2007-11-11 15:43:12 -0500173 /*
174 * The following memcpy is safe because read_buf is always
175 * called with nbytes > avail, and the two cases above both
176 * guarantee p points to at least nbytes bytes.
177 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 memcpy(p, argp->p, avail);
179 /* step to next page */
180 argp->p = page_address(argp->pagelist[0]);
181 argp->pagelist++;
182 if (argp->pagelen < PAGE_SIZE) {
Neil Brown2bc3c112010-04-20 12:16:52 +1000183 argp->end = argp->p + (argp->pagelen>>2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 argp->pagelen = 0;
185 } else {
Neil Brown2bc3c112010-04-20 12:16:52 +1000186 argp->end = argp->p + (PAGE_SIZE>>2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 argp->pagelen -= PAGE_SIZE;
188 }
189 memcpy(((char*)p)+avail, argp->p, (nbytes - avail));
190 argp->p += XDR_QUADLEN(nbytes - avail);
191 return p;
192}
193
Andy Adamson60adfc52009-04-03 08:28:50 +0300194static int zero_clientid(clientid_t *clid)
195{
196 return (clid->cl_boot == 0) && (clid->cl_id == 0);
197}
198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199static int
200defer_free(struct nfsd4_compoundargs *argp,
201 void (*release)(const void *), void *p)
202{
203 struct tmpbuf *tb;
204
205 tb = kmalloc(sizeof(*tb), GFP_KERNEL);
206 if (!tb)
207 return -ENOMEM;
208 tb->buf = p;
209 tb->release = release;
210 tb->next = argp->to_free;
211 argp->to_free = tb;
212 return 0;
213}
214
Al Viro2ebbc012006-10-19 23:28:58 -0700215static char *savemem(struct nfsd4_compoundargs *argp, __be32 *p, int nbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 if (p == argp->tmp) {
Thomas Meyer67114fe2011-11-17 23:43:40 +0100218 p = kmemdup(argp->tmp, nbytes, GFP_KERNEL);
J. Bruce Fieldsa4db5fe2007-02-16 01:28:30 -0800219 if (!p)
220 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 } else {
Eric Sesterhenn73dff8b2006-10-03 23:37:14 +0200222 BUG_ON(p != argp->tmpp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 argp->tmpp = NULL;
224 }
225 if (defer_free(argp, kfree, p)) {
J. Bruce Fieldsa4db5fe2007-02-16 01:28:30 -0800226 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 return NULL;
228 } else
229 return (char *)p;
230}
231
Al Virob37ad282006-10-19 23:28:59 -0700232static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233nfsd4_decode_bitmap(struct nfsd4_compoundargs *argp, u32 *bmval)
234{
235 u32 bmlen;
236 DECODE_HEAD;
237
238 bmval[0] = 0;
239 bmval[1] = 0;
Andy Adamson7e705702009-04-03 08:29:11 +0300240 bmval[2] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
242 READ_BUF(4);
243 READ32(bmlen);
244 if (bmlen > 1000)
245 goto xdr_error;
246
247 READ_BUF(bmlen << 2);
248 if (bmlen > 0)
249 READ32(bmval[0]);
250 if (bmlen > 1)
251 READ32(bmval[1]);
Andy Adamson7e705702009-04-03 08:29:11 +0300252 if (bmlen > 2)
253 READ32(bmval[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
255 DECODE_TAIL;
256}
257
Al Virob37ad282006-10-19 23:28:59 -0700258static __be32
Yu Zhiguo3c8e0312009-05-16 16:22:31 +0800259nfsd4_decode_fattr(struct nfsd4_compoundargs *argp, u32 *bmval,
Benny Halevyc0d6fc82009-04-03 08:29:05 +0300260 struct iattr *iattr, struct nfs4_acl **acl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
262 int expected_len, len = 0;
263 u32 dummy32;
264 char *buf;
Al Virob8dd7b92006-10-19 23:29:01 -0700265 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
267 DECODE_HEAD;
268 iattr->ia_valid = 0;
269 if ((status = nfsd4_decode_bitmap(argp, bmval)))
270 return status;
271
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 READ_BUF(4);
273 READ32(expected_len);
274
275 if (bmval[0] & FATTR4_WORD0_SIZE) {
276 READ_BUF(8);
277 len += 8;
278 READ64(iattr->ia_size);
279 iattr->ia_valid |= ATTR_SIZE;
280 }
281 if (bmval[0] & FATTR4_WORD0_ACL) {
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800282 int nace;
283 struct nfs4_ace *ace;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285 READ_BUF(4); len += 4;
286 READ32(nace);
287
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800288 if (nace > NFS4_ACL_MAX)
289 return nfserr_resource;
290
291 *acl = nfs4_acl_new(nace);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 if (*acl == NULL) {
Al Virob8dd7b92006-10-19 23:29:01 -0700293 host_err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 goto out_nfserr;
295 }
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800296 defer_free(argp, kfree, *acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800298 (*acl)->naces = nace;
299 for (ace = (*acl)->aces; ace < (*acl)->aces + nace; ace++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 READ_BUF(16); len += 16;
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800301 READ32(ace->type);
302 READ32(ace->flag);
303 READ32(ace->access_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 READ32(dummy32);
305 READ_BUF(dummy32);
306 len += XDR_QUADLEN(dummy32) << 2;
307 READMEM(buf, dummy32);
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800308 ace->whotype = nfs4_acl_get_whotype(buf, dummy32);
J. Bruce Fields3c726022011-01-04 17:53:52 -0500309 status = nfs_ok;
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800310 if (ace->whotype != NFS4_ACL_WHO_NAMED)
311 ace->who = 0;
312 else if (ace->flag & NFS4_ACE_IDENTIFIER_GROUP)
J. Bruce Fields3c726022011-01-04 17:53:52 -0500313 status = nfsd_map_name_to_gid(argp->rqstp,
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800314 buf, dummy32, &ace->who);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 else
J. Bruce Fields3c726022011-01-04 17:53:52 -0500316 status = nfsd_map_name_to_uid(argp->rqstp,
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800317 buf, dummy32, &ace->who);
J. Bruce Fields3c726022011-01-04 17:53:52 -0500318 if (status)
319 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 }
321 } else
322 *acl = NULL;
323 if (bmval[1] & FATTR4_WORD1_MODE) {
324 READ_BUF(4);
325 len += 4;
326 READ32(iattr->ia_mode);
327 iattr->ia_mode &= (S_IFMT | S_IALLUGO);
328 iattr->ia_valid |= ATTR_MODE;
329 }
330 if (bmval[1] & FATTR4_WORD1_OWNER) {
331 READ_BUF(4);
332 len += 4;
333 READ32(dummy32);
334 READ_BUF(dummy32);
335 len += (XDR_QUADLEN(dummy32) << 2);
336 READMEM(buf, dummy32);
NeilBrown47c85292011-02-16 13:08:35 +1100337 if ((status = nfsd_map_name_to_uid(argp->rqstp, buf, dummy32, &iattr->ia_uid)))
338 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 iattr->ia_valid |= ATTR_UID;
340 }
341 if (bmval[1] & FATTR4_WORD1_OWNER_GROUP) {
342 READ_BUF(4);
343 len += 4;
344 READ32(dummy32);
345 READ_BUF(dummy32);
346 len += (XDR_QUADLEN(dummy32) << 2);
347 READMEM(buf, dummy32);
NeilBrown47c85292011-02-16 13:08:35 +1100348 if ((status = nfsd_map_name_to_gid(argp->rqstp, buf, dummy32, &iattr->ia_gid)))
349 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 iattr->ia_valid |= ATTR_GID;
351 }
352 if (bmval[1] & FATTR4_WORD1_TIME_ACCESS_SET) {
353 READ_BUF(4);
354 len += 4;
355 READ32(dummy32);
356 switch (dummy32) {
357 case NFS4_SET_TO_CLIENT_TIME:
358 /* We require the high 32 bits of 'seconds' to be 0, and we ignore
359 all 32 bits of 'nseconds'. */
360 READ_BUF(12);
361 len += 12;
362 READ32(dummy32);
363 if (dummy32)
364 return nfserr_inval;
365 READ32(iattr->ia_atime.tv_sec);
366 READ32(iattr->ia_atime.tv_nsec);
367 if (iattr->ia_atime.tv_nsec >= (u32)1000000000)
368 return nfserr_inval;
369 iattr->ia_valid |= (ATTR_ATIME | ATTR_ATIME_SET);
370 break;
371 case NFS4_SET_TO_SERVER_TIME:
372 iattr->ia_valid |= ATTR_ATIME;
373 break;
374 default:
375 goto xdr_error;
376 }
377 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 if (bmval[1] & FATTR4_WORD1_TIME_MODIFY_SET) {
379 READ_BUF(4);
380 len += 4;
381 READ32(dummy32);
382 switch (dummy32) {
383 case NFS4_SET_TO_CLIENT_TIME:
384 /* We require the high 32 bits of 'seconds' to be 0, and we ignore
385 all 32 bits of 'nseconds'. */
386 READ_BUF(12);
387 len += 12;
388 READ32(dummy32);
389 if (dummy32)
390 return nfserr_inval;
391 READ32(iattr->ia_mtime.tv_sec);
392 READ32(iattr->ia_mtime.tv_nsec);
393 if (iattr->ia_mtime.tv_nsec >= (u32)1000000000)
394 return nfserr_inval;
395 iattr->ia_valid |= (ATTR_MTIME | ATTR_MTIME_SET);
396 break;
397 case NFS4_SET_TO_SERVER_TIME:
398 iattr->ia_valid |= ATTR_MTIME;
399 break;
400 default:
401 goto xdr_error;
402 }
403 }
Yu Zhiguo3c8e0312009-05-16 16:22:31 +0800404 if (bmval[0] & ~NFSD_WRITEABLE_ATTRS_WORD0
405 || bmval[1] & ~NFSD_WRITEABLE_ATTRS_WORD1
406 || bmval[2] & ~NFSD_WRITEABLE_ATTRS_WORD2)
407 READ_BUF(expected_len - len);
408 else if (len != expected_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 goto xdr_error;
410
411 DECODE_TAIL;
412
413out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -0700414 status = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 goto out;
416}
417
Al Virob37ad282006-10-19 23:28:59 -0700418static __be32
Benny Halevye31a1b62008-08-12 20:46:18 +0300419nfsd4_decode_stateid(struct nfsd4_compoundargs *argp, stateid_t *sid)
420{
421 DECODE_HEAD;
422
423 READ_BUF(sizeof(stateid_t));
424 READ32(sid->si_generation);
425 COPYMEM(&sid->si_opaque, sizeof(stateid_opaque_t));
426
427 DECODE_TAIL;
428}
429
430static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431nfsd4_decode_access(struct nfsd4_compoundargs *argp, struct nfsd4_access *access)
432{
433 DECODE_HEAD;
434
435 READ_BUF(4);
436 READ32(access->ac_req_access);
437
438 DECODE_TAIL;
439}
440
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -0400441static __be32 nfsd4_decode_bind_conn_to_session(struct nfsd4_compoundargs *argp, struct nfsd4_bind_conn_to_session *bcts)
442{
443 DECODE_HEAD;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -0400444
445 READ_BUF(NFS4_MAX_SESSIONID_LEN + 8);
446 COPYMEM(bcts->sessionid.data, NFS4_MAX_SESSIONID_LEN);
447 READ32(bcts->dir);
Bryan Schumaker6ce23572011-04-27 15:47:16 -0400448 /* XXX: skipping ctsa_use_conn_in_rdma_mode. Perhaps Tom Tucker
449 * could help us figure out we should be using it. */
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -0400450 DECODE_TAIL;
451}
452
Al Virob37ad282006-10-19 23:28:59 -0700453static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454nfsd4_decode_close(struct nfsd4_compoundargs *argp, struct nfsd4_close *close)
455{
456 DECODE_HEAD;
457
Benny Halevye31a1b62008-08-12 20:46:18 +0300458 READ_BUF(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 READ32(close->cl_seqid);
Benny Halevye31a1b62008-08-12 20:46:18 +0300460 return nfsd4_decode_stateid(argp, &close->cl_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
462 DECODE_TAIL;
463}
464
465
Al Virob37ad282006-10-19 23:28:59 -0700466static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467nfsd4_decode_commit(struct nfsd4_compoundargs *argp, struct nfsd4_commit *commit)
468{
469 DECODE_HEAD;
470
471 READ_BUF(12);
472 READ64(commit->co_offset);
473 READ32(commit->co_count);
474
475 DECODE_TAIL;
476}
477
Al Virob37ad282006-10-19 23:28:59 -0700478static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479nfsd4_decode_create(struct nfsd4_compoundargs *argp, struct nfsd4_create *create)
480{
481 DECODE_HEAD;
482
483 READ_BUF(4);
484 READ32(create->cr_type);
485 switch (create->cr_type) {
486 case NF4LNK:
487 READ_BUF(4);
488 READ32(create->cr_linklen);
489 READ_BUF(create->cr_linklen);
490 SAVEMEM(create->cr_linkname, create->cr_linklen);
491 break;
492 case NF4BLK:
493 case NF4CHR:
494 READ_BUF(8);
495 READ32(create->cr_specdata1);
496 READ32(create->cr_specdata2);
497 break;
498 case NF4SOCK:
499 case NF4FIFO:
500 case NF4DIR:
501 default:
502 break;
503 }
504
505 READ_BUF(4);
506 READ32(create->cr_namelen);
507 READ_BUF(create->cr_namelen);
508 SAVEMEM(create->cr_name, create->cr_namelen);
509 if ((status = check_filename(create->cr_name, create->cr_namelen, nfserr_inval)))
510 return status;
511
Yu Zhiguo3c8e0312009-05-16 16:22:31 +0800512 status = nfsd4_decode_fattr(argp, create->cr_bmval, &create->cr_iattr,
513 &create->cr_acl);
Benny Halevyc0d6fc82009-04-03 08:29:05 +0300514 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 goto out;
516
517 DECODE_TAIL;
518}
519
Al Virob37ad282006-10-19 23:28:59 -0700520static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521nfsd4_decode_delegreturn(struct nfsd4_compoundargs *argp, struct nfsd4_delegreturn *dr)
522{
Benny Halevye31a1b62008-08-12 20:46:18 +0300523 return nfsd4_decode_stateid(argp, &dr->dr_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524}
525
Al Virob37ad282006-10-19 23:28:59 -0700526static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527nfsd4_decode_getattr(struct nfsd4_compoundargs *argp, struct nfsd4_getattr *getattr)
528{
529 return nfsd4_decode_bitmap(argp, getattr->ga_bmval);
530}
531
Al Virob37ad282006-10-19 23:28:59 -0700532static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533nfsd4_decode_link(struct nfsd4_compoundargs *argp, struct nfsd4_link *link)
534{
535 DECODE_HEAD;
536
537 READ_BUF(4);
538 READ32(link->li_namelen);
539 READ_BUF(link->li_namelen);
540 SAVEMEM(link->li_name, link->li_namelen);
541 if ((status = check_filename(link->li_name, link->li_namelen, nfserr_inval)))
542 return status;
543
544 DECODE_TAIL;
545}
546
Al Virob37ad282006-10-19 23:28:59 -0700547static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548nfsd4_decode_lock(struct nfsd4_compoundargs *argp, struct nfsd4_lock *lock)
549{
550 DECODE_HEAD;
551
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 /*
553 * type, reclaim(boolean), offset, length, new_lock_owner(boolean)
554 */
555 READ_BUF(28);
556 READ32(lock->lk_type);
557 if ((lock->lk_type < NFS4_READ_LT) || (lock->lk_type > NFS4_WRITEW_LT))
558 goto xdr_error;
559 READ32(lock->lk_reclaim);
560 READ64(lock->lk_offset);
561 READ64(lock->lk_length);
562 READ32(lock->lk_is_new);
563
564 if (lock->lk_is_new) {
Benny Halevye31a1b62008-08-12 20:46:18 +0300565 READ_BUF(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 READ32(lock->lk_new_open_seqid);
Benny Halevye31a1b62008-08-12 20:46:18 +0300567 status = nfsd4_decode_stateid(argp, &lock->lk_new_open_stateid);
568 if (status)
569 return status;
570 READ_BUF(8 + sizeof(clientid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 READ32(lock->lk_new_lock_seqid);
572 COPYMEM(&lock->lk_new_clientid, sizeof(clientid_t));
573 READ32(lock->lk_new_owner.len);
574 READ_BUF(lock->lk_new_owner.len);
575 READMEM(lock->lk_new_owner.data, lock->lk_new_owner.len);
576 } else {
Benny Halevye31a1b62008-08-12 20:46:18 +0300577 status = nfsd4_decode_stateid(argp, &lock->lk_old_lock_stateid);
578 if (status)
579 return status;
580 READ_BUF(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 READ32(lock->lk_old_lock_seqid);
582 }
583
584 DECODE_TAIL;
585}
586
Al Virob37ad282006-10-19 23:28:59 -0700587static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588nfsd4_decode_lockt(struct nfsd4_compoundargs *argp, struct nfsd4_lockt *lockt)
589{
590 DECODE_HEAD;
591
592 READ_BUF(32);
593 READ32(lockt->lt_type);
594 if((lockt->lt_type < NFS4_READ_LT) || (lockt->lt_type > NFS4_WRITEW_LT))
595 goto xdr_error;
596 READ64(lockt->lt_offset);
597 READ64(lockt->lt_length);
598 COPYMEM(&lockt->lt_clientid, 8);
599 READ32(lockt->lt_owner.len);
600 READ_BUF(lockt->lt_owner.len);
601 READMEM(lockt->lt_owner.data, lockt->lt_owner.len);
602
603 DECODE_TAIL;
604}
605
Al Virob37ad282006-10-19 23:28:59 -0700606static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607nfsd4_decode_locku(struct nfsd4_compoundargs *argp, struct nfsd4_locku *locku)
608{
609 DECODE_HEAD;
610
Benny Halevye31a1b62008-08-12 20:46:18 +0300611 READ_BUF(8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 READ32(locku->lu_type);
613 if ((locku->lu_type < NFS4_READ_LT) || (locku->lu_type > NFS4_WRITEW_LT))
614 goto xdr_error;
615 READ32(locku->lu_seqid);
Benny Halevye31a1b62008-08-12 20:46:18 +0300616 status = nfsd4_decode_stateid(argp, &locku->lu_stateid);
617 if (status)
618 return status;
619 READ_BUF(16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 READ64(locku->lu_offset);
621 READ64(locku->lu_length);
622
623 DECODE_TAIL;
624}
625
Al Virob37ad282006-10-19 23:28:59 -0700626static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627nfsd4_decode_lookup(struct nfsd4_compoundargs *argp, struct nfsd4_lookup *lookup)
628{
629 DECODE_HEAD;
630
631 READ_BUF(4);
632 READ32(lookup->lo_len);
633 READ_BUF(lookup->lo_len);
634 SAVEMEM(lookup->lo_name, lookup->lo_len);
635 if ((status = check_filename(lookup->lo_name, lookup->lo_len, nfserr_noent)))
636 return status;
637
638 DECODE_TAIL;
639}
640
Benny Halevy2c8bd7e2012-02-16 20:57:09 +0200641static __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 -0400642{
643 __be32 *p;
644 u32 w;
645
646 READ_BUF(4);
647 READ32(w);
Benny Halevy2c8bd7e2012-02-16 20:57:09 +0200648 *share_access = w & NFS4_SHARE_ACCESS_MASK;
649 *deleg_want = w & NFS4_SHARE_WANT_MASK;
650 if (deleg_when)
651 *deleg_when = w & NFS4_SHARE_WHEN_MASK;
652
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400653 switch (w & NFS4_SHARE_ACCESS_MASK) {
654 case NFS4_SHARE_ACCESS_READ:
655 case NFS4_SHARE_ACCESS_WRITE:
656 case NFS4_SHARE_ACCESS_BOTH:
657 break;
658 default:
659 return nfserr_bad_xdr;
660 }
Benny Halevyfc0d14f2011-10-27 20:43:01 +0200661 w &= ~NFS4_SHARE_ACCESS_MASK;
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400662 if (!w)
663 return nfs_ok;
664 if (!argp->minorversion)
665 return nfserr_bad_xdr;
666 switch (w & NFS4_SHARE_WANT_MASK) {
667 case NFS4_SHARE_WANT_NO_PREFERENCE:
668 case NFS4_SHARE_WANT_READ_DELEG:
669 case NFS4_SHARE_WANT_WRITE_DELEG:
670 case NFS4_SHARE_WANT_ANY_DELEG:
671 case NFS4_SHARE_WANT_NO_DELEG:
672 case NFS4_SHARE_WANT_CANCEL:
673 break;
674 default:
675 return nfserr_bad_xdr;
676 }
Benny Halevy92bac8c2011-10-19 19:13:29 -0700677 w &= ~NFS4_SHARE_WANT_MASK;
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400678 if (!w)
679 return nfs_ok;
Benny Halevy2c8bd7e2012-02-16 20:57:09 +0200680
681 if (!deleg_when) /* open_downgrade */
682 return nfserr_inval;
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400683 switch (w) {
684 case NFS4_SHARE_SIGNAL_DELEG_WHEN_RESRC_AVAIL:
685 case NFS4_SHARE_PUSH_DELEG_WHEN_UNCONTENDED:
Benny Halevyc668fc62011-10-19 19:13:13 -0700686 case (NFS4_SHARE_SIGNAL_DELEG_WHEN_RESRC_AVAIL |
687 NFS4_SHARE_PUSH_DELEG_WHEN_UNCONTENDED):
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400688 return nfs_ok;
689 }
690xdr_error:
691 return nfserr_bad_xdr;
692}
693
694static __be32 nfsd4_decode_share_deny(struct nfsd4_compoundargs *argp, u32 *x)
695{
696 __be32 *p;
697
698 READ_BUF(4);
699 READ32(*x);
700 /* Note: unlinke access bits, deny bits may be zero. */
Dan Carpenter01cd4af2011-10-17 10:41:17 +0300701 if (*x & ~NFS4_SHARE_DENY_BOTH)
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400702 return nfserr_bad_xdr;
703 return nfs_ok;
704xdr_error:
705 return nfserr_bad_xdr;
706}
707
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -0400708static __be32 nfsd4_decode_opaque(struct nfsd4_compoundargs *argp, struct xdr_netobj *o)
709{
710 __be32 *p;
711
712 READ_BUF(4);
713 READ32(o->len);
714
715 if (o->len == 0 || o->len > NFS4_OPAQUE_LIMIT)
716 return nfserr_bad_xdr;
717
718 READ_BUF(o->len);
719 SAVEMEM(o->data, o->len);
720 return nfs_ok;
721xdr_error:
722 return nfserr_bad_xdr;
723}
724
Al Virob37ad282006-10-19 23:28:59 -0700725static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726nfsd4_decode_open(struct nfsd4_compoundargs *argp, struct nfsd4_open *open)
727{
728 DECODE_HEAD;
Benny Halevy2c8bd7e2012-02-16 20:57:09 +0200729 u32 dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
731 memset(open->op_bmval, 0, sizeof(open->op_bmval));
732 open->op_iattr.ia_valid = 0;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400733 open->op_openowner = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
735 /* seqid, share_access, share_deny, clientid, ownerlen */
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400736 READ_BUF(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 READ32(open->op_seqid);
Benny Halevy2c8bd7e2012-02-16 20:57:09 +0200738 /* decode, yet ignore deleg_when until supported */
739 status = nfsd4_decode_share_access(argp, &open->op_share_access,
740 &open->op_deleg_want, &dummy);
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400741 if (status)
742 goto xdr_error;
743 status = nfsd4_decode_share_deny(argp, &open->op_share_deny);
744 if (status)
745 goto xdr_error;
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -0400746 READ_BUF(sizeof(clientid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 COPYMEM(&open->op_clientid, sizeof(clientid_t));
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -0400748 status = nfsd4_decode_opaque(argp, &open->op_owner);
749 if (status)
750 goto xdr_error;
751 READ_BUF(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 READ32(open->op_create);
753 switch (open->op_create) {
754 case NFS4_OPEN_NOCREATE:
755 break;
756 case NFS4_OPEN_CREATE:
757 READ_BUF(4);
758 READ32(open->op_createmode);
759 switch (open->op_createmode) {
760 case NFS4_CREATE_UNCHECKED:
761 case NFS4_CREATE_GUARDED:
Benny Halevyc0d6fc82009-04-03 08:29:05 +0300762 status = nfsd4_decode_fattr(argp, open->op_bmval,
Yu Zhiguo3c8e0312009-05-16 16:22:31 +0800763 &open->op_iattr, &open->op_acl);
Benny Halevyc0d6fc82009-04-03 08:29:05 +0300764 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 goto out;
766 break;
767 case NFS4_CREATE_EXCLUSIVE:
768 READ_BUF(8);
769 COPYMEM(open->op_verf.data, 8);
770 break;
Benny Halevy79fb54a2009-04-03 08:29:17 +0300771 case NFS4_CREATE_EXCLUSIVE4_1:
772 if (argp->minorversion < 1)
773 goto xdr_error;
774 READ_BUF(8);
775 COPYMEM(open->op_verf.data, 8);
776 status = nfsd4_decode_fattr(argp, open->op_bmval,
Yu Zhiguo3c8e0312009-05-16 16:22:31 +0800777 &open->op_iattr, &open->op_acl);
Benny Halevy79fb54a2009-04-03 08:29:17 +0300778 if (status)
779 goto out;
780 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 default:
782 goto xdr_error;
783 }
784 break;
785 default:
786 goto xdr_error;
787 }
788
789 /* open_claim */
790 READ_BUF(4);
791 READ32(open->op_claim_type);
792 switch (open->op_claim_type) {
793 case NFS4_OPEN_CLAIM_NULL:
794 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
795 READ_BUF(4);
796 READ32(open->op_fname.len);
797 READ_BUF(open->op_fname.len);
798 SAVEMEM(open->op_fname.data, open->op_fname.len);
799 if ((status = check_filename(open->op_fname.data, open->op_fname.len, nfserr_inval)))
800 return status;
801 break;
802 case NFS4_OPEN_CLAIM_PREVIOUS:
803 READ_BUF(4);
804 READ32(open->op_delegate_type);
805 break;
806 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
Benny Halevye31a1b62008-08-12 20:46:18 +0300807 status = nfsd4_decode_stateid(argp, &open->op_delegate_stateid);
808 if (status)
809 return status;
810 READ_BUF(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 READ32(open->op_fname.len);
812 READ_BUF(open->op_fname.len);
813 SAVEMEM(open->op_fname.data, open->op_fname.len);
814 if ((status = check_filename(open->op_fname.data, open->op_fname.len, nfserr_inval)))
815 return status;
816 break;
J. Bruce Fields8b289b22011-10-19 11:52:12 -0400817 case NFS4_OPEN_CLAIM_FH:
818 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
819 if (argp->minorversion < 1)
820 goto xdr_error;
821 /* void */
822 break;
823 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
824 if (argp->minorversion < 1)
825 goto xdr_error;
826 status = nfsd4_decode_stateid(argp, &open->op_delegate_stateid);
827 if (status)
828 return status;
829 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 default:
831 goto xdr_error;
832 }
833
834 DECODE_TAIL;
835}
836
Al Virob37ad282006-10-19 23:28:59 -0700837static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838nfsd4_decode_open_confirm(struct nfsd4_compoundargs *argp, struct nfsd4_open_confirm *open_conf)
839{
840 DECODE_HEAD;
841
Benny Halevye31a1b62008-08-12 20:46:18 +0300842 status = nfsd4_decode_stateid(argp, &open_conf->oc_req_stateid);
843 if (status)
844 return status;
845 READ_BUF(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 READ32(open_conf->oc_seqid);
847
848 DECODE_TAIL;
849}
850
Al Virob37ad282006-10-19 23:28:59 -0700851static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852nfsd4_decode_open_downgrade(struct nfsd4_compoundargs *argp, struct nfsd4_open_downgrade *open_down)
853{
854 DECODE_HEAD;
855
Benny Halevye31a1b62008-08-12 20:46:18 +0300856 status = nfsd4_decode_stateid(argp, &open_down->od_stateid);
857 if (status)
858 return status;
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400859 READ_BUF(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 READ32(open_down->od_seqid);
Benny Halevy2c8bd7e2012-02-16 20:57:09 +0200861 status = nfsd4_decode_share_access(argp, &open_down->od_share_access,
862 &open_down->od_deleg_want, NULL);
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400863 if (status)
864 return status;
865 status = nfsd4_decode_share_deny(argp, &open_down->od_share_deny);
866 if (status)
867 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 DECODE_TAIL;
869}
870
Al Virob37ad282006-10-19 23:28:59 -0700871static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872nfsd4_decode_putfh(struct nfsd4_compoundargs *argp, struct nfsd4_putfh *putfh)
873{
874 DECODE_HEAD;
875
876 READ_BUF(4);
877 READ32(putfh->pf_fhlen);
878 if (putfh->pf_fhlen > NFS4_FHSIZE)
879 goto xdr_error;
880 READ_BUF(putfh->pf_fhlen);
881 SAVEMEM(putfh->pf_fhval, putfh->pf_fhlen);
882
883 DECODE_TAIL;
884}
885
Al Virob37ad282006-10-19 23:28:59 -0700886static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887nfsd4_decode_read(struct nfsd4_compoundargs *argp, struct nfsd4_read *read)
888{
889 DECODE_HEAD;
890
Benny Halevye31a1b62008-08-12 20:46:18 +0300891 status = nfsd4_decode_stateid(argp, &read->rd_stateid);
892 if (status)
893 return status;
894 READ_BUF(12);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 READ64(read->rd_offset);
896 READ32(read->rd_length);
897
898 DECODE_TAIL;
899}
900
Al Virob37ad282006-10-19 23:28:59 -0700901static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902nfsd4_decode_readdir(struct nfsd4_compoundargs *argp, struct nfsd4_readdir *readdir)
903{
904 DECODE_HEAD;
905
906 READ_BUF(24);
907 READ64(readdir->rd_cookie);
908 COPYMEM(readdir->rd_verf.data, sizeof(readdir->rd_verf.data));
909 READ32(readdir->rd_dircount); /* just in case you needed a useless field... */
910 READ32(readdir->rd_maxcount);
911 if ((status = nfsd4_decode_bitmap(argp, readdir->rd_bmval)))
912 goto out;
913
914 DECODE_TAIL;
915}
916
Al Virob37ad282006-10-19 23:28:59 -0700917static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918nfsd4_decode_remove(struct nfsd4_compoundargs *argp, struct nfsd4_remove *remove)
919{
920 DECODE_HEAD;
921
922 READ_BUF(4);
923 READ32(remove->rm_namelen);
924 READ_BUF(remove->rm_namelen);
925 SAVEMEM(remove->rm_name, remove->rm_namelen);
926 if ((status = check_filename(remove->rm_name, remove->rm_namelen, nfserr_noent)))
927 return status;
928
929 DECODE_TAIL;
930}
931
Al Virob37ad282006-10-19 23:28:59 -0700932static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933nfsd4_decode_rename(struct nfsd4_compoundargs *argp, struct nfsd4_rename *rename)
934{
935 DECODE_HEAD;
936
937 READ_BUF(4);
938 READ32(rename->rn_snamelen);
939 READ_BUF(rename->rn_snamelen + 4);
940 SAVEMEM(rename->rn_sname, rename->rn_snamelen);
941 READ32(rename->rn_tnamelen);
942 READ_BUF(rename->rn_tnamelen);
943 SAVEMEM(rename->rn_tname, rename->rn_tnamelen);
944 if ((status = check_filename(rename->rn_sname, rename->rn_snamelen, nfserr_noent)))
945 return status;
946 if ((status = check_filename(rename->rn_tname, rename->rn_tnamelen, nfserr_inval)))
947 return status;
948
949 DECODE_TAIL;
950}
951
Al Virob37ad282006-10-19 23:28:59 -0700952static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953nfsd4_decode_renew(struct nfsd4_compoundargs *argp, clientid_t *clientid)
954{
955 DECODE_HEAD;
956
957 READ_BUF(sizeof(clientid_t));
958 COPYMEM(clientid, sizeof(clientid_t));
959
960 DECODE_TAIL;
961}
962
Al Virob37ad282006-10-19 23:28:59 -0700963static __be32
Andy Adamsondcb488a32007-07-17 04:04:51 -0700964nfsd4_decode_secinfo(struct nfsd4_compoundargs *argp,
965 struct nfsd4_secinfo *secinfo)
966{
967 DECODE_HEAD;
968
969 READ_BUF(4);
970 READ32(secinfo->si_namelen);
971 READ_BUF(secinfo->si_namelen);
972 SAVEMEM(secinfo->si_name, secinfo->si_namelen);
973 status = check_filename(secinfo->si_name, secinfo->si_namelen,
974 nfserr_noent);
975 if (status)
976 return status;
977 DECODE_TAIL;
978}
979
980static __be32
J. Bruce Fields04f4ad12010-12-16 09:51:13 -0500981nfsd4_decode_secinfo_no_name(struct nfsd4_compoundargs *argp,
982 struct nfsd4_secinfo_no_name *sin)
983{
984 DECODE_HEAD;
985
986 READ_BUF(4);
987 READ32(sin->sin_style);
988 DECODE_TAIL;
989}
990
991static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992nfsd4_decode_setattr(struct nfsd4_compoundargs *argp, struct nfsd4_setattr *setattr)
993{
Benny Halevye31a1b62008-08-12 20:46:18 +0300994 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
Benny Halevye31a1b62008-08-12 20:46:18 +0300996 status = nfsd4_decode_stateid(argp, &setattr->sa_stateid);
997 if (status)
998 return status;
Yu Zhiguo3c8e0312009-05-16 16:22:31 +0800999 return nfsd4_decode_fattr(argp, setattr->sa_bmval, &setattr->sa_iattr,
1000 &setattr->sa_acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001}
1002
Al Virob37ad282006-10-19 23:28:59 -07001003static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004nfsd4_decode_setclientid(struct nfsd4_compoundargs *argp, struct nfsd4_setclientid *setclientid)
1005{
1006 DECODE_HEAD;
1007
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04001008 READ_BUF(8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 COPYMEM(setclientid->se_verf.data, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04001011 status = nfsd4_decode_opaque(argp, &setclientid->se_name);
1012 if (status)
1013 return nfserr_bad_xdr;
1014 READ_BUF(8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 READ32(setclientid->se_callback_prog);
1016 READ32(setclientid->se_callback_netid_len);
1017
1018 READ_BUF(setclientid->se_callback_netid_len + 4);
1019 SAVEMEM(setclientid->se_callback_netid_val, setclientid->se_callback_netid_len);
1020 READ32(setclientid->se_callback_addr_len);
1021
1022 READ_BUF(setclientid->se_callback_addr_len + 4);
1023 SAVEMEM(setclientid->se_callback_addr_val, setclientid->se_callback_addr_len);
1024 READ32(setclientid->se_callback_ident);
1025
1026 DECODE_TAIL;
1027}
1028
Al Virob37ad282006-10-19 23:28:59 -07001029static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030nfsd4_decode_setclientid_confirm(struct nfsd4_compoundargs *argp, struct nfsd4_setclientid_confirm *scd_c)
1031{
1032 DECODE_HEAD;
1033
1034 READ_BUF(8 + sizeof(nfs4_verifier));
1035 COPYMEM(&scd_c->sc_clientid, 8);
1036 COPYMEM(&scd_c->sc_confirm, sizeof(nfs4_verifier));
1037
1038 DECODE_TAIL;
1039}
1040
1041/* Also used for NVERIFY */
Al Virob37ad282006-10-19 23:28:59 -07001042static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043nfsd4_decode_verify(struct nfsd4_compoundargs *argp, struct nfsd4_verify *verify)
1044{
1045#if 0
1046 struct nfsd4_compoundargs save = {
1047 .p = argp->p,
1048 .end = argp->end,
1049 .rqstp = argp->rqstp,
1050 };
1051 u32 ve_bmval[2];
1052 struct iattr ve_iattr; /* request */
1053 struct nfs4_acl *ve_acl; /* request */
1054#endif
1055 DECODE_HEAD;
1056
1057 if ((status = nfsd4_decode_bitmap(argp, verify->ve_bmval)))
1058 goto out;
1059
1060 /* For convenience's sake, we compare raw xdr'd attributes in
1061 * nfsd4_proc_verify; however we still decode here just to return
1062 * correct error in case of bad xdr. */
1063#if 0
1064 status = nfsd4_decode_fattr(ve_bmval, &ve_iattr, &ve_acl);
1065 if (status == nfserr_inval) {
1066 status = nfserrno(status);
1067 goto out;
1068 }
1069#endif
1070 READ_BUF(4);
1071 READ32(verify->ve_attrlen);
1072 READ_BUF(verify->ve_attrlen);
1073 SAVEMEM(verify->ve_attrval, verify->ve_attrlen);
1074
1075 DECODE_TAIL;
1076}
1077
Al Virob37ad282006-10-19 23:28:59 -07001078static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079nfsd4_decode_write(struct nfsd4_compoundargs *argp, struct nfsd4_write *write)
1080{
1081 int avail;
1082 int v;
1083 int len;
1084 DECODE_HEAD;
1085
Benny Halevye31a1b62008-08-12 20:46:18 +03001086 status = nfsd4_decode_stateid(argp, &write->wr_stateid);
1087 if (status)
1088 return status;
1089 READ_BUF(16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 READ64(write->wr_offset);
1091 READ32(write->wr_stable_how);
1092 if (write->wr_stable_how > 2)
1093 goto xdr_error;
1094 READ32(write->wr_buflen);
1095
1096 /* Sorry .. no magic macros for this.. *
1097 * READ_BUF(write->wr_buflen);
1098 * SAVEMEM(write->wr_buf, write->wr_buflen);
1099 */
1100 avail = (char*)argp->end - (char*)argp->p;
1101 if (avail + argp->pagelen < write->wr_buflen) {
Chuck Lever817cb9d2007-09-11 18:01:20 -04001102 dprintk("NFSD: xdr error (%s:%d)\n",
1103 __FILE__, __LINE__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 goto xdr_error;
1105 }
NeilBrown3cc03b12006-10-04 02:15:47 -07001106 argp->rqstp->rq_vec[0].iov_base = p;
1107 argp->rqstp->rq_vec[0].iov_len = avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 v = 0;
1109 len = write->wr_buflen;
NeilBrown3cc03b12006-10-04 02:15:47 -07001110 while (len > argp->rqstp->rq_vec[v].iov_len) {
1111 len -= argp->rqstp->rq_vec[v].iov_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 v++;
NeilBrown3cc03b12006-10-04 02:15:47 -07001113 argp->rqstp->rq_vec[v].iov_base = page_address(argp->pagelist[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 argp->pagelist++;
1115 if (argp->pagelen >= PAGE_SIZE) {
NeilBrown3cc03b12006-10-04 02:15:47 -07001116 argp->rqstp->rq_vec[v].iov_len = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 argp->pagelen -= PAGE_SIZE;
1118 } else {
NeilBrown3cc03b12006-10-04 02:15:47 -07001119 argp->rqstp->rq_vec[v].iov_len = argp->pagelen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 argp->pagelen -= len;
1121 }
1122 }
Al Viro2ebbc012006-10-19 23:28:58 -07001123 argp->end = (__be32*) (argp->rqstp->rq_vec[v].iov_base + argp->rqstp->rq_vec[v].iov_len);
1124 argp->p = (__be32*) (argp->rqstp->rq_vec[v].iov_base + (XDR_QUADLEN(len) << 2));
NeilBrown3cc03b12006-10-04 02:15:47 -07001125 argp->rqstp->rq_vec[v].iov_len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 write->wr_vlen = v+1;
1127
1128 DECODE_TAIL;
1129}
1130
Al Virob37ad282006-10-19 23:28:59 -07001131static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132nfsd4_decode_release_lockowner(struct nfsd4_compoundargs *argp, struct nfsd4_release_lockowner *rlockowner)
1133{
1134 DECODE_HEAD;
1135
1136 READ_BUF(12);
1137 COPYMEM(&rlockowner->rl_clientid, sizeof(clientid_t));
1138 READ32(rlockowner->rl_owner.len);
1139 READ_BUF(rlockowner->rl_owner.len);
1140 READMEM(rlockowner->rl_owner.data, rlockowner->rl_owner.len);
1141
Andy Adamson60adfc52009-04-03 08:28:50 +03001142 if (argp->minorversion && !zero_clientid(&rlockowner->rl_clientid))
1143 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 DECODE_TAIL;
1145}
1146
Al Virob37ad282006-10-19 23:28:59 -07001147static __be32
Andy Adamson2db134e2009-04-03 08:27:55 +03001148nfsd4_decode_exchange_id(struct nfsd4_compoundargs *argp,
Andy Adamson0733d212009-04-03 08:28:01 +03001149 struct nfsd4_exchange_id *exid)
Andy Adamson2db134e2009-04-03 08:27:55 +03001150{
Mi Jinlong5afa0402010-11-09 09:39:23 +08001151 int dummy, tmp;
Andy Adamson0733d212009-04-03 08:28:01 +03001152 DECODE_HEAD;
1153
1154 READ_BUF(NFS4_VERIFIER_SIZE);
1155 COPYMEM(exid->verifier.data, NFS4_VERIFIER_SIZE);
1156
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04001157 status = nfsd4_decode_opaque(argp, &exid->clname);
1158 if (status)
1159 return nfserr_bad_xdr;
Andy Adamson0733d212009-04-03 08:28:01 +03001160
1161 READ_BUF(4);
1162 READ32(exid->flags);
1163
1164 /* Ignore state_protect4_a */
1165 READ_BUF(4);
1166 READ32(exid->spa_how);
1167 switch (exid->spa_how) {
1168 case SP4_NONE:
1169 break;
1170 case SP4_MACH_CRED:
1171 /* spo_must_enforce */
1172 READ_BUF(4);
1173 READ32(dummy);
1174 READ_BUF(dummy * 4);
1175 p += dummy;
1176
1177 /* spo_must_allow */
1178 READ_BUF(4);
1179 READ32(dummy);
1180 READ_BUF(dummy * 4);
1181 p += dummy;
1182 break;
1183 case SP4_SSV:
1184 /* ssp_ops */
1185 READ_BUF(4);
1186 READ32(dummy);
1187 READ_BUF(dummy * 4);
1188 p += dummy;
1189
1190 READ_BUF(4);
1191 READ32(dummy);
1192 READ_BUF(dummy * 4);
1193 p += dummy;
1194
1195 /* ssp_hash_algs<> */
1196 READ_BUF(4);
Mi Jinlong5afa0402010-11-09 09:39:23 +08001197 READ32(tmp);
1198 while (tmp--) {
1199 READ_BUF(4);
1200 READ32(dummy);
1201 READ_BUF(dummy);
1202 p += XDR_QUADLEN(dummy);
1203 }
Andy Adamson0733d212009-04-03 08:28:01 +03001204
1205 /* ssp_encr_algs<> */
1206 READ_BUF(4);
Mi Jinlong5afa0402010-11-09 09:39:23 +08001207 READ32(tmp);
1208 while (tmp--) {
1209 READ_BUF(4);
1210 READ32(dummy);
1211 READ_BUF(dummy);
1212 p += XDR_QUADLEN(dummy);
1213 }
Andy Adamson0733d212009-04-03 08:28:01 +03001214
1215 /* ssp_window and ssp_num_gss_handles */
1216 READ_BUF(8);
1217 READ32(dummy);
1218 READ32(dummy);
1219 break;
1220 default:
1221 goto xdr_error;
1222 }
1223
1224 /* Ignore Implementation ID */
1225 READ_BUF(4); /* nfs_impl_id4 array length */
1226 READ32(dummy);
1227
1228 if (dummy > 1)
1229 goto xdr_error;
1230
1231 if (dummy == 1) {
1232 /* nii_domain */
1233 READ_BUF(4);
1234 READ32(dummy);
1235 READ_BUF(dummy);
1236 p += XDR_QUADLEN(dummy);
1237
1238 /* nii_name */
1239 READ_BUF(4);
1240 READ32(dummy);
1241 READ_BUF(dummy);
1242 p += XDR_QUADLEN(dummy);
1243
1244 /* nii_date */
1245 READ_BUF(12);
1246 p += 3;
1247 }
1248 DECODE_TAIL;
Andy Adamson2db134e2009-04-03 08:27:55 +03001249}
1250
1251static __be32
1252nfsd4_decode_create_session(struct nfsd4_compoundargs *argp,
1253 struct nfsd4_create_session *sess)
1254{
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001255 DECODE_HEAD;
1256
1257 u32 dummy;
1258 char *machine_name;
Mi Jinlong5a02ab72011-03-11 12:13:55 +08001259 int i;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001260 int nr_secflavs;
1261
1262 READ_BUF(16);
1263 COPYMEM(&sess->clientid, 8);
1264 READ32(sess->seqid);
1265 READ32(sess->flags);
1266
1267 /* Fore channel attrs */
1268 READ_BUF(28);
1269 READ32(dummy); /* headerpadsz is always 0 */
1270 READ32(sess->fore_channel.maxreq_sz);
1271 READ32(sess->fore_channel.maxresp_sz);
1272 READ32(sess->fore_channel.maxresp_cached);
1273 READ32(sess->fore_channel.maxops);
1274 READ32(sess->fore_channel.maxreqs);
1275 READ32(sess->fore_channel.nr_rdma_attrs);
1276 if (sess->fore_channel.nr_rdma_attrs == 1) {
1277 READ_BUF(4);
1278 READ32(sess->fore_channel.rdma_attrs);
1279 } else if (sess->fore_channel.nr_rdma_attrs > 1) {
1280 dprintk("Too many fore channel attr bitmaps!\n");
1281 goto xdr_error;
1282 }
1283
1284 /* Back channel attrs */
1285 READ_BUF(28);
1286 READ32(dummy); /* headerpadsz is always 0 */
1287 READ32(sess->back_channel.maxreq_sz);
1288 READ32(sess->back_channel.maxresp_sz);
1289 READ32(sess->back_channel.maxresp_cached);
1290 READ32(sess->back_channel.maxops);
1291 READ32(sess->back_channel.maxreqs);
1292 READ32(sess->back_channel.nr_rdma_attrs);
1293 if (sess->back_channel.nr_rdma_attrs == 1) {
1294 READ_BUF(4);
1295 READ32(sess->back_channel.rdma_attrs);
1296 } else if (sess->back_channel.nr_rdma_attrs > 1) {
1297 dprintk("Too many back channel attr bitmaps!\n");
1298 goto xdr_error;
1299 }
1300
1301 READ_BUF(8);
1302 READ32(sess->callback_prog);
1303
1304 /* callback_sec_params4 */
1305 READ32(nr_secflavs);
1306 for (i = 0; i < nr_secflavs; ++i) {
1307 READ_BUF(4);
1308 READ32(dummy);
1309 switch (dummy) {
1310 case RPC_AUTH_NULL:
1311 /* Nothing to read */
1312 break;
1313 case RPC_AUTH_UNIX:
1314 READ_BUF(8);
1315 /* stamp */
1316 READ32(dummy);
1317
1318 /* machine name */
1319 READ32(dummy);
1320 READ_BUF(dummy);
1321 SAVEMEM(machine_name, dummy);
1322
1323 /* uid, gid */
1324 READ_BUF(8);
1325 READ32(sess->uid);
1326 READ32(sess->gid);
1327
1328 /* more gids */
1329 READ_BUF(4);
1330 READ32(dummy);
1331 READ_BUF(dummy * 4);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001332 break;
1333 case RPC_AUTH_GSS:
1334 dprintk("RPC_AUTH_GSS callback secflavor "
1335 "not supported!\n");
1336 READ_BUF(8);
1337 /* gcbp_service */
1338 READ32(dummy);
1339 /* gcbp_handle_from_server */
1340 READ32(dummy);
1341 READ_BUF(dummy);
1342 p += XDR_QUADLEN(dummy);
1343 /* gcbp_handle_from_client */
1344 READ_BUF(4);
1345 READ32(dummy);
1346 READ_BUF(dummy);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001347 break;
1348 default:
1349 dprintk("Illegal callback secflavor\n");
1350 return nfserr_inval;
1351 }
1352 }
1353 DECODE_TAIL;
Andy Adamson2db134e2009-04-03 08:27:55 +03001354}
1355
1356static __be32
1357nfsd4_decode_destroy_session(struct nfsd4_compoundargs *argp,
1358 struct nfsd4_destroy_session *destroy_session)
1359{
Benny Halevye10e0cf2009-04-03 08:28:38 +03001360 DECODE_HEAD;
1361 READ_BUF(NFS4_MAX_SESSIONID_LEN);
1362 COPYMEM(destroy_session->sessionid.data, NFS4_MAX_SESSIONID_LEN);
1363
1364 DECODE_TAIL;
Andy Adamson2db134e2009-04-03 08:27:55 +03001365}
1366
1367static __be32
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04001368nfsd4_decode_free_stateid(struct nfsd4_compoundargs *argp,
1369 struct nfsd4_free_stateid *free_stateid)
1370{
1371 DECODE_HEAD;
1372
1373 READ_BUF(sizeof(stateid_t));
1374 READ32(free_stateid->fr_stateid.si_generation);
1375 COPYMEM(&free_stateid->fr_stateid.si_opaque, sizeof(stateid_opaque_t));
1376
1377 DECODE_TAIL;
1378}
1379
1380static __be32
Andy Adamson2db134e2009-04-03 08:27:55 +03001381nfsd4_decode_sequence(struct nfsd4_compoundargs *argp,
1382 struct nfsd4_sequence *seq)
1383{
Benny Halevyb85d4c02009-04-03 08:28:08 +03001384 DECODE_HEAD;
1385
1386 READ_BUF(NFS4_MAX_SESSIONID_LEN + 16);
1387 COPYMEM(seq->sessionid.data, NFS4_MAX_SESSIONID_LEN);
1388 READ32(seq->seqid);
1389 READ32(seq->slotid);
1390 READ32(seq->maxslots);
1391 READ32(seq->cachethis);
1392
1393 DECODE_TAIL;
Andy Adamson2db134e2009-04-03 08:27:55 +03001394}
1395
Bryan Schumaker17456802011-07-13 10:50:48 -04001396static __be32
1397nfsd4_decode_test_stateid(struct nfsd4_compoundargs *argp, struct nfsd4_test_stateid *test_stateid)
1398{
1399 unsigned int nbytes;
1400 stateid_t si;
1401 int i;
1402 __be32 *p;
1403 __be32 status;
1404
1405 READ_BUF(4);
1406 test_stateid->ts_num_ids = ntohl(*p++);
1407
1408 nbytes = test_stateid->ts_num_ids * sizeof(stateid_t);
1409 if (nbytes > (u32)((char *)argp->end - (char *)argp->p))
1410 goto xdr_error;
1411
1412 test_stateid->ts_saved_args = argp;
1413 save_buf(argp, &test_stateid->ts_savedp);
1414
1415 for (i = 0; i < test_stateid->ts_num_ids; i++) {
1416 status = nfsd4_decode_stateid(argp, &si);
1417 if (status)
1418 return status;
1419 }
1420
1421 status = 0;
1422out:
1423 return status;
1424xdr_error:
1425 dprintk("NFSD: xdr error (%s:%d)\n", __FILE__, __LINE__);
1426 status = nfserr_bad_xdr;
1427 goto out;
1428}
1429
Mi Jinlong345c2842011-10-20 17:51:39 +08001430static __be32 nfsd4_decode_destroy_clientid(struct nfsd4_compoundargs *argp, struct nfsd4_destroy_clientid *dc)
1431{
1432 DECODE_HEAD;
1433
1434 READ_BUF(8);
1435 COPYMEM(&dc->clientid, 8);
1436
1437 DECODE_TAIL;
1438}
1439
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04001440static __be32 nfsd4_decode_reclaim_complete(struct nfsd4_compoundargs *argp, struct nfsd4_reclaim_complete *rc)
1441{
1442 DECODE_HEAD;
1443
1444 READ_BUF(4);
1445 READ32(rc->rca_one_fs);
1446
1447 DECODE_TAIL;
1448}
1449
Andy Adamson2db134e2009-04-03 08:27:55 +03001450static __be32
Benny Halevy347e0ad2008-07-02 11:13:41 +03001451nfsd4_decode_noop(struct nfsd4_compoundargs *argp, void *p)
1452{
1453 return nfs_ok;
1454}
1455
Benny Halevy3c375c62008-07-02 11:14:01 +03001456static __be32
1457nfsd4_decode_notsupp(struct nfsd4_compoundargs *argp, void *p)
1458{
Benny Halevy1e685ec2009-03-04 23:06:06 +02001459 return nfserr_notsupp;
Benny Halevy3c375c62008-07-02 11:14:01 +03001460}
1461
Benny Halevy347e0ad2008-07-02 11:13:41 +03001462typedef __be32(*nfsd4_dec)(struct nfsd4_compoundargs *argp, void *);
1463
1464static nfsd4_dec nfsd4_dec_ops[] = {
J. Bruce Fieldsad1060c2008-07-18 15:04:16 -04001465 [OP_ACCESS] = (nfsd4_dec)nfsd4_decode_access,
1466 [OP_CLOSE] = (nfsd4_dec)nfsd4_decode_close,
1467 [OP_COMMIT] = (nfsd4_dec)nfsd4_decode_commit,
1468 [OP_CREATE] = (nfsd4_dec)nfsd4_decode_create,
1469 [OP_DELEGPURGE] = (nfsd4_dec)nfsd4_decode_notsupp,
1470 [OP_DELEGRETURN] = (nfsd4_dec)nfsd4_decode_delegreturn,
1471 [OP_GETATTR] = (nfsd4_dec)nfsd4_decode_getattr,
1472 [OP_GETFH] = (nfsd4_dec)nfsd4_decode_noop,
1473 [OP_LINK] = (nfsd4_dec)nfsd4_decode_link,
1474 [OP_LOCK] = (nfsd4_dec)nfsd4_decode_lock,
1475 [OP_LOCKT] = (nfsd4_dec)nfsd4_decode_lockt,
1476 [OP_LOCKU] = (nfsd4_dec)nfsd4_decode_locku,
1477 [OP_LOOKUP] = (nfsd4_dec)nfsd4_decode_lookup,
1478 [OP_LOOKUPP] = (nfsd4_dec)nfsd4_decode_noop,
1479 [OP_NVERIFY] = (nfsd4_dec)nfsd4_decode_verify,
1480 [OP_OPEN] = (nfsd4_dec)nfsd4_decode_open,
1481 [OP_OPENATTR] = (nfsd4_dec)nfsd4_decode_notsupp,
1482 [OP_OPEN_CONFIRM] = (nfsd4_dec)nfsd4_decode_open_confirm,
1483 [OP_OPEN_DOWNGRADE] = (nfsd4_dec)nfsd4_decode_open_downgrade,
1484 [OP_PUTFH] = (nfsd4_dec)nfsd4_decode_putfh,
J. Bruce Fieldsa1c8c4d2009-03-09 12:17:29 -04001485 [OP_PUTPUBFH] = (nfsd4_dec)nfsd4_decode_noop,
J. Bruce Fieldsad1060c2008-07-18 15:04:16 -04001486 [OP_PUTROOTFH] = (nfsd4_dec)nfsd4_decode_noop,
1487 [OP_READ] = (nfsd4_dec)nfsd4_decode_read,
1488 [OP_READDIR] = (nfsd4_dec)nfsd4_decode_readdir,
1489 [OP_READLINK] = (nfsd4_dec)nfsd4_decode_noop,
1490 [OP_REMOVE] = (nfsd4_dec)nfsd4_decode_remove,
1491 [OP_RENAME] = (nfsd4_dec)nfsd4_decode_rename,
1492 [OP_RENEW] = (nfsd4_dec)nfsd4_decode_renew,
1493 [OP_RESTOREFH] = (nfsd4_dec)nfsd4_decode_noop,
1494 [OP_SAVEFH] = (nfsd4_dec)nfsd4_decode_noop,
1495 [OP_SECINFO] = (nfsd4_dec)nfsd4_decode_secinfo,
1496 [OP_SETATTR] = (nfsd4_dec)nfsd4_decode_setattr,
1497 [OP_SETCLIENTID] = (nfsd4_dec)nfsd4_decode_setclientid,
1498 [OP_SETCLIENTID_CONFIRM] = (nfsd4_dec)nfsd4_decode_setclientid_confirm,
1499 [OP_VERIFY] = (nfsd4_dec)nfsd4_decode_verify,
1500 [OP_WRITE] = (nfsd4_dec)nfsd4_decode_write,
1501 [OP_RELEASE_LOCKOWNER] = (nfsd4_dec)nfsd4_decode_release_lockowner,
Benny Halevy347e0ad2008-07-02 11:13:41 +03001502};
1503
Andy Adamson2db134e2009-04-03 08:27:55 +03001504static nfsd4_dec nfsd41_dec_ops[] = {
Randy Dunlap9064caa2009-04-28 16:48:25 -07001505 [OP_ACCESS] = (nfsd4_dec)nfsd4_decode_access,
1506 [OP_CLOSE] = (nfsd4_dec)nfsd4_decode_close,
1507 [OP_COMMIT] = (nfsd4_dec)nfsd4_decode_commit,
1508 [OP_CREATE] = (nfsd4_dec)nfsd4_decode_create,
1509 [OP_DELEGPURGE] = (nfsd4_dec)nfsd4_decode_notsupp,
1510 [OP_DELEGRETURN] = (nfsd4_dec)nfsd4_decode_delegreturn,
1511 [OP_GETATTR] = (nfsd4_dec)nfsd4_decode_getattr,
1512 [OP_GETFH] = (nfsd4_dec)nfsd4_decode_noop,
1513 [OP_LINK] = (nfsd4_dec)nfsd4_decode_link,
1514 [OP_LOCK] = (nfsd4_dec)nfsd4_decode_lock,
1515 [OP_LOCKT] = (nfsd4_dec)nfsd4_decode_lockt,
1516 [OP_LOCKU] = (nfsd4_dec)nfsd4_decode_locku,
1517 [OP_LOOKUP] = (nfsd4_dec)nfsd4_decode_lookup,
1518 [OP_LOOKUPP] = (nfsd4_dec)nfsd4_decode_noop,
1519 [OP_NVERIFY] = (nfsd4_dec)nfsd4_decode_verify,
1520 [OP_OPEN] = (nfsd4_dec)nfsd4_decode_open,
1521 [OP_OPENATTR] = (nfsd4_dec)nfsd4_decode_notsupp,
1522 [OP_OPEN_CONFIRM] = (nfsd4_dec)nfsd4_decode_notsupp,
1523 [OP_OPEN_DOWNGRADE] = (nfsd4_dec)nfsd4_decode_open_downgrade,
1524 [OP_PUTFH] = (nfsd4_dec)nfsd4_decode_putfh,
1525 [OP_PUTPUBFH] = (nfsd4_dec)nfsd4_decode_notsupp,
1526 [OP_PUTROOTFH] = (nfsd4_dec)nfsd4_decode_noop,
1527 [OP_READ] = (nfsd4_dec)nfsd4_decode_read,
1528 [OP_READDIR] = (nfsd4_dec)nfsd4_decode_readdir,
1529 [OP_READLINK] = (nfsd4_dec)nfsd4_decode_noop,
1530 [OP_REMOVE] = (nfsd4_dec)nfsd4_decode_remove,
1531 [OP_RENAME] = (nfsd4_dec)nfsd4_decode_rename,
1532 [OP_RENEW] = (nfsd4_dec)nfsd4_decode_notsupp,
1533 [OP_RESTOREFH] = (nfsd4_dec)nfsd4_decode_noop,
1534 [OP_SAVEFH] = (nfsd4_dec)nfsd4_decode_noop,
1535 [OP_SECINFO] = (nfsd4_dec)nfsd4_decode_secinfo,
1536 [OP_SETATTR] = (nfsd4_dec)nfsd4_decode_setattr,
1537 [OP_SETCLIENTID] = (nfsd4_dec)nfsd4_decode_notsupp,
1538 [OP_SETCLIENTID_CONFIRM]= (nfsd4_dec)nfsd4_decode_notsupp,
1539 [OP_VERIFY] = (nfsd4_dec)nfsd4_decode_verify,
1540 [OP_WRITE] = (nfsd4_dec)nfsd4_decode_write,
1541 [OP_RELEASE_LOCKOWNER] = (nfsd4_dec)nfsd4_decode_notsupp,
Andy Adamson2db134e2009-04-03 08:27:55 +03001542
1543 /* new operations for NFSv4.1 */
Randy Dunlap9064caa2009-04-28 16:48:25 -07001544 [OP_BACKCHANNEL_CTL] = (nfsd4_dec)nfsd4_decode_notsupp,
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001545 [OP_BIND_CONN_TO_SESSION]= (nfsd4_dec)nfsd4_decode_bind_conn_to_session,
Randy Dunlap9064caa2009-04-28 16:48:25 -07001546 [OP_EXCHANGE_ID] = (nfsd4_dec)nfsd4_decode_exchange_id,
1547 [OP_CREATE_SESSION] = (nfsd4_dec)nfsd4_decode_create_session,
1548 [OP_DESTROY_SESSION] = (nfsd4_dec)nfsd4_decode_destroy_session,
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04001549 [OP_FREE_STATEID] = (nfsd4_dec)nfsd4_decode_free_stateid,
Randy Dunlap9064caa2009-04-28 16:48:25 -07001550 [OP_GET_DIR_DELEGATION] = (nfsd4_dec)nfsd4_decode_notsupp,
1551 [OP_GETDEVICEINFO] = (nfsd4_dec)nfsd4_decode_notsupp,
1552 [OP_GETDEVICELIST] = (nfsd4_dec)nfsd4_decode_notsupp,
1553 [OP_LAYOUTCOMMIT] = (nfsd4_dec)nfsd4_decode_notsupp,
1554 [OP_LAYOUTGET] = (nfsd4_dec)nfsd4_decode_notsupp,
1555 [OP_LAYOUTRETURN] = (nfsd4_dec)nfsd4_decode_notsupp,
J. Bruce Fields04f4ad12010-12-16 09:51:13 -05001556 [OP_SECINFO_NO_NAME] = (nfsd4_dec)nfsd4_decode_secinfo_no_name,
Randy Dunlap9064caa2009-04-28 16:48:25 -07001557 [OP_SEQUENCE] = (nfsd4_dec)nfsd4_decode_sequence,
1558 [OP_SET_SSV] = (nfsd4_dec)nfsd4_decode_notsupp,
Bryan Schumaker17456802011-07-13 10:50:48 -04001559 [OP_TEST_STATEID] = (nfsd4_dec)nfsd4_decode_test_stateid,
Randy Dunlap9064caa2009-04-28 16:48:25 -07001560 [OP_WANT_DELEGATION] = (nfsd4_dec)nfsd4_decode_notsupp,
Mi Jinlong345c2842011-10-20 17:51:39 +08001561 [OP_DESTROY_CLIENTID] = (nfsd4_dec)nfsd4_decode_destroy_clientid,
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04001562 [OP_RECLAIM_COMPLETE] = (nfsd4_dec)nfsd4_decode_reclaim_complete,
Andy Adamson2db134e2009-04-03 08:27:55 +03001563};
1564
Benny Halevyf2feb962008-07-02 11:14:22 +03001565struct nfsd4_minorversion_ops {
1566 nfsd4_dec *decoders;
1567 int nops;
1568};
1569
1570static struct nfsd4_minorversion_ops nfsd4_minorversion[] = {
J. Bruce Fieldsad1060c2008-07-18 15:04:16 -04001571 [0] = { nfsd4_dec_ops, ARRAY_SIZE(nfsd4_dec_ops) },
Andy Adamson2db134e2009-04-03 08:27:55 +03001572 [1] = { nfsd41_dec_ops, ARRAY_SIZE(nfsd41_dec_ops) },
Benny Halevyf2feb962008-07-02 11:14:22 +03001573};
1574
Benny Halevy347e0ad2008-07-02 11:13:41 +03001575static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576nfsd4_decode_compound(struct nfsd4_compoundargs *argp)
1577{
1578 DECODE_HEAD;
1579 struct nfsd4_op *op;
Benny Halevyf2feb962008-07-02 11:14:22 +03001580 struct nfsd4_minorversion_ops *ops;
J. Bruce Fields10910062011-01-24 12:11:02 -05001581 bool cachethis = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 int i;
1583
1584 /*
1585 * XXX: According to spec, we should check the tag
1586 * for UTF-8 compliance. I'm postponing this for
1587 * now because it seems that some clients do use
1588 * binary tags.
1589 */
1590 READ_BUF(4);
1591 READ32(argp->taglen);
1592 READ_BUF(argp->taglen + 8);
1593 SAVEMEM(argp->tag, argp->taglen);
1594 READ32(argp->minorversion);
1595 READ32(argp->opcnt);
1596
1597 if (argp->taglen > NFSD4_MAX_TAGLEN)
1598 goto xdr_error;
1599 if (argp->opcnt > 100)
1600 goto xdr_error;
1601
Tobias Klausere8c96f82006-03-24 03:15:34 -08001602 if (argp->opcnt > ARRAY_SIZE(argp->iops)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 argp->ops = kmalloc(argp->opcnt * sizeof(*argp->ops), GFP_KERNEL);
1604 if (!argp->ops) {
1605 argp->ops = argp->iops;
Chuck Lever817cb9d2007-09-11 18:01:20 -04001606 dprintk("nfsd: couldn't allocate room for COMPOUND\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 goto xdr_error;
1608 }
1609 }
1610
Benny Halevyf2feb962008-07-02 11:14:22 +03001611 if (argp->minorversion >= ARRAY_SIZE(nfsd4_minorversion))
Benny Halevy30cff1f2008-07-02 11:13:18 +03001612 argp->opcnt = 0;
1613
Benny Halevyf2feb962008-07-02 11:14:22 +03001614 ops = &nfsd4_minorversion[argp->minorversion];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 for (i = 0; i < argp->opcnt; i++) {
1616 op = &argp->ops[i];
1617 op->replay = NULL;
1618
1619 /*
1620 * We can't use READ_BUF() here because we need to handle
1621 * a missing opcode as an OP_WRITE + 1. So we need to check
1622 * to see if we're truly at the end of our buffer or if there
1623 * is another page we need to flip to.
1624 */
1625
1626 if (argp->p == argp->end) {
1627 if (argp->pagelen < 4) {
1628 /* There isn't an opcode still on the wire */
1629 op->opnum = OP_WRITE + 1;
1630 op->status = nfserr_bad_xdr;
1631 argp->opcnt = i+1;
1632 break;
1633 }
1634
1635 /*
1636 * False alarm. We just hit a page boundary, but there
1637 * is still data available. Move pointer across page
1638 * boundary. *snip from READ_BUF*
1639 */
1640 argp->p = page_address(argp->pagelist[0]);
1641 argp->pagelist++;
1642 if (argp->pagelen < PAGE_SIZE) {
Neil Brown2bc3c112010-04-20 12:16:52 +10001643 argp->end = argp->p + (argp->pagelen>>2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 argp->pagelen = 0;
1645 } else {
Neil Brown2bc3c112010-04-20 12:16:52 +10001646 argp->end = argp->p + (PAGE_SIZE>>2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 argp->pagelen -= PAGE_SIZE;
1648 }
1649 }
1650 op->opnum = ntohl(*argp->p++);
1651
Ricardo Labiagade3cab72009-12-11 20:03:27 -08001652 if (op->opnum >= FIRST_NFS4_OP && op->opnum <= LAST_NFS4_OP)
Benny Halevyf2feb962008-07-02 11:14:22 +03001653 op->status = ops->decoders[op->opnum](argp, &op->u);
Benny Halevy347e0ad2008-07-02 11:13:41 +03001654 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 op->opnum = OP_ILLEGAL;
1656 op->status = nfserr_op_illegal;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 }
1658
1659 if (op->status) {
1660 argp->opcnt = i+1;
1661 break;
1662 }
J. Bruce Fields10910062011-01-24 12:11:02 -05001663 /*
1664 * We'll try to cache the result in the DRC if any one
1665 * op in the compound wants to be cached:
1666 */
1667 cachethis |= nfsd4_cache_this_op(op);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 }
J. Bruce Fields10910062011-01-24 12:11:02 -05001669 /* Sessions make the DRC unnecessary: */
1670 if (argp->minorversion)
1671 cachethis = false;
1672 argp->rqstp->rq_cachetype = cachethis ? RC_REPLBUFF : RC_NOCACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
1674 DECODE_TAIL;
1675}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677#define WRITE32(n) *p++ = htonl(n)
1678#define WRITE64(n) do { \
1679 *p++ = htonl((u32)((n) >> 32)); \
1680 *p++ = htonl((u32)(n)); \
1681} while (0)
Harvey Harrison5108b272008-07-17 21:33:04 -07001682#define WRITEMEM(ptr,nbytes) do { if (nbytes > 0) { \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 *(p + XDR_QUADLEN(nbytes) -1) = 0; \
1684 memcpy(p, ptr, nbytes); \
1685 p += XDR_QUADLEN(nbytes); \
Harvey Harrison5108b272008-07-17 21:33:04 -07001686}} while (0)
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04001687
1688static void write32(__be32 **p, u32 n)
1689{
1690 *(*p)++ = n;
1691}
1692
1693static void write64(__be32 **p, u64 n)
1694{
1695 write32(p, (u32)(n >> 32));
1696 write32(p, (u32)n);
1697}
1698
1699static void write_change(__be32 **p, struct kstat *stat, struct inode *inode)
1700{
1701 if (IS_I_VERSION(inode)) {
1702 write64(p, inode->i_version);
1703 } else {
1704 write32(p, stat->ctime.tv_sec);
1705 write32(p, stat->ctime.tv_nsec);
1706 }
1707}
1708
1709static void write_cinfo(__be32 **p, struct nfsd4_change_info *c)
1710{
1711 write32(p, c->atomic);
1712 if (c->change_supported) {
1713 write64(p, c->before_change);
1714 write64(p, c->after_change);
1715 } else {
1716 write32(p, c->before_ctime_sec);
1717 write32(p, c->before_ctime_nsec);
1718 write32(p, c->after_ctime_sec);
1719 write32(p, c->after_ctime_nsec);
1720 }
1721}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722
1723#define RESERVE_SPACE(nbytes) do { \
1724 p = resp->p; \
1725 BUG_ON(p + XDR_QUADLEN(nbytes) > resp->end); \
1726} while (0)
1727#define ADJUST_ARGS() resp->p = p
1728
1729/*
1730 * Header routine to setup seqid operation replay cache
1731 */
1732#define ENCODE_SEQID_OP_HEAD \
Al Viro2ebbc012006-10-19 23:28:58 -07001733 __be32 *save; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 \
1735 save = resp->p;
1736
1737/*
NeilBrown7fb64ce2005-07-07 17:59:20 -07001738 * Routine for encoding the result of a "seqid-mutating" NFSv4 operation. This
1739 * is where sequence id's are incremented, and the replay cache is filled.
1740 * Note that we increment sequence id's here, at the last moment, so we're sure
1741 * we know whether the error to be returned is a sequence id mutating error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 */
1743
J. Bruce Fieldsf3e42232011-08-24 12:27:31 -04001744static void encode_seqid_op_tail(struct nfsd4_compoundres *resp, __be32 *save, __be32 nfserr)
1745{
1746 struct nfs4_stateowner *stateowner = resp->cstate.replay_owner;
1747
1748 if (seqid_mutating_err(ntohl(nfserr)) && stateowner) {
1749 stateowner->so_seqid++;
1750 stateowner->so_replay.rp_status = nfserr;
1751 stateowner->so_replay.rp_buflen =
1752 (char *)resp->p - (char *)save;
1753 memcpy(stateowner->so_replay.rp_buf, save,
1754 stateowner->so_replay.rp_buflen);
J. Bruce Fields38c387b2011-09-16 17:42:48 -04001755 nfsd4_purge_closed_stateid(stateowner);
J. Bruce Fieldsf3e42232011-08-24 12:27:31 -04001756 }
1757}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001759/* Encode as an array of strings the string given with components
Daniel Mack3ad2f3f2010-02-03 08:01:28 +08001760 * separated @sep.
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001761 */
Al Virob37ad282006-10-19 23:28:59 -07001762static __be32 nfsd4_encode_components(char sep, char *components,
Al Viro2ebbc012006-10-19 23:28:58 -07001763 __be32 **pp, int *buflen)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001764{
Al Viro2ebbc012006-10-19 23:28:58 -07001765 __be32 *p = *pp;
1766 __be32 *countp = p;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001767 int strlen, count=0;
1768 char *str, *end;
1769
1770 dprintk("nfsd4_encode_components(%s)\n", components);
1771 if ((*buflen -= 4) < 0)
1772 return nfserr_resource;
1773 WRITE32(0); /* We will fill this in with @count later */
1774 end = str = components;
1775 while (*end) {
1776 for (; *end && (*end != sep); end++)
1777 ; /* Point to end of component */
1778 strlen = end - str;
1779 if (strlen) {
1780 if ((*buflen -= ((XDR_QUADLEN(strlen) << 2) + 4)) < 0)
1781 return nfserr_resource;
1782 WRITE32(strlen);
1783 WRITEMEM(str, strlen);
1784 count++;
1785 }
1786 else
1787 end++;
1788 str = end;
1789 }
1790 *pp = p;
1791 p = countp;
1792 WRITE32(count);
1793 return 0;
1794}
1795
1796/*
1797 * encode a location element of a fs_locations structure
1798 */
Al Virob37ad282006-10-19 23:28:59 -07001799static __be32 nfsd4_encode_fs_location4(struct nfsd4_fs_location *location,
Al Viro2ebbc012006-10-19 23:28:58 -07001800 __be32 **pp, int *buflen)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001801{
Al Virob37ad282006-10-19 23:28:59 -07001802 __be32 status;
Al Viro2ebbc012006-10-19 23:28:58 -07001803 __be32 *p = *pp;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001804
1805 status = nfsd4_encode_components(':', location->hosts, &p, buflen);
1806 if (status)
1807 return status;
1808 status = nfsd4_encode_components('/', location->path, &p, buflen);
1809 if (status)
1810 return status;
1811 *pp = p;
1812 return 0;
1813}
1814
1815/*
Trond Myklebusted748aa2011-09-12 19:37:06 -04001816 * Encode a path in RFC3530 'pathname4' format
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001817 */
Trond Myklebusted748aa2011-09-12 19:37:06 -04001818static __be32 nfsd4_encode_path(const struct path *root,
1819 const struct path *path, __be32 **pp, int *buflen)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001820{
Trond Myklebusted748aa2011-09-12 19:37:06 -04001821 struct path cur = {
1822 .mnt = path->mnt,
1823 .dentry = path->dentry,
1824 };
1825 __be32 *p = *pp;
1826 struct dentry **components = NULL;
1827 unsigned int ncomponents = 0;
1828 __be32 err = nfserr_jukebox;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001829
Trond Myklebusted748aa2011-09-12 19:37:06 -04001830 dprintk("nfsd4_encode_components(");
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001831
Trond Myklebusted748aa2011-09-12 19:37:06 -04001832 path_get(&cur);
1833 /* First walk the path up to the nfsd root, and store the
1834 * dentries/path components in an array.
1835 */
1836 for (;;) {
1837 if (cur.dentry == root->dentry && cur.mnt == root->mnt)
1838 break;
1839 if (cur.dentry == cur.mnt->mnt_root) {
1840 if (follow_up(&cur))
1841 continue;
1842 goto out_free;
1843 }
1844 if ((ncomponents & 15) == 0) {
1845 struct dentry **new;
1846 new = krealloc(components,
1847 sizeof(*new) * (ncomponents + 16),
1848 GFP_KERNEL);
1849 if (!new)
1850 goto out_free;
1851 components = new;
1852 }
1853 components[ncomponents++] = cur.dentry;
1854 cur.dentry = dget_parent(cur.dentry);
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001855 }
Trond Myklebusted748aa2011-09-12 19:37:06 -04001856
1857 *buflen -= 4;
1858 if (*buflen < 0)
1859 goto out_free;
1860 WRITE32(ncomponents);
1861
1862 while (ncomponents) {
1863 struct dentry *dentry = components[ncomponents - 1];
1864 unsigned int len = dentry->d_name.len;
1865
1866 *buflen -= 4 + (XDR_QUADLEN(len) << 2);
1867 if (*buflen < 0)
1868 goto out_free;
1869 WRITE32(len);
1870 WRITEMEM(dentry->d_name.name, len);
1871 dprintk("/%s", dentry->d_name.name);
1872 dput(dentry);
1873 ncomponents--;
1874 }
1875
1876 *pp = p;
1877 err = 0;
1878out_free:
1879 dprintk(")\n");
1880 while (ncomponents)
1881 dput(components[--ncomponents]);
1882 kfree(components);
1883 path_put(&cur);
1884 return err;
1885}
1886
1887static __be32 nfsd4_encode_fsloc_fsroot(struct svc_rqst *rqstp,
1888 const struct path *path, __be32 **pp, int *buflen)
1889{
1890 struct svc_export *exp_ps;
1891 __be32 res;
1892
1893 exp_ps = rqst_find_fsidzero_export(rqstp);
1894 if (IS_ERR(exp_ps))
1895 return nfserrno(PTR_ERR(exp_ps));
1896 res = nfsd4_encode_path(&exp_ps->ex_path, path, pp, buflen);
1897 exp_put(exp_ps);
1898 return res;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001899}
1900
1901/*
1902 * encode a fs_locations structure
1903 */
Al Virob37ad282006-10-19 23:28:59 -07001904static __be32 nfsd4_encode_fs_locations(struct svc_rqst *rqstp,
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001905 struct svc_export *exp,
Al Viro2ebbc012006-10-19 23:28:58 -07001906 __be32 **pp, int *buflen)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001907{
Al Virob37ad282006-10-19 23:28:59 -07001908 __be32 status;
Al Virocc45f012006-10-19 23:28:44 -07001909 int i;
Al Viro2ebbc012006-10-19 23:28:58 -07001910 __be32 *p = *pp;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001911 struct nfsd4_fs_locations *fslocs = &exp->ex_fslocs;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001912
Trond Myklebusted748aa2011-09-12 19:37:06 -04001913 status = nfsd4_encode_fsloc_fsroot(rqstp, &exp->ex_path, &p, buflen);
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001914 if (status)
1915 return status;
1916 if ((*buflen -= 4) < 0)
1917 return nfserr_resource;
1918 WRITE32(fslocs->locations_count);
1919 for (i=0; i<fslocs->locations_count; i++) {
1920 status = nfsd4_encode_fs_location4(&fslocs->locations[i],
1921 &p, buflen);
1922 if (status)
1923 return status;
1924 }
1925 *pp = p;
1926 return 0;
1927}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928
J. Bruce Fields3d2544b2011-08-15 11:49:30 -04001929static u32 nfs4_file_type(umode_t mode)
1930{
1931 switch (mode & S_IFMT) {
1932 case S_IFIFO: return NF4FIFO;
1933 case S_IFCHR: return NF4CHR;
1934 case S_IFDIR: return NF4DIR;
1935 case S_IFBLK: return NF4BLK;
1936 case S_IFLNK: return NF4LNK;
1937 case S_IFREG: return NF4REG;
1938 case S_IFSOCK: return NF4SOCK;
1939 default: return NF4BAD;
1940 };
1941}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942
Al Virob37ad282006-10-19 23:28:59 -07001943static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944nfsd4_encode_name(struct svc_rqst *rqstp, int whotype, uid_t id, int group,
Al Viro2ebbc012006-10-19 23:28:58 -07001945 __be32 **p, int *buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946{
1947 int status;
1948
1949 if (*buflen < (XDR_QUADLEN(IDMAP_NAMESZ) << 2) + 4)
1950 return nfserr_resource;
1951 if (whotype != NFS4_ACL_WHO_NAMED)
1952 status = nfs4_acl_write_who(whotype, (u8 *)(*p + 1));
1953 else if (group)
1954 status = nfsd_map_gid_to_name(rqstp, id, (u8 *)(*p + 1));
1955 else
1956 status = nfsd_map_uid_to_name(rqstp, id, (u8 *)(*p + 1));
1957 if (status < 0)
1958 return nfserrno(status);
1959 *p = xdr_encode_opaque(*p, NULL, status);
1960 *buflen -= (XDR_QUADLEN(status) << 2) + 4;
1961 BUG_ON(*buflen < 0);
1962 return 0;
1963}
1964
Al Virob37ad282006-10-19 23:28:59 -07001965static inline __be32
Al Viro2ebbc012006-10-19 23:28:58 -07001966nfsd4_encode_user(struct svc_rqst *rqstp, uid_t uid, __be32 **p, int *buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967{
1968 return nfsd4_encode_name(rqstp, NFS4_ACL_WHO_NAMED, uid, 0, p, buflen);
1969}
1970
Al Virob37ad282006-10-19 23:28:59 -07001971static inline __be32
Al Viro2ebbc012006-10-19 23:28:58 -07001972nfsd4_encode_group(struct svc_rqst *rqstp, uid_t gid, __be32 **p, int *buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973{
1974 return nfsd4_encode_name(rqstp, NFS4_ACL_WHO_NAMED, gid, 1, p, buflen);
1975}
1976
Al Virob37ad282006-10-19 23:28:59 -07001977static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978nfsd4_encode_aclname(struct svc_rqst *rqstp, int whotype, uid_t id, int group,
Al Viro2ebbc012006-10-19 23:28:58 -07001979 __be32 **p, int *buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980{
1981 return nfsd4_encode_name(rqstp, whotype, id, group, p, buflen);
1982}
1983
J.Bruce Fields42ca0992006-10-04 02:16:20 -07001984#define WORD0_ABSENT_FS_ATTRS (FATTR4_WORD0_FS_LOCATIONS | FATTR4_WORD0_FSID | \
1985 FATTR4_WORD0_RDATTR_ERROR)
1986#define WORD1_ABSENT_FS_ATTRS FATTR4_WORD1_MOUNTED_ON_FILEID
1987
Al Virob37ad282006-10-19 23:28:59 -07001988static __be32 fattr_handle_absent_fs(u32 *bmval0, u32 *bmval1, u32 *rdattr_err)
J.Bruce Fields42ca0992006-10-04 02:16:20 -07001989{
1990 /* As per referral draft: */
1991 if (*bmval0 & ~WORD0_ABSENT_FS_ATTRS ||
1992 *bmval1 & ~WORD1_ABSENT_FS_ATTRS) {
1993 if (*bmval0 & FATTR4_WORD0_RDATTR_ERROR ||
1994 *bmval0 & FATTR4_WORD0_FS_LOCATIONS)
1995 *rdattr_err = NFSERR_MOVED;
1996 else
1997 return nfserr_moved;
1998 }
1999 *bmval0 &= WORD0_ABSENT_FS_ATTRS;
2000 *bmval1 &= WORD1_ABSENT_FS_ATTRS;
2001 return 0;
2002}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003
2004/*
2005 * Note: @fhp can be NULL; in this case, we might have to compose the filehandle
2006 * ourselves.
2007 *
2008 * @countp is the buffer size in _words_; upon successful return this becomes
2009 * replaced with the number of words written.
2010 */
Al Virob37ad282006-10-19 23:28:59 -07002011__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012nfsd4_encode_fattr(struct svc_fh *fhp, struct svc_export *exp,
Al Viro2ebbc012006-10-19 23:28:58 -07002013 struct dentry *dentry, __be32 *buffer, int *countp, u32 *bmval,
Frank Filz406a7ea2007-11-27 11:34:05 -08002014 struct svc_rqst *rqstp, int ignore_crossmnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015{
2016 u32 bmval0 = bmval[0];
2017 u32 bmval1 = bmval[1];
Andy Adamson7e705702009-04-03 08:29:11 +03002018 u32 bmval2 = bmval[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 struct kstat stat;
2020 struct svc_fh tempfh;
2021 struct kstatfs statfs;
2022 int buflen = *countp << 2;
Al Viro2ebbc012006-10-19 23:28:58 -07002023 __be32 *attrlenp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 u32 dummy;
2025 u64 dummy64;
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002026 u32 rdattr_err = 0;
Al Viro2ebbc012006-10-19 23:28:58 -07002027 __be32 *p = buffer;
Al Virob37ad282006-10-19 23:28:59 -07002028 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07002029 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 int aclsupport = 0;
2031 struct nfs4_acl *acl = NULL;
Andy Adamson7e705702009-04-03 08:29:11 +03002032 struct nfsd4_compoundres *resp = rqstp->rq_resp;
2033 u32 minorversion = resp->cstate.minorversion;
Christoph Hellwigebabe9a2010-07-07 18:53:11 +02002034 struct path path = {
2035 .mnt = exp->ex_path.mnt,
2036 .dentry = dentry,
2037 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038
2039 BUG_ON(bmval1 & NFSD_WRITEONLY_ATTRS_WORD1);
Andy Adamson7e705702009-04-03 08:29:11 +03002040 BUG_ON(bmval0 & ~nfsd_suppattrs0(minorversion));
2041 BUG_ON(bmval1 & ~nfsd_suppattrs1(minorversion));
2042 BUG_ON(bmval2 & ~nfsd_suppattrs2(minorversion));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002044 if (exp->ex_fslocs.migrated) {
Andy Adamson7e705702009-04-03 08:29:11 +03002045 BUG_ON(bmval[2]);
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002046 status = fattr_handle_absent_fs(&bmval0, &bmval1, &rdattr_err);
2047 if (status)
2048 goto out;
2049 }
2050
Jan Blunck54775492008-02-14 19:38:39 -08002051 err = vfs_getattr(exp->ex_path.mnt, dentry, &stat);
Al Virob8dd7b92006-10-19 23:29:01 -07002052 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 goto out_nfserr;
J. Bruce Fieldsa16e92e2007-09-28 16:45:51 -04002054 if ((bmval0 & (FATTR4_WORD0_FILES_FREE | FATTR4_WORD0_FILES_TOTAL |
2055 FATTR4_WORD0_MAXNAME)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 (bmval1 & (FATTR4_WORD1_SPACE_AVAIL | FATTR4_WORD1_SPACE_FREE |
2057 FATTR4_WORD1_SPACE_TOTAL))) {
Christoph Hellwigebabe9a2010-07-07 18:53:11 +02002058 err = vfs_statfs(&path, &statfs);
Al Virob8dd7b92006-10-19 23:29:01 -07002059 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 goto out_nfserr;
2061 }
2062 if ((bmval0 & (FATTR4_WORD0_FILEHANDLE | FATTR4_WORD0_FSID)) && !fhp) {
2063 fh_init(&tempfh, NFS4_FHSIZE);
2064 status = fh_compose(&tempfh, exp, dentry, NULL);
2065 if (status)
2066 goto out;
2067 fhp = &tempfh;
2068 }
2069 if (bmval0 & (FATTR4_WORD0_ACL | FATTR4_WORD0_ACLSUPPORT
2070 | FATTR4_WORD0_SUPPORTED_ATTRS)) {
Al Virob8dd7b92006-10-19 23:29:01 -07002071 err = nfsd4_get_nfs4_acl(rqstp, dentry, &acl);
2072 aclsupport = (err == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 if (bmval0 & FATTR4_WORD0_ACL) {
Al Virob8dd7b92006-10-19 23:29:01 -07002074 if (err == -EOPNOTSUPP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 bmval0 &= ~FATTR4_WORD0_ACL;
Al Virob8dd7b92006-10-19 23:29:01 -07002076 else if (err == -EINVAL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 status = nfserr_attrnotsupp;
2078 goto out;
Al Virob8dd7b92006-10-19 23:29:01 -07002079 } else if (err != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 goto out_nfserr;
2081 }
2082 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083
Benny Halevy2b44f1b2010-09-30 20:47:46 +02002084 if (bmval2) {
2085 if ((buflen -= 16) < 0)
2086 goto out_resource;
Andy Adamson7e705702009-04-03 08:29:11 +03002087 WRITE32(3);
2088 WRITE32(bmval0);
2089 WRITE32(bmval1);
2090 WRITE32(bmval2);
Benny Halevy2b44f1b2010-09-30 20:47:46 +02002091 } else if (bmval1) {
2092 if ((buflen -= 12) < 0)
2093 goto out_resource;
Andy Adamson7e705702009-04-03 08:29:11 +03002094 WRITE32(2);
2095 WRITE32(bmval0);
2096 WRITE32(bmval1);
2097 } else {
Benny Halevy2b44f1b2010-09-30 20:47:46 +02002098 if ((buflen -= 8) < 0)
2099 goto out_resource;
Andy Adamson7e705702009-04-03 08:29:11 +03002100 WRITE32(1);
2101 WRITE32(bmval0);
2102 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 attrlenp = p++; /* to be backfilled later */
2104
2105 if (bmval0 & FATTR4_WORD0_SUPPORTED_ATTRS) {
Andy Adamson7e705702009-04-03 08:29:11 +03002106 u32 word0 = nfsd_suppattrs0(minorversion);
2107 u32 word1 = nfsd_suppattrs1(minorversion);
2108 u32 word2 = nfsd_suppattrs2(minorversion);
2109
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002110 if (!aclsupport)
2111 word0 &= ~FATTR4_WORD0_ACL;
Andy Adamson7e705702009-04-03 08:29:11 +03002112 if (!word2) {
Benny Halevy2b44f1b2010-09-30 20:47:46 +02002113 if ((buflen -= 12) < 0)
2114 goto out_resource;
Andy Adamson7e705702009-04-03 08:29:11 +03002115 WRITE32(2);
2116 WRITE32(word0);
2117 WRITE32(word1);
2118 } else {
Benny Halevy2b44f1b2010-09-30 20:47:46 +02002119 if ((buflen -= 16) < 0)
2120 goto out_resource;
Andy Adamson7e705702009-04-03 08:29:11 +03002121 WRITE32(3);
2122 WRITE32(word0);
2123 WRITE32(word1);
2124 WRITE32(word2);
2125 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 }
2127 if (bmval0 & FATTR4_WORD0_TYPE) {
2128 if ((buflen -= 4) < 0)
2129 goto out_resource;
J. Bruce Fields3d2544b2011-08-15 11:49:30 -04002130 dummy = nfs4_file_type(stat.mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 if (dummy == NF4BAD)
2132 goto out_serverfault;
2133 WRITE32(dummy);
2134 }
2135 if (bmval0 & FATTR4_WORD0_FH_EXPIRE_TYPE) {
2136 if ((buflen -= 4) < 0)
2137 goto out_resource;
NeilBrown49640002005-06-23 22:02:58 -07002138 if (exp->ex_flags & NFSEXP_NOSUBTREECHECK)
NeilBrowne34ac862005-07-07 17:59:30 -07002139 WRITE32(NFS4_FH_PERSISTENT);
NeilBrown49640002005-06-23 22:02:58 -07002140 else
NeilBrowne34ac862005-07-07 17:59:30 -07002141 WRITE32(NFS4_FH_PERSISTENT|NFS4_FH_VOL_RENAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 }
2143 if (bmval0 & FATTR4_WORD0_CHANGE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 if ((buflen -= 8) < 0)
2145 goto out_resource;
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002146 write_change(&p, &stat, dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 }
2148 if (bmval0 & FATTR4_WORD0_SIZE) {
2149 if ((buflen -= 8) < 0)
2150 goto out_resource;
2151 WRITE64(stat.size);
2152 }
2153 if (bmval0 & FATTR4_WORD0_LINK_SUPPORT) {
2154 if ((buflen -= 4) < 0)
2155 goto out_resource;
2156 WRITE32(1);
2157 }
2158 if (bmval0 & FATTR4_WORD0_SYMLINK_SUPPORT) {
2159 if ((buflen -= 4) < 0)
2160 goto out_resource;
2161 WRITE32(1);
2162 }
2163 if (bmval0 & FATTR4_WORD0_NAMED_ATTR) {
2164 if ((buflen -= 4) < 0)
2165 goto out_resource;
2166 WRITE32(0);
2167 }
2168 if (bmval0 & FATTR4_WORD0_FSID) {
2169 if ((buflen -= 16) < 0)
2170 goto out_resource;
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002171 if (exp->ex_fslocs.migrated) {
2172 WRITE64(NFS4_REFERRAL_FSID_MAJOR);
2173 WRITE64(NFS4_REFERRAL_FSID_MINOR);
NeilBrownaf6a4e22007-02-14 00:33:12 -08002174 } else switch(fsid_source(fhp)) {
2175 case FSIDSOURCE_FSID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 WRITE64((u64)exp->ex_fsid);
2177 WRITE64((u64)0);
NeilBrownaf6a4e22007-02-14 00:33:12 -08002178 break;
2179 case FSIDSOURCE_DEV:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 WRITE32(0);
2181 WRITE32(MAJOR(stat.dev));
2182 WRITE32(0);
2183 WRITE32(MINOR(stat.dev));
NeilBrownaf6a4e22007-02-14 00:33:12 -08002184 break;
2185 case FSIDSOURCE_UUID:
2186 WRITEMEM(exp->ex_uuid, 16);
2187 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 }
2189 }
2190 if (bmval0 & FATTR4_WORD0_UNIQUE_HANDLES) {
2191 if ((buflen -= 4) < 0)
2192 goto out_resource;
2193 WRITE32(0);
2194 }
2195 if (bmval0 & FATTR4_WORD0_LEASE_TIME) {
2196 if ((buflen -= 4) < 0)
2197 goto out_resource;
J. Bruce Fieldscf07d2e2010-02-28 23:20:19 -05002198 WRITE32(nfsd4_lease);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 }
2200 if (bmval0 & FATTR4_WORD0_RDATTR_ERROR) {
2201 if ((buflen -= 4) < 0)
2202 goto out_resource;
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002203 WRITE32(rdattr_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 }
2205 if (bmval0 & FATTR4_WORD0_ACL) {
2206 struct nfs4_ace *ace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207
2208 if (acl == NULL) {
2209 if ((buflen -= 4) < 0)
2210 goto out_resource;
2211
2212 WRITE32(0);
2213 goto out_acl;
2214 }
2215 if ((buflen -= 4) < 0)
2216 goto out_resource;
2217 WRITE32(acl->naces);
2218
J. Bruce Fields28e05dd2007-02-16 01:28:30 -08002219 for (ace = acl->aces; ace < acl->aces + acl->naces; ace++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 if ((buflen -= 4*3) < 0)
2221 goto out_resource;
2222 WRITE32(ace->type);
2223 WRITE32(ace->flag);
2224 WRITE32(ace->access_mask & NFS4_ACE_MASK_ALL);
2225 status = nfsd4_encode_aclname(rqstp, ace->whotype,
2226 ace->who, ace->flag & NFS4_ACE_IDENTIFIER_GROUP,
2227 &p, &buflen);
2228 if (status == nfserr_resource)
2229 goto out_resource;
2230 if (status)
2231 goto out;
2232 }
2233 }
2234out_acl:
2235 if (bmval0 & FATTR4_WORD0_ACLSUPPORT) {
2236 if ((buflen -= 4) < 0)
2237 goto out_resource;
2238 WRITE32(aclsupport ?
2239 ACL4_SUPPORT_ALLOW_ACL|ACL4_SUPPORT_DENY_ACL : 0);
2240 }
2241 if (bmval0 & FATTR4_WORD0_CANSETTIME) {
2242 if ((buflen -= 4) < 0)
2243 goto out_resource;
2244 WRITE32(1);
2245 }
2246 if (bmval0 & FATTR4_WORD0_CASE_INSENSITIVE) {
2247 if ((buflen -= 4) < 0)
2248 goto out_resource;
2249 WRITE32(1);
2250 }
2251 if (bmval0 & FATTR4_WORD0_CASE_PRESERVING) {
2252 if ((buflen -= 4) < 0)
2253 goto out_resource;
2254 WRITE32(1);
2255 }
2256 if (bmval0 & FATTR4_WORD0_CHOWN_RESTRICTED) {
2257 if ((buflen -= 4) < 0)
2258 goto out_resource;
2259 WRITE32(1);
2260 }
2261 if (bmval0 & FATTR4_WORD0_FILEHANDLE) {
2262 buflen -= (XDR_QUADLEN(fhp->fh_handle.fh_size) << 2) + 4;
2263 if (buflen < 0)
2264 goto out_resource;
2265 WRITE32(fhp->fh_handle.fh_size);
2266 WRITEMEM(&fhp->fh_handle.fh_base, fhp->fh_handle.fh_size);
2267 }
2268 if (bmval0 & FATTR4_WORD0_FILEID) {
2269 if ((buflen -= 8) < 0)
2270 goto out_resource;
Peter Staubach40ee5dc2007-08-16 12:10:07 -04002271 WRITE64(stat.ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 }
2273 if (bmval0 & FATTR4_WORD0_FILES_AVAIL) {
2274 if ((buflen -= 8) < 0)
2275 goto out_resource;
2276 WRITE64((u64) statfs.f_ffree);
2277 }
2278 if (bmval0 & FATTR4_WORD0_FILES_FREE) {
2279 if ((buflen -= 8) < 0)
2280 goto out_resource;
2281 WRITE64((u64) statfs.f_ffree);
2282 }
2283 if (bmval0 & FATTR4_WORD0_FILES_TOTAL) {
2284 if ((buflen -= 8) < 0)
2285 goto out_resource;
2286 WRITE64((u64) statfs.f_files);
2287 }
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002288 if (bmval0 & FATTR4_WORD0_FS_LOCATIONS) {
2289 status = nfsd4_encode_fs_locations(rqstp, exp, &p, &buflen);
2290 if (status == nfserr_resource)
2291 goto out_resource;
2292 if (status)
2293 goto out;
2294 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 if (bmval0 & FATTR4_WORD0_HOMOGENEOUS) {
2296 if ((buflen -= 4) < 0)
2297 goto out_resource;
2298 WRITE32(1);
2299 }
2300 if (bmval0 & FATTR4_WORD0_MAXFILESIZE) {
2301 if ((buflen -= 8) < 0)
2302 goto out_resource;
2303 WRITE64(~(u64)0);
2304 }
2305 if (bmval0 & FATTR4_WORD0_MAXLINK) {
2306 if ((buflen -= 4) < 0)
2307 goto out_resource;
2308 WRITE32(255);
2309 }
2310 if (bmval0 & FATTR4_WORD0_MAXNAME) {
2311 if ((buflen -= 4) < 0)
2312 goto out_resource;
J. Bruce Fieldsa16e92e2007-09-28 16:45:51 -04002313 WRITE32(statfs.f_namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 }
2315 if (bmval0 & FATTR4_WORD0_MAXREAD) {
2316 if ((buflen -= 8) < 0)
2317 goto out_resource;
Greg Banks7adae482006-10-04 02:15:47 -07002318 WRITE64((u64) svc_max_payload(rqstp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 }
2320 if (bmval0 & FATTR4_WORD0_MAXWRITE) {
2321 if ((buflen -= 8) < 0)
2322 goto out_resource;
Greg Banks7adae482006-10-04 02:15:47 -07002323 WRITE64((u64) svc_max_payload(rqstp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 }
2325 if (bmval1 & FATTR4_WORD1_MODE) {
2326 if ((buflen -= 4) < 0)
2327 goto out_resource;
2328 WRITE32(stat.mode & S_IALLUGO);
2329 }
2330 if (bmval1 & FATTR4_WORD1_NO_TRUNC) {
2331 if ((buflen -= 4) < 0)
2332 goto out_resource;
2333 WRITE32(1);
2334 }
2335 if (bmval1 & FATTR4_WORD1_NUMLINKS) {
2336 if ((buflen -= 4) < 0)
2337 goto out_resource;
2338 WRITE32(stat.nlink);
2339 }
2340 if (bmval1 & FATTR4_WORD1_OWNER) {
2341 status = nfsd4_encode_user(rqstp, stat.uid, &p, &buflen);
2342 if (status == nfserr_resource)
2343 goto out_resource;
2344 if (status)
2345 goto out;
2346 }
2347 if (bmval1 & FATTR4_WORD1_OWNER_GROUP) {
2348 status = nfsd4_encode_group(rqstp, stat.gid, &p, &buflen);
2349 if (status == nfserr_resource)
2350 goto out_resource;
2351 if (status)
2352 goto out;
2353 }
2354 if (bmval1 & FATTR4_WORD1_RAWDEV) {
2355 if ((buflen -= 8) < 0)
2356 goto out_resource;
2357 WRITE32((u32) MAJOR(stat.rdev));
2358 WRITE32((u32) MINOR(stat.rdev));
2359 }
2360 if (bmval1 & FATTR4_WORD1_SPACE_AVAIL) {
2361 if ((buflen -= 8) < 0)
2362 goto out_resource;
2363 dummy64 = (u64)statfs.f_bavail * (u64)statfs.f_bsize;
2364 WRITE64(dummy64);
2365 }
2366 if (bmval1 & FATTR4_WORD1_SPACE_FREE) {
2367 if ((buflen -= 8) < 0)
2368 goto out_resource;
2369 dummy64 = (u64)statfs.f_bfree * (u64)statfs.f_bsize;
2370 WRITE64(dummy64);
2371 }
2372 if (bmval1 & FATTR4_WORD1_SPACE_TOTAL) {
2373 if ((buflen -= 8) < 0)
2374 goto out_resource;
2375 dummy64 = (u64)statfs.f_blocks * (u64)statfs.f_bsize;
2376 WRITE64(dummy64);
2377 }
2378 if (bmval1 & FATTR4_WORD1_SPACE_USED) {
2379 if ((buflen -= 8) < 0)
2380 goto out_resource;
2381 dummy64 = (u64)stat.blocks << 9;
2382 WRITE64(dummy64);
2383 }
2384 if (bmval1 & FATTR4_WORD1_TIME_ACCESS) {
2385 if ((buflen -= 12) < 0)
2386 goto out_resource;
2387 WRITE32(0);
2388 WRITE32(stat.atime.tv_sec);
2389 WRITE32(stat.atime.tv_nsec);
2390 }
2391 if (bmval1 & FATTR4_WORD1_TIME_DELTA) {
2392 if ((buflen -= 12) < 0)
2393 goto out_resource;
2394 WRITE32(0);
2395 WRITE32(1);
2396 WRITE32(0);
2397 }
2398 if (bmval1 & FATTR4_WORD1_TIME_METADATA) {
2399 if ((buflen -= 12) < 0)
2400 goto out_resource;
2401 WRITE32(0);
2402 WRITE32(stat.ctime.tv_sec);
2403 WRITE32(stat.ctime.tv_nsec);
2404 }
2405 if (bmval1 & FATTR4_WORD1_TIME_MODIFY) {
2406 if ((buflen -= 12) < 0)
2407 goto out_resource;
2408 WRITE32(0);
2409 WRITE32(stat.mtime.tv_sec);
2410 WRITE32(stat.mtime.tv_nsec);
2411 }
2412 if (bmval1 & FATTR4_WORD1_MOUNTED_ON_FILEID) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 if ((buflen -= 8) < 0)
2414 goto out_resource;
Frank Filz406a7ea2007-11-27 11:34:05 -08002415 /*
2416 * Get parent's attributes if not ignoring crossmount
2417 * and this is the root of a cross-mounted filesystem.
2418 */
2419 if (ignore_crossmnt == 0 &&
Al Viro462d6052010-01-30 16:11:21 -05002420 dentry == exp->ex_path.mnt->mnt_root) {
2421 struct path path = exp->ex_path;
2422 path_get(&path);
2423 while (follow_up(&path)) {
2424 if (path.dentry != path.mnt->mnt_root)
2425 break;
2426 }
2427 err = vfs_getattr(path.mnt, path.dentry, &stat);
2428 path_put(&path);
Peter Staubach40ee5dc2007-08-16 12:10:07 -04002429 if (err)
2430 goto out_nfserr;
2431 }
2432 WRITE64(stat.ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 }
Benny Halevy8c18f202009-04-03 08:29:14 +03002434 if (bmval2 & FATTR4_WORD2_SUPPATTR_EXCLCREAT) {
2435 WRITE32(3);
2436 WRITE32(NFSD_SUPPATTR_EXCLCREAT_WORD0);
2437 WRITE32(NFSD_SUPPATTR_EXCLCREAT_WORD1);
2438 WRITE32(NFSD_SUPPATTR_EXCLCREAT_WORD2);
2439 }
Andy Adamson7e705702009-04-03 08:29:11 +03002440
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 *attrlenp = htonl((char *)p - (char *)attrlenp - 4);
2442 *countp = p - buffer;
2443 status = nfs_ok;
2444
2445out:
J. Bruce Fields28e05dd2007-02-16 01:28:30 -08002446 kfree(acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 if (fhp == &tempfh)
2448 fh_put(&tempfh);
2449 return status;
2450out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07002451 status = nfserrno(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 goto out;
2453out_resource:
2454 *countp = 0;
2455 status = nfserr_resource;
2456 goto out;
2457out_serverfault:
2458 status = nfserr_serverfault;
2459 goto out;
2460}
2461
J. Bruce Fieldsc0ce6ec2008-02-11 15:48:47 -05002462static inline int attributes_need_mount(u32 *bmval)
2463{
2464 if (bmval[0] & ~(FATTR4_WORD0_RDATTR_ERROR | FATTR4_WORD0_LEASE_TIME))
2465 return 1;
2466 if (bmval[1] & ~FATTR4_WORD1_MOUNTED_ON_FILEID)
2467 return 1;
2468 return 0;
2469}
2470
Al Virob37ad282006-10-19 23:28:59 -07002471static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472nfsd4_encode_dirent_fattr(struct nfsd4_readdir *cd,
Al Viro2ebbc012006-10-19 23:28:58 -07002473 const char *name, int namlen, __be32 *p, int *buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474{
2475 struct svc_export *exp = cd->rd_fhp->fh_export;
2476 struct dentry *dentry;
Al Virob37ad282006-10-19 23:28:59 -07002477 __be32 nfserr;
Frank Filz406a7ea2007-11-27 11:34:05 -08002478 int ignore_crossmnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479
2480 dentry = lookup_one_len(name, cd->rd_fhp->fh_dentry, namlen);
2481 if (IS_ERR(dentry))
2482 return nfserrno(PTR_ERR(dentry));
J. Bruce Fieldsb2c0cea2009-05-05 19:04:29 -04002483 if (!dentry->d_inode) {
2484 /*
2485 * nfsd_buffered_readdir drops the i_mutex between
2486 * readdir and calling this callback, leaving a window
2487 * where this directory entry could have gone away.
2488 */
2489 dput(dentry);
2490 return nfserr_noent;
2491 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492
2493 exp_get(exp);
Frank Filz406a7ea2007-11-27 11:34:05 -08002494 /*
2495 * In the case of a mountpoint, the client may be asking for
2496 * attributes that are only properties of the underlying filesystem
2497 * as opposed to the cross-mounted file system. In such a case,
2498 * we will not follow the cross mount and will fill the attribtutes
2499 * directly from the mountpoint dentry.
2500 */
J. Bruce Fields3227fa42009-10-25 21:43:01 -04002501 if (nfsd_mountpoint(dentry, exp)) {
J.Bruce Fields021d3a72006-12-13 00:35:24 -08002502 int err;
2503
J. Bruce Fields3227fa42009-10-25 21:43:01 -04002504 if (!(exp->ex_flags & NFSEXP_V4ROOT)
2505 && !attributes_need_mount(cd->rd_bmval)) {
2506 ignore_crossmnt = 1;
2507 goto out_encode;
2508 }
Andy Adamsondcb488a32007-07-17 04:04:51 -07002509 /*
2510 * Why the heck aren't we just using nfsd_lookup??
2511 * Different "."/".." handling? Something else?
2512 * At least, add a comment here to explain....
2513 */
J.Bruce Fields021d3a72006-12-13 00:35:24 -08002514 err = nfsd_cross_mnt(cd->rd_rqstp, &dentry, &exp);
2515 if (err) {
2516 nfserr = nfserrno(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 goto out_put;
2518 }
Andy Adamsondcb488a32007-07-17 04:04:51 -07002519 nfserr = check_nfsd_access(exp, cd->rd_rqstp);
2520 if (nfserr)
2521 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522
2523 }
J. Bruce Fields3227fa42009-10-25 21:43:01 -04002524out_encode:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 nfserr = nfsd4_encode_fattr(NULL, exp, dentry, p, buflen, cd->rd_bmval,
Frank Filz406a7ea2007-11-27 11:34:05 -08002526 cd->rd_rqstp, ignore_crossmnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527out_put:
2528 dput(dentry);
2529 exp_put(exp);
2530 return nfserr;
2531}
2532
Al Viro2ebbc012006-10-19 23:28:58 -07002533static __be32 *
Al Virob37ad282006-10-19 23:28:59 -07002534nfsd4_encode_rdattr_error(__be32 *p, int buflen, __be32 nfserr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535{
Al Viro2ebbc012006-10-19 23:28:58 -07002536 __be32 *attrlenp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537
2538 if (buflen < 6)
2539 return NULL;
2540 *p++ = htonl(2);
2541 *p++ = htonl(FATTR4_WORD0_RDATTR_ERROR); /* bmval0 */
2542 *p++ = htonl(0); /* bmval1 */
2543
2544 attrlenp = p++;
2545 *p++ = nfserr; /* no htonl */
2546 *attrlenp = htonl((char *)p - (char *)attrlenp - 4);
2547 return p;
2548}
2549
2550static int
NeilBrowna0ad13e2007-01-26 00:57:10 -08002551nfsd4_encode_dirent(void *ccdv, const char *name, int namlen,
2552 loff_t offset, u64 ino, unsigned int d_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553{
NeilBrowna0ad13e2007-01-26 00:57:10 -08002554 struct readdir_cd *ccd = ccdv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 struct nfsd4_readdir *cd = container_of(ccd, struct nfsd4_readdir, common);
2556 int buflen;
Al Viro2ebbc012006-10-19 23:28:58 -07002557 __be32 *p = cd->buffer;
J. Bruce Fieldsb2c0cea2009-05-05 19:04:29 -04002558 __be32 *cookiep;
Al Virob37ad282006-10-19 23:28:59 -07002559 __be32 nfserr = nfserr_toosmall;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560
2561 /* In nfsv4, "." and ".." never make it onto the wire.. */
2562 if (name && isdotent(name, namlen)) {
2563 cd->common.err = nfs_ok;
2564 return 0;
2565 }
2566
2567 if (cd->offset)
2568 xdr_encode_hyper(cd->offset, (u64) offset);
2569
2570 buflen = cd->buflen - 4 - XDR_QUADLEN(namlen);
2571 if (buflen < 0)
2572 goto fail;
2573
2574 *p++ = xdr_one; /* mark entry present */
J. Bruce Fieldsb2c0cea2009-05-05 19:04:29 -04002575 cookiep = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 p = xdr_encode_hyper(p, NFS_OFFSET_MAX); /* offset of next entry */
2577 p = xdr_encode_array(p, name, namlen); /* name length & name */
2578
2579 nfserr = nfsd4_encode_dirent_fattr(cd, name, namlen, p, &buflen);
2580 switch (nfserr) {
2581 case nfs_ok:
2582 p += buflen;
2583 break;
2584 case nfserr_resource:
2585 nfserr = nfserr_toosmall;
2586 goto fail;
J. Bruce Fieldsb2c0cea2009-05-05 19:04:29 -04002587 case nfserr_noent:
2588 goto skip_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 default:
2590 /*
2591 * If the client requested the RDATTR_ERROR attribute,
2592 * we stuff the error code into this attribute
2593 * and continue. If this attribute was not requested,
2594 * then in accordance with the spec, we fail the
2595 * entire READDIR operation(!)
2596 */
2597 if (!(cd->rd_bmval[0] & FATTR4_WORD0_RDATTR_ERROR))
2598 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 p = nfsd4_encode_rdattr_error(p, buflen, nfserr);
Fred Isaman34081ef2006-01-18 17:43:40 -08002600 if (p == NULL) {
2601 nfserr = nfserr_toosmall;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 goto fail;
Fred Isaman34081ef2006-01-18 17:43:40 -08002603 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 }
2605 cd->buflen -= (p - cd->buffer);
2606 cd->buffer = p;
J. Bruce Fieldsb2c0cea2009-05-05 19:04:29 -04002607 cd->offset = cookiep;
2608skip_entry:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 cd->common.err = nfs_ok;
2610 return 0;
2611fail:
2612 cd->common.err = nfserr;
2613 return -EINVAL;
2614}
2615
Benny Halevye2f282b2008-08-12 20:45:07 +03002616static void
2617nfsd4_encode_stateid(struct nfsd4_compoundres *resp, stateid_t *sid)
2618{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002619 __be32 *p;
Benny Halevye2f282b2008-08-12 20:45:07 +03002620
2621 RESERVE_SPACE(sizeof(stateid_t));
2622 WRITE32(sid->si_generation);
2623 WRITEMEM(&sid->si_opaque, sizeof(stateid_opaque_t));
2624 ADJUST_ARGS();
2625}
2626
Benny Halevy695e12f2008-07-04 14:38:33 +03002627static __be32
Al Virob37ad282006-10-19 23:28:59 -07002628nfsd4_encode_access(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_access *access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002630 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631
2632 if (!nfserr) {
2633 RESERVE_SPACE(8);
2634 WRITE32(access->ac_supported);
2635 WRITE32(access->ac_resp_access);
2636 ADJUST_ARGS();
2637 }
Benny Halevy695e12f2008-07-04 14:38:33 +03002638 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639}
2640
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002641static __be32 nfsd4_encode_bind_conn_to_session(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_bind_conn_to_session *bcts)
2642{
2643 __be32 *p;
2644
2645 if (!nfserr) {
2646 RESERVE_SPACE(NFS4_MAX_SESSIONID_LEN + 8);
2647 WRITEMEM(bcts->sessionid.data, NFS4_MAX_SESSIONID_LEN);
2648 WRITE32(bcts->dir);
2649 /* XXX: ? */
2650 WRITE32(0);
2651 ADJUST_ARGS();
2652 }
2653 return nfserr;
2654}
2655
Benny Halevy695e12f2008-07-04 14:38:33 +03002656static __be32
Al Virob37ad282006-10-19 23:28:59 -07002657nfsd4_encode_close(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658{
2659 ENCODE_SEQID_OP_HEAD;
2660
Benny Halevye2f282b2008-08-12 20:45:07 +03002661 if (!nfserr)
2662 nfsd4_encode_stateid(resp, &close->cl_stateid);
2663
J. Bruce Fieldsf3e42232011-08-24 12:27:31 -04002664 encode_seqid_op_tail(resp, save, nfserr);
Benny Halevy695e12f2008-07-04 14:38:33 +03002665 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666}
2667
2668
Benny Halevy695e12f2008-07-04 14:38:33 +03002669static __be32
Al Virob37ad282006-10-19 23:28:59 -07002670nfsd4_encode_commit(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_commit *commit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002672 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673
2674 if (!nfserr) {
2675 RESERVE_SPACE(8);
2676 WRITEMEM(commit->co_verf.data, 8);
2677 ADJUST_ARGS();
2678 }
Benny Halevy695e12f2008-07-04 14:38:33 +03002679 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680}
2681
Benny Halevy695e12f2008-07-04 14:38:33 +03002682static __be32
Al Virob37ad282006-10-19 23:28:59 -07002683nfsd4_encode_create(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_create *create)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002685 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686
2687 if (!nfserr) {
2688 RESERVE_SPACE(32);
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002689 write_cinfo(&p, &create->cr_cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 WRITE32(2);
2691 WRITE32(create->cr_bmval[0]);
2692 WRITE32(create->cr_bmval[1]);
2693 ADJUST_ARGS();
2694 }
Benny Halevy695e12f2008-07-04 14:38:33 +03002695 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696}
2697
Al Virob37ad282006-10-19 23:28:59 -07002698static __be32
2699nfsd4_encode_getattr(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_getattr *getattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700{
2701 struct svc_fh *fhp = getattr->ga_fhp;
2702 int buflen;
2703
2704 if (nfserr)
2705 return nfserr;
2706
2707 buflen = resp->end - resp->p - (COMPOUND_ERR_SLACK_SPACE >> 2);
2708 nfserr = nfsd4_encode_fattr(fhp, fhp->fh_export, fhp->fh_dentry,
2709 resp->p, &buflen, getattr->ga_bmval,
Frank Filz406a7ea2007-11-27 11:34:05 -08002710 resp->rqstp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 if (!nfserr)
2712 resp->p += buflen;
2713 return nfserr;
2714}
2715
Benny Halevy695e12f2008-07-04 14:38:33 +03002716static __be32
2717nfsd4_encode_getfh(struct nfsd4_compoundres *resp, __be32 nfserr, struct svc_fh **fhpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718{
Benny Halevy695e12f2008-07-04 14:38:33 +03002719 struct svc_fh *fhp = *fhpp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 unsigned int len;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002721 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722
2723 if (!nfserr) {
2724 len = fhp->fh_handle.fh_size;
2725 RESERVE_SPACE(len + 4);
2726 WRITE32(len);
2727 WRITEMEM(&fhp->fh_handle.fh_base, len);
2728 ADJUST_ARGS();
2729 }
Benny Halevy695e12f2008-07-04 14:38:33 +03002730 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731}
2732
2733/*
2734* Including all fields other than the name, a LOCK4denied structure requires
2735* 8(clientid) + 4(namelen) + 8(offset) + 8(length) + 4(type) = 32 bytes.
2736*/
2737static void
2738nfsd4_encode_lock_denied(struct nfsd4_compoundres *resp, struct nfsd4_lock_denied *ld)
2739{
J. Bruce Fields7c13f342011-08-30 22:15:47 -04002740 struct xdr_netobj *conf = &ld->ld_owner;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002741 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742
J. Bruce Fields7c13f342011-08-30 22:15:47 -04002743 RESERVE_SPACE(32 + XDR_LEN(conf->len));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 WRITE64(ld->ld_start);
2745 WRITE64(ld->ld_length);
2746 WRITE32(ld->ld_type);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04002747 if (conf->len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 WRITEMEM(&ld->ld_clientid, 8);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04002749 WRITE32(conf->len);
2750 WRITEMEM(conf->data, conf->len);
2751 kfree(conf->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 } else { /* non - nfsv4 lock in conflict, no clientid nor owner */
2753 WRITE64((u64)0); /* clientid */
2754 WRITE32(0); /* length of owner name */
2755 }
2756 ADJUST_ARGS();
2757}
2758
Benny Halevy695e12f2008-07-04 14:38:33 +03002759static __be32
Al Virob37ad282006-10-19 23:28:59 -07002760nfsd4_encode_lock(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 ENCODE_SEQID_OP_HEAD;
2763
Benny Halevye2f282b2008-08-12 20:45:07 +03002764 if (!nfserr)
2765 nfsd4_encode_stateid(resp, &lock->lk_resp_stateid);
2766 else if (nfserr == nfserr_denied)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 nfsd4_encode_lock_denied(resp, &lock->lk_denied);
2768
J. Bruce Fieldsf3e42232011-08-24 12:27:31 -04002769 encode_seqid_op_tail(resp, save, nfserr);
Benny Halevy695e12f2008-07-04 14:38:33 +03002770 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771}
2772
Benny Halevy695e12f2008-07-04 14:38:33 +03002773static __be32
Al Virob37ad282006-10-19 23:28:59 -07002774nfsd4_encode_lockt(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775{
2776 if (nfserr == nfserr_denied)
2777 nfsd4_encode_lock_denied(resp, &lockt->lt_denied);
Benny Halevy695e12f2008-07-04 14:38:33 +03002778 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779}
2780
Benny Halevy695e12f2008-07-04 14:38:33 +03002781static __be32
Al Virob37ad282006-10-19 23:28:59 -07002782nfsd4_encode_locku(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783{
2784 ENCODE_SEQID_OP_HEAD;
2785
Benny Halevye2f282b2008-08-12 20:45:07 +03002786 if (!nfserr)
2787 nfsd4_encode_stateid(resp, &locku->lu_stateid);
2788
J. Bruce Fieldsf3e42232011-08-24 12:27:31 -04002789 encode_seqid_op_tail(resp, save, nfserr);
Benny Halevy695e12f2008-07-04 14:38:33 +03002790 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791}
2792
2793
Benny Halevy695e12f2008-07-04 14:38:33 +03002794static __be32
Al Virob37ad282006-10-19 23:28:59 -07002795nfsd4_encode_link(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_link *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002797 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798
2799 if (!nfserr) {
2800 RESERVE_SPACE(20);
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002801 write_cinfo(&p, &link->li_cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 ADJUST_ARGS();
2803 }
Benny Halevy695e12f2008-07-04 14:38:33 +03002804 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805}
2806
2807
Benny Halevy695e12f2008-07-04 14:38:33 +03002808static __be32
Al Virob37ad282006-10-19 23:28:59 -07002809nfsd4_encode_open(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002811 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 ENCODE_SEQID_OP_HEAD;
2813
2814 if (nfserr)
2815 goto out;
2816
Benny Halevye2f282b2008-08-12 20:45:07 +03002817 nfsd4_encode_stateid(resp, &open->op_stateid);
2818 RESERVE_SPACE(40);
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002819 write_cinfo(&p, &open->op_cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 WRITE32(open->op_rflags);
2821 WRITE32(2);
2822 WRITE32(open->op_bmval[0]);
2823 WRITE32(open->op_bmval[1]);
2824 WRITE32(open->op_delegate_type);
2825 ADJUST_ARGS();
2826
2827 switch (open->op_delegate_type) {
2828 case NFS4_OPEN_DELEGATE_NONE:
2829 break;
2830 case NFS4_OPEN_DELEGATE_READ:
Benny Halevye2f282b2008-08-12 20:45:07 +03002831 nfsd4_encode_stateid(resp, &open->op_delegate_stateid);
2832 RESERVE_SPACE(20);
NeilBrown7b190fe2005-06-23 22:03:23 -07002833 WRITE32(open->op_recall);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834
2835 /*
2836 * TODO: ACE's in delegations
2837 */
2838 WRITE32(NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
2839 WRITE32(0);
2840 WRITE32(0);
2841 WRITE32(0); /* XXX: is NULL principal ok? */
2842 ADJUST_ARGS();
2843 break;
2844 case NFS4_OPEN_DELEGATE_WRITE:
Benny Halevye2f282b2008-08-12 20:45:07 +03002845 nfsd4_encode_stateid(resp, &open->op_delegate_stateid);
2846 RESERVE_SPACE(32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 WRITE32(0);
2848
2849 /*
2850 * TODO: space_limit's in delegations
2851 */
2852 WRITE32(NFS4_LIMIT_SIZE);
2853 WRITE32(~(u32)0);
2854 WRITE32(~(u32)0);
2855
2856 /*
2857 * TODO: ACE's in delegations
2858 */
2859 WRITE32(NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
2860 WRITE32(0);
2861 WRITE32(0);
2862 WRITE32(0); /* XXX: is NULL principal ok? */
2863 ADJUST_ARGS();
2864 break;
2865 default:
2866 BUG();
2867 }
2868 /* XXX save filehandle here */
2869out:
J. Bruce Fieldsf3e42232011-08-24 12:27:31 -04002870 encode_seqid_op_tail(resp, save, nfserr);
Benny Halevy695e12f2008-07-04 14:38:33 +03002871 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872}
2873
Benny Halevy695e12f2008-07-04 14:38:33 +03002874static __be32
Al Virob37ad282006-10-19 23:28:59 -07002875nfsd4_encode_open_confirm(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876{
2877 ENCODE_SEQID_OP_HEAD;
Benny Halevye2f282b2008-08-12 20:45:07 +03002878
2879 if (!nfserr)
2880 nfsd4_encode_stateid(resp, &oc->oc_resp_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881
J. Bruce Fieldsf3e42232011-08-24 12:27:31 -04002882 encode_seqid_op_tail(resp, save, nfserr);
Benny Halevy695e12f2008-07-04 14:38:33 +03002883 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884}
2885
Benny Halevy695e12f2008-07-04 14:38:33 +03002886static __be32
Al Virob37ad282006-10-19 23:28:59 -07002887nfsd4_encode_open_downgrade(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888{
2889 ENCODE_SEQID_OP_HEAD;
Benny Halevye2f282b2008-08-12 20:45:07 +03002890
2891 if (!nfserr)
2892 nfsd4_encode_stateid(resp, &od->od_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893
J. Bruce Fieldsf3e42232011-08-24 12:27:31 -04002894 encode_seqid_op_tail(resp, save, nfserr);
Benny Halevy695e12f2008-07-04 14:38:33 +03002895 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896}
2897
Al Virob37ad282006-10-19 23:28:59 -07002898static __be32
2899nfsd4_encode_read(struct nfsd4_compoundres *resp, __be32 nfserr,
NeilBrown44524352006-10-04 02:15:46 -07002900 struct nfsd4_read *read)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901{
2902 u32 eof;
2903 int v, pn;
2904 unsigned long maxcount;
2905 long len;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002906 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907
2908 if (nfserr)
2909 return nfserr;
2910 if (resp->xbuf->page_len)
2911 return nfserr_resource;
2912
2913 RESERVE_SPACE(8); /* eof flag and byte count */
2914
Greg Banks7adae482006-10-04 02:15:47 -07002915 maxcount = svc_max_payload(resp->rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 if (maxcount > read->rd_length)
2917 maxcount = read->rd_length;
2918
2919 len = maxcount;
2920 v = 0;
2921 while (len > 0) {
NeilBrown44524352006-10-04 02:15:46 -07002922 pn = resp->rqstp->rq_resused++;
NeilBrown3cc03b12006-10-04 02:15:47 -07002923 resp->rqstp->rq_vec[v].iov_base =
NeilBrown44524352006-10-04 02:15:46 -07002924 page_address(resp->rqstp->rq_respages[pn]);
NeilBrown3cc03b12006-10-04 02:15:47 -07002925 resp->rqstp->rq_vec[v].iov_len =
NeilBrown44524352006-10-04 02:15:46 -07002926 len < PAGE_SIZE ? len : PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 v++;
2928 len -= PAGE_SIZE;
2929 }
2930 read->rd_vlen = v;
2931
J. Bruce Fields039a87c2010-07-30 11:33:32 -04002932 nfserr = nfsd_read_file(read->rd_rqstp, read->rd_fhp, read->rd_filp,
NeilBrown3cc03b12006-10-04 02:15:47 -07002933 read->rd_offset, resp->rqstp->rq_vec, read->rd_vlen,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 &maxcount);
2935
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 if (nfserr)
2937 return nfserr;
NeilBrown44524352006-10-04 02:15:46 -07002938 eof = (read->rd_offset + maxcount >=
2939 read->rd_fhp->fh_dentry->d_inode->i_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940
2941 WRITE32(eof);
2942 WRITE32(maxcount);
2943 ADJUST_ARGS();
NeilBrown6ed6dec2006-04-10 22:55:32 -07002944 resp->xbuf->head[0].iov_len = (char*)p
2945 - (char*)resp->xbuf->head[0].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 resp->xbuf->page_len = maxcount;
2947
NeilBrown6ed6dec2006-04-10 22:55:32 -07002948 /* Use rest of head for padding and remaining ops: */
NeilBrown6ed6dec2006-04-10 22:55:32 -07002949 resp->xbuf->tail[0].iov_base = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 resp->xbuf->tail[0].iov_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 if (maxcount&3) {
NeilBrown6ed6dec2006-04-10 22:55:32 -07002952 RESERVE_SPACE(4);
2953 WRITE32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 resp->xbuf->tail[0].iov_base += maxcount&3;
2955 resp->xbuf->tail[0].iov_len = 4 - (maxcount&3);
NeilBrown6ed6dec2006-04-10 22:55:32 -07002956 ADJUST_ARGS();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 }
2958 return 0;
2959}
2960
Al Virob37ad282006-10-19 23:28:59 -07002961static __be32
2962nfsd4_encode_readlink(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_readlink *readlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963{
2964 int maxcount;
2965 char *page;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002966 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967
2968 if (nfserr)
2969 return nfserr;
2970 if (resp->xbuf->page_len)
2971 return nfserr_resource;
2972
NeilBrown44524352006-10-04 02:15:46 -07002973 page = page_address(resp->rqstp->rq_respages[resp->rqstp->rq_resused++]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974
2975 maxcount = PAGE_SIZE;
2976 RESERVE_SPACE(4);
2977
2978 /*
2979 * XXX: By default, the ->readlink() VFS op will truncate symlinks
2980 * if they would overflow the buffer. Is this kosher in NFSv4? If
2981 * not, one easy fix is: if ->readlink() precisely fills the buffer,
2982 * assume that truncation occurred, and return NFS4ERR_RESOURCE.
2983 */
2984 nfserr = nfsd_readlink(readlink->rl_rqstp, readlink->rl_fhp, page, &maxcount);
2985 if (nfserr == nfserr_isdir)
2986 return nfserr_inval;
2987 if (nfserr)
2988 return nfserr;
2989
2990 WRITE32(maxcount);
2991 ADJUST_ARGS();
NeilBrown6ed6dec2006-04-10 22:55:32 -07002992 resp->xbuf->head[0].iov_len = (char*)p
2993 - (char*)resp->xbuf->head[0].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 resp->xbuf->page_len = maxcount;
NeilBrown6ed6dec2006-04-10 22:55:32 -07002995
2996 /* Use rest of head for padding and remaining ops: */
NeilBrown6ed6dec2006-04-10 22:55:32 -07002997 resp->xbuf->tail[0].iov_base = p;
2998 resp->xbuf->tail[0].iov_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 if (maxcount&3) {
NeilBrown6ed6dec2006-04-10 22:55:32 -07003000 RESERVE_SPACE(4);
3001 WRITE32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 resp->xbuf->tail[0].iov_base += maxcount&3;
3003 resp->xbuf->tail[0].iov_len = 4 - (maxcount&3);
NeilBrown6ed6dec2006-04-10 22:55:32 -07003004 ADJUST_ARGS();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 }
3006 return 0;
3007}
3008
Al Virob37ad282006-10-19 23:28:59 -07003009static __be32
3010nfsd4_encode_readdir(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_readdir *readdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011{
3012 int maxcount;
3013 loff_t offset;
Al Viro2ebbc012006-10-19 23:28:58 -07003014 __be32 *page, *savep, *tailbase;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003015 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016
3017 if (nfserr)
3018 return nfserr;
3019 if (resp->xbuf->page_len)
3020 return nfserr_resource;
3021
3022 RESERVE_SPACE(8); /* verifier */
3023 savep = p;
3024
3025 /* XXX: Following NFSv3, we ignore the READDIR verifier for now. */
3026 WRITE32(0);
3027 WRITE32(0);
3028 ADJUST_ARGS();
3029 resp->xbuf->head[0].iov_len = ((char*)resp->p) - (char*)resp->xbuf->head[0].iov_base;
NeilBrownbb6e8a92006-04-10 22:55:33 -07003030 tailbase = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031
3032 maxcount = PAGE_SIZE;
3033 if (maxcount > readdir->rd_maxcount)
3034 maxcount = readdir->rd_maxcount;
3035
3036 /*
3037 * Convert from bytes to words, account for the two words already
3038 * written, make sure to leave two words at the end for the next
3039 * pointer and eof field.
3040 */
3041 maxcount = (maxcount >> 2) - 4;
3042 if (maxcount < 0) {
3043 nfserr = nfserr_toosmall;
3044 goto err_no_verf;
3045 }
3046
NeilBrown44524352006-10-04 02:15:46 -07003047 page = page_address(resp->rqstp->rq_respages[resp->rqstp->rq_resused++]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 readdir->common.err = 0;
3049 readdir->buflen = maxcount;
3050 readdir->buffer = page;
3051 readdir->offset = NULL;
3052
3053 offset = readdir->rd_cookie;
3054 nfserr = nfsd_readdir(readdir->rd_rqstp, readdir->rd_fhp,
3055 &offset,
3056 &readdir->common, nfsd4_encode_dirent);
3057 if (nfserr == nfs_ok &&
3058 readdir->common.err == nfserr_toosmall &&
3059 readdir->buffer == page)
3060 nfserr = nfserr_toosmall;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 if (nfserr)
3062 goto err_no_verf;
3063
3064 if (readdir->offset)
3065 xdr_encode_hyper(readdir->offset, offset);
3066
3067 p = readdir->buffer;
3068 *p++ = 0; /* no more entries */
3069 *p++ = htonl(readdir->common.err == nfserr_eof);
NeilBrown44524352006-10-04 02:15:46 -07003070 resp->xbuf->page_len = ((char*)p) - (char*)page_address(
3071 resp->rqstp->rq_respages[resp->rqstp->rq_resused-1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072
NeilBrownbb6e8a92006-04-10 22:55:33 -07003073 /* Use rest of head for padding and remaining ops: */
NeilBrownbb6e8a92006-04-10 22:55:33 -07003074 resp->xbuf->tail[0].iov_base = tailbase;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 resp->xbuf->tail[0].iov_len = 0;
3076 resp->p = resp->xbuf->tail[0].iov_base;
NeilBrownbb6e8a92006-04-10 22:55:33 -07003077 resp->end = resp->p + (PAGE_SIZE - resp->xbuf->head[0].iov_len)/4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078
3079 return 0;
3080err_no_verf:
3081 p = savep;
3082 ADJUST_ARGS();
3083 return nfserr;
3084}
3085
Benny Halevy695e12f2008-07-04 14:38:33 +03003086static __be32
Al Virob37ad282006-10-19 23:28:59 -07003087nfsd4_encode_remove(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_remove *remove)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003089 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090
3091 if (!nfserr) {
3092 RESERVE_SPACE(20);
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04003093 write_cinfo(&p, &remove->rm_cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 ADJUST_ARGS();
3095 }
Benny Halevy695e12f2008-07-04 14:38:33 +03003096 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097}
3098
Benny Halevy695e12f2008-07-04 14:38:33 +03003099static __be32
Al Virob37ad282006-10-19 23:28:59 -07003100nfsd4_encode_rename(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_rename *rename)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003102 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103
3104 if (!nfserr) {
3105 RESERVE_SPACE(40);
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04003106 write_cinfo(&p, &rename->rn_sinfo);
3107 write_cinfo(&p, &rename->rn_tinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 ADJUST_ARGS();
3109 }
Benny Halevy695e12f2008-07-04 14:38:33 +03003110 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111}
3112
Benny Halevy695e12f2008-07-04 14:38:33 +03003113static __be32
Mi Jinlong22b6dee2010-12-27 14:29:57 +08003114nfsd4_do_encode_secinfo(struct nfsd4_compoundres *resp,
3115 __be32 nfserr,struct svc_export *exp)
Andy Adamsondcb488a32007-07-17 04:04:51 -07003116{
3117 int i = 0;
J. Bruce Fields4796f452007-07-17 04:04:51 -07003118 u32 nflavs;
3119 struct exp_flavor_info *flavs;
3120 struct exp_flavor_info def_flavs[2];
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003121 __be32 *p;
Andy Adamsondcb488a32007-07-17 04:04:51 -07003122
3123 if (nfserr)
3124 goto out;
J. Bruce Fields4796f452007-07-17 04:04:51 -07003125 if (exp->ex_nflavors) {
3126 flavs = exp->ex_flavors;
3127 nflavs = exp->ex_nflavors;
3128 } else { /* Handling of some defaults in absence of real secinfo: */
3129 flavs = def_flavs;
3130 if (exp->ex_client->flavour->flavour == RPC_AUTH_UNIX) {
3131 nflavs = 2;
3132 flavs[0].pseudoflavor = RPC_AUTH_UNIX;
3133 flavs[1].pseudoflavor = RPC_AUTH_NULL;
3134 } else if (exp->ex_client->flavour->flavour == RPC_AUTH_GSS) {
3135 nflavs = 1;
3136 flavs[0].pseudoflavor
3137 = svcauth_gss_flavor(exp->ex_client);
3138 } else {
3139 nflavs = 1;
3140 flavs[0].pseudoflavor
3141 = exp->ex_client->flavour->flavour;
3142 }
3143 }
3144
Andy Adamsondcb488a32007-07-17 04:04:51 -07003145 RESERVE_SPACE(4);
J. Bruce Fields4796f452007-07-17 04:04:51 -07003146 WRITE32(nflavs);
Andy Adamsondcb488a32007-07-17 04:04:51 -07003147 ADJUST_ARGS();
J. Bruce Fields4796f452007-07-17 04:04:51 -07003148 for (i = 0; i < nflavs; i++) {
3149 u32 flav = flavs[i].pseudoflavor;
Andy Adamsondcb488a32007-07-17 04:04:51 -07003150 struct gss_api_mech *gm = gss_mech_get_by_pseudoflavor(flav);
3151
3152 if (gm) {
3153 RESERVE_SPACE(4);
3154 WRITE32(RPC_AUTH_GSS);
3155 ADJUST_ARGS();
3156 RESERVE_SPACE(4 + gm->gm_oid.len);
3157 WRITE32(gm->gm_oid.len);
3158 WRITEMEM(gm->gm_oid.data, gm->gm_oid.len);
3159 ADJUST_ARGS();
3160 RESERVE_SPACE(4);
3161 WRITE32(0); /* qop */
3162 ADJUST_ARGS();
3163 RESERVE_SPACE(4);
3164 WRITE32(gss_pseudoflavor_to_service(gm, flav));
3165 ADJUST_ARGS();
3166 gss_mech_put(gm);
3167 } else {
3168 RESERVE_SPACE(4);
3169 WRITE32(flav);
3170 ADJUST_ARGS();
3171 }
3172 }
3173out:
3174 if (exp)
3175 exp_put(exp);
Benny Halevy695e12f2008-07-04 14:38:33 +03003176 return nfserr;
Andy Adamsondcb488a32007-07-17 04:04:51 -07003177}
3178
Mi Jinlong22b6dee2010-12-27 14:29:57 +08003179static __be32
3180nfsd4_encode_secinfo(struct nfsd4_compoundres *resp, __be32 nfserr,
3181 struct nfsd4_secinfo *secinfo)
3182{
3183 return nfsd4_do_encode_secinfo(resp, nfserr, secinfo->si_exp);
3184}
3185
3186static __be32
3187nfsd4_encode_secinfo_no_name(struct nfsd4_compoundres *resp, __be32 nfserr,
3188 struct nfsd4_secinfo_no_name *secinfo)
3189{
3190 return nfsd4_do_encode_secinfo(resp, nfserr, secinfo->sin_exp);
3191}
3192
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193/*
3194 * The SETATTR encode routine is special -- it always encodes a bitmap,
3195 * regardless of the error status.
3196 */
Benny Halevy695e12f2008-07-04 14:38:33 +03003197static __be32
Al Virob37ad282006-10-19 23:28:59 -07003198nfsd4_encode_setattr(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_setattr *setattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003200 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201
3202 RESERVE_SPACE(12);
3203 if (nfserr) {
3204 WRITE32(2);
3205 WRITE32(0);
3206 WRITE32(0);
3207 }
3208 else {
3209 WRITE32(2);
3210 WRITE32(setattr->sa_bmval[0]);
3211 WRITE32(setattr->sa_bmval[1]);
3212 }
3213 ADJUST_ARGS();
Benny Halevy695e12f2008-07-04 14:38:33 +03003214 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215}
3216
Benny Halevy695e12f2008-07-04 14:38:33 +03003217static __be32
Al Virob37ad282006-10-19 23:28:59 -07003218nfsd4_encode_setclientid(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_setclientid *scd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003220 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221
3222 if (!nfserr) {
3223 RESERVE_SPACE(8 + sizeof(nfs4_verifier));
3224 WRITEMEM(&scd->se_clientid, 8);
3225 WRITEMEM(&scd->se_confirm, sizeof(nfs4_verifier));
3226 ADJUST_ARGS();
3227 }
3228 else if (nfserr == nfserr_clid_inuse) {
3229 RESERVE_SPACE(8);
3230 WRITE32(0);
3231 WRITE32(0);
3232 ADJUST_ARGS();
3233 }
Benny Halevy695e12f2008-07-04 14:38:33 +03003234 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235}
3236
Benny Halevy695e12f2008-07-04 14:38:33 +03003237static __be32
Al Virob37ad282006-10-19 23:28:59 -07003238nfsd4_encode_write(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_write *write)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003240 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241
3242 if (!nfserr) {
3243 RESERVE_SPACE(16);
3244 WRITE32(write->wr_bytes_written);
3245 WRITE32(write->wr_how_written);
3246 WRITEMEM(write->wr_verifier.data, 8);
3247 ADJUST_ARGS();
3248 }
Benny Halevy695e12f2008-07-04 14:38:33 +03003249 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250}
3251
Benny Halevy695e12f2008-07-04 14:38:33 +03003252static __be32
Andy Adamson2db134e2009-04-03 08:27:55 +03003253nfsd4_encode_exchange_id(struct nfsd4_compoundres *resp, int nfserr,
3254 struct nfsd4_exchange_id *exid)
3255{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003256 __be32 *p;
Andy Adamson0733d212009-04-03 08:28:01 +03003257 char *major_id;
3258 char *server_scope;
3259 int major_id_sz;
3260 int server_scope_sz;
3261 uint64_t minor_id = 0;
3262
3263 if (nfserr)
3264 return nfserr;
3265
3266 major_id = utsname()->nodename;
3267 major_id_sz = strlen(major_id);
3268 server_scope = utsname()->nodename;
3269 server_scope_sz = strlen(server_scope);
3270
3271 RESERVE_SPACE(
3272 8 /* eir_clientid */ +
3273 4 /* eir_sequenceid */ +
3274 4 /* eir_flags */ +
3275 4 /* spr_how (SP4_NONE) */ +
3276 8 /* so_minor_id */ +
3277 4 /* so_major_id.len */ +
3278 (XDR_QUADLEN(major_id_sz) * 4) +
3279 4 /* eir_server_scope.len */ +
3280 (XDR_QUADLEN(server_scope_sz) * 4) +
3281 4 /* eir_server_impl_id.count (0) */);
3282
3283 WRITEMEM(&exid->clientid, 8);
3284 WRITE32(exid->seqid);
3285 WRITE32(exid->flags);
3286
3287 /* state_protect4_r. Currently only support SP4_NONE */
3288 BUG_ON(exid->spa_how != SP4_NONE);
3289 WRITE32(exid->spa_how);
3290
3291 /* The server_owner struct */
3292 WRITE64(minor_id); /* Minor id */
3293 /* major id */
3294 WRITE32(major_id_sz);
3295 WRITEMEM(major_id, major_id_sz);
3296
3297 /* Server scope */
3298 WRITE32(server_scope_sz);
3299 WRITEMEM(server_scope, server_scope_sz);
3300
3301 /* Implementation id */
3302 WRITE32(0); /* zero length nfs_impl_id4 array */
3303 ADJUST_ARGS();
3304 return 0;
Andy Adamson2db134e2009-04-03 08:27:55 +03003305}
3306
3307static __be32
3308nfsd4_encode_create_session(struct nfsd4_compoundres *resp, int nfserr,
3309 struct nfsd4_create_session *sess)
3310{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003311 __be32 *p;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003312
3313 if (nfserr)
3314 return nfserr;
3315
3316 RESERVE_SPACE(24);
3317 WRITEMEM(sess->sessionid.data, NFS4_MAX_SESSIONID_LEN);
3318 WRITE32(sess->seqid);
3319 WRITE32(sess->flags);
3320 ADJUST_ARGS();
3321
3322 RESERVE_SPACE(28);
3323 WRITE32(0); /* headerpadsz */
3324 WRITE32(sess->fore_channel.maxreq_sz);
3325 WRITE32(sess->fore_channel.maxresp_sz);
3326 WRITE32(sess->fore_channel.maxresp_cached);
3327 WRITE32(sess->fore_channel.maxops);
3328 WRITE32(sess->fore_channel.maxreqs);
3329 WRITE32(sess->fore_channel.nr_rdma_attrs);
3330 ADJUST_ARGS();
3331
3332 if (sess->fore_channel.nr_rdma_attrs) {
3333 RESERVE_SPACE(4);
3334 WRITE32(sess->fore_channel.rdma_attrs);
3335 ADJUST_ARGS();
3336 }
3337
3338 RESERVE_SPACE(28);
3339 WRITE32(0); /* headerpadsz */
3340 WRITE32(sess->back_channel.maxreq_sz);
3341 WRITE32(sess->back_channel.maxresp_sz);
3342 WRITE32(sess->back_channel.maxresp_cached);
3343 WRITE32(sess->back_channel.maxops);
3344 WRITE32(sess->back_channel.maxreqs);
3345 WRITE32(sess->back_channel.nr_rdma_attrs);
3346 ADJUST_ARGS();
3347
3348 if (sess->back_channel.nr_rdma_attrs) {
3349 RESERVE_SPACE(4);
3350 WRITE32(sess->back_channel.rdma_attrs);
3351 ADJUST_ARGS();
3352 }
3353 return 0;
Andy Adamson2db134e2009-04-03 08:27:55 +03003354}
3355
3356static __be32
3357nfsd4_encode_destroy_session(struct nfsd4_compoundres *resp, int nfserr,
3358 struct nfsd4_destroy_session *destroy_session)
3359{
Andy Adamson2db134e2009-04-03 08:27:55 +03003360 return nfserr;
3361}
3362
Daniel Mackc47d8322011-05-16 16:38:14 +02003363static __be32
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003364nfsd4_encode_free_stateid(struct nfsd4_compoundres *resp, int nfserr,
3365 struct nfsd4_free_stateid *free_stateid)
3366{
3367 __be32 *p;
3368
3369 if (nfserr)
3370 return nfserr;
3371
3372 RESERVE_SPACE(4);
3373 WRITE32(nfserr);
3374 ADJUST_ARGS();
3375 return nfserr;
3376}
3377
3378static __be32
Andy Adamson2db134e2009-04-03 08:27:55 +03003379nfsd4_encode_sequence(struct nfsd4_compoundres *resp, int nfserr,
3380 struct nfsd4_sequence *seq)
3381{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003382 __be32 *p;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003383
3384 if (nfserr)
3385 return nfserr;
3386
3387 RESERVE_SPACE(NFS4_MAX_SESSIONID_LEN + 20);
3388 WRITEMEM(seq->sessionid.data, NFS4_MAX_SESSIONID_LEN);
3389 WRITE32(seq->seqid);
3390 WRITE32(seq->slotid);
J. Bruce Fieldsb7d7ca32011-08-31 15:39:30 -04003391 /* Note slotid's are numbered from zero: */
3392 WRITE32(seq->maxslots - 1); /* sr_highest_slotid */
3393 WRITE32(seq->maxslots - 1); /* sr_target_highest_slotid */
J. Bruce Fields0d7bb712010-11-18 08:30:33 -05003394 WRITE32(seq->status_flags);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003395
3396 ADJUST_ARGS();
Andy Adamson557ce262009-08-28 08:45:04 -04003397 resp->cstate.datap = p; /* DRC cache data pointer */
Benny Halevyb85d4c02009-04-03 08:28:08 +03003398 return 0;
Andy Adamson2db134e2009-04-03 08:27:55 +03003399}
3400
Bryan Schumaker17456802011-07-13 10:50:48 -04003401__be32
3402nfsd4_encode_test_stateid(struct nfsd4_compoundres *resp, int nfserr,
3403 struct nfsd4_test_stateid *test_stateid)
3404{
3405 struct nfsd4_compoundargs *argp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003406 struct nfs4_client *cl = resp->cstate.session->se_client;
Bryan Schumaker17456802011-07-13 10:50:48 -04003407 stateid_t si;
3408 __be32 *p;
3409 int i;
3410 int valid;
3411
3412 restore_buf(test_stateid->ts_saved_args, &test_stateid->ts_savedp);
3413 argp = test_stateid->ts_saved_args;
3414
3415 RESERVE_SPACE(4);
3416 *p++ = htonl(test_stateid->ts_num_ids);
3417 resp->p = p;
3418
3419 nfs4_lock_state();
3420 for (i = 0; i < test_stateid->ts_num_ids; i++) {
3421 nfsd4_decode_stateid(argp, &si);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003422 valid = nfs4_validate_stateid(cl, &si);
Bryan Schumaker17456802011-07-13 10:50:48 -04003423 RESERVE_SPACE(4);
3424 *p++ = htonl(valid);
3425 resp->p = p;
3426 }
3427 nfs4_unlock_state();
3428
3429 return nfserr;
3430}
3431
Andy Adamson2db134e2009-04-03 08:27:55 +03003432static __be32
Benny Halevy695e12f2008-07-04 14:38:33 +03003433nfsd4_encode_noop(struct nfsd4_compoundres *resp, __be32 nfserr, void *p)
3434{
3435 return nfserr;
3436}
3437
3438typedef __be32(* nfsd4_enc)(struct nfsd4_compoundres *, __be32, void *);
3439
Andy Adamson2db134e2009-04-03 08:27:55 +03003440/*
3441 * Note: nfsd4_enc_ops vector is shared for v4.0 and v4.1
3442 * since we don't need to filter out obsolete ops as this is
3443 * done in the decoding phase.
3444 */
Benny Halevy695e12f2008-07-04 14:38:33 +03003445static nfsd4_enc nfsd4_enc_ops[] = {
J. Bruce Fieldsad1060c2008-07-18 15:04:16 -04003446 [OP_ACCESS] = (nfsd4_enc)nfsd4_encode_access,
3447 [OP_CLOSE] = (nfsd4_enc)nfsd4_encode_close,
3448 [OP_COMMIT] = (nfsd4_enc)nfsd4_encode_commit,
3449 [OP_CREATE] = (nfsd4_enc)nfsd4_encode_create,
3450 [OP_DELEGPURGE] = (nfsd4_enc)nfsd4_encode_noop,
3451 [OP_DELEGRETURN] = (nfsd4_enc)nfsd4_encode_noop,
3452 [OP_GETATTR] = (nfsd4_enc)nfsd4_encode_getattr,
3453 [OP_GETFH] = (nfsd4_enc)nfsd4_encode_getfh,
3454 [OP_LINK] = (nfsd4_enc)nfsd4_encode_link,
3455 [OP_LOCK] = (nfsd4_enc)nfsd4_encode_lock,
3456 [OP_LOCKT] = (nfsd4_enc)nfsd4_encode_lockt,
3457 [OP_LOCKU] = (nfsd4_enc)nfsd4_encode_locku,
3458 [OP_LOOKUP] = (nfsd4_enc)nfsd4_encode_noop,
3459 [OP_LOOKUPP] = (nfsd4_enc)nfsd4_encode_noop,
3460 [OP_NVERIFY] = (nfsd4_enc)nfsd4_encode_noop,
3461 [OP_OPEN] = (nfsd4_enc)nfsd4_encode_open,
Benny Halevy84f09f42009-03-04 23:05:35 +02003462 [OP_OPENATTR] = (nfsd4_enc)nfsd4_encode_noop,
J. Bruce Fieldsad1060c2008-07-18 15:04:16 -04003463 [OP_OPEN_CONFIRM] = (nfsd4_enc)nfsd4_encode_open_confirm,
3464 [OP_OPEN_DOWNGRADE] = (nfsd4_enc)nfsd4_encode_open_downgrade,
3465 [OP_PUTFH] = (nfsd4_enc)nfsd4_encode_noop,
3466 [OP_PUTPUBFH] = (nfsd4_enc)nfsd4_encode_noop,
3467 [OP_PUTROOTFH] = (nfsd4_enc)nfsd4_encode_noop,
3468 [OP_READ] = (nfsd4_enc)nfsd4_encode_read,
3469 [OP_READDIR] = (nfsd4_enc)nfsd4_encode_readdir,
3470 [OP_READLINK] = (nfsd4_enc)nfsd4_encode_readlink,
3471 [OP_REMOVE] = (nfsd4_enc)nfsd4_encode_remove,
3472 [OP_RENAME] = (nfsd4_enc)nfsd4_encode_rename,
3473 [OP_RENEW] = (nfsd4_enc)nfsd4_encode_noop,
3474 [OP_RESTOREFH] = (nfsd4_enc)nfsd4_encode_noop,
3475 [OP_SAVEFH] = (nfsd4_enc)nfsd4_encode_noop,
3476 [OP_SECINFO] = (nfsd4_enc)nfsd4_encode_secinfo,
3477 [OP_SETATTR] = (nfsd4_enc)nfsd4_encode_setattr,
3478 [OP_SETCLIENTID] = (nfsd4_enc)nfsd4_encode_setclientid,
3479 [OP_SETCLIENTID_CONFIRM] = (nfsd4_enc)nfsd4_encode_noop,
3480 [OP_VERIFY] = (nfsd4_enc)nfsd4_encode_noop,
3481 [OP_WRITE] = (nfsd4_enc)nfsd4_encode_write,
3482 [OP_RELEASE_LOCKOWNER] = (nfsd4_enc)nfsd4_encode_noop,
Andy Adamson2db134e2009-04-03 08:27:55 +03003483
3484 /* NFSv4.1 operations */
3485 [OP_BACKCHANNEL_CTL] = (nfsd4_enc)nfsd4_encode_noop,
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003486 [OP_BIND_CONN_TO_SESSION] = (nfsd4_enc)nfsd4_encode_bind_conn_to_session,
Andy Adamson2db134e2009-04-03 08:27:55 +03003487 [OP_EXCHANGE_ID] = (nfsd4_enc)nfsd4_encode_exchange_id,
3488 [OP_CREATE_SESSION] = (nfsd4_enc)nfsd4_encode_create_session,
3489 [OP_DESTROY_SESSION] = (nfsd4_enc)nfsd4_encode_destroy_session,
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003490 [OP_FREE_STATEID] = (nfsd4_enc)nfsd4_encode_free_stateid,
Andy Adamson2db134e2009-04-03 08:27:55 +03003491 [OP_GET_DIR_DELEGATION] = (nfsd4_enc)nfsd4_encode_noop,
3492 [OP_GETDEVICEINFO] = (nfsd4_enc)nfsd4_encode_noop,
3493 [OP_GETDEVICELIST] = (nfsd4_enc)nfsd4_encode_noop,
3494 [OP_LAYOUTCOMMIT] = (nfsd4_enc)nfsd4_encode_noop,
3495 [OP_LAYOUTGET] = (nfsd4_enc)nfsd4_encode_noop,
3496 [OP_LAYOUTRETURN] = (nfsd4_enc)nfsd4_encode_noop,
Mi Jinlong22b6dee2010-12-27 14:29:57 +08003497 [OP_SECINFO_NO_NAME] = (nfsd4_enc)nfsd4_encode_secinfo_no_name,
Andy Adamson2db134e2009-04-03 08:27:55 +03003498 [OP_SEQUENCE] = (nfsd4_enc)nfsd4_encode_sequence,
3499 [OP_SET_SSV] = (nfsd4_enc)nfsd4_encode_noop,
Bryan Schumaker17456802011-07-13 10:50:48 -04003500 [OP_TEST_STATEID] = (nfsd4_enc)nfsd4_encode_test_stateid,
Andy Adamson2db134e2009-04-03 08:27:55 +03003501 [OP_WANT_DELEGATION] = (nfsd4_enc)nfsd4_encode_noop,
3502 [OP_DESTROY_CLIENTID] = (nfsd4_enc)nfsd4_encode_noop,
3503 [OP_RECLAIM_COMPLETE] = (nfsd4_enc)nfsd4_encode_noop,
Benny Halevy695e12f2008-07-04 14:38:33 +03003504};
3505
Andy Adamson496c2622009-04-03 08:28:48 +03003506/*
3507 * Calculate the total amount of memory that the compound response has taken
Mi Jinlong58e7b332011-08-28 18:18:56 +08003508 * after encoding the current operation with pad.
Andy Adamson496c2622009-04-03 08:28:48 +03003509 *
Mi Jinlong58e7b332011-08-28 18:18:56 +08003510 * pad: if operation is non-idempotent, pad was calculate by op_rsize_bop()
3511 * which was specified at nfsd4_operation, else pad is zero.
Andy Adamson496c2622009-04-03 08:28:48 +03003512 *
Mi Jinlong58e7b332011-08-28 18:18:56 +08003513 * Compare this length to the session se_fmaxresp_sz and se_fmaxresp_cached.
Andy Adamson496c2622009-04-03 08:28:48 +03003514 *
3515 * Our se_fmaxresp_cached will always be a multiple of PAGE_SIZE, and so
3516 * will be at least a page and will therefore hold the xdr_buf head.
3517 */
Mi Jinlong58e7b332011-08-28 18:18:56 +08003518int nfsd4_check_resp_size(struct nfsd4_compoundres *resp, u32 pad)
Andy Adamson496c2622009-04-03 08:28:48 +03003519{
Andy Adamson496c2622009-04-03 08:28:48 +03003520 struct xdr_buf *xb = &resp->rqstp->rq_res;
Andy Adamson496c2622009-04-03 08:28:48 +03003521 struct nfsd4_session *session = NULL;
3522 struct nfsd4_slot *slot = resp->cstate.slot;
Mi Jinlong58e7b332011-08-28 18:18:56 +08003523 u32 length, tlen = 0;
Andy Adamson496c2622009-04-03 08:28:48 +03003524
3525 if (!nfsd4_has_session(&resp->cstate))
Mi Jinlong58e7b332011-08-28 18:18:56 +08003526 return 0;
Andy Adamson496c2622009-04-03 08:28:48 +03003527
3528 session = resp->cstate.session;
Mi Jinlong58e7b332011-08-28 18:18:56 +08003529 if (session == NULL)
3530 return 0;
Andy Adamson496c2622009-04-03 08:28:48 +03003531
3532 if (xb->page_len == 0) {
3533 length = (char *)resp->p - (char *)xb->head[0].iov_base + pad;
3534 } else {
3535 if (xb->tail[0].iov_base && xb->tail[0].iov_len > 0)
3536 tlen = (char *)resp->p - (char *)xb->tail[0].iov_base;
3537
3538 length = xb->head[0].iov_len + xb->page_len + tlen + pad;
3539 }
3540 dprintk("%s length %u, xb->page_len %u tlen %u pad %u\n", __func__,
3541 length, xb->page_len, tlen, pad);
3542
Mi Jinlong58e7b332011-08-28 18:18:56 +08003543 if (length > session->se_fchannel.maxresp_sz)
3544 return nfserr_rep_too_big;
3545
J. Bruce Fields73e79482012-02-13 16:39:00 -05003546 if ((slot->sl_flags & NFSD4_SLOT_CACHETHIS) &&
Mi Jinlong58e7b332011-08-28 18:18:56 +08003547 length > session->se_fchannel.maxresp_cached)
Andy Adamson496c2622009-04-03 08:28:48 +03003548 return nfserr_rep_too_big_to_cache;
Mi Jinlong58e7b332011-08-28 18:18:56 +08003549
3550 return 0;
Andy Adamson496c2622009-04-03 08:28:48 +03003551}
3552
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553void
3554nfsd4_encode_operation(struct nfsd4_compoundres *resp, struct nfsd4_op *op)
3555{
Al Viro2ebbc012006-10-19 23:28:58 -07003556 __be32 *statp;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003557 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558
3559 RESERVE_SPACE(8);
3560 WRITE32(op->opnum);
3561 statp = p++; /* to be backfilled at the end */
3562 ADJUST_ARGS();
3563
Benny Halevy695e12f2008-07-04 14:38:33 +03003564 if (op->opnum == OP_ILLEGAL)
3565 goto status;
3566 BUG_ON(op->opnum < 0 || op->opnum >= ARRAY_SIZE(nfsd4_enc_ops) ||
3567 !nfsd4_enc_ops[op->opnum]);
3568 op->status = nfsd4_enc_ops[op->opnum](resp, op->status, &op->u);
Andy Adamson496c2622009-04-03 08:28:48 +03003569 /* nfsd4_check_drc_limit guarantees enough room for error status */
Mi Jinlong58e7b332011-08-28 18:18:56 +08003570 if (!op->status)
3571 op->status = nfsd4_check_resp_size(resp, 0);
Benny Halevy695e12f2008-07-04 14:38:33 +03003572status:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573 /*
3574 * Note: We write the status directly, instead of using WRITE32(),
3575 * since it is already in network byte order.
3576 */
3577 *statp = op->status;
3578}
3579
3580/*
3581 * Encode the reply stored in the stateowner reply cache
3582 *
3583 * XDR note: do not encode rp->rp_buflen: the buffer contains the
3584 * previously sent already encoded operation.
3585 *
3586 * called with nfs4_lock_state() held
3587 */
3588void
3589nfsd4_encode_replay(struct nfsd4_compoundres *resp, struct nfsd4_op *op)
3590{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003591 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592 struct nfs4_replay *rp = op->replay;
3593
3594 BUG_ON(!rp);
3595
3596 RESERVE_SPACE(8);
3597 WRITE32(op->opnum);
3598 *p++ = rp->rp_status; /* already xdr'ed */
3599 ADJUST_ARGS();
3600
3601 RESERVE_SPACE(rp->rp_buflen);
3602 WRITEMEM(rp->rp_buf, rp->rp_buflen);
3603 ADJUST_ARGS();
3604}
3605
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606int
Al Viro2ebbc012006-10-19 23:28:58 -07003607nfs4svc_encode_voidres(struct svc_rqst *rqstp, __be32 *p, void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608{
3609 return xdr_ressize_check(rqstp, p);
3610}
3611
J. Bruce Fields3e98abf2011-07-16 17:15:10 -04003612int nfsd4_release_compoundargs(void *rq, __be32 *p, void *resp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613{
J. Bruce Fields3e98abf2011-07-16 17:15:10 -04003614 struct svc_rqst *rqstp = rq;
3615 struct nfsd4_compoundargs *args = rqstp->rq_argp;
3616
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617 if (args->ops != args->iops) {
3618 kfree(args->ops);
3619 args->ops = args->iops;
3620 }
Jesper Juhlf99d49a2005-11-07 01:01:34 -08003621 kfree(args->tmpp);
3622 args->tmpp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623 while (args->to_free) {
3624 struct tmpbuf *tb = args->to_free;
3625 args->to_free = tb->next;
3626 tb->release(tb->buf);
3627 kfree(tb);
3628 }
J. Bruce Fields3e98abf2011-07-16 17:15:10 -04003629 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630}
3631
3632int
Al Viro2ebbc012006-10-19 23:28:58 -07003633nfs4svc_decode_compoundargs(struct svc_rqst *rqstp, __be32 *p, struct nfsd4_compoundargs *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635 args->p = p;
3636 args->end = rqstp->rq_arg.head[0].iov_base + rqstp->rq_arg.head[0].iov_len;
3637 args->pagelist = rqstp->rq_arg.pages;
3638 args->pagelen = rqstp->rq_arg.page_len;
3639 args->tmpp = NULL;
3640 args->to_free = NULL;
3641 args->ops = args->iops;
3642 args->rqstp = rqstp;
3643
J. Bruce Fields3e98abf2011-07-16 17:15:10 -04003644 return !nfsd4_decode_compound(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645}
3646
3647int
Al Viro2ebbc012006-10-19 23:28:58 -07003648nfs4svc_encode_compoundres(struct svc_rqst *rqstp, __be32 *p, struct nfsd4_compoundres *resp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649{
3650 /*
3651 * All that remains is to write the tag and operation count...
3652 */
Andy Adamson557ce262009-08-28 08:45:04 -04003653 struct nfsd4_compound_state *cs = &resp->cstate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654 struct kvec *iov;
3655 p = resp->tagp;
3656 *p++ = htonl(resp->taglen);
3657 memcpy(p, resp->tag, resp->taglen);
3658 p += XDR_QUADLEN(resp->taglen);
3659 *p++ = htonl(resp->opcnt);
3660
3661 if (rqstp->rq_res.page_len)
3662 iov = &rqstp->rq_res.tail[0];
3663 else
3664 iov = &rqstp->rq_res.head[0];
3665 iov->iov_len = ((char*)resp->p) - (char*)iov->iov_base;
3666 BUG_ON(iov->iov_len > PAGE_SIZE);
J. Bruce Fields26c0c752010-04-24 15:35:43 -04003667 if (nfsd4_has_session(cs)) {
3668 if (cs->status != nfserr_replay_cache) {
3669 nfsd4_store_cache_entry(resp);
J. Bruce Fields73e79482012-02-13 16:39:00 -05003670 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
J. Bruce Fields26c0c752010-04-24 15:35:43 -04003671 }
Benny Halevyd7682982010-05-12 00:13:54 +03003672 /* Renew the clientid on success and on replay */
3673 release_session_client(cs->session);
J. Bruce Fields76407f72010-06-22 14:10:14 -04003674 nfsd4_put_session(cs->session);
Andy Adamsonda3846a2009-04-03 08:28:22 +03003675 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676 return 1;
3677}
3678
3679/*
3680 * Local variables:
3681 * c-basic-offset: 8
3682 * End:
3683 */