Merge ../linux-2.6-watchdog-mm
[pandora-kernel.git] / fs / nfsd / nfsxdr.c
1 /*
2  * linux/fs/nfsd/nfsxdr.c
3  *
4  * XDR support for nfsd
5  *
6  * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
7  */
8
9 #include <linux/types.h>
10 #include <linux/time.h>
11 #include <linux/nfs.h>
12 #include <linux/vfs.h>
13 #include <linux/sunrpc/xdr.h>
14 #include <linux/sunrpc/svc.h>
15 #include <linux/nfsd/nfsd.h>
16 #include <linux/nfsd/xdr.h>
17 #include <linux/mm.h>
18
19 #define NFSDDBG_FACILITY                NFSDDBG_XDR
20
21 /*
22  * Mapping of S_IF* types to NFS file types
23  */
24 static u32      nfs_ftypes[] = {
25         NFNON,  NFCHR,  NFCHR, NFBAD,
26         NFDIR,  NFBAD,  NFBLK, NFBAD,
27         NFREG,  NFBAD,  NFLNK, NFBAD,
28         NFSOCK, NFBAD,  NFLNK, NFBAD,
29 };
30
31
32 /*
33  * XDR functions for basic NFS types
34  */
35 static __be32 *
36 decode_fh(__be32 *p, struct svc_fh *fhp)
37 {
38         fh_init(fhp, NFS_FHSIZE);
39         memcpy(&fhp->fh_handle.fh_base, p, NFS_FHSIZE);
40         fhp->fh_handle.fh_size = NFS_FHSIZE;
41
42         /* FIXME: Look up export pointer here and verify
43          * Sun Secure RPC if requested */
44         return p + (NFS_FHSIZE >> 2);
45 }
46
47 /* Helper function for NFSv2 ACL code */
48 __be32 *nfs2svc_decode_fh(__be32 *p, struct svc_fh *fhp)
49 {
50         return decode_fh(p, fhp);
51 }
52
53 static __be32 *
54 encode_fh(__be32 *p, struct svc_fh *fhp)
55 {
56         memcpy(p, &fhp->fh_handle.fh_base, NFS_FHSIZE);
57         return p + (NFS_FHSIZE>> 2);
58 }
59
60 /*
61  * Decode a file name and make sure that the path contains
62  * no slashes or null bytes.
63  */
64 static __be32 *
65 decode_filename(__be32 *p, char **namp, int *lenp)
66 {
67         char            *name;
68         int             i;
69
70         if ((p = xdr_decode_string_inplace(p, namp, lenp, NFS_MAXNAMLEN)) != NULL) {
71                 for (i = 0, name = *namp; i < *lenp; i++, name++) {
72                         if (*name == '\0' || *name == '/')
73                                 return NULL;
74                 }
75         }
76
77         return p;
78 }
79
80 static __be32 *
81 decode_pathname(__be32 *p, char **namp, int *lenp)
82 {
83         char            *name;
84         int             i;
85
86         if ((p = xdr_decode_string_inplace(p, namp, lenp, NFS_MAXPATHLEN)) != NULL) {
87                 for (i = 0, name = *namp; i < *lenp; i++, name++) {
88                         if (*name == '\0')
89                                 return NULL;
90                 }
91         }
92
93         return p;
94 }
95
96 static __be32 *
97 decode_sattr(__be32 *p, struct iattr *iap)
98 {
99         u32     tmp, tmp1;
100
101         iap->ia_valid = 0;
102
103         /* Sun client bug compatibility check: some sun clients seem to
104          * put 0xffff in the mode field when they mean 0xffffffff.
105          * Quoting the 4.4BSD nfs server code: Nah nah nah nah na nah.
106          */
107         if ((tmp = ntohl(*p++)) != (u32)-1 && tmp != 0xffff) {
108                 iap->ia_valid |= ATTR_MODE;
109                 iap->ia_mode = tmp;
110         }
111         if ((tmp = ntohl(*p++)) != (u32)-1) {
112                 iap->ia_valid |= ATTR_UID;
113                 iap->ia_uid = tmp;
114         }
115         if ((tmp = ntohl(*p++)) != (u32)-1) {
116                 iap->ia_valid |= ATTR_GID;
117                 iap->ia_gid = tmp;
118         }
119         if ((tmp = ntohl(*p++)) != (u32)-1) {
120                 iap->ia_valid |= ATTR_SIZE;
121                 iap->ia_size = tmp;
122         }
123         tmp  = ntohl(*p++); tmp1 = ntohl(*p++);
124         if (tmp != (u32)-1 && tmp1 != (u32)-1) {
125                 iap->ia_valid |= ATTR_ATIME | ATTR_ATIME_SET;
126                 iap->ia_atime.tv_sec = tmp;
127                 iap->ia_atime.tv_nsec = tmp1 * 1000; 
128         }
129         tmp  = ntohl(*p++); tmp1 = ntohl(*p++);
130         if (tmp != (u32)-1 && tmp1 != (u32)-1) {
131                 iap->ia_valid |= ATTR_MTIME | ATTR_MTIME_SET;
132                 iap->ia_mtime.tv_sec = tmp;
133                 iap->ia_mtime.tv_nsec = tmp1 * 1000; 
134                 /*
135                  * Passing the invalid value useconds=1000000 for mtime
136                  * is a Sun convention for "set both mtime and atime to
137                  * current server time".  It's needed to make permissions
138                  * checks for the "touch" program across v2 mounts to
139                  * Solaris and Irix boxes work correctly. See description of
140                  * sattr in section 6.1 of "NFS Illustrated" by
141                  * Brent Callaghan, Addison-Wesley, ISBN 0-201-32750-5
142                  */
143                 if (tmp1 == 1000000)
144                         iap->ia_valid &= ~(ATTR_ATIME_SET|ATTR_MTIME_SET);
145         }
146         return p;
147 }
148
149 static __be32 *
150 encode_fattr(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp,
151              struct kstat *stat)
152 {
153         struct dentry   *dentry = fhp->fh_dentry;
154         int type;
155         struct timespec time;
156
157         type = (stat->mode & S_IFMT);
158
159         *p++ = htonl(nfs_ftypes[type >> 12]);
160         *p++ = htonl((u32) stat->mode);
161         *p++ = htonl((u32) stat->nlink);
162         *p++ = htonl((u32) nfsd_ruid(rqstp, stat->uid));
163         *p++ = htonl((u32) nfsd_rgid(rqstp, stat->gid));
164
165         if (S_ISLNK(type) && stat->size > NFS_MAXPATHLEN) {
166                 *p++ = htonl(NFS_MAXPATHLEN);
167         } else {
168                 *p++ = htonl((u32) stat->size);
169         }
170         *p++ = htonl((u32) stat->blksize);
171         if (S_ISCHR(type) || S_ISBLK(type))
172                 *p++ = htonl(new_encode_dev(stat->rdev));
173         else
174                 *p++ = htonl(0xffffffff);
175         *p++ = htonl((u32) stat->blocks);
176         if (is_fsid(fhp, rqstp->rq_reffh))
177                 *p++ = htonl((u32) fhp->fh_export->ex_fsid);
178         else
179                 *p++ = htonl(new_encode_dev(stat->dev));
180         *p++ = htonl((u32) stat->ino);
181         *p++ = htonl((u32) stat->atime.tv_sec);
182         *p++ = htonl(stat->atime.tv_nsec ? stat->atime.tv_nsec / 1000 : 0);
183         lease_get_mtime(dentry->d_inode, &time); 
184         *p++ = htonl((u32) time.tv_sec);
185         *p++ = htonl(time.tv_nsec ? time.tv_nsec / 1000 : 0); 
186         *p++ = htonl((u32) stat->ctime.tv_sec);
187         *p++ = htonl(stat->ctime.tv_nsec ? stat->ctime.tv_nsec / 1000 : 0);
188
189         return p;
190 }
191
192 /* Helper function for NFSv2 ACL code */
193 __be32 *nfs2svc_encode_fattr(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp)
194 {
195         struct kstat stat;
196         vfs_getattr(fhp->fh_export->ex_mnt, fhp->fh_dentry, &stat);
197         return encode_fattr(rqstp, p, fhp, &stat);
198 }
199
200 /*
201  * XDR decode functions
202  */
203 int
204 nfssvc_decode_void(struct svc_rqst *rqstp, __be32 *p, void *dummy)
205 {
206         return xdr_argsize_check(rqstp, p);
207 }
208
209 int
210 nfssvc_decode_fhandle(struct svc_rqst *rqstp, __be32 *p, struct nfsd_fhandle *args)
211 {
212         if (!(p = decode_fh(p, &args->fh)))
213                 return 0;
214         return xdr_argsize_check(rqstp, p);
215 }
216
217 int
218 nfssvc_decode_sattrargs(struct svc_rqst *rqstp, __be32 *p,
219                                         struct nfsd_sattrargs *args)
220 {
221         if (!(p = decode_fh(p, &args->fh))
222          || !(p = decode_sattr(p, &args->attrs)))
223                 return 0;
224
225         return xdr_argsize_check(rqstp, p);
226 }
227
228 int
229 nfssvc_decode_diropargs(struct svc_rqst *rqstp, __be32 *p,
230                                         struct nfsd_diropargs *args)
231 {
232         if (!(p = decode_fh(p, &args->fh))
233          || !(p = decode_filename(p, &args->name, &args->len)))
234                 return 0;
235
236          return xdr_argsize_check(rqstp, p);
237 }
238
239 int
240 nfssvc_decode_readargs(struct svc_rqst *rqstp, __be32 *p,
241                                         struct nfsd_readargs *args)
242 {
243         unsigned int len;
244         int v,pn;
245         if (!(p = decode_fh(p, &args->fh)))
246                 return 0;
247
248         args->offset    = ntohl(*p++);
249         len = args->count     = ntohl(*p++);
250         p++; /* totalcount - unused */
251
252         if (len > NFSSVC_MAXBLKSIZE_V2)
253                 len = NFSSVC_MAXBLKSIZE_V2;
254
255         /* set up somewhere to store response.
256          * We take pages, put them on reslist and include in iovec
257          */
258         v=0;
259         while (len > 0) {
260                 pn = rqstp->rq_resused++;
261                 rqstp->rq_vec[v].iov_base = page_address(rqstp->rq_respages[pn]);
262                 rqstp->rq_vec[v].iov_len = len < PAGE_SIZE?len:PAGE_SIZE;
263                 len -= rqstp->rq_vec[v].iov_len;
264                 v++;
265         }
266         args->vlen = v;
267         return xdr_argsize_check(rqstp, p);
268 }
269
270 int
271 nfssvc_decode_writeargs(struct svc_rqst *rqstp, __be32 *p,
272                                         struct nfsd_writeargs *args)
273 {
274         unsigned int len;
275         int v;
276         if (!(p = decode_fh(p, &args->fh)))
277                 return 0;
278
279         p++;                            /* beginoffset */
280         args->offset = ntohl(*p++);     /* offset */
281         p++;                            /* totalcount */
282         len = args->len = ntohl(*p++);
283         rqstp->rq_vec[0].iov_base = (void*)p;
284         rqstp->rq_vec[0].iov_len = rqstp->rq_arg.head[0].iov_len -
285                                 (((void*)p) - rqstp->rq_arg.head[0].iov_base);
286         if (len > NFSSVC_MAXBLKSIZE_V2)
287                 len = NFSSVC_MAXBLKSIZE_V2;
288         v = 0;
289         while (len > rqstp->rq_vec[v].iov_len) {
290                 len -= rqstp->rq_vec[v].iov_len;
291                 v++;
292                 rqstp->rq_vec[v].iov_base = page_address(rqstp->rq_pages[v]);
293                 rqstp->rq_vec[v].iov_len = PAGE_SIZE;
294         }
295         rqstp->rq_vec[v].iov_len = len;
296         args->vlen = v+1;
297         return rqstp->rq_vec[0].iov_len > 0;
298 }
299
300 int
301 nfssvc_decode_createargs(struct svc_rqst *rqstp, __be32 *p,
302                                         struct nfsd_createargs *args)
303 {
304         if (!(p = decode_fh(p, &args->fh))
305          || !(p = decode_filename(p, &args->name, &args->len))
306          || !(p = decode_sattr(p, &args->attrs)))
307                 return 0;
308
309         return xdr_argsize_check(rqstp, p);
310 }
311
312 int
313 nfssvc_decode_renameargs(struct svc_rqst *rqstp, __be32 *p,
314                                         struct nfsd_renameargs *args)
315 {
316         if (!(p = decode_fh(p, &args->ffh))
317          || !(p = decode_filename(p, &args->fname, &args->flen))
318          || !(p = decode_fh(p, &args->tfh))
319          || !(p = decode_filename(p, &args->tname, &args->tlen)))
320                 return 0;
321
322         return xdr_argsize_check(rqstp, p);
323 }
324
325 int
326 nfssvc_decode_readlinkargs(struct svc_rqst *rqstp, __be32 *p, struct nfsd_readlinkargs *args)
327 {
328         if (!(p = decode_fh(p, &args->fh)))
329                 return 0;
330         args->buffer = page_address(rqstp->rq_respages[rqstp->rq_resused++]);
331
332         return xdr_argsize_check(rqstp, p);
333 }
334
335 int
336 nfssvc_decode_linkargs(struct svc_rqst *rqstp, __be32 *p,
337                                         struct nfsd_linkargs *args)
338 {
339         if (!(p = decode_fh(p, &args->ffh))
340          || !(p = decode_fh(p, &args->tfh))
341          || !(p = decode_filename(p, &args->tname, &args->tlen)))
342                 return 0;
343
344         return xdr_argsize_check(rqstp, p);
345 }
346
347 int
348 nfssvc_decode_symlinkargs(struct svc_rqst *rqstp, __be32 *p,
349                                         struct nfsd_symlinkargs *args)
350 {
351         if (!(p = decode_fh(p, &args->ffh))
352          || !(p = decode_filename(p, &args->fname, &args->flen))
353          || !(p = decode_pathname(p, &args->tname, &args->tlen))
354          || !(p = decode_sattr(p, &args->attrs)))
355                 return 0;
356
357         return xdr_argsize_check(rqstp, p);
358 }
359
360 int
361 nfssvc_decode_readdirargs(struct svc_rqst *rqstp, __be32 *p,
362                                         struct nfsd_readdirargs *args)
363 {
364         if (!(p = decode_fh(p, &args->fh)))
365                 return 0;
366         args->cookie = ntohl(*p++);
367         args->count  = ntohl(*p++);
368         if (args->count > PAGE_SIZE)
369                 args->count = PAGE_SIZE;
370
371         args->buffer = page_address(rqstp->rq_respages[rqstp->rq_resused++]);
372
373         return xdr_argsize_check(rqstp, p);
374 }
375
376 /*
377  * XDR encode functions
378  */
379 int
380 nfssvc_encode_void(struct svc_rqst *rqstp, __be32 *p, void *dummy)
381 {
382         return xdr_ressize_check(rqstp, p);
383 }
384
385 int
386 nfssvc_encode_attrstat(struct svc_rqst *rqstp, __be32 *p,
387                                         struct nfsd_attrstat *resp)
388 {
389         p = encode_fattr(rqstp, p, &resp->fh, &resp->stat);
390         return xdr_ressize_check(rqstp, p);
391 }
392
393 int
394 nfssvc_encode_diropres(struct svc_rqst *rqstp, __be32 *p,
395                                         struct nfsd_diropres *resp)
396 {
397         p = encode_fh(p, &resp->fh);
398         p = encode_fattr(rqstp, p, &resp->fh, &resp->stat);
399         return xdr_ressize_check(rqstp, p);
400 }
401
402 int
403 nfssvc_encode_readlinkres(struct svc_rqst *rqstp, __be32 *p,
404                                         struct nfsd_readlinkres *resp)
405 {
406         *p++ = htonl(resp->len);
407         xdr_ressize_check(rqstp, p);
408         rqstp->rq_res.page_len = resp->len;
409         if (resp->len & 3) {
410                 /* need to pad the tail */
411                 rqstp->rq_res.tail[0].iov_base = p;
412                 *p = 0;
413                 rqstp->rq_res.tail[0].iov_len = 4 - (resp->len&3);
414         }
415         return 1;
416 }
417
418 int
419 nfssvc_encode_readres(struct svc_rqst *rqstp, __be32 *p,
420                                         struct nfsd_readres *resp)
421 {
422         p = encode_fattr(rqstp, p, &resp->fh, &resp->stat);
423         *p++ = htonl(resp->count);
424         xdr_ressize_check(rqstp, p);
425
426         /* now update rqstp->rq_res to reflect data aswell */
427         rqstp->rq_res.page_len = resp->count;
428         if (resp->count & 3) {
429                 /* need to pad the tail */
430                 rqstp->rq_res.tail[0].iov_base = p;
431                 *p = 0;
432                 rqstp->rq_res.tail[0].iov_len = 4 - (resp->count&3);
433         }
434         return 1;
435 }
436
437 int
438 nfssvc_encode_readdirres(struct svc_rqst *rqstp, __be32 *p,
439                                         struct nfsd_readdirres *resp)
440 {
441         xdr_ressize_check(rqstp, p);
442         p = resp->buffer;
443         *p++ = 0;                       /* no more entries */
444         *p++ = htonl((resp->common.err == nfserr_eof));
445         rqstp->rq_res.page_len = (((unsigned long)p-1) & ~PAGE_MASK)+1;
446
447         return 1;
448 }
449
450 int
451 nfssvc_encode_statfsres(struct svc_rqst *rqstp, __be32 *p,
452                                         struct nfsd_statfsres *resp)
453 {
454         struct kstatfs  *stat = &resp->stats;
455
456         *p++ = htonl(NFSSVC_MAXBLKSIZE_V2);     /* max transfer size */
457         *p++ = htonl(stat->f_bsize);
458         *p++ = htonl(stat->f_blocks);
459         *p++ = htonl(stat->f_bfree);
460         *p++ = htonl(stat->f_bavail);
461         return xdr_ressize_check(rqstp, p);
462 }
463
464 int
465 nfssvc_encode_entry(void *ccdv, const char *name,
466                     int namlen, loff_t offset, u64 ino, unsigned int d_type)
467 {
468         struct readdir_cd *ccd = ccdv;
469         struct nfsd_readdirres *cd = container_of(ccd, struct nfsd_readdirres, common);
470         __be32  *p = cd->buffer;
471         int     buflen, slen;
472
473         /*
474         dprintk("nfsd: entry(%.*s off %ld ino %ld)\n",
475                         namlen, name, offset, ino);
476          */
477
478         if (offset > ~((u32) 0)) {
479                 cd->common.err = nfserr_fbig;
480                 return -EINVAL;
481         }
482         if (cd->offset)
483                 *cd->offset = htonl(offset);
484         if (namlen > NFS2_MAXNAMLEN)
485                 namlen = NFS2_MAXNAMLEN;/* truncate filename */
486
487         slen = XDR_QUADLEN(namlen);
488         if ((buflen = cd->buflen - slen - 4) < 0) {
489                 cd->common.err = nfserr_toosmall;
490                 return -EINVAL;
491         }
492         *p++ = xdr_one;                         /* mark entry present */
493         *p++ = htonl((u32) ino);                /* file id */
494         p    = xdr_encode_array(p, name, namlen);/* name length & name */
495         cd->offset = p;                 /* remember pointer */
496         *p++ = htonl(~0U);              /* offset of next entry */
497
498         cd->buflen = buflen;
499         cd->buffer = p;
500         cd->common.err = nfs_ok;
501         return 0;
502 }
503
504 /*
505  * XDR release functions
506  */
507 int
508 nfssvc_release_fhandle(struct svc_rqst *rqstp, __be32 *p,
509                                         struct nfsd_fhandle *resp)
510 {
511         fh_put(&resp->fh);
512         return 1;
513 }