blob: 17d0dd997204a8442efc3514c98045c81363730c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/nfsd/nfs3xdr.c
3 *
4 * XDR support for nfsd/protocol version 3.
5 *
6 * Copyright (C) 1995, 1996, 1997 Olaf Kirch <okir@monad.swb.de>
7 *
8 * 2003-08-09 Jamie Lokier: Use htonl() for nanoseconds, not htons()!
9 */
10
11#include <linux/types.h>
12#include <linux/time.h>
13#include <linux/nfs3.h>
14#include <linux/list.h>
15#include <linux/spinlock.h>
16#include <linux/dcache.h>
17#include <linux/namei.h>
18#include <linux/mm.h>
19#include <linux/vfs.h>
20#include <linux/sunrpc/xdr.h>
21#include <linux/sunrpc/svc.h>
22#include <linux/nfsd/nfsd.h>
23#include <linux/nfsd/xdr3.h>
J. Bruce Fields2e8138a2007-11-15 17:05:43 -050024#include "auth.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26#define NFSDDBG_FACILITY NFSDDBG_XDR
27
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29/*
30 * Mapping of S_IF* types to NFS file types
31 */
32static u32 nfs3_ftypes[] = {
33 NF3NON, NF3FIFO, NF3CHR, NF3BAD,
34 NF3DIR, NF3BAD, NF3BLK, NF3BAD,
35 NF3REG, NF3BAD, NF3LNK, NF3BAD,
36 NF3SOCK, NF3BAD, NF3LNK, NF3BAD,
37};
38
39/*
40 * XDR functions for basic NFS types
41 */
Adrian Bunk3ee6f612006-12-06 20:40:23 -080042static __be32 *
Al Viro91f07162006-10-19 23:28:57 -070043encode_time3(__be32 *p, struct timespec *time)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044{
45 *p++ = htonl((u32) time->tv_sec); *p++ = htonl(time->tv_nsec);
46 return p;
47}
48
Adrian Bunk3ee6f612006-12-06 20:40:23 -080049static __be32 *
Al Viro91f07162006-10-19 23:28:57 -070050decode_time3(__be32 *p, struct timespec *time)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051{
52 time->tv_sec = ntohl(*p++);
53 time->tv_nsec = ntohl(*p++);
54 return p;
55}
56
Adrian Bunk3ee6f612006-12-06 20:40:23 -080057static __be32 *
Al Viro91f07162006-10-19 23:28:57 -070058decode_fh(__be32 *p, struct svc_fh *fhp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059{
60 unsigned int size;
61 fh_init(fhp, NFS3_FHSIZE);
62 size = ntohl(*p++);
63 if (size > NFS3_FHSIZE)
64 return NULL;
65
66 memcpy(&fhp->fh_handle.fh_base, p, size);
67 fhp->fh_handle.fh_size = size;
68 return p + XDR_QUADLEN(size);
69}
70
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +000071/* Helper function for NFSv3 ACL code */
Al Viro91f07162006-10-19 23:28:57 -070072__be32 *nfs3svc_decode_fh(__be32 *p, struct svc_fh *fhp)
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +000073{
74 return decode_fh(p, fhp);
75}
76
Adrian Bunk3ee6f612006-12-06 20:40:23 -080077static __be32 *
Al Viro91f07162006-10-19 23:28:57 -070078encode_fh(__be32 *p, struct svc_fh *fhp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
80 unsigned int size = fhp->fh_handle.fh_size;
81 *p++ = htonl(size);
82 if (size) p[XDR_QUADLEN(size)-1]=0;
83 memcpy(p, &fhp->fh_handle.fh_base, size);
84 return p + XDR_QUADLEN(size);
85}
86
87/*
88 * Decode a file name and make sure that the path contains
89 * no slashes or null bytes.
90 */
Adrian Bunk3ee6f612006-12-06 20:40:23 -080091static __be32 *
Chuck Leveree1a95b2007-11-01 16:56:58 -040092decode_filename(__be32 *p, char **namp, unsigned int *lenp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
94 char *name;
Chuck Leveree1a95b2007-11-01 16:56:58 -040095 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97 if ((p = xdr_decode_string_inplace(p, namp, lenp, NFS3_MAXNAMLEN)) != NULL) {
98 for (i = 0, name = *namp; i < *lenp; i++, name++) {
99 if (*name == '\0' || *name == '/')
100 return NULL;
101 }
102 }
103
104 return p;
105}
106
Adrian Bunk3ee6f612006-12-06 20:40:23 -0800107static __be32 *
Al Viro91f07162006-10-19 23:28:57 -0700108decode_sattr3(__be32 *p, struct iattr *iap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109{
110 u32 tmp;
111
112 iap->ia_valid = 0;
113
114 if (*p++) {
115 iap->ia_valid |= ATTR_MODE;
116 iap->ia_mode = ntohl(*p++);
117 }
118 if (*p++) {
119 iap->ia_valid |= ATTR_UID;
120 iap->ia_uid = ntohl(*p++);
121 }
122 if (*p++) {
123 iap->ia_valid |= ATTR_GID;
124 iap->ia_gid = ntohl(*p++);
125 }
126 if (*p++) {
127 u64 newsize;
128
129 iap->ia_valid |= ATTR_SIZE;
130 p = xdr_decode_hyper(p, &newsize);
131 if (newsize <= NFS_OFFSET_MAX)
132 iap->ia_size = newsize;
133 else
134 iap->ia_size = NFS_OFFSET_MAX;
135 }
136 if ((tmp = ntohl(*p++)) == 1) { /* set to server time */
137 iap->ia_valid |= ATTR_ATIME;
138 } else if (tmp == 2) { /* set to client time */
139 iap->ia_valid |= ATTR_ATIME | ATTR_ATIME_SET;
140 iap->ia_atime.tv_sec = ntohl(*p++);
141 iap->ia_atime.tv_nsec = ntohl(*p++);
142 }
143 if ((tmp = ntohl(*p++)) == 1) { /* set to server time */
144 iap->ia_valid |= ATTR_MTIME;
145 } else if (tmp == 2) { /* set to client time */
146 iap->ia_valid |= ATTR_MTIME | ATTR_MTIME_SET;
147 iap->ia_mtime.tv_sec = ntohl(*p++);
148 iap->ia_mtime.tv_nsec = ntohl(*p++);
149 }
150 return p;
151}
152
NeilBrownaf6a4e22007-02-14 00:33:12 -0800153static __be32 *encode_fsid(__be32 *p, struct svc_fh *fhp)
154{
155 u64 f;
156 switch(fsid_source(fhp)) {
157 default:
158 case FSIDSOURCE_DEV:
159 p = xdr_encode_hyper(p, (u64)huge_encode_dev
160 (fhp->fh_dentry->d_inode->i_sb->s_dev));
161 break;
162 case FSIDSOURCE_FSID:
163 p = xdr_encode_hyper(p, (u64) fhp->fh_export->ex_fsid);
164 break;
165 case FSIDSOURCE_UUID:
166 f = ((u64*)fhp->fh_export->ex_uuid)[0];
167 f ^= ((u64*)fhp->fh_export->ex_uuid)[1];
168 p = xdr_encode_hyper(p, f);
169 break;
170 }
171 return p;
172}
173
Adrian Bunk3ee6f612006-12-06 20:40:23 -0800174static __be32 *
Al Viro91f07162006-10-19 23:28:57 -0700175encode_fattr3(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp,
David Shawa334de22006-01-06 00:19:58 -0800176 struct kstat *stat)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
David Shawa334de22006-01-06 00:19:58 -0800178 *p++ = htonl(nfs3_ftypes[(stat->mode & S_IFMT) >> 12]);
179 *p++ = htonl((u32) stat->mode);
180 *p++ = htonl((u32) stat->nlink);
181 *p++ = htonl((u32) nfsd_ruid(rqstp, stat->uid));
182 *p++ = htonl((u32) nfsd_rgid(rqstp, stat->gid));
183 if (S_ISLNK(stat->mode) && stat->size > NFS3_MAXPATHLEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 p = xdr_encode_hyper(p, (u64) NFS3_MAXPATHLEN);
185 } else {
David Shawa334de22006-01-06 00:19:58 -0800186 p = xdr_encode_hyper(p, (u64) stat->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 }
David Shawa334de22006-01-06 00:19:58 -0800188 p = xdr_encode_hyper(p, ((u64)stat->blocks) << 9);
189 *p++ = htonl((u32) MAJOR(stat->rdev));
190 *p++ = htonl((u32) MINOR(stat->rdev));
NeilBrownaf6a4e22007-02-14 00:33:12 -0800191 p = encode_fsid(p, fhp);
Peter Staubach40ee5dc2007-08-16 12:10:07 -0400192 p = xdr_encode_hyper(p, stat->ino);
David Shawa334de22006-01-06 00:19:58 -0800193 p = encode_time3(p, &stat->atime);
Peter Staubach40ee5dc2007-08-16 12:10:07 -0400194 p = encode_time3(p, &stat->mtime);
David Shawa334de22006-01-06 00:19:58 -0800195 p = encode_time3(p, &stat->ctime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
197 return p;
198}
199
Adrian Bunk3ee6f612006-12-06 20:40:23 -0800200static __be32 *
Al Viro91f07162006-10-19 23:28:57 -0700201encode_saved_post_attr(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 /* Attributes to follow */
204 *p++ = xdr_one;
Peter Staubach40ee5dc2007-08-16 12:10:07 -0400205 return encode_fattr3(rqstp, p, fhp, &fhp->fh_post_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206}
207
208/*
209 * Encode post-operation attributes.
210 * The inode may be NULL if the call failed because of a stale file
211 * handle. In this case, no attributes are returned.
212 */
Al Viro91f07162006-10-19 23:28:57 -0700213static __be32 *
214encode_post_op_attr(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215{
216 struct dentry *dentry = fhp->fh_dentry;
NeilBrown072f62e2007-05-09 02:34:57 -0700217 if (dentry && dentry->d_inode) {
David Shawa334de22006-01-06 00:19:58 -0800218 int err;
219 struct kstat stat;
220
Jan Blunck54775492008-02-14 19:38:39 -0800221 err = vfs_getattr(fhp->fh_export->ex_path.mnt, dentry, &stat);
David Shawa334de22006-01-06 00:19:58 -0800222 if (!err) {
223 *p++ = xdr_one; /* attributes follow */
Peter Staubach40ee5dc2007-08-16 12:10:07 -0400224 lease_get_mtime(dentry->d_inode, &stat.mtime);
David Shawa334de22006-01-06 00:19:58 -0800225 return encode_fattr3(rqstp, p, fhp, &stat);
226 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 }
228 *p++ = xdr_zero;
229 return p;
230}
231
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000232/* Helper for NFSv3 ACLs */
Al Viro91f07162006-10-19 23:28:57 -0700233__be32 *
234nfs3svc_encode_post_op_attr(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp)
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000235{
236 return encode_post_op_attr(rqstp, p, fhp);
237}
238
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239/*
240 * Enocde weak cache consistency data
241 */
Al Viro91f07162006-10-19 23:28:57 -0700242static __be32 *
243encode_wcc_data(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
245 struct dentry *dentry = fhp->fh_dentry;
246
247 if (dentry && dentry->d_inode && fhp->fh_post_saved) {
248 if (fhp->fh_pre_saved) {
249 *p++ = xdr_one;
250 p = xdr_encode_hyper(p, (u64) fhp->fh_pre_size);
251 p = encode_time3(p, &fhp->fh_pre_mtime);
252 p = encode_time3(p, &fhp->fh_pre_ctime);
253 } else {
254 *p++ = xdr_zero;
255 }
256 return encode_saved_post_attr(rqstp, p, fhp);
257 }
258 /* no pre- or post-attrs */
259 *p++ = xdr_zero;
260 return encode_post_op_attr(rqstp, p, fhp);
261}
262
Peter Staubach40ee5dc2007-08-16 12:10:07 -0400263/*
264 * Fill in the post_op attr for the wcc data
265 */
266void fill_post_wcc(struct svc_fh *fhp)
267{
268 int err;
269
270 if (fhp->fh_post_saved)
271 printk("nfsd: inode locked twice during operation.\n");
272
Jan Blunck54775492008-02-14 19:38:39 -0800273 err = vfs_getattr(fhp->fh_export->ex_path.mnt, fhp->fh_dentry,
Peter Staubach40ee5dc2007-08-16 12:10:07 -0400274 &fhp->fh_post_attr);
275 if (err)
276 fhp->fh_post_saved = 0;
277 else
278 fhp->fh_post_saved = 1;
279}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
281/*
282 * XDR decode functions
283 */
284int
Al Viro91f07162006-10-19 23:28:57 -0700285nfs3svc_decode_fhandle(struct svc_rqst *rqstp, __be32 *p, struct nfsd_fhandle *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
287 if (!(p = decode_fh(p, &args->fh)))
288 return 0;
289 return xdr_argsize_check(rqstp, p);
290}
291
292int
Al Viro91f07162006-10-19 23:28:57 -0700293nfs3svc_decode_sattrargs(struct svc_rqst *rqstp, __be32 *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 struct nfsd3_sattrargs *args)
295{
NeilBrown072f62e2007-05-09 02:34:57 -0700296 if (!(p = decode_fh(p, &args->fh)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 return 0;
NeilBrown072f62e2007-05-09 02:34:57 -0700298 p = decode_sattr3(p, &args->attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
300 if ((args->check_guard = ntohl(*p++)) != 0) {
301 struct timespec time;
302 p = decode_time3(p, &time);
303 args->guardtime = time.tv_sec;
304 }
305
306 return xdr_argsize_check(rqstp, p);
307}
308
309int
Al Viro91f07162006-10-19 23:28:57 -0700310nfs3svc_decode_diropargs(struct svc_rqst *rqstp, __be32 *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 struct nfsd3_diropargs *args)
312{
313 if (!(p = decode_fh(p, &args->fh))
314 || !(p = decode_filename(p, &args->name, &args->len)))
315 return 0;
316
317 return xdr_argsize_check(rqstp, p);
318}
319
320int
Al Viro91f07162006-10-19 23:28:57 -0700321nfs3svc_decode_accessargs(struct svc_rqst *rqstp, __be32 *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 struct nfsd3_accessargs *args)
323{
324 if (!(p = decode_fh(p, &args->fh)))
325 return 0;
326 args->access = ntohl(*p++);
327
328 return xdr_argsize_check(rqstp, p);
329}
330
331int
Al Viro91f07162006-10-19 23:28:57 -0700332nfs3svc_decode_readargs(struct svc_rqst *rqstp, __be32 *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 struct nfsd3_readargs *args)
334{
335 unsigned int len;
336 int v,pn;
Greg Banks7adae482006-10-04 02:15:47 -0700337 u32 max_blocksize = svc_max_payload(rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
NeilBrown072f62e2007-05-09 02:34:57 -0700339 if (!(p = decode_fh(p, &args->fh)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 return 0;
NeilBrown072f62e2007-05-09 02:34:57 -0700341 p = xdr_decode_hyper(p, &args->offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
343 len = args->count = ntohl(*p++);
344
Greg Banks7adae482006-10-04 02:15:47 -0700345 if (len > max_blocksize)
346 len = max_blocksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
348 /* set up the kvec */
349 v=0;
350 while (len > 0) {
NeilBrown44524352006-10-04 02:15:46 -0700351 pn = rqstp->rq_resused++;
NeilBrown3cc03b12006-10-04 02:15:47 -0700352 rqstp->rq_vec[v].iov_base = page_address(rqstp->rq_respages[pn]);
353 rqstp->rq_vec[v].iov_len = len < PAGE_SIZE? len : PAGE_SIZE;
354 len -= rqstp->rq_vec[v].iov_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 v++;
356 }
357 args->vlen = v;
358 return xdr_argsize_check(rqstp, p);
359}
360
361int
Al Viro91f07162006-10-19 23:28:57 -0700362nfs3svc_decode_writeargs(struct svc_rqst *rqstp, __be32 *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 struct nfsd3_writeargs *args)
364{
Peter Staubachf34b9562007-05-09 02:34:48 -0700365 unsigned int len, v, hdr, dlen;
Greg Banks7adae482006-10-04 02:15:47 -0700366 u32 max_blocksize = svc_max_payload(rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
NeilBrown072f62e2007-05-09 02:34:57 -0700368 if (!(p = decode_fh(p, &args->fh)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 return 0;
NeilBrown072f62e2007-05-09 02:34:57 -0700370 p = xdr_decode_hyper(p, &args->offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
372 args->count = ntohl(*p++);
373 args->stable = ntohl(*p++);
374 len = args->len = ntohl(*p++);
Peter Staubachf34b9562007-05-09 02:34:48 -0700375 /*
376 * The count must equal the amount of data passed.
377 */
378 if (args->count != args->len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 return 0;
380
Peter Staubachf34b9562007-05-09 02:34:48 -0700381 /*
382 * Check to make sure that we got the right number of
383 * bytes.
Peter Staubachf34b9562007-05-09 02:34:48 -0700384 */
385 hdr = (void*)p - rqstp->rq_arg.head[0].iov_base;
NeilBrown072f62e2007-05-09 02:34:57 -0700386 dlen = rqstp->rq_arg.head[0].iov_len + rqstp->rq_arg.page_len
387 - hdr;
Peter Staubachf34b9562007-05-09 02:34:48 -0700388 /*
389 * Round the length of the data which was specified up to
390 * the next multiple of XDR units and then compare that
391 * against the length which was actually received.
NeilBrownba67a392008-01-11 17:06:52 -0500392 * Note that when RPCSEC/GSS (for example) is used, the
393 * data buffer can be padded so dlen might be larger
394 * than required. It must never be smaller.
Peter Staubachf34b9562007-05-09 02:34:48 -0700395 */
NeilBrownba67a392008-01-11 17:06:52 -0500396 if (dlen < XDR_QUADLEN(len)*4)
Peter Staubachf34b9562007-05-09 02:34:48 -0700397 return 0;
398
399 if (args->count > max_blocksize) {
400 args->count = max_blocksize;
401 len = args->len = max_blocksize;
402 }
NeilBrown3cc03b12006-10-04 02:15:47 -0700403 rqstp->rq_vec[0].iov_base = (void*)p;
404 rqstp->rq_vec[0].iov_len = rqstp->rq_arg.head[0].iov_len - hdr;
Peter Staubachf34b9562007-05-09 02:34:48 -0700405 v = 0;
NeilBrown3cc03b12006-10-04 02:15:47 -0700406 while (len > rqstp->rq_vec[v].iov_len) {
407 len -= rqstp->rq_vec[v].iov_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 v++;
NeilBrown3cc03b12006-10-04 02:15:47 -0700409 rqstp->rq_vec[v].iov_base = page_address(rqstp->rq_pages[v]);
410 rqstp->rq_vec[v].iov_len = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 }
NeilBrown3cc03b12006-10-04 02:15:47 -0700412 rqstp->rq_vec[v].iov_len = len;
Peter Staubachf34b9562007-05-09 02:34:48 -0700413 args->vlen = v + 1;
414 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415}
416
417int
Al Viro91f07162006-10-19 23:28:57 -0700418nfs3svc_decode_createargs(struct svc_rqst *rqstp, __be32 *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 struct nfsd3_createargs *args)
420{
421 if (!(p = decode_fh(p, &args->fh))
422 || !(p = decode_filename(p, &args->name, &args->len)))
423 return 0;
424
425 switch (args->createmode = ntohl(*p++)) {
426 case NFS3_CREATE_UNCHECKED:
427 case NFS3_CREATE_GUARDED:
NeilBrown072f62e2007-05-09 02:34:57 -0700428 p = decode_sattr3(p, &args->attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 break;
430 case NFS3_CREATE_EXCLUSIVE:
431 args->verf = p;
432 p += 2;
433 break;
434 default:
435 return 0;
436 }
437
438 return xdr_argsize_check(rqstp, p);
439}
440int
Al Viro91f07162006-10-19 23:28:57 -0700441nfs3svc_decode_mkdirargs(struct svc_rqst *rqstp, __be32 *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 struct nfsd3_createargs *args)
443{
NeilBrown072f62e2007-05-09 02:34:57 -0700444 if (!(p = decode_fh(p, &args->fh)) ||
445 !(p = decode_filename(p, &args->name, &args->len)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 return 0;
NeilBrown072f62e2007-05-09 02:34:57 -0700447 p = decode_sattr3(p, &args->attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
449 return xdr_argsize_check(rqstp, p);
450}
451
452int
Al Viro91f07162006-10-19 23:28:57 -0700453nfs3svc_decode_symlinkargs(struct svc_rqst *rqstp, __be32 *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 struct nfsd3_symlinkargs *args)
455{
Chuck Levera628f662007-11-01 16:57:20 -0400456 unsigned int len, avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 char *old, *new;
458 struct kvec *vec;
459
NeilBrown072f62e2007-05-09 02:34:57 -0700460 if (!(p = decode_fh(p, &args->ffh)) ||
461 !(p = decode_filename(p, &args->fname, &args->flen))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 )
463 return 0;
NeilBrown072f62e2007-05-09 02:34:57 -0700464 p = decode_sattr3(p, &args->attrs);
465
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 /* now decode the pathname, which might be larger than the first page.
467 * As we have to check for nul's anyway, we copy it into a new page
468 * This page appears in the rq_res.pages list, but as pages_len is always
469 * 0, it won't get in the way
470 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 len = ntohl(*p++);
472 if (len == 0 || len > NFS3_MAXPATHLEN || len >= PAGE_SIZE)
473 return 0;
NeilBrown44524352006-10-04 02:15:46 -0700474 args->tname = new =
475 page_address(rqstp->rq_respages[rqstp->rq_resused++]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 args->tlen = len;
477 /* first copy and check from the first page */
478 old = (char*)p;
479 vec = &rqstp->rq_arg.head[0];
480 avail = vec->iov_len - (old - (char*)vec->iov_base);
481 while (len && avail && *old) {
482 *new++ = *old++;
483 len--;
484 avail--;
485 }
486 /* now copy next page if there is one */
487 if (len && !avail && rqstp->rq_arg.page_len) {
488 avail = rqstp->rq_arg.page_len;
Chuck Levera628f662007-11-01 16:57:20 -0400489 if (avail > PAGE_SIZE)
490 avail = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 old = page_address(rqstp->rq_arg.pages[0]);
492 }
493 while (len && avail && *old) {
494 *new++ = *old++;
495 len--;
496 avail--;
497 }
498 *new = '\0';
499 if (len)
500 return 0;
501
502 return 1;
503}
504
505int
Al Viro91f07162006-10-19 23:28:57 -0700506nfs3svc_decode_mknodargs(struct svc_rqst *rqstp, __be32 *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 struct nfsd3_mknodargs *args)
508{
509 if (!(p = decode_fh(p, &args->fh))
510 || !(p = decode_filename(p, &args->name, &args->len)))
511 return 0;
512
513 args->ftype = ntohl(*p++);
514
515 if (args->ftype == NF3BLK || args->ftype == NF3CHR
NeilBrown072f62e2007-05-09 02:34:57 -0700516 || args->ftype == NF3SOCK || args->ftype == NF3FIFO)
517 p = decode_sattr3(p, &args->attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
519 if (args->ftype == NF3BLK || args->ftype == NF3CHR) {
520 args->major = ntohl(*p++);
521 args->minor = ntohl(*p++);
522 }
523
524 return xdr_argsize_check(rqstp, p);
525}
526
527int
Al Viro91f07162006-10-19 23:28:57 -0700528nfs3svc_decode_renameargs(struct svc_rqst *rqstp, __be32 *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 struct nfsd3_renameargs *args)
530{
531 if (!(p = decode_fh(p, &args->ffh))
532 || !(p = decode_filename(p, &args->fname, &args->flen))
533 || !(p = decode_fh(p, &args->tfh))
534 || !(p = decode_filename(p, &args->tname, &args->tlen)))
535 return 0;
536
537 return xdr_argsize_check(rqstp, p);
538}
539
540int
Al Viro91f07162006-10-19 23:28:57 -0700541nfs3svc_decode_readlinkargs(struct svc_rqst *rqstp, __be32 *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 struct nfsd3_readlinkargs *args)
543{
544 if (!(p = decode_fh(p, &args->fh)))
545 return 0;
NeilBrown44524352006-10-04 02:15:46 -0700546 args->buffer =
547 page_address(rqstp->rq_respages[rqstp->rq_resused++]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
549 return xdr_argsize_check(rqstp, p);
550}
551
552int
Al Viro91f07162006-10-19 23:28:57 -0700553nfs3svc_decode_linkargs(struct svc_rqst *rqstp, __be32 *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 struct nfsd3_linkargs *args)
555{
556 if (!(p = decode_fh(p, &args->ffh))
557 || !(p = decode_fh(p, &args->tfh))
558 || !(p = decode_filename(p, &args->tname, &args->tlen)))
559 return 0;
560
561 return xdr_argsize_check(rqstp, p);
562}
563
564int
Al Viro91f07162006-10-19 23:28:57 -0700565nfs3svc_decode_readdirargs(struct svc_rqst *rqstp, __be32 *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 struct nfsd3_readdirargs *args)
567{
568 if (!(p = decode_fh(p, &args->fh)))
569 return 0;
570 p = xdr_decode_hyper(p, &args->cookie);
571 args->verf = p; p += 2;
572 args->dircount = ~0;
573 args->count = ntohl(*p++);
574
575 if (args->count > PAGE_SIZE)
576 args->count = PAGE_SIZE;
577
NeilBrown44524352006-10-04 02:15:46 -0700578 args->buffer =
579 page_address(rqstp->rq_respages[rqstp->rq_resused++]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
581 return xdr_argsize_check(rqstp, p);
582}
583
584int
Al Viro91f07162006-10-19 23:28:57 -0700585nfs3svc_decode_readdirplusargs(struct svc_rqst *rqstp, __be32 *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 struct nfsd3_readdirargs *args)
587{
588 int len, pn;
Greg Banks7adae482006-10-04 02:15:47 -0700589 u32 max_blocksize = svc_max_payload(rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
591 if (!(p = decode_fh(p, &args->fh)))
592 return 0;
593 p = xdr_decode_hyper(p, &args->cookie);
594 args->verf = p; p += 2;
595 args->dircount = ntohl(*p++);
596 args->count = ntohl(*p++);
597
Greg Banks7adae482006-10-04 02:15:47 -0700598 len = (args->count > max_blocksize) ? max_blocksize :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 args->count;
600 args->count = len;
601
602 while (len > 0) {
NeilBrown44524352006-10-04 02:15:46 -0700603 pn = rqstp->rq_resused++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 if (!args->buffer)
605 args->buffer = page_address(rqstp->rq_respages[pn]);
606 len -= PAGE_SIZE;
607 }
608
609 return xdr_argsize_check(rqstp, p);
610}
611
612int
Al Viro91f07162006-10-19 23:28:57 -0700613nfs3svc_decode_commitargs(struct svc_rqst *rqstp, __be32 *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 struct nfsd3_commitargs *args)
615{
616 if (!(p = decode_fh(p, &args->fh)))
617 return 0;
618 p = xdr_decode_hyper(p, &args->offset);
619 args->count = ntohl(*p++);
620
621 return xdr_argsize_check(rqstp, p);
622}
623
624/*
625 * XDR encode functions
626 */
627/*
628 * There must be an encoding function for void results so svc_process
629 * will work properly.
630 */
631int
Al Viro91f07162006-10-19 23:28:57 -0700632nfs3svc_encode_voidres(struct svc_rqst *rqstp, __be32 *p, void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633{
634 return xdr_ressize_check(rqstp, p);
635}
636
637/* GETATTR */
638int
Al Viro91f07162006-10-19 23:28:57 -0700639nfs3svc_encode_attrstat(struct svc_rqst *rqstp, __be32 *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 struct nfsd3_attrstat *resp)
641{
Peter Staubach40ee5dc2007-08-16 12:10:07 -0400642 if (resp->status == 0) {
643 lease_get_mtime(resp->fh.fh_dentry->d_inode,
644 &resp->stat.mtime);
David Shawa334de22006-01-06 00:19:58 -0800645 p = encode_fattr3(rqstp, p, &resp->fh, &resp->stat);
Peter Staubach40ee5dc2007-08-16 12:10:07 -0400646 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 return xdr_ressize_check(rqstp, p);
648}
649
650/* SETATTR, REMOVE, RMDIR */
651int
Al Viro91f07162006-10-19 23:28:57 -0700652nfs3svc_encode_wccstat(struct svc_rqst *rqstp, __be32 *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 struct nfsd3_attrstat *resp)
654{
655 p = encode_wcc_data(rqstp, p, &resp->fh);
656 return xdr_ressize_check(rqstp, p);
657}
658
659/* LOOKUP */
660int
Al Viro91f07162006-10-19 23:28:57 -0700661nfs3svc_encode_diropres(struct svc_rqst *rqstp, __be32 *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 struct nfsd3_diropres *resp)
663{
664 if (resp->status == 0) {
665 p = encode_fh(p, &resp->fh);
666 p = encode_post_op_attr(rqstp, p, &resp->fh);
667 }
668 p = encode_post_op_attr(rqstp, p, &resp->dirfh);
669 return xdr_ressize_check(rqstp, p);
670}
671
672/* ACCESS */
673int
Al Viro91f07162006-10-19 23:28:57 -0700674nfs3svc_encode_accessres(struct svc_rqst *rqstp, __be32 *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 struct nfsd3_accessres *resp)
676{
677 p = encode_post_op_attr(rqstp, p, &resp->fh);
678 if (resp->status == 0)
679 *p++ = htonl(resp->access);
680 return xdr_ressize_check(rqstp, p);
681}
682
683/* READLINK */
684int
Al Viro91f07162006-10-19 23:28:57 -0700685nfs3svc_encode_readlinkres(struct svc_rqst *rqstp, __be32 *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 struct nfsd3_readlinkres *resp)
687{
688 p = encode_post_op_attr(rqstp, p, &resp->fh);
689 if (resp->status == 0) {
690 *p++ = htonl(resp->len);
691 xdr_ressize_check(rqstp, p);
692 rqstp->rq_res.page_len = resp->len;
693 if (resp->len & 3) {
694 /* need to pad the tail */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 rqstp->rq_res.tail[0].iov_base = p;
696 *p = 0;
697 rqstp->rq_res.tail[0].iov_len = 4 - (resp->len&3);
698 }
699 return 1;
700 } else
701 return xdr_ressize_check(rqstp, p);
702}
703
704/* READ */
705int
Al Viro91f07162006-10-19 23:28:57 -0700706nfs3svc_encode_readres(struct svc_rqst *rqstp, __be32 *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 struct nfsd3_readres *resp)
708{
709 p = encode_post_op_attr(rqstp, p, &resp->fh);
710 if (resp->status == 0) {
711 *p++ = htonl(resp->count);
712 *p++ = htonl(resp->eof);
713 *p++ = htonl(resp->count); /* xdr opaque count */
714 xdr_ressize_check(rqstp, p);
715 /* now update rqstp->rq_res to reflect data aswell */
716 rqstp->rq_res.page_len = resp->count;
717 if (resp->count & 3) {
718 /* need to pad the tail */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 rqstp->rq_res.tail[0].iov_base = p;
720 *p = 0;
721 rqstp->rq_res.tail[0].iov_len = 4 - (resp->count & 3);
722 }
723 return 1;
724 } else
725 return xdr_ressize_check(rqstp, p);
726}
727
728/* WRITE */
729int
Al Viro91f07162006-10-19 23:28:57 -0700730nfs3svc_encode_writeres(struct svc_rqst *rqstp, __be32 *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 struct nfsd3_writeres *resp)
732{
733 p = encode_wcc_data(rqstp, p, &resp->fh);
734 if (resp->status == 0) {
735 *p++ = htonl(resp->count);
736 *p++ = htonl(resp->committed);
737 *p++ = htonl(nfssvc_boot.tv_sec);
738 *p++ = htonl(nfssvc_boot.tv_usec);
739 }
740 return xdr_ressize_check(rqstp, p);
741}
742
743/* CREATE, MKDIR, SYMLINK, MKNOD */
744int
Al Viro91f07162006-10-19 23:28:57 -0700745nfs3svc_encode_createres(struct svc_rqst *rqstp, __be32 *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 struct nfsd3_diropres *resp)
747{
748 if (resp->status == 0) {
749 *p++ = xdr_one;
750 p = encode_fh(p, &resp->fh);
751 p = encode_post_op_attr(rqstp, p, &resp->fh);
752 }
753 p = encode_wcc_data(rqstp, p, &resp->dirfh);
754 return xdr_ressize_check(rqstp, p);
755}
756
757/* RENAME */
758int
Al Viro91f07162006-10-19 23:28:57 -0700759nfs3svc_encode_renameres(struct svc_rqst *rqstp, __be32 *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 struct nfsd3_renameres *resp)
761{
762 p = encode_wcc_data(rqstp, p, &resp->ffh);
763 p = encode_wcc_data(rqstp, p, &resp->tfh);
764 return xdr_ressize_check(rqstp, p);
765}
766
767/* LINK */
768int
Al Viro91f07162006-10-19 23:28:57 -0700769nfs3svc_encode_linkres(struct svc_rqst *rqstp, __be32 *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 struct nfsd3_linkres *resp)
771{
772 p = encode_post_op_attr(rqstp, p, &resp->fh);
773 p = encode_wcc_data(rqstp, p, &resp->tfh);
774 return xdr_ressize_check(rqstp, p);
775}
776
777/* READDIR */
778int
Al Viro91f07162006-10-19 23:28:57 -0700779nfs3svc_encode_readdirres(struct svc_rqst *rqstp, __be32 *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 struct nfsd3_readdirres *resp)
781{
782 p = encode_post_op_attr(rqstp, p, &resp->fh);
783
784 if (resp->status == 0) {
785 /* stupid readdir cookie */
786 memcpy(p, resp->verf, 8); p += 2;
787 xdr_ressize_check(rqstp, p);
788 if (rqstp->rq_res.head[0].iov_len + (2<<2) > PAGE_SIZE)
789 return 1; /*No room for trailer */
790 rqstp->rq_res.page_len = (resp->count) << 2;
791
792 /* add the 'tail' to the end of the 'head' page - page 0. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 rqstp->rq_res.tail[0].iov_base = p;
794 *p++ = 0; /* no more entries */
795 *p++ = htonl(resp->common.err == nfserr_eof);
796 rqstp->rq_res.tail[0].iov_len = 2<<2;
797 return 1;
798 } else
799 return xdr_ressize_check(rqstp, p);
800}
801
Adrian Bunk3ee6f612006-12-06 20:40:23 -0800802static __be32 *
Al Viro91f07162006-10-19 23:28:57 -0700803encode_entry_baggage(struct nfsd3_readdirres *cd, __be32 *p, const char *name,
Peter Staubach40ee5dc2007-08-16 12:10:07 -0400804 int namlen, u64 ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805{
806 *p++ = xdr_one; /* mark entry present */
807 p = xdr_encode_hyper(p, ino); /* file id */
808 p = xdr_encode_array(p, name, namlen);/* name length & name */
809
810 cd->offset = p; /* remember pointer */
811 p = xdr_encode_hyper(p, NFS_OFFSET_MAX);/* offset of next entry */
812
813 return p;
814}
815
Adrian Bunk3ee6f612006-12-06 20:40:23 -0800816static __be32 *
Al Viro91f07162006-10-19 23:28:57 -0700817encode_entryplus_baggage(struct nfsd3_readdirres *cd, __be32 *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 struct svc_fh *fhp)
819{
J. Bruce Fields99d965e2007-11-21 14:10:07 -0500820 p = encode_post_op_attr(cd->rqstp, p, fhp);
821 *p++ = xdr_one; /* yes, a file handle follows */
822 p = encode_fh(p, fhp);
823 fh_put(fhp);
824 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825}
826
827static int
828compose_entry_fh(struct nfsd3_readdirres *cd, struct svc_fh *fhp,
829 const char *name, int namlen)
830{
831 struct svc_export *exp;
832 struct dentry *dparent, *dchild;
833 int rv = 0;
834
835 dparent = cd->fh.fh_dentry;
836 exp = cd->fh.fh_export;
837
838 fh_init(fhp, NFS3_FHSIZE);
839 if (isdotent(name, namlen)) {
840 if (namlen == 2) {
841 dchild = dget_parent(dparent);
842 if (dchild == dparent) {
843 /* filesystem root - cannot return filehandle for ".." */
844 dput(dchild);
845 return 1;
846 }
847 } else
848 dchild = dget(dparent);
849 } else
850 dchild = lookup_one_len(name, dparent, namlen);
851 if (IS_ERR(dchild))
852 return 1;
853 if (d_mountpoint(dchild) ||
854 fh_compose(fhp, exp, dchild, &cd->fh) != 0 ||
855 !dchild->d_inode)
856 rv = 1;
857 dput(dchild);
858 return rv;
859}
860
861/*
862 * Encode a directory entry. This one works for both normal readdir
863 * and readdirplus.
864 * The normal readdir reply requires 2 (fileid) + 1 (stringlen)
865 * + string + 2 (cookie) + 1 (next) words, i.e. 6 + strlen.
866 *
867 * The readdirplus baggage is 1+21 words for post_op_attr, plus the
868 * file handle.
869 */
870
871#define NFS3_ENTRY_BAGGAGE (2 + 1 + 2 + 1)
872#define NFS3_ENTRYPLUS_BAGGAGE (1 + 21 + 1 + (NFS3_FHSIZE >> 2))
873static int
NeilBrown598b9a52007-03-26 21:32:08 -0800874encode_entry(struct readdir_cd *ccd, const char *name, int namlen,
Peter Staubach40ee5dc2007-08-16 12:10:07 -0400875 loff_t offset, u64 ino, unsigned int d_type, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876{
877 struct nfsd3_readdirres *cd = container_of(ccd, struct nfsd3_readdirres,
878 common);
Al Viro91f07162006-10-19 23:28:57 -0700879 __be32 *p = cd->buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 caddr_t curr_page_addr = NULL;
881 int pn; /* current page number */
882 int slen; /* string (name) length */
883 int elen; /* estimated entry length in words */
884 int num_entry_words = 0; /* actual number of words */
885
886 if (cd->offset) {
887 u64 offset64 = offset;
888
889 if (unlikely(cd->offset1)) {
890 /* we ended up with offset on a page boundary */
891 *cd->offset = htonl(offset64 >> 32);
892 *cd->offset1 = htonl(offset64 & 0xffffffff);
893 cd->offset1 = NULL;
894 } else {
NeilBrown598b9a52007-03-26 21:32:08 -0800895 xdr_encode_hyper(cd->offset, offset64);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 }
897 }
898
899 /*
900 dprintk("encode_entry(%.*s @%ld%s)\n",
901 namlen, name, (long) offset, plus? " plus" : "");
902 */
903
904 /* truncate filename if too long */
905 if (namlen > NFS3_MAXNAMLEN)
906 namlen = NFS3_MAXNAMLEN;
907
908 slen = XDR_QUADLEN(namlen);
909 elen = slen + NFS3_ENTRY_BAGGAGE
910 + (plus? NFS3_ENTRYPLUS_BAGGAGE : 0);
911
912 if (cd->buflen < elen) {
913 cd->common.err = nfserr_toosmall;
914 return -EINVAL;
915 }
916
917 /* determine which page in rq_respages[] we are currently filling */
918 for (pn=1; pn < cd->rqstp->rq_resused; pn++) {
919 curr_page_addr = page_address(cd->rqstp->rq_respages[pn]);
920
921 if (((caddr_t)cd->buffer >= curr_page_addr) &&
922 ((caddr_t)cd->buffer < curr_page_addr + PAGE_SIZE))
923 break;
924 }
925
926 if ((caddr_t)(cd->buffer + elen) < (curr_page_addr + PAGE_SIZE)) {
927 /* encode entry in current page */
928
929 p = encode_entry_baggage(cd, p, name, namlen, ino);
930
931 /* throw in readdirplus baggage */
932 if (plus) {
933 struct svc_fh fh;
934
935 if (compose_entry_fh(cd, &fh, name, namlen) > 0) {
936 *p++ = 0;
937 *p++ = 0;
938 } else
939 p = encode_entryplus_baggage(cd, p, &fh);
940 }
941 num_entry_words = p - cd->buffer;
942 } else if (cd->rqstp->rq_respages[pn+1] != NULL) {
943 /* temporarily encode entry into next page, then move back to
944 * current and next page in rq_respages[] */
Al Viro91f07162006-10-19 23:28:57 -0700945 __be32 *p1, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 int len1, len2;
947
948 /* grab next page for temporary storage of entry */
949 p1 = tmp = page_address(cd->rqstp->rq_respages[pn+1]);
950
951 p1 = encode_entry_baggage(cd, p1, name, namlen, ino);
952
953 /* throw in readdirplus baggage */
954 if (plus) {
955 struct svc_fh fh;
956
957 if (compose_entry_fh(cd, &fh, name, namlen) > 0) {
958 /* zero out the filehandle */
959 *p1++ = 0;
960 *p1++ = 0;
961 } else
962 p1 = encode_entryplus_baggage(cd, p1, &fh);
963 }
964
965 /* determine entry word length and lengths to go in pages */
966 num_entry_words = p1 - tmp;
967 len1 = curr_page_addr + PAGE_SIZE - (caddr_t)cd->buffer;
968 if ((num_entry_words << 2) < len1) {
969 /* the actual number of words in the entry is less
970 * than elen and can still fit in the current page
971 */
972 memmove(p, tmp, num_entry_words << 2);
973 p += num_entry_words;
974
975 /* update offset */
976 cd->offset = cd->buffer + (cd->offset - tmp);
977 } else {
978 unsigned int offset_r = (cd->offset - tmp) << 2;
979
980 /* update pointer to offset location.
981 * This is a 64bit quantity, so we need to
982 * deal with 3 cases:
983 * - entirely in first page
984 * - entirely in second page
985 * - 4 bytes in each page
986 */
987 if (offset_r + 8 <= len1) {
988 cd->offset = p + (cd->offset - tmp);
989 } else if (offset_r >= len1) {
990 cd->offset -= len1 >> 2;
991 } else {
992 /* sitting on the fence */
993 BUG_ON(offset_r != len1 - 4);
994 cd->offset = p + (cd->offset - tmp);
995 cd->offset1 = tmp;
996 }
997
998 len2 = (num_entry_words << 2) - len1;
999
1000 /* move from temp page to current and next pages */
1001 memmove(p, tmp, len1);
1002 memmove(tmp, (caddr_t)tmp+len1, len2);
1003
1004 p = tmp + (len2 >> 2);
1005 }
1006 }
1007 else {
1008 cd->common.err = nfserr_toosmall;
1009 return -EINVAL;
1010 }
1011
1012 cd->buflen -= num_entry_words;
1013 cd->buffer = p;
1014 cd->common.err = nfs_ok;
1015 return 0;
1016
1017}
1018
1019int
NeilBrowna0ad13e2007-01-26 00:57:10 -08001020nfs3svc_encode_entry(void *cd, const char *name,
1021 int namlen, loff_t offset, u64 ino, unsigned int d_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022{
1023 return encode_entry(cd, name, namlen, offset, ino, d_type, 0);
1024}
1025
1026int
NeilBrowna0ad13e2007-01-26 00:57:10 -08001027nfs3svc_encode_entry_plus(void *cd, const char *name,
1028 int namlen, loff_t offset, u64 ino,
1029 unsigned int d_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030{
1031 return encode_entry(cd, name, namlen, offset, ino, d_type, 1);
1032}
1033
1034/* FSSTAT */
1035int
Al Viro91f07162006-10-19 23:28:57 -07001036nfs3svc_encode_fsstatres(struct svc_rqst *rqstp, __be32 *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 struct nfsd3_fsstatres *resp)
1038{
1039 struct kstatfs *s = &resp->stats;
1040 u64 bs = s->f_bsize;
1041
1042 *p++ = xdr_zero; /* no post_op_attr */
1043
1044 if (resp->status == 0) {
1045 p = xdr_encode_hyper(p, bs * s->f_blocks); /* total bytes */
1046 p = xdr_encode_hyper(p, bs * s->f_bfree); /* free bytes */
1047 p = xdr_encode_hyper(p, bs * s->f_bavail); /* user available bytes */
1048 p = xdr_encode_hyper(p, s->f_files); /* total inodes */
1049 p = xdr_encode_hyper(p, s->f_ffree); /* free inodes */
1050 p = xdr_encode_hyper(p, s->f_ffree); /* user available inodes */
1051 *p++ = htonl(resp->invarsec); /* mean unchanged time */
1052 }
1053 return xdr_ressize_check(rqstp, p);
1054}
1055
1056/* FSINFO */
1057int
Al Viro91f07162006-10-19 23:28:57 -07001058nfs3svc_encode_fsinfores(struct svc_rqst *rqstp, __be32 *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 struct nfsd3_fsinfores *resp)
1060{
1061 *p++ = xdr_zero; /* no post_op_attr */
1062
1063 if (resp->status == 0) {
1064 *p++ = htonl(resp->f_rtmax);
1065 *p++ = htonl(resp->f_rtpref);
1066 *p++ = htonl(resp->f_rtmult);
1067 *p++ = htonl(resp->f_wtmax);
1068 *p++ = htonl(resp->f_wtpref);
1069 *p++ = htonl(resp->f_wtmult);
1070 *p++ = htonl(resp->f_dtpref);
1071 p = xdr_encode_hyper(p, resp->f_maxfilesize);
1072 *p++ = xdr_one;
1073 *p++ = xdr_zero;
1074 *p++ = htonl(resp->f_properties);
1075 }
1076
1077 return xdr_ressize_check(rqstp, p);
1078}
1079
1080/* PATHCONF */
1081int
Al Viro91f07162006-10-19 23:28:57 -07001082nfs3svc_encode_pathconfres(struct svc_rqst *rqstp, __be32 *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 struct nfsd3_pathconfres *resp)
1084{
1085 *p++ = xdr_zero; /* no post_op_attr */
1086
1087 if (resp->status == 0) {
1088 *p++ = htonl(resp->p_link_max);
1089 *p++ = htonl(resp->p_name_max);
1090 *p++ = htonl(resp->p_no_trunc);
1091 *p++ = htonl(resp->p_chown_restricted);
1092 *p++ = htonl(resp->p_case_insensitive);
1093 *p++ = htonl(resp->p_case_preserving);
1094 }
1095
1096 return xdr_ressize_check(rqstp, p);
1097}
1098
1099/* COMMIT */
1100int
Al Viro91f07162006-10-19 23:28:57 -07001101nfs3svc_encode_commitres(struct svc_rqst *rqstp, __be32 *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 struct nfsd3_commitres *resp)
1103{
1104 p = encode_wcc_data(rqstp, p, &resp->fh);
1105 /* Write verifier */
1106 if (resp->status == 0) {
1107 *p++ = htonl(nfssvc_boot.tv_sec);
1108 *p++ = htonl(nfssvc_boot.tv_usec);
1109 }
1110 return xdr_ressize_check(rqstp, p);
1111}
1112
1113/*
1114 * XDR release functions
1115 */
1116int
Al Viro91f07162006-10-19 23:28:57 -07001117nfs3svc_release_fhandle(struct svc_rqst *rqstp, __be32 *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 struct nfsd3_attrstat *resp)
1119{
1120 fh_put(&resp->fh);
1121 return 1;
1122}
1123
1124int
Al Viro91f07162006-10-19 23:28:57 -07001125nfs3svc_release_fhandle2(struct svc_rqst *rqstp, __be32 *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 struct nfsd3_fhandle_pair *resp)
1127{
1128 fh_put(&resp->fh1);
1129 fh_put(&resp->fh2);
1130 return 1;
1131}