patch-2_6_7-vs1_9_1_12
[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
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 inline u32 *
150 encode_fattr(struct svc_rqst *rqstp, u32 *p, struct svc_fh *fhp)
151 {
152         struct vfsmount *mnt = fhp->fh_export->ex_mnt;
153         struct dentry   *dentry = fhp->fh_dentry;
154         struct kstat stat;
155         int type;
156         struct timespec time;
157
158         vfs_getattr(mnt, dentry, &stat);
159         type = (stat.mode & S_IFMT);
160
161         *p++ = htonl(nfs_ftypes[type >> 12]);
162         *p++ = htonl((u32) stat.mode);
163         *p++ = htonl((u32) stat.nlink);
164         *p++ = htonl((u32) nfsd_ruid(rqstp,
165                 XIDINO_UID(stat.uid, stat.xid)));
166         *p++ = htonl((u32) nfsd_rgid(rqstp,
167                 XIDINO_GID(stat.gid, stat.xid)));
168
169         if (S_ISLNK(type) && stat.size > NFS_MAXPATHLEN) {
170                 *p++ = htonl(NFS_MAXPATHLEN);
171         } else {
172                 *p++ = htonl((u32) stat.size);
173         }
174         *p++ = htonl((u32) stat.blksize);
175         if (S_ISCHR(type) || S_ISBLK(type))
176                 *p++ = htonl(new_encode_dev(stat.rdev));
177         else
178                 *p++ = htonl(0xffffffff);
179         *p++ = htonl((u32) stat.blocks);
180         if (is_fsid(fhp, rqstp->rq_reffh))
181                 *p++ = htonl((u32) fhp->fh_export->ex_fsid);
182         else
183                 *p++ = htonl(new_encode_dev(stat.dev));
184         *p++ = htonl((u32) stat.ino);
185         *p++ = htonl((u32) stat.atime.tv_sec);
186         *p++ = htonl(stat.atime.tv_nsec ? stat.atime.tv_nsec / 1000 : 0);
187         lease_get_mtime(dentry->d_inode, &time); 
188         *p++ = htonl((u32) time.tv_sec);
189         *p++ = htonl(time.tv_nsec ? time.tv_nsec / 1000 : 0); 
190         *p++ = htonl((u32) stat.ctime.tv_sec);
191         *p++ = htonl(stat.ctime.tv_nsec ? stat.ctime.tv_nsec / 1000 : 0);
192
193         return p;
194 }
195
196
197 /*
198  * XDR decode functions
199  */
200 int
201 nfssvc_decode_void(struct svc_rqst *rqstp, u32 *p, void *dummy)
202 {
203         return xdr_argsize_check(rqstp, p);
204 }
205
206 int
207 nfssvc_decode_fhandle(struct svc_rqst *rqstp, u32 *p, struct nfsd_fhandle *args)
208 {
209         if (!(p = decode_fh(p, &args->fh)))
210                 return 0;
211         return xdr_argsize_check(rqstp, p);
212 }
213
214 int
215 nfssvc_decode_sattrargs(struct svc_rqst *rqstp, u32 *p,
216                                         struct nfsd_sattrargs *args)
217 {
218         if (!(p = decode_fh(p, &args->fh))
219          || !(p = decode_sattr(p, &args->attrs)))
220                 return 0;
221
222         return xdr_argsize_check(rqstp, p);
223 }
224
225 int
226 nfssvc_decode_diropargs(struct svc_rqst *rqstp, u32 *p,
227                                         struct nfsd_diropargs *args)
228 {
229         if (!(p = decode_fh(p, &args->fh))
230          || !(p = decode_filename(p, &args->name, &args->len)))
231                 return 0;
232
233          return xdr_argsize_check(rqstp, p);
234 }
235
236 int
237 nfssvc_decode_readargs(struct svc_rqst *rqstp, u32 *p,
238                                         struct nfsd_readargs *args)
239 {
240         int len;
241         int v,pn;
242         if (!(p = decode_fh(p, &args->fh)))
243                 return 0;
244
245         args->offset    = ntohl(*p++);
246         len = args->count     = ntohl(*p++);
247         p++; /* totalcount - unused */
248
249         if (len > NFSSVC_MAXBLKSIZE)
250                 len = NFSSVC_MAXBLKSIZE;
251
252         /* set up somewhere to store response.
253          * We take pages, put them on reslist and include in iovec
254          */
255         v=0;
256         while (len > 0) {
257                 pn=rqstp->rq_resused;
258                 svc_take_page(rqstp);
259                 args->vec[v].iov_base = page_address(rqstp->rq_respages[pn]);
260                 args->vec[v].iov_len = len < PAGE_SIZE?len:PAGE_SIZE;
261                 v++;
262                 len -= PAGE_SIZE;
263         }
264         args->vlen = v;
265         return xdr_argsize_check(rqstp, p);
266 }
267
268 int
269 nfssvc_decode_writeargs(struct svc_rqst *rqstp, u32 *p,
270                                         struct nfsd_writeargs *args)
271 {
272         int len;
273         int v;
274         if (!(p = decode_fh(p, &args->fh)))
275                 return 0;
276
277         p++;                            /* beginoffset */
278         args->offset = ntohl(*p++);     /* offset */
279         p++;                            /* totalcount */
280         len = args->len = ntohl(*p++);
281         args->vec[0].iov_base = (void*)p;
282         args->vec[0].iov_len = rqstp->rq_arg.head[0].iov_len -
283                                 (((void*)p) - rqstp->rq_arg.head[0].iov_base);
284         if (len > NFSSVC_MAXBLKSIZE)
285                 len = NFSSVC_MAXBLKSIZE;
286         v = 0;
287         while (len > args->vec[v].iov_len) {
288                 len -= args->vec[v].iov_len;
289                 v++;
290                 args->vec[v].iov_base = page_address(rqstp->rq_argpages[v]);
291                 args->vec[v].iov_len = PAGE_SIZE;
292         }
293         args->vec[v].iov_len = len;
294         args->vlen = v+1;
295         return args->vec[0].iov_len > 0;
296 }
297
298 int
299 nfssvc_decode_createargs(struct svc_rqst *rqstp, u32 *p,
300                                         struct nfsd_createargs *args)
301 {
302         if (!(p = decode_fh(p, &args->fh))
303          || !(p = decode_filename(p, &args->name, &args->len))
304          || !(p = decode_sattr(p, &args->attrs)))
305                 return 0;
306
307         return xdr_argsize_check(rqstp, p);
308 }
309
310 int
311 nfssvc_decode_renameargs(struct svc_rqst *rqstp, u32 *p,
312                                         struct nfsd_renameargs *args)
313 {
314         if (!(p = decode_fh(p, &args->ffh))
315          || !(p = decode_filename(p, &args->fname, &args->flen))
316          || !(p = decode_fh(p, &args->tfh))
317          || !(p = decode_filename(p, &args->tname, &args->tlen)))
318                 return 0;
319
320         return xdr_argsize_check(rqstp, p);
321 }
322
323 int
324 nfssvc_decode_readlinkargs(struct svc_rqst *rqstp, u32 *p, struct nfsd_readlinkargs *args)
325 {
326         if (!(p = decode_fh(p, &args->fh)))
327                 return 0;
328         svc_take_page(rqstp);
329         args->buffer = page_address(rqstp->rq_respages[rqstp->rq_resused-1]);
330
331         return xdr_argsize_check(rqstp, p);
332 }
333
334 int
335 nfssvc_decode_linkargs(struct svc_rqst *rqstp, u32 *p,
336                                         struct nfsd_linkargs *args)
337 {
338         if (!(p = decode_fh(p, &args->ffh))
339          || !(p = decode_fh(p, &args->tfh))
340          || !(p = decode_filename(p, &args->tname, &args->tlen)))
341                 return 0;
342
343         return xdr_argsize_check(rqstp, p);
344 }
345
346 int
347 nfssvc_decode_symlinkargs(struct svc_rqst *rqstp, u32 *p,
348                                         struct nfsd_symlinkargs *args)
349 {
350         if (!(p = decode_fh(p, &args->ffh))
351          || !(p = decode_filename(p, &args->fname, &args->flen))
352          || !(p = decode_pathname(p, &args->tname, &args->tlen))
353          || !(p = decode_sattr(p, &args->attrs)))
354                 return 0;
355
356         return xdr_argsize_check(rqstp, p);
357 }
358
359 int
360 nfssvc_decode_readdirargs(struct svc_rqst *rqstp, u32 *p,
361                                         struct nfsd_readdirargs *args)
362 {
363         if (!(p = decode_fh(p, &args->fh)))
364                 return 0;
365         args->cookie = ntohl(*p++);
366         args->count  = ntohl(*p++);
367         if (args->count > PAGE_SIZE)
368                 args->count = PAGE_SIZE;
369
370         svc_take_page(rqstp);
371         args->buffer = page_address(rqstp->rq_respages[rqstp->rq_resused-1]);
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, u32 *p, void *dummy)
381 {
382         return xdr_ressize_check(rqstp, p);
383 }
384
385 int
386 nfssvc_encode_attrstat(struct svc_rqst *rqstp, u32 *p,
387                                         struct nfsd_attrstat *resp)
388 {
389         p = encode_fattr(rqstp, p, &resp->fh);
390         return xdr_ressize_check(rqstp, p);
391 }
392
393 int
394 nfssvc_encode_diropres(struct svc_rqst *rqstp, u32 *p,
395                                         struct nfsd_diropres *resp)
396 {
397         p = encode_fh(p, &resp->fh);
398         p = encode_fattr(rqstp, p, &resp->fh);
399         return xdr_ressize_check(rqstp, p);
400 }
401
402 int
403 nfssvc_encode_readlinkres(struct svc_rqst *rqstp, u32 *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_restailpage = 0;
412                 rqstp->rq_res.tail[0].iov_base = p;
413                 *p = 0;
414                 rqstp->rq_res.tail[0].iov_len = 4 - (resp->len&3);
415         }
416         return 1;
417 }
418
419 int
420 nfssvc_encode_readres(struct svc_rqst *rqstp, u32 *p,
421                                         struct nfsd_readres *resp)
422 {
423         p = encode_fattr(rqstp, p, &resp->fh);
424         *p++ = htonl(resp->count);
425         xdr_ressize_check(rqstp, p);
426
427         /* now update rqstp->rq_res to reflect data aswell */
428         rqstp->rq_res.page_len = resp->count;
429         if (resp->count & 3) {
430                 /* need to pad the tail */
431                 rqstp->rq_restailpage = 0;
432                 rqstp->rq_res.tail[0].iov_base = p;
433                 *p = 0;
434                 rqstp->rq_res.tail[0].iov_len = 4 - (resp->count&3);
435         }
436         return 1;
437 }
438
439 int
440 nfssvc_encode_readdirres(struct svc_rqst *rqstp, u32 *p,
441                                         struct nfsd_readdirres *resp)
442 {
443         xdr_ressize_check(rqstp, p);
444         p = resp->buffer;
445         *p++ = 0;                       /* no more entries */
446         *p++ = htonl((resp->common.err == nfserr_eof));
447         rqstp->rq_res.page_len = (((unsigned long)p-1) & ~PAGE_MASK)+1;
448
449         return 1;
450 }
451
452 int
453 nfssvc_encode_statfsres(struct svc_rqst *rqstp, u32 *p,
454                                         struct nfsd_statfsres *resp)
455 {
456         struct kstatfs  *stat = &resp->stats;
457
458         *p++ = htonl(NFSSVC_MAXBLKSIZE);        /* max transfer size */
459         *p++ = htonl(stat->f_bsize);
460         *p++ = htonl(stat->f_blocks);
461         *p++ = htonl(stat->f_bfree);
462         *p++ = htonl(stat->f_bavail);
463         return xdr_ressize_check(rqstp, p);
464 }
465
466 int
467 nfssvc_encode_entry(struct readdir_cd *ccd, const char *name,
468                     int namlen, loff_t offset, ino_t ino, unsigned int d_type)
469 {
470         struct nfsd_readdirres *cd = container_of(ccd, struct nfsd_readdirres, common);
471         u32     *p = cd->buffer;
472         int     buflen, slen;
473
474         /*
475         dprintk("nfsd: entry(%.*s off %ld ino %ld)\n",
476                         namlen, name, offset, ino);
477          */
478
479         if (offset > ~((u32) 0)) {
480                 cd->common.err = nfserr_fbig;
481                 return -EINVAL;
482         }
483         if (cd->offset)
484                 *cd->offset = htonl(offset);
485         if (namlen > NFS2_MAXNAMLEN)
486                 namlen = NFS2_MAXNAMLEN;/* truncate filename */
487
488         slen = XDR_QUADLEN(namlen);
489         if ((buflen = cd->buflen - slen - 4) < 0) {
490                 cd->common.err = nfserr_toosmall;
491                 return -EINVAL;
492         }
493         *p++ = xdr_one;                         /* mark entry present */
494         *p++ = htonl((u32) ino);                /* file id */
495         p    = xdr_encode_array(p, name, namlen);/* name length & name */
496         cd->offset = p;                 /* remember pointer */
497         *p++ = ~(u32) 0;                /* offset of next entry */
498
499         cd->buflen = buflen;
500         cd->buffer = p;
501         cd->common.err = nfs_ok;
502         return 0;
503 }
504
505 /*
506  * XDR release functions
507  */
508 int
509 nfssvc_release_fhandle(struct svc_rqst *rqstp, u32 *p,
510                                         struct nfsd_fhandle *resp)
511 {
512         fh_put(&resp->fh);
513         return 1;
514 }