usbnet: add a mutex around phy register access
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / nfsd / nfs3xdr.c
blob247d518248bf71b2a95b5ce04b460374dd88c51f
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
27 #ifdef NFSD_OPTIMIZE_SPACE
28 # define inline
29 #endif
33 * Mapping of S_IF* types to NFS file types
35 static u32 nfs3_ftypes[] = {
36 NF3NON, NF3FIFO, NF3CHR, NF3BAD,
37 NF3DIR, NF3BAD, NF3BLK, NF3BAD,
38 NF3REG, NF3BAD, NF3LNK, NF3BAD,
39 NF3SOCK, NF3BAD, NF3LNK, NF3BAD,
43 * XDR functions for basic NFS types
45 static inline u32 *
46 encode_time3(u32 *p, struct timespec *time)
48 *p++ = htonl((u32) time->tv_sec); *p++ = htonl(time->tv_nsec);
49 return p;
52 static inline u32 *
53 decode_time3(u32 *p, struct timespec *time)
55 time->tv_sec = ntohl(*p++);
56 time->tv_nsec = ntohl(*p++);
57 return p;
60 static inline u32 *
61 decode_fh(u32 *p, struct svc_fh *fhp)
63 unsigned int size;
64 fh_init(fhp, NFS3_FHSIZE);
65 size = ntohl(*p++);
66 if (size > NFS3_FHSIZE)
67 return NULL;
69 memcpy(&fhp->fh_handle.fh_base, p, size);
70 fhp->fh_handle.fh_size = size;
71 return p + XDR_QUADLEN(size);
74 /* Helper function for NFSv3 ACL code */
75 u32 *nfs3svc_decode_fh(u32 *p, struct svc_fh *fhp)
77 return decode_fh(p, fhp);
80 static inline u32 *
81 encode_fh(u32 *p, struct svc_fh *fhp)
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);
91 * Decode a file name and make sure that the path contains
92 * no slashes or null bytes.
94 static inline u32 *
95 decode_filename(u32 *p, char **namp, int *lenp)
97 char *name;
98 int i;
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;
107 return p;
110 static inline u32 *
111 decode_sattr3(u32 *p, struct iattr *iap)
113 u32 tmp;
115 iap->ia_valid = 0;
117 if (*p++) {
118 iap->ia_valid |= ATTR_MODE;
119 iap->ia_mode = ntohl(*p++);
121 if (*p++) {
122 iap->ia_valid |= ATTR_UID;
123 iap->ia_uid = ntohl(*p++);
125 if (*p++) {
126 iap->ia_valid |= ATTR_GID;
127 iap->ia_gid = ntohl(*p++);
129 if (*p++) {
130 u64 newsize;
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;
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++);
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++);
153 return p;
156 static inline u32 *
157 encode_fattr3(struct svc_rqst *rqstp, u32 *p, struct svc_fh *fhp,
158 struct kstat *stat)
160 struct dentry *dentry = fhp->fh_dentry;
161 struct timespec time;
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) {
169 p = xdr_encode_hyper(p, (u64) NFS3_MAXPATHLEN);
170 } else {
171 p = xdr_encode_hyper(p, (u64) stat->size);
173 p = xdr_encode_hyper(p, ((u64)stat->blocks) << 9);
174 *p++ = htonl((u32) MAJOR(stat->rdev));
175 *p++ = htonl((u32) MINOR(stat->rdev));
176 if (is_fsid(fhp, rqstp->rq_reffh))
177 p = xdr_encode_hyper(p, (u64) fhp->fh_export->ex_fsid);
178 else
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);
182 lease_get_mtime(dentry->d_inode, &time);
183 p = encode_time3(p, &time);
184 p = encode_time3(p, &stat->ctime);
186 return p;
189 static inline u32 *
190 encode_saved_post_attr(struct svc_rqst *rqstp, u32 *p, struct svc_fh *fhp)
192 struct inode *inode = fhp->fh_dentry->d_inode;
194 /* Attributes to follow */
195 *p++ = xdr_one;
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);
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);
219 return p;
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.
227 static u32 *
228 encode_post_op_attr(struct svc_rqst *rqstp, u32 *p, struct svc_fh *fhp)
230 struct dentry *dentry = fhp->fh_dentry;
231 if (dentry && dentry->d_inode != NULL) {
232 int err;
233 struct kstat stat;
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);
241 *p++ = xdr_zero;
242 return p;
245 /* Helper for NFSv3 ACLs */
246 u32 *
247 nfs3svc_encode_post_op_attr(struct svc_rqst *rqstp, u32 *p, struct svc_fh *fhp)
249 return encode_post_op_attr(rqstp, p, fhp);
253 * Enocde weak cache consistency data
255 static u32 *
256 encode_wcc_data(struct svc_rqst *rqstp, u32 *p, struct svc_fh *fhp)
258 struct dentry *dentry = fhp->fh_dentry;
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;
269 return encode_saved_post_attr(rqstp, p, fhp);
271 /* no pre- or post-attrs */
272 *p++ = xdr_zero;
273 return encode_post_op_attr(rqstp, p, fhp);
278 * XDR decode functions
281 nfs3svc_decode_fhandle(struct svc_rqst *rqstp, u32 *p, struct nfsd_fhandle *args)
283 if (!(p = decode_fh(p, &args->fh)))
284 return 0;
285 return xdr_argsize_check(rqstp, p);
289 nfs3svc_decode_sattrargs(struct svc_rqst *rqstp, u32 *p,
290 struct nfsd3_sattrargs *args)
292 if (!(p = decode_fh(p, &args->fh))
293 || !(p = decode_sattr3(p, &args->attrs)))
294 return 0;
296 if ((args->check_guard = ntohl(*p++)) != 0) {
297 struct timespec time;
298 p = decode_time3(p, &time);
299 args->guardtime = time.tv_sec;
302 return xdr_argsize_check(rqstp, p);
306 nfs3svc_decode_diropargs(struct svc_rqst *rqstp, u32 *p,
307 struct nfsd3_diropargs *args)
309 if (!(p = decode_fh(p, &args->fh))
310 || !(p = decode_filename(p, &args->name, &args->len)))
311 return 0;
313 return xdr_argsize_check(rqstp, p);
317 nfs3svc_decode_accessargs(struct svc_rqst *rqstp, u32 *p,
318 struct nfsd3_accessargs *args)
320 if (!(p = decode_fh(p, &args->fh)))
321 return 0;
322 args->access = ntohl(*p++);
324 return xdr_argsize_check(rqstp, p);
328 nfs3svc_decode_readargs(struct svc_rqst *rqstp, u32 *p,
329 struct nfsd3_readargs *args)
331 unsigned int len;
332 int v,pn;
333 u32 max_blocksize = svc_max_payload(rqstp);
335 if (!(p = decode_fh(p, &args->fh))
336 || !(p = xdr_decode_hyper(p, &args->offset)))
337 return 0;
339 len = args->count = ntohl(*p++);
341 if (len > max_blocksize)
342 len = max_blocksize;
344 /* set up the kvec */
345 v=0;
346 while (len > 0) {
347 pn = rqstp->rq_resused++;
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;
351 v++;
353 args->vlen = v;
354 return xdr_argsize_check(rqstp, p);
358 nfs3svc_decode_writeargs(struct svc_rqst *rqstp, u32 *p,
359 struct nfsd3_writeargs *args)
361 unsigned int len, v, hdr;
362 u32 max_blocksize = svc_max_payload(rqstp);
364 if (!(p = decode_fh(p, &args->fh))
365 || !(p = xdr_decode_hyper(p, &args->offset)))
366 return 0;
368 args->count = ntohl(*p++);
369 args->stable = ntohl(*p++);
370 len = args->len = ntohl(*p++);
372 hdr = (void*)p - rqstp->rq_arg.head[0].iov_base;
373 if (rqstp->rq_arg.len < hdr ||
374 rqstp->rq_arg.len - hdr < len)
375 return 0;
377 rqstp->rq_vec[0].iov_base = (void*)p;
378 rqstp->rq_vec[0].iov_len = rqstp->rq_arg.head[0].iov_len - hdr;
380 if (len > max_blocksize)
381 len = max_blocksize;
382 v= 0;
383 while (len > rqstp->rq_vec[v].iov_len) {
384 len -= rqstp->rq_vec[v].iov_len;
385 v++;
386 rqstp->rq_vec[v].iov_base = page_address(rqstp->rq_pages[v]);
387 rqstp->rq_vec[v].iov_len = PAGE_SIZE;
389 rqstp->rq_vec[v].iov_len = len;
390 args->vlen = v+1;
392 return args->count == args->len && rqstp->rq_vec[0].iov_len > 0;
396 nfs3svc_decode_createargs(struct svc_rqst *rqstp, u32 *p,
397 struct nfsd3_createargs *args)
399 if (!(p = decode_fh(p, &args->fh))
400 || !(p = decode_filename(p, &args->name, &args->len)))
401 return 0;
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;
417 return xdr_argsize_check(rqstp, p);
420 nfs3svc_decode_mkdirargs(struct svc_rqst *rqstp, u32 *p,
421 struct nfsd3_createargs *args)
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;
428 return xdr_argsize_check(rqstp, p);
432 nfs3svc_decode_symlinkargs(struct svc_rqst *rqstp, u32 *p,
433 struct nfsd3_symlinkargs *args)
435 unsigned int len;
436 int avail;
437 char *old, *new;
438 struct kvec *vec;
440 if (!(p = decode_fh(p, &args->ffh))
441 || !(p = decode_filename(p, &args->fname, &args->flen))
442 || !(p = decode_sattr3(p, &args->attrs))
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
450 len = ntohl(*p++);
451 if (len == 0 || len > NFS3_MAXPATHLEN || len >= PAGE_SIZE)
452 return 0;
453 args->tname = new =
454 page_address(rqstp->rq_respages[rqstp->rq_resused++]);
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--;
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]);
471 while (len && avail && *old) {
472 *new++ = *old++;
473 len--;
474 avail--;
476 *new = '\0';
477 if (len)
478 return 0;
480 return 1;
484 nfs3svc_decode_mknodargs(struct svc_rqst *rqstp, u32 *p,
485 struct nfsd3_mknodargs *args)
487 if (!(p = decode_fh(p, &args->fh))
488 || !(p = decode_filename(p, &args->name, &args->len)))
489 return 0;
491 args->ftype = ntohl(*p++);
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;
499 if (args->ftype == NF3BLK || args->ftype == NF3CHR) {
500 args->major = ntohl(*p++);
501 args->minor = ntohl(*p++);
504 return xdr_argsize_check(rqstp, p);
508 nfs3svc_decode_renameargs(struct svc_rqst *rqstp, u32 *p,
509 struct nfsd3_renameargs *args)
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;
517 return xdr_argsize_check(rqstp, p);
521 nfs3svc_decode_readlinkargs(struct svc_rqst *rqstp, u32 *p,
522 struct nfsd3_readlinkargs *args)
524 if (!(p = decode_fh(p, &args->fh)))
525 return 0;
526 args->buffer =
527 page_address(rqstp->rq_respages[rqstp->rq_resused++]);
529 return xdr_argsize_check(rqstp, p);
533 nfs3svc_decode_linkargs(struct svc_rqst *rqstp, u32 *p,
534 struct nfsd3_linkargs *args)
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;
541 return xdr_argsize_check(rqstp, p);
545 nfs3svc_decode_readdirargs(struct svc_rqst *rqstp, u32 *p,
546 struct nfsd3_readdirargs *args)
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++);
555 if (args->count > PAGE_SIZE)
556 args->count = PAGE_SIZE;
558 args->buffer =
559 page_address(rqstp->rq_respages[rqstp->rq_resused++]);
561 return xdr_argsize_check(rqstp, p);
565 nfs3svc_decode_readdirplusargs(struct svc_rqst *rqstp, u32 *p,
566 struct nfsd3_readdirargs *args)
568 int len, pn;
569 u32 max_blocksize = svc_max_payload(rqstp);
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++);
578 len = (args->count > max_blocksize) ? max_blocksize :
579 args->count;
580 args->count = len;
582 while (len > 0) {
583 pn = rqstp->rq_resused++;
584 if (!args->buffer)
585 args->buffer = page_address(rqstp->rq_respages[pn]);
586 len -= PAGE_SIZE;
589 return xdr_argsize_check(rqstp, p);
593 nfs3svc_decode_commitargs(struct svc_rqst *rqstp, u32 *p,
594 struct nfsd3_commitargs *args)
596 if (!(p = decode_fh(p, &args->fh)))
597 return 0;
598 p = xdr_decode_hyper(p, &args->offset);
599 args->count = ntohl(*p++);
601 return xdr_argsize_check(rqstp, p);
605 * XDR encode functions
608 * There must be an encoding function for void results so svc_process
609 * will work properly.
612 nfs3svc_encode_voidres(struct svc_rqst *rqstp, u32 *p, void *dummy)
614 return xdr_ressize_check(rqstp, p);
617 /* GETATTR */
619 nfs3svc_encode_attrstat(struct svc_rqst *rqstp, u32 *p,
620 struct nfsd3_attrstat *resp)
622 if (resp->status == 0)
623 p = encode_fattr3(rqstp, p, &resp->fh, &resp->stat);
624 return xdr_ressize_check(rqstp, p);
627 /* SETATTR, REMOVE, RMDIR */
629 nfs3svc_encode_wccstat(struct svc_rqst *rqstp, u32 *p,
630 struct nfsd3_attrstat *resp)
632 p = encode_wcc_data(rqstp, p, &resp->fh);
633 return xdr_ressize_check(rqstp, p);
636 /* LOOKUP */
638 nfs3svc_encode_diropres(struct svc_rqst *rqstp, u32 *p,
639 struct nfsd3_diropres *resp)
641 if (resp->status == 0) {
642 p = encode_fh(p, &resp->fh);
643 p = encode_post_op_attr(rqstp, p, &resp->fh);
645 p = encode_post_op_attr(rqstp, p, &resp->dirfh);
646 return xdr_ressize_check(rqstp, p);
649 /* ACCESS */
651 nfs3svc_encode_accessres(struct svc_rqst *rqstp, u32 *p,
652 struct nfsd3_accessres *resp)
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);
660 /* READLINK */
662 nfs3svc_encode_readlinkres(struct svc_rqst *rqstp, u32 *p,
663 struct nfsd3_readlinkres *resp)
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 */
672 rqstp->rq_res.tail[0].iov_base = p;
673 *p = 0;
674 rqstp->rq_res.tail[0].iov_len = 4 - (resp->len&3);
676 return 1;
677 } else
678 return xdr_ressize_check(rqstp, p);
681 /* READ */
683 nfs3svc_encode_readres(struct svc_rqst *rqstp, u32 *p,
684 struct nfsd3_readres *resp)
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 */
696 rqstp->rq_res.tail[0].iov_base = p;
697 *p = 0;
698 rqstp->rq_res.tail[0].iov_len = 4 - (resp->count & 3);
700 return 1;
701 } else
702 return xdr_ressize_check(rqstp, p);
705 /* WRITE */
707 nfs3svc_encode_writeres(struct svc_rqst *rqstp, u32 *p,
708 struct nfsd3_writeres *resp)
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);
717 return xdr_ressize_check(rqstp, p);
720 /* CREATE, MKDIR, SYMLINK, MKNOD */
722 nfs3svc_encode_createres(struct svc_rqst *rqstp, u32 *p,
723 struct nfsd3_diropres *resp)
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);
730 p = encode_wcc_data(rqstp, p, &resp->dirfh);
731 return xdr_ressize_check(rqstp, p);
734 /* RENAME */
736 nfs3svc_encode_renameres(struct svc_rqst *rqstp, u32 *p,
737 struct nfsd3_renameres *resp)
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);
744 /* LINK */
746 nfs3svc_encode_linkres(struct svc_rqst *rqstp, u32 *p,
747 struct nfsd3_linkres *resp)
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);
754 /* READDIR */
756 nfs3svc_encode_readdirres(struct svc_rqst *rqstp, u32 *p,
757 struct nfsd3_readdirres *resp)
759 p = encode_post_op_attr(rqstp, p, &resp->fh);
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;
769 /* add the 'tail' to the end of the 'head' page - page 0. */
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);
779 static inline u32 *
780 encode_entry_baggage(struct nfsd3_readdirres *cd, u32 *p, const char *name,
781 int namlen, ino_t ino)
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 */
787 cd->offset = p; /* remember pointer */
788 p = xdr_encode_hyper(p, NFS_OFFSET_MAX);/* offset of next entry */
790 return p;
793 static inline u32 *
794 encode_entryplus_baggage(struct nfsd3_readdirres *cd, u32 *p,
795 struct svc_fh *fhp)
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;
804 static int
805 compose_entry_fh(struct nfsd3_readdirres *cd, struct svc_fh *fhp,
806 const char *name, int namlen)
808 struct svc_export *exp;
809 struct dentry *dparent, *dchild;
810 int rv = 0;
812 dparent = cd->fh.fh_dentry;
813 exp = cd->fh.fh_export;
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;
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;
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.
844 * The readdirplus baggage is 1+21 words for post_op_attr, plus the
845 * file handle.
848 #define NFS3_ENTRY_BAGGAGE (2 + 1 + 2 + 1)
849 #define NFS3_ENTRYPLUS_BAGGAGE (1 + 21 + 1 + (NFS3_FHSIZE >> 2))
850 static int
851 encode_entry(struct readdir_cd *ccd, const char *name,
852 int namlen, off_t offset, ino_t ino, unsigned int d_type, int plus)
854 struct nfsd3_readdirres *cd = container_of(ccd, struct nfsd3_readdirres,
855 common);
856 u32 *p = cd->buffer;
857 caddr_t curr_page_addr = NULL;
858 int pn; /* current page number */
859 int slen; /* string (name) length */
860 int elen; /* estimated entry length in words */
861 int num_entry_words = 0; /* actual number of words */
863 if (cd->offset) {
864 u64 offset64 = offset;
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);
877 dprintk("encode_entry(%.*s @%ld%s)\n",
878 namlen, name, (long) offset, plus? " plus" : "");
881 /* truncate filename if too long */
882 if (namlen > NFS3_MAXNAMLEN)
883 namlen = NFS3_MAXNAMLEN;
885 slen = XDR_QUADLEN(namlen);
886 elen = slen + NFS3_ENTRY_BAGGAGE
887 + (plus? NFS3_ENTRYPLUS_BAGGAGE : 0);
889 if (cd->buflen < elen) {
890 cd->common.err = nfserr_toosmall;
891 return -EINVAL;
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]);
898 if (((caddr_t)cd->buffer >= curr_page_addr) &&
899 ((caddr_t)cd->buffer < curr_page_addr + PAGE_SIZE))
900 break;
903 if ((caddr_t)(cd->buffer + elen) < (curr_page_addr + PAGE_SIZE)) {
904 /* encode entry in current page */
906 p = encode_entry_baggage(cd, p, name, namlen, ino);
908 /* throw in readdirplus baggage */
909 if (plus) {
910 struct svc_fh fh;
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);
918 num_entry_words = p - cd->buffer;
919 } else if (cd->rqstp->rq_respages[pn+1] != NULL) {
920 /* temporarily encode entry into next page, then move back to
921 * current and next page in rq_respages[] */
922 u32 *p1, *tmp;
923 int len1, len2;
925 /* grab next page for temporary storage of entry */
926 p1 = tmp = page_address(cd->rqstp->rq_respages[pn+1]);
928 p1 = encode_entry_baggage(cd, p1, name, namlen, ino);
930 /* throw in readdirplus baggage */
931 if (plus) {
932 struct svc_fh fh;
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);
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
949 memmove(p, tmp, num_entry_words << 2);
950 p += num_entry_words;
952 /* update offset */
953 cd->offset = cd->buffer + (cd->offset - tmp);
954 } else {
955 unsigned int offset_r = (cd->offset - tmp) << 2;
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
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;
975 len2 = (num_entry_words << 2) - len1;
977 /* move from temp page to current and next pages */
978 memmove(p, tmp, len1);
979 memmove(tmp, (caddr_t)tmp+len1, len2);
981 p = tmp + (len2 >> 2);
984 else {
985 cd->common.err = nfserr_toosmall;
986 return -EINVAL;
989 cd->buflen -= num_entry_words;
990 cd->buffer = p;
991 cd->common.err = nfs_ok;
992 return 0;
997 nfs3svc_encode_entry(struct readdir_cd *cd, const char *name,
998 int namlen, loff_t offset, ino_t ino, unsigned int d_type)
1000 return encode_entry(cd, name, namlen, offset, ino, d_type, 0);
1004 nfs3svc_encode_entry_plus(struct readdir_cd *cd, const char *name,
1005 int namlen, loff_t offset, ino_t ino, unsigned int d_type)
1007 return encode_entry(cd, name, namlen, offset, ino, d_type, 1);
1010 /* FSSTAT */
1012 nfs3svc_encode_fsstatres(struct svc_rqst *rqstp, u32 *p,
1013 struct nfsd3_fsstatres *resp)
1015 struct kstatfs *s = &resp->stats;
1016 u64 bs = s->f_bsize;
1018 *p++ = xdr_zero; /* no post_op_attr */
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 */
1029 return xdr_ressize_check(rqstp, p);
1032 /* FSINFO */
1034 nfs3svc_encode_fsinfores(struct svc_rqst *rqstp, u32 *p,
1035 struct nfsd3_fsinfores *resp)
1037 *p++ = xdr_zero; /* no post_op_attr */
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);
1053 return xdr_ressize_check(rqstp, p);
1056 /* PATHCONF */
1058 nfs3svc_encode_pathconfres(struct svc_rqst *rqstp, u32 *p,
1059 struct nfsd3_pathconfres *resp)
1061 *p++ = xdr_zero; /* no post_op_attr */
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);
1072 return xdr_ressize_check(rqstp, p);
1075 /* COMMIT */
1077 nfs3svc_encode_commitres(struct svc_rqst *rqstp, u32 *p,
1078 struct nfsd3_commitres *resp)
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);
1086 return xdr_ressize_check(rqstp, p);
1090 * XDR release functions
1093 nfs3svc_release_fhandle(struct svc_rqst *rqstp, u32 *p,
1094 struct nfsd3_attrstat *resp)
1096 fh_put(&resp->fh);
1097 return 1;
1101 nfs3svc_release_fhandle2(struct svc_rqst *rqstp, u32 *p,
1102 struct nfsd3_fhandle_pair *resp)
1104 fh_put(&resp->fh1);
1105 fh_put(&resp->fh2);
1106 return 1;