]> bbs.cooldavid.org Git - net-next-2.6.git/blame - fs/nfsd/nfs3xdr.c
[PATCH] include/asm-h8300/: "extern inline" -> "static inline"
[net-next-2.6.git] / fs / nfsd / nfs3xdr.c
CommitLineData
1da177e4
LT
1/*
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 */
91f07168
AV
45static inline __be32 *
46encode_time3(__be32 *p, struct timespec *time)
1da177e4
LT
47{
48 *p++ = htonl((u32) time->tv_sec); *p++ = htonl(time->tv_nsec);
49 return p;
50}
51
91f07168
AV
52static inline __be32 *
53decode_time3(__be32 *p, struct timespec *time)
1da177e4
LT
54{
55 time->tv_sec = ntohl(*p++);
56 time->tv_nsec = ntohl(*p++);
57 return p;
58}
59
91f07168
AV
60static inline __be32 *
61decode_fh(__be32 *p, struct svc_fh *fhp)
1da177e4
LT
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
a257cdd0 74/* Helper function for NFSv3 ACL code */
91f07168 75__be32 *nfs3svc_decode_fh(__be32 *p, struct svc_fh *fhp)
a257cdd0
AG
76{
77 return decode_fh(p, fhp);
78}
79
91f07168
AV
80static inline __be32 *
81encode_fh(__be32 *p, struct svc_fh *fhp)
1da177e4
LT
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 */
91f07168
AV
94static inline __be32 *
95decode_filename(__be32 *p, char **namp, int *lenp)
1da177e4
LT
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
91f07168
AV
110static inline __be32 *
111decode_sattr3(__be32 *p, struct iattr *iap)
1da177e4
LT
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
91f07168
AV
156static inline __be32 *
157encode_fattr3(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp,
a334de28 158 struct kstat *stat)
1da177e4 159{
1da177e4 160 struct dentry *dentry = fhp->fh_dentry;
1da177e4
LT
161 struct timespec time;
162
a334de28
DS
163 *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) {
1da177e4
LT
169 p = xdr_encode_hyper(p, (u64) NFS3_MAXPATHLEN);
170 } else {
a334de28 171 p = xdr_encode_hyper(p, (u64) stat->size);
1da177e4 172 }
a334de28
DS
173 p = xdr_encode_hyper(p, ((u64)stat->blocks) << 9);
174 *p++ = htonl((u32) MAJOR(stat->rdev));
175 *p++ = htonl((u32) MINOR(stat->rdev));
1da177e4
LT
176 if (is_fsid(fhp, rqstp->rq_reffh))
177 p = xdr_encode_hyper(p, (u64) fhp->fh_export->ex_fsid);
178 else
a334de28
DS
179 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);
1da177e4
LT
182 lease_get_mtime(dentry->d_inode, &time);
183 p = encode_time3(p, &time);
a334de28 184 p = encode_time3(p, &stat->ctime);
1da177e4
LT
185
186 return p;
187}
188
91f07168
AV
189static inline __be32 *
190encode_saved_post_attr(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp)
1da177e4
LT
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 */
91f07168
AV
227static __be32 *
228encode_post_op_attr(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp)
1da177e4
LT
229{
230 struct dentry *dentry = fhp->fh_dentry;
231 if (dentry && dentry->d_inode != NULL) {
a334de28
DS
232 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 }
1da177e4
LT
240 }
241 *p++ = xdr_zero;
242 return p;
243}
244
a257cdd0 245/* Helper for NFSv3 ACLs */
91f07168
AV
246__be32 *
247nfs3svc_encode_post_op_attr(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp)
a257cdd0
AG
248{
249 return encode_post_op_attr(rqstp, p, fhp);
250}
251
1da177e4
LT
252/*
253 * Enocde weak cache consistency data
254 */
91f07168
AV
255static __be32 *
256encode_wcc_data(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp)
1da177e4
LT
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
91f07168 281nfs3svc_decode_fhandle(struct svc_rqst *rqstp, __be32 *p, struct nfsd_fhandle *args)
1da177e4
LT
282{
283 if (!(p = decode_fh(p, &args->fh)))
284 return 0;
285 return xdr_argsize_check(rqstp, p);
286}
287
288int
91f07168 289nfs3svc_decode_sattrargs(struct svc_rqst *rqstp, __be32 *p,
1da177e4
LT
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
91f07168 306nfs3svc_decode_diropargs(struct svc_rqst *rqstp, __be32 *p,
1da177e4
LT
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
91f07168 317nfs3svc_decode_accessargs(struct svc_rqst *rqstp, __be32 *p,
1da177e4
LT
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
91f07168 328nfs3svc_decode_readargs(struct svc_rqst *rqstp, __be32 *p,
1da177e4
LT
329 struct nfsd3_readargs *args)
330{
331 unsigned int len;
332 int v,pn;
7adae489 333 u32 max_blocksize = svc_max_payload(rqstp);
1da177e4
LT
334
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
7adae489
GB
341 if (len > max_blocksize)
342 len = max_blocksize;
1da177e4
LT
343
344 /* set up the kvec */
345 v=0;
346 while (len > 0) {
44524359 347 pn = rqstp->rq_resused++;
3cc03b16
N
348 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;
1da177e4
LT
351 v++;
352 }
353 args->vlen = v;
354 return xdr_argsize_check(rqstp, p);
355}
356
357int
91f07168 358nfs3svc_decode_writeargs(struct svc_rqst *rqstp, __be32 *p,
1da177e4
LT
359 struct nfsd3_writeargs *args)
360{
361 unsigned int len, v, hdr;
7adae489 362 u32 max_blocksize = svc_max_payload(rqstp);
1da177e4
LT
363
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;
0ba7536d
N
373 if (rqstp->rq_arg.len < hdr ||
374 rqstp->rq_arg.len - hdr < len)
1da177e4
LT
375 return 0;
376
3cc03b16
N
377 rqstp->rq_vec[0].iov_base = (void*)p;
378 rqstp->rq_vec[0].iov_len = rqstp->rq_arg.head[0].iov_len - hdr;
1da177e4 379
7adae489
GB
380 if (len > max_blocksize)
381 len = max_blocksize;
1da177e4 382 v= 0;
3cc03b16
N
383 while (len > rqstp->rq_vec[v].iov_len) {
384 len -= rqstp->rq_vec[v].iov_len;
1da177e4 385 v++;
3cc03b16
N
386 rqstp->rq_vec[v].iov_base = page_address(rqstp->rq_pages[v]);
387 rqstp->rq_vec[v].iov_len = PAGE_SIZE;
1da177e4 388 }
3cc03b16 389 rqstp->rq_vec[v].iov_len = len;
1da177e4
LT
390 args->vlen = v+1;
391
3cc03b16 392 return args->count == args->len && rqstp->rq_vec[0].iov_len > 0;
1da177e4
LT
393}
394
395int
91f07168 396nfs3svc_decode_createargs(struct svc_rqst *rqstp, __be32 *p,
1da177e4
LT
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
91f07168 420nfs3svc_decode_mkdirargs(struct svc_rqst *rqstp, __be32 *p,
1da177e4
LT
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
91f07168 432nfs3svc_decode_symlinkargs(struct svc_rqst *rqstp, __be32 *p,
1da177e4
LT
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 */
1da177e4
LT
450 len = ntohl(*p++);
451 if (len == 0 || len > NFS3_MAXPATHLEN || len >= PAGE_SIZE)
452 return 0;
44524359
N
453 args->tname = new =
454 page_address(rqstp->rq_respages[rqstp->rq_resused++]);
1da177e4
LT
455 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
91f07168 484nfs3svc_decode_mknodargs(struct svc_rqst *rqstp, __be32 *p,
1da177e4
LT
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
91f07168 508nfs3svc_decode_renameargs(struct svc_rqst *rqstp, __be32 *p,
1da177e4
LT
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
91f07168 521nfs3svc_decode_readlinkargs(struct svc_rqst *rqstp, __be32 *p,
1da177e4
LT
522 struct nfsd3_readlinkargs *args)
523{
524 if (!(p = decode_fh(p, &args->fh)))
525 return 0;
44524359
N
526 args->buffer =
527 page_address(rqstp->rq_respages[rqstp->rq_resused++]);
1da177e4
LT
528
529 return xdr_argsize_check(rqstp, p);
530}
531
532int
91f07168 533nfs3svc_decode_linkargs(struct svc_rqst *rqstp, __be32 *p,
1da177e4
LT
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
91f07168 545nfs3svc_decode_readdirargs(struct svc_rqst *rqstp, __be32 *p,
1da177e4
LT
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
44524359
N
558 args->buffer =
559 page_address(rqstp->rq_respages[rqstp->rq_resused++]);
1da177e4
LT
560
561 return xdr_argsize_check(rqstp, p);
562}
563
564int
91f07168 565nfs3svc_decode_readdirplusargs(struct svc_rqst *rqstp, __be32 *p,
1da177e4
LT
566 struct nfsd3_readdirargs *args)
567{
568 int len, pn;
7adae489 569 u32 max_blocksize = svc_max_payload(rqstp);
1da177e4
LT
570
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
7adae489 578 len = (args->count > max_blocksize) ? max_blocksize :
1da177e4
LT
579 args->count;
580 args->count = len;
581
582 while (len > 0) {
44524359 583 pn = rqstp->rq_resused++;
1da177e4
LT
584 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
91f07168 593nfs3svc_decode_commitargs(struct svc_rqst *rqstp, __be32 *p,
1da177e4
LT
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
91f07168 612nfs3svc_encode_voidres(struct svc_rqst *rqstp, __be32 *p, void *dummy)
1da177e4
LT
613{
614 return xdr_ressize_check(rqstp, p);
615}
616
617/* GETATTR */
618int
91f07168 619nfs3svc_encode_attrstat(struct svc_rqst *rqstp, __be32 *p,
1da177e4
LT
620 struct nfsd3_attrstat *resp)
621{
622 if (resp->status == 0)
a334de28 623 p = encode_fattr3(rqstp, p, &resp->fh, &resp->stat);
1da177e4
LT
624 return xdr_ressize_check(rqstp, p);
625}
626
627/* SETATTR, REMOVE, RMDIR */
628int
91f07168 629nfs3svc_encode_wccstat(struct svc_rqst *rqstp, __be32 *p,
1da177e4
LT
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
91f07168 638nfs3svc_encode_diropres(struct svc_rqst *rqstp, __be32 *p,
1da177e4
LT
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
91f07168 651nfs3svc_encode_accessres(struct svc_rqst *rqstp, __be32 *p,
1da177e4
LT
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
91f07168 662nfs3svc_encode_readlinkres(struct svc_rqst *rqstp, __be32 *p,
1da177e4
LT
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 */
1da177e4
LT
672 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
91f07168 683nfs3svc_encode_readres(struct svc_rqst *rqstp, __be32 *p,
1da177e4
LT
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 */
1da177e4
LT
696 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
91f07168 707nfs3svc_encode_writeres(struct svc_rqst *rqstp, __be32 *p,
1da177e4
LT
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
91f07168 722nfs3svc_encode_createres(struct svc_rqst *rqstp, __be32 *p,
1da177e4
LT
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
91f07168 736nfs3svc_encode_renameres(struct svc_rqst *rqstp, __be32 *p,
1da177e4
LT
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
91f07168 746nfs3svc_encode_linkres(struct svc_rqst *rqstp, __be32 *p,
1da177e4
LT
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
91f07168 756nfs3svc_encode_readdirres(struct svc_rqst *rqstp, __be32 *p,
1da177e4
LT
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. */
1da177e4
LT
770 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
91f07168
AV
779static inline __be32 *
780encode_entry_baggage(struct nfsd3_readdirres *cd, __be32 *p, const char *name,
1da177e4
LT
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
91f07168
AV
793static inline __be32 *
794encode_entryplus_baggage(struct nfsd3_readdirres *cd, __be32 *p,
1da177e4
LT
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);
91f07168 856 __be32 *p = cd->buffer;
1da177e4
LT
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[] */
91f07168 922 __be32 *p1, *tmp;
1da177e4
LT
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
91f07168 1012nfs3svc_encode_fsstatres(struct svc_rqst *rqstp, __be32 *p,
1da177e4
LT
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
91f07168 1034nfs3svc_encode_fsinfores(struct svc_rqst *rqstp, __be32 *p,
1da177e4
LT
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
91f07168 1058nfs3svc_encode_pathconfres(struct svc_rqst *rqstp, __be32 *p,
1da177e4
LT
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
91f07168 1077nfs3svc_encode_commitres(struct svc_rqst *rqstp, __be32 *p,
1da177e4
LT
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
91f07168 1093nfs3svc_release_fhandle(struct svc_rqst *rqstp, __be32 *p,
1da177e4
LT
1094 struct nfsd3_attrstat *resp)
1095{
1096 fh_put(&resp->fh);
1097 return 1;
1098}
1099
1100int
91f07168 1101nfs3svc_release_fhandle2(struct svc_rqst *rqstp, __be32 *p,
1da177e4
LT
1102 struct nfsd3_fhandle_pair *resp)
1103{
1104 fh_put(&resp->fh1);
1105 fh_put(&resp->fh2);
1106 return 1;
1107}