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 19 #define NFSDDBG_FACILITY NFSDDBG_XDR 20 21 22 #ifdef NFSD_OPTIMIZE_SPACE 23 # define inline 24 #endif 25 26 /* 27 * Mapping of S_IF* types to NFS file types 28 */ 29 static u32 nfs_ftypes[] = { 30 NFNON, NFCHR, NFCHR, NFBAD, 31 NFDIR, NFBAD, NFBLK, NFBAD, 32 NFREG, NFBAD, NFLNK, NFBAD, 33 NFSOCK, NFBAD, NFLNK, NFBAD, 34 }; 35 36 37 /* 38 * XDR functions for basic NFS types 39 */ 40 static inline u32 * 41 decode_fh(u32 *p, struct svc_fh *fhp) 42 { 43 fh_init(fhp, NFS_FHSIZE); 44 memcpy(&fhp->fh_handle.fh_base, p, NFS_FHSIZE); 45 fhp->fh_handle.fh_size = NFS_FHSIZE; 46 47 /* FIXME: Look up export pointer here and verify 48 * Sun Secure RPC if requested */ 49 return p + (NFS_FHSIZE >> 2); 50 } 51 52 /* Helper function for NFSv2 ACL code */ 53 u32 *nfs2svc_decode_fh(u32 *p, struct svc_fh *fhp) 54 { 55 return decode_fh(p, fhp); 56 } 57 58 static inline u32 * 59 encode_fh(u32 *p, struct svc_fh *fhp) 60 { 61 memcpy(p, &fhp->fh_handle.fh_base, NFS_FHSIZE); 62 return p + (NFS_FHSIZE>> 2); 63 } 64 65 /* 66 * Decode a file name and make sure that the path contains 67 * no slashes or null bytes. 68 */ 69 static inline u32 * 70 decode_filename(u32 *p, char **namp, int *lenp) 71 { 72 char *name; 73 int i; 74 75 if ((p = xdr_decode_string_inplace(p, namp, lenp, NFS_MAXNAMLEN)) != NULL) { 76 for (i = 0, name = *namp; i < *lenp; i++, name++) { 77 if (*name == '\0' || *name == '/') 78 return NULL; 79 } 80 } 81 82 return p; 83 } 84 85 static inline u32 * 86 decode_pathname(u32 *p, char **namp, int *lenp) 87 { 88 char *name; 89 int i; 90 91 if ((p = xdr_decode_string_inplace(p, namp, lenp, NFS_MAXPATHLEN)) != NULL) { 92 for (i = 0, name = *namp; i < *lenp; i++, name++) { 93 if (*name == '\0') 94 return NULL; 95 } 96 } 97 98 return p; 99 } 100 101 static inline u32 * 102 decode_sattr(u32 *p, struct iattr *iap) 103 { 104 u32 tmp, tmp1; 105 106 iap->ia_valid = 0; 107 108 /* Sun client bug compatibility check: some sun clients seem to 109 * put 0xffff in the mode field when they mean 0xffffffff. 110 * Quoting the 4.4BSD nfs server code: Nah nah nah nah na nah. 111 */ 112 if ((tmp = ntohl(*p++)) != (u32)-1 && tmp != 0xffff) { 113 iap->ia_valid |= ATTR_MODE; 114 iap->ia_mode = tmp; 115 } 116 if ((tmp = ntohl(*p++)) != (u32)-1) { 117 iap->ia_valid |= ATTR_UID; 118 iap->ia_uid = tmp; 119 } 120 if ((tmp = ntohl(*p++)) != (u32)-1) { 121 iap->ia_valid |= ATTR_GID; 122 iap->ia_gid = tmp; 123 } 124 if ((tmp = ntohl(*p++)) != (u32)-1) { 125 iap->ia_valid |= ATTR_SIZE; 126 iap->ia_size = tmp; 127 } 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; 133 } 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; 139 /* 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 147 */ 148 if (tmp1 == 1000000) 149 iap->ia_valid &= ~(ATTR_ATIME_SET|ATTR_MTIME_SET); 150 } 151 return p; 152 } 153 154 static inline u32 * 155 encode_fattr(struct svc_rqst *rqstp, u32 *p, struct svc_fh *fhp) 156 { 157 struct vfsmount *mnt = fhp->fh_export->ex_mnt; 158 struct dentry *dentry = fhp->fh_dentry; 159 struct kstat stat; 160 int type; 161 struct timespec time; 162 163 vfs_getattr(mnt, dentry, &stat); 164 type = (stat.mode & S_IFMT); 165 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, stat.uid)); 170 *p++ = htonl((u32) nfsd_rgid(rqstp, stat.gid)); 171 172 if (S_ISLNK(type) && stat.size > NFS_MAXPATHLEN) { 173 *p++ = htonl(NFS_MAXPATHLEN); 174 } else { 175 *p++ = htonl((u32) stat.size); 176 } 177 *p++ = htonl((u32) stat.blksize); 178 if (S_ISCHR(type) || S_ISBLK(type)) 179 *p++ = htonl(new_encode_dev(stat.rdev)); 180 else 181 *p++ = htonl(0xffffffff); 182 *p++ = htonl((u32) stat.blocks); 183 if (is_fsid(fhp, rqstp->rq_reffh)) 184 *p++ = htonl((u32) fhp->fh_export->ex_fsid); 185 else 186 *p++ = htonl(new_encode_dev(stat.dev)); 187 *p++ = htonl((u32) stat.ino); 188 *p++ = htonl((u32) stat.atime.tv_sec); 189 *p++ = htonl(stat.atime.tv_nsec ? stat.atime.tv_nsec / 1000 : 0); 190 lease_get_mtime(dentry->d_inode, &time); 191 *p++ = htonl((u32) time.tv_sec); 192 *p++ = htonl(time.tv_nsec ? time.tv_nsec / 1000 : 0); 193 *p++ = htonl((u32) stat.ctime.tv_sec); 194 *p++ = htonl(stat.ctime.tv_nsec ? stat.ctime.tv_nsec / 1000 : 0); 195 196 return p; 197 } 198 199 /* Helper function for NFSv2 ACL code */ 200 u32 *nfs2svc_encode_fattr(struct svc_rqst *rqstp, u32 *p, struct svc_fh *fhp) 201 { 202 return encode_fattr(rqstp, p, fhp); 203 } 204 205 /* 206 * XDR decode functions 207 */ 208 int 209 nfssvc_decode_void(struct svc_rqst *rqstp, u32 *p, void *dummy) 210 { 211 return xdr_argsize_check(rqstp, p); 212 } 213 214 int 215 nfssvc_decode_fhandle(struct svc_rqst *rqstp, u32 *p, struct nfsd_fhandle *args) 216 { 217 if (!(p = decode_fh(p, &args->fh))) 218 return 0; 219 return xdr_argsize_check(rqstp, p); 220 } 221 222 int 223 nfssvc_decode_sattrargs(struct svc_rqst *rqstp, u32 *p, 224 struct nfsd_sattrargs *args) 225 { 226 if (!(p = decode_fh(p, &args->fh)) 227 || !(p = decode_sattr(p, &args->attrs))) 228 return 0; 229 230 return xdr_argsize_check(rqstp, p); 231 } 232 233 int 234 nfssvc_decode_diropargs(struct svc_rqst *rqstp, u32 *p, 235 struct nfsd_diropargs *args) 236 { 237 if (!(p = decode_fh(p, &args->fh)) 238 || !(p = decode_filename(p, &args->name, &args->len))) 239 return 0; 240 241 return xdr_argsize_check(rqstp, p); 242 } 243 244 int 245 nfssvc_decode_readargs(struct svc_rqst *rqstp, u32 *p, 246 struct nfsd_readargs *args) 247 { 248 unsigned int len; 249 int v,pn; 250 if (!(p = decode_fh(p, &args->fh))) 251 return 0; 252 253 args->offset = ntohl(*p++); 254 len = args->count = ntohl(*p++); 255 p++; /* totalcount - unused */ 256 257 if (len > NFSSVC_MAXBLKSIZE) 258 len = NFSSVC_MAXBLKSIZE; 259 260 /* set up somewhere to store response. 261 * We take pages, put them on reslist and include in iovec 262 */ 263 v=0; 264 while (len > 0) { 265 pn=rqstp->rq_resused; 266 svc_take_page(rqstp); 267 args->vec[v].iov_base = page_address(rqstp->rq_respages[pn]); 268 args->vec[v].iov_len = len < PAGE_SIZE?len:PAGE_SIZE; 269 len -= args->vec[v].iov_len; 270 v++; 271 } 272 args->vlen = v; 273 return xdr_argsize_check(rqstp, p); 274 } 275 276 int 277 nfssvc_decode_writeargs(struct svc_rqst *rqstp, u32 *p, 278 struct nfsd_writeargs *args) 279 { 280 unsigned int len; 281 int v; 282 if (!(p = decode_fh(p, &args->fh))) 283 return 0; 284 285 p++; /* beginoffset */ 286 args->offset = ntohl(*p++); /* offset */ 287 p++; /* totalcount */ 288 len = args->len = ntohl(*p++); 289 args->vec[0].iov_base = (void*)p; 290 args->vec[0].iov_len = rqstp->rq_arg.head[0].iov_len - 291 (((void*)p) - rqstp->rq_arg.head[0].iov_base); 292 if (len > NFSSVC_MAXBLKSIZE) 293 len = NFSSVC_MAXBLKSIZE; 294 v = 0; 295 while (len > args->vec[v].iov_len) { 296 len -= args->vec[v].iov_len; 297 v++; 298 args->vec[v].iov_base = page_address(rqstp->rq_argpages[v]); 299 args->vec[v].iov_len = PAGE_SIZE; 300 } 301 args->vec[v].iov_len = len; 302 args->vlen = v+1; 303 return args->vec[0].iov_len > 0; 304 } 305 306 int 307 nfssvc_decode_createargs(struct svc_rqst *rqstp, u32 *p, 308 struct nfsd_createargs *args) 309 { 310 if (!(p = decode_fh(p, &args->fh)) 311 || !(p = decode_filename(p, &args->name, &args->len)) 312 || !(p = decode_sattr(p, &args->attrs))) 313 return 0; 314 315 return xdr_argsize_check(rqstp, p); 316 } 317 318 int 319 nfssvc_decode_renameargs(struct svc_rqst *rqstp, u32 *p, 320 struct nfsd_renameargs *args) 321 { 322 if (!(p = decode_fh(p, &args->ffh)) 323 || !(p = decode_filename(p, &args->fname, &args->flen)) 324 || !(p = decode_fh(p, &args->tfh)) 325 || !(p = decode_filename(p, &args->tname, &args->tlen))) 326 return 0; 327 328 return xdr_argsize_check(rqstp, p); 329 } 330 331 int 332 nfssvc_decode_readlinkargs(struct svc_rqst *rqstp, u32 *p, struct nfsd_readlinkargs *args) 333 { 334 if (!(p = decode_fh(p, &args->fh))) 335 return 0; 336 svc_take_page(rqstp); 337 args->buffer = page_address(rqstp->rq_respages[rqstp->rq_resused-1]); 338 339 return xdr_argsize_check(rqstp, p); 340 } 341 342 int 343 nfssvc_decode_linkargs(struct svc_rqst *rqstp, u32 *p, 344 struct nfsd_linkargs *args) 345 { 346 if (!(p = decode_fh(p, &args->ffh)) 347 || !(p = decode_fh(p, &args->tfh)) 348 || !(p = decode_filename(p, &args->tname, &args->tlen))) 349 return 0; 350 351 return xdr_argsize_check(rqstp, p); 352 } 353 354 int 355 nfssvc_decode_symlinkargs(struct svc_rqst *rqstp, u32 *p, 356 struct nfsd_symlinkargs *args) 357 { 358 if (!(p = decode_fh(p, &args->ffh)) 359 || !(p = decode_filename(p, &args->fname, &args->flen)) 360 || !(p = decode_pathname(p, &args->tname, &args->tlen)) 361 || !(p = decode_sattr(p, &args->attrs))) 362 return 0; 363 364 return xdr_argsize_check(rqstp, p); 365 } 366 367 int 368 nfssvc_decode_readdirargs(struct svc_rqst *rqstp, u32 *p, 369 struct nfsd_readdirargs *args) 370 { 371 if (!(p = decode_fh(p, &args->fh))) 372 return 0; 373 args->cookie = ntohl(*p++); 374 args->count = ntohl(*p++); 375 if (args->count > PAGE_SIZE) 376 args->count = PAGE_SIZE; 377 378 svc_take_page(rqstp); 379 args->buffer = page_address(rqstp->rq_respages[rqstp->rq_resused-1]); 380 381 return xdr_argsize_check(rqstp, p); 382 } 383 384 /* 385 * XDR encode functions 386 */ 387 int 388 nfssvc_encode_void(struct svc_rqst *rqstp, u32 *p, void *dummy) 389 { 390 return xdr_ressize_check(rqstp, p); 391 } 392 393 int 394 nfssvc_encode_attrstat(struct svc_rqst *rqstp, u32 *p, 395 struct nfsd_attrstat *resp) 396 { 397 p = encode_fattr(rqstp, p, &resp->fh); 398 return xdr_ressize_check(rqstp, p); 399 } 400 401 int 402 nfssvc_encode_diropres(struct svc_rqst *rqstp, u32 *p, 403 struct nfsd_diropres *resp) 404 { 405 p = encode_fh(p, &resp->fh); 406 p = encode_fattr(rqstp, p, &resp->fh); 407 return xdr_ressize_check(rqstp, p); 408 } 409 410 int 411 nfssvc_encode_readlinkres(struct svc_rqst *rqstp, u32 *p, 412 struct nfsd_readlinkres *resp) 413 { 414 *p++ = htonl(resp->len); 415 xdr_ressize_check(rqstp, p); 416 rqstp->rq_res.page_len = resp->len; 417 if (resp->len & 3) { 418 /* need to pad the tail */ 419 rqstp->rq_restailpage = 0; 420 rqstp->rq_res.tail[0].iov_base = p; 421 *p = 0; 422 rqstp->rq_res.tail[0].iov_len = 4 - (resp->len&3); 423 } 424 return 1; 425 } 426 427 int 428 nfssvc_encode_readres(struct svc_rqst *rqstp, u32 *p, 429 struct nfsd_readres *resp) 430 { 431 p = encode_fattr(rqstp, p, &resp->fh); 432 *p++ = htonl(resp->count); 433 xdr_ressize_check(rqstp, p); 434 435 /* now update rqstp->rq_res to reflect data aswell */ 436 rqstp->rq_res.page_len = resp->count; 437 if (resp->count & 3) { 438 /* need to pad the tail */ 439 rqstp->rq_restailpage = 0; 440 rqstp->rq_res.tail[0].iov_base = p; 441 *p = 0; 442 rqstp->rq_res.tail[0].iov_len = 4 - (resp->count&3); 443 } 444 return 1; 445 } 446 447 int 448 nfssvc_encode_readdirres(struct svc_rqst *rqstp, u32 *p, 449 struct nfsd_readdirres *resp) 450 { 451 xdr_ressize_check(rqstp, p); 452 p = resp->buffer; 453 *p++ = 0; /* no more entries */ 454 *p++ = htonl((resp->common.err == nfserr_eof)); 455 rqstp->rq_res.page_len = (((unsigned long)p-1) & ~PAGE_MASK)+1; 456 457 return 1; 458 } 459 460 int 461 nfssvc_encode_statfsres(struct svc_rqst *rqstp, u32 *p, 462 struct nfsd_statfsres *resp) 463 { 464 struct kstatfs *stat = &resp->stats; 465 466 *p++ = htonl(NFSSVC_MAXBLKSIZE); /* max transfer size */ 467 *p++ = htonl(stat->f_bsize); 468 *p++ = htonl(stat->f_blocks); 469 *p++ = htonl(stat->f_bfree); 470 *p++ = htonl(stat->f_bavail); 471 return xdr_ressize_check(rqstp, p); 472 } 473 474 int 475 nfssvc_encode_entry(struct readdir_cd *ccd, const char *name, 476 int namlen, loff_t offset, ino_t ino, unsigned int d_type) 477 { 478 struct nfsd_readdirres *cd = container_of(ccd, struct nfsd_readdirres, common); 479 u32 *p = cd->buffer; 480 int buflen, slen; 481 482 /* 483 dprintk("nfsd: entry(%.*s off %ld ino %ld)\n", 484 namlen, name, offset, ino); 485 */ 486 487 if (offset > ~((u32) 0)) { 488 cd->common.err = nfserr_fbig; 489 return -EINVAL; 490 } 491 if (cd->offset) 492 *cd->offset = htonl(offset); 493 if (namlen > NFS2_MAXNAMLEN) 494 namlen = NFS2_MAXNAMLEN;/* truncate filename */ 495 496 slen = XDR_QUADLEN(namlen); 497 if ((buflen = cd->buflen - slen - 4) < 0) { 498 cd->common.err = nfserr_toosmall; 499 return -EINVAL; 500 } 501 *p++ = xdr_one; /* mark entry present */ 502 *p++ = htonl((u32) ino); /* file id */ 503 p = xdr_encode_array(p, name, namlen);/* name length & name */ 504 cd->offset = p; /* remember pointer */ 505 *p++ = ~(u32) 0; /* offset of next entry */ 506 507 cd->buflen = buflen; 508 cd->buffer = p; 509 cd->common.err = nfs_ok; 510 return 0; 511 } 512 513 /* 514 * XDR release functions 515 */ 516 int 517 nfssvc_release_fhandle(struct svc_rqst *rqstp, u32 *p, 518 struct nfsd_fhandle *resp) 519 { 520 fh_put(&resp->fh); 521 return 1; 522 } 523