6 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
9 #include <linux/types.h>
10 #include <linux/sched.h>
11 #include <linux/nfs.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 #define NFSDDBG_FACILITY NFSDDBG_XDR
21 #ifdef NFSD_OPTIMIZE_SPACE
26 * Mapping of S_IF* types to NFS file types
28 static u32 nfs_ftypes[] = {
29 NFNON, NFCHR, NFCHR, NFBAD,
30 NFDIR, NFBAD, NFBLK, NFBAD,
31 NFREG, NFBAD, NFLNK, NFBAD,
32 NFSOCK, NFBAD, NFLNK, NFBAD,
37 * XDR functions for basic NFS types
40 decode_fh(u32 *p, struct svc_fh *fhp)
42 fh_init(fhp, NFS_FHSIZE);
43 memcpy(&fhp->fh_handle.fh_base, p, NFS_FHSIZE);
44 fhp->fh_handle.fh_size = NFS_FHSIZE;
46 /* FIXME: Look up export pointer here and verify
47 * Sun Secure RPC if requested */
48 return p + (NFS_FHSIZE >> 2);
52 encode_fh(u32 *p, struct svc_fh *fhp)
54 memcpy(p, &fhp->fh_handle.fh_base, NFS_FHSIZE);
55 return p + (NFS_FHSIZE>> 2);
59 * Decode a file name and make sure that the path contains
60 * no slashes or null bytes.
63 decode_filename(u32 *p, char **namp, int *lenp)
68 if ((p = xdr_decode_string_inplace(p, namp, lenp, NFS_MAXNAMLEN)) != NULL) {
69 for (i = 0, name = *namp; i < *lenp; i++, name++) {
70 if (*name == '\0' || *name == '/')
79 decode_pathname(u32 *p, char **namp, int *lenp)
84 if ((p = xdr_decode_string(p, namp, lenp, NFS_MAXPATHLEN)) != NULL) {
85 for (i = 0, name = *namp; i < *lenp; i++, name++) {
95 decode_sattr(u32 *p, struct iattr *iap)
101 /* Sun client bug compatibility check: some sun clients seem to
102 * put 0xffff in the mode field when they mean 0xffffffff.
103 * Quoting the 4.4BSD nfs server code: Nah nah nah nah na nah.
105 if ((tmp = ntohl(*p++)) != (u32)-1 && tmp != 0xffff) {
106 iap->ia_valid |= ATTR_MODE;
109 if ((tmp = ntohl(*p++)) != (u32)-1) {
110 iap->ia_valid |= ATTR_UID;
113 if ((tmp = ntohl(*p++)) != (u32)-1) {
114 iap->ia_valid |= ATTR_GID;
117 if ((tmp = ntohl(*p++)) != (u32)-1) {
118 iap->ia_valid |= ATTR_SIZE;
121 tmp = ntohl(*p++); tmp1 = ntohl(*p++);
122 if (tmp != (u32)-1 && tmp1 != (u32)-1) {
123 iap->ia_valid |= ATTR_ATIME | ATTR_ATIME_SET;
126 tmp = ntohl(*p++); tmp1 = ntohl(*p++);
127 if (tmp != (u32)-1 && tmp1 != (u32)-1) {
128 iap->ia_valid |= ATTR_MTIME | ATTR_MTIME_SET;
131 * Passing the invalid value useconds=1000000 for mtime
132 * is a Sun convention for "set both mtime and atime to
133 * current server time". It's needed to make permissions
134 * checks for the "touch" program across v2 mounts to
135 * Solaris and Irix boxes work correctly. See description of
136 * sattr in section 6.1 of "NFS Illustrated" by
137 * Brent Callaghan, Addison-Wesley, ISBN 0-201-32750-5
140 iap->ia_valid &= ~(ATTR_ATIME_SET|ATTR_MTIME_SET);
146 encode_fattr(struct svc_rqst *rqstp, u32 *p, struct svc_fh *fhp)
148 struct inode *inode = fhp->fh_dentry->d_inode;
149 int type = (inode->i_mode & S_IFMT);
151 *p++ = htonl(nfs_ftypes[type >> 12]);
152 *p++ = htonl((u32) inode->i_mode);
153 *p++ = htonl((u32) inode->i_nlink);
154 *p++ = htonl((u32) nfsd_ruid(rqstp, inode->i_uid));
155 *p++ = htonl((u32) nfsd_rgid(rqstp, inode->i_gid));
157 if (S_ISLNK(type) && inode->i_size > NFS_MAXPATHLEN) {
158 *p++ = htonl(NFS_MAXPATHLEN);
160 *p++ = htonl((u32) inode->i_size);
162 *p++ = htonl((u32) inode->i_blksize);
163 if (S_ISCHR(type) || S_ISBLK(type))
164 *p++ = htonl((u32) inode->i_rdev);
166 *p++ = htonl(0xffffffff);
167 *p++ = htonl((u32) inode->i_blocks);
168 if (rqstp->rq_reffh->fh_version == 1
169 && rqstp->rq_reffh->fh_fsid_type == 1
170 && (fhp->fh_export->ex_flags & NFSEXP_FSID))
171 *p++ = htonl((u32) fhp->fh_export->ex_fsid);
173 *p++ = htonl((u32) inode->i_dev);
174 *p++ = htonl((u32) inode->i_ino);
175 *p++ = htonl((u32) inode->i_atime);
177 *p++ = htonl((u32) lease_get_mtime(inode));
179 *p++ = htonl((u32) inode->i_ctime);
186 * Check buffer bounds after decoding arguments
189 xdr_argsize_check(struct svc_rqst *rqstp, u32 *p)
191 struct svc_buf *buf = &rqstp->rq_argbuf;
193 return p - buf->base <= buf->buflen;
197 xdr_ressize_check(struct svc_rqst *rqstp, u32 *p)
199 struct svc_buf *buf = &rqstp->rq_resbuf;
201 buf->len = p - buf->base;
202 dprintk("nfsd: ressize_check p %p base %p len %d\n",
203 p, buf->base, buf->buflen);
204 return (buf->len <= buf->buflen);
208 * XDR decode functions
211 nfssvc_decode_void(struct svc_rqst *rqstp, u32 *p, void *dummy)
213 return xdr_argsize_check(rqstp, p);
217 nfssvc_decode_fhandle(struct svc_rqst *rqstp, u32 *p, struct svc_fh *fhp)
219 if (!(p = decode_fh(p, fhp)))
221 return xdr_argsize_check(rqstp, p);
225 nfssvc_decode_sattrargs(struct svc_rqst *rqstp, u32 *p,
226 struct nfsd_sattrargs *args)
228 if (!(p = decode_fh(p, &args->fh))
229 || !(p = decode_sattr(p, &args->attrs)))
232 return xdr_argsize_check(rqstp, p);
236 nfssvc_decode_diropargs(struct svc_rqst *rqstp, u32 *p,
237 struct nfsd_diropargs *args)
239 if (!(p = decode_fh(p, &args->fh))
240 || !(p = decode_filename(p, &args->name, &args->len)))
243 return xdr_argsize_check(rqstp, p);
247 nfssvc_decode_readargs(struct svc_rqst *rqstp, u32 *p,
248 struct nfsd_readargs *args)
250 if (!(p = decode_fh(p, &args->fh)))
253 args->offset = ntohl(*p++);
254 args->count = ntohl(*p++);
255 args->totalsize = ntohl(*p++);
257 return xdr_argsize_check(rqstp, p);
261 nfssvc_decode_writeargs(struct svc_rqst *rqstp, u32 *p,
262 struct nfsd_writeargs *args)
264 if (!(p = decode_fh(p, &args->fh)))
267 p++; /* beginoffset */
268 args->offset = ntohl(*p++); /* offset */
269 p++; /* totalcount */
270 args->len = ntohl(*p++);
271 args->data = (char *) p;
272 p += XDR_QUADLEN(args->len);
274 return xdr_argsize_check(rqstp, p);
278 nfssvc_decode_createargs(struct svc_rqst *rqstp, u32 *p,
279 struct nfsd_createargs *args)
281 if (!(p = decode_fh(p, &args->fh))
282 || !(p = decode_filename(p, &args->name, &args->len))
283 || !(p = decode_sattr(p, &args->attrs)))
286 return xdr_argsize_check(rqstp, p);
290 nfssvc_decode_renameargs(struct svc_rqst *rqstp, u32 *p,
291 struct nfsd_renameargs *args)
293 if (!(p = decode_fh(p, &args->ffh))
294 || !(p = decode_filename(p, &args->fname, &args->flen))
295 || !(p = decode_fh(p, &args->tfh))
296 || !(p = decode_filename(p, &args->tname, &args->tlen)))
299 return xdr_argsize_check(rqstp, p);
303 nfssvc_decode_linkargs(struct svc_rqst *rqstp, u32 *p,
304 struct nfsd_linkargs *args)
306 if (!(p = decode_fh(p, &args->ffh))
307 || !(p = decode_fh(p, &args->tfh))
308 || !(p = decode_filename(p, &args->tname, &args->tlen)))
311 return xdr_argsize_check(rqstp, p);
315 nfssvc_decode_symlinkargs(struct svc_rqst *rqstp, u32 *p,
316 struct nfsd_symlinkargs *args)
318 if (!(p = decode_fh(p, &args->ffh))
319 || !(p = decode_filename(p, &args->fname, &args->flen))
320 || !(p = decode_pathname(p, &args->tname, &args->tlen))
321 || !(p = decode_sattr(p, &args->attrs)))
324 return xdr_argsize_check(rqstp, p);
328 nfssvc_decode_readdirargs(struct svc_rqst *rqstp, u32 *p,
329 struct nfsd_readdirargs *args)
331 if (!(p = decode_fh(p, &args->fh)))
333 args->cookie = ntohl(*p++);
334 args->count = ntohl(*p++);
336 return xdr_argsize_check(rqstp, p);
340 * XDR encode functions
343 nfssvc_encode_void(struct svc_rqst *rqstp, u32 *p, void *dummy)
345 return xdr_ressize_check(rqstp, p);
349 nfssvc_encode_attrstat(struct svc_rqst *rqstp, u32 *p,
350 struct nfsd_attrstat *resp)
352 p = encode_fattr(rqstp, p, &resp->fh);
353 return xdr_ressize_check(rqstp, p);
357 nfssvc_encode_diropres(struct svc_rqst *rqstp, u32 *p,
358 struct nfsd_diropres *resp)
360 p = encode_fh(p, &resp->fh);
361 p = encode_fattr(rqstp, p, &resp->fh);
362 return xdr_ressize_check(rqstp, p);
366 nfssvc_encode_readlinkres(struct svc_rqst *rqstp, u32 *p,
367 struct nfsd_readlinkres *resp)
369 *p++ = htonl(resp->len);
370 p += XDR_QUADLEN(resp->len);
371 return xdr_ressize_check(rqstp, p);
375 nfssvc_encode_readres(struct svc_rqst *rqstp, u32 *p,
376 struct nfsd_readres *resp)
378 p = encode_fattr(rqstp, p, &resp->fh);
379 *p++ = htonl(resp->count);
380 p += XDR_QUADLEN(resp->count);
382 return xdr_ressize_check(rqstp, p);
386 nfssvc_encode_readdirres(struct svc_rqst *rqstp, u32 *p,
387 struct nfsd_readdirres *resp)
389 p += XDR_QUADLEN(resp->count);
390 return xdr_ressize_check(rqstp, p);
394 nfssvc_encode_statfsres(struct svc_rqst *rqstp, u32 *p,
395 struct nfsd_statfsres *resp)
397 struct statfs *stat = &resp->stats;
399 *p++ = htonl(NFSSVC_MAXBLKSIZE); /* max transfer size */
400 *p++ = htonl(stat->f_bsize);
401 *p++ = htonl(stat->f_blocks);
402 *p++ = htonl(stat->f_bfree);
403 *p++ = htonl(stat->f_bavail);
404 return xdr_ressize_check(rqstp, p);
408 nfssvc_encode_entry(struct readdir_cd *cd, const char *name,
409 int namlen, loff_t offset, ino_t ino, unsigned int d_type)
415 dprintk("nfsd: entry(%.*s off %ld ino %ld)\n",
416 namlen, name, offset, ino);
419 if (offset > ~((u32) 0))
422 *cd->offset = htonl(offset);
423 if (namlen > NFS2_MAXNAMLEN)
424 namlen = NFS2_MAXNAMLEN;/* truncate filename */
426 slen = XDR_QUADLEN(namlen);
427 if ((buflen = cd->buflen - slen - 4) < 0) {
431 *p++ = xdr_one; /* mark entry present */
432 *p++ = htonl((u32) ino); /* file id */
433 p = xdr_encode_array(p, name, namlen);/* name length & name */
434 cd->offset = p; /* remember pointer */
435 *p++ = ~(u32) 0; /* offset of next entry */
443 * XDR release functions
446 nfssvc_release_fhandle(struct svc_rqst *rqstp, u32 *p,
447 struct nfsd_fhandle *resp)