blob: 94da8bb36c855837c0c0b458adff52e754bba4bb [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) {
J. Bruce Fieldsa4db5fe2007-02-16 01:28:30 -0800218 p = kmalloc(nbytes, GFP_KERNEL);
219 if (!p)
220 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 memcpy(p, argp->tmp, nbytes);
222 } else {
Eric Sesterhenn73dff8b2006-10-03 23:37:14 +0200223 BUG_ON(p != argp->tmpp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 argp->tmpp = NULL;
225 }
226 if (defer_free(argp, kfree, p)) {
J. Bruce Fieldsa4db5fe2007-02-16 01:28:30 -0800227 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 return NULL;
229 } else
230 return (char *)p;
231}
232
Al Virob37ad282006-10-19 23:28:59 -0700233static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234nfsd4_decode_bitmap(struct nfsd4_compoundargs *argp, u32 *bmval)
235{
236 u32 bmlen;
237 DECODE_HEAD;
238
239 bmval[0] = 0;
240 bmval[1] = 0;
Andy Adamson7e705702009-04-03 08:29:11 +0300241 bmval[2] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
243 READ_BUF(4);
244 READ32(bmlen);
245 if (bmlen > 1000)
246 goto xdr_error;
247
248 READ_BUF(bmlen << 2);
249 if (bmlen > 0)
250 READ32(bmval[0]);
251 if (bmlen > 1)
252 READ32(bmval[1]);
Andy Adamson7e705702009-04-03 08:29:11 +0300253 if (bmlen > 2)
254 READ32(bmval[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
256 DECODE_TAIL;
257}
258
Al Virob37ad282006-10-19 23:28:59 -0700259static __be32
Yu Zhiguo3c8e0312009-05-16 16:22:31 +0800260nfsd4_decode_fattr(struct nfsd4_compoundargs *argp, u32 *bmval,
Benny Halevyc0d6fc82009-04-03 08:29:05 +0300261 struct iattr *iattr, struct nfs4_acl **acl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
263 int expected_len, len = 0;
264 u32 dummy32;
265 char *buf;
Al Virob8dd7b92006-10-19 23:29:01 -0700266 int host_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
268 DECODE_HEAD;
269 iattr->ia_valid = 0;
270 if ((status = nfsd4_decode_bitmap(argp, bmval)))
271 return status;
272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 READ_BUF(4);
274 READ32(expected_len);
275
276 if (bmval[0] & FATTR4_WORD0_SIZE) {
277 READ_BUF(8);
278 len += 8;
279 READ64(iattr->ia_size);
280 iattr->ia_valid |= ATTR_SIZE;
281 }
282 if (bmval[0] & FATTR4_WORD0_ACL) {
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800283 int nace;
284 struct nfs4_ace *ace;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286 READ_BUF(4); len += 4;
287 READ32(nace);
288
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800289 if (nace > NFS4_ACL_MAX)
290 return nfserr_resource;
291
292 *acl = nfs4_acl_new(nace);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 if (*acl == NULL) {
Al Virob8dd7b92006-10-19 23:29:01 -0700294 host_err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 goto out_nfserr;
296 }
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800297 defer_free(argp, kfree, *acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800299 (*acl)->naces = nace;
300 for (ace = (*acl)->aces; ace < (*acl)->aces + nace; ace++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 READ_BUF(16); len += 16;
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800302 READ32(ace->type);
303 READ32(ace->flag);
304 READ32(ace->access_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 READ32(dummy32);
306 READ_BUF(dummy32);
307 len += XDR_QUADLEN(dummy32) << 2;
308 READMEM(buf, dummy32);
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800309 ace->whotype = nfs4_acl_get_whotype(buf, dummy32);
J. Bruce Fields3c726022011-01-04 17:53:52 -0500310 status = nfs_ok;
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800311 if (ace->whotype != NFS4_ACL_WHO_NAMED)
312 ace->who = 0;
313 else if (ace->flag & NFS4_ACE_IDENTIFIER_GROUP)
J. Bruce Fields3c726022011-01-04 17:53:52 -0500314 status = nfsd_map_name_to_gid(argp->rqstp,
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800315 buf, dummy32, &ace->who);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 else
J. Bruce Fields3c726022011-01-04 17:53:52 -0500317 status = nfsd_map_name_to_uid(argp->rqstp,
J. Bruce Fields28e05dd2007-02-16 01:28:30 -0800318 buf, dummy32, &ace->who);
J. Bruce Fields3c726022011-01-04 17:53:52 -0500319 if (status)
320 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 }
322 } else
323 *acl = NULL;
324 if (bmval[1] & FATTR4_WORD1_MODE) {
325 READ_BUF(4);
326 len += 4;
327 READ32(iattr->ia_mode);
328 iattr->ia_mode &= (S_IFMT | S_IALLUGO);
329 iattr->ia_valid |= ATTR_MODE;
330 }
331 if (bmval[1] & FATTR4_WORD1_OWNER) {
332 READ_BUF(4);
333 len += 4;
334 READ32(dummy32);
335 READ_BUF(dummy32);
336 len += (XDR_QUADLEN(dummy32) << 2);
337 READMEM(buf, dummy32);
NeilBrown47c85292011-02-16 13:08:35 +1100338 if ((status = nfsd_map_name_to_uid(argp->rqstp, buf, dummy32, &iattr->ia_uid)))
339 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 iattr->ia_valid |= ATTR_UID;
341 }
342 if (bmval[1] & FATTR4_WORD1_OWNER_GROUP) {
343 READ_BUF(4);
344 len += 4;
345 READ32(dummy32);
346 READ_BUF(dummy32);
347 len += (XDR_QUADLEN(dummy32) << 2);
348 READMEM(buf, dummy32);
NeilBrown47c85292011-02-16 13:08:35 +1100349 if ((status = nfsd_map_name_to_gid(argp->rqstp, buf, dummy32, &iattr->ia_gid)))
350 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 iattr->ia_valid |= ATTR_GID;
352 }
353 if (bmval[1] & FATTR4_WORD1_TIME_ACCESS_SET) {
354 READ_BUF(4);
355 len += 4;
356 READ32(dummy32);
357 switch (dummy32) {
358 case NFS4_SET_TO_CLIENT_TIME:
359 /* We require the high 32 bits of 'seconds' to be 0, and we ignore
360 all 32 bits of 'nseconds'. */
361 READ_BUF(12);
362 len += 12;
363 READ32(dummy32);
364 if (dummy32)
365 return nfserr_inval;
366 READ32(iattr->ia_atime.tv_sec);
367 READ32(iattr->ia_atime.tv_nsec);
368 if (iattr->ia_atime.tv_nsec >= (u32)1000000000)
369 return nfserr_inval;
370 iattr->ia_valid |= (ATTR_ATIME | ATTR_ATIME_SET);
371 break;
372 case NFS4_SET_TO_SERVER_TIME:
373 iattr->ia_valid |= ATTR_ATIME;
374 break;
375 default:
376 goto xdr_error;
377 }
378 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 if (bmval[1] & FATTR4_WORD1_TIME_MODIFY_SET) {
380 READ_BUF(4);
381 len += 4;
382 READ32(dummy32);
383 switch (dummy32) {
384 case NFS4_SET_TO_CLIENT_TIME:
385 /* We require the high 32 bits of 'seconds' to be 0, and we ignore
386 all 32 bits of 'nseconds'. */
387 READ_BUF(12);
388 len += 12;
389 READ32(dummy32);
390 if (dummy32)
391 return nfserr_inval;
392 READ32(iattr->ia_mtime.tv_sec);
393 READ32(iattr->ia_mtime.tv_nsec);
394 if (iattr->ia_mtime.tv_nsec >= (u32)1000000000)
395 return nfserr_inval;
396 iattr->ia_valid |= (ATTR_MTIME | ATTR_MTIME_SET);
397 break;
398 case NFS4_SET_TO_SERVER_TIME:
399 iattr->ia_valid |= ATTR_MTIME;
400 break;
401 default:
402 goto xdr_error;
403 }
404 }
Yu Zhiguo3c8e0312009-05-16 16:22:31 +0800405 if (bmval[0] & ~NFSD_WRITEABLE_ATTRS_WORD0
406 || bmval[1] & ~NFSD_WRITEABLE_ATTRS_WORD1
407 || bmval[2] & ~NFSD_WRITEABLE_ATTRS_WORD2)
408 READ_BUF(expected_len - len);
409 else if (len != expected_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 goto xdr_error;
411
412 DECODE_TAIL;
413
414out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -0700415 status = nfserrno(host_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 goto out;
417}
418
Al Virob37ad282006-10-19 23:28:59 -0700419static __be32
Benny Halevye31a1b62008-08-12 20:46:18 +0300420nfsd4_decode_stateid(struct nfsd4_compoundargs *argp, stateid_t *sid)
421{
422 DECODE_HEAD;
423
424 READ_BUF(sizeof(stateid_t));
425 READ32(sid->si_generation);
426 COPYMEM(&sid->si_opaque, sizeof(stateid_opaque_t));
427
428 DECODE_TAIL;
429}
430
431static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432nfsd4_decode_access(struct nfsd4_compoundargs *argp, struct nfsd4_access *access)
433{
434 DECODE_HEAD;
435
436 READ_BUF(4);
437 READ32(access->ac_req_access);
438
439 DECODE_TAIL;
440}
441
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -0400442static __be32 nfsd4_decode_bind_conn_to_session(struct nfsd4_compoundargs *argp, struct nfsd4_bind_conn_to_session *bcts)
443{
444 DECODE_HEAD;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -0400445
446 READ_BUF(NFS4_MAX_SESSIONID_LEN + 8);
447 COPYMEM(bcts->sessionid.data, NFS4_MAX_SESSIONID_LEN);
448 READ32(bcts->dir);
Bryan Schumaker6ce23572011-04-27 15:47:16 -0400449 /* XXX: skipping ctsa_use_conn_in_rdma_mode. Perhaps Tom Tucker
450 * could help us figure out we should be using it. */
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -0400451 DECODE_TAIL;
452}
453
Al Virob37ad282006-10-19 23:28:59 -0700454static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455nfsd4_decode_close(struct nfsd4_compoundargs *argp, struct nfsd4_close *close)
456{
457 DECODE_HEAD;
458
Benny Halevye31a1b62008-08-12 20:46:18 +0300459 READ_BUF(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 READ32(close->cl_seqid);
Benny Halevye31a1b62008-08-12 20:46:18 +0300461 return nfsd4_decode_stateid(argp, &close->cl_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
463 DECODE_TAIL;
464}
465
466
Al Virob37ad282006-10-19 23:28:59 -0700467static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468nfsd4_decode_commit(struct nfsd4_compoundargs *argp, struct nfsd4_commit *commit)
469{
470 DECODE_HEAD;
471
472 READ_BUF(12);
473 READ64(commit->co_offset);
474 READ32(commit->co_count);
475
476 DECODE_TAIL;
477}
478
Al Virob37ad282006-10-19 23:28:59 -0700479static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480nfsd4_decode_create(struct nfsd4_compoundargs *argp, struct nfsd4_create *create)
481{
482 DECODE_HEAD;
483
484 READ_BUF(4);
485 READ32(create->cr_type);
486 switch (create->cr_type) {
487 case NF4LNK:
488 READ_BUF(4);
489 READ32(create->cr_linklen);
490 READ_BUF(create->cr_linklen);
491 SAVEMEM(create->cr_linkname, create->cr_linklen);
492 break;
493 case NF4BLK:
494 case NF4CHR:
495 READ_BUF(8);
496 READ32(create->cr_specdata1);
497 READ32(create->cr_specdata2);
498 break;
499 case NF4SOCK:
500 case NF4FIFO:
501 case NF4DIR:
502 default:
503 break;
504 }
505
506 READ_BUF(4);
507 READ32(create->cr_namelen);
508 READ_BUF(create->cr_namelen);
509 SAVEMEM(create->cr_name, create->cr_namelen);
510 if ((status = check_filename(create->cr_name, create->cr_namelen, nfserr_inval)))
511 return status;
512
Yu Zhiguo3c8e0312009-05-16 16:22:31 +0800513 status = nfsd4_decode_fattr(argp, create->cr_bmval, &create->cr_iattr,
514 &create->cr_acl);
Benny Halevyc0d6fc82009-04-03 08:29:05 +0300515 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 goto out;
517
518 DECODE_TAIL;
519}
520
Al Virob37ad282006-10-19 23:28:59 -0700521static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522nfsd4_decode_delegreturn(struct nfsd4_compoundargs *argp, struct nfsd4_delegreturn *dr)
523{
Benny Halevye31a1b62008-08-12 20:46:18 +0300524 return nfsd4_decode_stateid(argp, &dr->dr_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525}
526
Al Virob37ad282006-10-19 23:28:59 -0700527static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528nfsd4_decode_getattr(struct nfsd4_compoundargs *argp, struct nfsd4_getattr *getattr)
529{
530 return nfsd4_decode_bitmap(argp, getattr->ga_bmval);
531}
532
Al Virob37ad282006-10-19 23:28:59 -0700533static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534nfsd4_decode_link(struct nfsd4_compoundargs *argp, struct nfsd4_link *link)
535{
536 DECODE_HEAD;
537
538 READ_BUF(4);
539 READ32(link->li_namelen);
540 READ_BUF(link->li_namelen);
541 SAVEMEM(link->li_name, link->li_namelen);
542 if ((status = check_filename(link->li_name, link->li_namelen, nfserr_inval)))
543 return status;
544
545 DECODE_TAIL;
546}
547
Al Virob37ad282006-10-19 23:28:59 -0700548static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549nfsd4_decode_lock(struct nfsd4_compoundargs *argp, struct nfsd4_lock *lock)
550{
551 DECODE_HEAD;
552
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 /*
554 * type, reclaim(boolean), offset, length, new_lock_owner(boolean)
555 */
556 READ_BUF(28);
557 READ32(lock->lk_type);
558 if ((lock->lk_type < NFS4_READ_LT) || (lock->lk_type > NFS4_WRITEW_LT))
559 goto xdr_error;
560 READ32(lock->lk_reclaim);
561 READ64(lock->lk_offset);
562 READ64(lock->lk_length);
563 READ32(lock->lk_is_new);
564
565 if (lock->lk_is_new) {
Benny Halevye31a1b62008-08-12 20:46:18 +0300566 READ_BUF(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 READ32(lock->lk_new_open_seqid);
Benny Halevye31a1b62008-08-12 20:46:18 +0300568 status = nfsd4_decode_stateid(argp, &lock->lk_new_open_stateid);
569 if (status)
570 return status;
571 READ_BUF(8 + sizeof(clientid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 READ32(lock->lk_new_lock_seqid);
573 COPYMEM(&lock->lk_new_clientid, sizeof(clientid_t));
574 READ32(lock->lk_new_owner.len);
575 READ_BUF(lock->lk_new_owner.len);
576 READMEM(lock->lk_new_owner.data, lock->lk_new_owner.len);
577 } else {
Benny Halevye31a1b62008-08-12 20:46:18 +0300578 status = nfsd4_decode_stateid(argp, &lock->lk_old_lock_stateid);
579 if (status)
580 return status;
581 READ_BUF(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 READ32(lock->lk_old_lock_seqid);
583 }
584
585 DECODE_TAIL;
586}
587
Al Virob37ad282006-10-19 23:28:59 -0700588static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589nfsd4_decode_lockt(struct nfsd4_compoundargs *argp, struct nfsd4_lockt *lockt)
590{
591 DECODE_HEAD;
592
593 READ_BUF(32);
594 READ32(lockt->lt_type);
595 if((lockt->lt_type < NFS4_READ_LT) || (lockt->lt_type > NFS4_WRITEW_LT))
596 goto xdr_error;
597 READ64(lockt->lt_offset);
598 READ64(lockt->lt_length);
599 COPYMEM(&lockt->lt_clientid, 8);
600 READ32(lockt->lt_owner.len);
601 READ_BUF(lockt->lt_owner.len);
602 READMEM(lockt->lt_owner.data, lockt->lt_owner.len);
603
604 DECODE_TAIL;
605}
606
Al Virob37ad282006-10-19 23:28:59 -0700607static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608nfsd4_decode_locku(struct nfsd4_compoundargs *argp, struct nfsd4_locku *locku)
609{
610 DECODE_HEAD;
611
Benny Halevye31a1b62008-08-12 20:46:18 +0300612 READ_BUF(8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 READ32(locku->lu_type);
614 if ((locku->lu_type < NFS4_READ_LT) || (locku->lu_type > NFS4_WRITEW_LT))
615 goto xdr_error;
616 READ32(locku->lu_seqid);
Benny Halevye31a1b62008-08-12 20:46:18 +0300617 status = nfsd4_decode_stateid(argp, &locku->lu_stateid);
618 if (status)
619 return status;
620 READ_BUF(16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 READ64(locku->lu_offset);
622 READ64(locku->lu_length);
623
624 DECODE_TAIL;
625}
626
Al Virob37ad282006-10-19 23:28:59 -0700627static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628nfsd4_decode_lookup(struct nfsd4_compoundargs *argp, struct nfsd4_lookup *lookup)
629{
630 DECODE_HEAD;
631
632 READ_BUF(4);
633 READ32(lookup->lo_len);
634 READ_BUF(lookup->lo_len);
635 SAVEMEM(lookup->lo_name, lookup->lo_len);
636 if ((status = check_filename(lookup->lo_name, lookup->lo_len, nfserr_noent)))
637 return status;
638
639 DECODE_TAIL;
640}
641
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400642static __be32 nfsd4_decode_share_access(struct nfsd4_compoundargs *argp, u32 *x)
643{
644 __be32 *p;
645 u32 w;
646
647 READ_BUF(4);
648 READ32(w);
649 *x = w;
650 switch (w & NFS4_SHARE_ACCESS_MASK) {
651 case NFS4_SHARE_ACCESS_READ:
652 case NFS4_SHARE_ACCESS_WRITE:
653 case NFS4_SHARE_ACCESS_BOTH:
654 break;
655 default:
656 return nfserr_bad_xdr;
657 }
658 w &= !NFS4_SHARE_ACCESS_MASK;
659 if (!w)
660 return nfs_ok;
661 if (!argp->minorversion)
662 return nfserr_bad_xdr;
663 switch (w & NFS4_SHARE_WANT_MASK) {
664 case NFS4_SHARE_WANT_NO_PREFERENCE:
665 case NFS4_SHARE_WANT_READ_DELEG:
666 case NFS4_SHARE_WANT_WRITE_DELEG:
667 case NFS4_SHARE_WANT_ANY_DELEG:
668 case NFS4_SHARE_WANT_NO_DELEG:
669 case NFS4_SHARE_WANT_CANCEL:
670 break;
671 default:
672 return nfserr_bad_xdr;
673 }
674 w &= !NFS4_SHARE_WANT_MASK;
675 if (!w)
676 return nfs_ok;
677 switch (w) {
678 case NFS4_SHARE_SIGNAL_DELEG_WHEN_RESRC_AVAIL:
679 case NFS4_SHARE_PUSH_DELEG_WHEN_UNCONTENDED:
680 return nfs_ok;
681 }
682xdr_error:
683 return nfserr_bad_xdr;
684}
685
686static __be32 nfsd4_decode_share_deny(struct nfsd4_compoundargs *argp, u32 *x)
687{
688 __be32 *p;
689
690 READ_BUF(4);
691 READ32(*x);
692 /* Note: unlinke access bits, deny bits may be zero. */
693 if (*x & !NFS4_SHARE_DENY_BOTH)
694 return nfserr_bad_xdr;
695 return nfs_ok;
696xdr_error:
697 return nfserr_bad_xdr;
698}
699
Al Virob37ad282006-10-19 23:28:59 -0700700static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701nfsd4_decode_open(struct nfsd4_compoundargs *argp, struct nfsd4_open *open)
702{
703 DECODE_HEAD;
704
705 memset(open->op_bmval, 0, sizeof(open->op_bmval));
706 open->op_iattr.ia_valid = 0;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400707 open->op_openowner = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
709 /* seqid, share_access, share_deny, clientid, ownerlen */
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400710 READ_BUF(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 READ32(open->op_seqid);
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400712 status = nfsd4_decode_share_access(argp, &open->op_share_access);
713 if (status)
714 goto xdr_error;
715 status = nfsd4_decode_share_deny(argp, &open->op_share_deny);
716 if (status)
717 goto xdr_error;
718 READ_BUF(sizeof(clientid_t) + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 COPYMEM(&open->op_clientid, sizeof(clientid_t));
720 READ32(open->op_owner.len);
721
722 /* owner, open_flag */
723 READ_BUF(open->op_owner.len + 4);
724 SAVEMEM(open->op_owner.data, open->op_owner.len);
725 READ32(open->op_create);
726 switch (open->op_create) {
727 case NFS4_OPEN_NOCREATE:
728 break;
729 case NFS4_OPEN_CREATE:
730 READ_BUF(4);
731 READ32(open->op_createmode);
732 switch (open->op_createmode) {
733 case NFS4_CREATE_UNCHECKED:
734 case NFS4_CREATE_GUARDED:
Benny Halevyc0d6fc82009-04-03 08:29:05 +0300735 status = nfsd4_decode_fattr(argp, open->op_bmval,
Yu Zhiguo3c8e0312009-05-16 16:22:31 +0800736 &open->op_iattr, &open->op_acl);
Benny Halevyc0d6fc82009-04-03 08:29:05 +0300737 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 goto out;
739 break;
740 case NFS4_CREATE_EXCLUSIVE:
741 READ_BUF(8);
742 COPYMEM(open->op_verf.data, 8);
743 break;
Benny Halevy79fb54a2009-04-03 08:29:17 +0300744 case NFS4_CREATE_EXCLUSIVE4_1:
745 if (argp->minorversion < 1)
746 goto xdr_error;
747 READ_BUF(8);
748 COPYMEM(open->op_verf.data, 8);
749 status = nfsd4_decode_fattr(argp, open->op_bmval,
Yu Zhiguo3c8e0312009-05-16 16:22:31 +0800750 &open->op_iattr, &open->op_acl);
Benny Halevy79fb54a2009-04-03 08:29:17 +0300751 if (status)
752 goto out;
753 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 default:
755 goto xdr_error;
756 }
757 break;
758 default:
759 goto xdr_error;
760 }
761
762 /* open_claim */
763 READ_BUF(4);
764 READ32(open->op_claim_type);
765 switch (open->op_claim_type) {
766 case NFS4_OPEN_CLAIM_NULL:
767 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
768 READ_BUF(4);
769 READ32(open->op_fname.len);
770 READ_BUF(open->op_fname.len);
771 SAVEMEM(open->op_fname.data, open->op_fname.len);
772 if ((status = check_filename(open->op_fname.data, open->op_fname.len, nfserr_inval)))
773 return status;
774 break;
775 case NFS4_OPEN_CLAIM_PREVIOUS:
776 READ_BUF(4);
777 READ32(open->op_delegate_type);
778 break;
779 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
Benny Halevye31a1b62008-08-12 20:46:18 +0300780 status = nfsd4_decode_stateid(argp, &open->op_delegate_stateid);
781 if (status)
782 return status;
783 READ_BUF(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 READ32(open->op_fname.len);
785 READ_BUF(open->op_fname.len);
786 SAVEMEM(open->op_fname.data, open->op_fname.len);
787 if ((status = check_filename(open->op_fname.data, open->op_fname.len, nfserr_inval)))
788 return status;
789 break;
790 default:
791 goto xdr_error;
792 }
793
794 DECODE_TAIL;
795}
796
Al Virob37ad282006-10-19 23:28:59 -0700797static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798nfsd4_decode_open_confirm(struct nfsd4_compoundargs *argp, struct nfsd4_open_confirm *open_conf)
799{
800 DECODE_HEAD;
801
Benny Halevye31a1b62008-08-12 20:46:18 +0300802 status = nfsd4_decode_stateid(argp, &open_conf->oc_req_stateid);
803 if (status)
804 return status;
805 READ_BUF(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 READ32(open_conf->oc_seqid);
807
808 DECODE_TAIL;
809}
810
Al Virob37ad282006-10-19 23:28:59 -0700811static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812nfsd4_decode_open_downgrade(struct nfsd4_compoundargs *argp, struct nfsd4_open_downgrade *open_down)
813{
814 DECODE_HEAD;
815
Benny Halevye31a1b62008-08-12 20:46:18 +0300816 status = nfsd4_decode_stateid(argp, &open_down->od_stateid);
817 if (status)
818 return status;
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400819 READ_BUF(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 READ32(open_down->od_seqid);
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400821 status = nfsd4_decode_share_access(argp, &open_down->od_share_access);
822 if (status)
823 return status;
824 status = nfsd4_decode_share_deny(argp, &open_down->od_share_deny);
825 if (status)
826 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 DECODE_TAIL;
828}
829
Al Virob37ad282006-10-19 23:28:59 -0700830static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831nfsd4_decode_putfh(struct nfsd4_compoundargs *argp, struct nfsd4_putfh *putfh)
832{
833 DECODE_HEAD;
834
835 READ_BUF(4);
836 READ32(putfh->pf_fhlen);
837 if (putfh->pf_fhlen > NFS4_FHSIZE)
838 goto xdr_error;
839 READ_BUF(putfh->pf_fhlen);
840 SAVEMEM(putfh->pf_fhval, putfh->pf_fhlen);
841
842 DECODE_TAIL;
843}
844
Al Virob37ad282006-10-19 23:28:59 -0700845static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846nfsd4_decode_read(struct nfsd4_compoundargs *argp, struct nfsd4_read *read)
847{
848 DECODE_HEAD;
849
Benny Halevye31a1b62008-08-12 20:46:18 +0300850 status = nfsd4_decode_stateid(argp, &read->rd_stateid);
851 if (status)
852 return status;
853 READ_BUF(12);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 READ64(read->rd_offset);
855 READ32(read->rd_length);
856
857 DECODE_TAIL;
858}
859
Al Virob37ad282006-10-19 23:28:59 -0700860static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861nfsd4_decode_readdir(struct nfsd4_compoundargs *argp, struct nfsd4_readdir *readdir)
862{
863 DECODE_HEAD;
864
865 READ_BUF(24);
866 READ64(readdir->rd_cookie);
867 COPYMEM(readdir->rd_verf.data, sizeof(readdir->rd_verf.data));
868 READ32(readdir->rd_dircount); /* just in case you needed a useless field... */
869 READ32(readdir->rd_maxcount);
870 if ((status = nfsd4_decode_bitmap(argp, readdir->rd_bmval)))
871 goto out;
872
873 DECODE_TAIL;
874}
875
Al Virob37ad282006-10-19 23:28:59 -0700876static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877nfsd4_decode_remove(struct nfsd4_compoundargs *argp, struct nfsd4_remove *remove)
878{
879 DECODE_HEAD;
880
881 READ_BUF(4);
882 READ32(remove->rm_namelen);
883 READ_BUF(remove->rm_namelen);
884 SAVEMEM(remove->rm_name, remove->rm_namelen);
885 if ((status = check_filename(remove->rm_name, remove->rm_namelen, nfserr_noent)))
886 return status;
887
888 DECODE_TAIL;
889}
890
Al Virob37ad282006-10-19 23:28:59 -0700891static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892nfsd4_decode_rename(struct nfsd4_compoundargs *argp, struct nfsd4_rename *rename)
893{
894 DECODE_HEAD;
895
896 READ_BUF(4);
897 READ32(rename->rn_snamelen);
898 READ_BUF(rename->rn_snamelen + 4);
899 SAVEMEM(rename->rn_sname, rename->rn_snamelen);
900 READ32(rename->rn_tnamelen);
901 READ_BUF(rename->rn_tnamelen);
902 SAVEMEM(rename->rn_tname, rename->rn_tnamelen);
903 if ((status = check_filename(rename->rn_sname, rename->rn_snamelen, nfserr_noent)))
904 return status;
905 if ((status = check_filename(rename->rn_tname, rename->rn_tnamelen, nfserr_inval)))
906 return status;
907
908 DECODE_TAIL;
909}
910
Al Virob37ad282006-10-19 23:28:59 -0700911static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912nfsd4_decode_renew(struct nfsd4_compoundargs *argp, clientid_t *clientid)
913{
914 DECODE_HEAD;
915
916 READ_BUF(sizeof(clientid_t));
917 COPYMEM(clientid, sizeof(clientid_t));
918
919 DECODE_TAIL;
920}
921
Al Virob37ad282006-10-19 23:28:59 -0700922static __be32
Andy Adamsondcb488a32007-07-17 04:04:51 -0700923nfsd4_decode_secinfo(struct nfsd4_compoundargs *argp,
924 struct nfsd4_secinfo *secinfo)
925{
926 DECODE_HEAD;
927
928 READ_BUF(4);
929 READ32(secinfo->si_namelen);
930 READ_BUF(secinfo->si_namelen);
931 SAVEMEM(secinfo->si_name, secinfo->si_namelen);
932 status = check_filename(secinfo->si_name, secinfo->si_namelen,
933 nfserr_noent);
934 if (status)
935 return status;
936 DECODE_TAIL;
937}
938
939static __be32
J. Bruce Fields04f4ad12010-12-16 09:51:13 -0500940nfsd4_decode_secinfo_no_name(struct nfsd4_compoundargs *argp,
941 struct nfsd4_secinfo_no_name *sin)
942{
943 DECODE_HEAD;
944
945 READ_BUF(4);
946 READ32(sin->sin_style);
947 DECODE_TAIL;
948}
949
950static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951nfsd4_decode_setattr(struct nfsd4_compoundargs *argp, struct nfsd4_setattr *setattr)
952{
Benny Halevye31a1b62008-08-12 20:46:18 +0300953 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
Benny Halevye31a1b62008-08-12 20:46:18 +0300955 status = nfsd4_decode_stateid(argp, &setattr->sa_stateid);
956 if (status)
957 return status;
Yu Zhiguo3c8e0312009-05-16 16:22:31 +0800958 return nfsd4_decode_fattr(argp, setattr->sa_bmval, &setattr->sa_iattr,
959 &setattr->sa_acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960}
961
Al Virob37ad282006-10-19 23:28:59 -0700962static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963nfsd4_decode_setclientid(struct nfsd4_compoundargs *argp, struct nfsd4_setclientid *setclientid)
964{
965 DECODE_HEAD;
966
967 READ_BUF(12);
968 COPYMEM(setclientid->se_verf.data, 8);
969 READ32(setclientid->se_namelen);
970
971 READ_BUF(setclientid->se_namelen + 8);
972 SAVEMEM(setclientid->se_name, setclientid->se_namelen);
973 READ32(setclientid->se_callback_prog);
974 READ32(setclientid->se_callback_netid_len);
975
976 READ_BUF(setclientid->se_callback_netid_len + 4);
977 SAVEMEM(setclientid->se_callback_netid_val, setclientid->se_callback_netid_len);
978 READ32(setclientid->se_callback_addr_len);
979
980 READ_BUF(setclientid->se_callback_addr_len + 4);
981 SAVEMEM(setclientid->se_callback_addr_val, setclientid->se_callback_addr_len);
982 READ32(setclientid->se_callback_ident);
983
984 DECODE_TAIL;
985}
986
Al Virob37ad282006-10-19 23:28:59 -0700987static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988nfsd4_decode_setclientid_confirm(struct nfsd4_compoundargs *argp, struct nfsd4_setclientid_confirm *scd_c)
989{
990 DECODE_HEAD;
991
992 READ_BUF(8 + sizeof(nfs4_verifier));
993 COPYMEM(&scd_c->sc_clientid, 8);
994 COPYMEM(&scd_c->sc_confirm, sizeof(nfs4_verifier));
995
996 DECODE_TAIL;
997}
998
999/* Also used for NVERIFY */
Al Virob37ad282006-10-19 23:28:59 -07001000static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001nfsd4_decode_verify(struct nfsd4_compoundargs *argp, struct nfsd4_verify *verify)
1002{
1003#if 0
1004 struct nfsd4_compoundargs save = {
1005 .p = argp->p,
1006 .end = argp->end,
1007 .rqstp = argp->rqstp,
1008 };
1009 u32 ve_bmval[2];
1010 struct iattr ve_iattr; /* request */
1011 struct nfs4_acl *ve_acl; /* request */
1012#endif
1013 DECODE_HEAD;
1014
1015 if ((status = nfsd4_decode_bitmap(argp, verify->ve_bmval)))
1016 goto out;
1017
1018 /* For convenience's sake, we compare raw xdr'd attributes in
1019 * nfsd4_proc_verify; however we still decode here just to return
1020 * correct error in case of bad xdr. */
1021#if 0
1022 status = nfsd4_decode_fattr(ve_bmval, &ve_iattr, &ve_acl);
1023 if (status == nfserr_inval) {
1024 status = nfserrno(status);
1025 goto out;
1026 }
1027#endif
1028 READ_BUF(4);
1029 READ32(verify->ve_attrlen);
1030 READ_BUF(verify->ve_attrlen);
1031 SAVEMEM(verify->ve_attrval, verify->ve_attrlen);
1032
1033 DECODE_TAIL;
1034}
1035
Al Virob37ad282006-10-19 23:28:59 -07001036static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037nfsd4_decode_write(struct nfsd4_compoundargs *argp, struct nfsd4_write *write)
1038{
1039 int avail;
1040 int v;
1041 int len;
1042 DECODE_HEAD;
1043
Benny Halevye31a1b62008-08-12 20:46:18 +03001044 status = nfsd4_decode_stateid(argp, &write->wr_stateid);
1045 if (status)
1046 return status;
1047 READ_BUF(16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 READ64(write->wr_offset);
1049 READ32(write->wr_stable_how);
1050 if (write->wr_stable_how > 2)
1051 goto xdr_error;
1052 READ32(write->wr_buflen);
1053
1054 /* Sorry .. no magic macros for this.. *
1055 * READ_BUF(write->wr_buflen);
1056 * SAVEMEM(write->wr_buf, write->wr_buflen);
1057 */
1058 avail = (char*)argp->end - (char*)argp->p;
1059 if (avail + argp->pagelen < write->wr_buflen) {
Chuck Lever817cb9d2007-09-11 18:01:20 -04001060 dprintk("NFSD: xdr error (%s:%d)\n",
1061 __FILE__, __LINE__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 goto xdr_error;
1063 }
NeilBrown3cc03b12006-10-04 02:15:47 -07001064 argp->rqstp->rq_vec[0].iov_base = p;
1065 argp->rqstp->rq_vec[0].iov_len = avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 v = 0;
1067 len = write->wr_buflen;
NeilBrown3cc03b12006-10-04 02:15:47 -07001068 while (len > argp->rqstp->rq_vec[v].iov_len) {
1069 len -= argp->rqstp->rq_vec[v].iov_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 v++;
NeilBrown3cc03b12006-10-04 02:15:47 -07001071 argp->rqstp->rq_vec[v].iov_base = page_address(argp->pagelist[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 argp->pagelist++;
1073 if (argp->pagelen >= PAGE_SIZE) {
NeilBrown3cc03b12006-10-04 02:15:47 -07001074 argp->rqstp->rq_vec[v].iov_len = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 argp->pagelen -= PAGE_SIZE;
1076 } else {
NeilBrown3cc03b12006-10-04 02:15:47 -07001077 argp->rqstp->rq_vec[v].iov_len = argp->pagelen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 argp->pagelen -= len;
1079 }
1080 }
Al Viro2ebbc012006-10-19 23:28:58 -07001081 argp->end = (__be32*) (argp->rqstp->rq_vec[v].iov_base + argp->rqstp->rq_vec[v].iov_len);
1082 argp->p = (__be32*) (argp->rqstp->rq_vec[v].iov_base + (XDR_QUADLEN(len) << 2));
NeilBrown3cc03b12006-10-04 02:15:47 -07001083 argp->rqstp->rq_vec[v].iov_len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 write->wr_vlen = v+1;
1085
1086 DECODE_TAIL;
1087}
1088
Al Virob37ad282006-10-19 23:28:59 -07001089static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090nfsd4_decode_release_lockowner(struct nfsd4_compoundargs *argp, struct nfsd4_release_lockowner *rlockowner)
1091{
1092 DECODE_HEAD;
1093
1094 READ_BUF(12);
1095 COPYMEM(&rlockowner->rl_clientid, sizeof(clientid_t));
1096 READ32(rlockowner->rl_owner.len);
1097 READ_BUF(rlockowner->rl_owner.len);
1098 READMEM(rlockowner->rl_owner.data, rlockowner->rl_owner.len);
1099
Andy Adamson60adfc52009-04-03 08:28:50 +03001100 if (argp->minorversion && !zero_clientid(&rlockowner->rl_clientid))
1101 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 DECODE_TAIL;
1103}
1104
Al Virob37ad282006-10-19 23:28:59 -07001105static __be32
Andy Adamson2db134e2009-04-03 08:27:55 +03001106nfsd4_decode_exchange_id(struct nfsd4_compoundargs *argp,
Andy Adamson0733d212009-04-03 08:28:01 +03001107 struct nfsd4_exchange_id *exid)
Andy Adamson2db134e2009-04-03 08:27:55 +03001108{
Mi Jinlong5afa0402010-11-09 09:39:23 +08001109 int dummy, tmp;
Andy Adamson0733d212009-04-03 08:28:01 +03001110 DECODE_HEAD;
1111
1112 READ_BUF(NFS4_VERIFIER_SIZE);
1113 COPYMEM(exid->verifier.data, NFS4_VERIFIER_SIZE);
1114
1115 READ_BUF(4);
1116 READ32(exid->clname.len);
1117
1118 READ_BUF(exid->clname.len);
1119 SAVEMEM(exid->clname.data, exid->clname.len);
1120
1121 READ_BUF(4);
1122 READ32(exid->flags);
1123
1124 /* Ignore state_protect4_a */
1125 READ_BUF(4);
1126 READ32(exid->spa_how);
1127 switch (exid->spa_how) {
1128 case SP4_NONE:
1129 break;
1130 case SP4_MACH_CRED:
1131 /* spo_must_enforce */
1132 READ_BUF(4);
1133 READ32(dummy);
1134 READ_BUF(dummy * 4);
1135 p += dummy;
1136
1137 /* spo_must_allow */
1138 READ_BUF(4);
1139 READ32(dummy);
1140 READ_BUF(dummy * 4);
1141 p += dummy;
1142 break;
1143 case SP4_SSV:
1144 /* ssp_ops */
1145 READ_BUF(4);
1146 READ32(dummy);
1147 READ_BUF(dummy * 4);
1148 p += dummy;
1149
1150 READ_BUF(4);
1151 READ32(dummy);
1152 READ_BUF(dummy * 4);
1153 p += dummy;
1154
1155 /* ssp_hash_algs<> */
1156 READ_BUF(4);
Mi Jinlong5afa0402010-11-09 09:39:23 +08001157 READ32(tmp);
1158 while (tmp--) {
1159 READ_BUF(4);
1160 READ32(dummy);
1161 READ_BUF(dummy);
1162 p += XDR_QUADLEN(dummy);
1163 }
Andy Adamson0733d212009-04-03 08:28:01 +03001164
1165 /* ssp_encr_algs<> */
1166 READ_BUF(4);
Mi Jinlong5afa0402010-11-09 09:39:23 +08001167 READ32(tmp);
1168 while (tmp--) {
1169 READ_BUF(4);
1170 READ32(dummy);
1171 READ_BUF(dummy);
1172 p += XDR_QUADLEN(dummy);
1173 }
Andy Adamson0733d212009-04-03 08:28:01 +03001174
1175 /* ssp_window and ssp_num_gss_handles */
1176 READ_BUF(8);
1177 READ32(dummy);
1178 READ32(dummy);
1179 break;
1180 default:
1181 goto xdr_error;
1182 }
1183
1184 /* Ignore Implementation ID */
1185 READ_BUF(4); /* nfs_impl_id4 array length */
1186 READ32(dummy);
1187
1188 if (dummy > 1)
1189 goto xdr_error;
1190
1191 if (dummy == 1) {
1192 /* nii_domain */
1193 READ_BUF(4);
1194 READ32(dummy);
1195 READ_BUF(dummy);
1196 p += XDR_QUADLEN(dummy);
1197
1198 /* nii_name */
1199 READ_BUF(4);
1200 READ32(dummy);
1201 READ_BUF(dummy);
1202 p += XDR_QUADLEN(dummy);
1203
1204 /* nii_date */
1205 READ_BUF(12);
1206 p += 3;
1207 }
1208 DECODE_TAIL;
Andy Adamson2db134e2009-04-03 08:27:55 +03001209}
1210
1211static __be32
1212nfsd4_decode_create_session(struct nfsd4_compoundargs *argp,
1213 struct nfsd4_create_session *sess)
1214{
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001215 DECODE_HEAD;
1216
1217 u32 dummy;
1218 char *machine_name;
Mi Jinlong5a02ab72011-03-11 12:13:55 +08001219 int i;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001220 int nr_secflavs;
1221
1222 READ_BUF(16);
1223 COPYMEM(&sess->clientid, 8);
1224 READ32(sess->seqid);
1225 READ32(sess->flags);
1226
1227 /* Fore channel attrs */
1228 READ_BUF(28);
1229 READ32(dummy); /* headerpadsz is always 0 */
1230 READ32(sess->fore_channel.maxreq_sz);
1231 READ32(sess->fore_channel.maxresp_sz);
1232 READ32(sess->fore_channel.maxresp_cached);
1233 READ32(sess->fore_channel.maxops);
1234 READ32(sess->fore_channel.maxreqs);
1235 READ32(sess->fore_channel.nr_rdma_attrs);
1236 if (sess->fore_channel.nr_rdma_attrs == 1) {
1237 READ_BUF(4);
1238 READ32(sess->fore_channel.rdma_attrs);
1239 } else if (sess->fore_channel.nr_rdma_attrs > 1) {
1240 dprintk("Too many fore channel attr bitmaps!\n");
1241 goto xdr_error;
1242 }
1243
1244 /* Back channel attrs */
1245 READ_BUF(28);
1246 READ32(dummy); /* headerpadsz is always 0 */
1247 READ32(sess->back_channel.maxreq_sz);
1248 READ32(sess->back_channel.maxresp_sz);
1249 READ32(sess->back_channel.maxresp_cached);
1250 READ32(sess->back_channel.maxops);
1251 READ32(sess->back_channel.maxreqs);
1252 READ32(sess->back_channel.nr_rdma_attrs);
1253 if (sess->back_channel.nr_rdma_attrs == 1) {
1254 READ_BUF(4);
1255 READ32(sess->back_channel.rdma_attrs);
1256 } else if (sess->back_channel.nr_rdma_attrs > 1) {
1257 dprintk("Too many back channel attr bitmaps!\n");
1258 goto xdr_error;
1259 }
1260
1261 READ_BUF(8);
1262 READ32(sess->callback_prog);
1263
1264 /* callback_sec_params4 */
1265 READ32(nr_secflavs);
1266 for (i = 0; i < nr_secflavs; ++i) {
1267 READ_BUF(4);
1268 READ32(dummy);
1269 switch (dummy) {
1270 case RPC_AUTH_NULL:
1271 /* Nothing to read */
1272 break;
1273 case RPC_AUTH_UNIX:
1274 READ_BUF(8);
1275 /* stamp */
1276 READ32(dummy);
1277
1278 /* machine name */
1279 READ32(dummy);
1280 READ_BUF(dummy);
1281 SAVEMEM(machine_name, dummy);
1282
1283 /* uid, gid */
1284 READ_BUF(8);
1285 READ32(sess->uid);
1286 READ32(sess->gid);
1287
1288 /* more gids */
1289 READ_BUF(4);
1290 READ32(dummy);
1291 READ_BUF(dummy * 4);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001292 break;
1293 case RPC_AUTH_GSS:
1294 dprintk("RPC_AUTH_GSS callback secflavor "
1295 "not supported!\n");
1296 READ_BUF(8);
1297 /* gcbp_service */
1298 READ32(dummy);
1299 /* gcbp_handle_from_server */
1300 READ32(dummy);
1301 READ_BUF(dummy);
1302 p += XDR_QUADLEN(dummy);
1303 /* gcbp_handle_from_client */
1304 READ_BUF(4);
1305 READ32(dummy);
1306 READ_BUF(dummy);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001307 break;
1308 default:
1309 dprintk("Illegal callback secflavor\n");
1310 return nfserr_inval;
1311 }
1312 }
1313 DECODE_TAIL;
Andy Adamson2db134e2009-04-03 08:27:55 +03001314}
1315
1316static __be32
1317nfsd4_decode_destroy_session(struct nfsd4_compoundargs *argp,
1318 struct nfsd4_destroy_session *destroy_session)
1319{
Benny Halevye10e0cf2009-04-03 08:28:38 +03001320 DECODE_HEAD;
1321 READ_BUF(NFS4_MAX_SESSIONID_LEN);
1322 COPYMEM(destroy_session->sessionid.data, NFS4_MAX_SESSIONID_LEN);
1323
1324 DECODE_TAIL;
Andy Adamson2db134e2009-04-03 08:27:55 +03001325}
1326
1327static __be32
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04001328nfsd4_decode_free_stateid(struct nfsd4_compoundargs *argp,
1329 struct nfsd4_free_stateid *free_stateid)
1330{
1331 DECODE_HEAD;
1332
1333 READ_BUF(sizeof(stateid_t));
1334 READ32(free_stateid->fr_stateid.si_generation);
1335 COPYMEM(&free_stateid->fr_stateid.si_opaque, sizeof(stateid_opaque_t));
1336
1337 DECODE_TAIL;
1338}
1339
1340static __be32
Andy Adamson2db134e2009-04-03 08:27:55 +03001341nfsd4_decode_sequence(struct nfsd4_compoundargs *argp,
1342 struct nfsd4_sequence *seq)
1343{
Benny Halevyb85d4c02009-04-03 08:28:08 +03001344 DECODE_HEAD;
1345
1346 READ_BUF(NFS4_MAX_SESSIONID_LEN + 16);
1347 COPYMEM(seq->sessionid.data, NFS4_MAX_SESSIONID_LEN);
1348 READ32(seq->seqid);
1349 READ32(seq->slotid);
1350 READ32(seq->maxslots);
1351 READ32(seq->cachethis);
1352
1353 DECODE_TAIL;
Andy Adamson2db134e2009-04-03 08:27:55 +03001354}
1355
Bryan Schumaker17456802011-07-13 10:50:48 -04001356static __be32
1357nfsd4_decode_test_stateid(struct nfsd4_compoundargs *argp, struct nfsd4_test_stateid *test_stateid)
1358{
1359 unsigned int nbytes;
1360 stateid_t si;
1361 int i;
1362 __be32 *p;
1363 __be32 status;
1364
1365 READ_BUF(4);
1366 test_stateid->ts_num_ids = ntohl(*p++);
1367
1368 nbytes = test_stateid->ts_num_ids * sizeof(stateid_t);
1369 if (nbytes > (u32)((char *)argp->end - (char *)argp->p))
1370 goto xdr_error;
1371
1372 test_stateid->ts_saved_args = argp;
1373 save_buf(argp, &test_stateid->ts_savedp);
1374
1375 for (i = 0; i < test_stateid->ts_num_ids; i++) {
1376 status = nfsd4_decode_stateid(argp, &si);
1377 if (status)
1378 return status;
1379 }
1380
1381 status = 0;
1382out:
1383 return status;
1384xdr_error:
1385 dprintk("NFSD: xdr error (%s:%d)\n", __FILE__, __LINE__);
1386 status = nfserr_bad_xdr;
1387 goto out;
1388}
1389
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04001390static __be32 nfsd4_decode_reclaim_complete(struct nfsd4_compoundargs *argp, struct nfsd4_reclaim_complete *rc)
1391{
1392 DECODE_HEAD;
1393
1394 READ_BUF(4);
1395 READ32(rc->rca_one_fs);
1396
1397 DECODE_TAIL;
1398}
1399
Andy Adamson2db134e2009-04-03 08:27:55 +03001400static __be32
Benny Halevy347e0ad2008-07-02 11:13:41 +03001401nfsd4_decode_noop(struct nfsd4_compoundargs *argp, void *p)
1402{
1403 return nfs_ok;
1404}
1405
Benny Halevy3c375c62008-07-02 11:14:01 +03001406static __be32
1407nfsd4_decode_notsupp(struct nfsd4_compoundargs *argp, void *p)
1408{
Benny Halevy1e685ec2009-03-04 23:06:06 +02001409 return nfserr_notsupp;
Benny Halevy3c375c62008-07-02 11:14:01 +03001410}
1411
Benny Halevy347e0ad2008-07-02 11:13:41 +03001412typedef __be32(*nfsd4_dec)(struct nfsd4_compoundargs *argp, void *);
1413
1414static nfsd4_dec nfsd4_dec_ops[] = {
J. Bruce Fieldsad1060c2008-07-18 15:04:16 -04001415 [OP_ACCESS] = (nfsd4_dec)nfsd4_decode_access,
1416 [OP_CLOSE] = (nfsd4_dec)nfsd4_decode_close,
1417 [OP_COMMIT] = (nfsd4_dec)nfsd4_decode_commit,
1418 [OP_CREATE] = (nfsd4_dec)nfsd4_decode_create,
1419 [OP_DELEGPURGE] = (nfsd4_dec)nfsd4_decode_notsupp,
1420 [OP_DELEGRETURN] = (nfsd4_dec)nfsd4_decode_delegreturn,
1421 [OP_GETATTR] = (nfsd4_dec)nfsd4_decode_getattr,
1422 [OP_GETFH] = (nfsd4_dec)nfsd4_decode_noop,
1423 [OP_LINK] = (nfsd4_dec)nfsd4_decode_link,
1424 [OP_LOCK] = (nfsd4_dec)nfsd4_decode_lock,
1425 [OP_LOCKT] = (nfsd4_dec)nfsd4_decode_lockt,
1426 [OP_LOCKU] = (nfsd4_dec)nfsd4_decode_locku,
1427 [OP_LOOKUP] = (nfsd4_dec)nfsd4_decode_lookup,
1428 [OP_LOOKUPP] = (nfsd4_dec)nfsd4_decode_noop,
1429 [OP_NVERIFY] = (nfsd4_dec)nfsd4_decode_verify,
1430 [OP_OPEN] = (nfsd4_dec)nfsd4_decode_open,
1431 [OP_OPENATTR] = (nfsd4_dec)nfsd4_decode_notsupp,
1432 [OP_OPEN_CONFIRM] = (nfsd4_dec)nfsd4_decode_open_confirm,
1433 [OP_OPEN_DOWNGRADE] = (nfsd4_dec)nfsd4_decode_open_downgrade,
1434 [OP_PUTFH] = (nfsd4_dec)nfsd4_decode_putfh,
J. Bruce Fieldsa1c8c4d2009-03-09 12:17:29 -04001435 [OP_PUTPUBFH] = (nfsd4_dec)nfsd4_decode_noop,
J. Bruce Fieldsad1060c2008-07-18 15:04:16 -04001436 [OP_PUTROOTFH] = (nfsd4_dec)nfsd4_decode_noop,
1437 [OP_READ] = (nfsd4_dec)nfsd4_decode_read,
1438 [OP_READDIR] = (nfsd4_dec)nfsd4_decode_readdir,
1439 [OP_READLINK] = (nfsd4_dec)nfsd4_decode_noop,
1440 [OP_REMOVE] = (nfsd4_dec)nfsd4_decode_remove,
1441 [OP_RENAME] = (nfsd4_dec)nfsd4_decode_rename,
1442 [OP_RENEW] = (nfsd4_dec)nfsd4_decode_renew,
1443 [OP_RESTOREFH] = (nfsd4_dec)nfsd4_decode_noop,
1444 [OP_SAVEFH] = (nfsd4_dec)nfsd4_decode_noop,
1445 [OP_SECINFO] = (nfsd4_dec)nfsd4_decode_secinfo,
1446 [OP_SETATTR] = (nfsd4_dec)nfsd4_decode_setattr,
1447 [OP_SETCLIENTID] = (nfsd4_dec)nfsd4_decode_setclientid,
1448 [OP_SETCLIENTID_CONFIRM] = (nfsd4_dec)nfsd4_decode_setclientid_confirm,
1449 [OP_VERIFY] = (nfsd4_dec)nfsd4_decode_verify,
1450 [OP_WRITE] = (nfsd4_dec)nfsd4_decode_write,
1451 [OP_RELEASE_LOCKOWNER] = (nfsd4_dec)nfsd4_decode_release_lockowner,
Benny Halevy347e0ad2008-07-02 11:13:41 +03001452};
1453
Andy Adamson2db134e2009-04-03 08:27:55 +03001454static nfsd4_dec nfsd41_dec_ops[] = {
Randy Dunlap9064caa2009-04-28 16:48:25 -07001455 [OP_ACCESS] = (nfsd4_dec)nfsd4_decode_access,
1456 [OP_CLOSE] = (nfsd4_dec)nfsd4_decode_close,
1457 [OP_COMMIT] = (nfsd4_dec)nfsd4_decode_commit,
1458 [OP_CREATE] = (nfsd4_dec)nfsd4_decode_create,
1459 [OP_DELEGPURGE] = (nfsd4_dec)nfsd4_decode_notsupp,
1460 [OP_DELEGRETURN] = (nfsd4_dec)nfsd4_decode_delegreturn,
1461 [OP_GETATTR] = (nfsd4_dec)nfsd4_decode_getattr,
1462 [OP_GETFH] = (nfsd4_dec)nfsd4_decode_noop,
1463 [OP_LINK] = (nfsd4_dec)nfsd4_decode_link,
1464 [OP_LOCK] = (nfsd4_dec)nfsd4_decode_lock,
1465 [OP_LOCKT] = (nfsd4_dec)nfsd4_decode_lockt,
1466 [OP_LOCKU] = (nfsd4_dec)nfsd4_decode_locku,
1467 [OP_LOOKUP] = (nfsd4_dec)nfsd4_decode_lookup,
1468 [OP_LOOKUPP] = (nfsd4_dec)nfsd4_decode_noop,
1469 [OP_NVERIFY] = (nfsd4_dec)nfsd4_decode_verify,
1470 [OP_OPEN] = (nfsd4_dec)nfsd4_decode_open,
1471 [OP_OPENATTR] = (nfsd4_dec)nfsd4_decode_notsupp,
1472 [OP_OPEN_CONFIRM] = (nfsd4_dec)nfsd4_decode_notsupp,
1473 [OP_OPEN_DOWNGRADE] = (nfsd4_dec)nfsd4_decode_open_downgrade,
1474 [OP_PUTFH] = (nfsd4_dec)nfsd4_decode_putfh,
1475 [OP_PUTPUBFH] = (nfsd4_dec)nfsd4_decode_notsupp,
1476 [OP_PUTROOTFH] = (nfsd4_dec)nfsd4_decode_noop,
1477 [OP_READ] = (nfsd4_dec)nfsd4_decode_read,
1478 [OP_READDIR] = (nfsd4_dec)nfsd4_decode_readdir,
1479 [OP_READLINK] = (nfsd4_dec)nfsd4_decode_noop,
1480 [OP_REMOVE] = (nfsd4_dec)nfsd4_decode_remove,
1481 [OP_RENAME] = (nfsd4_dec)nfsd4_decode_rename,
1482 [OP_RENEW] = (nfsd4_dec)nfsd4_decode_notsupp,
1483 [OP_RESTOREFH] = (nfsd4_dec)nfsd4_decode_noop,
1484 [OP_SAVEFH] = (nfsd4_dec)nfsd4_decode_noop,
1485 [OP_SECINFO] = (nfsd4_dec)nfsd4_decode_secinfo,
1486 [OP_SETATTR] = (nfsd4_dec)nfsd4_decode_setattr,
1487 [OP_SETCLIENTID] = (nfsd4_dec)nfsd4_decode_notsupp,
1488 [OP_SETCLIENTID_CONFIRM]= (nfsd4_dec)nfsd4_decode_notsupp,
1489 [OP_VERIFY] = (nfsd4_dec)nfsd4_decode_verify,
1490 [OP_WRITE] = (nfsd4_dec)nfsd4_decode_write,
1491 [OP_RELEASE_LOCKOWNER] = (nfsd4_dec)nfsd4_decode_notsupp,
Andy Adamson2db134e2009-04-03 08:27:55 +03001492
1493 /* new operations for NFSv4.1 */
Randy Dunlap9064caa2009-04-28 16:48:25 -07001494 [OP_BACKCHANNEL_CTL] = (nfsd4_dec)nfsd4_decode_notsupp,
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04001495 [OP_BIND_CONN_TO_SESSION]= (nfsd4_dec)nfsd4_decode_bind_conn_to_session,
Randy Dunlap9064caa2009-04-28 16:48:25 -07001496 [OP_EXCHANGE_ID] = (nfsd4_dec)nfsd4_decode_exchange_id,
1497 [OP_CREATE_SESSION] = (nfsd4_dec)nfsd4_decode_create_session,
1498 [OP_DESTROY_SESSION] = (nfsd4_dec)nfsd4_decode_destroy_session,
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04001499 [OP_FREE_STATEID] = (nfsd4_dec)nfsd4_decode_free_stateid,
Randy Dunlap9064caa2009-04-28 16:48:25 -07001500 [OP_GET_DIR_DELEGATION] = (nfsd4_dec)nfsd4_decode_notsupp,
1501 [OP_GETDEVICEINFO] = (nfsd4_dec)nfsd4_decode_notsupp,
1502 [OP_GETDEVICELIST] = (nfsd4_dec)nfsd4_decode_notsupp,
1503 [OP_LAYOUTCOMMIT] = (nfsd4_dec)nfsd4_decode_notsupp,
1504 [OP_LAYOUTGET] = (nfsd4_dec)nfsd4_decode_notsupp,
1505 [OP_LAYOUTRETURN] = (nfsd4_dec)nfsd4_decode_notsupp,
J. Bruce Fields04f4ad12010-12-16 09:51:13 -05001506 [OP_SECINFO_NO_NAME] = (nfsd4_dec)nfsd4_decode_secinfo_no_name,
Randy Dunlap9064caa2009-04-28 16:48:25 -07001507 [OP_SEQUENCE] = (nfsd4_dec)nfsd4_decode_sequence,
1508 [OP_SET_SSV] = (nfsd4_dec)nfsd4_decode_notsupp,
Bryan Schumaker17456802011-07-13 10:50:48 -04001509 [OP_TEST_STATEID] = (nfsd4_dec)nfsd4_decode_test_stateid,
Randy Dunlap9064caa2009-04-28 16:48:25 -07001510 [OP_WANT_DELEGATION] = (nfsd4_dec)nfsd4_decode_notsupp,
1511 [OP_DESTROY_CLIENTID] = (nfsd4_dec)nfsd4_decode_notsupp,
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04001512 [OP_RECLAIM_COMPLETE] = (nfsd4_dec)nfsd4_decode_reclaim_complete,
Andy Adamson2db134e2009-04-03 08:27:55 +03001513};
1514
Benny Halevyf2feb962008-07-02 11:14:22 +03001515struct nfsd4_minorversion_ops {
1516 nfsd4_dec *decoders;
1517 int nops;
1518};
1519
1520static struct nfsd4_minorversion_ops nfsd4_minorversion[] = {
J. Bruce Fieldsad1060c2008-07-18 15:04:16 -04001521 [0] = { nfsd4_dec_ops, ARRAY_SIZE(nfsd4_dec_ops) },
Andy Adamson2db134e2009-04-03 08:27:55 +03001522 [1] = { nfsd41_dec_ops, ARRAY_SIZE(nfsd41_dec_ops) },
Benny Halevyf2feb962008-07-02 11:14:22 +03001523};
1524
Benny Halevy347e0ad2008-07-02 11:13:41 +03001525static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526nfsd4_decode_compound(struct nfsd4_compoundargs *argp)
1527{
1528 DECODE_HEAD;
1529 struct nfsd4_op *op;
Benny Halevyf2feb962008-07-02 11:14:22 +03001530 struct nfsd4_minorversion_ops *ops;
J. Bruce Fields10910062011-01-24 12:11:02 -05001531 bool cachethis = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 int i;
1533
1534 /*
1535 * XXX: According to spec, we should check the tag
1536 * for UTF-8 compliance. I'm postponing this for
1537 * now because it seems that some clients do use
1538 * binary tags.
1539 */
1540 READ_BUF(4);
1541 READ32(argp->taglen);
1542 READ_BUF(argp->taglen + 8);
1543 SAVEMEM(argp->tag, argp->taglen);
1544 READ32(argp->minorversion);
1545 READ32(argp->opcnt);
1546
1547 if (argp->taglen > NFSD4_MAX_TAGLEN)
1548 goto xdr_error;
1549 if (argp->opcnt > 100)
1550 goto xdr_error;
1551
Tobias Klausere8c96f82006-03-24 03:15:34 -08001552 if (argp->opcnt > ARRAY_SIZE(argp->iops)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 argp->ops = kmalloc(argp->opcnt * sizeof(*argp->ops), GFP_KERNEL);
1554 if (!argp->ops) {
1555 argp->ops = argp->iops;
Chuck Lever817cb9d2007-09-11 18:01:20 -04001556 dprintk("nfsd: couldn't allocate room for COMPOUND\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 goto xdr_error;
1558 }
1559 }
1560
Benny Halevyf2feb962008-07-02 11:14:22 +03001561 if (argp->minorversion >= ARRAY_SIZE(nfsd4_minorversion))
Benny Halevy30cff1f2008-07-02 11:13:18 +03001562 argp->opcnt = 0;
1563
Benny Halevyf2feb962008-07-02 11:14:22 +03001564 ops = &nfsd4_minorversion[argp->minorversion];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 for (i = 0; i < argp->opcnt; i++) {
1566 op = &argp->ops[i];
1567 op->replay = NULL;
1568
1569 /*
1570 * We can't use READ_BUF() here because we need to handle
1571 * a missing opcode as an OP_WRITE + 1. So we need to check
1572 * to see if we're truly at the end of our buffer or if there
1573 * is another page we need to flip to.
1574 */
1575
1576 if (argp->p == argp->end) {
1577 if (argp->pagelen < 4) {
1578 /* There isn't an opcode still on the wire */
1579 op->opnum = OP_WRITE + 1;
1580 op->status = nfserr_bad_xdr;
1581 argp->opcnt = i+1;
1582 break;
1583 }
1584
1585 /*
1586 * False alarm. We just hit a page boundary, but there
1587 * is still data available. Move pointer across page
1588 * boundary. *snip from READ_BUF*
1589 */
1590 argp->p = page_address(argp->pagelist[0]);
1591 argp->pagelist++;
1592 if (argp->pagelen < PAGE_SIZE) {
Neil Brown2bc3c112010-04-20 12:16:52 +10001593 argp->end = argp->p + (argp->pagelen>>2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 argp->pagelen = 0;
1595 } else {
Neil Brown2bc3c112010-04-20 12:16:52 +10001596 argp->end = argp->p + (PAGE_SIZE>>2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 argp->pagelen -= PAGE_SIZE;
1598 }
1599 }
1600 op->opnum = ntohl(*argp->p++);
1601
Ricardo Labiagade3cab72009-12-11 20:03:27 -08001602 if (op->opnum >= FIRST_NFS4_OP && op->opnum <= LAST_NFS4_OP)
Benny Halevyf2feb962008-07-02 11:14:22 +03001603 op->status = ops->decoders[op->opnum](argp, &op->u);
Benny Halevy347e0ad2008-07-02 11:13:41 +03001604 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 op->opnum = OP_ILLEGAL;
1606 op->status = nfserr_op_illegal;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 }
1608
1609 if (op->status) {
1610 argp->opcnt = i+1;
1611 break;
1612 }
J. Bruce Fields10910062011-01-24 12:11:02 -05001613 /*
1614 * We'll try to cache the result in the DRC if any one
1615 * op in the compound wants to be cached:
1616 */
1617 cachethis |= nfsd4_cache_this_op(op);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 }
J. Bruce Fields10910062011-01-24 12:11:02 -05001619 /* Sessions make the DRC unnecessary: */
1620 if (argp->minorversion)
1621 cachethis = false;
1622 argp->rqstp->rq_cachetype = cachethis ? RC_REPLBUFF : RC_NOCACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
1624 DECODE_TAIL;
1625}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627#define WRITE32(n) *p++ = htonl(n)
1628#define WRITE64(n) do { \
1629 *p++ = htonl((u32)((n) >> 32)); \
1630 *p++ = htonl((u32)(n)); \
1631} while (0)
Harvey Harrison5108b272008-07-17 21:33:04 -07001632#define WRITEMEM(ptr,nbytes) do { if (nbytes > 0) { \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 *(p + XDR_QUADLEN(nbytes) -1) = 0; \
1634 memcpy(p, ptr, nbytes); \
1635 p += XDR_QUADLEN(nbytes); \
Harvey Harrison5108b272008-07-17 21:33:04 -07001636}} while (0)
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04001637
1638static void write32(__be32 **p, u32 n)
1639{
1640 *(*p)++ = n;
1641}
1642
1643static void write64(__be32 **p, u64 n)
1644{
1645 write32(p, (u32)(n >> 32));
1646 write32(p, (u32)n);
1647}
1648
1649static void write_change(__be32 **p, struct kstat *stat, struct inode *inode)
1650{
1651 if (IS_I_VERSION(inode)) {
1652 write64(p, inode->i_version);
1653 } else {
1654 write32(p, stat->ctime.tv_sec);
1655 write32(p, stat->ctime.tv_nsec);
1656 }
1657}
1658
1659static void write_cinfo(__be32 **p, struct nfsd4_change_info *c)
1660{
1661 write32(p, c->atomic);
1662 if (c->change_supported) {
1663 write64(p, c->before_change);
1664 write64(p, c->after_change);
1665 } else {
1666 write32(p, c->before_ctime_sec);
1667 write32(p, c->before_ctime_nsec);
1668 write32(p, c->after_ctime_sec);
1669 write32(p, c->after_ctime_nsec);
1670 }
1671}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672
1673#define RESERVE_SPACE(nbytes) do { \
1674 p = resp->p; \
1675 BUG_ON(p + XDR_QUADLEN(nbytes) > resp->end); \
1676} while (0)
1677#define ADJUST_ARGS() resp->p = p
1678
1679/*
1680 * Header routine to setup seqid operation replay cache
1681 */
1682#define ENCODE_SEQID_OP_HEAD \
Al Viro2ebbc012006-10-19 23:28:58 -07001683 __be32 *save; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 \
1685 save = resp->p;
1686
1687/*
NeilBrown7fb64ce2005-07-07 17:59:20 -07001688 * Routine for encoding the result of a "seqid-mutating" NFSv4 operation. This
1689 * is where sequence id's are incremented, and the replay cache is filled.
1690 * Note that we increment sequence id's here, at the last moment, so we're sure
1691 * we know whether the error to be returned is a sequence id mutating error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 */
1693
J. Bruce Fieldsf3e42232011-08-24 12:27:31 -04001694static void encode_seqid_op_tail(struct nfsd4_compoundres *resp, __be32 *save, __be32 nfserr)
1695{
1696 struct nfs4_stateowner *stateowner = resp->cstate.replay_owner;
1697
1698 if (seqid_mutating_err(ntohl(nfserr)) && stateowner) {
1699 stateowner->so_seqid++;
1700 stateowner->so_replay.rp_status = nfserr;
1701 stateowner->so_replay.rp_buflen =
1702 (char *)resp->p - (char *)save;
1703 memcpy(stateowner->so_replay.rp_buf, save,
1704 stateowner->so_replay.rp_buflen);
J. Bruce Fields38c387b2011-09-16 17:42:48 -04001705 nfsd4_purge_closed_stateid(stateowner);
J. Bruce Fieldsf3e42232011-08-24 12:27:31 -04001706 }
1707}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001709/* Encode as an array of strings the string given with components
Daniel Mack3ad2f3f2010-02-03 08:01:28 +08001710 * separated @sep.
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001711 */
Al Virob37ad282006-10-19 23:28:59 -07001712static __be32 nfsd4_encode_components(char sep, char *components,
Al Viro2ebbc012006-10-19 23:28:58 -07001713 __be32 **pp, int *buflen)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001714{
Al Viro2ebbc012006-10-19 23:28:58 -07001715 __be32 *p = *pp;
1716 __be32 *countp = p;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001717 int strlen, count=0;
1718 char *str, *end;
1719
1720 dprintk("nfsd4_encode_components(%s)\n", components);
1721 if ((*buflen -= 4) < 0)
1722 return nfserr_resource;
1723 WRITE32(0); /* We will fill this in with @count later */
1724 end = str = components;
1725 while (*end) {
1726 for (; *end && (*end != sep); end++)
1727 ; /* Point to end of component */
1728 strlen = end - str;
1729 if (strlen) {
1730 if ((*buflen -= ((XDR_QUADLEN(strlen) << 2) + 4)) < 0)
1731 return nfserr_resource;
1732 WRITE32(strlen);
1733 WRITEMEM(str, strlen);
1734 count++;
1735 }
1736 else
1737 end++;
1738 str = end;
1739 }
1740 *pp = p;
1741 p = countp;
1742 WRITE32(count);
1743 return 0;
1744}
1745
1746/*
1747 * encode a location element of a fs_locations structure
1748 */
Al Virob37ad282006-10-19 23:28:59 -07001749static __be32 nfsd4_encode_fs_location4(struct nfsd4_fs_location *location,
Al Viro2ebbc012006-10-19 23:28:58 -07001750 __be32 **pp, int *buflen)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001751{
Al Virob37ad282006-10-19 23:28:59 -07001752 __be32 status;
Al Viro2ebbc012006-10-19 23:28:58 -07001753 __be32 *p = *pp;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001754
1755 status = nfsd4_encode_components(':', location->hosts, &p, buflen);
1756 if (status)
1757 return status;
1758 status = nfsd4_encode_components('/', location->path, &p, buflen);
1759 if (status)
1760 return status;
1761 *pp = p;
1762 return 0;
1763}
1764
1765/*
Trond Myklebusted748aa2011-09-12 19:37:06 -04001766 * Encode a path in RFC3530 'pathname4' format
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001767 */
Trond Myklebusted748aa2011-09-12 19:37:06 -04001768static __be32 nfsd4_encode_path(const struct path *root,
1769 const struct path *path, __be32 **pp, int *buflen)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001770{
Trond Myklebusted748aa2011-09-12 19:37:06 -04001771 struct path cur = {
1772 .mnt = path->mnt,
1773 .dentry = path->dentry,
1774 };
1775 __be32 *p = *pp;
1776 struct dentry **components = NULL;
1777 unsigned int ncomponents = 0;
1778 __be32 err = nfserr_jukebox;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001779
Trond Myklebusted748aa2011-09-12 19:37:06 -04001780 dprintk("nfsd4_encode_components(");
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001781
Trond Myklebusted748aa2011-09-12 19:37:06 -04001782 path_get(&cur);
1783 /* First walk the path up to the nfsd root, and store the
1784 * dentries/path components in an array.
1785 */
1786 for (;;) {
1787 if (cur.dentry == root->dentry && cur.mnt == root->mnt)
1788 break;
1789 if (cur.dentry == cur.mnt->mnt_root) {
1790 if (follow_up(&cur))
1791 continue;
1792 goto out_free;
1793 }
1794 if ((ncomponents & 15) == 0) {
1795 struct dentry **new;
1796 new = krealloc(components,
1797 sizeof(*new) * (ncomponents + 16),
1798 GFP_KERNEL);
1799 if (!new)
1800 goto out_free;
1801 components = new;
1802 }
1803 components[ncomponents++] = cur.dentry;
1804 cur.dentry = dget_parent(cur.dentry);
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001805 }
Trond Myklebusted748aa2011-09-12 19:37:06 -04001806
1807 *buflen -= 4;
1808 if (*buflen < 0)
1809 goto out_free;
1810 WRITE32(ncomponents);
1811
1812 while (ncomponents) {
1813 struct dentry *dentry = components[ncomponents - 1];
1814 unsigned int len = dentry->d_name.len;
1815
1816 *buflen -= 4 + (XDR_QUADLEN(len) << 2);
1817 if (*buflen < 0)
1818 goto out_free;
1819 WRITE32(len);
1820 WRITEMEM(dentry->d_name.name, len);
1821 dprintk("/%s", dentry->d_name.name);
1822 dput(dentry);
1823 ncomponents--;
1824 }
1825
1826 *pp = p;
1827 err = 0;
1828out_free:
1829 dprintk(")\n");
1830 while (ncomponents)
1831 dput(components[--ncomponents]);
1832 kfree(components);
1833 path_put(&cur);
1834 return err;
1835}
1836
1837static __be32 nfsd4_encode_fsloc_fsroot(struct svc_rqst *rqstp,
1838 const struct path *path, __be32 **pp, int *buflen)
1839{
1840 struct svc_export *exp_ps;
1841 __be32 res;
1842
1843 exp_ps = rqst_find_fsidzero_export(rqstp);
1844 if (IS_ERR(exp_ps))
1845 return nfserrno(PTR_ERR(exp_ps));
1846 res = nfsd4_encode_path(&exp_ps->ex_path, path, pp, buflen);
1847 exp_put(exp_ps);
1848 return res;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001849}
1850
1851/*
1852 * encode a fs_locations structure
1853 */
Al Virob37ad282006-10-19 23:28:59 -07001854static __be32 nfsd4_encode_fs_locations(struct svc_rqst *rqstp,
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001855 struct svc_export *exp,
Al Viro2ebbc012006-10-19 23:28:58 -07001856 __be32 **pp, int *buflen)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001857{
Al Virob37ad282006-10-19 23:28:59 -07001858 __be32 status;
Al Virocc45f012006-10-19 23:28:44 -07001859 int i;
Al Viro2ebbc012006-10-19 23:28:58 -07001860 __be32 *p = *pp;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001861 struct nfsd4_fs_locations *fslocs = &exp->ex_fslocs;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001862
Trond Myklebusted748aa2011-09-12 19:37:06 -04001863 status = nfsd4_encode_fsloc_fsroot(rqstp, &exp->ex_path, &p, buflen);
J.Bruce Fields81c3f412006-10-04 02:16:19 -07001864 if (status)
1865 return status;
1866 if ((*buflen -= 4) < 0)
1867 return nfserr_resource;
1868 WRITE32(fslocs->locations_count);
1869 for (i=0; i<fslocs->locations_count; i++) {
1870 status = nfsd4_encode_fs_location4(&fslocs->locations[i],
1871 &p, buflen);
1872 if (status)
1873 return status;
1874 }
1875 *pp = p;
1876 return 0;
1877}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878
J. Bruce Fields3d2544b2011-08-15 11:49:30 -04001879static u32 nfs4_file_type(umode_t mode)
1880{
1881 switch (mode & S_IFMT) {
1882 case S_IFIFO: return NF4FIFO;
1883 case S_IFCHR: return NF4CHR;
1884 case S_IFDIR: return NF4DIR;
1885 case S_IFBLK: return NF4BLK;
1886 case S_IFLNK: return NF4LNK;
1887 case S_IFREG: return NF4REG;
1888 case S_IFSOCK: return NF4SOCK;
1889 default: return NF4BAD;
1890 };
1891}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892
Al Virob37ad282006-10-19 23:28:59 -07001893static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894nfsd4_encode_name(struct svc_rqst *rqstp, int whotype, uid_t id, int group,
Al Viro2ebbc012006-10-19 23:28:58 -07001895 __be32 **p, int *buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896{
1897 int status;
1898
1899 if (*buflen < (XDR_QUADLEN(IDMAP_NAMESZ) << 2) + 4)
1900 return nfserr_resource;
1901 if (whotype != NFS4_ACL_WHO_NAMED)
1902 status = nfs4_acl_write_who(whotype, (u8 *)(*p + 1));
1903 else if (group)
1904 status = nfsd_map_gid_to_name(rqstp, id, (u8 *)(*p + 1));
1905 else
1906 status = nfsd_map_uid_to_name(rqstp, id, (u8 *)(*p + 1));
1907 if (status < 0)
1908 return nfserrno(status);
1909 *p = xdr_encode_opaque(*p, NULL, status);
1910 *buflen -= (XDR_QUADLEN(status) << 2) + 4;
1911 BUG_ON(*buflen < 0);
1912 return 0;
1913}
1914
Al Virob37ad282006-10-19 23:28:59 -07001915static inline __be32
Al Viro2ebbc012006-10-19 23:28:58 -07001916nfsd4_encode_user(struct svc_rqst *rqstp, uid_t uid, __be32 **p, int *buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917{
1918 return nfsd4_encode_name(rqstp, NFS4_ACL_WHO_NAMED, uid, 0, p, buflen);
1919}
1920
Al Virob37ad282006-10-19 23:28:59 -07001921static inline __be32
Al Viro2ebbc012006-10-19 23:28:58 -07001922nfsd4_encode_group(struct svc_rqst *rqstp, uid_t gid, __be32 **p, int *buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923{
1924 return nfsd4_encode_name(rqstp, NFS4_ACL_WHO_NAMED, gid, 1, p, buflen);
1925}
1926
Al Virob37ad282006-10-19 23:28:59 -07001927static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928nfsd4_encode_aclname(struct svc_rqst *rqstp, int whotype, uid_t id, int group,
Al Viro2ebbc012006-10-19 23:28:58 -07001929 __be32 **p, int *buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930{
1931 return nfsd4_encode_name(rqstp, whotype, id, group, p, buflen);
1932}
1933
J.Bruce Fields42ca0992006-10-04 02:16:20 -07001934#define WORD0_ABSENT_FS_ATTRS (FATTR4_WORD0_FS_LOCATIONS | FATTR4_WORD0_FSID | \
1935 FATTR4_WORD0_RDATTR_ERROR)
1936#define WORD1_ABSENT_FS_ATTRS FATTR4_WORD1_MOUNTED_ON_FILEID
1937
Al Virob37ad282006-10-19 23:28:59 -07001938static __be32 fattr_handle_absent_fs(u32 *bmval0, u32 *bmval1, u32 *rdattr_err)
J.Bruce Fields42ca0992006-10-04 02:16:20 -07001939{
1940 /* As per referral draft: */
1941 if (*bmval0 & ~WORD0_ABSENT_FS_ATTRS ||
1942 *bmval1 & ~WORD1_ABSENT_FS_ATTRS) {
1943 if (*bmval0 & FATTR4_WORD0_RDATTR_ERROR ||
1944 *bmval0 & FATTR4_WORD0_FS_LOCATIONS)
1945 *rdattr_err = NFSERR_MOVED;
1946 else
1947 return nfserr_moved;
1948 }
1949 *bmval0 &= WORD0_ABSENT_FS_ATTRS;
1950 *bmval1 &= WORD1_ABSENT_FS_ATTRS;
1951 return 0;
1952}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953
1954/*
1955 * Note: @fhp can be NULL; in this case, we might have to compose the filehandle
1956 * ourselves.
1957 *
1958 * @countp is the buffer size in _words_; upon successful return this becomes
1959 * replaced with the number of words written.
1960 */
Al Virob37ad282006-10-19 23:28:59 -07001961__be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962nfsd4_encode_fattr(struct svc_fh *fhp, struct svc_export *exp,
Al Viro2ebbc012006-10-19 23:28:58 -07001963 struct dentry *dentry, __be32 *buffer, int *countp, u32 *bmval,
Frank Filz406a7ea2007-11-27 11:34:05 -08001964 struct svc_rqst *rqstp, int ignore_crossmnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965{
1966 u32 bmval0 = bmval[0];
1967 u32 bmval1 = bmval[1];
Andy Adamson7e705702009-04-03 08:29:11 +03001968 u32 bmval2 = bmval[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 struct kstat stat;
1970 struct svc_fh tempfh;
1971 struct kstatfs statfs;
1972 int buflen = *countp << 2;
Al Viro2ebbc012006-10-19 23:28:58 -07001973 __be32 *attrlenp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 u32 dummy;
1975 u64 dummy64;
J.Bruce Fields42ca0992006-10-04 02:16:20 -07001976 u32 rdattr_err = 0;
Al Viro2ebbc012006-10-19 23:28:58 -07001977 __be32 *p = buffer;
Al Virob37ad282006-10-19 23:28:59 -07001978 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07001979 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 int aclsupport = 0;
1981 struct nfs4_acl *acl = NULL;
Andy Adamson7e705702009-04-03 08:29:11 +03001982 struct nfsd4_compoundres *resp = rqstp->rq_resp;
1983 u32 minorversion = resp->cstate.minorversion;
Christoph Hellwigebabe9a2010-07-07 18:53:11 +02001984 struct path path = {
1985 .mnt = exp->ex_path.mnt,
1986 .dentry = dentry,
1987 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988
1989 BUG_ON(bmval1 & NFSD_WRITEONLY_ATTRS_WORD1);
Andy Adamson7e705702009-04-03 08:29:11 +03001990 BUG_ON(bmval0 & ~nfsd_suppattrs0(minorversion));
1991 BUG_ON(bmval1 & ~nfsd_suppattrs1(minorversion));
1992 BUG_ON(bmval2 & ~nfsd_suppattrs2(minorversion));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993
J.Bruce Fields42ca0992006-10-04 02:16:20 -07001994 if (exp->ex_fslocs.migrated) {
Andy Adamson7e705702009-04-03 08:29:11 +03001995 BUG_ON(bmval[2]);
J.Bruce Fields42ca0992006-10-04 02:16:20 -07001996 status = fattr_handle_absent_fs(&bmval0, &bmval1, &rdattr_err);
1997 if (status)
1998 goto out;
1999 }
2000
Jan Blunck54775492008-02-14 19:38:39 -08002001 err = vfs_getattr(exp->ex_path.mnt, dentry, &stat);
Al Virob8dd7b92006-10-19 23:29:01 -07002002 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 goto out_nfserr;
J. Bruce Fieldsa16e92e2007-09-28 16:45:51 -04002004 if ((bmval0 & (FATTR4_WORD0_FILES_FREE | FATTR4_WORD0_FILES_TOTAL |
2005 FATTR4_WORD0_MAXNAME)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 (bmval1 & (FATTR4_WORD1_SPACE_AVAIL | FATTR4_WORD1_SPACE_FREE |
2007 FATTR4_WORD1_SPACE_TOTAL))) {
Christoph Hellwigebabe9a2010-07-07 18:53:11 +02002008 err = vfs_statfs(&path, &statfs);
Al Virob8dd7b92006-10-19 23:29:01 -07002009 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 goto out_nfserr;
2011 }
2012 if ((bmval0 & (FATTR4_WORD0_FILEHANDLE | FATTR4_WORD0_FSID)) && !fhp) {
2013 fh_init(&tempfh, NFS4_FHSIZE);
2014 status = fh_compose(&tempfh, exp, dentry, NULL);
2015 if (status)
2016 goto out;
2017 fhp = &tempfh;
2018 }
2019 if (bmval0 & (FATTR4_WORD0_ACL | FATTR4_WORD0_ACLSUPPORT
2020 | FATTR4_WORD0_SUPPORTED_ATTRS)) {
Al Virob8dd7b92006-10-19 23:29:01 -07002021 err = nfsd4_get_nfs4_acl(rqstp, dentry, &acl);
2022 aclsupport = (err == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 if (bmval0 & FATTR4_WORD0_ACL) {
Al Virob8dd7b92006-10-19 23:29:01 -07002024 if (err == -EOPNOTSUPP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 bmval0 &= ~FATTR4_WORD0_ACL;
Al Virob8dd7b92006-10-19 23:29:01 -07002026 else if (err == -EINVAL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 status = nfserr_attrnotsupp;
2028 goto out;
Al Virob8dd7b92006-10-19 23:29:01 -07002029 } else if (err != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 goto out_nfserr;
2031 }
2032 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033
Benny Halevy2b44f1b2010-09-30 20:47:46 +02002034 if (bmval2) {
2035 if ((buflen -= 16) < 0)
2036 goto out_resource;
Andy Adamson7e705702009-04-03 08:29:11 +03002037 WRITE32(3);
2038 WRITE32(bmval0);
2039 WRITE32(bmval1);
2040 WRITE32(bmval2);
Benny Halevy2b44f1b2010-09-30 20:47:46 +02002041 } else if (bmval1) {
2042 if ((buflen -= 12) < 0)
2043 goto out_resource;
Andy Adamson7e705702009-04-03 08:29:11 +03002044 WRITE32(2);
2045 WRITE32(bmval0);
2046 WRITE32(bmval1);
2047 } else {
Benny Halevy2b44f1b2010-09-30 20:47:46 +02002048 if ((buflen -= 8) < 0)
2049 goto out_resource;
Andy Adamson7e705702009-04-03 08:29:11 +03002050 WRITE32(1);
2051 WRITE32(bmval0);
2052 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 attrlenp = p++; /* to be backfilled later */
2054
2055 if (bmval0 & FATTR4_WORD0_SUPPORTED_ATTRS) {
Andy Adamson7e705702009-04-03 08:29:11 +03002056 u32 word0 = nfsd_suppattrs0(minorversion);
2057 u32 word1 = nfsd_suppattrs1(minorversion);
2058 u32 word2 = nfsd_suppattrs2(minorversion);
2059
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002060 if (!aclsupport)
2061 word0 &= ~FATTR4_WORD0_ACL;
Andy Adamson7e705702009-04-03 08:29:11 +03002062 if (!word2) {
Benny Halevy2b44f1b2010-09-30 20:47:46 +02002063 if ((buflen -= 12) < 0)
2064 goto out_resource;
Andy Adamson7e705702009-04-03 08:29:11 +03002065 WRITE32(2);
2066 WRITE32(word0);
2067 WRITE32(word1);
2068 } else {
Benny Halevy2b44f1b2010-09-30 20:47:46 +02002069 if ((buflen -= 16) < 0)
2070 goto out_resource;
Andy Adamson7e705702009-04-03 08:29:11 +03002071 WRITE32(3);
2072 WRITE32(word0);
2073 WRITE32(word1);
2074 WRITE32(word2);
2075 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 }
2077 if (bmval0 & FATTR4_WORD0_TYPE) {
2078 if ((buflen -= 4) < 0)
2079 goto out_resource;
J. Bruce Fields3d2544b2011-08-15 11:49:30 -04002080 dummy = nfs4_file_type(stat.mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 if (dummy == NF4BAD)
2082 goto out_serverfault;
2083 WRITE32(dummy);
2084 }
2085 if (bmval0 & FATTR4_WORD0_FH_EXPIRE_TYPE) {
2086 if ((buflen -= 4) < 0)
2087 goto out_resource;
NeilBrown49640002005-06-23 22:02:58 -07002088 if (exp->ex_flags & NFSEXP_NOSUBTREECHECK)
NeilBrowne34ac862005-07-07 17:59:30 -07002089 WRITE32(NFS4_FH_PERSISTENT);
NeilBrown49640002005-06-23 22:02:58 -07002090 else
NeilBrowne34ac862005-07-07 17:59:30 -07002091 WRITE32(NFS4_FH_PERSISTENT|NFS4_FH_VOL_RENAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 }
2093 if (bmval0 & FATTR4_WORD0_CHANGE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 if ((buflen -= 8) < 0)
2095 goto out_resource;
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002096 write_change(&p, &stat, dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 }
2098 if (bmval0 & FATTR4_WORD0_SIZE) {
2099 if ((buflen -= 8) < 0)
2100 goto out_resource;
2101 WRITE64(stat.size);
2102 }
2103 if (bmval0 & FATTR4_WORD0_LINK_SUPPORT) {
2104 if ((buflen -= 4) < 0)
2105 goto out_resource;
2106 WRITE32(1);
2107 }
2108 if (bmval0 & FATTR4_WORD0_SYMLINK_SUPPORT) {
2109 if ((buflen -= 4) < 0)
2110 goto out_resource;
2111 WRITE32(1);
2112 }
2113 if (bmval0 & FATTR4_WORD0_NAMED_ATTR) {
2114 if ((buflen -= 4) < 0)
2115 goto out_resource;
2116 WRITE32(0);
2117 }
2118 if (bmval0 & FATTR4_WORD0_FSID) {
2119 if ((buflen -= 16) < 0)
2120 goto out_resource;
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002121 if (exp->ex_fslocs.migrated) {
2122 WRITE64(NFS4_REFERRAL_FSID_MAJOR);
2123 WRITE64(NFS4_REFERRAL_FSID_MINOR);
NeilBrownaf6a4e22007-02-14 00:33:12 -08002124 } else switch(fsid_source(fhp)) {
2125 case FSIDSOURCE_FSID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 WRITE64((u64)exp->ex_fsid);
2127 WRITE64((u64)0);
NeilBrownaf6a4e22007-02-14 00:33:12 -08002128 break;
2129 case FSIDSOURCE_DEV:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 WRITE32(0);
2131 WRITE32(MAJOR(stat.dev));
2132 WRITE32(0);
2133 WRITE32(MINOR(stat.dev));
NeilBrownaf6a4e22007-02-14 00:33:12 -08002134 break;
2135 case FSIDSOURCE_UUID:
2136 WRITEMEM(exp->ex_uuid, 16);
2137 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 }
2139 }
2140 if (bmval0 & FATTR4_WORD0_UNIQUE_HANDLES) {
2141 if ((buflen -= 4) < 0)
2142 goto out_resource;
2143 WRITE32(0);
2144 }
2145 if (bmval0 & FATTR4_WORD0_LEASE_TIME) {
2146 if ((buflen -= 4) < 0)
2147 goto out_resource;
J. Bruce Fieldscf07d2e2010-02-28 23:20:19 -05002148 WRITE32(nfsd4_lease);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 }
2150 if (bmval0 & FATTR4_WORD0_RDATTR_ERROR) {
2151 if ((buflen -= 4) < 0)
2152 goto out_resource;
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002153 WRITE32(rdattr_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 }
2155 if (bmval0 & FATTR4_WORD0_ACL) {
2156 struct nfs4_ace *ace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157
2158 if (acl == NULL) {
2159 if ((buflen -= 4) < 0)
2160 goto out_resource;
2161
2162 WRITE32(0);
2163 goto out_acl;
2164 }
2165 if ((buflen -= 4) < 0)
2166 goto out_resource;
2167 WRITE32(acl->naces);
2168
J. Bruce Fields28e05dd2007-02-16 01:28:30 -08002169 for (ace = acl->aces; ace < acl->aces + acl->naces; ace++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 if ((buflen -= 4*3) < 0)
2171 goto out_resource;
2172 WRITE32(ace->type);
2173 WRITE32(ace->flag);
2174 WRITE32(ace->access_mask & NFS4_ACE_MASK_ALL);
2175 status = nfsd4_encode_aclname(rqstp, ace->whotype,
2176 ace->who, ace->flag & NFS4_ACE_IDENTIFIER_GROUP,
2177 &p, &buflen);
2178 if (status == nfserr_resource)
2179 goto out_resource;
2180 if (status)
2181 goto out;
2182 }
2183 }
2184out_acl:
2185 if (bmval0 & FATTR4_WORD0_ACLSUPPORT) {
2186 if ((buflen -= 4) < 0)
2187 goto out_resource;
2188 WRITE32(aclsupport ?
2189 ACL4_SUPPORT_ALLOW_ACL|ACL4_SUPPORT_DENY_ACL : 0);
2190 }
2191 if (bmval0 & FATTR4_WORD0_CANSETTIME) {
2192 if ((buflen -= 4) < 0)
2193 goto out_resource;
2194 WRITE32(1);
2195 }
2196 if (bmval0 & FATTR4_WORD0_CASE_INSENSITIVE) {
2197 if ((buflen -= 4) < 0)
2198 goto out_resource;
2199 WRITE32(1);
2200 }
2201 if (bmval0 & FATTR4_WORD0_CASE_PRESERVING) {
2202 if ((buflen -= 4) < 0)
2203 goto out_resource;
2204 WRITE32(1);
2205 }
2206 if (bmval0 & FATTR4_WORD0_CHOWN_RESTRICTED) {
2207 if ((buflen -= 4) < 0)
2208 goto out_resource;
2209 WRITE32(1);
2210 }
2211 if (bmval0 & FATTR4_WORD0_FILEHANDLE) {
2212 buflen -= (XDR_QUADLEN(fhp->fh_handle.fh_size) << 2) + 4;
2213 if (buflen < 0)
2214 goto out_resource;
2215 WRITE32(fhp->fh_handle.fh_size);
2216 WRITEMEM(&fhp->fh_handle.fh_base, fhp->fh_handle.fh_size);
2217 }
2218 if (bmval0 & FATTR4_WORD0_FILEID) {
2219 if ((buflen -= 8) < 0)
2220 goto out_resource;
Peter Staubach40ee5dc2007-08-16 12:10:07 -04002221 WRITE64(stat.ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 }
2223 if (bmval0 & FATTR4_WORD0_FILES_AVAIL) {
2224 if ((buflen -= 8) < 0)
2225 goto out_resource;
2226 WRITE64((u64) statfs.f_ffree);
2227 }
2228 if (bmval0 & FATTR4_WORD0_FILES_FREE) {
2229 if ((buflen -= 8) < 0)
2230 goto out_resource;
2231 WRITE64((u64) statfs.f_ffree);
2232 }
2233 if (bmval0 & FATTR4_WORD0_FILES_TOTAL) {
2234 if ((buflen -= 8) < 0)
2235 goto out_resource;
2236 WRITE64((u64) statfs.f_files);
2237 }
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002238 if (bmval0 & FATTR4_WORD0_FS_LOCATIONS) {
2239 status = nfsd4_encode_fs_locations(rqstp, exp, &p, &buflen);
2240 if (status == nfserr_resource)
2241 goto out_resource;
2242 if (status)
2243 goto out;
2244 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 if (bmval0 & FATTR4_WORD0_HOMOGENEOUS) {
2246 if ((buflen -= 4) < 0)
2247 goto out_resource;
2248 WRITE32(1);
2249 }
2250 if (bmval0 & FATTR4_WORD0_MAXFILESIZE) {
2251 if ((buflen -= 8) < 0)
2252 goto out_resource;
2253 WRITE64(~(u64)0);
2254 }
2255 if (bmval0 & FATTR4_WORD0_MAXLINK) {
2256 if ((buflen -= 4) < 0)
2257 goto out_resource;
2258 WRITE32(255);
2259 }
2260 if (bmval0 & FATTR4_WORD0_MAXNAME) {
2261 if ((buflen -= 4) < 0)
2262 goto out_resource;
J. Bruce Fieldsa16e92e2007-09-28 16:45:51 -04002263 WRITE32(statfs.f_namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 }
2265 if (bmval0 & FATTR4_WORD0_MAXREAD) {
2266 if ((buflen -= 8) < 0)
2267 goto out_resource;
Greg Banks7adae482006-10-04 02:15:47 -07002268 WRITE64((u64) svc_max_payload(rqstp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 }
2270 if (bmval0 & FATTR4_WORD0_MAXWRITE) {
2271 if ((buflen -= 8) < 0)
2272 goto out_resource;
Greg Banks7adae482006-10-04 02:15:47 -07002273 WRITE64((u64) svc_max_payload(rqstp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 }
2275 if (bmval1 & FATTR4_WORD1_MODE) {
2276 if ((buflen -= 4) < 0)
2277 goto out_resource;
2278 WRITE32(stat.mode & S_IALLUGO);
2279 }
2280 if (bmval1 & FATTR4_WORD1_NO_TRUNC) {
2281 if ((buflen -= 4) < 0)
2282 goto out_resource;
2283 WRITE32(1);
2284 }
2285 if (bmval1 & FATTR4_WORD1_NUMLINKS) {
2286 if ((buflen -= 4) < 0)
2287 goto out_resource;
2288 WRITE32(stat.nlink);
2289 }
2290 if (bmval1 & FATTR4_WORD1_OWNER) {
2291 status = nfsd4_encode_user(rqstp, stat.uid, &p, &buflen);
2292 if (status == nfserr_resource)
2293 goto out_resource;
2294 if (status)
2295 goto out;
2296 }
2297 if (bmval1 & FATTR4_WORD1_OWNER_GROUP) {
2298 status = nfsd4_encode_group(rqstp, stat.gid, &p, &buflen);
2299 if (status == nfserr_resource)
2300 goto out_resource;
2301 if (status)
2302 goto out;
2303 }
2304 if (bmval1 & FATTR4_WORD1_RAWDEV) {
2305 if ((buflen -= 8) < 0)
2306 goto out_resource;
2307 WRITE32((u32) MAJOR(stat.rdev));
2308 WRITE32((u32) MINOR(stat.rdev));
2309 }
2310 if (bmval1 & FATTR4_WORD1_SPACE_AVAIL) {
2311 if ((buflen -= 8) < 0)
2312 goto out_resource;
2313 dummy64 = (u64)statfs.f_bavail * (u64)statfs.f_bsize;
2314 WRITE64(dummy64);
2315 }
2316 if (bmval1 & FATTR4_WORD1_SPACE_FREE) {
2317 if ((buflen -= 8) < 0)
2318 goto out_resource;
2319 dummy64 = (u64)statfs.f_bfree * (u64)statfs.f_bsize;
2320 WRITE64(dummy64);
2321 }
2322 if (bmval1 & FATTR4_WORD1_SPACE_TOTAL) {
2323 if ((buflen -= 8) < 0)
2324 goto out_resource;
2325 dummy64 = (u64)statfs.f_blocks * (u64)statfs.f_bsize;
2326 WRITE64(dummy64);
2327 }
2328 if (bmval1 & FATTR4_WORD1_SPACE_USED) {
2329 if ((buflen -= 8) < 0)
2330 goto out_resource;
2331 dummy64 = (u64)stat.blocks << 9;
2332 WRITE64(dummy64);
2333 }
2334 if (bmval1 & FATTR4_WORD1_TIME_ACCESS) {
2335 if ((buflen -= 12) < 0)
2336 goto out_resource;
2337 WRITE32(0);
2338 WRITE32(stat.atime.tv_sec);
2339 WRITE32(stat.atime.tv_nsec);
2340 }
2341 if (bmval1 & FATTR4_WORD1_TIME_DELTA) {
2342 if ((buflen -= 12) < 0)
2343 goto out_resource;
2344 WRITE32(0);
2345 WRITE32(1);
2346 WRITE32(0);
2347 }
2348 if (bmval1 & FATTR4_WORD1_TIME_METADATA) {
2349 if ((buflen -= 12) < 0)
2350 goto out_resource;
2351 WRITE32(0);
2352 WRITE32(stat.ctime.tv_sec);
2353 WRITE32(stat.ctime.tv_nsec);
2354 }
2355 if (bmval1 & FATTR4_WORD1_TIME_MODIFY) {
2356 if ((buflen -= 12) < 0)
2357 goto out_resource;
2358 WRITE32(0);
2359 WRITE32(stat.mtime.tv_sec);
2360 WRITE32(stat.mtime.tv_nsec);
2361 }
2362 if (bmval1 & FATTR4_WORD1_MOUNTED_ON_FILEID) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 if ((buflen -= 8) < 0)
2364 goto out_resource;
Frank Filz406a7ea2007-11-27 11:34:05 -08002365 /*
2366 * Get parent's attributes if not ignoring crossmount
2367 * and this is the root of a cross-mounted filesystem.
2368 */
2369 if (ignore_crossmnt == 0 &&
Al Viro462d6052010-01-30 16:11:21 -05002370 dentry == exp->ex_path.mnt->mnt_root) {
2371 struct path path = exp->ex_path;
2372 path_get(&path);
2373 while (follow_up(&path)) {
2374 if (path.dentry != path.mnt->mnt_root)
2375 break;
2376 }
2377 err = vfs_getattr(path.mnt, path.dentry, &stat);
2378 path_put(&path);
Peter Staubach40ee5dc2007-08-16 12:10:07 -04002379 if (err)
2380 goto out_nfserr;
2381 }
2382 WRITE64(stat.ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 }
Benny Halevy8c18f202009-04-03 08:29:14 +03002384 if (bmval2 & FATTR4_WORD2_SUPPATTR_EXCLCREAT) {
2385 WRITE32(3);
2386 WRITE32(NFSD_SUPPATTR_EXCLCREAT_WORD0);
2387 WRITE32(NFSD_SUPPATTR_EXCLCREAT_WORD1);
2388 WRITE32(NFSD_SUPPATTR_EXCLCREAT_WORD2);
2389 }
Andy Adamson7e705702009-04-03 08:29:11 +03002390
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 *attrlenp = htonl((char *)p - (char *)attrlenp - 4);
2392 *countp = p - buffer;
2393 status = nfs_ok;
2394
2395out:
J. Bruce Fields28e05dd2007-02-16 01:28:30 -08002396 kfree(acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 if (fhp == &tempfh)
2398 fh_put(&tempfh);
2399 return status;
2400out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07002401 status = nfserrno(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 goto out;
2403out_resource:
2404 *countp = 0;
2405 status = nfserr_resource;
2406 goto out;
2407out_serverfault:
2408 status = nfserr_serverfault;
2409 goto out;
2410}
2411
J. Bruce Fieldsc0ce6ec2008-02-11 15:48:47 -05002412static inline int attributes_need_mount(u32 *bmval)
2413{
2414 if (bmval[0] & ~(FATTR4_WORD0_RDATTR_ERROR | FATTR4_WORD0_LEASE_TIME))
2415 return 1;
2416 if (bmval[1] & ~FATTR4_WORD1_MOUNTED_ON_FILEID)
2417 return 1;
2418 return 0;
2419}
2420
Al Virob37ad282006-10-19 23:28:59 -07002421static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422nfsd4_encode_dirent_fattr(struct nfsd4_readdir *cd,
Al Viro2ebbc012006-10-19 23:28:58 -07002423 const char *name, int namlen, __be32 *p, int *buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424{
2425 struct svc_export *exp = cd->rd_fhp->fh_export;
2426 struct dentry *dentry;
Al Virob37ad282006-10-19 23:28:59 -07002427 __be32 nfserr;
Frank Filz406a7ea2007-11-27 11:34:05 -08002428 int ignore_crossmnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429
2430 dentry = lookup_one_len(name, cd->rd_fhp->fh_dentry, namlen);
2431 if (IS_ERR(dentry))
2432 return nfserrno(PTR_ERR(dentry));
J. Bruce Fieldsb2c0cea2009-05-05 19:04:29 -04002433 if (!dentry->d_inode) {
2434 /*
2435 * nfsd_buffered_readdir drops the i_mutex between
2436 * readdir and calling this callback, leaving a window
2437 * where this directory entry could have gone away.
2438 */
2439 dput(dentry);
2440 return nfserr_noent;
2441 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442
2443 exp_get(exp);
Frank Filz406a7ea2007-11-27 11:34:05 -08002444 /*
2445 * In the case of a mountpoint, the client may be asking for
2446 * attributes that are only properties of the underlying filesystem
2447 * as opposed to the cross-mounted file system. In such a case,
2448 * we will not follow the cross mount and will fill the attribtutes
2449 * directly from the mountpoint dentry.
2450 */
J. Bruce Fields3227fa42009-10-25 21:43:01 -04002451 if (nfsd_mountpoint(dentry, exp)) {
J.Bruce Fields021d3a72006-12-13 00:35:24 -08002452 int err;
2453
J. Bruce Fields3227fa42009-10-25 21:43:01 -04002454 if (!(exp->ex_flags & NFSEXP_V4ROOT)
2455 && !attributes_need_mount(cd->rd_bmval)) {
2456 ignore_crossmnt = 1;
2457 goto out_encode;
2458 }
Andy Adamsondcb488a32007-07-17 04:04:51 -07002459 /*
2460 * Why the heck aren't we just using nfsd_lookup??
2461 * Different "."/".." handling? Something else?
2462 * At least, add a comment here to explain....
2463 */
J.Bruce Fields021d3a72006-12-13 00:35:24 -08002464 err = nfsd_cross_mnt(cd->rd_rqstp, &dentry, &exp);
2465 if (err) {
2466 nfserr = nfserrno(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 goto out_put;
2468 }
Andy Adamsondcb488a32007-07-17 04:04:51 -07002469 nfserr = check_nfsd_access(exp, cd->rd_rqstp);
2470 if (nfserr)
2471 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472
2473 }
J. Bruce Fields3227fa42009-10-25 21:43:01 -04002474out_encode:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 nfserr = nfsd4_encode_fattr(NULL, exp, dentry, p, buflen, cd->rd_bmval,
Frank Filz406a7ea2007-11-27 11:34:05 -08002476 cd->rd_rqstp, ignore_crossmnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477out_put:
2478 dput(dentry);
2479 exp_put(exp);
2480 return nfserr;
2481}
2482
Al Viro2ebbc012006-10-19 23:28:58 -07002483static __be32 *
Al Virob37ad282006-10-19 23:28:59 -07002484nfsd4_encode_rdattr_error(__be32 *p, int buflen, __be32 nfserr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485{
Al Viro2ebbc012006-10-19 23:28:58 -07002486 __be32 *attrlenp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487
2488 if (buflen < 6)
2489 return NULL;
2490 *p++ = htonl(2);
2491 *p++ = htonl(FATTR4_WORD0_RDATTR_ERROR); /* bmval0 */
2492 *p++ = htonl(0); /* bmval1 */
2493
2494 attrlenp = p++;
2495 *p++ = nfserr; /* no htonl */
2496 *attrlenp = htonl((char *)p - (char *)attrlenp - 4);
2497 return p;
2498}
2499
2500static int
NeilBrowna0ad13e2007-01-26 00:57:10 -08002501nfsd4_encode_dirent(void *ccdv, const char *name, int namlen,
2502 loff_t offset, u64 ino, unsigned int d_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503{
NeilBrowna0ad13e2007-01-26 00:57:10 -08002504 struct readdir_cd *ccd = ccdv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 struct nfsd4_readdir *cd = container_of(ccd, struct nfsd4_readdir, common);
2506 int buflen;
Al Viro2ebbc012006-10-19 23:28:58 -07002507 __be32 *p = cd->buffer;
J. Bruce Fieldsb2c0cea2009-05-05 19:04:29 -04002508 __be32 *cookiep;
Al Virob37ad282006-10-19 23:28:59 -07002509 __be32 nfserr = nfserr_toosmall;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510
2511 /* In nfsv4, "." and ".." never make it onto the wire.. */
2512 if (name && isdotent(name, namlen)) {
2513 cd->common.err = nfs_ok;
2514 return 0;
2515 }
2516
2517 if (cd->offset)
2518 xdr_encode_hyper(cd->offset, (u64) offset);
2519
2520 buflen = cd->buflen - 4 - XDR_QUADLEN(namlen);
2521 if (buflen < 0)
2522 goto fail;
2523
2524 *p++ = xdr_one; /* mark entry present */
J. Bruce Fieldsb2c0cea2009-05-05 19:04:29 -04002525 cookiep = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 p = xdr_encode_hyper(p, NFS_OFFSET_MAX); /* offset of next entry */
2527 p = xdr_encode_array(p, name, namlen); /* name length & name */
2528
2529 nfserr = nfsd4_encode_dirent_fattr(cd, name, namlen, p, &buflen);
2530 switch (nfserr) {
2531 case nfs_ok:
2532 p += buflen;
2533 break;
2534 case nfserr_resource:
2535 nfserr = nfserr_toosmall;
2536 goto fail;
J. Bruce Fieldsb2c0cea2009-05-05 19:04:29 -04002537 case nfserr_noent:
2538 goto skip_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 default:
2540 /*
2541 * If the client requested the RDATTR_ERROR attribute,
2542 * we stuff the error code into this attribute
2543 * and continue. If this attribute was not requested,
2544 * then in accordance with the spec, we fail the
2545 * entire READDIR operation(!)
2546 */
2547 if (!(cd->rd_bmval[0] & FATTR4_WORD0_RDATTR_ERROR))
2548 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 p = nfsd4_encode_rdattr_error(p, buflen, nfserr);
Fred Isaman34081ef2006-01-18 17:43:40 -08002550 if (p == NULL) {
2551 nfserr = nfserr_toosmall;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 goto fail;
Fred Isaman34081ef2006-01-18 17:43:40 -08002553 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 }
2555 cd->buflen -= (p - cd->buffer);
2556 cd->buffer = p;
J. Bruce Fieldsb2c0cea2009-05-05 19:04:29 -04002557 cd->offset = cookiep;
2558skip_entry:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 cd->common.err = nfs_ok;
2560 return 0;
2561fail:
2562 cd->common.err = nfserr;
2563 return -EINVAL;
2564}
2565
Benny Halevye2f282b2008-08-12 20:45:07 +03002566static void
2567nfsd4_encode_stateid(struct nfsd4_compoundres *resp, stateid_t *sid)
2568{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002569 __be32 *p;
Benny Halevye2f282b2008-08-12 20:45:07 +03002570
2571 RESERVE_SPACE(sizeof(stateid_t));
2572 WRITE32(sid->si_generation);
2573 WRITEMEM(&sid->si_opaque, sizeof(stateid_opaque_t));
2574 ADJUST_ARGS();
2575}
2576
Benny Halevy695e12f2008-07-04 14:38:33 +03002577static __be32
Al Virob37ad282006-10-19 23:28:59 -07002578nfsd4_encode_access(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_access *access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002580 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581
2582 if (!nfserr) {
2583 RESERVE_SPACE(8);
2584 WRITE32(access->ac_supported);
2585 WRITE32(access->ac_resp_access);
2586 ADJUST_ARGS();
2587 }
Benny Halevy695e12f2008-07-04 14:38:33 +03002588 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589}
2590
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002591static __be32 nfsd4_encode_bind_conn_to_session(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_bind_conn_to_session *bcts)
2592{
2593 __be32 *p;
2594
2595 if (!nfserr) {
2596 RESERVE_SPACE(NFS4_MAX_SESSIONID_LEN + 8);
2597 WRITEMEM(bcts->sessionid.data, NFS4_MAX_SESSIONID_LEN);
2598 WRITE32(bcts->dir);
2599 /* XXX: ? */
2600 WRITE32(0);
2601 ADJUST_ARGS();
2602 }
2603 return nfserr;
2604}
2605
Benny Halevy695e12f2008-07-04 14:38:33 +03002606static __be32
Al Virob37ad282006-10-19 23:28:59 -07002607nfsd4_encode_close(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608{
2609 ENCODE_SEQID_OP_HEAD;
2610
Benny Halevye2f282b2008-08-12 20:45:07 +03002611 if (!nfserr)
2612 nfsd4_encode_stateid(resp, &close->cl_stateid);
2613
J. Bruce Fieldsf3e42232011-08-24 12:27:31 -04002614 encode_seqid_op_tail(resp, save, nfserr);
Benny Halevy695e12f2008-07-04 14:38:33 +03002615 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616}
2617
2618
Benny Halevy695e12f2008-07-04 14:38:33 +03002619static __be32
Al Virob37ad282006-10-19 23:28:59 -07002620nfsd4_encode_commit(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_commit *commit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002622 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623
2624 if (!nfserr) {
2625 RESERVE_SPACE(8);
2626 WRITEMEM(commit->co_verf.data, 8);
2627 ADJUST_ARGS();
2628 }
Benny Halevy695e12f2008-07-04 14:38:33 +03002629 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630}
2631
Benny Halevy695e12f2008-07-04 14:38:33 +03002632static __be32
Al Virob37ad282006-10-19 23:28:59 -07002633nfsd4_encode_create(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_create *create)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002635 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636
2637 if (!nfserr) {
2638 RESERVE_SPACE(32);
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002639 write_cinfo(&p, &create->cr_cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 WRITE32(2);
2641 WRITE32(create->cr_bmval[0]);
2642 WRITE32(create->cr_bmval[1]);
2643 ADJUST_ARGS();
2644 }
Benny Halevy695e12f2008-07-04 14:38:33 +03002645 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646}
2647
Al Virob37ad282006-10-19 23:28:59 -07002648static __be32
2649nfsd4_encode_getattr(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_getattr *getattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650{
2651 struct svc_fh *fhp = getattr->ga_fhp;
2652 int buflen;
2653
2654 if (nfserr)
2655 return nfserr;
2656
2657 buflen = resp->end - resp->p - (COMPOUND_ERR_SLACK_SPACE >> 2);
2658 nfserr = nfsd4_encode_fattr(fhp, fhp->fh_export, fhp->fh_dentry,
2659 resp->p, &buflen, getattr->ga_bmval,
Frank Filz406a7ea2007-11-27 11:34:05 -08002660 resp->rqstp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 if (!nfserr)
2662 resp->p += buflen;
2663 return nfserr;
2664}
2665
Benny Halevy695e12f2008-07-04 14:38:33 +03002666static __be32
2667nfsd4_encode_getfh(struct nfsd4_compoundres *resp, __be32 nfserr, struct svc_fh **fhpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668{
Benny Halevy695e12f2008-07-04 14:38:33 +03002669 struct svc_fh *fhp = *fhpp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 unsigned int len;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002671 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672
2673 if (!nfserr) {
2674 len = fhp->fh_handle.fh_size;
2675 RESERVE_SPACE(len + 4);
2676 WRITE32(len);
2677 WRITEMEM(&fhp->fh_handle.fh_base, len);
2678 ADJUST_ARGS();
2679 }
Benny Halevy695e12f2008-07-04 14:38:33 +03002680 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681}
2682
2683/*
2684* Including all fields other than the name, a LOCK4denied structure requires
2685* 8(clientid) + 4(namelen) + 8(offset) + 8(length) + 4(type) = 32 bytes.
2686*/
2687static void
2688nfsd4_encode_lock_denied(struct nfsd4_compoundres *resp, struct nfsd4_lock_denied *ld)
2689{
J. Bruce Fields7c13f342011-08-30 22:15:47 -04002690 struct xdr_netobj *conf = &ld->ld_owner;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002691 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692
J. Bruce Fields7c13f342011-08-30 22:15:47 -04002693 RESERVE_SPACE(32 + XDR_LEN(conf->len));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 WRITE64(ld->ld_start);
2695 WRITE64(ld->ld_length);
2696 WRITE32(ld->ld_type);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04002697 if (conf->len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 WRITEMEM(&ld->ld_clientid, 8);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04002699 WRITE32(conf->len);
2700 WRITEMEM(conf->data, conf->len);
2701 kfree(conf->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 } else { /* non - nfsv4 lock in conflict, no clientid nor owner */
2703 WRITE64((u64)0); /* clientid */
2704 WRITE32(0); /* length of owner name */
2705 }
2706 ADJUST_ARGS();
2707}
2708
Benny Halevy695e12f2008-07-04 14:38:33 +03002709static __be32
Al Virob37ad282006-10-19 23:28:59 -07002710nfsd4_encode_lock(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 ENCODE_SEQID_OP_HEAD;
2713
Benny Halevye2f282b2008-08-12 20:45:07 +03002714 if (!nfserr)
2715 nfsd4_encode_stateid(resp, &lock->lk_resp_stateid);
2716 else if (nfserr == nfserr_denied)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 nfsd4_encode_lock_denied(resp, &lock->lk_denied);
2718
J. Bruce Fieldsf3e42232011-08-24 12:27:31 -04002719 encode_seqid_op_tail(resp, save, nfserr);
Benny Halevy695e12f2008-07-04 14:38:33 +03002720 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721}
2722
Benny Halevy695e12f2008-07-04 14:38:33 +03002723static __be32
Al Virob37ad282006-10-19 23:28:59 -07002724nfsd4_encode_lockt(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725{
2726 if (nfserr == nfserr_denied)
2727 nfsd4_encode_lock_denied(resp, &lockt->lt_denied);
Benny Halevy695e12f2008-07-04 14:38:33 +03002728 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729}
2730
Benny Halevy695e12f2008-07-04 14:38:33 +03002731static __be32
Al Virob37ad282006-10-19 23:28:59 -07002732nfsd4_encode_locku(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733{
2734 ENCODE_SEQID_OP_HEAD;
2735
Benny Halevye2f282b2008-08-12 20:45:07 +03002736 if (!nfserr)
2737 nfsd4_encode_stateid(resp, &locku->lu_stateid);
2738
J. Bruce Fieldsf3e42232011-08-24 12:27:31 -04002739 encode_seqid_op_tail(resp, save, nfserr);
Benny Halevy695e12f2008-07-04 14:38:33 +03002740 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741}
2742
2743
Benny Halevy695e12f2008-07-04 14:38:33 +03002744static __be32
Al Virob37ad282006-10-19 23:28:59 -07002745nfsd4_encode_link(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_link *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002747 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748
2749 if (!nfserr) {
2750 RESERVE_SPACE(20);
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002751 write_cinfo(&p, &link->li_cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 ADJUST_ARGS();
2753 }
Benny Halevy695e12f2008-07-04 14:38:33 +03002754 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755}
2756
2757
Benny Halevy695e12f2008-07-04 14:38:33 +03002758static __be32
Al Virob37ad282006-10-19 23:28:59 -07002759nfsd4_encode_open(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002761 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 ENCODE_SEQID_OP_HEAD;
2763
2764 if (nfserr)
2765 goto out;
2766
Benny Halevye2f282b2008-08-12 20:45:07 +03002767 nfsd4_encode_stateid(resp, &open->op_stateid);
2768 RESERVE_SPACE(40);
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002769 write_cinfo(&p, &open->op_cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 WRITE32(open->op_rflags);
2771 WRITE32(2);
2772 WRITE32(open->op_bmval[0]);
2773 WRITE32(open->op_bmval[1]);
2774 WRITE32(open->op_delegate_type);
2775 ADJUST_ARGS();
2776
2777 switch (open->op_delegate_type) {
2778 case NFS4_OPEN_DELEGATE_NONE:
2779 break;
2780 case NFS4_OPEN_DELEGATE_READ:
Benny Halevye2f282b2008-08-12 20:45:07 +03002781 nfsd4_encode_stateid(resp, &open->op_delegate_stateid);
2782 RESERVE_SPACE(20);
NeilBrown7b190fe2005-06-23 22:03:23 -07002783 WRITE32(open->op_recall);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784
2785 /*
2786 * TODO: ACE's in delegations
2787 */
2788 WRITE32(NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
2789 WRITE32(0);
2790 WRITE32(0);
2791 WRITE32(0); /* XXX: is NULL principal ok? */
2792 ADJUST_ARGS();
2793 break;
2794 case NFS4_OPEN_DELEGATE_WRITE:
Benny Halevye2f282b2008-08-12 20:45:07 +03002795 nfsd4_encode_stateid(resp, &open->op_delegate_stateid);
2796 RESERVE_SPACE(32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 WRITE32(0);
2798
2799 /*
2800 * TODO: space_limit's in delegations
2801 */
2802 WRITE32(NFS4_LIMIT_SIZE);
2803 WRITE32(~(u32)0);
2804 WRITE32(~(u32)0);
2805
2806 /*
2807 * TODO: ACE's in delegations
2808 */
2809 WRITE32(NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
2810 WRITE32(0);
2811 WRITE32(0);
2812 WRITE32(0); /* XXX: is NULL principal ok? */
2813 ADJUST_ARGS();
2814 break;
2815 default:
2816 BUG();
2817 }
2818 /* XXX save filehandle here */
2819out:
J. Bruce Fieldsf3e42232011-08-24 12:27:31 -04002820 encode_seqid_op_tail(resp, save, nfserr);
Benny Halevy695e12f2008-07-04 14:38:33 +03002821 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822}
2823
Benny Halevy695e12f2008-07-04 14:38:33 +03002824static __be32
Al Virob37ad282006-10-19 23:28:59 -07002825nfsd4_encode_open_confirm(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826{
2827 ENCODE_SEQID_OP_HEAD;
Benny Halevye2f282b2008-08-12 20:45:07 +03002828
2829 if (!nfserr)
2830 nfsd4_encode_stateid(resp, &oc->oc_resp_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831
J. Bruce Fieldsf3e42232011-08-24 12:27:31 -04002832 encode_seqid_op_tail(resp, save, nfserr);
Benny Halevy695e12f2008-07-04 14:38:33 +03002833 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834}
2835
Benny Halevy695e12f2008-07-04 14:38:33 +03002836static __be32
Al Virob37ad282006-10-19 23:28:59 -07002837nfsd4_encode_open_downgrade(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838{
2839 ENCODE_SEQID_OP_HEAD;
Benny Halevye2f282b2008-08-12 20:45:07 +03002840
2841 if (!nfserr)
2842 nfsd4_encode_stateid(resp, &od->od_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843
J. Bruce Fieldsf3e42232011-08-24 12:27:31 -04002844 encode_seqid_op_tail(resp, save, nfserr);
Benny Halevy695e12f2008-07-04 14:38:33 +03002845 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846}
2847
Al Virob37ad282006-10-19 23:28:59 -07002848static __be32
2849nfsd4_encode_read(struct nfsd4_compoundres *resp, __be32 nfserr,
NeilBrown44524352006-10-04 02:15:46 -07002850 struct nfsd4_read *read)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851{
2852 u32 eof;
2853 int v, pn;
2854 unsigned long maxcount;
2855 long len;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002856 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857
2858 if (nfserr)
2859 return nfserr;
2860 if (resp->xbuf->page_len)
2861 return nfserr_resource;
2862
2863 RESERVE_SPACE(8); /* eof flag and byte count */
2864
Greg Banks7adae482006-10-04 02:15:47 -07002865 maxcount = svc_max_payload(resp->rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 if (maxcount > read->rd_length)
2867 maxcount = read->rd_length;
2868
2869 len = maxcount;
2870 v = 0;
2871 while (len > 0) {
NeilBrown44524352006-10-04 02:15:46 -07002872 pn = resp->rqstp->rq_resused++;
NeilBrown3cc03b12006-10-04 02:15:47 -07002873 resp->rqstp->rq_vec[v].iov_base =
NeilBrown44524352006-10-04 02:15:46 -07002874 page_address(resp->rqstp->rq_respages[pn]);
NeilBrown3cc03b12006-10-04 02:15:47 -07002875 resp->rqstp->rq_vec[v].iov_len =
NeilBrown44524352006-10-04 02:15:46 -07002876 len < PAGE_SIZE ? len : PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 v++;
2878 len -= PAGE_SIZE;
2879 }
2880 read->rd_vlen = v;
2881
J. Bruce Fields039a87c2010-07-30 11:33:32 -04002882 nfserr = nfsd_read_file(read->rd_rqstp, read->rd_fhp, read->rd_filp,
NeilBrown3cc03b12006-10-04 02:15:47 -07002883 read->rd_offset, resp->rqstp->rq_vec, read->rd_vlen,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 &maxcount);
2885
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 if (nfserr)
2887 return nfserr;
NeilBrown44524352006-10-04 02:15:46 -07002888 eof = (read->rd_offset + maxcount >=
2889 read->rd_fhp->fh_dentry->d_inode->i_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890
2891 WRITE32(eof);
2892 WRITE32(maxcount);
2893 ADJUST_ARGS();
NeilBrown6ed6dec2006-04-10 22:55:32 -07002894 resp->xbuf->head[0].iov_len = (char*)p
2895 - (char*)resp->xbuf->head[0].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 resp->xbuf->page_len = maxcount;
2897
NeilBrown6ed6dec2006-04-10 22:55:32 -07002898 /* Use rest of head for padding and remaining ops: */
NeilBrown6ed6dec2006-04-10 22:55:32 -07002899 resp->xbuf->tail[0].iov_base = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 resp->xbuf->tail[0].iov_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 if (maxcount&3) {
NeilBrown6ed6dec2006-04-10 22:55:32 -07002902 RESERVE_SPACE(4);
2903 WRITE32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 resp->xbuf->tail[0].iov_base += maxcount&3;
2905 resp->xbuf->tail[0].iov_len = 4 - (maxcount&3);
NeilBrown6ed6dec2006-04-10 22:55:32 -07002906 ADJUST_ARGS();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 }
2908 return 0;
2909}
2910
Al Virob37ad282006-10-19 23:28:59 -07002911static __be32
2912nfsd4_encode_readlink(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_readlink *readlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913{
2914 int maxcount;
2915 char *page;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002916 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917
2918 if (nfserr)
2919 return nfserr;
2920 if (resp->xbuf->page_len)
2921 return nfserr_resource;
2922
NeilBrown44524352006-10-04 02:15:46 -07002923 page = page_address(resp->rqstp->rq_respages[resp->rqstp->rq_resused++]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924
2925 maxcount = PAGE_SIZE;
2926 RESERVE_SPACE(4);
2927
2928 /*
2929 * XXX: By default, the ->readlink() VFS op will truncate symlinks
2930 * if they would overflow the buffer. Is this kosher in NFSv4? If
2931 * not, one easy fix is: if ->readlink() precisely fills the buffer,
2932 * assume that truncation occurred, and return NFS4ERR_RESOURCE.
2933 */
2934 nfserr = nfsd_readlink(readlink->rl_rqstp, readlink->rl_fhp, page, &maxcount);
2935 if (nfserr == nfserr_isdir)
2936 return nfserr_inval;
2937 if (nfserr)
2938 return nfserr;
2939
2940 WRITE32(maxcount);
2941 ADJUST_ARGS();
NeilBrown6ed6dec2006-04-10 22:55:32 -07002942 resp->xbuf->head[0].iov_len = (char*)p
2943 - (char*)resp->xbuf->head[0].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 resp->xbuf->page_len = maxcount;
NeilBrown6ed6dec2006-04-10 22:55:32 -07002945
2946 /* Use rest of head for padding and remaining ops: */
NeilBrown6ed6dec2006-04-10 22:55:32 -07002947 resp->xbuf->tail[0].iov_base = p;
2948 resp->xbuf->tail[0].iov_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 if (maxcount&3) {
NeilBrown6ed6dec2006-04-10 22:55:32 -07002950 RESERVE_SPACE(4);
2951 WRITE32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 resp->xbuf->tail[0].iov_base += maxcount&3;
2953 resp->xbuf->tail[0].iov_len = 4 - (maxcount&3);
NeilBrown6ed6dec2006-04-10 22:55:32 -07002954 ADJUST_ARGS();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 }
2956 return 0;
2957}
2958
Al Virob37ad282006-10-19 23:28:59 -07002959static __be32
2960nfsd4_encode_readdir(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_readdir *readdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961{
2962 int maxcount;
2963 loff_t offset;
Al Viro2ebbc012006-10-19 23:28:58 -07002964 __be32 *page, *savep, *tailbase;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07002965 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966
2967 if (nfserr)
2968 return nfserr;
2969 if (resp->xbuf->page_len)
2970 return nfserr_resource;
2971
2972 RESERVE_SPACE(8); /* verifier */
2973 savep = p;
2974
2975 /* XXX: Following NFSv3, we ignore the READDIR verifier for now. */
2976 WRITE32(0);
2977 WRITE32(0);
2978 ADJUST_ARGS();
2979 resp->xbuf->head[0].iov_len = ((char*)resp->p) - (char*)resp->xbuf->head[0].iov_base;
NeilBrownbb6e8a92006-04-10 22:55:33 -07002980 tailbase = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981
2982 maxcount = PAGE_SIZE;
2983 if (maxcount > readdir->rd_maxcount)
2984 maxcount = readdir->rd_maxcount;
2985
2986 /*
2987 * Convert from bytes to words, account for the two words already
2988 * written, make sure to leave two words at the end for the next
2989 * pointer and eof field.
2990 */
2991 maxcount = (maxcount >> 2) - 4;
2992 if (maxcount < 0) {
2993 nfserr = nfserr_toosmall;
2994 goto err_no_verf;
2995 }
2996
NeilBrown44524352006-10-04 02:15:46 -07002997 page = page_address(resp->rqstp->rq_respages[resp->rqstp->rq_resused++]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 readdir->common.err = 0;
2999 readdir->buflen = maxcount;
3000 readdir->buffer = page;
3001 readdir->offset = NULL;
3002
3003 offset = readdir->rd_cookie;
3004 nfserr = nfsd_readdir(readdir->rd_rqstp, readdir->rd_fhp,
3005 &offset,
3006 &readdir->common, nfsd4_encode_dirent);
3007 if (nfserr == nfs_ok &&
3008 readdir->common.err == nfserr_toosmall &&
3009 readdir->buffer == page)
3010 nfserr = nfserr_toosmall;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 if (nfserr)
3012 goto err_no_verf;
3013
3014 if (readdir->offset)
3015 xdr_encode_hyper(readdir->offset, offset);
3016
3017 p = readdir->buffer;
3018 *p++ = 0; /* no more entries */
3019 *p++ = htonl(readdir->common.err == nfserr_eof);
NeilBrown44524352006-10-04 02:15:46 -07003020 resp->xbuf->page_len = ((char*)p) - (char*)page_address(
3021 resp->rqstp->rq_respages[resp->rqstp->rq_resused-1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022
NeilBrownbb6e8a92006-04-10 22:55:33 -07003023 /* Use rest of head for padding and remaining ops: */
NeilBrownbb6e8a92006-04-10 22:55:33 -07003024 resp->xbuf->tail[0].iov_base = tailbase;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 resp->xbuf->tail[0].iov_len = 0;
3026 resp->p = resp->xbuf->tail[0].iov_base;
NeilBrownbb6e8a92006-04-10 22:55:33 -07003027 resp->end = resp->p + (PAGE_SIZE - resp->xbuf->head[0].iov_len)/4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028
3029 return 0;
3030err_no_verf:
3031 p = savep;
3032 ADJUST_ARGS();
3033 return nfserr;
3034}
3035
Benny Halevy695e12f2008-07-04 14:38:33 +03003036static __be32
Al Virob37ad282006-10-19 23:28:59 -07003037nfsd4_encode_remove(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_remove *remove)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003039 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040
3041 if (!nfserr) {
3042 RESERVE_SPACE(20);
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04003043 write_cinfo(&p, &remove->rm_cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 ADJUST_ARGS();
3045 }
Benny Halevy695e12f2008-07-04 14:38:33 +03003046 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047}
3048
Benny Halevy695e12f2008-07-04 14:38:33 +03003049static __be32
Al Virob37ad282006-10-19 23:28:59 -07003050nfsd4_encode_rename(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_rename *rename)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003052 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053
3054 if (!nfserr) {
3055 RESERVE_SPACE(40);
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04003056 write_cinfo(&p, &rename->rn_sinfo);
3057 write_cinfo(&p, &rename->rn_tinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 ADJUST_ARGS();
3059 }
Benny Halevy695e12f2008-07-04 14:38:33 +03003060 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061}
3062
Benny Halevy695e12f2008-07-04 14:38:33 +03003063static __be32
Mi Jinlong22b6dee2010-12-27 14:29:57 +08003064nfsd4_do_encode_secinfo(struct nfsd4_compoundres *resp,
3065 __be32 nfserr,struct svc_export *exp)
Andy Adamsondcb488a32007-07-17 04:04:51 -07003066{
3067 int i = 0;
J. Bruce Fields4796f452007-07-17 04:04:51 -07003068 u32 nflavs;
3069 struct exp_flavor_info *flavs;
3070 struct exp_flavor_info def_flavs[2];
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003071 __be32 *p;
Andy Adamsondcb488a32007-07-17 04:04:51 -07003072
3073 if (nfserr)
3074 goto out;
J. Bruce Fields4796f452007-07-17 04:04:51 -07003075 if (exp->ex_nflavors) {
3076 flavs = exp->ex_flavors;
3077 nflavs = exp->ex_nflavors;
3078 } else { /* Handling of some defaults in absence of real secinfo: */
3079 flavs = def_flavs;
3080 if (exp->ex_client->flavour->flavour == RPC_AUTH_UNIX) {
3081 nflavs = 2;
3082 flavs[0].pseudoflavor = RPC_AUTH_UNIX;
3083 flavs[1].pseudoflavor = RPC_AUTH_NULL;
3084 } else if (exp->ex_client->flavour->flavour == RPC_AUTH_GSS) {
3085 nflavs = 1;
3086 flavs[0].pseudoflavor
3087 = svcauth_gss_flavor(exp->ex_client);
3088 } else {
3089 nflavs = 1;
3090 flavs[0].pseudoflavor
3091 = exp->ex_client->flavour->flavour;
3092 }
3093 }
3094
Andy Adamsondcb488a32007-07-17 04:04:51 -07003095 RESERVE_SPACE(4);
J. Bruce Fields4796f452007-07-17 04:04:51 -07003096 WRITE32(nflavs);
Andy Adamsondcb488a32007-07-17 04:04:51 -07003097 ADJUST_ARGS();
J. Bruce Fields4796f452007-07-17 04:04:51 -07003098 for (i = 0; i < nflavs; i++) {
3099 u32 flav = flavs[i].pseudoflavor;
Andy Adamsondcb488a32007-07-17 04:04:51 -07003100 struct gss_api_mech *gm = gss_mech_get_by_pseudoflavor(flav);
3101
3102 if (gm) {
3103 RESERVE_SPACE(4);
3104 WRITE32(RPC_AUTH_GSS);
3105 ADJUST_ARGS();
3106 RESERVE_SPACE(4 + gm->gm_oid.len);
3107 WRITE32(gm->gm_oid.len);
3108 WRITEMEM(gm->gm_oid.data, gm->gm_oid.len);
3109 ADJUST_ARGS();
3110 RESERVE_SPACE(4);
3111 WRITE32(0); /* qop */
3112 ADJUST_ARGS();
3113 RESERVE_SPACE(4);
3114 WRITE32(gss_pseudoflavor_to_service(gm, flav));
3115 ADJUST_ARGS();
3116 gss_mech_put(gm);
3117 } else {
3118 RESERVE_SPACE(4);
3119 WRITE32(flav);
3120 ADJUST_ARGS();
3121 }
3122 }
3123out:
3124 if (exp)
3125 exp_put(exp);
Benny Halevy695e12f2008-07-04 14:38:33 +03003126 return nfserr;
Andy Adamsondcb488a32007-07-17 04:04:51 -07003127}
3128
Mi Jinlong22b6dee2010-12-27 14:29:57 +08003129static __be32
3130nfsd4_encode_secinfo(struct nfsd4_compoundres *resp, __be32 nfserr,
3131 struct nfsd4_secinfo *secinfo)
3132{
3133 return nfsd4_do_encode_secinfo(resp, nfserr, secinfo->si_exp);
3134}
3135
3136static __be32
3137nfsd4_encode_secinfo_no_name(struct nfsd4_compoundres *resp, __be32 nfserr,
3138 struct nfsd4_secinfo_no_name *secinfo)
3139{
3140 return nfsd4_do_encode_secinfo(resp, nfserr, secinfo->sin_exp);
3141}
3142
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143/*
3144 * The SETATTR encode routine is special -- it always encodes a bitmap,
3145 * regardless of the error status.
3146 */
Benny Halevy695e12f2008-07-04 14:38:33 +03003147static __be32
Al Virob37ad282006-10-19 23:28:59 -07003148nfsd4_encode_setattr(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_setattr *setattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003150 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151
3152 RESERVE_SPACE(12);
3153 if (nfserr) {
3154 WRITE32(2);
3155 WRITE32(0);
3156 WRITE32(0);
3157 }
3158 else {
3159 WRITE32(2);
3160 WRITE32(setattr->sa_bmval[0]);
3161 WRITE32(setattr->sa_bmval[1]);
3162 }
3163 ADJUST_ARGS();
Benny Halevy695e12f2008-07-04 14:38:33 +03003164 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165}
3166
Benny Halevy695e12f2008-07-04 14:38:33 +03003167static __be32
Al Virob37ad282006-10-19 23:28:59 -07003168nfsd4_encode_setclientid(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_setclientid *scd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003170 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171
3172 if (!nfserr) {
3173 RESERVE_SPACE(8 + sizeof(nfs4_verifier));
3174 WRITEMEM(&scd->se_clientid, 8);
3175 WRITEMEM(&scd->se_confirm, sizeof(nfs4_verifier));
3176 ADJUST_ARGS();
3177 }
3178 else if (nfserr == nfserr_clid_inuse) {
3179 RESERVE_SPACE(8);
3180 WRITE32(0);
3181 WRITE32(0);
3182 ADJUST_ARGS();
3183 }
Benny Halevy695e12f2008-07-04 14:38:33 +03003184 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185}
3186
Benny Halevy695e12f2008-07-04 14:38:33 +03003187static __be32
Al Virob37ad282006-10-19 23:28:59 -07003188nfsd4_encode_write(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_write *write)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003190 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191
3192 if (!nfserr) {
3193 RESERVE_SPACE(16);
3194 WRITE32(write->wr_bytes_written);
3195 WRITE32(write->wr_how_written);
3196 WRITEMEM(write->wr_verifier.data, 8);
3197 ADJUST_ARGS();
3198 }
Benny Halevy695e12f2008-07-04 14:38:33 +03003199 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200}
3201
Benny Halevy695e12f2008-07-04 14:38:33 +03003202static __be32
Andy Adamson2db134e2009-04-03 08:27:55 +03003203nfsd4_encode_exchange_id(struct nfsd4_compoundres *resp, int nfserr,
3204 struct nfsd4_exchange_id *exid)
3205{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003206 __be32 *p;
Andy Adamson0733d212009-04-03 08:28:01 +03003207 char *major_id;
3208 char *server_scope;
3209 int major_id_sz;
3210 int server_scope_sz;
3211 uint64_t minor_id = 0;
3212
3213 if (nfserr)
3214 return nfserr;
3215
3216 major_id = utsname()->nodename;
3217 major_id_sz = strlen(major_id);
3218 server_scope = utsname()->nodename;
3219 server_scope_sz = strlen(server_scope);
3220
3221 RESERVE_SPACE(
3222 8 /* eir_clientid */ +
3223 4 /* eir_sequenceid */ +
3224 4 /* eir_flags */ +
3225 4 /* spr_how (SP4_NONE) */ +
3226 8 /* so_minor_id */ +
3227 4 /* so_major_id.len */ +
3228 (XDR_QUADLEN(major_id_sz) * 4) +
3229 4 /* eir_server_scope.len */ +
3230 (XDR_QUADLEN(server_scope_sz) * 4) +
3231 4 /* eir_server_impl_id.count (0) */);
3232
3233 WRITEMEM(&exid->clientid, 8);
3234 WRITE32(exid->seqid);
3235 WRITE32(exid->flags);
3236
3237 /* state_protect4_r. Currently only support SP4_NONE */
3238 BUG_ON(exid->spa_how != SP4_NONE);
3239 WRITE32(exid->spa_how);
3240
3241 /* The server_owner struct */
3242 WRITE64(minor_id); /* Minor id */
3243 /* major id */
3244 WRITE32(major_id_sz);
3245 WRITEMEM(major_id, major_id_sz);
3246
3247 /* Server scope */
3248 WRITE32(server_scope_sz);
3249 WRITEMEM(server_scope, server_scope_sz);
3250
3251 /* Implementation id */
3252 WRITE32(0); /* zero length nfs_impl_id4 array */
3253 ADJUST_ARGS();
3254 return 0;
Andy Adamson2db134e2009-04-03 08:27:55 +03003255}
3256
3257static __be32
3258nfsd4_encode_create_session(struct nfsd4_compoundres *resp, int nfserr,
3259 struct nfsd4_create_session *sess)
3260{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003261 __be32 *p;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03003262
3263 if (nfserr)
3264 return nfserr;
3265
3266 RESERVE_SPACE(24);
3267 WRITEMEM(sess->sessionid.data, NFS4_MAX_SESSIONID_LEN);
3268 WRITE32(sess->seqid);
3269 WRITE32(sess->flags);
3270 ADJUST_ARGS();
3271
3272 RESERVE_SPACE(28);
3273 WRITE32(0); /* headerpadsz */
3274 WRITE32(sess->fore_channel.maxreq_sz);
3275 WRITE32(sess->fore_channel.maxresp_sz);
3276 WRITE32(sess->fore_channel.maxresp_cached);
3277 WRITE32(sess->fore_channel.maxops);
3278 WRITE32(sess->fore_channel.maxreqs);
3279 WRITE32(sess->fore_channel.nr_rdma_attrs);
3280 ADJUST_ARGS();
3281
3282 if (sess->fore_channel.nr_rdma_attrs) {
3283 RESERVE_SPACE(4);
3284 WRITE32(sess->fore_channel.rdma_attrs);
3285 ADJUST_ARGS();
3286 }
3287
3288 RESERVE_SPACE(28);
3289 WRITE32(0); /* headerpadsz */
3290 WRITE32(sess->back_channel.maxreq_sz);
3291 WRITE32(sess->back_channel.maxresp_sz);
3292 WRITE32(sess->back_channel.maxresp_cached);
3293 WRITE32(sess->back_channel.maxops);
3294 WRITE32(sess->back_channel.maxreqs);
3295 WRITE32(sess->back_channel.nr_rdma_attrs);
3296 ADJUST_ARGS();
3297
3298 if (sess->back_channel.nr_rdma_attrs) {
3299 RESERVE_SPACE(4);
3300 WRITE32(sess->back_channel.rdma_attrs);
3301 ADJUST_ARGS();
3302 }
3303 return 0;
Andy Adamson2db134e2009-04-03 08:27:55 +03003304}
3305
3306static __be32
3307nfsd4_encode_destroy_session(struct nfsd4_compoundres *resp, int nfserr,
3308 struct nfsd4_destroy_session *destroy_session)
3309{
Andy Adamson2db134e2009-04-03 08:27:55 +03003310 return nfserr;
3311}
3312
Daniel Mackc47d8322011-05-16 16:38:14 +02003313static __be32
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003314nfsd4_encode_free_stateid(struct nfsd4_compoundres *resp, int nfserr,
3315 struct nfsd4_free_stateid *free_stateid)
3316{
3317 __be32 *p;
3318
3319 if (nfserr)
3320 return nfserr;
3321
3322 RESERVE_SPACE(4);
3323 WRITE32(nfserr);
3324 ADJUST_ARGS();
3325 return nfserr;
3326}
3327
3328static __be32
Andy Adamson2db134e2009-04-03 08:27:55 +03003329nfsd4_encode_sequence(struct nfsd4_compoundres *resp, int nfserr,
3330 struct nfsd4_sequence *seq)
3331{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003332 __be32 *p;
Benny Halevyb85d4c02009-04-03 08:28:08 +03003333
3334 if (nfserr)
3335 return nfserr;
3336
3337 RESERVE_SPACE(NFS4_MAX_SESSIONID_LEN + 20);
3338 WRITEMEM(seq->sessionid.data, NFS4_MAX_SESSIONID_LEN);
3339 WRITE32(seq->seqid);
3340 WRITE32(seq->slotid);
J. Bruce Fieldsb7d7ca32011-08-31 15:39:30 -04003341 /* Note slotid's are numbered from zero: */
3342 WRITE32(seq->maxslots - 1); /* sr_highest_slotid */
3343 WRITE32(seq->maxslots - 1); /* sr_target_highest_slotid */
J. Bruce Fields0d7bb712010-11-18 08:30:33 -05003344 WRITE32(seq->status_flags);
Benny Halevyb85d4c02009-04-03 08:28:08 +03003345
3346 ADJUST_ARGS();
Andy Adamson557ce262009-08-28 08:45:04 -04003347 resp->cstate.datap = p; /* DRC cache data pointer */
Benny Halevyb85d4c02009-04-03 08:28:08 +03003348 return 0;
Andy Adamson2db134e2009-04-03 08:27:55 +03003349}
3350
Bryan Schumaker17456802011-07-13 10:50:48 -04003351__be32
3352nfsd4_encode_test_stateid(struct nfsd4_compoundres *resp, int nfserr,
3353 struct nfsd4_test_stateid *test_stateid)
3354{
3355 struct nfsd4_compoundargs *argp;
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003356 struct nfs4_client *cl = resp->cstate.session->se_client;
Bryan Schumaker17456802011-07-13 10:50:48 -04003357 stateid_t si;
3358 __be32 *p;
3359 int i;
3360 int valid;
3361
3362 restore_buf(test_stateid->ts_saved_args, &test_stateid->ts_savedp);
3363 argp = test_stateid->ts_saved_args;
3364
3365 RESERVE_SPACE(4);
3366 *p++ = htonl(test_stateid->ts_num_ids);
3367 resp->p = p;
3368
3369 nfs4_lock_state();
3370 for (i = 0; i < test_stateid->ts_num_ids; i++) {
3371 nfsd4_decode_stateid(argp, &si);
J. Bruce Fields38c2f4b2011-09-23 17:01:19 -04003372 valid = nfs4_validate_stateid(cl, &si);
Bryan Schumaker17456802011-07-13 10:50:48 -04003373 RESERVE_SPACE(4);
3374 *p++ = htonl(valid);
3375 resp->p = p;
3376 }
3377 nfs4_unlock_state();
3378
3379 return nfserr;
3380}
3381
Andy Adamson2db134e2009-04-03 08:27:55 +03003382static __be32
Benny Halevy695e12f2008-07-04 14:38:33 +03003383nfsd4_encode_noop(struct nfsd4_compoundres *resp, __be32 nfserr, void *p)
3384{
3385 return nfserr;
3386}
3387
3388typedef __be32(* nfsd4_enc)(struct nfsd4_compoundres *, __be32, void *);
3389
Andy Adamson2db134e2009-04-03 08:27:55 +03003390/*
3391 * Note: nfsd4_enc_ops vector is shared for v4.0 and v4.1
3392 * since we don't need to filter out obsolete ops as this is
3393 * done in the decoding phase.
3394 */
Benny Halevy695e12f2008-07-04 14:38:33 +03003395static nfsd4_enc nfsd4_enc_ops[] = {
J. Bruce Fieldsad1060c2008-07-18 15:04:16 -04003396 [OP_ACCESS] = (nfsd4_enc)nfsd4_encode_access,
3397 [OP_CLOSE] = (nfsd4_enc)nfsd4_encode_close,
3398 [OP_COMMIT] = (nfsd4_enc)nfsd4_encode_commit,
3399 [OP_CREATE] = (nfsd4_enc)nfsd4_encode_create,
3400 [OP_DELEGPURGE] = (nfsd4_enc)nfsd4_encode_noop,
3401 [OP_DELEGRETURN] = (nfsd4_enc)nfsd4_encode_noop,
3402 [OP_GETATTR] = (nfsd4_enc)nfsd4_encode_getattr,
3403 [OP_GETFH] = (nfsd4_enc)nfsd4_encode_getfh,
3404 [OP_LINK] = (nfsd4_enc)nfsd4_encode_link,
3405 [OP_LOCK] = (nfsd4_enc)nfsd4_encode_lock,
3406 [OP_LOCKT] = (nfsd4_enc)nfsd4_encode_lockt,
3407 [OP_LOCKU] = (nfsd4_enc)nfsd4_encode_locku,
3408 [OP_LOOKUP] = (nfsd4_enc)nfsd4_encode_noop,
3409 [OP_LOOKUPP] = (nfsd4_enc)nfsd4_encode_noop,
3410 [OP_NVERIFY] = (nfsd4_enc)nfsd4_encode_noop,
3411 [OP_OPEN] = (nfsd4_enc)nfsd4_encode_open,
Benny Halevy84f09f42009-03-04 23:05:35 +02003412 [OP_OPENATTR] = (nfsd4_enc)nfsd4_encode_noop,
J. Bruce Fieldsad1060c2008-07-18 15:04:16 -04003413 [OP_OPEN_CONFIRM] = (nfsd4_enc)nfsd4_encode_open_confirm,
3414 [OP_OPEN_DOWNGRADE] = (nfsd4_enc)nfsd4_encode_open_downgrade,
3415 [OP_PUTFH] = (nfsd4_enc)nfsd4_encode_noop,
3416 [OP_PUTPUBFH] = (nfsd4_enc)nfsd4_encode_noop,
3417 [OP_PUTROOTFH] = (nfsd4_enc)nfsd4_encode_noop,
3418 [OP_READ] = (nfsd4_enc)nfsd4_encode_read,
3419 [OP_READDIR] = (nfsd4_enc)nfsd4_encode_readdir,
3420 [OP_READLINK] = (nfsd4_enc)nfsd4_encode_readlink,
3421 [OP_REMOVE] = (nfsd4_enc)nfsd4_encode_remove,
3422 [OP_RENAME] = (nfsd4_enc)nfsd4_encode_rename,
3423 [OP_RENEW] = (nfsd4_enc)nfsd4_encode_noop,
3424 [OP_RESTOREFH] = (nfsd4_enc)nfsd4_encode_noop,
3425 [OP_SAVEFH] = (nfsd4_enc)nfsd4_encode_noop,
3426 [OP_SECINFO] = (nfsd4_enc)nfsd4_encode_secinfo,
3427 [OP_SETATTR] = (nfsd4_enc)nfsd4_encode_setattr,
3428 [OP_SETCLIENTID] = (nfsd4_enc)nfsd4_encode_setclientid,
3429 [OP_SETCLIENTID_CONFIRM] = (nfsd4_enc)nfsd4_encode_noop,
3430 [OP_VERIFY] = (nfsd4_enc)nfsd4_encode_noop,
3431 [OP_WRITE] = (nfsd4_enc)nfsd4_encode_write,
3432 [OP_RELEASE_LOCKOWNER] = (nfsd4_enc)nfsd4_encode_noop,
Andy Adamson2db134e2009-04-03 08:27:55 +03003433
3434 /* NFSv4.1 operations */
3435 [OP_BACKCHANNEL_CTL] = (nfsd4_enc)nfsd4_encode_noop,
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003436 [OP_BIND_CONN_TO_SESSION] = (nfsd4_enc)nfsd4_encode_bind_conn_to_session,
Andy Adamson2db134e2009-04-03 08:27:55 +03003437 [OP_EXCHANGE_ID] = (nfsd4_enc)nfsd4_encode_exchange_id,
3438 [OP_CREATE_SESSION] = (nfsd4_enc)nfsd4_encode_create_session,
3439 [OP_DESTROY_SESSION] = (nfsd4_enc)nfsd4_encode_destroy_session,
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04003440 [OP_FREE_STATEID] = (nfsd4_enc)nfsd4_encode_free_stateid,
Andy Adamson2db134e2009-04-03 08:27:55 +03003441 [OP_GET_DIR_DELEGATION] = (nfsd4_enc)nfsd4_encode_noop,
3442 [OP_GETDEVICEINFO] = (nfsd4_enc)nfsd4_encode_noop,
3443 [OP_GETDEVICELIST] = (nfsd4_enc)nfsd4_encode_noop,
3444 [OP_LAYOUTCOMMIT] = (nfsd4_enc)nfsd4_encode_noop,
3445 [OP_LAYOUTGET] = (nfsd4_enc)nfsd4_encode_noop,
3446 [OP_LAYOUTRETURN] = (nfsd4_enc)nfsd4_encode_noop,
Mi Jinlong22b6dee2010-12-27 14:29:57 +08003447 [OP_SECINFO_NO_NAME] = (nfsd4_enc)nfsd4_encode_secinfo_no_name,
Andy Adamson2db134e2009-04-03 08:27:55 +03003448 [OP_SEQUENCE] = (nfsd4_enc)nfsd4_encode_sequence,
3449 [OP_SET_SSV] = (nfsd4_enc)nfsd4_encode_noop,
Bryan Schumaker17456802011-07-13 10:50:48 -04003450 [OP_TEST_STATEID] = (nfsd4_enc)nfsd4_encode_test_stateid,
Andy Adamson2db134e2009-04-03 08:27:55 +03003451 [OP_WANT_DELEGATION] = (nfsd4_enc)nfsd4_encode_noop,
3452 [OP_DESTROY_CLIENTID] = (nfsd4_enc)nfsd4_encode_noop,
3453 [OP_RECLAIM_COMPLETE] = (nfsd4_enc)nfsd4_encode_noop,
Benny Halevy695e12f2008-07-04 14:38:33 +03003454};
3455
Andy Adamson496c2622009-04-03 08:28:48 +03003456/*
3457 * Calculate the total amount of memory that the compound response has taken
Mi Jinlong58e7b332011-08-28 18:18:56 +08003458 * after encoding the current operation with pad.
Andy Adamson496c2622009-04-03 08:28:48 +03003459 *
Mi Jinlong58e7b332011-08-28 18:18:56 +08003460 * pad: if operation is non-idempotent, pad was calculate by op_rsize_bop()
3461 * which was specified at nfsd4_operation, else pad is zero.
Andy Adamson496c2622009-04-03 08:28:48 +03003462 *
Mi Jinlong58e7b332011-08-28 18:18:56 +08003463 * Compare this length to the session se_fmaxresp_sz and se_fmaxresp_cached.
Andy Adamson496c2622009-04-03 08:28:48 +03003464 *
3465 * Our se_fmaxresp_cached will always be a multiple of PAGE_SIZE, and so
3466 * will be at least a page and will therefore hold the xdr_buf head.
3467 */
Mi Jinlong58e7b332011-08-28 18:18:56 +08003468int nfsd4_check_resp_size(struct nfsd4_compoundres *resp, u32 pad)
Andy Adamson496c2622009-04-03 08:28:48 +03003469{
Andy Adamson496c2622009-04-03 08:28:48 +03003470 struct xdr_buf *xb = &resp->rqstp->rq_res;
Andy Adamson496c2622009-04-03 08:28:48 +03003471 struct nfsd4_session *session = NULL;
3472 struct nfsd4_slot *slot = resp->cstate.slot;
Mi Jinlong58e7b332011-08-28 18:18:56 +08003473 u32 length, tlen = 0;
Andy Adamson496c2622009-04-03 08:28:48 +03003474
3475 if (!nfsd4_has_session(&resp->cstate))
Mi Jinlong58e7b332011-08-28 18:18:56 +08003476 return 0;
Andy Adamson496c2622009-04-03 08:28:48 +03003477
3478 session = resp->cstate.session;
Mi Jinlong58e7b332011-08-28 18:18:56 +08003479 if (session == NULL)
3480 return 0;
Andy Adamson496c2622009-04-03 08:28:48 +03003481
3482 if (xb->page_len == 0) {
3483 length = (char *)resp->p - (char *)xb->head[0].iov_base + pad;
3484 } else {
3485 if (xb->tail[0].iov_base && xb->tail[0].iov_len > 0)
3486 tlen = (char *)resp->p - (char *)xb->tail[0].iov_base;
3487
3488 length = xb->head[0].iov_len + xb->page_len + tlen + pad;
3489 }
3490 dprintk("%s length %u, xb->page_len %u tlen %u pad %u\n", __func__,
3491 length, xb->page_len, tlen, pad);
3492
Mi Jinlong58e7b332011-08-28 18:18:56 +08003493 if (length > session->se_fchannel.maxresp_sz)
3494 return nfserr_rep_too_big;
3495
3496 if (slot->sl_cachethis == 1 &&
3497 length > session->se_fchannel.maxresp_cached)
Andy Adamson496c2622009-04-03 08:28:48 +03003498 return nfserr_rep_too_big_to_cache;
Mi Jinlong58e7b332011-08-28 18:18:56 +08003499
3500 return 0;
Andy Adamson496c2622009-04-03 08:28:48 +03003501}
3502
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503void
3504nfsd4_encode_operation(struct nfsd4_compoundres *resp, struct nfsd4_op *op)
3505{
Al Viro2ebbc012006-10-19 23:28:58 -07003506 __be32 *statp;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003507 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508
3509 RESERVE_SPACE(8);
3510 WRITE32(op->opnum);
3511 statp = p++; /* to be backfilled at the end */
3512 ADJUST_ARGS();
3513
Benny Halevy695e12f2008-07-04 14:38:33 +03003514 if (op->opnum == OP_ILLEGAL)
3515 goto status;
3516 BUG_ON(op->opnum < 0 || op->opnum >= ARRAY_SIZE(nfsd4_enc_ops) ||
3517 !nfsd4_enc_ops[op->opnum]);
3518 op->status = nfsd4_enc_ops[op->opnum](resp, op->status, &op->u);
Andy Adamson496c2622009-04-03 08:28:48 +03003519 /* nfsd4_check_drc_limit guarantees enough room for error status */
Mi Jinlong58e7b332011-08-28 18:18:56 +08003520 if (!op->status)
3521 op->status = nfsd4_check_resp_size(resp, 0);
Benny Halevy695e12f2008-07-04 14:38:33 +03003522status:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523 /*
3524 * Note: We write the status directly, instead of using WRITE32(),
3525 * since it is already in network byte order.
3526 */
3527 *statp = op->status;
3528}
3529
3530/*
3531 * Encode the reply stored in the stateowner reply cache
3532 *
3533 * XDR note: do not encode rp->rp_buflen: the buffer contains the
3534 * previously sent already encoded operation.
3535 *
3536 * called with nfs4_lock_state() held
3537 */
3538void
3539nfsd4_encode_replay(struct nfsd4_compoundres *resp, struct nfsd4_op *op)
3540{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003541 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542 struct nfs4_replay *rp = op->replay;
3543
3544 BUG_ON(!rp);
3545
3546 RESERVE_SPACE(8);
3547 WRITE32(op->opnum);
3548 *p++ = rp->rp_status; /* already xdr'ed */
3549 ADJUST_ARGS();
3550
3551 RESERVE_SPACE(rp->rp_buflen);
3552 WRITEMEM(rp->rp_buf, rp->rp_buflen);
3553 ADJUST_ARGS();
3554}
3555
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556int
Al Viro2ebbc012006-10-19 23:28:58 -07003557nfs4svc_encode_voidres(struct svc_rqst *rqstp, __be32 *p, void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558{
3559 return xdr_ressize_check(rqstp, p);
3560}
3561
J. Bruce Fields3e98abf2011-07-16 17:15:10 -04003562int nfsd4_release_compoundargs(void *rq, __be32 *p, void *resp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563{
J. Bruce Fields3e98abf2011-07-16 17:15:10 -04003564 struct svc_rqst *rqstp = rq;
3565 struct nfsd4_compoundargs *args = rqstp->rq_argp;
3566
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567 if (args->ops != args->iops) {
3568 kfree(args->ops);
3569 args->ops = args->iops;
3570 }
Jesper Juhlf99d49a2005-11-07 01:01:34 -08003571 kfree(args->tmpp);
3572 args->tmpp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573 while (args->to_free) {
3574 struct tmpbuf *tb = args->to_free;
3575 args->to_free = tb->next;
3576 tb->release(tb->buf);
3577 kfree(tb);
3578 }
J. Bruce Fields3e98abf2011-07-16 17:15:10 -04003579 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580}
3581
3582int
Al Viro2ebbc012006-10-19 23:28:58 -07003583nfs4svc_decode_compoundargs(struct svc_rqst *rqstp, __be32 *p, struct nfsd4_compoundargs *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585 args->p = p;
3586 args->end = rqstp->rq_arg.head[0].iov_base + rqstp->rq_arg.head[0].iov_len;
3587 args->pagelist = rqstp->rq_arg.pages;
3588 args->pagelen = rqstp->rq_arg.page_len;
3589 args->tmpp = NULL;
3590 args->to_free = NULL;
3591 args->ops = args->iops;
3592 args->rqstp = rqstp;
3593
J. Bruce Fields3e98abf2011-07-16 17:15:10 -04003594 return !nfsd4_decode_compound(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595}
3596
3597int
Al Viro2ebbc012006-10-19 23:28:58 -07003598nfs4svc_encode_compoundres(struct svc_rqst *rqstp, __be32 *p, struct nfsd4_compoundres *resp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599{
3600 /*
3601 * All that remains is to write the tag and operation count...
3602 */
Andy Adamson557ce262009-08-28 08:45:04 -04003603 struct nfsd4_compound_state *cs = &resp->cstate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604 struct kvec *iov;
3605 p = resp->tagp;
3606 *p++ = htonl(resp->taglen);
3607 memcpy(p, resp->tag, resp->taglen);
3608 p += XDR_QUADLEN(resp->taglen);
3609 *p++ = htonl(resp->opcnt);
3610
3611 if (rqstp->rq_res.page_len)
3612 iov = &rqstp->rq_res.tail[0];
3613 else
3614 iov = &rqstp->rq_res.head[0];
3615 iov->iov_len = ((char*)resp->p) - (char*)iov->iov_base;
3616 BUG_ON(iov->iov_len > PAGE_SIZE);
J. Bruce Fields26c0c752010-04-24 15:35:43 -04003617 if (nfsd4_has_session(cs)) {
3618 if (cs->status != nfserr_replay_cache) {
3619 nfsd4_store_cache_entry(resp);
3620 dprintk("%s: SET SLOT STATE TO AVAILABLE\n", __func__);
Benny Halevydbd65a72010-05-03 19:31:33 +03003621 cs->slot->sl_inuse = false;
J. Bruce Fields26c0c752010-04-24 15:35:43 -04003622 }
Benny Halevyd7682982010-05-12 00:13:54 +03003623 /* Renew the clientid on success and on replay */
3624 release_session_client(cs->session);
J. Bruce Fields76407f72010-06-22 14:10:14 -04003625 nfsd4_put_session(cs->session);
Andy Adamsonda3846a2009-04-03 08:28:22 +03003626 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627 return 1;
3628}
3629
3630/*
3631 * Local variables:
3632 * c-basic-offset: 8
3633 * End:
3634 */