ALSA: HDA: Fix internal microphone on Dell Studio 16 XPS 1645
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / nfsd / nfs3xdr.c
blob856c6d4daea6bc64024f218ee10c6d51cefa68bb
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>
24 #include "auth.h"
26 #define NFSDDBG_FACILITY NFSDDBG_XDR
30 * Mapping of S_IF* types to NFS file types
32 static u32 nfs3_ftypes[] = {
33 NF3NON, NF3FIFO, NF3CHR, NF3BAD,
34 NF3DIR, NF3BAD, NF3BLK, NF3BAD,
35 NF3REG, NF3BAD, NF3LNK, NF3BAD,
36 NF3SOCK, NF3BAD, NF3LNK, NF3BAD,
40 * XDR functions for basic NFS types
42 static __be32 *
43 encode_time3(__be32 *p, struct timespec *time)
45 *p++ = htonl((u32) time->tv_sec); *p++ = htonl(time->tv_nsec);
46 return p;
49 static __be32 *
50 decode_time3(__be32 *p, struct timespec *time)
52 time->tv_sec = ntohl(*p++);
53 time->tv_nsec = ntohl(*p++);
54 return p;
57 static __be32 *
58 decode_fh(__be32 *p, struct svc_fh *fhp)
60 unsigned int size;
61 fh_init(fhp, NFS3_FHSIZE);
62 size = ntohl(*p++);
63 if (size > NFS3_FHSIZE)
64 return NULL;
66 memcpy(&fhp->fh_handle.fh_base, p, size);
67 fhp->fh_handle.fh_size = size;
68 return p + XDR_QUADLEN(size);
71 /* Helper function for NFSv3 ACL code */
72 __be32 *nfs3svc_decode_fh(__be32 *p, struct svc_fh *fhp)
74 return decode_fh(p, fhp);
77 static __be32 *
78 encode_fh(__be32 *p, struct svc_fh *fhp)
80 unsigned int size = fhp->fh_handle.fh_size;
81 *p++ = htonl(size);
82 if (size) p[XDR_QUADLEN(size)-1]=0;
83 memcpy(p, &fhp->fh_handle.fh_base, size);
84 return p + XDR_QUADLEN(size);
88 * Decode a file name and make sure that the path contains
89 * no slashes or null bytes.
91 static __be32 *
92 decode_filename(__be32 *p, char **namp, unsigned int *lenp)
94 char *name;
95 unsigned int i;
97 if ((p = xdr_decode_string_inplace(p, namp, lenp, NFS3_MAXNAMLEN)) != NULL) {
98 for (i = 0, name = *namp; i < *lenp; i++, name++) {
99 if (*name == '\0' || *name == '/')
100 return NULL;
104 return p;
107 static __be32 *
108 decode_sattr3(__be32 *p, struct iattr *iap)
110 u32 tmp;
112 iap->ia_valid = 0;
114 if (*p++) {
115 iap->ia_valid |= ATTR_MODE;
116 iap->ia_mode = ntohl(*p++);
118 if (*p++) {
119 iap->ia_valid |= ATTR_UID;
120 iap->ia_uid = ntohl(*p++);
122 if (*p++) {
123 iap->ia_valid |= ATTR_GID;
124 iap->ia_gid = ntohl(*p++);
126 if (*p++) {
127 u64 newsize;
129 iap->ia_valid |= ATTR_SIZE;
130 p = xdr_decode_hyper(p, &newsize);
131 if (newsize <= NFS_OFFSET_MAX)
132 iap->ia_size = newsize;
133 else
134 iap->ia_size = NFS_OFFSET_MAX;
136 if ((tmp = ntohl(*p++)) == 1) { /* set to server time */
137 iap->ia_valid |= ATTR_ATIME;
138 } else if (tmp == 2) { /* set to client time */
139 iap->ia_valid |= ATTR_ATIME | ATTR_ATIME_SET;
140 iap->ia_atime.tv_sec = ntohl(*p++);
141 iap->ia_atime.tv_nsec = ntohl(*p++);
143 if ((tmp = ntohl(*p++)) == 1) { /* set to server time */
144 iap->ia_valid |= ATTR_MTIME;
145 } else if (tmp == 2) { /* set to client time */
146 iap->ia_valid |= ATTR_MTIME | ATTR_MTIME_SET;
147 iap->ia_mtime.tv_sec = ntohl(*p++);
148 iap->ia_mtime.tv_nsec = ntohl(*p++);
150 return p;
153 static __be32 *encode_fsid(__be32 *p, struct svc_fh *fhp)
155 u64 f;
156 switch(fsid_source(fhp)) {
157 default:
158 case FSIDSOURCE_DEV:
159 p = xdr_encode_hyper(p, (u64)huge_encode_dev
160 (fhp->fh_dentry->d_inode->i_sb->s_dev));
161 break;
162 case FSIDSOURCE_FSID:
163 p = xdr_encode_hyper(p, (u64) fhp->fh_export->ex_fsid);
164 break;
165 case FSIDSOURCE_UUID:
166 f = ((u64*)fhp->fh_export->ex_uuid)[0];
167 f ^= ((u64*)fhp->fh_export->ex_uuid)[1];
168 p = xdr_encode_hyper(p, f);
169 break;
171 return p;
174 static __be32 *
175 encode_fattr3(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp,
176 struct kstat *stat)
178 *p++ = htonl(nfs3_ftypes[(stat->mode & S_IFMT) >> 12]);
179 *p++ = htonl((u32) stat->mode);
180 *p++ = htonl((u32) stat->nlink);
181 *p++ = htonl((u32) nfsd_ruid(rqstp, stat->uid));
182 *p++ = htonl((u32) nfsd_rgid(rqstp, stat->gid));
183 if (S_ISLNK(stat->mode) && stat->size > NFS3_MAXPATHLEN) {
184 p = xdr_encode_hyper(p, (u64) NFS3_MAXPATHLEN);
185 } else {
186 p = xdr_encode_hyper(p, (u64) stat->size);
188 p = xdr_encode_hyper(p, ((u64)stat->blocks) << 9);
189 *p++ = htonl((u32) MAJOR(stat->rdev));
190 *p++ = htonl((u32) MINOR(stat->rdev));
191 p = encode_fsid(p, fhp);
192 p = xdr_encode_hyper(p, stat->ino);
193 p = encode_time3(p, &stat->atime);
194 p = encode_time3(p, &stat->mtime);
195 p = encode_time3(p, &stat->ctime);
197 return p;
200 static __be32 *
201 encode_saved_post_attr(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp)
203 /* Attributes to follow */
204 *p++ = xdr_one;
205 return encode_fattr3(rqstp, p, fhp, &fhp->fh_post_attr);
209 * Encode post-operation attributes.
210 * The inode may be NULL if the call failed because of a stale file
211 * handle. In this case, no attributes are returned.
213 static __be32 *
214 encode_post_op_attr(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp)
216 struct dentry *dentry = fhp->fh_dentry;
217 if (dentry && dentry->d_inode) {
218 int err;
219 struct kstat stat;
221 err = vfs_getattr(fhp->fh_export->ex_path.mnt, dentry, &stat);
222 if (!err) {
223 *p++ = xdr_one; /* attributes follow */
224 lease_get_mtime(dentry->d_inode, &stat.mtime);
225 return encode_fattr3(rqstp, p, fhp, &stat);
228 *p++ = xdr_zero;
229 return p;
232 /* Helper for NFSv3 ACLs */
233 __be32 *
234 nfs3svc_encode_post_op_attr(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp)
236 return encode_post_op_attr(rqstp, p, fhp);
240 * Enocde weak cache consistency data
242 static __be32 *
243 encode_wcc_data(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp)
245 struct dentry *dentry = fhp->fh_dentry;
247 if (dentry && dentry->d_inode && fhp->fh_post_saved) {
248 if (fhp->fh_pre_saved) {
249 *p++ = xdr_one;
250 p = xdr_encode_hyper(p, (u64) fhp->fh_pre_size);
251 p = encode_time3(p, &fhp->fh_pre_mtime);
252 p = encode_time3(p, &fhp->fh_pre_ctime);
253 } else {
254 *p++ = xdr_zero;
256 return encode_saved_post_attr(rqstp, p, fhp);
258 /* no pre- or post-attrs */
259 *p++ = xdr_zero;
260 return encode_post_op_attr(rqstp, p, fhp);
264 * Fill in the post_op attr for the wcc data
266 void fill_post_wcc(struct svc_fh *fhp)
268 int err;
270 if (fhp->fh_post_saved)
271 printk("nfsd: inode locked twice during operation.\n");
273 err = vfs_getattr(fhp->fh_export->ex_path.mnt, fhp->fh_dentry,
274 &fhp->fh_post_attr);
275 fhp->fh_post_change = fhp->fh_dentry->d_inode->i_version;
276 if (err) {
277 fhp->fh_post_saved = 0;
278 /* Grab the ctime anyway - set_change_info might use it */
279 fhp->fh_post_attr.ctime = fhp->fh_dentry->d_inode->i_ctime;
280 } else
281 fhp->fh_post_saved = 1;
285 * XDR decode functions
288 nfs3svc_decode_fhandle(struct svc_rqst *rqstp, __be32 *p, struct nfsd_fhandle *args)
290 if (!(p = decode_fh(p, &args->fh)))
291 return 0;
292 return xdr_argsize_check(rqstp, p);
296 nfs3svc_decode_sattrargs(struct svc_rqst *rqstp, __be32 *p,
297 struct nfsd3_sattrargs *args)
299 if (!(p = decode_fh(p, &args->fh)))
300 return 0;
301 p = decode_sattr3(p, &args->attrs);
303 if ((args->check_guard = ntohl(*p++)) != 0) {
304 struct timespec time;
305 p = decode_time3(p, &time);
306 args->guardtime = time.tv_sec;
309 return xdr_argsize_check(rqstp, p);
313 nfs3svc_decode_diropargs(struct svc_rqst *rqstp, __be32 *p,
314 struct nfsd3_diropargs *args)
316 if (!(p = decode_fh(p, &args->fh))
317 || !(p = decode_filename(p, &args->name, &args->len)))
318 return 0;
320 return xdr_argsize_check(rqstp, p);
324 nfs3svc_decode_accessargs(struct svc_rqst *rqstp, __be32 *p,
325 struct nfsd3_accessargs *args)
327 if (!(p = decode_fh(p, &args->fh)))
328 return 0;
329 args->access = ntohl(*p++);
331 return xdr_argsize_check(rqstp, p);
335 nfs3svc_decode_readargs(struct svc_rqst *rqstp, __be32 *p,
336 struct nfsd3_readargs *args)
338 unsigned int len;
339 int v,pn;
340 u32 max_blocksize = svc_max_payload(rqstp);
342 if (!(p = decode_fh(p, &args->fh)))
343 return 0;
344 p = xdr_decode_hyper(p, &args->offset);
346 len = args->count = ntohl(*p++);
348 if (len > max_blocksize)
349 len = max_blocksize;
351 /* set up the kvec */
352 v=0;
353 while (len > 0) {
354 pn = rqstp->rq_resused++;
355 rqstp->rq_vec[v].iov_base = page_address(rqstp->rq_respages[pn]);
356 rqstp->rq_vec[v].iov_len = len < PAGE_SIZE? len : PAGE_SIZE;
357 len -= rqstp->rq_vec[v].iov_len;
358 v++;
360 args->vlen = v;
361 return xdr_argsize_check(rqstp, p);
365 nfs3svc_decode_writeargs(struct svc_rqst *rqstp, __be32 *p,
366 struct nfsd3_writeargs *args)
368 unsigned int len, v, hdr, dlen;
369 u32 max_blocksize = svc_max_payload(rqstp);
371 if (!(p = decode_fh(p, &args->fh)))
372 return 0;
373 p = xdr_decode_hyper(p, &args->offset);
375 args->count = ntohl(*p++);
376 args->stable = ntohl(*p++);
377 len = args->len = ntohl(*p++);
379 * The count must equal the amount of data passed.
381 if (args->count != args->len)
382 return 0;
385 * Check to make sure that we got the right number of
386 * bytes.
388 hdr = (void*)p - rqstp->rq_arg.head[0].iov_base;
389 dlen = rqstp->rq_arg.head[0].iov_len + rqstp->rq_arg.page_len
390 - hdr;
392 * Round the length of the data which was specified up to
393 * the next multiple of XDR units and then compare that
394 * against the length which was actually received.
395 * Note that when RPCSEC/GSS (for example) is used, the
396 * data buffer can be padded so dlen might be larger
397 * than required. It must never be smaller.
399 if (dlen < XDR_QUADLEN(len)*4)
400 return 0;
402 if (args->count > max_blocksize) {
403 args->count = max_blocksize;
404 len = args->len = max_blocksize;
406 rqstp->rq_vec[0].iov_base = (void*)p;
407 rqstp->rq_vec[0].iov_len = rqstp->rq_arg.head[0].iov_len - hdr;
408 v = 0;
409 while (len > rqstp->rq_vec[v].iov_len) {
410 len -= rqstp->rq_vec[v].iov_len;
411 v++;
412 rqstp->rq_vec[v].iov_base = page_address(rqstp->rq_pages[v]);
413 rqstp->rq_vec[v].iov_len = PAGE_SIZE;
415 rqstp->rq_vec[v].iov_len = len;
416 args->vlen = v + 1;
417 return 1;
421 nfs3svc_decode_createargs(struct svc_rqst *rqstp, __be32 *p,
422 struct nfsd3_createargs *args)
424 if (!(p = decode_fh(p, &args->fh))
425 || !(p = decode_filename(p, &args->name, &args->len)))
426 return 0;
428 switch (args->createmode = ntohl(*p++)) {
429 case NFS3_CREATE_UNCHECKED:
430 case NFS3_CREATE_GUARDED:
431 p = decode_sattr3(p, &args->attrs);
432 break;
433 case NFS3_CREATE_EXCLUSIVE:
434 args->verf = p;
435 p += 2;
436 break;
437 default:
438 return 0;
441 return xdr_argsize_check(rqstp, p);
444 nfs3svc_decode_mkdirargs(struct svc_rqst *rqstp, __be32 *p,
445 struct nfsd3_createargs *args)
447 if (!(p = decode_fh(p, &args->fh)) ||
448 !(p = decode_filename(p, &args->name, &args->len)))
449 return 0;
450 p = decode_sattr3(p, &args->attrs);
452 return xdr_argsize_check(rqstp, p);
456 nfs3svc_decode_symlinkargs(struct svc_rqst *rqstp, __be32 *p,
457 struct nfsd3_symlinkargs *args)
459 unsigned int len, avail;
460 char *old, *new;
461 struct kvec *vec;
463 if (!(p = decode_fh(p, &args->ffh)) ||
464 !(p = decode_filename(p, &args->fname, &args->flen))
466 return 0;
467 p = decode_sattr3(p, &args->attrs);
469 /* now decode the pathname, which might be larger than the first page.
470 * As we have to check for nul's anyway, we copy it into a new page
471 * This page appears in the rq_res.pages list, but as pages_len is always
472 * 0, it won't get in the way
474 len = ntohl(*p++);
475 if (len == 0 || len > NFS3_MAXPATHLEN || len >= PAGE_SIZE)
476 return 0;
477 args->tname = new =
478 page_address(rqstp->rq_respages[rqstp->rq_resused++]);
479 args->tlen = len;
480 /* first copy and check from the first page */
481 old = (char*)p;
482 vec = &rqstp->rq_arg.head[0];
483 avail = vec->iov_len - (old - (char*)vec->iov_base);
484 while (len && avail && *old) {
485 *new++ = *old++;
486 len--;
487 avail--;
489 /* now copy next page if there is one */
490 if (len && !avail && rqstp->rq_arg.page_len) {
491 avail = rqstp->rq_arg.page_len;
492 if (avail > PAGE_SIZE)
493 avail = PAGE_SIZE;
494 old = page_address(rqstp->rq_arg.pages[0]);
496 while (len && avail && *old) {
497 *new++ = *old++;
498 len--;
499 avail--;
501 *new = '\0';
502 if (len)
503 return 0;
505 return 1;
509 nfs3svc_decode_mknodargs(struct svc_rqst *rqstp, __be32 *p,
510 struct nfsd3_mknodargs *args)
512 if (!(p = decode_fh(p, &args->fh))
513 || !(p = decode_filename(p, &args->name, &args->len)))
514 return 0;
516 args->ftype = ntohl(*p++);
518 if (args->ftype == NF3BLK || args->ftype == NF3CHR
519 || args->ftype == NF3SOCK || args->ftype == NF3FIFO)
520 p = decode_sattr3(p, &args->attrs);
522 if (args->ftype == NF3BLK || args->ftype == NF3CHR) {
523 args->major = ntohl(*p++);
524 args->minor = ntohl(*p++);
527 return xdr_argsize_check(rqstp, p);
531 nfs3svc_decode_renameargs(struct svc_rqst *rqstp, __be32 *p,
532 struct nfsd3_renameargs *args)
534 if (!(p = decode_fh(p, &args->ffh))
535 || !(p = decode_filename(p, &args->fname, &args->flen))
536 || !(p = decode_fh(p, &args->tfh))
537 || !(p = decode_filename(p, &args->tname, &args->tlen)))
538 return 0;
540 return xdr_argsize_check(rqstp, p);
544 nfs3svc_decode_readlinkargs(struct svc_rqst *rqstp, __be32 *p,
545 struct nfsd3_readlinkargs *args)
547 if (!(p = decode_fh(p, &args->fh)))
548 return 0;
549 args->buffer =
550 page_address(rqstp->rq_respages[rqstp->rq_resused++]);
552 return xdr_argsize_check(rqstp, p);
556 nfs3svc_decode_linkargs(struct svc_rqst *rqstp, __be32 *p,
557 struct nfsd3_linkargs *args)
559 if (!(p = decode_fh(p, &args->ffh))
560 || !(p = decode_fh(p, &args->tfh))
561 || !(p = decode_filename(p, &args->tname, &args->tlen)))
562 return 0;
564 return xdr_argsize_check(rqstp, p);
568 nfs3svc_decode_readdirargs(struct svc_rqst *rqstp, __be32 *p,
569 struct nfsd3_readdirargs *args)
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 = ~0;
576 args->count = ntohl(*p++);
578 if (args->count > PAGE_SIZE)
579 args->count = PAGE_SIZE;
581 args->buffer =
582 page_address(rqstp->rq_respages[rqstp->rq_resused++]);
584 return xdr_argsize_check(rqstp, p);
588 nfs3svc_decode_readdirplusargs(struct svc_rqst *rqstp, __be32 *p,
589 struct nfsd3_readdirargs *args)
591 int len, pn;
592 u32 max_blocksize = svc_max_payload(rqstp);
594 if (!(p = decode_fh(p, &args->fh)))
595 return 0;
596 p = xdr_decode_hyper(p, &args->cookie);
597 args->verf = p; p += 2;
598 args->dircount = ntohl(*p++);
599 args->count = ntohl(*p++);
601 len = (args->count > max_blocksize) ? max_blocksize :
602 args->count;
603 args->count = len;
605 while (len > 0) {
606 pn = rqstp->rq_resused++;
607 if (!args->buffer)
608 args->buffer = page_address(rqstp->rq_respages[pn]);
609 len -= PAGE_SIZE;
612 return xdr_argsize_check(rqstp, p);
616 nfs3svc_decode_commitargs(struct svc_rqst *rqstp, __be32 *p,
617 struct nfsd3_commitargs *args)
619 if (!(p = decode_fh(p, &args->fh)))
620 return 0;
621 p = xdr_decode_hyper(p, &args->offset);
622 args->count = ntohl(*p++);
624 return xdr_argsize_check(rqstp, p);
628 * XDR encode functions
631 * There must be an encoding function for void results so svc_process
632 * will work properly.
635 nfs3svc_encode_voidres(struct svc_rqst *rqstp, __be32 *p, void *dummy)
637 return xdr_ressize_check(rqstp, p);
640 /* GETATTR */
642 nfs3svc_encode_attrstat(struct svc_rqst *rqstp, __be32 *p,
643 struct nfsd3_attrstat *resp)
645 if (resp->status == 0) {
646 lease_get_mtime(resp->fh.fh_dentry->d_inode,
647 &resp->stat.mtime);
648 p = encode_fattr3(rqstp, p, &resp->fh, &resp->stat);
650 return xdr_ressize_check(rqstp, p);
653 /* SETATTR, REMOVE, RMDIR */
655 nfs3svc_encode_wccstat(struct svc_rqst *rqstp, __be32 *p,
656 struct nfsd3_attrstat *resp)
658 p = encode_wcc_data(rqstp, p, &resp->fh);
659 return xdr_ressize_check(rqstp, p);
662 /* LOOKUP */
664 nfs3svc_encode_diropres(struct svc_rqst *rqstp, __be32 *p,
665 struct nfsd3_diropres *resp)
667 if (resp->status == 0) {
668 p = encode_fh(p, &resp->fh);
669 p = encode_post_op_attr(rqstp, p, &resp->fh);
671 p = encode_post_op_attr(rqstp, p, &resp->dirfh);
672 return xdr_ressize_check(rqstp, p);
675 /* ACCESS */
677 nfs3svc_encode_accessres(struct svc_rqst *rqstp, __be32 *p,
678 struct nfsd3_accessres *resp)
680 p = encode_post_op_attr(rqstp, p, &resp->fh);
681 if (resp->status == 0)
682 *p++ = htonl(resp->access);
683 return xdr_ressize_check(rqstp, p);
686 /* READLINK */
688 nfs3svc_encode_readlinkres(struct svc_rqst *rqstp, __be32 *p,
689 struct nfsd3_readlinkres *resp)
691 p = encode_post_op_attr(rqstp, p, &resp->fh);
692 if (resp->status == 0) {
693 *p++ = htonl(resp->len);
694 xdr_ressize_check(rqstp, p);
695 rqstp->rq_res.page_len = resp->len;
696 if (resp->len & 3) {
697 /* need to pad the tail */
698 rqstp->rq_res.tail[0].iov_base = p;
699 *p = 0;
700 rqstp->rq_res.tail[0].iov_len = 4 - (resp->len&3);
702 return 1;
703 } else
704 return xdr_ressize_check(rqstp, p);
707 /* READ */
709 nfs3svc_encode_readres(struct svc_rqst *rqstp, __be32 *p,
710 struct nfsd3_readres *resp)
712 p = encode_post_op_attr(rqstp, p, &resp->fh);
713 if (resp->status == 0) {
714 *p++ = htonl(resp->count);
715 *p++ = htonl(resp->eof);
716 *p++ = htonl(resp->count); /* xdr opaque count */
717 xdr_ressize_check(rqstp, p);
718 /* now update rqstp->rq_res to reflect data aswell */
719 rqstp->rq_res.page_len = resp->count;
720 if (resp->count & 3) {
721 /* need to pad the tail */
722 rqstp->rq_res.tail[0].iov_base = p;
723 *p = 0;
724 rqstp->rq_res.tail[0].iov_len = 4 - (resp->count & 3);
726 return 1;
727 } else
728 return xdr_ressize_check(rqstp, p);
731 /* WRITE */
733 nfs3svc_encode_writeres(struct svc_rqst *rqstp, __be32 *p,
734 struct nfsd3_writeres *resp)
736 p = encode_wcc_data(rqstp, p, &resp->fh);
737 if (resp->status == 0) {
738 *p++ = htonl(resp->count);
739 *p++ = htonl(resp->committed);
740 *p++ = htonl(nfssvc_boot.tv_sec);
741 *p++ = htonl(nfssvc_boot.tv_usec);
743 return xdr_ressize_check(rqstp, p);
746 /* CREATE, MKDIR, SYMLINK, MKNOD */
748 nfs3svc_encode_createres(struct svc_rqst *rqstp, __be32 *p,
749 struct nfsd3_diropres *resp)
751 if (resp->status == 0) {
752 *p++ = xdr_one;
753 p = encode_fh(p, &resp->fh);
754 p = encode_post_op_attr(rqstp, p, &resp->fh);
756 p = encode_wcc_data(rqstp, p, &resp->dirfh);
757 return xdr_ressize_check(rqstp, p);
760 /* RENAME */
762 nfs3svc_encode_renameres(struct svc_rqst *rqstp, __be32 *p,
763 struct nfsd3_renameres *resp)
765 p = encode_wcc_data(rqstp, p, &resp->ffh);
766 p = encode_wcc_data(rqstp, p, &resp->tfh);
767 return xdr_ressize_check(rqstp, p);
770 /* LINK */
772 nfs3svc_encode_linkres(struct svc_rqst *rqstp, __be32 *p,
773 struct nfsd3_linkres *resp)
775 p = encode_post_op_attr(rqstp, p, &resp->fh);
776 p = encode_wcc_data(rqstp, p, &resp->tfh);
777 return xdr_ressize_check(rqstp, p);
780 /* READDIR */
782 nfs3svc_encode_readdirres(struct svc_rqst *rqstp, __be32 *p,
783 struct nfsd3_readdirres *resp)
785 p = encode_post_op_attr(rqstp, p, &resp->fh);
787 if (resp->status == 0) {
788 /* stupid readdir cookie */
789 memcpy(p, resp->verf, 8); p += 2;
790 xdr_ressize_check(rqstp, p);
791 if (rqstp->rq_res.head[0].iov_len + (2<<2) > PAGE_SIZE)
792 return 1; /*No room for trailer */
793 rqstp->rq_res.page_len = (resp->count) << 2;
795 /* add the 'tail' to the end of the 'head' page - page 0. */
796 rqstp->rq_res.tail[0].iov_base = p;
797 *p++ = 0; /* no more entries */
798 *p++ = htonl(resp->common.err == nfserr_eof);
799 rqstp->rq_res.tail[0].iov_len = 2<<2;
800 return 1;
801 } else
802 return xdr_ressize_check(rqstp, p);
805 static __be32 *
806 encode_entry_baggage(struct nfsd3_readdirres *cd, __be32 *p, const char *name,
807 int namlen, u64 ino)
809 *p++ = xdr_one; /* mark entry present */
810 p = xdr_encode_hyper(p, ino); /* file id */
811 p = xdr_encode_array(p, name, namlen);/* name length & name */
813 cd->offset = p; /* remember pointer */
814 p = xdr_encode_hyper(p, NFS_OFFSET_MAX);/* offset of next entry */
816 return p;
819 static int
820 compose_entry_fh(struct nfsd3_readdirres *cd, struct svc_fh *fhp,
821 const char *name, int namlen)
823 struct svc_export *exp;
824 struct dentry *dparent, *dchild;
825 int rv = 0;
827 dparent = cd->fh.fh_dentry;
828 exp = cd->fh.fh_export;
830 if (isdotent(name, namlen)) {
831 if (namlen == 2) {
832 dchild = dget_parent(dparent);
833 if (dchild == dparent) {
834 /* filesystem root - cannot return filehandle for ".." */
835 dput(dchild);
836 return -ENOENT;
838 } else
839 dchild = dget(dparent);
840 } else
841 dchild = lookup_one_len(name, dparent, namlen);
842 if (IS_ERR(dchild))
843 return -ENOENT;
844 rv = -ENOENT;
845 if (d_mountpoint(dchild))
846 goto out;
847 rv = fh_compose(fhp, exp, dchild, &cd->fh);
848 if (rv)
849 goto out;
850 if (!dchild->d_inode)
851 goto out;
852 rv = 0;
853 out:
854 dput(dchild);
855 return rv;
858 __be32 *encode_entryplus_baggage(struct nfsd3_readdirres *cd, __be32 *p, const char *name, int namlen)
860 struct svc_fh fh;
861 int err;
863 fh_init(&fh, NFS3_FHSIZE);
864 err = compose_entry_fh(cd, &fh, name, namlen);
865 if (err) {
866 *p++ = 0;
867 *p++ = 0;
868 goto out;
870 p = encode_post_op_attr(cd->rqstp, p, &fh);
871 *p++ = xdr_one; /* yes, a file handle follows */
872 p = encode_fh(p, &fh);
873 out:
874 fh_put(&fh);
875 return p;
879 * Encode a directory entry. This one works for both normal readdir
880 * and readdirplus.
881 * The normal readdir reply requires 2 (fileid) + 1 (stringlen)
882 * + string + 2 (cookie) + 1 (next) words, i.e. 6 + strlen.
884 * The readdirplus baggage is 1+21 words for post_op_attr, plus the
885 * file handle.
888 #define NFS3_ENTRY_BAGGAGE (2 + 1 + 2 + 1)
889 #define NFS3_ENTRYPLUS_BAGGAGE (1 + 21 + 1 + (NFS3_FHSIZE >> 2))
890 static int
891 encode_entry(struct readdir_cd *ccd, const char *name, int namlen,
892 loff_t offset, u64 ino, unsigned int d_type, int plus)
894 struct nfsd3_readdirres *cd = container_of(ccd, struct nfsd3_readdirres,
895 common);
896 __be32 *p = cd->buffer;
897 caddr_t curr_page_addr = NULL;
898 int pn; /* current page number */
899 int slen; /* string (name) length */
900 int elen; /* estimated entry length in words */
901 int num_entry_words = 0; /* actual number of words */
903 if (cd->offset) {
904 u64 offset64 = offset;
906 if (unlikely(cd->offset1)) {
907 /* we ended up with offset on a page boundary */
908 *cd->offset = htonl(offset64 >> 32);
909 *cd->offset1 = htonl(offset64 & 0xffffffff);
910 cd->offset1 = NULL;
911 } else {
912 xdr_encode_hyper(cd->offset, offset64);
917 dprintk("encode_entry(%.*s @%ld%s)\n",
918 namlen, name, (long) offset, plus? " plus" : "");
921 /* truncate filename if too long */
922 if (namlen > NFS3_MAXNAMLEN)
923 namlen = NFS3_MAXNAMLEN;
925 slen = XDR_QUADLEN(namlen);
926 elen = slen + NFS3_ENTRY_BAGGAGE
927 + (plus? NFS3_ENTRYPLUS_BAGGAGE : 0);
929 if (cd->buflen < elen) {
930 cd->common.err = nfserr_toosmall;
931 return -EINVAL;
934 /* determine which page in rq_respages[] we are currently filling */
935 for (pn=1; pn < cd->rqstp->rq_resused; pn++) {
936 curr_page_addr = page_address(cd->rqstp->rq_respages[pn]);
938 if (((caddr_t)cd->buffer >= curr_page_addr) &&
939 ((caddr_t)cd->buffer < curr_page_addr + PAGE_SIZE))
940 break;
943 if ((caddr_t)(cd->buffer + elen) < (curr_page_addr + PAGE_SIZE)) {
944 /* encode entry in current page */
946 p = encode_entry_baggage(cd, p, name, namlen, ino);
948 if (plus)
949 p = encode_entryplus_baggage(cd, p, name, namlen);
950 num_entry_words = p - cd->buffer;
951 } else if (cd->rqstp->rq_respages[pn+1] != NULL) {
952 /* temporarily encode entry into next page, then move back to
953 * current and next page in rq_respages[] */
954 __be32 *p1, *tmp;
955 int len1, len2;
957 /* grab next page for temporary storage of entry */
958 p1 = tmp = page_address(cd->rqstp->rq_respages[pn+1]);
960 p1 = encode_entry_baggage(cd, p1, name, namlen, ino);
962 if (plus)
963 p1 = encode_entryplus_baggage(cd, p1, name, namlen);
965 /* determine entry word length and lengths to go in pages */
966 num_entry_words = p1 - tmp;
967 len1 = curr_page_addr + PAGE_SIZE - (caddr_t)cd->buffer;
968 if ((num_entry_words << 2) < len1) {
969 /* the actual number of words in the entry is less
970 * than elen and can still fit in the current page
972 memmove(p, tmp, num_entry_words << 2);
973 p += num_entry_words;
975 /* update offset */
976 cd->offset = cd->buffer + (cd->offset - tmp);
977 } else {
978 unsigned int offset_r = (cd->offset - tmp) << 2;
980 /* update pointer to offset location.
981 * This is a 64bit quantity, so we need to
982 * deal with 3 cases:
983 * - entirely in first page
984 * - entirely in second page
985 * - 4 bytes in each page
987 if (offset_r + 8 <= len1) {
988 cd->offset = p + (cd->offset - tmp);
989 } else if (offset_r >= len1) {
990 cd->offset -= len1 >> 2;
991 } else {
992 /* sitting on the fence */
993 BUG_ON(offset_r != len1 - 4);
994 cd->offset = p + (cd->offset - tmp);
995 cd->offset1 = tmp;
998 len2 = (num_entry_words << 2) - len1;
1000 /* move from temp page to current and next pages */
1001 memmove(p, tmp, len1);
1002 memmove(tmp, (caddr_t)tmp+len1, len2);
1004 p = tmp + (len2 >> 2);
1007 else {
1008 cd->common.err = nfserr_toosmall;
1009 return -EINVAL;
1012 cd->buflen -= num_entry_words;
1013 cd->buffer = p;
1014 cd->common.err = nfs_ok;
1015 return 0;
1020 nfs3svc_encode_entry(void *cd, const char *name,
1021 int namlen, loff_t offset, u64 ino, unsigned int d_type)
1023 return encode_entry(cd, name, namlen, offset, ino, d_type, 0);
1027 nfs3svc_encode_entry_plus(void *cd, const char *name,
1028 int namlen, loff_t offset, u64 ino,
1029 unsigned int d_type)
1031 return encode_entry(cd, name, namlen, offset, ino, d_type, 1);
1034 /* FSSTAT */
1036 nfs3svc_encode_fsstatres(struct svc_rqst *rqstp, __be32 *p,
1037 struct nfsd3_fsstatres *resp)
1039 struct kstatfs *s = &resp->stats;
1040 u64 bs = s->f_bsize;
1042 *p++ = xdr_zero; /* no post_op_attr */
1044 if (resp->status == 0) {
1045 p = xdr_encode_hyper(p, bs * s->f_blocks); /* total bytes */
1046 p = xdr_encode_hyper(p, bs * s->f_bfree); /* free bytes */
1047 p = xdr_encode_hyper(p, bs * s->f_bavail); /* user available bytes */
1048 p = xdr_encode_hyper(p, s->f_files); /* total inodes */
1049 p = xdr_encode_hyper(p, s->f_ffree); /* free inodes */
1050 p = xdr_encode_hyper(p, s->f_ffree); /* user available inodes */
1051 *p++ = htonl(resp->invarsec); /* mean unchanged time */
1053 return xdr_ressize_check(rqstp, p);
1056 /* FSINFO */
1058 nfs3svc_encode_fsinfores(struct svc_rqst *rqstp, __be32 *p,
1059 struct nfsd3_fsinfores *resp)
1061 *p++ = xdr_zero; /* no post_op_attr */
1063 if (resp->status == 0) {
1064 *p++ = htonl(resp->f_rtmax);
1065 *p++ = htonl(resp->f_rtpref);
1066 *p++ = htonl(resp->f_rtmult);
1067 *p++ = htonl(resp->f_wtmax);
1068 *p++ = htonl(resp->f_wtpref);
1069 *p++ = htonl(resp->f_wtmult);
1070 *p++ = htonl(resp->f_dtpref);
1071 p = xdr_encode_hyper(p, resp->f_maxfilesize);
1072 *p++ = xdr_one;
1073 *p++ = xdr_zero;
1074 *p++ = htonl(resp->f_properties);
1077 return xdr_ressize_check(rqstp, p);
1080 /* PATHCONF */
1082 nfs3svc_encode_pathconfres(struct svc_rqst *rqstp, __be32 *p,
1083 struct nfsd3_pathconfres *resp)
1085 *p++ = xdr_zero; /* no post_op_attr */
1087 if (resp->status == 0) {
1088 *p++ = htonl(resp->p_link_max);
1089 *p++ = htonl(resp->p_name_max);
1090 *p++ = htonl(resp->p_no_trunc);
1091 *p++ = htonl(resp->p_chown_restricted);
1092 *p++ = htonl(resp->p_case_insensitive);
1093 *p++ = htonl(resp->p_case_preserving);
1096 return xdr_ressize_check(rqstp, p);
1099 /* COMMIT */
1101 nfs3svc_encode_commitres(struct svc_rqst *rqstp, __be32 *p,
1102 struct nfsd3_commitres *resp)
1104 p = encode_wcc_data(rqstp, p, &resp->fh);
1105 /* Write verifier */
1106 if (resp->status == 0) {
1107 *p++ = htonl(nfssvc_boot.tv_sec);
1108 *p++ = htonl(nfssvc_boot.tv_usec);
1110 return xdr_ressize_check(rqstp, p);
1114 * XDR release functions
1117 nfs3svc_release_fhandle(struct svc_rqst *rqstp, __be32 *p,
1118 struct nfsd3_attrstat *resp)
1120 fh_put(&resp->fh);
1121 return 1;
1125 nfs3svc_release_fhandle2(struct svc_rqst *rqstp, __be32 *p,
1126 struct nfsd3_fhandle_pair *resp)
1128 fh_put(&resp->fh1);
1129 fh_put(&resp->fh2);
1130 return 1;