[PATCH] IPMI: fix timeout list handling
[linux-2.6/kvm.git] / fs / nfsd / nfs3xdr.c
blobe695660921ec30adfd6f0ebb3546120ee536964e
1 /*
2 * linux/fs/nfsd/nfs3xdr.c
4 * XDR support for nfsd/protocol version 3.
6 * Copyright (C) 1995, 1996, 1997 Olaf Kirch <okir@monad.swb.de>
8 * 2003-08-09 Jamie Lokier: Use htonl() for nanoseconds, not htons()!
9 */
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>
25 #define NFSDDBG_FACILITY NFSDDBG_XDR
29 * Mapping of S_IF* types to NFS file types
31 static u32 nfs3_ftypes[] = {
32 NF3NON, NF3FIFO, NF3CHR, NF3BAD,
33 NF3DIR, NF3BAD, NF3BLK, NF3BAD,
34 NF3REG, NF3BAD, NF3LNK, NF3BAD,
35 NF3SOCK, NF3BAD, NF3LNK, NF3BAD,
39 * XDR functions for basic NFS types
41 static __be32 *
42 encode_time3(__be32 *p, struct timespec *time)
44 *p++ = htonl((u32) time->tv_sec); *p++ = htonl(time->tv_nsec);
45 return p;
48 static __be32 *
49 decode_time3(__be32 *p, struct timespec *time)
51 time->tv_sec = ntohl(*p++);
52 time->tv_nsec = ntohl(*p++);
53 return p;
56 static __be32 *
57 decode_fh(__be32 *p, struct svc_fh *fhp)
59 unsigned int size;
60 fh_init(fhp, NFS3_FHSIZE);
61 size = ntohl(*p++);
62 if (size > NFS3_FHSIZE)
63 return NULL;
65 memcpy(&fhp->fh_handle.fh_base, p, size);
66 fhp->fh_handle.fh_size = size;
67 return p + XDR_QUADLEN(size);
70 /* Helper function for NFSv3 ACL code */
71 __be32 *nfs3svc_decode_fh(__be32 *p, struct svc_fh *fhp)
73 return decode_fh(p, fhp);
76 static __be32 *
77 encode_fh(__be32 *p, struct svc_fh *fhp)
79 unsigned int size = fhp->fh_handle.fh_size;
80 *p++ = htonl(size);
81 if (size) p[XDR_QUADLEN(size)-1]=0;
82 memcpy(p, &fhp->fh_handle.fh_base, size);
83 return p + XDR_QUADLEN(size);
87 * Decode a file name and make sure that the path contains
88 * no slashes or null bytes.
90 static __be32 *
91 decode_filename(__be32 *p, char **namp, int *lenp)
93 char *name;
94 int i;
96 if ((p = xdr_decode_string_inplace(p, namp, lenp, NFS3_MAXNAMLEN)) != NULL) {
97 for (i = 0, name = *namp; i < *lenp; i++, name++) {
98 if (*name == '\0' || *name == '/')
99 return NULL;
103 return p;
106 static __be32 *
107 decode_sattr3(__be32 *p, struct iattr *iap)
109 u32 tmp;
111 iap->ia_valid = 0;
113 if (*p++) {
114 iap->ia_valid |= ATTR_MODE;
115 iap->ia_mode = ntohl(*p++);
117 if (*p++) {
118 iap->ia_valid |= ATTR_UID;
119 iap->ia_uid = ntohl(*p++);
121 if (*p++) {
122 iap->ia_valid |= ATTR_GID;
123 iap->ia_gid = ntohl(*p++);
125 if (*p++) {
126 u64 newsize;
128 iap->ia_valid |= ATTR_SIZE;
129 p = xdr_decode_hyper(p, &newsize);
130 if (newsize <= NFS_OFFSET_MAX)
131 iap->ia_size = newsize;
132 else
133 iap->ia_size = NFS_OFFSET_MAX;
135 if ((tmp = ntohl(*p++)) == 1) { /* set to server time */
136 iap->ia_valid |= ATTR_ATIME;
137 } else if (tmp == 2) { /* set to client time */
138 iap->ia_valid |= ATTR_ATIME | ATTR_ATIME_SET;
139 iap->ia_atime.tv_sec = ntohl(*p++);
140 iap->ia_atime.tv_nsec = ntohl(*p++);
142 if ((tmp = ntohl(*p++)) == 1) { /* set to server time */
143 iap->ia_valid |= ATTR_MTIME;
144 } else if (tmp == 2) { /* set to client time */
145 iap->ia_valid |= ATTR_MTIME | ATTR_MTIME_SET;
146 iap->ia_mtime.tv_sec = ntohl(*p++);
147 iap->ia_mtime.tv_nsec = ntohl(*p++);
149 return p;
152 static __be32 *
153 encode_fattr3(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp,
154 struct kstat *stat)
156 struct dentry *dentry = fhp->fh_dentry;
157 struct timespec time;
159 *p++ = htonl(nfs3_ftypes[(stat->mode & S_IFMT) >> 12]);
160 *p++ = htonl((u32) stat->mode);
161 *p++ = htonl((u32) stat->nlink);
162 *p++ = htonl((u32) nfsd_ruid(rqstp, stat->uid));
163 *p++ = htonl((u32) nfsd_rgid(rqstp, stat->gid));
164 if (S_ISLNK(stat->mode) && stat->size > NFS3_MAXPATHLEN) {
165 p = xdr_encode_hyper(p, (u64) NFS3_MAXPATHLEN);
166 } else {
167 p = xdr_encode_hyper(p, (u64) stat->size);
169 p = xdr_encode_hyper(p, ((u64)stat->blocks) << 9);
170 *p++ = htonl((u32) MAJOR(stat->rdev));
171 *p++ = htonl((u32) MINOR(stat->rdev));
172 if (is_fsid(fhp, rqstp->rq_reffh))
173 p = xdr_encode_hyper(p, (u64) fhp->fh_export->ex_fsid);
174 else
175 p = xdr_encode_hyper(p, (u64) huge_encode_dev(stat->dev));
176 p = xdr_encode_hyper(p, (u64) stat->ino);
177 p = encode_time3(p, &stat->atime);
178 lease_get_mtime(dentry->d_inode, &time);
179 p = encode_time3(p, &time);
180 p = encode_time3(p, &stat->ctime);
182 return p;
185 static __be32 *
186 encode_saved_post_attr(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp)
188 struct inode *inode = fhp->fh_dentry->d_inode;
190 /* Attributes to follow */
191 *p++ = xdr_one;
193 *p++ = htonl(nfs3_ftypes[(fhp->fh_post_mode & S_IFMT) >> 12]);
194 *p++ = htonl((u32) fhp->fh_post_mode);
195 *p++ = htonl((u32) fhp->fh_post_nlink);
196 *p++ = htonl((u32) nfsd_ruid(rqstp, fhp->fh_post_uid));
197 *p++ = htonl((u32) nfsd_rgid(rqstp, fhp->fh_post_gid));
198 if (S_ISLNK(fhp->fh_post_mode) && fhp->fh_post_size > NFS3_MAXPATHLEN) {
199 p = xdr_encode_hyper(p, (u64) NFS3_MAXPATHLEN);
200 } else {
201 p = xdr_encode_hyper(p, (u64) fhp->fh_post_size);
203 p = xdr_encode_hyper(p, ((u64)fhp->fh_post_blocks) << 9);
204 *p++ = fhp->fh_post_rdev[0];
205 *p++ = fhp->fh_post_rdev[1];
206 if (is_fsid(fhp, rqstp->rq_reffh))
207 p = xdr_encode_hyper(p, (u64) fhp->fh_export->ex_fsid);
208 else
209 p = xdr_encode_hyper(p, (u64)huge_encode_dev(inode->i_sb->s_dev));
210 p = xdr_encode_hyper(p, (u64) inode->i_ino);
211 p = encode_time3(p, &fhp->fh_post_atime);
212 p = encode_time3(p, &fhp->fh_post_mtime);
213 p = encode_time3(p, &fhp->fh_post_ctime);
215 return p;
219 * Encode post-operation attributes.
220 * The inode may be NULL if the call failed because of a stale file
221 * handle. In this case, no attributes are returned.
223 static __be32 *
224 encode_post_op_attr(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp)
226 struct dentry *dentry = fhp->fh_dentry;
227 if (dentry && dentry->d_inode != NULL) {
228 int err;
229 struct kstat stat;
231 err = vfs_getattr(fhp->fh_export->ex_mnt, dentry, &stat);
232 if (!err) {
233 *p++ = xdr_one; /* attributes follow */
234 return encode_fattr3(rqstp, p, fhp, &stat);
237 *p++ = xdr_zero;
238 return p;
241 /* Helper for NFSv3 ACLs */
242 __be32 *
243 nfs3svc_encode_post_op_attr(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp)
245 return encode_post_op_attr(rqstp, p, fhp);
249 * Enocde weak cache consistency data
251 static __be32 *
252 encode_wcc_data(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp)
254 struct dentry *dentry = fhp->fh_dentry;
256 if (dentry && dentry->d_inode && fhp->fh_post_saved) {
257 if (fhp->fh_pre_saved) {
258 *p++ = xdr_one;
259 p = xdr_encode_hyper(p, (u64) fhp->fh_pre_size);
260 p = encode_time3(p, &fhp->fh_pre_mtime);
261 p = encode_time3(p, &fhp->fh_pre_ctime);
262 } else {
263 *p++ = xdr_zero;
265 return encode_saved_post_attr(rqstp, p, fhp);
267 /* no pre- or post-attrs */
268 *p++ = xdr_zero;
269 return encode_post_op_attr(rqstp, p, fhp);
274 * XDR decode functions
277 nfs3svc_decode_fhandle(struct svc_rqst *rqstp, __be32 *p, struct nfsd_fhandle *args)
279 if (!(p = decode_fh(p, &args->fh)))
280 return 0;
281 return xdr_argsize_check(rqstp, p);
285 nfs3svc_decode_sattrargs(struct svc_rqst *rqstp, __be32 *p,
286 struct nfsd3_sattrargs *args)
288 if (!(p = decode_fh(p, &args->fh))
289 || !(p = decode_sattr3(p, &args->attrs)))
290 return 0;
292 if ((args->check_guard = ntohl(*p++)) != 0) {
293 struct timespec time;
294 p = decode_time3(p, &time);
295 args->guardtime = time.tv_sec;
298 return xdr_argsize_check(rqstp, p);
302 nfs3svc_decode_diropargs(struct svc_rqst *rqstp, __be32 *p,
303 struct nfsd3_diropargs *args)
305 if (!(p = decode_fh(p, &args->fh))
306 || !(p = decode_filename(p, &args->name, &args->len)))
307 return 0;
309 return xdr_argsize_check(rqstp, p);
313 nfs3svc_decode_accessargs(struct svc_rqst *rqstp, __be32 *p,
314 struct nfsd3_accessargs *args)
316 if (!(p = decode_fh(p, &args->fh)))
317 return 0;
318 args->access = ntohl(*p++);
320 return xdr_argsize_check(rqstp, p);
324 nfs3svc_decode_readargs(struct svc_rqst *rqstp, __be32 *p,
325 struct nfsd3_readargs *args)
327 unsigned int len;
328 int v,pn;
329 u32 max_blocksize = svc_max_payload(rqstp);
331 if (!(p = decode_fh(p, &args->fh))
332 || !(p = xdr_decode_hyper(p, &args->offset)))
333 return 0;
335 len = args->count = ntohl(*p++);
337 if (len > max_blocksize)
338 len = max_blocksize;
340 /* set up the kvec */
341 v=0;
342 while (len > 0) {
343 pn = rqstp->rq_resused++;
344 rqstp->rq_vec[v].iov_base = page_address(rqstp->rq_respages[pn]);
345 rqstp->rq_vec[v].iov_len = len < PAGE_SIZE? len : PAGE_SIZE;
346 len -= rqstp->rq_vec[v].iov_len;
347 v++;
349 args->vlen = v;
350 return xdr_argsize_check(rqstp, p);
354 nfs3svc_decode_writeargs(struct svc_rqst *rqstp, __be32 *p,
355 struct nfsd3_writeargs *args)
357 unsigned int len, v, hdr;
358 u32 max_blocksize = svc_max_payload(rqstp);
360 if (!(p = decode_fh(p, &args->fh))
361 || !(p = xdr_decode_hyper(p, &args->offset)))
362 return 0;
364 args->count = ntohl(*p++);
365 args->stable = ntohl(*p++);
366 len = args->len = ntohl(*p++);
368 hdr = (void*)p - rqstp->rq_arg.head[0].iov_base;
369 if (rqstp->rq_arg.len < hdr ||
370 rqstp->rq_arg.len - hdr < len)
371 return 0;
373 rqstp->rq_vec[0].iov_base = (void*)p;
374 rqstp->rq_vec[0].iov_len = rqstp->rq_arg.head[0].iov_len - hdr;
376 if (len > max_blocksize)
377 len = max_blocksize;
378 v= 0;
379 while (len > rqstp->rq_vec[v].iov_len) {
380 len -= rqstp->rq_vec[v].iov_len;
381 v++;
382 rqstp->rq_vec[v].iov_base = page_address(rqstp->rq_pages[v]);
383 rqstp->rq_vec[v].iov_len = PAGE_SIZE;
385 rqstp->rq_vec[v].iov_len = len;
386 args->vlen = v+1;
388 return args->count == args->len && rqstp->rq_vec[0].iov_len > 0;
392 nfs3svc_decode_createargs(struct svc_rqst *rqstp, __be32 *p,
393 struct nfsd3_createargs *args)
395 if (!(p = decode_fh(p, &args->fh))
396 || !(p = decode_filename(p, &args->name, &args->len)))
397 return 0;
399 switch (args->createmode = ntohl(*p++)) {
400 case NFS3_CREATE_UNCHECKED:
401 case NFS3_CREATE_GUARDED:
402 if (!(p = decode_sattr3(p, &args->attrs)))
403 return 0;
404 break;
405 case NFS3_CREATE_EXCLUSIVE:
406 args->verf = p;
407 p += 2;
408 break;
409 default:
410 return 0;
413 return xdr_argsize_check(rqstp, p);
416 nfs3svc_decode_mkdirargs(struct svc_rqst *rqstp, __be32 *p,
417 struct nfsd3_createargs *args)
419 if (!(p = decode_fh(p, &args->fh))
420 || !(p = decode_filename(p, &args->name, &args->len))
421 || !(p = decode_sattr3(p, &args->attrs)))
422 return 0;
424 return xdr_argsize_check(rqstp, p);
428 nfs3svc_decode_symlinkargs(struct svc_rqst *rqstp, __be32 *p,
429 struct nfsd3_symlinkargs *args)
431 unsigned int len;
432 int avail;
433 char *old, *new;
434 struct kvec *vec;
436 if (!(p = decode_fh(p, &args->ffh))
437 || !(p = decode_filename(p, &args->fname, &args->flen))
438 || !(p = decode_sattr3(p, &args->attrs))
440 return 0;
441 /* now decode the pathname, which might be larger than the first page.
442 * As we have to check for nul's anyway, we copy it into a new page
443 * This page appears in the rq_res.pages list, but as pages_len is always
444 * 0, it won't get in the way
446 len = ntohl(*p++);
447 if (len == 0 || len > NFS3_MAXPATHLEN || len >= PAGE_SIZE)
448 return 0;
449 args->tname = new =
450 page_address(rqstp->rq_respages[rqstp->rq_resused++]);
451 args->tlen = len;
452 /* first copy and check from the first page */
453 old = (char*)p;
454 vec = &rqstp->rq_arg.head[0];
455 avail = vec->iov_len - (old - (char*)vec->iov_base);
456 while (len && avail && *old) {
457 *new++ = *old++;
458 len--;
459 avail--;
461 /* now copy next page if there is one */
462 if (len && !avail && rqstp->rq_arg.page_len) {
463 avail = rqstp->rq_arg.page_len;
464 if (avail > PAGE_SIZE) avail = PAGE_SIZE;
465 old = page_address(rqstp->rq_arg.pages[0]);
467 while (len && avail && *old) {
468 *new++ = *old++;
469 len--;
470 avail--;
472 *new = '\0';
473 if (len)
474 return 0;
476 return 1;
480 nfs3svc_decode_mknodargs(struct svc_rqst *rqstp, __be32 *p,
481 struct nfsd3_mknodargs *args)
483 if (!(p = decode_fh(p, &args->fh))
484 || !(p = decode_filename(p, &args->name, &args->len)))
485 return 0;
487 args->ftype = ntohl(*p++);
489 if (args->ftype == NF3BLK || args->ftype == NF3CHR
490 || args->ftype == NF3SOCK || args->ftype == NF3FIFO) {
491 if (!(p = decode_sattr3(p, &args->attrs)))
492 return 0;
495 if (args->ftype == NF3BLK || args->ftype == NF3CHR) {
496 args->major = ntohl(*p++);
497 args->minor = ntohl(*p++);
500 return xdr_argsize_check(rqstp, p);
504 nfs3svc_decode_renameargs(struct svc_rqst *rqstp, __be32 *p,
505 struct nfsd3_renameargs *args)
507 if (!(p = decode_fh(p, &args->ffh))
508 || !(p = decode_filename(p, &args->fname, &args->flen))
509 || !(p = decode_fh(p, &args->tfh))
510 || !(p = decode_filename(p, &args->tname, &args->tlen)))
511 return 0;
513 return xdr_argsize_check(rqstp, p);
517 nfs3svc_decode_readlinkargs(struct svc_rqst *rqstp, __be32 *p,
518 struct nfsd3_readlinkargs *args)
520 if (!(p = decode_fh(p, &args->fh)))
521 return 0;
522 args->buffer =
523 page_address(rqstp->rq_respages[rqstp->rq_resused++]);
525 return xdr_argsize_check(rqstp, p);
529 nfs3svc_decode_linkargs(struct svc_rqst *rqstp, __be32 *p,
530 struct nfsd3_linkargs *args)
532 if (!(p = decode_fh(p, &args->ffh))
533 || !(p = decode_fh(p, &args->tfh))
534 || !(p = decode_filename(p, &args->tname, &args->tlen)))
535 return 0;
537 return xdr_argsize_check(rqstp, p);
541 nfs3svc_decode_readdirargs(struct svc_rqst *rqstp, __be32 *p,
542 struct nfsd3_readdirargs *args)
544 if (!(p = decode_fh(p, &args->fh)))
545 return 0;
546 p = xdr_decode_hyper(p, &args->cookie);
547 args->verf = p; p += 2;
548 args->dircount = ~0;
549 args->count = ntohl(*p++);
551 if (args->count > PAGE_SIZE)
552 args->count = PAGE_SIZE;
554 args->buffer =
555 page_address(rqstp->rq_respages[rqstp->rq_resused++]);
557 return xdr_argsize_check(rqstp, p);
561 nfs3svc_decode_readdirplusargs(struct svc_rqst *rqstp, __be32 *p,
562 struct nfsd3_readdirargs *args)
564 int len, pn;
565 u32 max_blocksize = svc_max_payload(rqstp);
567 if (!(p = decode_fh(p, &args->fh)))
568 return 0;
569 p = xdr_decode_hyper(p, &args->cookie);
570 args->verf = p; p += 2;
571 args->dircount = ntohl(*p++);
572 args->count = ntohl(*p++);
574 len = (args->count > max_blocksize) ? max_blocksize :
575 args->count;
576 args->count = len;
578 while (len > 0) {
579 pn = rqstp->rq_resused++;
580 if (!args->buffer)
581 args->buffer = page_address(rqstp->rq_respages[pn]);
582 len -= PAGE_SIZE;
585 return xdr_argsize_check(rqstp, p);
589 nfs3svc_decode_commitargs(struct svc_rqst *rqstp, __be32 *p,
590 struct nfsd3_commitargs *args)
592 if (!(p = decode_fh(p, &args->fh)))
593 return 0;
594 p = xdr_decode_hyper(p, &args->offset);
595 args->count = ntohl(*p++);
597 return xdr_argsize_check(rqstp, p);
601 * XDR encode functions
604 * There must be an encoding function for void results so svc_process
605 * will work properly.
608 nfs3svc_encode_voidres(struct svc_rqst *rqstp, __be32 *p, void *dummy)
610 return xdr_ressize_check(rqstp, p);
613 /* GETATTR */
615 nfs3svc_encode_attrstat(struct svc_rqst *rqstp, __be32 *p,
616 struct nfsd3_attrstat *resp)
618 if (resp->status == 0)
619 p = encode_fattr3(rqstp, p, &resp->fh, &resp->stat);
620 return xdr_ressize_check(rqstp, p);
623 /* SETATTR, REMOVE, RMDIR */
625 nfs3svc_encode_wccstat(struct svc_rqst *rqstp, __be32 *p,
626 struct nfsd3_attrstat *resp)
628 p = encode_wcc_data(rqstp, p, &resp->fh);
629 return xdr_ressize_check(rqstp, p);
632 /* LOOKUP */
634 nfs3svc_encode_diropres(struct svc_rqst *rqstp, __be32 *p,
635 struct nfsd3_diropres *resp)
637 if (resp->status == 0) {
638 p = encode_fh(p, &resp->fh);
639 p = encode_post_op_attr(rqstp, p, &resp->fh);
641 p = encode_post_op_attr(rqstp, p, &resp->dirfh);
642 return xdr_ressize_check(rqstp, p);
645 /* ACCESS */
647 nfs3svc_encode_accessres(struct svc_rqst *rqstp, __be32 *p,
648 struct nfsd3_accessres *resp)
650 p = encode_post_op_attr(rqstp, p, &resp->fh);
651 if (resp->status == 0)
652 *p++ = htonl(resp->access);
653 return xdr_ressize_check(rqstp, p);
656 /* READLINK */
658 nfs3svc_encode_readlinkres(struct svc_rqst *rqstp, __be32 *p,
659 struct nfsd3_readlinkres *resp)
661 p = encode_post_op_attr(rqstp, p, &resp->fh);
662 if (resp->status == 0) {
663 *p++ = htonl(resp->len);
664 xdr_ressize_check(rqstp, p);
665 rqstp->rq_res.page_len = resp->len;
666 if (resp->len & 3) {
667 /* need to pad the tail */
668 rqstp->rq_res.tail[0].iov_base = p;
669 *p = 0;
670 rqstp->rq_res.tail[0].iov_len = 4 - (resp->len&3);
672 return 1;
673 } else
674 return xdr_ressize_check(rqstp, p);
677 /* READ */
679 nfs3svc_encode_readres(struct svc_rqst *rqstp, __be32 *p,
680 struct nfsd3_readres *resp)
682 p = encode_post_op_attr(rqstp, p, &resp->fh);
683 if (resp->status == 0) {
684 *p++ = htonl(resp->count);
685 *p++ = htonl(resp->eof);
686 *p++ = htonl(resp->count); /* xdr opaque count */
687 xdr_ressize_check(rqstp, p);
688 /* now update rqstp->rq_res to reflect data aswell */
689 rqstp->rq_res.page_len = resp->count;
690 if (resp->count & 3) {
691 /* need to pad the tail */
692 rqstp->rq_res.tail[0].iov_base = p;
693 *p = 0;
694 rqstp->rq_res.tail[0].iov_len = 4 - (resp->count & 3);
696 return 1;
697 } else
698 return xdr_ressize_check(rqstp, p);
701 /* WRITE */
703 nfs3svc_encode_writeres(struct svc_rqst *rqstp, __be32 *p,
704 struct nfsd3_writeres *resp)
706 p = encode_wcc_data(rqstp, p, &resp->fh);
707 if (resp->status == 0) {
708 *p++ = htonl(resp->count);
709 *p++ = htonl(resp->committed);
710 *p++ = htonl(nfssvc_boot.tv_sec);
711 *p++ = htonl(nfssvc_boot.tv_usec);
713 return xdr_ressize_check(rqstp, p);
716 /* CREATE, MKDIR, SYMLINK, MKNOD */
718 nfs3svc_encode_createres(struct svc_rqst *rqstp, __be32 *p,
719 struct nfsd3_diropres *resp)
721 if (resp->status == 0) {
722 *p++ = xdr_one;
723 p = encode_fh(p, &resp->fh);
724 p = encode_post_op_attr(rqstp, p, &resp->fh);
726 p = encode_wcc_data(rqstp, p, &resp->dirfh);
727 return xdr_ressize_check(rqstp, p);
730 /* RENAME */
732 nfs3svc_encode_renameres(struct svc_rqst *rqstp, __be32 *p,
733 struct nfsd3_renameres *resp)
735 p = encode_wcc_data(rqstp, p, &resp->ffh);
736 p = encode_wcc_data(rqstp, p, &resp->tfh);
737 return xdr_ressize_check(rqstp, p);
740 /* LINK */
742 nfs3svc_encode_linkres(struct svc_rqst *rqstp, __be32 *p,
743 struct nfsd3_linkres *resp)
745 p = encode_post_op_attr(rqstp, p, &resp->fh);
746 p = encode_wcc_data(rqstp, p, &resp->tfh);
747 return xdr_ressize_check(rqstp, p);
750 /* READDIR */
752 nfs3svc_encode_readdirres(struct svc_rqst *rqstp, __be32 *p,
753 struct nfsd3_readdirres *resp)
755 p = encode_post_op_attr(rqstp, p, &resp->fh);
757 if (resp->status == 0) {
758 /* stupid readdir cookie */
759 memcpy(p, resp->verf, 8); p += 2;
760 xdr_ressize_check(rqstp, p);
761 if (rqstp->rq_res.head[0].iov_len + (2<<2) > PAGE_SIZE)
762 return 1; /*No room for trailer */
763 rqstp->rq_res.page_len = (resp->count) << 2;
765 /* add the 'tail' to the end of the 'head' page - page 0. */
766 rqstp->rq_res.tail[0].iov_base = p;
767 *p++ = 0; /* no more entries */
768 *p++ = htonl(resp->common.err == nfserr_eof);
769 rqstp->rq_res.tail[0].iov_len = 2<<2;
770 return 1;
771 } else
772 return xdr_ressize_check(rqstp, p);
775 static __be32 *
776 encode_entry_baggage(struct nfsd3_readdirres *cd, __be32 *p, const char *name,
777 int namlen, ino_t ino)
779 *p++ = xdr_one; /* mark entry present */
780 p = xdr_encode_hyper(p, ino); /* file id */
781 p = xdr_encode_array(p, name, namlen);/* name length & name */
783 cd->offset = p; /* remember pointer */
784 p = xdr_encode_hyper(p, NFS_OFFSET_MAX);/* offset of next entry */
786 return p;
789 static __be32 *
790 encode_entryplus_baggage(struct nfsd3_readdirres *cd, __be32 *p,
791 struct svc_fh *fhp)
793 p = encode_post_op_attr(cd->rqstp, p, fhp);
794 *p++ = xdr_one; /* yes, a file handle follows */
795 p = encode_fh(p, fhp);
796 fh_put(fhp);
797 return p;
800 static int
801 compose_entry_fh(struct nfsd3_readdirres *cd, struct svc_fh *fhp,
802 const char *name, int namlen)
804 struct svc_export *exp;
805 struct dentry *dparent, *dchild;
806 int rv = 0;
808 dparent = cd->fh.fh_dentry;
809 exp = cd->fh.fh_export;
811 fh_init(fhp, NFS3_FHSIZE);
812 if (isdotent(name, namlen)) {
813 if (namlen == 2) {
814 dchild = dget_parent(dparent);
815 if (dchild == dparent) {
816 /* filesystem root - cannot return filehandle for ".." */
817 dput(dchild);
818 return 1;
820 } else
821 dchild = dget(dparent);
822 } else
823 dchild = lookup_one_len(name, dparent, namlen);
824 if (IS_ERR(dchild))
825 return 1;
826 if (d_mountpoint(dchild) ||
827 fh_compose(fhp, exp, dchild, &cd->fh) != 0 ||
828 !dchild->d_inode)
829 rv = 1;
830 dput(dchild);
831 return rv;
835 * Encode a directory entry. This one works for both normal readdir
836 * and readdirplus.
837 * The normal readdir reply requires 2 (fileid) + 1 (stringlen)
838 * + string + 2 (cookie) + 1 (next) words, i.e. 6 + strlen.
840 * The readdirplus baggage is 1+21 words for post_op_attr, plus the
841 * file handle.
844 #define NFS3_ENTRY_BAGGAGE (2 + 1 + 2 + 1)
845 #define NFS3_ENTRYPLUS_BAGGAGE (1 + 21 + 1 + (NFS3_FHSIZE >> 2))
846 static int
847 encode_entry(struct readdir_cd *ccd, const char *name,
848 int namlen, off_t offset, ino_t ino, unsigned int d_type, int plus)
850 struct nfsd3_readdirres *cd = container_of(ccd, struct nfsd3_readdirres,
851 common);
852 __be32 *p = cd->buffer;
853 caddr_t curr_page_addr = NULL;
854 int pn; /* current page number */
855 int slen; /* string (name) length */
856 int elen; /* estimated entry length in words */
857 int num_entry_words = 0; /* actual number of words */
859 if (cd->offset) {
860 u64 offset64 = offset;
862 if (unlikely(cd->offset1)) {
863 /* we ended up with offset on a page boundary */
864 *cd->offset = htonl(offset64 >> 32);
865 *cd->offset1 = htonl(offset64 & 0xffffffff);
866 cd->offset1 = NULL;
867 } else {
868 xdr_encode_hyper(cd->offset, (u64) offset);
873 dprintk("encode_entry(%.*s @%ld%s)\n",
874 namlen, name, (long) offset, plus? " plus" : "");
877 /* truncate filename if too long */
878 if (namlen > NFS3_MAXNAMLEN)
879 namlen = NFS3_MAXNAMLEN;
881 slen = XDR_QUADLEN(namlen);
882 elen = slen + NFS3_ENTRY_BAGGAGE
883 + (plus? NFS3_ENTRYPLUS_BAGGAGE : 0);
885 if (cd->buflen < elen) {
886 cd->common.err = nfserr_toosmall;
887 return -EINVAL;
890 /* determine which page in rq_respages[] we are currently filling */
891 for (pn=1; pn < cd->rqstp->rq_resused; pn++) {
892 curr_page_addr = page_address(cd->rqstp->rq_respages[pn]);
894 if (((caddr_t)cd->buffer >= curr_page_addr) &&
895 ((caddr_t)cd->buffer < curr_page_addr + PAGE_SIZE))
896 break;
899 if ((caddr_t)(cd->buffer + elen) < (curr_page_addr + PAGE_SIZE)) {
900 /* encode entry in current page */
902 p = encode_entry_baggage(cd, p, name, namlen, ino);
904 /* throw in readdirplus baggage */
905 if (plus) {
906 struct svc_fh fh;
908 if (compose_entry_fh(cd, &fh, name, namlen) > 0) {
909 *p++ = 0;
910 *p++ = 0;
911 } else
912 p = encode_entryplus_baggage(cd, p, &fh);
914 num_entry_words = p - cd->buffer;
915 } else if (cd->rqstp->rq_respages[pn+1] != NULL) {
916 /* temporarily encode entry into next page, then move back to
917 * current and next page in rq_respages[] */
918 __be32 *p1, *tmp;
919 int len1, len2;
921 /* grab next page for temporary storage of entry */
922 p1 = tmp = page_address(cd->rqstp->rq_respages[pn+1]);
924 p1 = encode_entry_baggage(cd, p1, name, namlen, ino);
926 /* throw in readdirplus baggage */
927 if (plus) {
928 struct svc_fh fh;
930 if (compose_entry_fh(cd, &fh, name, namlen) > 0) {
931 /* zero out the filehandle */
932 *p1++ = 0;
933 *p1++ = 0;
934 } else
935 p1 = encode_entryplus_baggage(cd, p1, &fh);
938 /* determine entry word length and lengths to go in pages */
939 num_entry_words = p1 - tmp;
940 len1 = curr_page_addr + PAGE_SIZE - (caddr_t)cd->buffer;
941 if ((num_entry_words << 2) < len1) {
942 /* the actual number of words in the entry is less
943 * than elen and can still fit in the current page
945 memmove(p, tmp, num_entry_words << 2);
946 p += num_entry_words;
948 /* update offset */
949 cd->offset = cd->buffer + (cd->offset - tmp);
950 } else {
951 unsigned int offset_r = (cd->offset - tmp) << 2;
953 /* update pointer to offset location.
954 * This is a 64bit quantity, so we need to
955 * deal with 3 cases:
956 * - entirely in first page
957 * - entirely in second page
958 * - 4 bytes in each page
960 if (offset_r + 8 <= len1) {
961 cd->offset = p + (cd->offset - tmp);
962 } else if (offset_r >= len1) {
963 cd->offset -= len1 >> 2;
964 } else {
965 /* sitting on the fence */
966 BUG_ON(offset_r != len1 - 4);
967 cd->offset = p + (cd->offset - tmp);
968 cd->offset1 = tmp;
971 len2 = (num_entry_words << 2) - len1;
973 /* move from temp page to current and next pages */
974 memmove(p, tmp, len1);
975 memmove(tmp, (caddr_t)tmp+len1, len2);
977 p = tmp + (len2 >> 2);
980 else {
981 cd->common.err = nfserr_toosmall;
982 return -EINVAL;
985 cd->buflen -= num_entry_words;
986 cd->buffer = p;
987 cd->common.err = nfs_ok;
988 return 0;
993 nfs3svc_encode_entry(void *cd, const char *name,
994 int namlen, loff_t offset, u64 ino, unsigned int d_type)
996 return encode_entry(cd, name, namlen, offset, ino, d_type, 0);
1000 nfs3svc_encode_entry_plus(void *cd, const char *name,
1001 int namlen, loff_t offset, u64 ino,
1002 unsigned int d_type)
1004 return encode_entry(cd, name, namlen, offset, ino, d_type, 1);
1007 /* FSSTAT */
1009 nfs3svc_encode_fsstatres(struct svc_rqst *rqstp, __be32 *p,
1010 struct nfsd3_fsstatres *resp)
1012 struct kstatfs *s = &resp->stats;
1013 u64 bs = s->f_bsize;
1015 *p++ = xdr_zero; /* no post_op_attr */
1017 if (resp->status == 0) {
1018 p = xdr_encode_hyper(p, bs * s->f_blocks); /* total bytes */
1019 p = xdr_encode_hyper(p, bs * s->f_bfree); /* free bytes */
1020 p = xdr_encode_hyper(p, bs * s->f_bavail); /* user available bytes */
1021 p = xdr_encode_hyper(p, s->f_files); /* total inodes */
1022 p = xdr_encode_hyper(p, s->f_ffree); /* free inodes */
1023 p = xdr_encode_hyper(p, s->f_ffree); /* user available inodes */
1024 *p++ = htonl(resp->invarsec); /* mean unchanged time */
1026 return xdr_ressize_check(rqstp, p);
1029 /* FSINFO */
1031 nfs3svc_encode_fsinfores(struct svc_rqst *rqstp, __be32 *p,
1032 struct nfsd3_fsinfores *resp)
1034 *p++ = xdr_zero; /* no post_op_attr */
1036 if (resp->status == 0) {
1037 *p++ = htonl(resp->f_rtmax);
1038 *p++ = htonl(resp->f_rtpref);
1039 *p++ = htonl(resp->f_rtmult);
1040 *p++ = htonl(resp->f_wtmax);
1041 *p++ = htonl(resp->f_wtpref);
1042 *p++ = htonl(resp->f_wtmult);
1043 *p++ = htonl(resp->f_dtpref);
1044 p = xdr_encode_hyper(p, resp->f_maxfilesize);
1045 *p++ = xdr_one;
1046 *p++ = xdr_zero;
1047 *p++ = htonl(resp->f_properties);
1050 return xdr_ressize_check(rqstp, p);
1053 /* PATHCONF */
1055 nfs3svc_encode_pathconfres(struct svc_rqst *rqstp, __be32 *p,
1056 struct nfsd3_pathconfres *resp)
1058 *p++ = xdr_zero; /* no post_op_attr */
1060 if (resp->status == 0) {
1061 *p++ = htonl(resp->p_link_max);
1062 *p++ = htonl(resp->p_name_max);
1063 *p++ = htonl(resp->p_no_trunc);
1064 *p++ = htonl(resp->p_chown_restricted);
1065 *p++ = htonl(resp->p_case_insensitive);
1066 *p++ = htonl(resp->p_case_preserving);
1069 return xdr_ressize_check(rqstp, p);
1072 /* COMMIT */
1074 nfs3svc_encode_commitres(struct svc_rqst *rqstp, __be32 *p,
1075 struct nfsd3_commitres *resp)
1077 p = encode_wcc_data(rqstp, p, &resp->fh);
1078 /* Write verifier */
1079 if (resp->status == 0) {
1080 *p++ = htonl(nfssvc_boot.tv_sec);
1081 *p++ = htonl(nfssvc_boot.tv_usec);
1083 return xdr_ressize_check(rqstp, p);
1087 * XDR release functions
1090 nfs3svc_release_fhandle(struct svc_rqst *rqstp, __be32 *p,
1091 struct nfsd3_attrstat *resp)
1093 fh_put(&resp->fh);
1094 return 1;
1098 nfs3svc_release_fhandle2(struct svc_rqst *rqstp, __be32 *p,
1099 struct nfsd3_fhandle_pair *resp)
1101 fh_put(&resp->fh1);
1102 fh_put(&resp->fh2);
1103 return 1;