blob: 247d518248bf71b2a95b5ce04b460374dd88c51f [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>
24
25#define NFSDDBG_FACILITY NFSDDBG_XDR
26
27#ifdef NFSD_OPTIMIZE_SPACE
28# define inline
29#endif
30
31
32/*
33 * Mapping of S_IF* types to NFS file types
34 */
35static u32 nfs3_ftypes[] = {
36 NF3NON, NF3FIFO, NF3CHR, NF3BAD,
37 NF3DIR, NF3BAD, NF3BLK, NF3BAD,
38 NF3REG, NF3BAD, NF3LNK, NF3BAD,
39 NF3SOCK, NF3BAD, NF3LNK, NF3BAD,
40};
41
42/*
43 * XDR functions for basic NFS types
44 */
45static inline u32 *
46encode_time3(u32 *p, struct timespec *time)
47{
48 *p++ = htonl((u32) time->tv_sec); *p++ = htonl(time->tv_nsec);
49 return p;
50}
51
52static inline u32 *
53decode_time3(u32 *p, struct timespec *time)
54{
55 time->tv_sec = ntohl(*p++);
56 time->tv_nsec = ntohl(*p++);
57 return p;
58}
59
60static inline u32 *
61decode_fh(u32 *p, struct svc_fh *fhp)
62{
63 unsigned int size;
64 fh_init(fhp, NFS3_FHSIZE);
65 size = ntohl(*p++);
66 if (size > NFS3_FHSIZE)
67 return NULL;
68
69 memcpy(&fhp->fh_handle.fh_base, p, size);
70 fhp->fh_handle.fh_size = size;
71 return p + XDR_QUADLEN(size);
72}
73
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +000074/* Helper function for NFSv3 ACL code */
75u32 *nfs3svc_decode_fh(u32 *p, struct svc_fh *fhp)
76{
77 return decode_fh(p, fhp);
78}
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080static inline u32 *
81encode_fh(u32 *p, struct svc_fh *fhp)
82{
83 unsigned int size = fhp->fh_handle.fh_size;
84 *p++ = htonl(size);
85 if (size) p[XDR_QUADLEN(size)-1]=0;
86 memcpy(p, &fhp->fh_handle.fh_base, size);
87 return p + XDR_QUADLEN(size);
88}
89
90/*
91 * Decode a file name and make sure that the path contains
92 * no slashes or null bytes.
93 */
94static inline u32 *
95decode_filename(u32 *p, char **namp, int *lenp)
96{
97 char *name;
98 int i;
99
100 if ((p = xdr_decode_string_inplace(p, namp, lenp, NFS3_MAXNAMLEN)) != NULL) {
101 for (i = 0, name = *namp; i < *lenp; i++, name++) {
102 if (*name == '\0' || *name == '/')
103 return NULL;
104 }
105 }
106
107 return p;
108}
109
110static inline u32 *
111decode_sattr3(u32 *p, struct iattr *iap)
112{
113 u32 tmp;
114
115 iap->ia_valid = 0;
116
117 if (*p++) {
118 iap->ia_valid |= ATTR_MODE;
119 iap->ia_mode = ntohl(*p++);
120 }
121 if (*p++) {
122 iap->ia_valid |= ATTR_UID;
123 iap->ia_uid = ntohl(*p++);
124 }
125 if (*p++) {
126 iap->ia_valid |= ATTR_GID;
127 iap->ia_gid = ntohl(*p++);
128 }
129 if (*p++) {
130 u64 newsize;
131
132 iap->ia_valid |= ATTR_SIZE;
133 p = xdr_decode_hyper(p, &newsize);
134 if (newsize <= NFS_OFFSET_MAX)
135 iap->ia_size = newsize;
136 else
137 iap->ia_size = NFS_OFFSET_MAX;
138 }
139 if ((tmp = ntohl(*p++)) == 1) { /* set to server time */
140 iap->ia_valid |= ATTR_ATIME;
141 } else if (tmp == 2) { /* set to client time */
142 iap->ia_valid |= ATTR_ATIME | ATTR_ATIME_SET;
143 iap->ia_atime.tv_sec = ntohl(*p++);
144 iap->ia_atime.tv_nsec = ntohl(*p++);
145 }
146 if ((tmp = ntohl(*p++)) == 1) { /* set to server time */
147 iap->ia_valid |= ATTR_MTIME;
148 } else if (tmp == 2) { /* set to client time */
149 iap->ia_valid |= ATTR_MTIME | ATTR_MTIME_SET;
150 iap->ia_mtime.tv_sec = ntohl(*p++);
151 iap->ia_mtime.tv_nsec = ntohl(*p++);
152 }
153 return p;
154}
155
156static inline u32 *
David Shawa334de22006-01-06 00:19:58 -0800157encode_fattr3(struct svc_rqst *rqstp, u32 *p, struct svc_fh *fhp,
158 struct kstat *stat)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 struct dentry *dentry = fhp->fh_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 struct timespec time;
162
David Shawa334de22006-01-06 00:19:58 -0800163 *p++ = htonl(nfs3_ftypes[(stat->mode & S_IFMT) >> 12]);
164 *p++ = htonl((u32) stat->mode);
165 *p++ = htonl((u32) stat->nlink);
166 *p++ = htonl((u32) nfsd_ruid(rqstp, stat->uid));
167 *p++ = htonl((u32) nfsd_rgid(rqstp, stat->gid));
168 if (S_ISLNK(stat->mode) && stat->size > NFS3_MAXPATHLEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 p = xdr_encode_hyper(p, (u64) NFS3_MAXPATHLEN);
170 } else {
David Shawa334de22006-01-06 00:19:58 -0800171 p = xdr_encode_hyper(p, (u64) stat->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 }
David Shawa334de22006-01-06 00:19:58 -0800173 p = xdr_encode_hyper(p, ((u64)stat->blocks) << 9);
174 *p++ = htonl((u32) MAJOR(stat->rdev));
175 *p++ = htonl((u32) MINOR(stat->rdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 if (is_fsid(fhp, rqstp->rq_reffh))
177 p = xdr_encode_hyper(p, (u64) fhp->fh_export->ex_fsid);
178 else
David Shawa334de22006-01-06 00:19:58 -0800179 p = xdr_encode_hyper(p, (u64) huge_encode_dev(stat->dev));
180 p = xdr_encode_hyper(p, (u64) stat->ino);
181 p = encode_time3(p, &stat->atime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 lease_get_mtime(dentry->d_inode, &time);
183 p = encode_time3(p, &time);
David Shawa334de22006-01-06 00:19:58 -0800184 p = encode_time3(p, &stat->ctime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
186 return p;
187}
188
189static inline u32 *
190encode_saved_post_attr(struct svc_rqst *rqstp, u32 *p, struct svc_fh *fhp)
191{
192 struct inode *inode = fhp->fh_dentry->d_inode;
193
194 /* Attributes to follow */
195 *p++ = xdr_one;
196
197 *p++ = htonl(nfs3_ftypes[(fhp->fh_post_mode & S_IFMT) >> 12]);
198 *p++ = htonl((u32) fhp->fh_post_mode);
199 *p++ = htonl((u32) fhp->fh_post_nlink);
200 *p++ = htonl((u32) nfsd_ruid(rqstp, fhp->fh_post_uid));
201 *p++ = htonl((u32) nfsd_rgid(rqstp, fhp->fh_post_gid));
202 if (S_ISLNK(fhp->fh_post_mode) && fhp->fh_post_size > NFS3_MAXPATHLEN) {
203 p = xdr_encode_hyper(p, (u64) NFS3_MAXPATHLEN);
204 } else {
205 p = xdr_encode_hyper(p, (u64) fhp->fh_post_size);
206 }
207 p = xdr_encode_hyper(p, ((u64)fhp->fh_post_blocks) << 9);
208 *p++ = fhp->fh_post_rdev[0];
209 *p++ = fhp->fh_post_rdev[1];
210 if (is_fsid(fhp, rqstp->rq_reffh))
211 p = xdr_encode_hyper(p, (u64) fhp->fh_export->ex_fsid);
212 else
213 p = xdr_encode_hyper(p, (u64)huge_encode_dev(inode->i_sb->s_dev));
214 p = xdr_encode_hyper(p, (u64) inode->i_ino);
215 p = encode_time3(p, &fhp->fh_post_atime);
216 p = encode_time3(p, &fhp->fh_post_mtime);
217 p = encode_time3(p, &fhp->fh_post_ctime);
218
219 return p;
220}
221
222/*
223 * Encode post-operation attributes.
224 * The inode may be NULL if the call failed because of a stale file
225 * handle. In this case, no attributes are returned.
226 */
227static u32 *
228encode_post_op_attr(struct svc_rqst *rqstp, u32 *p, struct svc_fh *fhp)
229{
230 struct dentry *dentry = fhp->fh_dentry;
231 if (dentry && dentry->d_inode != NULL) {
David Shawa334de22006-01-06 00:19:58 -0800232 int err;
233 struct kstat stat;
234
235 err = vfs_getattr(fhp->fh_export->ex_mnt, dentry, &stat);
236 if (!err) {
237 *p++ = xdr_one; /* attributes follow */
238 return encode_fattr3(rqstp, p, fhp, &stat);
239 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 }
241 *p++ = xdr_zero;
242 return p;
243}
244
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000245/* Helper for NFSv3 ACLs */
246u32 *
247nfs3svc_encode_post_op_attr(struct svc_rqst *rqstp, u32 *p, struct svc_fh *fhp)
248{
249 return encode_post_op_attr(rqstp, p, fhp);
250}
251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252/*
253 * Enocde weak cache consistency data
254 */
255static u32 *
256encode_wcc_data(struct svc_rqst *rqstp, u32 *p, struct svc_fh *fhp)
257{
258 struct dentry *dentry = fhp->fh_dentry;
259
260 if (dentry && dentry->d_inode && fhp->fh_post_saved) {
261 if (fhp->fh_pre_saved) {
262 *p++ = xdr_one;
263 p = xdr_encode_hyper(p, (u64) fhp->fh_pre_size);
264 p = encode_time3(p, &fhp->fh_pre_mtime);
265 p = encode_time3(p, &fhp->fh_pre_ctime);
266 } else {
267 *p++ = xdr_zero;
268 }
269 return encode_saved_post_attr(rqstp, p, fhp);
270 }
271 /* no pre- or post-attrs */
272 *p++ = xdr_zero;
273 return encode_post_op_attr(rqstp, p, fhp);
274}
275
276
277/*
278 * XDR decode functions
279 */
280int
281nfs3svc_decode_fhandle(struct svc_rqst *rqstp, u32 *p, struct nfsd_fhandle *args)
282{
283 if (!(p = decode_fh(p, &args->fh)))
284 return 0;
285 return xdr_argsize_check(rqstp, p);
286}
287
288int
289nfs3svc_decode_sattrargs(struct svc_rqst *rqstp, u32 *p,
290 struct nfsd3_sattrargs *args)
291{
292 if (!(p = decode_fh(p, &args->fh))
293 || !(p = decode_sattr3(p, &args->attrs)))
294 return 0;
295
296 if ((args->check_guard = ntohl(*p++)) != 0) {
297 struct timespec time;
298 p = decode_time3(p, &time);
299 args->guardtime = time.tv_sec;
300 }
301
302 return xdr_argsize_check(rqstp, p);
303}
304
305int
306nfs3svc_decode_diropargs(struct svc_rqst *rqstp, u32 *p,
307 struct nfsd3_diropargs *args)
308{
309 if (!(p = decode_fh(p, &args->fh))
310 || !(p = decode_filename(p, &args->name, &args->len)))
311 return 0;
312
313 return xdr_argsize_check(rqstp, p);
314}
315
316int
317nfs3svc_decode_accessargs(struct svc_rqst *rqstp, u32 *p,
318 struct nfsd3_accessargs *args)
319{
320 if (!(p = decode_fh(p, &args->fh)))
321 return 0;
322 args->access = ntohl(*p++);
323
324 return xdr_argsize_check(rqstp, p);
325}
326
327int
328nfs3svc_decode_readargs(struct svc_rqst *rqstp, u32 *p,
329 struct nfsd3_readargs *args)
330{
331 unsigned int len;
332 int v,pn;
Greg Banks7adae482006-10-04 02:15:47 -0700333 u32 max_blocksize = svc_max_payload(rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
335 if (!(p = decode_fh(p, &args->fh))
336 || !(p = xdr_decode_hyper(p, &args->offset)))
337 return 0;
338
339 len = args->count = ntohl(*p++);
340
Greg Banks7adae482006-10-04 02:15:47 -0700341 if (len > max_blocksize)
342 len = max_blocksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
344 /* set up the kvec */
345 v=0;
346 while (len > 0) {
NeilBrown44524352006-10-04 02:15:46 -0700347 pn = rqstp->rq_resused++;
NeilBrown3cc03b12006-10-04 02:15:47 -0700348 rqstp->rq_vec[v].iov_base = page_address(rqstp->rq_respages[pn]);
349 rqstp->rq_vec[v].iov_len = len < PAGE_SIZE? len : PAGE_SIZE;
350 len -= rqstp->rq_vec[v].iov_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 v++;
352 }
353 args->vlen = v;
354 return xdr_argsize_check(rqstp, p);
355}
356
357int
358nfs3svc_decode_writeargs(struct svc_rqst *rqstp, u32 *p,
359 struct nfsd3_writeargs *args)
360{
361 unsigned int len, v, hdr;
Greg Banks7adae482006-10-04 02:15:47 -0700362 u32 max_blocksize = svc_max_payload(rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
364 if (!(p = decode_fh(p, &args->fh))
365 || !(p = xdr_decode_hyper(p, &args->offset)))
366 return 0;
367
368 args->count = ntohl(*p++);
369 args->stable = ntohl(*p++);
370 len = args->len = ntohl(*p++);
371
372 hdr = (void*)p - rqstp->rq_arg.head[0].iov_base;
NeilBrown0ba75362005-11-07 01:00:26 -0800373 if (rqstp->rq_arg.len < hdr ||
374 rqstp->rq_arg.len - hdr < len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 return 0;
376
NeilBrown3cc03b12006-10-04 02:15:47 -0700377 rqstp->rq_vec[0].iov_base = (void*)p;
378 rqstp->rq_vec[0].iov_len = rqstp->rq_arg.head[0].iov_len - hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Greg Banks7adae482006-10-04 02:15:47 -0700380 if (len > max_blocksize)
381 len = max_blocksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 v= 0;
NeilBrown3cc03b12006-10-04 02:15:47 -0700383 while (len > rqstp->rq_vec[v].iov_len) {
384 len -= rqstp->rq_vec[v].iov_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 v++;
NeilBrown3cc03b12006-10-04 02:15:47 -0700386 rqstp->rq_vec[v].iov_base = page_address(rqstp->rq_pages[v]);
387 rqstp->rq_vec[v].iov_len = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 }
NeilBrown3cc03b12006-10-04 02:15:47 -0700389 rqstp->rq_vec[v].iov_len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 args->vlen = v+1;
391
NeilBrown3cc03b12006-10-04 02:15:47 -0700392 return args->count == args->len && rqstp->rq_vec[0].iov_len > 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393}
394
395int
396nfs3svc_decode_createargs(struct svc_rqst *rqstp, u32 *p,
397 struct nfsd3_createargs *args)
398{
399 if (!(p = decode_fh(p, &args->fh))
400 || !(p = decode_filename(p, &args->name, &args->len)))
401 return 0;
402
403 switch (args->createmode = ntohl(*p++)) {
404 case NFS3_CREATE_UNCHECKED:
405 case NFS3_CREATE_GUARDED:
406 if (!(p = decode_sattr3(p, &args->attrs)))
407 return 0;
408 break;
409 case NFS3_CREATE_EXCLUSIVE:
410 args->verf = p;
411 p += 2;
412 break;
413 default:
414 return 0;
415 }
416
417 return xdr_argsize_check(rqstp, p);
418}
419int
420nfs3svc_decode_mkdirargs(struct svc_rqst *rqstp, u32 *p,
421 struct nfsd3_createargs *args)
422{
423 if (!(p = decode_fh(p, &args->fh))
424 || !(p = decode_filename(p, &args->name, &args->len))
425 || !(p = decode_sattr3(p, &args->attrs)))
426 return 0;
427
428 return xdr_argsize_check(rqstp, p);
429}
430
431int
432nfs3svc_decode_symlinkargs(struct svc_rqst *rqstp, u32 *p,
433 struct nfsd3_symlinkargs *args)
434{
435 unsigned int len;
436 int avail;
437 char *old, *new;
438 struct kvec *vec;
439
440 if (!(p = decode_fh(p, &args->ffh))
441 || !(p = decode_filename(p, &args->fname, &args->flen))
442 || !(p = decode_sattr3(p, &args->attrs))
443 )
444 return 0;
445 /* now decode the pathname, which might be larger than the first page.
446 * As we have to check for nul's anyway, we copy it into a new page
447 * This page appears in the rq_res.pages list, but as pages_len is always
448 * 0, it won't get in the way
449 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 len = ntohl(*p++);
451 if (len == 0 || len > NFS3_MAXPATHLEN || len >= PAGE_SIZE)
452 return 0;
NeilBrown44524352006-10-04 02:15:46 -0700453 args->tname = new =
454 page_address(rqstp->rq_respages[rqstp->rq_resused++]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 args->tlen = len;
456 /* first copy and check from the first page */
457 old = (char*)p;
458 vec = &rqstp->rq_arg.head[0];
459 avail = vec->iov_len - (old - (char*)vec->iov_base);
460 while (len && avail && *old) {
461 *new++ = *old++;
462 len--;
463 avail--;
464 }
465 /* now copy next page if there is one */
466 if (len && !avail && rqstp->rq_arg.page_len) {
467 avail = rqstp->rq_arg.page_len;
468 if (avail > PAGE_SIZE) avail = PAGE_SIZE;
469 old = page_address(rqstp->rq_arg.pages[0]);
470 }
471 while (len && avail && *old) {
472 *new++ = *old++;
473 len--;
474 avail--;
475 }
476 *new = '\0';
477 if (len)
478 return 0;
479
480 return 1;
481}
482
483int
484nfs3svc_decode_mknodargs(struct svc_rqst *rqstp, u32 *p,
485 struct nfsd3_mknodargs *args)
486{
487 if (!(p = decode_fh(p, &args->fh))
488 || !(p = decode_filename(p, &args->name, &args->len)))
489 return 0;
490
491 args->ftype = ntohl(*p++);
492
493 if (args->ftype == NF3BLK || args->ftype == NF3CHR
494 || args->ftype == NF3SOCK || args->ftype == NF3FIFO) {
495 if (!(p = decode_sattr3(p, &args->attrs)))
496 return 0;
497 }
498
499 if (args->ftype == NF3BLK || args->ftype == NF3CHR) {
500 args->major = ntohl(*p++);
501 args->minor = ntohl(*p++);
502 }
503
504 return xdr_argsize_check(rqstp, p);
505}
506
507int
508nfs3svc_decode_renameargs(struct svc_rqst *rqstp, u32 *p,
509 struct nfsd3_renameargs *args)
510{
511 if (!(p = decode_fh(p, &args->ffh))
512 || !(p = decode_filename(p, &args->fname, &args->flen))
513 || !(p = decode_fh(p, &args->tfh))
514 || !(p = decode_filename(p, &args->tname, &args->tlen)))
515 return 0;
516
517 return xdr_argsize_check(rqstp, p);
518}
519
520int
521nfs3svc_decode_readlinkargs(struct svc_rqst *rqstp, u32 *p,
522 struct nfsd3_readlinkargs *args)
523{
524 if (!(p = decode_fh(p, &args->fh)))
525 return 0;
NeilBrown44524352006-10-04 02:15:46 -0700526 args->buffer =
527 page_address(rqstp->rq_respages[rqstp->rq_resused++]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
529 return xdr_argsize_check(rqstp, p);
530}
531
532int
533nfs3svc_decode_linkargs(struct svc_rqst *rqstp, u32 *p,
534 struct nfsd3_linkargs *args)
535{
536 if (!(p = decode_fh(p, &args->ffh))
537 || !(p = decode_fh(p, &args->tfh))
538 || !(p = decode_filename(p, &args->tname, &args->tlen)))
539 return 0;
540
541 return xdr_argsize_check(rqstp, p);
542}
543
544int
545nfs3svc_decode_readdirargs(struct svc_rqst *rqstp, u32 *p,
546 struct nfsd3_readdirargs *args)
547{
548 if (!(p = decode_fh(p, &args->fh)))
549 return 0;
550 p = xdr_decode_hyper(p, &args->cookie);
551 args->verf = p; p += 2;
552 args->dircount = ~0;
553 args->count = ntohl(*p++);
554
555 if (args->count > PAGE_SIZE)
556 args->count = PAGE_SIZE;
557
NeilBrown44524352006-10-04 02:15:46 -0700558 args->buffer =
559 page_address(rqstp->rq_respages[rqstp->rq_resused++]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
561 return xdr_argsize_check(rqstp, p);
562}
563
564int
565nfs3svc_decode_readdirplusargs(struct svc_rqst *rqstp, u32 *p,
566 struct nfsd3_readdirargs *args)
567{
568 int len, pn;
Greg Banks7adae482006-10-04 02:15:47 -0700569 u32 max_blocksize = svc_max_payload(rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
571 if (!(p = decode_fh(p, &args->fh)))
572 return 0;
573 p = xdr_decode_hyper(p, &args->cookie);
574 args->verf = p; p += 2;
575 args->dircount = ntohl(*p++);
576 args->count = ntohl(*p++);
577
Greg Banks7adae482006-10-04 02:15:47 -0700578 len = (args->count > max_blocksize) ? max_blocksize :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 args->count;
580 args->count = len;
581
582 while (len > 0) {
NeilBrown44524352006-10-04 02:15:46 -0700583 pn = rqstp->rq_resused++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 if (!args->buffer)
585 args->buffer = page_address(rqstp->rq_respages[pn]);
586 len -= PAGE_SIZE;
587 }
588
589 return xdr_argsize_check(rqstp, p);
590}
591
592int
593nfs3svc_decode_commitargs(struct svc_rqst *rqstp, u32 *p,
594 struct nfsd3_commitargs *args)
595{
596 if (!(p = decode_fh(p, &args->fh)))
597 return 0;
598 p = xdr_decode_hyper(p, &args->offset);
599 args->count = ntohl(*p++);
600
601 return xdr_argsize_check(rqstp, p);
602}
603
604/*
605 * XDR encode functions
606 */
607/*
608 * There must be an encoding function for void results so svc_process
609 * will work properly.
610 */
611int
612nfs3svc_encode_voidres(struct svc_rqst *rqstp, u32 *p, void *dummy)
613{
614 return xdr_ressize_check(rqstp, p);
615}
616
617/* GETATTR */
618int
619nfs3svc_encode_attrstat(struct svc_rqst *rqstp, u32 *p,
620 struct nfsd3_attrstat *resp)
621{
622 if (resp->status == 0)
David Shawa334de22006-01-06 00:19:58 -0800623 p = encode_fattr3(rqstp, p, &resp->fh, &resp->stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 return xdr_ressize_check(rqstp, p);
625}
626
627/* SETATTR, REMOVE, RMDIR */
628int
629nfs3svc_encode_wccstat(struct svc_rqst *rqstp, u32 *p,
630 struct nfsd3_attrstat *resp)
631{
632 p = encode_wcc_data(rqstp, p, &resp->fh);
633 return xdr_ressize_check(rqstp, p);
634}
635
636/* LOOKUP */
637int
638nfs3svc_encode_diropres(struct svc_rqst *rqstp, u32 *p,
639 struct nfsd3_diropres *resp)
640{
641 if (resp->status == 0) {
642 p = encode_fh(p, &resp->fh);
643 p = encode_post_op_attr(rqstp, p, &resp->fh);
644 }
645 p = encode_post_op_attr(rqstp, p, &resp->dirfh);
646 return xdr_ressize_check(rqstp, p);
647}
648
649/* ACCESS */
650int
651nfs3svc_encode_accessres(struct svc_rqst *rqstp, u32 *p,
652 struct nfsd3_accessres *resp)
653{
654 p = encode_post_op_attr(rqstp, p, &resp->fh);
655 if (resp->status == 0)
656 *p++ = htonl(resp->access);
657 return xdr_ressize_check(rqstp, p);
658}
659
660/* READLINK */
661int
662nfs3svc_encode_readlinkres(struct svc_rqst *rqstp, u32 *p,
663 struct nfsd3_readlinkres *resp)
664{
665 p = encode_post_op_attr(rqstp, p, &resp->fh);
666 if (resp->status == 0) {
667 *p++ = htonl(resp->len);
668 xdr_ressize_check(rqstp, p);
669 rqstp->rq_res.page_len = resp->len;
670 if (resp->len & 3) {
671 /* need to pad the tail */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 rqstp->rq_res.tail[0].iov_base = p;
673 *p = 0;
674 rqstp->rq_res.tail[0].iov_len = 4 - (resp->len&3);
675 }
676 return 1;
677 } else
678 return xdr_ressize_check(rqstp, p);
679}
680
681/* READ */
682int
683nfs3svc_encode_readres(struct svc_rqst *rqstp, u32 *p,
684 struct nfsd3_readres *resp)
685{
686 p = encode_post_op_attr(rqstp, p, &resp->fh);
687 if (resp->status == 0) {
688 *p++ = htonl(resp->count);
689 *p++ = htonl(resp->eof);
690 *p++ = htonl(resp->count); /* xdr opaque count */
691 xdr_ressize_check(rqstp, p);
692 /* now update rqstp->rq_res to reflect data aswell */
693 rqstp->rq_res.page_len = resp->count;
694 if (resp->count & 3) {
695 /* need to pad the tail */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 rqstp->rq_res.tail[0].iov_base = p;
697 *p = 0;
698 rqstp->rq_res.tail[0].iov_len = 4 - (resp->count & 3);
699 }
700 return 1;
701 } else
702 return xdr_ressize_check(rqstp, p);
703}
704
705/* WRITE */
706int
707nfs3svc_encode_writeres(struct svc_rqst *rqstp, u32 *p,
708 struct nfsd3_writeres *resp)
709{
710 p = encode_wcc_data(rqstp, p, &resp->fh);
711 if (resp->status == 0) {
712 *p++ = htonl(resp->count);
713 *p++ = htonl(resp->committed);
714 *p++ = htonl(nfssvc_boot.tv_sec);
715 *p++ = htonl(nfssvc_boot.tv_usec);
716 }
717 return xdr_ressize_check(rqstp, p);
718}
719
720/* CREATE, MKDIR, SYMLINK, MKNOD */
721int
722nfs3svc_encode_createres(struct svc_rqst *rqstp, u32 *p,
723 struct nfsd3_diropres *resp)
724{
725 if (resp->status == 0) {
726 *p++ = xdr_one;
727 p = encode_fh(p, &resp->fh);
728 p = encode_post_op_attr(rqstp, p, &resp->fh);
729 }
730 p = encode_wcc_data(rqstp, p, &resp->dirfh);
731 return xdr_ressize_check(rqstp, p);
732}
733
734/* RENAME */
735int
736nfs3svc_encode_renameres(struct svc_rqst *rqstp, u32 *p,
737 struct nfsd3_renameres *resp)
738{
739 p = encode_wcc_data(rqstp, p, &resp->ffh);
740 p = encode_wcc_data(rqstp, p, &resp->tfh);
741 return xdr_ressize_check(rqstp, p);
742}
743
744/* LINK */
745int
746nfs3svc_encode_linkres(struct svc_rqst *rqstp, u32 *p,
747 struct nfsd3_linkres *resp)
748{
749 p = encode_post_op_attr(rqstp, p, &resp->fh);
750 p = encode_wcc_data(rqstp, p, &resp->tfh);
751 return xdr_ressize_check(rqstp, p);
752}
753
754/* READDIR */
755int
756nfs3svc_encode_readdirres(struct svc_rqst *rqstp, u32 *p,
757 struct nfsd3_readdirres *resp)
758{
759 p = encode_post_op_attr(rqstp, p, &resp->fh);
760
761 if (resp->status == 0) {
762 /* stupid readdir cookie */
763 memcpy(p, resp->verf, 8); p += 2;
764 xdr_ressize_check(rqstp, p);
765 if (rqstp->rq_res.head[0].iov_len + (2<<2) > PAGE_SIZE)
766 return 1; /*No room for trailer */
767 rqstp->rq_res.page_len = (resp->count) << 2;
768
769 /* add the 'tail' to the end of the 'head' page - page 0. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 rqstp->rq_res.tail[0].iov_base = p;
771 *p++ = 0; /* no more entries */
772 *p++ = htonl(resp->common.err == nfserr_eof);
773 rqstp->rq_res.tail[0].iov_len = 2<<2;
774 return 1;
775 } else
776 return xdr_ressize_check(rqstp, p);
777}
778
779static inline u32 *
780encode_entry_baggage(struct nfsd3_readdirres *cd, u32 *p, const char *name,
781 int namlen, ino_t ino)
782{
783 *p++ = xdr_one; /* mark entry present */
784 p = xdr_encode_hyper(p, ino); /* file id */
785 p = xdr_encode_array(p, name, namlen);/* name length & name */
786
787 cd->offset = p; /* remember pointer */
788 p = xdr_encode_hyper(p, NFS_OFFSET_MAX);/* offset of next entry */
789
790 return p;
791}
792
793static inline u32 *
794encode_entryplus_baggage(struct nfsd3_readdirres *cd, u32 *p,
795 struct svc_fh *fhp)
796{
797 p = encode_post_op_attr(cd->rqstp, p, fhp);
798 *p++ = xdr_one; /* yes, a file handle follows */
799 p = encode_fh(p, fhp);
800 fh_put(fhp);
801 return p;
802}
803
804static int
805compose_entry_fh(struct nfsd3_readdirres *cd, struct svc_fh *fhp,
806 const char *name, int namlen)
807{
808 struct svc_export *exp;
809 struct dentry *dparent, *dchild;
810 int rv = 0;
811
812 dparent = cd->fh.fh_dentry;
813 exp = cd->fh.fh_export;
814
815 fh_init(fhp, NFS3_FHSIZE);
816 if (isdotent(name, namlen)) {
817 if (namlen == 2) {
818 dchild = dget_parent(dparent);
819 if (dchild == dparent) {
820 /* filesystem root - cannot return filehandle for ".." */
821 dput(dchild);
822 return 1;
823 }
824 } else
825 dchild = dget(dparent);
826 } else
827 dchild = lookup_one_len(name, dparent, namlen);
828 if (IS_ERR(dchild))
829 return 1;
830 if (d_mountpoint(dchild) ||
831 fh_compose(fhp, exp, dchild, &cd->fh) != 0 ||
832 !dchild->d_inode)
833 rv = 1;
834 dput(dchild);
835 return rv;
836}
837
838/*
839 * Encode a directory entry. This one works for both normal readdir
840 * and readdirplus.
841 * The normal readdir reply requires 2 (fileid) + 1 (stringlen)
842 * + string + 2 (cookie) + 1 (next) words, i.e. 6 + strlen.
843 *
844 * The readdirplus baggage is 1+21 words for post_op_attr, plus the
845 * file handle.
846 */
847
848#define NFS3_ENTRY_BAGGAGE (2 + 1 + 2 + 1)
849#define NFS3_ENTRYPLUS_BAGGAGE (1 + 21 + 1 + (NFS3_FHSIZE >> 2))
850static int
851encode_entry(struct readdir_cd *ccd, const char *name,
852 int namlen, off_t offset, ino_t ino, unsigned int d_type, int plus)
853{
854 struct nfsd3_readdirres *cd = container_of(ccd, struct nfsd3_readdirres,
855 common);
856 u32 *p = cd->buffer;
857 caddr_t curr_page_addr = NULL;
858 int pn; /* current page number */
859 int slen; /* string (name) length */
860 int elen; /* estimated entry length in words */
861 int num_entry_words = 0; /* actual number of words */
862
863 if (cd->offset) {
864 u64 offset64 = offset;
865
866 if (unlikely(cd->offset1)) {
867 /* we ended up with offset on a page boundary */
868 *cd->offset = htonl(offset64 >> 32);
869 *cd->offset1 = htonl(offset64 & 0xffffffff);
870 cd->offset1 = NULL;
871 } else {
872 xdr_encode_hyper(cd->offset, (u64) offset);
873 }
874 }
875
876 /*
877 dprintk("encode_entry(%.*s @%ld%s)\n",
878 namlen, name, (long) offset, plus? " plus" : "");
879 */
880
881 /* truncate filename if too long */
882 if (namlen > NFS3_MAXNAMLEN)
883 namlen = NFS3_MAXNAMLEN;
884
885 slen = XDR_QUADLEN(namlen);
886 elen = slen + NFS3_ENTRY_BAGGAGE
887 + (plus? NFS3_ENTRYPLUS_BAGGAGE : 0);
888
889 if (cd->buflen < elen) {
890 cd->common.err = nfserr_toosmall;
891 return -EINVAL;
892 }
893
894 /* determine which page in rq_respages[] we are currently filling */
895 for (pn=1; pn < cd->rqstp->rq_resused; pn++) {
896 curr_page_addr = page_address(cd->rqstp->rq_respages[pn]);
897
898 if (((caddr_t)cd->buffer >= curr_page_addr) &&
899 ((caddr_t)cd->buffer < curr_page_addr + PAGE_SIZE))
900 break;
901 }
902
903 if ((caddr_t)(cd->buffer + elen) < (curr_page_addr + PAGE_SIZE)) {
904 /* encode entry in current page */
905
906 p = encode_entry_baggage(cd, p, name, namlen, ino);
907
908 /* throw in readdirplus baggage */
909 if (plus) {
910 struct svc_fh fh;
911
912 if (compose_entry_fh(cd, &fh, name, namlen) > 0) {
913 *p++ = 0;
914 *p++ = 0;
915 } else
916 p = encode_entryplus_baggage(cd, p, &fh);
917 }
918 num_entry_words = p - cd->buffer;
919 } else if (cd->rqstp->rq_respages[pn+1] != NULL) {
920 /* temporarily encode entry into next page, then move back to
921 * current and next page in rq_respages[] */
922 u32 *p1, *tmp;
923 int len1, len2;
924
925 /* grab next page for temporary storage of entry */
926 p1 = tmp = page_address(cd->rqstp->rq_respages[pn+1]);
927
928 p1 = encode_entry_baggage(cd, p1, name, namlen, ino);
929
930 /* throw in readdirplus baggage */
931 if (plus) {
932 struct svc_fh fh;
933
934 if (compose_entry_fh(cd, &fh, name, namlen) > 0) {
935 /* zero out the filehandle */
936 *p1++ = 0;
937 *p1++ = 0;
938 } else
939 p1 = encode_entryplus_baggage(cd, p1, &fh);
940 }
941
942 /* determine entry word length and lengths to go in pages */
943 num_entry_words = p1 - tmp;
944 len1 = curr_page_addr + PAGE_SIZE - (caddr_t)cd->buffer;
945 if ((num_entry_words << 2) < len1) {
946 /* the actual number of words in the entry is less
947 * than elen and can still fit in the current page
948 */
949 memmove(p, tmp, num_entry_words << 2);
950 p += num_entry_words;
951
952 /* update offset */
953 cd->offset = cd->buffer + (cd->offset - tmp);
954 } else {
955 unsigned int offset_r = (cd->offset - tmp) << 2;
956
957 /* update pointer to offset location.
958 * This is a 64bit quantity, so we need to
959 * deal with 3 cases:
960 * - entirely in first page
961 * - entirely in second page
962 * - 4 bytes in each page
963 */
964 if (offset_r + 8 <= len1) {
965 cd->offset = p + (cd->offset - tmp);
966 } else if (offset_r >= len1) {
967 cd->offset -= len1 >> 2;
968 } else {
969 /* sitting on the fence */
970 BUG_ON(offset_r != len1 - 4);
971 cd->offset = p + (cd->offset - tmp);
972 cd->offset1 = tmp;
973 }
974
975 len2 = (num_entry_words << 2) - len1;
976
977 /* move from temp page to current and next pages */
978 memmove(p, tmp, len1);
979 memmove(tmp, (caddr_t)tmp+len1, len2);
980
981 p = tmp + (len2 >> 2);
982 }
983 }
984 else {
985 cd->common.err = nfserr_toosmall;
986 return -EINVAL;
987 }
988
989 cd->buflen -= num_entry_words;
990 cd->buffer = p;
991 cd->common.err = nfs_ok;
992 return 0;
993
994}
995
996int
997nfs3svc_encode_entry(struct readdir_cd *cd, const char *name,
998 int namlen, loff_t offset, ino_t ino, unsigned int d_type)
999{
1000 return encode_entry(cd, name, namlen, offset, ino, d_type, 0);
1001}
1002
1003int
1004nfs3svc_encode_entry_plus(struct readdir_cd *cd, const char *name,
1005 int namlen, loff_t offset, ino_t ino, unsigned int d_type)
1006{
1007 return encode_entry(cd, name, namlen, offset, ino, d_type, 1);
1008}
1009
1010/* FSSTAT */
1011int
1012nfs3svc_encode_fsstatres(struct svc_rqst *rqstp, u32 *p,
1013 struct nfsd3_fsstatres *resp)
1014{
1015 struct kstatfs *s = &resp->stats;
1016 u64 bs = s->f_bsize;
1017
1018 *p++ = xdr_zero; /* no post_op_attr */
1019
1020 if (resp->status == 0) {
1021 p = xdr_encode_hyper(p, bs * s->f_blocks); /* total bytes */
1022 p = xdr_encode_hyper(p, bs * s->f_bfree); /* free bytes */
1023 p = xdr_encode_hyper(p, bs * s->f_bavail); /* user available bytes */
1024 p = xdr_encode_hyper(p, s->f_files); /* total inodes */
1025 p = xdr_encode_hyper(p, s->f_ffree); /* free inodes */
1026 p = xdr_encode_hyper(p, s->f_ffree); /* user available inodes */
1027 *p++ = htonl(resp->invarsec); /* mean unchanged time */
1028 }
1029 return xdr_ressize_check(rqstp, p);
1030}
1031
1032/* FSINFO */
1033int
1034nfs3svc_encode_fsinfores(struct svc_rqst *rqstp, u32 *p,
1035 struct nfsd3_fsinfores *resp)
1036{
1037 *p++ = xdr_zero; /* no post_op_attr */
1038
1039 if (resp->status == 0) {
1040 *p++ = htonl(resp->f_rtmax);
1041 *p++ = htonl(resp->f_rtpref);
1042 *p++ = htonl(resp->f_rtmult);
1043 *p++ = htonl(resp->f_wtmax);
1044 *p++ = htonl(resp->f_wtpref);
1045 *p++ = htonl(resp->f_wtmult);
1046 *p++ = htonl(resp->f_dtpref);
1047 p = xdr_encode_hyper(p, resp->f_maxfilesize);
1048 *p++ = xdr_one;
1049 *p++ = xdr_zero;
1050 *p++ = htonl(resp->f_properties);
1051 }
1052
1053 return xdr_ressize_check(rqstp, p);
1054}
1055
1056/* PATHCONF */
1057int
1058nfs3svc_encode_pathconfres(struct svc_rqst *rqstp, u32 *p,
1059 struct nfsd3_pathconfres *resp)
1060{
1061 *p++ = xdr_zero; /* no post_op_attr */
1062
1063 if (resp->status == 0) {
1064 *p++ = htonl(resp->p_link_max);
1065 *p++ = htonl(resp->p_name_max);
1066 *p++ = htonl(resp->p_no_trunc);
1067 *p++ = htonl(resp->p_chown_restricted);
1068 *p++ = htonl(resp->p_case_insensitive);
1069 *p++ = htonl(resp->p_case_preserving);
1070 }
1071
1072 return xdr_ressize_check(rqstp, p);
1073}
1074
1075/* COMMIT */
1076int
1077nfs3svc_encode_commitres(struct svc_rqst *rqstp, u32 *p,
1078 struct nfsd3_commitres *resp)
1079{
1080 p = encode_wcc_data(rqstp, p, &resp->fh);
1081 /* Write verifier */
1082 if (resp->status == 0) {
1083 *p++ = htonl(nfssvc_boot.tv_sec);
1084 *p++ = htonl(nfssvc_boot.tv_usec);
1085 }
1086 return xdr_ressize_check(rqstp, p);
1087}
1088
1089/*
1090 * XDR release functions
1091 */
1092int
1093nfs3svc_release_fhandle(struct svc_rqst *rqstp, u32 *p,
1094 struct nfsd3_attrstat *resp)
1095{
1096 fh_put(&resp->fh);
1097 return 1;
1098}
1099
1100int
1101nfs3svc_release_fhandle2(struct svc_rqst *rqstp, u32 *p,
1102 struct nfsd3_fhandle_pair *resp)
1103{
1104 fh_put(&resp->fh1);
1105 fh_put(&resp->fh2);
1106 return 1;
1107}