2 * linux/fs/nfsd/nfsxdr.c
6 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
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>
18 #include <linux/vs_tag.h>
20 #define NFSDDBG_FACILITY NFSDDBG_XDR
23 * Mapping of S_IF* types to NFS file types
25 static u32 nfs_ftypes[] = {
26 NFNON, NFCHR, NFCHR, NFBAD,
27 NFDIR, NFBAD, NFBLK, NFBAD,
28 NFREG, NFBAD, NFLNK, NFBAD,
29 NFSOCK, NFBAD, NFLNK, NFBAD,
34 * XDR functions for basic NFS types
37 decode_fh(__be32 *p, struct svc_fh *fhp)
39 fh_init(fhp, NFS_FHSIZE);
40 memcpy(&fhp->fh_handle.fh_base, p, NFS_FHSIZE);
41 fhp->fh_handle.fh_size = NFS_FHSIZE;
43 /* FIXME: Look up export pointer here and verify
44 * Sun Secure RPC if requested */
45 return p + (NFS_FHSIZE >> 2);
48 /* Helper function for NFSv2 ACL code */
49 __be32 *nfs2svc_decode_fh(__be32 *p, struct svc_fh *fhp)
51 return decode_fh(p, fhp);
55 encode_fh(__be32 *p, struct svc_fh *fhp)
57 memcpy(p, &fhp->fh_handle.fh_base, NFS_FHSIZE);
58 return p + (NFS_FHSIZE>> 2);
62 * Decode a file name and make sure that the path contains
63 * no slashes or null bytes.
66 decode_filename(__be32 *p, char **namp, int *lenp)
71 if ((p = xdr_decode_string_inplace(p, namp, lenp, NFS_MAXNAMLEN)) != NULL) {
72 for (i = 0, name = *namp; i < *lenp; i++, name++) {
73 if (*name == '\0' || *name == '/')
82 decode_pathname(__be32 *p, char **namp, int *lenp)
87 if ((p = xdr_decode_string_inplace(p, namp, lenp, NFS_MAXPATHLEN)) != NULL) {
88 for (i = 0, name = *namp; i < *lenp; i++, name++) {
98 decode_sattr(__be32 *p, struct iattr *iap)
106 /* Sun client bug compatibility check: some sun clients seem to
107 * put 0xffff in the mode field when they mean 0xffffffff.
108 * Quoting the 4.4BSD nfs server code: Nah nah nah nah na nah.
110 if ((tmp = ntohl(*p++)) != (u32)-1 && tmp != 0xffff) {
111 iap->ia_valid |= ATTR_MODE;
114 if ((tmp = ntohl(*p++)) != (u32)-1) {
115 iap->ia_valid |= ATTR_UID;
118 if ((tmp = ntohl(*p++)) != (u32)-1) {
119 iap->ia_valid |= ATTR_GID;
122 iap->ia_uid = INOTAG_UID(DX_TAG_NFSD, uid, gid);
123 iap->ia_gid = INOTAG_GID(DX_TAG_NFSD, uid, gid);
124 iap->ia_tag = INOTAG_TAG(DX_TAG_NFSD, uid, gid, 0);
125 if ((tmp = ntohl(*p++)) != (u32)-1) {
126 iap->ia_valid |= ATTR_SIZE;
129 tmp = ntohl(*p++); tmp1 = ntohl(*p++);
130 if (tmp != (u32)-1 && tmp1 != (u32)-1) {
131 iap->ia_valid |= ATTR_ATIME | ATTR_ATIME_SET;
132 iap->ia_atime.tv_sec = tmp;
133 iap->ia_atime.tv_nsec = tmp1 * 1000;
135 tmp = ntohl(*p++); tmp1 = ntohl(*p++);
136 if (tmp != (u32)-1 && tmp1 != (u32)-1) {
137 iap->ia_valid |= ATTR_MTIME | ATTR_MTIME_SET;
138 iap->ia_mtime.tv_sec = tmp;
139 iap->ia_mtime.tv_nsec = tmp1 * 1000;
141 * Passing the invalid value useconds=1000000 for mtime
142 * is a Sun convention for "set both mtime and atime to
143 * current server time". It's needed to make permissions
144 * checks for the "touch" program across v2 mounts to
145 * Solaris and Irix boxes work correctly. See description of
146 * sattr in section 6.1 of "NFS Illustrated" by
147 * Brent Callaghan, Addison-Wesley, ISBN 0-201-32750-5
150 iap->ia_valid &= ~(ATTR_ATIME_SET|ATTR_MTIME_SET);
156 encode_fattr(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp,
159 struct dentry *dentry = fhp->fh_dentry;
161 struct timespec time;
163 type = (stat->mode & S_IFMT);
165 *p++ = htonl(nfs_ftypes[type >> 12]);
166 *p++ = htonl((u32) stat->mode);
167 *p++ = htonl((u32) stat->nlink);
168 *p++ = htonl((u32) nfsd_ruid(rqstp,
169 TAGINO_UID(DX_TAG(dentry->d_inode), stat->uid, stat->tag)));
170 *p++ = htonl((u32) nfsd_rgid(rqstp,
171 TAGINO_GID(DX_TAG(dentry->d_inode), stat->gid, stat->tag)));
173 if (S_ISLNK(type) && stat->size > NFS_MAXPATHLEN) {
174 *p++ = htonl(NFS_MAXPATHLEN);
176 *p++ = htonl((u32) stat->size);
178 *p++ = htonl((u32) stat->blksize);
179 if (S_ISCHR(type) || S_ISBLK(type))
180 *p++ = htonl(new_encode_dev(stat->rdev));
182 *p++ = htonl(0xffffffff);
183 *p++ = htonl((u32) stat->blocks);
184 if (is_fsid(fhp, rqstp->rq_reffh))
185 *p++ = htonl((u32) fhp->fh_export->ex_fsid);
187 *p++ = htonl(new_encode_dev(stat->dev));
188 *p++ = htonl((u32) stat->ino);
189 *p++ = htonl((u32) stat->atime.tv_sec);
190 *p++ = htonl(stat->atime.tv_nsec ? stat->atime.tv_nsec / 1000 : 0);
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);
200 /* Helper function for NFSv2 ACL code */
201 __be32 *nfs2svc_encode_fattr(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp)
204 vfs_getattr(fhp->fh_export->ex_mnt, fhp->fh_dentry, &stat);
205 return encode_fattr(rqstp, p, fhp, &stat);
209 * XDR decode functions
212 nfssvc_decode_void(struct svc_rqst *rqstp, __be32 *p, void *dummy)
214 return xdr_argsize_check(rqstp, p);
218 nfssvc_decode_fhandle(struct svc_rqst *rqstp, __be32 *p, struct nfsd_fhandle *args)
220 if (!(p = decode_fh(p, &args->fh)))
222 return xdr_argsize_check(rqstp, p);
226 nfssvc_decode_sattrargs(struct svc_rqst *rqstp, __be32 *p,
227 struct nfsd_sattrargs *args)
229 if (!(p = decode_fh(p, &args->fh))
230 || !(p = decode_sattr(p, &args->attrs)))
233 return xdr_argsize_check(rqstp, p);
237 nfssvc_decode_diropargs(struct svc_rqst *rqstp, __be32 *p,
238 struct nfsd_diropargs *args)
240 if (!(p = decode_fh(p, &args->fh))
241 || !(p = decode_filename(p, &args->name, &args->len)))
244 return xdr_argsize_check(rqstp, p);
248 nfssvc_decode_readargs(struct svc_rqst *rqstp, __be32 *p,
249 struct nfsd_readargs *args)
253 if (!(p = decode_fh(p, &args->fh)))
256 args->offset = ntohl(*p++);
257 len = args->count = ntohl(*p++);
258 p++; /* totalcount - unused */
260 if (len > NFSSVC_MAXBLKSIZE_V2)
261 len = NFSSVC_MAXBLKSIZE_V2;
263 /* set up somewhere to store response.
264 * We take pages, put them on reslist and include in iovec
268 pn = rqstp->rq_resused++;
269 rqstp->rq_vec[v].iov_base = page_address(rqstp->rq_respages[pn]);
270 rqstp->rq_vec[v].iov_len = len < PAGE_SIZE?len:PAGE_SIZE;
271 len -= rqstp->rq_vec[v].iov_len;
275 return xdr_argsize_check(rqstp, p);
279 nfssvc_decode_writeargs(struct svc_rqst *rqstp, __be32 *p,
280 struct nfsd_writeargs *args)
284 if (!(p = decode_fh(p, &args->fh)))
287 p++; /* beginoffset */
288 args->offset = ntohl(*p++); /* offset */
289 p++; /* totalcount */
290 len = args->len = ntohl(*p++);
291 rqstp->rq_vec[0].iov_base = (void*)p;
292 rqstp->rq_vec[0].iov_len = rqstp->rq_arg.head[0].iov_len -
293 (((void*)p) - rqstp->rq_arg.head[0].iov_base);
294 if (len > NFSSVC_MAXBLKSIZE_V2)
295 len = NFSSVC_MAXBLKSIZE_V2;
297 while (len > rqstp->rq_vec[v].iov_len) {
298 len -= rqstp->rq_vec[v].iov_len;
300 rqstp->rq_vec[v].iov_base = page_address(rqstp->rq_pages[v]);
301 rqstp->rq_vec[v].iov_len = PAGE_SIZE;
303 rqstp->rq_vec[v].iov_len = len;
305 return rqstp->rq_vec[0].iov_len > 0;
309 nfssvc_decode_createargs(struct svc_rqst *rqstp, __be32 *p,
310 struct nfsd_createargs *args)
312 if (!(p = decode_fh(p, &args->fh))
313 || !(p = decode_filename(p, &args->name, &args->len))
314 || !(p = decode_sattr(p, &args->attrs)))
317 return xdr_argsize_check(rqstp, p);
321 nfssvc_decode_renameargs(struct svc_rqst *rqstp, __be32 *p,
322 struct nfsd_renameargs *args)
324 if (!(p = decode_fh(p, &args->ffh))
325 || !(p = decode_filename(p, &args->fname, &args->flen))
326 || !(p = decode_fh(p, &args->tfh))
327 || !(p = decode_filename(p, &args->tname, &args->tlen)))
330 return xdr_argsize_check(rqstp, p);
334 nfssvc_decode_readlinkargs(struct svc_rqst *rqstp, __be32 *p, struct nfsd_readlinkargs *args)
336 if (!(p = decode_fh(p, &args->fh)))
338 args->buffer = page_address(rqstp->rq_respages[rqstp->rq_resused++]);
340 return xdr_argsize_check(rqstp, p);
344 nfssvc_decode_linkargs(struct svc_rqst *rqstp, __be32 *p,
345 struct nfsd_linkargs *args)
347 if (!(p = decode_fh(p, &args->ffh))
348 || !(p = decode_fh(p, &args->tfh))
349 || !(p = decode_filename(p, &args->tname, &args->tlen)))
352 return xdr_argsize_check(rqstp, p);
356 nfssvc_decode_symlinkargs(struct svc_rqst *rqstp, __be32 *p,
357 struct nfsd_symlinkargs *args)
359 if (!(p = decode_fh(p, &args->ffh))
360 || !(p = decode_filename(p, &args->fname, &args->flen))
361 || !(p = decode_pathname(p, &args->tname, &args->tlen))
362 || !(p = decode_sattr(p, &args->attrs)))
365 return xdr_argsize_check(rqstp, p);
369 nfssvc_decode_readdirargs(struct svc_rqst *rqstp, __be32 *p,
370 struct nfsd_readdirargs *args)
372 if (!(p = decode_fh(p, &args->fh)))
374 args->cookie = ntohl(*p++);
375 args->count = ntohl(*p++);
376 if (args->count > PAGE_SIZE)
377 args->count = PAGE_SIZE;
379 args->buffer = page_address(rqstp->rq_respages[rqstp->rq_resused++]);
381 return xdr_argsize_check(rqstp, p);
385 * XDR encode functions
388 nfssvc_encode_void(struct svc_rqst *rqstp, __be32 *p, void *dummy)
390 return xdr_ressize_check(rqstp, p);
394 nfssvc_encode_attrstat(struct svc_rqst *rqstp, __be32 *p,
395 struct nfsd_attrstat *resp)
397 p = encode_fattr(rqstp, p, &resp->fh, &resp->stat);
398 return xdr_ressize_check(rqstp, p);
402 nfssvc_encode_diropres(struct svc_rqst *rqstp, __be32 *p,
403 struct nfsd_diropres *resp)
405 p = encode_fh(p, &resp->fh);
406 p = encode_fattr(rqstp, p, &resp->fh, &resp->stat);
407 return xdr_ressize_check(rqstp, p);
411 nfssvc_encode_readlinkres(struct svc_rqst *rqstp, __be32 *p,
412 struct nfsd_readlinkres *resp)
414 *p++ = htonl(resp->len);
415 xdr_ressize_check(rqstp, p);
416 rqstp->rq_res.page_len = resp->len;
418 /* need to pad the tail */
419 rqstp->rq_res.tail[0].iov_base = p;
421 rqstp->rq_res.tail[0].iov_len = 4 - (resp->len&3);
427 nfssvc_encode_readres(struct svc_rqst *rqstp, __be32 *p,
428 struct nfsd_readres *resp)
430 p = encode_fattr(rqstp, p, &resp->fh, &resp->stat);
431 *p++ = htonl(resp->count);
432 xdr_ressize_check(rqstp, p);
434 /* now update rqstp->rq_res to reflect data aswell */
435 rqstp->rq_res.page_len = resp->count;
436 if (resp->count & 3) {
437 /* need to pad the tail */
438 rqstp->rq_res.tail[0].iov_base = p;
440 rqstp->rq_res.tail[0].iov_len = 4 - (resp->count&3);
446 nfssvc_encode_readdirres(struct svc_rqst *rqstp, __be32 *p,
447 struct nfsd_readdirres *resp)
449 xdr_ressize_check(rqstp, p);
451 *p++ = 0; /* no more entries */
452 *p++ = htonl((resp->common.err == nfserr_eof));
453 rqstp->rq_res.page_len = (((unsigned long)p-1) & ~PAGE_MASK)+1;
459 nfssvc_encode_statfsres(struct svc_rqst *rqstp, __be32 *p,
460 struct nfsd_statfsres *resp)
462 struct kstatfs *stat = &resp->stats;
464 *p++ = htonl(NFSSVC_MAXBLKSIZE_V2); /* max transfer size */
465 *p++ = htonl(stat->f_bsize);
466 *p++ = htonl(stat->f_blocks);
467 *p++ = htonl(stat->f_bfree);
468 *p++ = htonl(stat->f_bavail);
469 return xdr_ressize_check(rqstp, p);
473 nfssvc_encode_entry(void *ccdv, const char *name,
474 int namlen, loff_t offset, u64 ino, unsigned int d_type)
476 struct readdir_cd *ccd = ccdv;
477 struct nfsd_readdirres *cd = container_of(ccd, struct nfsd_readdirres, common);
478 __be32 *p = cd->buffer;
482 dprintk("nfsd: entry(%.*s off %ld ino %ld)\n",
483 namlen, name, offset, ino);
486 if (offset > ~((u32) 0)) {
487 cd->common.err = nfserr_fbig;
491 *cd->offset = htonl(offset);
492 if (namlen > NFS2_MAXNAMLEN)
493 namlen = NFS2_MAXNAMLEN;/* truncate filename */
495 slen = XDR_QUADLEN(namlen);
496 if ((buflen = cd->buflen - slen - 4) < 0) {
497 cd->common.err = nfserr_toosmall;
500 *p++ = xdr_one; /* mark entry present */
501 *p++ = htonl((u32) ino); /* file id */
502 p = xdr_encode_array(p, name, namlen);/* name length & name */
503 cd->offset = p; /* remember pointer */
504 *p++ = htonl(~0U); /* offset of next entry */
508 cd->common.err = nfs_ok;
513 * XDR release functions
516 nfssvc_release_fhandle(struct svc_rqst *rqstp, __be32 *p,
517 struct nfsd_fhandle *resp)