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