1
2
3
4
5
6
7#include "vfs.h"
8#include "xdr.h"
9#include "auth.h"
10
11#define NFSDDBG_FACILITY NFSDDBG_XDR
12
13
14
15
16static u32 nfs_ftypes[] = {
17 NFNON, NFCHR, NFCHR, NFBAD,
18 NFDIR, NFBAD, NFBLK, NFBAD,
19 NFREG, NFBAD, NFLNK, NFBAD,
20 NFSOCK, NFBAD, NFLNK, NFBAD,
21};
22
23
24
25
26
27static __be32 *
28decode_fh(__be32 *p, struct svc_fh *fhp)
29{
30 fh_init(fhp, NFS_FHSIZE);
31 memcpy(&fhp->fh_handle.fh_base, p, NFS_FHSIZE);
32 fhp->fh_handle.fh_size = NFS_FHSIZE;
33
34
35
36 return p + (NFS_FHSIZE >> 2);
37}
38
39
40__be32 *nfs2svc_decode_fh(__be32 *p, struct svc_fh *fhp)
41{
42 return decode_fh(p, fhp);
43}
44
45static __be32 *
46encode_fh(__be32 *p, struct svc_fh *fhp)
47{
48 memcpy(p, &fhp->fh_handle.fh_base, NFS_FHSIZE);
49 return p + (NFS_FHSIZE>> 2);
50}
51
52
53
54
55
56static __be32 *
57decode_filename(__be32 *p, char **namp, unsigned int *lenp)
58{
59 char *name;
60 unsigned int i;
61
62 if ((p = xdr_decode_string_inplace(p, namp, lenp, NFS_MAXNAMLEN)) != NULL) {
63 for (i = 0, name = *namp; i < *lenp; i++, name++) {
64 if (*name == '\0' || *name == '/')
65 return NULL;
66 }
67 }
68
69 return p;
70}
71
72static __be32 *
73decode_pathname(__be32 *p, char **namp, unsigned int *lenp)
74{
75 char *name;
76 unsigned int i;
77
78 if ((p = xdr_decode_string_inplace(p, namp, lenp, NFS_MAXPATHLEN)) != NULL) {
79 for (i = 0, name = *namp; i < *lenp; i++, name++) {
80 if (*name == '\0')
81 return NULL;
82 }
83 }
84
85 return p;
86}
87
88static __be32 *
89decode_sattr(__be32 *p, struct iattr *iap)
90{
91 u32 tmp, tmp1;
92
93 iap->ia_valid = 0;
94
95
96
97
98
99 if ((tmp = ntohl(*p++)) != (u32)-1 && tmp != 0xffff) {
100 iap->ia_valid |= ATTR_MODE;
101 iap->ia_mode = tmp;
102 }
103 if ((tmp = ntohl(*p++)) != (u32)-1) {
104 iap->ia_uid = make_kuid(&init_user_ns, tmp);
105 if (uid_valid(iap->ia_uid))
106 iap->ia_valid |= ATTR_UID;
107 }
108 if ((tmp = ntohl(*p++)) != (u32)-1) {
109 iap->ia_gid = make_kgid(&init_user_ns, tmp);
110 if (gid_valid(iap->ia_gid))
111 iap->ia_valid |= ATTR_GID;
112 }
113 if ((tmp = ntohl(*p++)) != (u32)-1) {
114 iap->ia_valid |= ATTR_SIZE;
115 iap->ia_size = tmp;
116 }
117 tmp = ntohl(*p++); tmp1 = ntohl(*p++);
118 if (tmp != (u32)-1 && tmp1 != (u32)-1) {
119 iap->ia_valid |= ATTR_ATIME | ATTR_ATIME_SET;
120 iap->ia_atime.tv_sec = tmp;
121 iap->ia_atime.tv_nsec = tmp1 * 1000;
122 }
123 tmp = ntohl(*p++); tmp1 = ntohl(*p++);
124 if (tmp != (u32)-1 && tmp1 != (u32)-1) {
125 iap->ia_valid |= ATTR_MTIME | ATTR_MTIME_SET;
126 iap->ia_mtime.tv_sec = tmp;
127 iap->ia_mtime.tv_nsec = tmp1 * 1000;
128
129
130
131
132
133
134
135
136
137 if (tmp1 == 1000000)
138 iap->ia_valid &= ~(ATTR_ATIME_SET|ATTR_MTIME_SET);
139 }
140 return p;
141}
142
143static __be32 *
144encode_fattr(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp,
145 struct kstat *stat)
146{
147 struct dentry *dentry = fhp->fh_dentry;
148 int type;
149 struct timespec time;
150 u32 f;
151
152 type = (stat->mode & S_IFMT);
153
154 *p++ = htonl(nfs_ftypes[type >> 12]);
155 *p++ = htonl((u32) stat->mode);
156 *p++ = htonl((u32) stat->nlink);
157 *p++ = htonl((u32) from_kuid(&init_user_ns, stat->uid));
158 *p++ = htonl((u32) from_kgid(&init_user_ns, stat->gid));
159
160 if (S_ISLNK(type) && stat->size > NFS_MAXPATHLEN) {
161 *p++ = htonl(NFS_MAXPATHLEN);
162 } else {
163 *p++ = htonl((u32) stat->size);
164 }
165 *p++ = htonl((u32) stat->blksize);
166 if (S_ISCHR(type) || S_ISBLK(type))
167 *p++ = htonl(new_encode_dev(stat->rdev));
168 else
169 *p++ = htonl(0xffffffff);
170 *p++ = htonl((u32) stat->blocks);
171 switch (fsid_source(fhp)) {
172 default:
173 case FSIDSOURCE_DEV:
174 *p++ = htonl(new_encode_dev(stat->dev));
175 break;
176 case FSIDSOURCE_FSID:
177 *p++ = htonl((u32) fhp->fh_export->ex_fsid);
178 break;
179 case FSIDSOURCE_UUID:
180 f = ((u32*)fhp->fh_export->ex_uuid)[0];
181 f ^= ((u32*)fhp->fh_export->ex_uuid)[1];
182 f ^= ((u32*)fhp->fh_export->ex_uuid)[2];
183 f ^= ((u32*)fhp->fh_export->ex_uuid)[3];
184 *p++ = htonl(f);
185 break;
186 }
187 *p++ = htonl((u32) stat->ino);
188 *p++ = htonl((u32) stat->atime.tv_sec);
189 *p++ = htonl(stat->atime.tv_nsec ? stat->atime.tv_nsec / 1000 : 0);
190 time = stat->mtime;
191 lease_get_mtime(dentry->d_inode, &time);
192 *p++ = htonl((u32) time.tv_sec);
193 *p++ = htonl(time.tv_nsec ? time.tv_nsec / 1000 : 0);
194 *p++ = htonl((u32) stat->ctime.tv_sec);
195 *p++ = htonl(stat->ctime.tv_nsec ? stat->ctime.tv_nsec / 1000 : 0);
196
197 return p;
198}
199
200
201__be32 *nfs2svc_encode_fattr(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp, struct kstat *stat)
202{
203 return encode_fattr(rqstp, p, fhp, stat);
204}
205
206
207
208
209int
210nfssvc_decode_void(struct svc_rqst *rqstp, __be32 *p, void *dummy)
211{
212 return xdr_argsize_check(rqstp, p);
213}
214
215int
216nfssvc_decode_fhandle(struct svc_rqst *rqstp, __be32 *p, struct nfsd_fhandle *args)
217{
218 p = decode_fh(p, &args->fh);
219 if (!p)
220 return 0;
221 return xdr_argsize_check(rqstp, p);
222}
223
224int
225nfssvc_decode_sattrargs(struct svc_rqst *rqstp, __be32 *p,
226 struct nfsd_sattrargs *args)
227{
228 p = decode_fh(p, &args->fh);
229 if (!p)
230 return 0;
231 p = decode_sattr(p, &args->attrs);
232
233 return xdr_argsize_check(rqstp, p);
234}
235
236int
237nfssvc_decode_diropargs(struct svc_rqst *rqstp, __be32 *p,
238 struct nfsd_diropargs *args)
239{
240 if (!(p = decode_fh(p, &args->fh))
241 || !(p = decode_filename(p, &args->name, &args->len)))
242 return 0;
243
244 return xdr_argsize_check(rqstp, p);
245}
246
247int
248nfssvc_decode_readargs(struct svc_rqst *rqstp, __be32 *p,
249 struct nfsd_readargs *args)
250{
251 unsigned int len;
252 int v;
253 p = decode_fh(p, &args->fh);
254 if (!p)
255 return 0;
256
257 args->offset = ntohl(*p++);
258 len = args->count = ntohl(*p++);
259 p++;
260
261 len = min_t(unsigned int, len, NFSSVC_MAXBLKSIZE_V2);
262
263
264
265
266 v=0;
267 while (len > 0) {
268 struct page *p = *(rqstp->rq_next_page++);
269
270 rqstp->rq_vec[v].iov_base = page_address(p);
271 rqstp->rq_vec[v].iov_len = min_t(unsigned int, len, PAGE_SIZE);
272 len -= rqstp->rq_vec[v].iov_len;
273 v++;
274 }
275 args->vlen = v;
276 return xdr_argsize_check(rqstp, p);
277}
278
279int
280nfssvc_decode_writeargs(struct svc_rqst *rqstp, __be32 *p,
281 struct nfsd_writeargs *args)
282{
283 unsigned int len, hdr, dlen;
284 struct kvec *head = rqstp->rq_arg.head;
285
286 p = decode_fh(p, &args->fh);
287 if (!p)
288 return 0;
289
290 p++;
291 args->offset = ntohl(*p++);
292 p++;
293 len = args->len = ntohl(*p++);
294
295
296
297 if (len > NFSSVC_MAXBLKSIZE_V2)
298 return 0;
299
300
301
302
303
304 hdr = (void*)p - head->iov_base;
305 if (hdr > head->iov_len)
306 return 0;
307 dlen = head->iov_len + rqstp->rq_arg.page_len - hdr;
308
309
310
311
312
313
314
315
316
317 if (dlen < XDR_QUADLEN(len)*4)
318 return 0;
319
320 args->first.iov_base = (void *)p;
321 args->first.iov_len = head->iov_len - hdr;
322 return 1;
323}
324
325int
326nfssvc_decode_createargs(struct svc_rqst *rqstp, __be32 *p,
327 struct nfsd_createargs *args)
328{
329 if ( !(p = decode_fh(p, &args->fh))
330 || !(p = decode_filename(p, &args->name, &args->len)))
331 return 0;
332 p = decode_sattr(p, &args->attrs);
333
334 return xdr_argsize_check(rqstp, p);
335}
336
337int
338nfssvc_decode_renameargs(struct svc_rqst *rqstp, __be32 *p,
339 struct nfsd_renameargs *args)
340{
341 if (!(p = decode_fh(p, &args->ffh))
342 || !(p = decode_filename(p, &args->fname, &args->flen))
343 || !(p = decode_fh(p, &args->tfh))
344 || !(p = decode_filename(p, &args->tname, &args->tlen)))
345 return 0;
346
347 return xdr_argsize_check(rqstp, p);
348}
349
350int
351nfssvc_decode_readlinkargs(struct svc_rqst *rqstp, __be32 *p, struct nfsd_readlinkargs *args)
352{
353 p = decode_fh(p, &args->fh);
354 if (!p)
355 return 0;
356 args->buffer = page_address(*(rqstp->rq_next_page++));
357
358 return xdr_argsize_check(rqstp, p);
359}
360
361int
362nfssvc_decode_linkargs(struct svc_rqst *rqstp, __be32 *p,
363 struct nfsd_linkargs *args)
364{
365 if (!(p = decode_fh(p, &args->ffh))
366 || !(p = decode_fh(p, &args->tfh))
367 || !(p = decode_filename(p, &args->tname, &args->tlen)))
368 return 0;
369
370 return xdr_argsize_check(rqstp, p);
371}
372
373int
374nfssvc_decode_symlinkargs(struct svc_rqst *rqstp, __be32 *p,
375 struct nfsd_symlinkargs *args)
376{
377 if ( !(p = decode_fh(p, &args->ffh))
378 || !(p = decode_filename(p, &args->fname, &args->flen))
379 || !(p = decode_pathname(p, &args->tname, &args->tlen)))
380 return 0;
381 p = decode_sattr(p, &args->attrs);
382
383 return xdr_argsize_check(rqstp, p);
384}
385
386int
387nfssvc_decode_readdirargs(struct svc_rqst *rqstp, __be32 *p,
388 struct nfsd_readdirargs *args)
389{
390 p = decode_fh(p, &args->fh);
391 if (!p)
392 return 0;
393 args->cookie = ntohl(*p++);
394 args->count = ntohl(*p++);
395 args->count = min_t(u32, args->count, PAGE_SIZE);
396 args->buffer = page_address(*(rqstp->rq_next_page++));
397
398 return xdr_argsize_check(rqstp, p);
399}
400
401
402
403
404int
405nfssvc_encode_void(struct svc_rqst *rqstp, __be32 *p, void *dummy)
406{
407 return xdr_ressize_check(rqstp, p);
408}
409
410int
411nfssvc_encode_attrstat(struct svc_rqst *rqstp, __be32 *p,
412 struct nfsd_attrstat *resp)
413{
414 p = encode_fattr(rqstp, p, &resp->fh, &resp->stat);
415 return xdr_ressize_check(rqstp, p);
416}
417
418int
419nfssvc_encode_diropres(struct svc_rqst *rqstp, __be32 *p,
420 struct nfsd_diropres *resp)
421{
422 p = encode_fh(p, &resp->fh);
423 p = encode_fattr(rqstp, p, &resp->fh, &resp->stat);
424 return xdr_ressize_check(rqstp, p);
425}
426
427int
428nfssvc_encode_readlinkres(struct svc_rqst *rqstp, __be32 *p,
429 struct nfsd_readlinkres *resp)
430{
431 *p++ = htonl(resp->len);
432 xdr_ressize_check(rqstp, p);
433 rqstp->rq_res.page_len = resp->len;
434 if (resp->len & 3) {
435
436 rqstp->rq_res.tail[0].iov_base = p;
437 *p = 0;
438 rqstp->rq_res.tail[0].iov_len = 4 - (resp->len&3);
439 }
440 return 1;
441}
442
443int
444nfssvc_encode_readres(struct svc_rqst *rqstp, __be32 *p,
445 struct nfsd_readres *resp)
446{
447 p = encode_fattr(rqstp, p, &resp->fh, &resp->stat);
448 *p++ = htonl(resp->count);
449 xdr_ressize_check(rqstp, p);
450
451
452 rqstp->rq_res.page_len = resp->count;
453 if (resp->count & 3) {
454
455 rqstp->rq_res.tail[0].iov_base = p;
456 *p = 0;
457 rqstp->rq_res.tail[0].iov_len = 4 - (resp->count&3);
458 }
459 return 1;
460}
461
462int
463nfssvc_encode_readdirres(struct svc_rqst *rqstp, __be32 *p,
464 struct nfsd_readdirres *resp)
465{
466 xdr_ressize_check(rqstp, p);
467 p = resp->buffer;
468 *p++ = 0;
469 *p++ = htonl((resp->common.err == nfserr_eof));
470 rqstp->rq_res.page_len = (((unsigned long)p-1) & ~PAGE_MASK)+1;
471
472 return 1;
473}
474
475int
476nfssvc_encode_statfsres(struct svc_rqst *rqstp, __be32 *p,
477 struct nfsd_statfsres *resp)
478{
479 struct kstatfs *stat = &resp->stats;
480
481 *p++ = htonl(NFSSVC_MAXBLKSIZE_V2);
482 *p++ = htonl(stat->f_bsize);
483 *p++ = htonl(stat->f_blocks);
484 *p++ = htonl(stat->f_bfree);
485 *p++ = htonl(stat->f_bavail);
486 return xdr_ressize_check(rqstp, p);
487}
488
489int
490nfssvc_encode_entry(void *ccdv, const char *name,
491 int namlen, loff_t offset, u64 ino, unsigned int d_type)
492{
493 struct readdir_cd *ccd = ccdv;
494 struct nfsd_readdirres *cd = container_of(ccd, struct nfsd_readdirres, common);
495 __be32 *p = cd->buffer;
496 int buflen, slen;
497
498
499
500
501
502
503 if (offset > ~((u32) 0)) {
504 cd->common.err = nfserr_fbig;
505 return -EINVAL;
506 }
507 if (cd->offset)
508 *cd->offset = htonl(offset);
509
510
511 namlen = min(namlen, NFS2_MAXNAMLEN);
512 slen = XDR_QUADLEN(namlen);
513
514 if ((buflen = cd->buflen - slen - 4) < 0) {
515 cd->common.err = nfserr_toosmall;
516 return -EINVAL;
517 }
518 if (ino > ~((u32) 0)) {
519 cd->common.err = nfserr_fbig;
520 return -EINVAL;
521 }
522 *p++ = xdr_one;
523 *p++ = htonl((u32) ino);
524 p = xdr_encode_array(p, name, namlen);
525 cd->offset = p;
526 *p++ = htonl(~0U);
527
528 cd->buflen = buflen;
529 cd->buffer = p;
530 cd->common.err = nfs_ok;
531 return 0;
532}
533
534
535
536
537int
538nfssvc_release_fhandle(struct svc_rqst *rqstp, __be32 *p,
539 struct nfsd_fhandle *resp)
540{
541 fh_put(&resp->fh);
542 return 1;
543}
544