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)
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.
109 if ((tmp = ntohl(*p++)) != (u32)-1 && tmp != 0xffff) {
110 iap->ia_valid |= ATTR_MODE;
113 if ((tmp = ntohl(*p++)) != (u32)-1) {
114 iap->ia_valid |= ATTR_UID;
117 if ((tmp = ntohl(*p++)) != (u32)-1) {
118 iap->ia_valid |= ATTR_GID;
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;
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;
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;
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
149 iap->ia_valid &= ~(ATTR_ATIME_SET|ATTR_MTIME_SET);
155 encode_fattr(struct svc_rqst *rqstp, u32 *p, struct svc_fh *fhp)
157 struct vfsmount *mnt = fhp->fh_export->ex_mnt;
158 struct dentry *dentry = fhp->fh_dentry;
161 struct timespec time;
163 vfs_getattr(mnt, dentry, &stat);
164 type = (stat.mode & S_IFMT);
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)));
174 if (S_ISLNK(type) && stat.size > NFS_MAXPATHLEN) {
175 *p++ = htonl(NFS_MAXPATHLEN);
177 *p++ = htonl((u32) stat.size);
179 *p++ = htonl((u32) stat.blksize);
180 if (S_ISCHR(type) || S_ISBLK(type))
181 *p++ = htonl(new_encode_dev(stat.rdev));
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);
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);
203 * XDR decode functions
206 nfssvc_decode_void(struct svc_rqst *rqstp, u32 *p, void *dummy)
208 return xdr_argsize_check(rqstp, p);
212 nfssvc_decode_fhandle(struct svc_rqst *rqstp, u32 *p, struct nfsd_fhandle *args)
214 if (!(p = decode_fh(p, &args->fh)))
216 return xdr_argsize_check(rqstp, p);
220 nfssvc_decode_sattrargs(struct svc_rqst *rqstp, u32 *p,
221 struct nfsd_sattrargs *args)
223 if (!(p = decode_fh(p, &args->fh))
224 || !(p = decode_sattr(p, &args->attrs)))
227 return xdr_argsize_check(rqstp, p);
231 nfssvc_decode_diropargs(struct svc_rqst *rqstp, u32 *p,
232 struct nfsd_diropargs *args)
234 if (!(p = decode_fh(p, &args->fh))
235 || !(p = decode_filename(p, &args->name, &args->len)))
238 return xdr_argsize_check(rqstp, p);
242 nfssvc_decode_readargs(struct svc_rqst *rqstp, u32 *p,
243 struct nfsd_readargs *args)
247 if (!(p = decode_fh(p, &args->fh)))
250 args->offset = ntohl(*p++);
251 len = args->count = ntohl(*p++);
252 p++; /* totalcount - unused */
254 if (len > NFSSVC_MAXBLKSIZE)
255 len = NFSSVC_MAXBLKSIZE;
257 /* set up somewhere to store response.
258 * We take pages, put them on reslist and include in iovec
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;
270 return xdr_argsize_check(rqstp, p);
274 nfssvc_decode_writeargs(struct svc_rqst *rqstp, u32 *p,
275 struct nfsd_writeargs *args)
279 if (!(p = decode_fh(p, &args->fh)))
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;
292 while (len > args->vec[v].iov_len) {
293 len -= args->vec[v].iov_len;
295 args->vec[v].iov_base = page_address(rqstp->rq_argpages[v]);
296 args->vec[v].iov_len = PAGE_SIZE;
298 args->vec[v].iov_len = len;
300 return args->vec[0].iov_len > 0;
304 nfssvc_decode_createargs(struct svc_rqst *rqstp, u32 *p,
305 struct nfsd_createargs *args)
307 if (!(p = decode_fh(p, &args->fh))
308 || !(p = decode_filename(p, &args->name, &args->len))
309 || !(p = decode_sattr(p, &args->attrs)))
312 return xdr_argsize_check(rqstp, p);
316 nfssvc_decode_renameargs(struct svc_rqst *rqstp, u32 *p,
317 struct nfsd_renameargs *args)
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)))
325 return xdr_argsize_check(rqstp, p);
329 nfssvc_decode_readlinkargs(struct svc_rqst *rqstp, u32 *p, struct nfsd_readlinkargs *args)
331 if (!(p = decode_fh(p, &args->fh)))
333 svc_take_page(rqstp);
334 args->buffer = page_address(rqstp->rq_respages[rqstp->rq_resused-1]);
336 return xdr_argsize_check(rqstp, p);
340 nfssvc_decode_linkargs(struct svc_rqst *rqstp, u32 *p,
341 struct nfsd_linkargs *args)
343 if (!(p = decode_fh(p, &args->ffh))
344 || !(p = decode_fh(p, &args->tfh))
345 || !(p = decode_filename(p, &args->tname, &args->tlen)))
348 return xdr_argsize_check(rqstp, p);
352 nfssvc_decode_symlinkargs(struct svc_rqst *rqstp, u32 *p,
353 struct nfsd_symlinkargs *args)
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)))
361 return xdr_argsize_check(rqstp, p);
365 nfssvc_decode_readdirargs(struct svc_rqst *rqstp, u32 *p,
366 struct nfsd_readdirargs *args)
368 if (!(p = decode_fh(p, &args->fh)))
370 args->cookie = ntohl(*p++);
371 args->count = ntohl(*p++);
372 if (args->count > PAGE_SIZE)
373 args->count = PAGE_SIZE;
375 svc_take_page(rqstp);
376 args->buffer = page_address(rqstp->rq_respages[rqstp->rq_resused-1]);
378 return xdr_argsize_check(rqstp, p);
382 * XDR encode functions
385 nfssvc_encode_void(struct svc_rqst *rqstp, u32 *p, void *dummy)
387 return xdr_ressize_check(rqstp, p);
391 nfssvc_encode_attrstat(struct svc_rqst *rqstp, u32 *p,
392 struct nfsd_attrstat *resp)
394 p = encode_fattr(rqstp, p, &resp->fh);
395 return xdr_ressize_check(rqstp, p);
399 nfssvc_encode_diropres(struct svc_rqst *rqstp, u32 *p,
400 struct nfsd_diropres *resp)
402 p = encode_fh(p, &resp->fh);
403 p = encode_fattr(rqstp, p, &resp->fh);
404 return xdr_ressize_check(rqstp, p);
408 nfssvc_encode_readlinkres(struct svc_rqst *rqstp, u32 *p,
409 struct nfsd_readlinkres *resp)
411 *p++ = htonl(resp->len);
412 xdr_ressize_check(rqstp, p);
413 rqstp->rq_res.page_len = resp->len;
415 /* need to pad the tail */
416 rqstp->rq_restailpage = 0;
417 rqstp->rq_res.tail[0].iov_base = p;
419 rqstp->rq_res.tail[0].iov_len = 4 - (resp->len&3);
425 nfssvc_encode_readres(struct svc_rqst *rqstp, u32 *p,
426 struct nfsd_readres *resp)
428 p = encode_fattr(rqstp, p, &resp->fh);
429 *p++ = htonl(resp->count);
430 xdr_ressize_check(rqstp, p);
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;
439 rqstp->rq_res.tail[0].iov_len = 4 - (resp->count&3);
445 nfssvc_encode_readdirres(struct svc_rqst *rqstp, u32 *p,
446 struct nfsd_readdirres *resp)
448 xdr_ressize_check(rqstp, p);
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;
458 nfssvc_encode_statfsres(struct svc_rqst *rqstp, u32 *p,
459 struct nfsd_statfsres *resp)
461 struct kstatfs *stat = &resp->stats;
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);
472 nfssvc_encode_entry(struct readdir_cd *ccd, const char *name,
473 int namlen, loff_t offset, ino_t ino, unsigned int d_type)
475 struct nfsd_readdirres *cd = container_of(ccd, struct nfsd_readdirres, common);
480 dprintk("nfsd: entry(%.*s off %ld ino %ld)\n",
481 namlen, name, offset, ino);
484 if (offset > ~((u32) 0)) {
485 cd->common.err = nfserr_fbig;
489 *cd->offset = htonl(offset);
490 if (namlen > NFS2_MAXNAMLEN)
491 namlen = NFS2_MAXNAMLEN;/* truncate filename */
493 slen = XDR_QUADLEN(namlen);
494 if ((buflen = cd->buflen - slen - 4) < 0) {
495 cd->common.err = nfserr_toosmall;
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 */
506 cd->common.err = nfs_ok;
511 * XDR release functions
514 nfssvc_release_fhandle(struct svc_rqst *rqstp, u32 *p,
515 struct nfsd_fhandle *resp)