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/vserver/xid.h>
20 #define NFSDDBG_FACILITY NFSDDBG_XDR
23 #ifdef NFSD_OPTIMIZE_SPACE
28 * Mapping of S_IF* types to NFS file types
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,
39 * XDR functions for basic NFS types
42 decode_fh(u32 *p, struct svc_fh *fhp)
44 fh_init(fhp, NFS_FHSIZE);
45 memcpy(&fhp->fh_handle.fh_base, p, NFS_FHSIZE);
46 fhp->fh_handle.fh_size = NFS_FHSIZE;
48 /* FIXME: Look up export pointer here and verify
49 * Sun Secure RPC if requested */
50 return p + (NFS_FHSIZE >> 2);
54 encode_fh(u32 *p, struct svc_fh *fhp)
56 memcpy(p, &fhp->fh_handle.fh_base, NFS_FHSIZE);
57 return p + (NFS_FHSIZE>> 2);
61 * Decode a file name and make sure that the path contains
62 * no slashes or null bytes.
65 decode_filename(u32 *p, char **namp, int *lenp)
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 == '/')
81 decode_pathname(u32 *p, char **namp, int *lenp)
86 if ((p = xdr_decode_string_inplace(p, namp, lenp, NFS_MAXPATHLEN)) != NULL) {
87 for (i = 0, name = *namp; i < *lenp; i++, name++) {
97 decode_sattr(u32 *p, struct iattr *iap)
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.
107 if ((tmp = ntohl(*p++)) != (u32)-1 && tmp != 0xffff) {
108 iap->ia_valid |= ATTR_MODE;
111 if ((tmp = ntohl(*p++)) != (u32)-1) {
112 iap->ia_valid |= ATTR_UID;
115 if ((tmp = ntohl(*p++)) != (u32)-1) {
116 iap->ia_valid |= ATTR_GID;
119 if ((tmp = ntohl(*p++)) != (u32)-1) {
120 iap->ia_valid |= ATTR_SIZE;
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;
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;
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
144 iap->ia_valid &= ~(ATTR_ATIME_SET|ATTR_MTIME_SET);
150 encode_fattr(struct svc_rqst *rqstp, u32 *p, struct svc_fh *fhp)
152 struct vfsmount *mnt = fhp->fh_export->ex_mnt;
153 struct dentry *dentry = fhp->fh_dentry;
156 struct timespec time;
158 vfs_getattr(mnt, dentry, &stat);
159 type = (stat.mode & S_IFMT);
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)));
169 if (S_ISLNK(type) && stat.size > NFS_MAXPATHLEN) {
170 *p++ = htonl(NFS_MAXPATHLEN);
172 *p++ = htonl((u32) stat.size);
174 *p++ = htonl((u32) stat.blksize);
175 if (S_ISCHR(type) || S_ISBLK(type))
176 *p++ = htonl(new_encode_dev(stat.rdev));
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);
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);
198 * XDR decode functions
201 nfssvc_decode_void(struct svc_rqst *rqstp, u32 *p, void *dummy)
203 return xdr_argsize_check(rqstp, p);
207 nfssvc_decode_fhandle(struct svc_rqst *rqstp, u32 *p, struct nfsd_fhandle *args)
209 if (!(p = decode_fh(p, &args->fh)))
211 return xdr_argsize_check(rqstp, p);
215 nfssvc_decode_sattrargs(struct svc_rqst *rqstp, u32 *p,
216 struct nfsd_sattrargs *args)
218 if (!(p = decode_fh(p, &args->fh))
219 || !(p = decode_sattr(p, &args->attrs)))
222 return xdr_argsize_check(rqstp, p);
226 nfssvc_decode_diropargs(struct svc_rqst *rqstp, u32 *p,
227 struct nfsd_diropargs *args)
229 if (!(p = decode_fh(p, &args->fh))
230 || !(p = decode_filename(p, &args->name, &args->len)))
233 return xdr_argsize_check(rqstp, p);
237 nfssvc_decode_readargs(struct svc_rqst *rqstp, u32 *p,
238 struct nfsd_readargs *args)
242 if (!(p = decode_fh(p, &args->fh)))
245 args->offset = ntohl(*p++);
246 len = args->count = ntohl(*p++);
247 p++; /* totalcount - unused */
249 if (len > NFSSVC_MAXBLKSIZE)
250 len = NFSSVC_MAXBLKSIZE;
252 /* set up somewhere to store response.
253 * We take pages, put them on reslist and include in iovec
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 len -= args->vec[v].iov_len;
265 return xdr_argsize_check(rqstp, p);
269 nfssvc_decode_writeargs(struct svc_rqst *rqstp, u32 *p,
270 struct nfsd_writeargs *args)
274 if (!(p = decode_fh(p, &args->fh)))
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;
287 while (len > args->vec[v].iov_len) {
288 len -= args->vec[v].iov_len;
290 args->vec[v].iov_base = page_address(rqstp->rq_argpages[v]);
291 args->vec[v].iov_len = PAGE_SIZE;
293 args->vec[v].iov_len = len;
295 return args->vec[0].iov_len > 0;
299 nfssvc_decode_createargs(struct svc_rqst *rqstp, u32 *p,
300 struct nfsd_createargs *args)
302 if (!(p = decode_fh(p, &args->fh))
303 || !(p = decode_filename(p, &args->name, &args->len))
304 || !(p = decode_sattr(p, &args->attrs)))
307 return xdr_argsize_check(rqstp, p);
311 nfssvc_decode_renameargs(struct svc_rqst *rqstp, u32 *p,
312 struct nfsd_renameargs *args)
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)))
320 return xdr_argsize_check(rqstp, p);
324 nfssvc_decode_readlinkargs(struct svc_rqst *rqstp, u32 *p, struct nfsd_readlinkargs *args)
326 if (!(p = decode_fh(p, &args->fh)))
328 svc_take_page(rqstp);
329 args->buffer = page_address(rqstp->rq_respages[rqstp->rq_resused-1]);
331 return xdr_argsize_check(rqstp, p);
335 nfssvc_decode_linkargs(struct svc_rqst *rqstp, u32 *p,
336 struct nfsd_linkargs *args)
338 if (!(p = decode_fh(p, &args->ffh))
339 || !(p = decode_fh(p, &args->tfh))
340 || !(p = decode_filename(p, &args->tname, &args->tlen)))
343 return xdr_argsize_check(rqstp, p);
347 nfssvc_decode_symlinkargs(struct svc_rqst *rqstp, u32 *p,
348 struct nfsd_symlinkargs *args)
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)))
356 return xdr_argsize_check(rqstp, p);
360 nfssvc_decode_readdirargs(struct svc_rqst *rqstp, u32 *p,
361 struct nfsd_readdirargs *args)
363 if (!(p = decode_fh(p, &args->fh)))
365 args->cookie = ntohl(*p++);
366 args->count = ntohl(*p++);
367 if (args->count > PAGE_SIZE)
368 args->count = PAGE_SIZE;
370 svc_take_page(rqstp);
371 args->buffer = page_address(rqstp->rq_respages[rqstp->rq_resused-1]);
373 return xdr_argsize_check(rqstp, p);
377 * XDR encode functions
380 nfssvc_encode_void(struct svc_rqst *rqstp, u32 *p, void *dummy)
382 return xdr_ressize_check(rqstp, p);
386 nfssvc_encode_attrstat(struct svc_rqst *rqstp, u32 *p,
387 struct nfsd_attrstat *resp)
389 p = encode_fattr(rqstp, p, &resp->fh);
390 return xdr_ressize_check(rqstp, p);
394 nfssvc_encode_diropres(struct svc_rqst *rqstp, u32 *p,
395 struct nfsd_diropres *resp)
397 p = encode_fh(p, &resp->fh);
398 p = encode_fattr(rqstp, p, &resp->fh);
399 return xdr_ressize_check(rqstp, p);
403 nfssvc_encode_readlinkres(struct svc_rqst *rqstp, u32 *p,
404 struct nfsd_readlinkres *resp)
406 *p++ = htonl(resp->len);
407 xdr_ressize_check(rqstp, p);
408 rqstp->rq_res.page_len = resp->len;
410 /* need to pad the tail */
411 rqstp->rq_restailpage = 0;
412 rqstp->rq_res.tail[0].iov_base = p;
414 rqstp->rq_res.tail[0].iov_len = 4 - (resp->len&3);
420 nfssvc_encode_readres(struct svc_rqst *rqstp, u32 *p,
421 struct nfsd_readres *resp)
423 p = encode_fattr(rqstp, p, &resp->fh);
424 *p++ = htonl(resp->count);
425 xdr_ressize_check(rqstp, p);
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;
434 rqstp->rq_res.tail[0].iov_len = 4 - (resp->count&3);
440 nfssvc_encode_readdirres(struct svc_rqst *rqstp, u32 *p,
441 struct nfsd_readdirres *resp)
443 xdr_ressize_check(rqstp, p);
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;
453 nfssvc_encode_statfsres(struct svc_rqst *rqstp, u32 *p,
454 struct nfsd_statfsres *resp)
456 struct kstatfs *stat = &resp->stats;
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);
467 nfssvc_encode_entry(struct readdir_cd *ccd, const char *name,
468 int namlen, loff_t offset, ino_t ino, unsigned int d_type)
470 struct nfsd_readdirres *cd = container_of(ccd, struct nfsd_readdirres, common);
475 dprintk("nfsd: entry(%.*s off %ld ino %ld)\n",
476 namlen, name, offset, ino);
479 if (offset > ~((u32) 0)) {
480 cd->common.err = nfserr_fbig;
484 *cd->offset = htonl(offset);
485 if (namlen > NFS2_MAXNAMLEN)
486 namlen = NFS2_MAXNAMLEN;/* truncate filename */
488 slen = XDR_QUADLEN(namlen);
489 if ((buflen = cd->buflen - slen - 4) < 0) {
490 cd->common.err = nfserr_toosmall;
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 */
501 cd->common.err = nfs_ok;
506 * XDR release functions
509 nfssvc_release_fhandle(struct svc_rqst *rqstp, u32 *p,
510 struct nfsd_fhandle *resp)