Use access mode instead of open flags to determine needed permissions
[linux-2.6/openmoko-kernel/knife-kernel.git] / fs / nfsd / nfs3xdr.c
blob2d116d2298f8e3aec23982bc04795dd9daf69ca2
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 *encode_fsid(__be32 *p, struct svc_fh *fhp)
154 u64 f;
155 switch(fsid_source(fhp)) {
156 default:
157 case FSIDSOURCE_DEV:
158 p = xdr_encode_hyper(p, (u64)huge_encode_dev
159 (fhp->fh_dentry->d_inode->i_sb->s_dev));
160 break;
161 case FSIDSOURCE_FSID:
162 p = xdr_encode_hyper(p, (u64) fhp->fh_export->ex_fsid);
163 break;
164 case FSIDSOURCE_UUID:
165 f = ((u64*)fhp->fh_export->ex_uuid)[0];
166 f ^= ((u64*)fhp->fh_export->ex_uuid)[1];
167 p = xdr_encode_hyper(p, f);
168 break;
170 return p;
173 static __be32 *
174 encode_fattr3(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp,
175 struct kstat *stat)
177 *p++ = htonl(nfs3_ftypes[(stat->mode & S_IFMT) >> 12]);
178 *p++ = htonl((u32) stat->mode);
179 *p++ = htonl((u32) stat->nlink);
180 *p++ = htonl((u32) nfsd_ruid(rqstp, stat->uid));
181 *p++ = htonl((u32) nfsd_rgid(rqstp, stat->gid));
182 if (S_ISLNK(stat->mode) && stat->size > NFS3_MAXPATHLEN) {
183 p = xdr_encode_hyper(p, (u64) NFS3_MAXPATHLEN);
184 } else {
185 p = xdr_encode_hyper(p, (u64) stat->size);
187 p = xdr_encode_hyper(p, ((u64)stat->blocks) << 9);
188 *p++ = htonl((u32) MAJOR(stat->rdev));
189 *p++ = htonl((u32) MINOR(stat->rdev));
190 p = encode_fsid(p, fhp);
191 p = xdr_encode_hyper(p, stat->ino);
192 p = encode_time3(p, &stat->atime);
193 p = encode_time3(p, &stat->mtime);
194 p = encode_time3(p, &stat->ctime);
196 return p;
199 static __be32 *
200 encode_saved_post_attr(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp)
202 /* Attributes to follow */
203 *p++ = xdr_one;
204 return encode_fattr3(rqstp, p, fhp, &fhp->fh_post_attr);
208 * Encode post-operation attributes.
209 * The inode may be NULL if the call failed because of a stale file
210 * handle. In this case, no attributes are returned.
212 static __be32 *
213 encode_post_op_attr(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp)
215 struct dentry *dentry = fhp->fh_dentry;
216 if (dentry && dentry->d_inode) {
217 int err;
218 struct kstat stat;
220 err = vfs_getattr(fhp->fh_export->ex_mnt, dentry, &stat);
221 if (!err) {
222 *p++ = xdr_one; /* attributes follow */
223 lease_get_mtime(dentry->d_inode, &stat.mtime);
224 return encode_fattr3(rqstp, p, fhp, &stat);
227 *p++ = xdr_zero;
228 return p;
231 /* Helper for NFSv3 ACLs */
232 __be32 *
233 nfs3svc_encode_post_op_attr(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp)
235 return encode_post_op_attr(rqstp, p, fhp);
239 * Enocde weak cache consistency data
241 static __be32 *
242 encode_wcc_data(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp)
244 struct dentry *dentry = fhp->fh_dentry;
246 if (dentry && dentry->d_inode && fhp->fh_post_saved) {
247 if (fhp->fh_pre_saved) {
248 *p++ = xdr_one;
249 p = xdr_encode_hyper(p, (u64) fhp->fh_pre_size);
250 p = encode_time3(p, &fhp->fh_pre_mtime);
251 p = encode_time3(p, &fhp->fh_pre_ctime);
252 } else {
253 *p++ = xdr_zero;
255 return encode_saved_post_attr(rqstp, p, fhp);
257 /* no pre- or post-attrs */
258 *p++ = xdr_zero;
259 return encode_post_op_attr(rqstp, p, fhp);
263 * Fill in the post_op attr for the wcc data
265 void fill_post_wcc(struct svc_fh *fhp)
267 int err;
269 if (fhp->fh_post_saved)
270 printk("nfsd: inode locked twice during operation.\n");
272 err = vfs_getattr(fhp->fh_export->ex_mnt, fhp->fh_dentry,
273 &fhp->fh_post_attr);
274 if (err)
275 fhp->fh_post_saved = 0;
276 else
277 fhp->fh_post_saved = 1;
281 * XDR decode functions
284 nfs3svc_decode_fhandle(struct svc_rqst *rqstp, __be32 *p, struct nfsd_fhandle *args)
286 if (!(p = decode_fh(p, &args->fh)))
287 return 0;
288 return xdr_argsize_check(rqstp, p);
292 nfs3svc_decode_sattrargs(struct svc_rqst *rqstp, __be32 *p,
293 struct nfsd3_sattrargs *args)
295 if (!(p = decode_fh(p, &args->fh)))
296 return 0;
297 p = decode_sattr3(p, &args->attrs);
299 if ((args->check_guard = ntohl(*p++)) != 0) {
300 struct timespec time;
301 p = decode_time3(p, &time);
302 args->guardtime = time.tv_sec;
305 return xdr_argsize_check(rqstp, p);
309 nfs3svc_decode_diropargs(struct svc_rqst *rqstp, __be32 *p,
310 struct nfsd3_diropargs *args)
312 if (!(p = decode_fh(p, &args->fh))
313 || !(p = decode_filename(p, &args->name, &args->len)))
314 return 0;
316 return xdr_argsize_check(rqstp, p);
320 nfs3svc_decode_accessargs(struct svc_rqst *rqstp, __be32 *p,
321 struct nfsd3_accessargs *args)
323 if (!(p = decode_fh(p, &args->fh)))
324 return 0;
325 args->access = ntohl(*p++);
327 return xdr_argsize_check(rqstp, p);
331 nfs3svc_decode_readargs(struct svc_rqst *rqstp, __be32 *p,
332 struct nfsd3_readargs *args)
334 unsigned int len;
335 int v,pn;
336 u32 max_blocksize = svc_max_payload(rqstp);
338 if (!(p = decode_fh(p, &args->fh)))
339 return 0;
340 p = xdr_decode_hyper(p, &args->offset);
342 len = args->count = ntohl(*p++);
344 if (len > max_blocksize)
345 len = max_blocksize;
347 /* set up the kvec */
348 v=0;
349 while (len > 0) {
350 pn = rqstp->rq_resused++;
351 rqstp->rq_vec[v].iov_base = page_address(rqstp->rq_respages[pn]);
352 rqstp->rq_vec[v].iov_len = len < PAGE_SIZE? len : PAGE_SIZE;
353 len -= rqstp->rq_vec[v].iov_len;
354 v++;
356 args->vlen = v;
357 return xdr_argsize_check(rqstp, p);
361 nfs3svc_decode_writeargs(struct svc_rqst *rqstp, __be32 *p,
362 struct nfsd3_writeargs *args)
364 unsigned int len, v, hdr, dlen;
365 u32 max_blocksize = svc_max_payload(rqstp);
367 if (!(p = decode_fh(p, &args->fh)))
368 return 0;
369 p = xdr_decode_hyper(p, &args->offset);
371 args->count = ntohl(*p++);
372 args->stable = ntohl(*p++);
373 len = args->len = ntohl(*p++);
375 * The count must equal the amount of data passed.
377 if (args->count != args->len)
378 return 0;
381 * Check to make sure that we got the right number of
382 * bytes.
384 hdr = (void*)p - rqstp->rq_arg.head[0].iov_base;
385 dlen = rqstp->rq_arg.head[0].iov_len + rqstp->rq_arg.page_len
386 - hdr;
388 * Round the length of the data which was specified up to
389 * the next multiple of XDR units and then compare that
390 * against the length which was actually received.
392 if (dlen != XDR_QUADLEN(len)*4)
393 return 0;
395 if (args->count > max_blocksize) {
396 args->count = max_blocksize;
397 len = args->len = max_blocksize;
399 rqstp->rq_vec[0].iov_base = (void*)p;
400 rqstp->rq_vec[0].iov_len = rqstp->rq_arg.head[0].iov_len - hdr;
401 v = 0;
402 while (len > rqstp->rq_vec[v].iov_len) {
403 len -= rqstp->rq_vec[v].iov_len;
404 v++;
405 rqstp->rq_vec[v].iov_base = page_address(rqstp->rq_pages[v]);
406 rqstp->rq_vec[v].iov_len = PAGE_SIZE;
408 rqstp->rq_vec[v].iov_len = len;
409 args->vlen = v + 1;
410 return 1;
414 nfs3svc_decode_createargs(struct svc_rqst *rqstp, __be32 *p,
415 struct nfsd3_createargs *args)
417 if (!(p = decode_fh(p, &args->fh))
418 || !(p = decode_filename(p, &args->name, &args->len)))
419 return 0;
421 switch (args->createmode = ntohl(*p++)) {
422 case NFS3_CREATE_UNCHECKED:
423 case NFS3_CREATE_GUARDED:
424 p = decode_sattr3(p, &args->attrs);
425 break;
426 case NFS3_CREATE_EXCLUSIVE:
427 args->verf = p;
428 p += 2;
429 break;
430 default:
431 return 0;
434 return xdr_argsize_check(rqstp, p);
437 nfs3svc_decode_mkdirargs(struct svc_rqst *rqstp, __be32 *p,
438 struct nfsd3_createargs *args)
440 if (!(p = decode_fh(p, &args->fh)) ||
441 !(p = decode_filename(p, &args->name, &args->len)))
442 return 0;
443 p = decode_sattr3(p, &args->attrs);
445 return xdr_argsize_check(rqstp, p);
449 nfs3svc_decode_symlinkargs(struct svc_rqst *rqstp, __be32 *p,
450 struct nfsd3_symlinkargs *args)
452 unsigned int len;
453 int avail;
454 char *old, *new;
455 struct kvec *vec;
457 if (!(p = decode_fh(p, &args->ffh)) ||
458 !(p = decode_filename(p, &args->fname, &args->flen))
460 return 0;
461 p = decode_sattr3(p, &args->attrs);
463 /* now decode the pathname, which might be larger than the first page.
464 * As we have to check for nul's anyway, we copy it into a new page
465 * This page appears in the rq_res.pages list, but as pages_len is always
466 * 0, it won't get in the way
468 len = ntohl(*p++);
469 if (len == 0 || len > NFS3_MAXPATHLEN || len >= PAGE_SIZE)
470 return 0;
471 args->tname = new =
472 page_address(rqstp->rq_respages[rqstp->rq_resused++]);
473 args->tlen = len;
474 /* first copy and check from the first page */
475 old = (char*)p;
476 vec = &rqstp->rq_arg.head[0];
477 avail = vec->iov_len - (old - (char*)vec->iov_base);
478 while (len && avail && *old) {
479 *new++ = *old++;
480 len--;
481 avail--;
483 /* now copy next page if there is one */
484 if (len && !avail && rqstp->rq_arg.page_len) {
485 avail = rqstp->rq_arg.page_len;
486 if (avail > PAGE_SIZE) avail = PAGE_SIZE;
487 old = page_address(rqstp->rq_arg.pages[0]);
489 while (len && avail && *old) {
490 *new++ = *old++;
491 len--;
492 avail--;
494 *new = '\0';
495 if (len)
496 return 0;
498 return 1;
502 nfs3svc_decode_mknodargs(struct svc_rqst *rqstp, __be32 *p,
503 struct nfsd3_mknodargs *args)
505 if (!(p = decode_fh(p, &args->fh))
506 || !(p = decode_filename(p, &args->name, &args->len)))
507 return 0;
509 args->ftype = ntohl(*p++);
511 if (args->ftype == NF3BLK || args->ftype == NF3CHR
512 || args->ftype == NF3SOCK || args->ftype == NF3FIFO)
513 p = decode_sattr3(p, &args->attrs);
515 if (args->ftype == NF3BLK || args->ftype == NF3CHR) {
516 args->major = ntohl(*p++);
517 args->minor = ntohl(*p++);
520 return xdr_argsize_check(rqstp, p);
524 nfs3svc_decode_renameargs(struct svc_rqst *rqstp, __be32 *p,
525 struct nfsd3_renameargs *args)
527 if (!(p = decode_fh(p, &args->ffh))
528 || !(p = decode_filename(p, &args->fname, &args->flen))
529 || !(p = decode_fh(p, &args->tfh))
530 || !(p = decode_filename(p, &args->tname, &args->tlen)))
531 return 0;
533 return xdr_argsize_check(rqstp, p);
537 nfs3svc_decode_readlinkargs(struct svc_rqst *rqstp, __be32 *p,
538 struct nfsd3_readlinkargs *args)
540 if (!(p = decode_fh(p, &args->fh)))
541 return 0;
542 args->buffer =
543 page_address(rqstp->rq_respages[rqstp->rq_resused++]);
545 return xdr_argsize_check(rqstp, p);
549 nfs3svc_decode_linkargs(struct svc_rqst *rqstp, __be32 *p,
550 struct nfsd3_linkargs *args)
552 if (!(p = decode_fh(p, &args->ffh))
553 || !(p = decode_fh(p, &args->tfh))
554 || !(p = decode_filename(p, &args->tname, &args->tlen)))
555 return 0;
557 return xdr_argsize_check(rqstp, p);
561 nfs3svc_decode_readdirargs(struct svc_rqst *rqstp, __be32 *p,
562 struct nfsd3_readdirargs *args)
564 if (!(p = decode_fh(p, &args->fh)))
565 return 0;
566 p = xdr_decode_hyper(p, &args->cookie);
567 args->verf = p; p += 2;
568 args->dircount = ~0;
569 args->count = ntohl(*p++);
571 if (args->count > PAGE_SIZE)
572 args->count = PAGE_SIZE;
574 args->buffer =
575 page_address(rqstp->rq_respages[rqstp->rq_resused++]);
577 return xdr_argsize_check(rqstp, p);
581 nfs3svc_decode_readdirplusargs(struct svc_rqst *rqstp, __be32 *p,
582 struct nfsd3_readdirargs *args)
584 int len, pn;
585 u32 max_blocksize = svc_max_payload(rqstp);
587 if (!(p = decode_fh(p, &args->fh)))
588 return 0;
589 p = xdr_decode_hyper(p, &args->cookie);
590 args->verf = p; p += 2;
591 args->dircount = ntohl(*p++);
592 args->count = ntohl(*p++);
594 len = (args->count > max_blocksize) ? max_blocksize :
595 args->count;
596 args->count = len;
598 while (len > 0) {
599 pn = rqstp->rq_resused++;
600 if (!args->buffer)
601 args->buffer = page_address(rqstp->rq_respages[pn]);
602 len -= PAGE_SIZE;
605 return xdr_argsize_check(rqstp, p);
609 nfs3svc_decode_commitargs(struct svc_rqst *rqstp, __be32 *p,
610 struct nfsd3_commitargs *args)
612 if (!(p = decode_fh(p, &args->fh)))
613 return 0;
614 p = xdr_decode_hyper(p, &args->offset);
615 args->count = ntohl(*p++);
617 return xdr_argsize_check(rqstp, p);
621 * XDR encode functions
624 * There must be an encoding function for void results so svc_process
625 * will work properly.
628 nfs3svc_encode_voidres(struct svc_rqst *rqstp, __be32 *p, void *dummy)
630 return xdr_ressize_check(rqstp, p);
633 /* GETATTR */
635 nfs3svc_encode_attrstat(struct svc_rqst *rqstp, __be32 *p,
636 struct nfsd3_attrstat *resp)
638 if (resp->status == 0) {
639 lease_get_mtime(resp->fh.fh_dentry->d_inode,
640 &resp->stat.mtime);
641 p = encode_fattr3(rqstp, p, &resp->fh, &resp->stat);
643 return xdr_ressize_check(rqstp, p);
646 /* SETATTR, REMOVE, RMDIR */
648 nfs3svc_encode_wccstat(struct svc_rqst *rqstp, __be32 *p,
649 struct nfsd3_attrstat *resp)
651 p = encode_wcc_data(rqstp, p, &resp->fh);
652 return xdr_ressize_check(rqstp, p);
655 /* LOOKUP */
657 nfs3svc_encode_diropres(struct svc_rqst *rqstp, __be32 *p,
658 struct nfsd3_diropres *resp)
660 if (resp->status == 0) {
661 p = encode_fh(p, &resp->fh);
662 p = encode_post_op_attr(rqstp, p, &resp->fh);
664 p = encode_post_op_attr(rqstp, p, &resp->dirfh);
665 return xdr_ressize_check(rqstp, p);
668 /* ACCESS */
670 nfs3svc_encode_accessres(struct svc_rqst *rqstp, __be32 *p,
671 struct nfsd3_accessres *resp)
673 p = encode_post_op_attr(rqstp, p, &resp->fh);
674 if (resp->status == 0)
675 *p++ = htonl(resp->access);
676 return xdr_ressize_check(rqstp, p);
679 /* READLINK */
681 nfs3svc_encode_readlinkres(struct svc_rqst *rqstp, __be32 *p,
682 struct nfsd3_readlinkres *resp)
684 p = encode_post_op_attr(rqstp, p, &resp->fh);
685 if (resp->status == 0) {
686 *p++ = htonl(resp->len);
687 xdr_ressize_check(rqstp, p);
688 rqstp->rq_res.page_len = resp->len;
689 if (resp->len & 3) {
690 /* need to pad the tail */
691 rqstp->rq_res.tail[0].iov_base = p;
692 *p = 0;
693 rqstp->rq_res.tail[0].iov_len = 4 - (resp->len&3);
695 return 1;
696 } else
697 return xdr_ressize_check(rqstp, p);
700 /* READ */
702 nfs3svc_encode_readres(struct svc_rqst *rqstp, __be32 *p,
703 struct nfsd3_readres *resp)
705 p = encode_post_op_attr(rqstp, p, &resp->fh);
706 if (resp->status == 0) {
707 *p++ = htonl(resp->count);
708 *p++ = htonl(resp->eof);
709 *p++ = htonl(resp->count); /* xdr opaque count */
710 xdr_ressize_check(rqstp, p);
711 /* now update rqstp->rq_res to reflect data aswell */
712 rqstp->rq_res.page_len = resp->count;
713 if (resp->count & 3) {
714 /* need to pad the tail */
715 rqstp->rq_res.tail[0].iov_base = p;
716 *p = 0;
717 rqstp->rq_res.tail[0].iov_len = 4 - (resp->count & 3);
719 return 1;
720 } else
721 return xdr_ressize_check(rqstp, p);
724 /* WRITE */
726 nfs3svc_encode_writeres(struct svc_rqst *rqstp, __be32 *p,
727 struct nfsd3_writeres *resp)
729 p = encode_wcc_data(rqstp, p, &resp->fh);
730 if (resp->status == 0) {
731 *p++ = htonl(resp->count);
732 *p++ = htonl(resp->committed);
733 *p++ = htonl(nfssvc_boot.tv_sec);
734 *p++ = htonl(nfssvc_boot.tv_usec);
736 return xdr_ressize_check(rqstp, p);
739 /* CREATE, MKDIR, SYMLINK, MKNOD */
741 nfs3svc_encode_createres(struct svc_rqst *rqstp, __be32 *p,
742 struct nfsd3_diropres *resp)
744 if (resp->status == 0) {
745 *p++ = xdr_one;
746 p = encode_fh(p, &resp->fh);
747 p = encode_post_op_attr(rqstp, p, &resp->fh);
749 p = encode_wcc_data(rqstp, p, &resp->dirfh);
750 return xdr_ressize_check(rqstp, p);
753 /* RENAME */
755 nfs3svc_encode_renameres(struct svc_rqst *rqstp, __be32 *p,
756 struct nfsd3_renameres *resp)
758 p = encode_wcc_data(rqstp, p, &resp->ffh);
759 p = encode_wcc_data(rqstp, p, &resp->tfh);
760 return xdr_ressize_check(rqstp, p);
763 /* LINK */
765 nfs3svc_encode_linkres(struct svc_rqst *rqstp, __be32 *p,
766 struct nfsd3_linkres *resp)
768 p = encode_post_op_attr(rqstp, p, &resp->fh);
769 p = encode_wcc_data(rqstp, p, &resp->tfh);
770 return xdr_ressize_check(rqstp, p);
773 /* READDIR */
775 nfs3svc_encode_readdirres(struct svc_rqst *rqstp, __be32 *p,
776 struct nfsd3_readdirres *resp)
778 p = encode_post_op_attr(rqstp, p, &resp->fh);
780 if (resp->status == 0) {
781 /* stupid readdir cookie */
782 memcpy(p, resp->verf, 8); p += 2;
783 xdr_ressize_check(rqstp, p);
784 if (rqstp->rq_res.head[0].iov_len + (2<<2) > PAGE_SIZE)
785 return 1; /*No room for trailer */
786 rqstp->rq_res.page_len = (resp->count) << 2;
788 /* add the 'tail' to the end of the 'head' page - page 0. */
789 rqstp->rq_res.tail[0].iov_base = p;
790 *p++ = 0; /* no more entries */
791 *p++ = htonl(resp->common.err == nfserr_eof);
792 rqstp->rq_res.tail[0].iov_len = 2<<2;
793 return 1;
794 } else
795 return xdr_ressize_check(rqstp, p);
798 static __be32 *
799 encode_entry_baggage(struct nfsd3_readdirres *cd, __be32 *p, const char *name,
800 int namlen, u64 ino)
802 *p++ = xdr_one; /* mark entry present */
803 p = xdr_encode_hyper(p, ino); /* file id */
804 p = xdr_encode_array(p, name, namlen);/* name length & name */
806 cd->offset = p; /* remember pointer */
807 p = xdr_encode_hyper(p, NFS_OFFSET_MAX);/* offset of next entry */
809 return p;
812 static __be32 *
813 encode_entryplus_baggage(struct nfsd3_readdirres *cd, __be32 *p,
814 struct svc_fh *fhp)
816 p = encode_post_op_attr(cd->rqstp, p, fhp);
817 *p++ = xdr_one; /* yes, a file handle follows */
818 p = encode_fh(p, fhp);
819 fh_put(fhp);
820 return p;
823 static int
824 compose_entry_fh(struct nfsd3_readdirres *cd, struct svc_fh *fhp,
825 const char *name, int namlen)
827 struct svc_export *exp;
828 struct dentry *dparent, *dchild;
829 int rv = 0;
831 dparent = cd->fh.fh_dentry;
832 exp = cd->fh.fh_export;
834 fh_init(fhp, NFS3_FHSIZE);
835 if (isdotent(name, namlen)) {
836 if (namlen == 2) {
837 dchild = dget_parent(dparent);
838 if (dchild == dparent) {
839 /* filesystem root - cannot return filehandle for ".." */
840 dput(dchild);
841 return 1;
843 } else
844 dchild = dget(dparent);
845 } else
846 dchild = lookup_one_len(name, dparent, namlen);
847 if (IS_ERR(dchild))
848 return 1;
849 if (d_mountpoint(dchild) ||
850 fh_compose(fhp, exp, dchild, &cd->fh) != 0 ||
851 !dchild->d_inode)
852 rv = 1;
853 dput(dchild);
854 return rv;
858 * Encode a directory entry. This one works for both normal readdir
859 * and readdirplus.
860 * The normal readdir reply requires 2 (fileid) + 1 (stringlen)
861 * + string + 2 (cookie) + 1 (next) words, i.e. 6 + strlen.
863 * The readdirplus baggage is 1+21 words for post_op_attr, plus the
864 * file handle.
867 #define NFS3_ENTRY_BAGGAGE (2 + 1 + 2 + 1)
868 #define NFS3_ENTRYPLUS_BAGGAGE (1 + 21 + 1 + (NFS3_FHSIZE >> 2))
869 static int
870 encode_entry(struct readdir_cd *ccd, const char *name, int namlen,
871 loff_t offset, u64 ino, unsigned int d_type, int plus)
873 struct nfsd3_readdirres *cd = container_of(ccd, struct nfsd3_readdirres,
874 common);
875 __be32 *p = cd->buffer;
876 caddr_t curr_page_addr = NULL;
877 int pn; /* current page number */
878 int slen; /* string (name) length */
879 int elen; /* estimated entry length in words */
880 int num_entry_words = 0; /* actual number of words */
882 if (cd->offset) {
883 u64 offset64 = offset;
885 if (unlikely(cd->offset1)) {
886 /* we ended up with offset on a page boundary */
887 *cd->offset = htonl(offset64 >> 32);
888 *cd->offset1 = htonl(offset64 & 0xffffffff);
889 cd->offset1 = NULL;
890 } else {
891 xdr_encode_hyper(cd->offset, offset64);
896 dprintk("encode_entry(%.*s @%ld%s)\n",
897 namlen, name, (long) offset, plus? " plus" : "");
900 /* truncate filename if too long */
901 if (namlen > NFS3_MAXNAMLEN)
902 namlen = NFS3_MAXNAMLEN;
904 slen = XDR_QUADLEN(namlen);
905 elen = slen + NFS3_ENTRY_BAGGAGE
906 + (plus? NFS3_ENTRYPLUS_BAGGAGE : 0);
908 if (cd->buflen < elen) {
909 cd->common.err = nfserr_toosmall;
910 return -EINVAL;
913 /* determine which page in rq_respages[] we are currently filling */
914 for (pn=1; pn < cd->rqstp->rq_resused; pn++) {
915 curr_page_addr = page_address(cd->rqstp->rq_respages[pn]);
917 if (((caddr_t)cd->buffer >= curr_page_addr) &&
918 ((caddr_t)cd->buffer < curr_page_addr + PAGE_SIZE))
919 break;
922 if ((caddr_t)(cd->buffer + elen) < (curr_page_addr + PAGE_SIZE)) {
923 /* encode entry in current page */
925 p = encode_entry_baggage(cd, p, name, namlen, ino);
927 /* throw in readdirplus baggage */
928 if (plus) {
929 struct svc_fh fh;
931 if (compose_entry_fh(cd, &fh, name, namlen) > 0) {
932 *p++ = 0;
933 *p++ = 0;
934 } else
935 p = encode_entryplus_baggage(cd, p, &fh);
937 num_entry_words = p - cd->buffer;
938 } else if (cd->rqstp->rq_respages[pn+1] != NULL) {
939 /* temporarily encode entry into next page, then move back to
940 * current and next page in rq_respages[] */
941 __be32 *p1, *tmp;
942 int len1, len2;
944 /* grab next page for temporary storage of entry */
945 p1 = tmp = page_address(cd->rqstp->rq_respages[pn+1]);
947 p1 = encode_entry_baggage(cd, p1, name, namlen, ino);
949 /* throw in readdirplus baggage */
950 if (plus) {
951 struct svc_fh fh;
953 if (compose_entry_fh(cd, &fh, name, namlen) > 0) {
954 /* zero out the filehandle */
955 *p1++ = 0;
956 *p1++ = 0;
957 } else
958 p1 = encode_entryplus_baggage(cd, p1, &fh);
961 /* determine entry word length and lengths to go in pages */
962 num_entry_words = p1 - tmp;
963 len1 = curr_page_addr + PAGE_SIZE - (caddr_t)cd->buffer;
964 if ((num_entry_words << 2) < len1) {
965 /* the actual number of words in the entry is less
966 * than elen and can still fit in the current page
968 memmove(p, tmp, num_entry_words << 2);
969 p += num_entry_words;
971 /* update offset */
972 cd->offset = cd->buffer + (cd->offset - tmp);
973 } else {
974 unsigned int offset_r = (cd->offset - tmp) << 2;
976 /* update pointer to offset location.
977 * This is a 64bit quantity, so we need to
978 * deal with 3 cases:
979 * - entirely in first page
980 * - entirely in second page
981 * - 4 bytes in each page
983 if (offset_r + 8 <= len1) {
984 cd->offset = p + (cd->offset - tmp);
985 } else if (offset_r >= len1) {
986 cd->offset -= len1 >> 2;
987 } else {
988 /* sitting on the fence */
989 BUG_ON(offset_r != len1 - 4);
990 cd->offset = p + (cd->offset - tmp);
991 cd->offset1 = tmp;
994 len2 = (num_entry_words << 2) - len1;
996 /* move from temp page to current and next pages */
997 memmove(p, tmp, len1);
998 memmove(tmp, (caddr_t)tmp+len1, len2);
1000 p = tmp + (len2 >> 2);
1003 else {
1004 cd->common.err = nfserr_toosmall;
1005 return -EINVAL;
1008 cd->buflen -= num_entry_words;
1009 cd->buffer = p;
1010 cd->common.err = nfs_ok;
1011 return 0;
1016 nfs3svc_encode_entry(void *cd, const char *name,
1017 int namlen, loff_t offset, u64 ino, unsigned int d_type)
1019 return encode_entry(cd, name, namlen, offset, ino, d_type, 0);
1023 nfs3svc_encode_entry_plus(void *cd, const char *name,
1024 int namlen, loff_t offset, u64 ino,
1025 unsigned int d_type)
1027 return encode_entry(cd, name, namlen, offset, ino, d_type, 1);
1030 /* FSSTAT */
1032 nfs3svc_encode_fsstatres(struct svc_rqst *rqstp, __be32 *p,
1033 struct nfsd3_fsstatres *resp)
1035 struct kstatfs *s = &resp->stats;
1036 u64 bs = s->f_bsize;
1038 *p++ = xdr_zero; /* no post_op_attr */
1040 if (resp->status == 0) {
1041 p = xdr_encode_hyper(p, bs * s->f_blocks); /* total bytes */
1042 p = xdr_encode_hyper(p, bs * s->f_bfree); /* free bytes */
1043 p = xdr_encode_hyper(p, bs * s->f_bavail); /* user available bytes */
1044 p = xdr_encode_hyper(p, s->f_files); /* total inodes */
1045 p = xdr_encode_hyper(p, s->f_ffree); /* free inodes */
1046 p = xdr_encode_hyper(p, s->f_ffree); /* user available inodes */
1047 *p++ = htonl(resp->invarsec); /* mean unchanged time */
1049 return xdr_ressize_check(rqstp, p);
1052 /* FSINFO */
1054 nfs3svc_encode_fsinfores(struct svc_rqst *rqstp, __be32 *p,
1055 struct nfsd3_fsinfores *resp)
1057 *p++ = xdr_zero; /* no post_op_attr */
1059 if (resp->status == 0) {
1060 *p++ = htonl(resp->f_rtmax);
1061 *p++ = htonl(resp->f_rtpref);
1062 *p++ = htonl(resp->f_rtmult);
1063 *p++ = htonl(resp->f_wtmax);
1064 *p++ = htonl(resp->f_wtpref);
1065 *p++ = htonl(resp->f_wtmult);
1066 *p++ = htonl(resp->f_dtpref);
1067 p = xdr_encode_hyper(p, resp->f_maxfilesize);
1068 *p++ = xdr_one;
1069 *p++ = xdr_zero;
1070 *p++ = htonl(resp->f_properties);
1073 return xdr_ressize_check(rqstp, p);
1076 /* PATHCONF */
1078 nfs3svc_encode_pathconfres(struct svc_rqst *rqstp, __be32 *p,
1079 struct nfsd3_pathconfres *resp)
1081 *p++ = xdr_zero; /* no post_op_attr */
1083 if (resp->status == 0) {
1084 *p++ = htonl(resp->p_link_max);
1085 *p++ = htonl(resp->p_name_max);
1086 *p++ = htonl(resp->p_no_trunc);
1087 *p++ = htonl(resp->p_chown_restricted);
1088 *p++ = htonl(resp->p_case_insensitive);
1089 *p++ = htonl(resp->p_case_preserving);
1092 return xdr_ressize_check(rqstp, p);
1095 /* COMMIT */
1097 nfs3svc_encode_commitres(struct svc_rqst *rqstp, __be32 *p,
1098 struct nfsd3_commitres *resp)
1100 p = encode_wcc_data(rqstp, p, &resp->fh);
1101 /* Write verifier */
1102 if (resp->status == 0) {
1103 *p++ = htonl(nfssvc_boot.tv_sec);
1104 *p++ = htonl(nfssvc_boot.tv_usec);
1106 return xdr_ressize_check(rqstp, p);
1110 * XDR release functions
1113 nfs3svc_release_fhandle(struct svc_rqst *rqstp, __be32 *p,
1114 struct nfsd3_attrstat *resp)
1116 fh_put(&resp->fh);
1117 return 1;
1121 nfs3svc_release_fhandle2(struct svc_rqst *rqstp, __be32 *p,
1122 struct nfsd3_fhandle_pair *resp)
1124 fh_put(&resp->fh1);
1125 fh_put(&resp->fh2);
1126 return 1;