[PATCH] knfsd: Replace two page lists in struct svc_rqst with one
[deliverable/linux.git] / fs / nfsd / nfs3xdr.c
1 /*
2 * linux/fs/nfsd/nfs3xdr.c
3 *
4 * XDR support for nfsd/protocol version 3.
5 *
6 * Copyright (C) 1995, 1996, 1997 Olaf Kirch <okir@monad.swb.de>
7 *
8 * 2003-08-09 Jamie Lokier: Use htonl() for nanoseconds, not htons()!
9 */
10
11 #include <linux/types.h>
12 #include <linux/time.h>
13 #include <linux/nfs3.h>
14 #include <linux/list.h>
15 #include <linux/spinlock.h>
16 #include <linux/dcache.h>
17 #include <linux/namei.h>
18 #include <linux/mm.h>
19 #include <linux/vfs.h>
20 #include <linux/sunrpc/xdr.h>
21 #include <linux/sunrpc/svc.h>
22 #include <linux/nfsd/nfsd.h>
23 #include <linux/nfsd/xdr3.h>
24
25 #define NFSDDBG_FACILITY NFSDDBG_XDR
26
27 #ifdef NFSD_OPTIMIZE_SPACE
28 # define inline
29 #endif
30
31
32 /*
33 * Mapping of S_IF* types to NFS file types
34 */
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,
40 };
41
42 /*
43 * XDR functions for basic NFS types
44 */
45 static inline u32 *
46 encode_time3(u32 *p, struct timespec *time)
47 {
48 *p++ = htonl((u32) time->tv_sec); *p++ = htonl(time->tv_nsec);
49 return p;
50 }
51
52 static inline u32 *
53 decode_time3(u32 *p, struct timespec *time)
54 {
55 time->tv_sec = ntohl(*p++);
56 time->tv_nsec = ntohl(*p++);
57 return p;
58 }
59
60 static inline u32 *
61 decode_fh(u32 *p, struct svc_fh *fhp)
62 {
63 unsigned int size;
64 fh_init(fhp, NFS3_FHSIZE);
65 size = ntohl(*p++);
66 if (size > NFS3_FHSIZE)
67 return NULL;
68
69 memcpy(&fhp->fh_handle.fh_base, p, size);
70 fhp->fh_handle.fh_size = size;
71 return p + XDR_QUADLEN(size);
72 }
73
74 /* Helper function for NFSv3 ACL code */
75 u32 *nfs3svc_decode_fh(u32 *p, struct svc_fh *fhp)
76 {
77 return decode_fh(p, fhp);
78 }
79
80 static inline u32 *
81 encode_fh(u32 *p, struct svc_fh *fhp)
82 {
83 unsigned int size = fhp->fh_handle.fh_size;
84 *p++ = htonl(size);
85 if (size) p[XDR_QUADLEN(size)-1]=0;
86 memcpy(p, &fhp->fh_handle.fh_base, size);
87 return p + XDR_QUADLEN(size);
88 }
89
90 /*
91 * Decode a file name and make sure that the path contains
92 * no slashes or null bytes.
93 */
94 static inline u32 *
95 decode_filename(u32 *p, char **namp, int *lenp)
96 {
97 char *name;
98 int i;
99
100 if ((p = xdr_decode_string_inplace(p, namp, lenp, NFS3_MAXNAMLEN)) != NULL) {
101 for (i = 0, name = *namp; i < *lenp; i++, name++) {
102 if (*name == '\0' || *name == '/')
103 return NULL;
104 }
105 }
106
107 return p;
108 }
109
110 static inline u32 *
111 decode_sattr3(u32 *p, struct iattr *iap)
112 {
113 u32 tmp;
114
115 iap->ia_valid = 0;
116
117 if (*p++) {
118 iap->ia_valid |= ATTR_MODE;
119 iap->ia_mode = ntohl(*p++);
120 }
121 if (*p++) {
122 iap->ia_valid |= ATTR_UID;
123 iap->ia_uid = ntohl(*p++);
124 }
125 if (*p++) {
126 iap->ia_valid |= ATTR_GID;
127 iap->ia_gid = ntohl(*p++);
128 }
129 if (*p++) {
130 u64 newsize;
131
132 iap->ia_valid |= ATTR_SIZE;
133 p = xdr_decode_hyper(p, &newsize);
134 if (newsize <= NFS_OFFSET_MAX)
135 iap->ia_size = newsize;
136 else
137 iap->ia_size = NFS_OFFSET_MAX;
138 }
139 if ((tmp = ntohl(*p++)) == 1) { /* set to server time */
140 iap->ia_valid |= ATTR_ATIME;
141 } else if (tmp == 2) { /* set to client time */
142 iap->ia_valid |= ATTR_ATIME | ATTR_ATIME_SET;
143 iap->ia_atime.tv_sec = ntohl(*p++);
144 iap->ia_atime.tv_nsec = ntohl(*p++);
145 }
146 if ((tmp = ntohl(*p++)) == 1) { /* set to server time */
147 iap->ia_valid |= ATTR_MTIME;
148 } else if (tmp == 2) { /* set to client time */
149 iap->ia_valid |= ATTR_MTIME | ATTR_MTIME_SET;
150 iap->ia_mtime.tv_sec = ntohl(*p++);
151 iap->ia_mtime.tv_nsec = ntohl(*p++);
152 }
153 return p;
154 }
155
156 static inline u32 *
157 encode_fattr3(struct svc_rqst *rqstp, u32 *p, struct svc_fh *fhp,
158 struct kstat *stat)
159 {
160 struct dentry *dentry = fhp->fh_dentry;
161 struct timespec time;
162
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);
172 }
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);
185
186 return p;
187 }
188
189 static inline u32 *
190 encode_saved_post_attr(struct svc_rqst *rqstp, u32 *p, struct svc_fh *fhp)
191 {
192 struct inode *inode = fhp->fh_dentry->d_inode;
193
194 /* Attributes to follow */
195 *p++ = xdr_one;
196
197 *p++ = htonl(nfs3_ftypes[(fhp->fh_post_mode & S_IFMT) >> 12]);
198 *p++ = htonl((u32) fhp->fh_post_mode);
199 *p++ = htonl((u32) fhp->fh_post_nlink);
200 *p++ = htonl((u32) nfsd_ruid(rqstp, fhp->fh_post_uid));
201 *p++ = htonl((u32) nfsd_rgid(rqstp, fhp->fh_post_gid));
202 if (S_ISLNK(fhp->fh_post_mode) && fhp->fh_post_size > NFS3_MAXPATHLEN) {
203 p = xdr_encode_hyper(p, (u64) NFS3_MAXPATHLEN);
204 } else {
205 p = xdr_encode_hyper(p, (u64) fhp->fh_post_size);
206 }
207 p = xdr_encode_hyper(p, ((u64)fhp->fh_post_blocks) << 9);
208 *p++ = fhp->fh_post_rdev[0];
209 *p++ = fhp->fh_post_rdev[1];
210 if (is_fsid(fhp, rqstp->rq_reffh))
211 p = xdr_encode_hyper(p, (u64) fhp->fh_export->ex_fsid);
212 else
213 p = xdr_encode_hyper(p, (u64)huge_encode_dev(inode->i_sb->s_dev));
214 p = xdr_encode_hyper(p, (u64) inode->i_ino);
215 p = encode_time3(p, &fhp->fh_post_atime);
216 p = encode_time3(p, &fhp->fh_post_mtime);
217 p = encode_time3(p, &fhp->fh_post_ctime);
218
219 return p;
220 }
221
222 /*
223 * Encode post-operation attributes.
224 * The inode may be NULL if the call failed because of a stale file
225 * handle. In this case, no attributes are returned.
226 */
227 static u32 *
228 encode_post_op_attr(struct svc_rqst *rqstp, u32 *p, struct svc_fh *fhp)
229 {
230 struct dentry *dentry = fhp->fh_dentry;
231 if (dentry && dentry->d_inode != NULL) {
232 int err;
233 struct kstat stat;
234
235 err = vfs_getattr(fhp->fh_export->ex_mnt, dentry, &stat);
236 if (!err) {
237 *p++ = xdr_one; /* attributes follow */
238 return encode_fattr3(rqstp, p, fhp, &stat);
239 }
240 }
241 *p++ = xdr_zero;
242 return p;
243 }
244
245 /* Helper for NFSv3 ACLs */
246 u32 *
247 nfs3svc_encode_post_op_attr(struct svc_rqst *rqstp, u32 *p, struct svc_fh *fhp)
248 {
249 return encode_post_op_attr(rqstp, p, fhp);
250 }
251
252 /*
253 * Enocde weak cache consistency data
254 */
255 static u32 *
256 encode_wcc_data(struct svc_rqst *rqstp, u32 *p, struct svc_fh *fhp)
257 {
258 struct dentry *dentry = fhp->fh_dentry;
259
260 if (dentry && dentry->d_inode && fhp->fh_post_saved) {
261 if (fhp->fh_pre_saved) {
262 *p++ = xdr_one;
263 p = xdr_encode_hyper(p, (u64) fhp->fh_pre_size);
264 p = encode_time3(p, &fhp->fh_pre_mtime);
265 p = encode_time3(p, &fhp->fh_pre_ctime);
266 } else {
267 *p++ = xdr_zero;
268 }
269 return encode_saved_post_attr(rqstp, p, fhp);
270 }
271 /* no pre- or post-attrs */
272 *p++ = xdr_zero;
273 return encode_post_op_attr(rqstp, p, fhp);
274 }
275
276
277 /*
278 * XDR decode functions
279 */
280 int
281 nfs3svc_decode_fhandle(struct svc_rqst *rqstp, u32 *p, struct nfsd_fhandle *args)
282 {
283 if (!(p = decode_fh(p, &args->fh)))
284 return 0;
285 return xdr_argsize_check(rqstp, p);
286 }
287
288 int
289 nfs3svc_decode_sattrargs(struct svc_rqst *rqstp, u32 *p,
290 struct nfsd3_sattrargs *args)
291 {
292 if (!(p = decode_fh(p, &args->fh))
293 || !(p = decode_sattr3(p, &args->attrs)))
294 return 0;
295
296 if ((args->check_guard = ntohl(*p++)) != 0) {
297 struct timespec time;
298 p = decode_time3(p, &time);
299 args->guardtime = time.tv_sec;
300 }
301
302 return xdr_argsize_check(rqstp, p);
303 }
304
305 int
306 nfs3svc_decode_diropargs(struct svc_rqst *rqstp, u32 *p,
307 struct nfsd3_diropargs *args)
308 {
309 if (!(p = decode_fh(p, &args->fh))
310 || !(p = decode_filename(p, &args->name, &args->len)))
311 return 0;
312
313 return xdr_argsize_check(rqstp, p);
314 }
315
316 int
317 nfs3svc_decode_accessargs(struct svc_rqst *rqstp, u32 *p,
318 struct nfsd3_accessargs *args)
319 {
320 if (!(p = decode_fh(p, &args->fh)))
321 return 0;
322 args->access = ntohl(*p++);
323
324 return xdr_argsize_check(rqstp, p);
325 }
326
327 int
328 nfs3svc_decode_readargs(struct svc_rqst *rqstp, u32 *p,
329 struct nfsd3_readargs *args)
330 {
331 unsigned int len;
332 int v,pn;
333
334 if (!(p = decode_fh(p, &args->fh))
335 || !(p = xdr_decode_hyper(p, &args->offset)))
336 return 0;
337
338 len = args->count = ntohl(*p++);
339
340 if (len > NFSSVC_MAXBLKSIZE)
341 len = NFSSVC_MAXBLKSIZE;
342
343 /* set up the kvec */
344 v=0;
345 while (len > 0) {
346 pn = rqstp->rq_resused++;
347 args->vec[v].iov_base = page_address(rqstp->rq_respages[pn]);
348 args->vec[v].iov_len = len < PAGE_SIZE? len : PAGE_SIZE;
349 len -= args->vec[v].iov_len;
350 v++;
351 }
352 args->vlen = v;
353 return xdr_argsize_check(rqstp, p);
354 }
355
356 int
357 nfs3svc_decode_writeargs(struct svc_rqst *rqstp, u32 *p,
358 struct nfsd3_writeargs *args)
359 {
360 unsigned int len, v, hdr;
361
362 if (!(p = decode_fh(p, &args->fh))
363 || !(p = xdr_decode_hyper(p, &args->offset)))
364 return 0;
365
366 args->count = ntohl(*p++);
367 args->stable = ntohl(*p++);
368 len = args->len = ntohl(*p++);
369
370 hdr = (void*)p - rqstp->rq_arg.head[0].iov_base;
371 if (rqstp->rq_arg.len < hdr ||
372 rqstp->rq_arg.len - hdr < len)
373 return 0;
374
375 args->vec[0].iov_base = (void*)p;
376 args->vec[0].iov_len = rqstp->rq_arg.head[0].iov_len - hdr;
377
378 if (len > NFSSVC_MAXBLKSIZE)
379 len = NFSSVC_MAXBLKSIZE;
380 v= 0;
381 while (len > args->vec[v].iov_len) {
382 len -= args->vec[v].iov_len;
383 v++;
384 args->vec[v].iov_base = page_address(rqstp->rq_pages[v]);
385 args->vec[v].iov_len = PAGE_SIZE;
386 }
387 args->vec[v].iov_len = len;
388 args->vlen = v+1;
389
390 return args->count == args->len && args->vec[0].iov_len > 0;
391 }
392
393 int
394 nfs3svc_decode_createargs(struct svc_rqst *rqstp, u32 *p,
395 struct nfsd3_createargs *args)
396 {
397 if (!(p = decode_fh(p, &args->fh))
398 || !(p = decode_filename(p, &args->name, &args->len)))
399 return 0;
400
401 switch (args->createmode = ntohl(*p++)) {
402 case NFS3_CREATE_UNCHECKED:
403 case NFS3_CREATE_GUARDED:
404 if (!(p = decode_sattr3(p, &args->attrs)))
405 return 0;
406 break;
407 case NFS3_CREATE_EXCLUSIVE:
408 args->verf = p;
409 p += 2;
410 break;
411 default:
412 return 0;
413 }
414
415 return xdr_argsize_check(rqstp, p);
416 }
417 int
418 nfs3svc_decode_mkdirargs(struct svc_rqst *rqstp, u32 *p,
419 struct nfsd3_createargs *args)
420 {
421 if (!(p = decode_fh(p, &args->fh))
422 || !(p = decode_filename(p, &args->name, &args->len))
423 || !(p = decode_sattr3(p, &args->attrs)))
424 return 0;
425
426 return xdr_argsize_check(rqstp, p);
427 }
428
429 int
430 nfs3svc_decode_symlinkargs(struct svc_rqst *rqstp, u32 *p,
431 struct nfsd3_symlinkargs *args)
432 {
433 unsigned int len;
434 int avail;
435 char *old, *new;
436 struct kvec *vec;
437
438 if (!(p = decode_fh(p, &args->ffh))
439 || !(p = decode_filename(p, &args->fname, &args->flen))
440 || !(p = decode_sattr3(p, &args->attrs))
441 )
442 return 0;
443 /* now decode the pathname, which might be larger than the first page.
444 * As we have to check for nul's anyway, we copy it into a new page
445 * This page appears in the rq_res.pages list, but as pages_len is always
446 * 0, it won't get in the way
447 */
448 len = ntohl(*p++);
449 if (len == 0 || len > NFS3_MAXPATHLEN || len >= PAGE_SIZE)
450 return 0;
451 args->tname = new =
452 page_address(rqstp->rq_respages[rqstp->rq_resused++]);
453 args->tlen = len;
454 /* first copy and check from the first page */
455 old = (char*)p;
456 vec = &rqstp->rq_arg.head[0];
457 avail = vec->iov_len - (old - (char*)vec->iov_base);
458 while (len && avail && *old) {
459 *new++ = *old++;
460 len--;
461 avail--;
462 }
463 /* now copy next page if there is one */
464 if (len && !avail && rqstp->rq_arg.page_len) {
465 avail = rqstp->rq_arg.page_len;
466 if (avail > PAGE_SIZE) avail = PAGE_SIZE;
467 old = page_address(rqstp->rq_arg.pages[0]);
468 }
469 while (len && avail && *old) {
470 *new++ = *old++;
471 len--;
472 avail--;
473 }
474 *new = '\0';
475 if (len)
476 return 0;
477
478 return 1;
479 }
480
481 int
482 nfs3svc_decode_mknodargs(struct svc_rqst *rqstp, u32 *p,
483 struct nfsd3_mknodargs *args)
484 {
485 if (!(p = decode_fh(p, &args->fh))
486 || !(p = decode_filename(p, &args->name, &args->len)))
487 return 0;
488
489 args->ftype = ntohl(*p++);
490
491 if (args->ftype == NF3BLK || args->ftype == NF3CHR
492 || args->ftype == NF3SOCK || args->ftype == NF3FIFO) {
493 if (!(p = decode_sattr3(p, &args->attrs)))
494 return 0;
495 }
496
497 if (args->ftype == NF3BLK || args->ftype == NF3CHR) {
498 args->major = ntohl(*p++);
499 args->minor = ntohl(*p++);
500 }
501
502 return xdr_argsize_check(rqstp, p);
503 }
504
505 int
506 nfs3svc_decode_renameargs(struct svc_rqst *rqstp, u32 *p,
507 struct nfsd3_renameargs *args)
508 {
509 if (!(p = decode_fh(p, &args->ffh))
510 || !(p = decode_filename(p, &args->fname, &args->flen))
511 || !(p = decode_fh(p, &args->tfh))
512 || !(p = decode_filename(p, &args->tname, &args->tlen)))
513 return 0;
514
515 return xdr_argsize_check(rqstp, p);
516 }
517
518 int
519 nfs3svc_decode_readlinkargs(struct svc_rqst *rqstp, u32 *p,
520 struct nfsd3_readlinkargs *args)
521 {
522 if (!(p = decode_fh(p, &args->fh)))
523 return 0;
524 args->buffer =
525 page_address(rqstp->rq_respages[rqstp->rq_resused++]);
526
527 return xdr_argsize_check(rqstp, p);
528 }
529
530 int
531 nfs3svc_decode_linkargs(struct svc_rqst *rqstp, u32 *p,
532 struct nfsd3_linkargs *args)
533 {
534 if (!(p = decode_fh(p, &args->ffh))
535 || !(p = decode_fh(p, &args->tfh))
536 || !(p = decode_filename(p, &args->tname, &args->tlen)))
537 return 0;
538
539 return xdr_argsize_check(rqstp, p);
540 }
541
542 int
543 nfs3svc_decode_readdirargs(struct svc_rqst *rqstp, u32 *p,
544 struct nfsd3_readdirargs *args)
545 {
546 if (!(p = decode_fh(p, &args->fh)))
547 return 0;
548 p = xdr_decode_hyper(p, &args->cookie);
549 args->verf = p; p += 2;
550 args->dircount = ~0;
551 args->count = ntohl(*p++);
552
553 if (args->count > PAGE_SIZE)
554 args->count = PAGE_SIZE;
555
556 args->buffer =
557 page_address(rqstp->rq_respages[rqstp->rq_resused++]);
558
559 return xdr_argsize_check(rqstp, p);
560 }
561
562 int
563 nfs3svc_decode_readdirplusargs(struct svc_rqst *rqstp, u32 *p,
564 struct nfsd3_readdirargs *args)
565 {
566 int len, pn;
567
568 if (!(p = decode_fh(p, &args->fh)))
569 return 0;
570 p = xdr_decode_hyper(p, &args->cookie);
571 args->verf = p; p += 2;
572 args->dircount = ntohl(*p++);
573 args->count = ntohl(*p++);
574
575 len = (args->count > NFSSVC_MAXBLKSIZE) ? NFSSVC_MAXBLKSIZE :
576 args->count;
577 args->count = len;
578
579 while (len > 0) {
580 pn = rqstp->rq_resused++;
581 if (!args->buffer)
582 args->buffer = page_address(rqstp->rq_respages[pn]);
583 len -= PAGE_SIZE;
584 }
585
586 return xdr_argsize_check(rqstp, p);
587 }
588
589 int
590 nfs3svc_decode_commitargs(struct svc_rqst *rqstp, u32 *p,
591 struct nfsd3_commitargs *args)
592 {
593 if (!(p = decode_fh(p, &args->fh)))
594 return 0;
595 p = xdr_decode_hyper(p, &args->offset);
596 args->count = ntohl(*p++);
597
598 return xdr_argsize_check(rqstp, p);
599 }
600
601 /*
602 * XDR encode functions
603 */
604 /*
605 * There must be an encoding function for void results so svc_process
606 * will work properly.
607 */
608 int
609 nfs3svc_encode_voidres(struct svc_rqst *rqstp, u32 *p, void *dummy)
610 {
611 return xdr_ressize_check(rqstp, p);
612 }
613
614 /* GETATTR */
615 int
616 nfs3svc_encode_attrstat(struct svc_rqst *rqstp, u32 *p,
617 struct nfsd3_attrstat *resp)
618 {
619 if (resp->status == 0)
620 p = encode_fattr3(rqstp, p, &resp->fh, &resp->stat);
621 return xdr_ressize_check(rqstp, p);
622 }
623
624 /* SETATTR, REMOVE, RMDIR */
625 int
626 nfs3svc_encode_wccstat(struct svc_rqst *rqstp, u32 *p,
627 struct nfsd3_attrstat *resp)
628 {
629 p = encode_wcc_data(rqstp, p, &resp->fh);
630 return xdr_ressize_check(rqstp, p);
631 }
632
633 /* LOOKUP */
634 int
635 nfs3svc_encode_diropres(struct svc_rqst *rqstp, u32 *p,
636 struct nfsd3_diropres *resp)
637 {
638 if (resp->status == 0) {
639 p = encode_fh(p, &resp->fh);
640 p = encode_post_op_attr(rqstp, p, &resp->fh);
641 }
642 p = encode_post_op_attr(rqstp, p, &resp->dirfh);
643 return xdr_ressize_check(rqstp, p);
644 }
645
646 /* ACCESS */
647 int
648 nfs3svc_encode_accessres(struct svc_rqst *rqstp, u32 *p,
649 struct nfsd3_accessres *resp)
650 {
651 p = encode_post_op_attr(rqstp, p, &resp->fh);
652 if (resp->status == 0)
653 *p++ = htonl(resp->access);
654 return xdr_ressize_check(rqstp, p);
655 }
656
657 /* READLINK */
658 int
659 nfs3svc_encode_readlinkres(struct svc_rqst *rqstp, u32 *p,
660 struct nfsd3_readlinkres *resp)
661 {
662 p = encode_post_op_attr(rqstp, p, &resp->fh);
663 if (resp->status == 0) {
664 *p++ = htonl(resp->len);
665 xdr_ressize_check(rqstp, p);
666 rqstp->rq_res.page_len = resp->len;
667 if (resp->len & 3) {
668 /* need to pad the tail */
669 rqstp->rq_res.tail[0].iov_base = p;
670 *p = 0;
671 rqstp->rq_res.tail[0].iov_len = 4 - (resp->len&3);
672 }
673 return 1;
674 } else
675 return xdr_ressize_check(rqstp, p);
676 }
677
678 /* READ */
679 int
680 nfs3svc_encode_readres(struct svc_rqst *rqstp, u32 *p,
681 struct nfsd3_readres *resp)
682 {
683 p = encode_post_op_attr(rqstp, p, &resp->fh);
684 if (resp->status == 0) {
685 *p++ = htonl(resp->count);
686 *p++ = htonl(resp->eof);
687 *p++ = htonl(resp->count); /* xdr opaque count */
688 xdr_ressize_check(rqstp, p);
689 /* now update rqstp->rq_res to reflect data aswell */
690 rqstp->rq_res.page_len = resp->count;
691 if (resp->count & 3) {
692 /* need to pad the tail */
693 rqstp->rq_res.tail[0].iov_base = p;
694 *p = 0;
695 rqstp->rq_res.tail[0].iov_len = 4 - (resp->count & 3);
696 }
697 return 1;
698 } else
699 return xdr_ressize_check(rqstp, p);
700 }
701
702 /* WRITE */
703 int
704 nfs3svc_encode_writeres(struct svc_rqst *rqstp, u32 *p,
705 struct nfsd3_writeres *resp)
706 {
707 p = encode_wcc_data(rqstp, p, &resp->fh);
708 if (resp->status == 0) {
709 *p++ = htonl(resp->count);
710 *p++ = htonl(resp->committed);
711 *p++ = htonl(nfssvc_boot.tv_sec);
712 *p++ = htonl(nfssvc_boot.tv_usec);
713 }
714 return xdr_ressize_check(rqstp, p);
715 }
716
717 /* CREATE, MKDIR, SYMLINK, MKNOD */
718 int
719 nfs3svc_encode_createres(struct svc_rqst *rqstp, u32 *p,
720 struct nfsd3_diropres *resp)
721 {
722 if (resp->status == 0) {
723 *p++ = xdr_one;
724 p = encode_fh(p, &resp->fh);
725 p = encode_post_op_attr(rqstp, p, &resp->fh);
726 }
727 p = encode_wcc_data(rqstp, p, &resp->dirfh);
728 return xdr_ressize_check(rqstp, p);
729 }
730
731 /* RENAME */
732 int
733 nfs3svc_encode_renameres(struct svc_rqst *rqstp, u32 *p,
734 struct nfsd3_renameres *resp)
735 {
736 p = encode_wcc_data(rqstp, p, &resp->ffh);
737 p = encode_wcc_data(rqstp, p, &resp->tfh);
738 return xdr_ressize_check(rqstp, p);
739 }
740
741 /* LINK */
742 int
743 nfs3svc_encode_linkres(struct svc_rqst *rqstp, u32 *p,
744 struct nfsd3_linkres *resp)
745 {
746 p = encode_post_op_attr(rqstp, p, &resp->fh);
747 p = encode_wcc_data(rqstp, p, &resp->tfh);
748 return xdr_ressize_check(rqstp, p);
749 }
750
751 /* READDIR */
752 int
753 nfs3svc_encode_readdirres(struct svc_rqst *rqstp, u32 *p,
754 struct nfsd3_readdirres *resp)
755 {
756 p = encode_post_op_attr(rqstp, p, &resp->fh);
757
758 if (resp->status == 0) {
759 /* stupid readdir cookie */
760 memcpy(p, resp->verf, 8); p += 2;
761 xdr_ressize_check(rqstp, p);
762 if (rqstp->rq_res.head[0].iov_len + (2<<2) > PAGE_SIZE)
763 return 1; /*No room for trailer */
764 rqstp->rq_res.page_len = (resp->count) << 2;
765
766 /* add the 'tail' to the end of the 'head' page - page 0. */
767 rqstp->rq_res.tail[0].iov_base = p;
768 *p++ = 0; /* no more entries */
769 *p++ = htonl(resp->common.err == nfserr_eof);
770 rqstp->rq_res.tail[0].iov_len = 2<<2;
771 return 1;
772 } else
773 return xdr_ressize_check(rqstp, p);
774 }
775
776 static inline u32 *
777 encode_entry_baggage(struct nfsd3_readdirres *cd, u32 *p, const char *name,
778 int namlen, ino_t ino)
779 {
780 *p++ = xdr_one; /* mark entry present */
781 p = xdr_encode_hyper(p, ino); /* file id */
782 p = xdr_encode_array(p, name, namlen);/* name length & name */
783
784 cd->offset = p; /* remember pointer */
785 p = xdr_encode_hyper(p, NFS_OFFSET_MAX);/* offset of next entry */
786
787 return p;
788 }
789
790 static inline u32 *
791 encode_entryplus_baggage(struct nfsd3_readdirres *cd, u32 *p,
792 struct svc_fh *fhp)
793 {
794 p = encode_post_op_attr(cd->rqstp, p, fhp);
795 *p++ = xdr_one; /* yes, a file handle follows */
796 p = encode_fh(p, fhp);
797 fh_put(fhp);
798 return p;
799 }
800
801 static int
802 compose_entry_fh(struct nfsd3_readdirres *cd, struct svc_fh *fhp,
803 const char *name, int namlen)
804 {
805 struct svc_export *exp;
806 struct dentry *dparent, *dchild;
807 int rv = 0;
808
809 dparent = cd->fh.fh_dentry;
810 exp = cd->fh.fh_export;
811
812 fh_init(fhp, NFS3_FHSIZE);
813 if (isdotent(name, namlen)) {
814 if (namlen == 2) {
815 dchild = dget_parent(dparent);
816 if (dchild == dparent) {
817 /* filesystem root - cannot return filehandle for ".." */
818 dput(dchild);
819 return 1;
820 }
821 } else
822 dchild = dget(dparent);
823 } else
824 dchild = lookup_one_len(name, dparent, namlen);
825 if (IS_ERR(dchild))
826 return 1;
827 if (d_mountpoint(dchild) ||
828 fh_compose(fhp, exp, dchild, &cd->fh) != 0 ||
829 !dchild->d_inode)
830 rv = 1;
831 dput(dchild);
832 return rv;
833 }
834
835 /*
836 * Encode a directory entry. This one works for both normal readdir
837 * and readdirplus.
838 * The normal readdir reply requires 2 (fileid) + 1 (stringlen)
839 * + string + 2 (cookie) + 1 (next) words, i.e. 6 + strlen.
840 *
841 * The readdirplus baggage is 1+21 words for post_op_attr, plus the
842 * file handle.
843 */
844
845 #define NFS3_ENTRY_BAGGAGE (2 + 1 + 2 + 1)
846 #define NFS3_ENTRYPLUS_BAGGAGE (1 + 21 + 1 + (NFS3_FHSIZE >> 2))
847 static int
848 encode_entry(struct readdir_cd *ccd, const char *name,
849 int namlen, off_t offset, ino_t ino, unsigned int d_type, int plus)
850 {
851 struct nfsd3_readdirres *cd = container_of(ccd, struct nfsd3_readdirres,
852 common);
853 u32 *p = cd->buffer;
854 caddr_t curr_page_addr = NULL;
855 int pn; /* current page number */
856 int slen; /* string (name) length */
857 int elen; /* estimated entry length in words */
858 int num_entry_words = 0; /* actual number of words */
859
860 if (cd->offset) {
861 u64 offset64 = offset;
862
863 if (unlikely(cd->offset1)) {
864 /* we ended up with offset on a page boundary */
865 *cd->offset = htonl(offset64 >> 32);
866 *cd->offset1 = htonl(offset64 & 0xffffffff);
867 cd->offset1 = NULL;
868 } else {
869 xdr_encode_hyper(cd->offset, (u64) offset);
870 }
871 }
872
873 /*
874 dprintk("encode_entry(%.*s @%ld%s)\n",
875 namlen, name, (long) offset, plus? " plus" : "");
876 */
877
878 /* truncate filename if too long */
879 if (namlen > NFS3_MAXNAMLEN)
880 namlen = NFS3_MAXNAMLEN;
881
882 slen = XDR_QUADLEN(namlen);
883 elen = slen + NFS3_ENTRY_BAGGAGE
884 + (plus? NFS3_ENTRYPLUS_BAGGAGE : 0);
885
886 if (cd->buflen < elen) {
887 cd->common.err = nfserr_toosmall;
888 return -EINVAL;
889 }
890
891 /* determine which page in rq_respages[] we are currently filling */
892 for (pn=1; pn < cd->rqstp->rq_resused; pn++) {
893 curr_page_addr = page_address(cd->rqstp->rq_respages[pn]);
894
895 if (((caddr_t)cd->buffer >= curr_page_addr) &&
896 ((caddr_t)cd->buffer < curr_page_addr + PAGE_SIZE))
897 break;
898 }
899
900 if ((caddr_t)(cd->buffer + elen) < (curr_page_addr + PAGE_SIZE)) {
901 /* encode entry in current page */
902
903 p = encode_entry_baggage(cd, p, name, namlen, ino);
904
905 /* throw in readdirplus baggage */
906 if (plus) {
907 struct svc_fh fh;
908
909 if (compose_entry_fh(cd, &fh, name, namlen) > 0) {
910 *p++ = 0;
911 *p++ = 0;
912 } else
913 p = encode_entryplus_baggage(cd, p, &fh);
914 }
915 num_entry_words = p - cd->buffer;
916 } else if (cd->rqstp->rq_respages[pn+1] != NULL) {
917 /* temporarily encode entry into next page, then move back to
918 * current and next page in rq_respages[] */
919 u32 *p1, *tmp;
920 int len1, len2;
921
922 /* grab next page for temporary storage of entry */
923 p1 = tmp = page_address(cd->rqstp->rq_respages[pn+1]);
924
925 p1 = encode_entry_baggage(cd, p1, name, namlen, ino);
926
927 /* throw in readdirplus baggage */
928 if (plus) {
929 struct svc_fh fh;
930
931 if (compose_entry_fh(cd, &fh, name, namlen) > 0) {
932 /* zero out the filehandle */
933 *p1++ = 0;
934 *p1++ = 0;
935 } else
936 p1 = encode_entryplus_baggage(cd, p1, &fh);
937 }
938
939 /* determine entry word length and lengths to go in pages */
940 num_entry_words = p1 - tmp;
941 len1 = curr_page_addr + PAGE_SIZE - (caddr_t)cd->buffer;
942 if ((num_entry_words << 2) < len1) {
943 /* the actual number of words in the entry is less
944 * than elen and can still fit in the current page
945 */
946 memmove(p, tmp, num_entry_words << 2);
947 p += num_entry_words;
948
949 /* update offset */
950 cd->offset = cd->buffer + (cd->offset - tmp);
951 } else {
952 unsigned int offset_r = (cd->offset - tmp) << 2;
953
954 /* update pointer to offset location.
955 * This is a 64bit quantity, so we need to
956 * deal with 3 cases:
957 * - entirely in first page
958 * - entirely in second page
959 * - 4 bytes in each page
960 */
961 if (offset_r + 8 <= len1) {
962 cd->offset = p + (cd->offset - tmp);
963 } else if (offset_r >= len1) {
964 cd->offset -= len1 >> 2;
965 } else {
966 /* sitting on the fence */
967 BUG_ON(offset_r != len1 - 4);
968 cd->offset = p + (cd->offset - tmp);
969 cd->offset1 = tmp;
970 }
971
972 len2 = (num_entry_words << 2) - len1;
973
974 /* move from temp page to current and next pages */
975 memmove(p, tmp, len1);
976 memmove(tmp, (caddr_t)tmp+len1, len2);
977
978 p = tmp + (len2 >> 2);
979 }
980 }
981 else {
982 cd->common.err = nfserr_toosmall;
983 return -EINVAL;
984 }
985
986 cd->buflen -= num_entry_words;
987 cd->buffer = p;
988 cd->common.err = nfs_ok;
989 return 0;
990
991 }
992
993 int
994 nfs3svc_encode_entry(struct readdir_cd *cd, const char *name,
995 int namlen, loff_t offset, ino_t ino, unsigned int d_type)
996 {
997 return encode_entry(cd, name, namlen, offset, ino, d_type, 0);
998 }
999
1000 int
1001 nfs3svc_encode_entry_plus(struct readdir_cd *cd, const char *name,
1002 int namlen, loff_t offset, ino_t ino, unsigned int d_type)
1003 {
1004 return encode_entry(cd, name, namlen, offset, ino, d_type, 1);
1005 }
1006
1007 /* FSSTAT */
1008 int
1009 nfs3svc_encode_fsstatres(struct svc_rqst *rqstp, u32 *p,
1010 struct nfsd3_fsstatres *resp)
1011 {
1012 struct kstatfs *s = &resp->stats;
1013 u64 bs = s->f_bsize;
1014
1015 *p++ = xdr_zero; /* no post_op_attr */
1016
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 */
1025 }
1026 return xdr_ressize_check(rqstp, p);
1027 }
1028
1029 /* FSINFO */
1030 int
1031 nfs3svc_encode_fsinfores(struct svc_rqst *rqstp, u32 *p,
1032 struct nfsd3_fsinfores *resp)
1033 {
1034 *p++ = xdr_zero; /* no post_op_attr */
1035
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);
1048 }
1049
1050 return xdr_ressize_check(rqstp, p);
1051 }
1052
1053 /* PATHCONF */
1054 int
1055 nfs3svc_encode_pathconfres(struct svc_rqst *rqstp, u32 *p,
1056 struct nfsd3_pathconfres *resp)
1057 {
1058 *p++ = xdr_zero; /* no post_op_attr */
1059
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);
1067 }
1068
1069 return xdr_ressize_check(rqstp, p);
1070 }
1071
1072 /* COMMIT */
1073 int
1074 nfs3svc_encode_commitres(struct svc_rqst *rqstp, u32 *p,
1075 struct nfsd3_commitres *resp)
1076 {
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);
1082 }
1083 return xdr_ressize_check(rqstp, p);
1084 }
1085
1086 /*
1087 * XDR release functions
1088 */
1089 int
1090 nfs3svc_release_fhandle(struct svc_rqst *rqstp, u32 *p,
1091 struct nfsd3_attrstat *resp)
1092 {
1093 fh_put(&resp->fh);
1094 return 1;
1095 }
1096
1097 int
1098 nfs3svc_release_fhandle2(struct svc_rqst *rqstp, u32 *p,
1099 struct nfsd3_fhandle_pair *resp)
1100 {
1101 fh_put(&resp->fh1);
1102 fh_put(&resp->fh2);
1103 return 1;
1104 }
This page took 0.052292 seconds and 6 git commands to generate.