1 /* 2 * nfsproc2.c Process version 2 NFS requests. 3 * linux/fs/nfsd/nfs2proc.c 4 * 5 * Process version 2 NFS requests. 6 * 7 * Copyright (C) 1995-1997 Olaf Kirch <okir@monad.swb.de> 8 */ 9 10 #include <linux/linkage.h> 11 #include <linux/time.h> 12 #include <linux/errno.h> 13 #include <linux/fs.h> 14 #include <linux/stat.h> 15 #include <linux/fcntl.h> 16 #include <linux/net.h> 17 #include <linux/in.h> 18 #include <linux/namei.h> 19 #include <linux/unistd.h> 20 #include <linux/slab.h> 21 22 #include <linux/sunrpc/clnt.h> 23 #include <linux/sunrpc/svc.h> 24 #include <linux/nfsd/nfsd.h> 25 #include <linux/nfsd/cache.h> 26 #include <linux/nfsd/xdr.h> 27 28 typedef struct svc_rqst svc_rqst; 29 typedef struct svc_buf svc_buf; 30 31 #define NFSDDBG_FACILITY NFSDDBG_PROC 32 33 34 static __be32 35 nfsd_proc_null(struct svc_rqst *rqstp, void *argp, void *resp) 36 { 37 return nfs_ok; 38 } 39 40 static __be32 41 nfsd_return_attrs(__be32 err, struct nfsd_attrstat *resp) 42 { 43 if (err) return err; 44 return nfserrno(vfs_getattr(resp->fh.fh_export->ex_mnt, 45 resp->fh.fh_dentry, 46 &resp->stat)); 47 } 48 static __be32 49 nfsd_return_dirop(__be32 err, struct nfsd_diropres *resp) 50 { 51 if (err) return err; 52 return nfserrno(vfs_getattr(resp->fh.fh_export->ex_mnt, 53 resp->fh.fh_dentry, 54 &resp->stat)); 55 } 56 /* 57 * Get a file's attributes 58 * N.B. After this call resp->fh needs an fh_put 59 */ 60 static __be32 61 nfsd_proc_getattr(struct svc_rqst *rqstp, struct nfsd_fhandle *argp, 62 struct nfsd_attrstat *resp) 63 { 64 __be32 nfserr; 65 dprintk("nfsd: GETATTR %s\n", SVCFH_fmt(&argp->fh)); 66 67 fh_copy(&resp->fh, &argp->fh); 68 nfserr = fh_verify(rqstp, &resp->fh, 0, MAY_NOP); 69 return nfsd_return_attrs(nfserr, resp); 70 } 71 72 /* 73 * Set a file's attributes 74 * N.B. After this call resp->fh needs an fh_put 75 */ 76 static __be32 77 nfsd_proc_setattr(struct svc_rqst *rqstp, struct nfsd_sattrargs *argp, 78 struct nfsd_attrstat *resp) 79 { 80 __be32 nfserr; 81 dprintk("nfsd: SETATTR %s, valid=%x, size=%ld\n", 82 SVCFH_fmt(&argp->fh), 83 argp->attrs.ia_valid, (long) argp->attrs.ia_size); 84 85 fh_copy(&resp->fh, &argp->fh); 86 nfserr = nfsd_setattr(rqstp, &resp->fh, &argp->attrs,0, (time_t)0); 87 return nfsd_return_attrs(nfserr, resp); 88 } 89 90 /* 91 * Look up a path name component 92 * Note: the dentry in the resp->fh may be negative if the file 93 * doesn't exist yet. 94 * N.B. After this call resp->fh needs an fh_put 95 */ 96 static __be32 97 nfsd_proc_lookup(struct svc_rqst *rqstp, struct nfsd_diropargs *argp, 98 struct nfsd_diropres *resp) 99 { 100 __be32 nfserr; 101 102 dprintk("nfsd: LOOKUP %s %.*s\n", 103 SVCFH_fmt(&argp->fh), argp->len, argp->name); 104 105 fh_init(&resp->fh, NFS_FHSIZE); 106 nfserr = nfsd_lookup(rqstp, &argp->fh, argp->name, argp->len, 107 &resp->fh); 108 109 fh_put(&argp->fh); 110 return nfsd_return_dirop(nfserr, resp); 111 } 112 113 /* 114 * Read a symlink. 115 */ 116 static __be32 117 nfsd_proc_readlink(struct svc_rqst *rqstp, struct nfsd_readlinkargs *argp, 118 struct nfsd_readlinkres *resp) 119 { 120 __be32 nfserr; 121 122 dprintk("nfsd: READLINK %s\n", SVCFH_fmt(&argp->fh)); 123 124 /* Read the symlink. */ 125 resp->len = NFS_MAXPATHLEN; 126 nfserr = nfsd_readlink(rqstp, &argp->fh, argp->buffer, &resp->len); 127 128 fh_put(&argp->fh); 129 return nfserr; 130 } 131 132 /* 133 * Read a portion of a file. 134 * N.B. After this call resp->fh needs an fh_put 135 */ 136 static __be32 137 nfsd_proc_read(struct svc_rqst *rqstp, struct nfsd_readargs *argp, 138 struct nfsd_readres *resp) 139 { 140 __be32 nfserr; 141 142 dprintk("nfsd: READ %s %d bytes at %d\n", 143 SVCFH_fmt(&argp->fh), 144 argp->count, argp->offset); 145 146 /* Obtain buffer pointer for payload. 19 is 1 word for 147 * status, 17 words for fattr, and 1 word for the byte count. 148 */ 149 150 if (NFSSVC_MAXBLKSIZE_V2 < argp->count) { 151 char buf[RPC_MAX_ADDRBUFLEN]; 152 printk(KERN_NOTICE 153 "oversized read request from %s (%d bytes)\n", 154 svc_print_addr(rqstp, buf, sizeof(buf)), 155 argp->count); 156 argp->count = NFSSVC_MAXBLKSIZE_V2; 157 } 158 svc_reserve(rqstp, (19<<2) + argp->count + 4); 159 160 resp->count = argp->count; 161 nfserr = nfsd_read(rqstp, fh_copy(&resp->fh, &argp->fh), NULL, 162 argp->offset, 163 rqstp->rq_vec, argp->vlen, 164 &resp->count); 165 166 if (nfserr) return nfserr; 167 return nfserrno(vfs_getattr(resp->fh.fh_export->ex_mnt, 168 resp->fh.fh_dentry, 169 &resp->stat)); 170 } 171 172 /* 173 * Write data to a file 174 * N.B. After this call resp->fh needs an fh_put 175 */ 176 static __be32 177 nfsd_proc_write(struct svc_rqst *rqstp, struct nfsd_writeargs *argp, 178 struct nfsd_attrstat *resp) 179 { 180 __be32 nfserr; 181 int stable = 1; 182 183 dprintk("nfsd: WRITE %s %d bytes at %d\n", 184 SVCFH_fmt(&argp->fh), 185 argp->len, argp->offset); 186 187 nfserr = nfsd_write(rqstp, fh_copy(&resp->fh, &argp->fh), NULL, 188 argp->offset, 189 rqstp->rq_vec, argp->vlen, 190 argp->len, 191 &stable); 192 return nfsd_return_attrs(nfserr, resp); 193 } 194 195 /* 196 * CREATE processing is complicated. The keyword here is `overloaded.' 197 * The parent directory is kept locked between the check for existence 198 * and the actual create() call in compliance with VFS protocols. 199 * N.B. After this call _both_ argp->fh and resp->fh need an fh_put 200 */ 201 static __be32 202 nfsd_proc_create(struct svc_rqst *rqstp, struct nfsd_createargs *argp, 203 struct nfsd_diropres *resp) 204 { 205 svc_fh *dirfhp = &argp->fh; 206 svc_fh *newfhp = &resp->fh; 207 struct iattr *attr = &argp->attrs; 208 struct inode *inode; 209 struct dentry *dchild; 210 int type, mode; 211 __be32 nfserr; 212 dev_t rdev = 0, wanted = new_decode_dev(attr->ia_size); 213 214 dprintk("nfsd: CREATE %s %.*s\n", 215 SVCFH_fmt(dirfhp), argp->len, argp->name); 216 217 /* First verify the parent file handle */ 218 nfserr = fh_verify(rqstp, dirfhp, S_IFDIR, MAY_EXEC); 219 if (nfserr) 220 goto done; /* must fh_put dirfhp even on error */ 221 222 /* Check for MAY_WRITE in nfsd_create if necessary */ 223 224 nfserr = nfserr_acces; 225 if (!argp->len) 226 goto done; 227 nfserr = nfserr_exist; 228 if (isdotent(argp->name, argp->len)) 229 goto done; 230 fh_lock_nested(dirfhp, I_MUTEX_PARENT); 231 dchild = lookup_one_len(argp->name, dirfhp->fh_dentry, argp->len); 232 if (IS_ERR(dchild)) { 233 nfserr = nfserrno(PTR_ERR(dchild)); 234 goto out_unlock; 235 } 236 fh_init(newfhp, NFS_FHSIZE); 237 nfserr = fh_compose(newfhp, dirfhp->fh_export, dchild, dirfhp); 238 if (!nfserr && !dchild->d_inode) 239 nfserr = nfserr_noent; 240 dput(dchild); 241 if (nfserr) { 242 if (nfserr != nfserr_noent) 243 goto out_unlock; 244 /* 245 * If the new file handle wasn't verified, we can't tell 246 * whether the file exists or not. Time to bail ... 247 */ 248 nfserr = nfserr_acces; 249 if (!newfhp->fh_dentry) { 250 printk(KERN_WARNING 251 "nfsd_proc_create: file handle not verified\n"); 252 goto out_unlock; 253 } 254 } 255 256 inode = newfhp->fh_dentry->d_inode; 257 258 /* Unfudge the mode bits */ 259 if (attr->ia_valid & ATTR_MODE) { 260 type = attr->ia_mode & S_IFMT; 261 mode = attr->ia_mode & ~S_IFMT; 262 if (!type) { 263 /* no type, so if target exists, assume same as that, 264 * else assume a file */ 265 if (inode) { 266 type = inode->i_mode & S_IFMT; 267 switch(type) { 268 case S_IFCHR: 269 case S_IFBLK: 270 /* reserve rdev for later checking */ 271 rdev = inode->i_rdev; 272 attr->ia_valid |= ATTR_SIZE; 273 274 /* FALLTHROUGH */ 275 case S_IFIFO: 276 /* this is probably a permission check.. 277 * at least IRIX implements perm checking on 278 * echo thing > device-special-file-or-pipe 279 * by doing a CREATE with type==0 280 */ 281 nfserr = nfsd_permission(newfhp->fh_export, 282 newfhp->fh_dentry, 283 MAY_WRITE|MAY_LOCAL_ACCESS); 284 if (nfserr && nfserr != nfserr_rofs) 285 goto out_unlock; 286 } 287 } else 288 type = S_IFREG; 289 } 290 } else if (inode) { 291 type = inode->i_mode & S_IFMT; 292 mode = inode->i_mode & ~S_IFMT; 293 } else { 294 type = S_IFREG; 295 mode = 0; /* ??? */ 296 } 297 298 attr->ia_valid |= ATTR_MODE; 299 attr->ia_mode = mode; 300 301 /* Special treatment for non-regular files according to the 302 * gospel of sun micro 303 */ 304 if (type != S_IFREG) { 305 int is_borc = 0; 306 if (type != S_IFBLK && type != S_IFCHR) { 307 rdev = 0; 308 } else if (type == S_IFCHR && !(attr->ia_valid & ATTR_SIZE)) { 309 /* If you think you've seen the worst, grok this. */ 310 type = S_IFIFO; 311 } else { 312 /* Okay, char or block special */ 313 is_borc = 1; 314 if (!rdev) 315 rdev = wanted; 316 } 317 318 /* we've used the SIZE information, so discard it */ 319 attr->ia_valid &= ~ATTR_SIZE; 320 321 /* Make sure the type and device matches */ 322 nfserr = nfserr_exist; 323 if (inode && type != (inode->i_mode & S_IFMT)) 324 goto out_unlock; 325 } 326 327 nfserr = 0; 328 if (!inode) { 329 /* File doesn't exist. Create it and set attrs */ 330 nfserr = nfsd_create(rqstp, dirfhp, argp->name, argp->len, 331 attr, type, rdev, newfhp); 332 } else if (type == S_IFREG) { 333 dprintk("nfsd: existing %s, valid=%x, size=%ld\n", 334 argp->name, attr->ia_valid, (long) attr->ia_size); 335 /* File already exists. We ignore all attributes except 336 * size, so that creat() behaves exactly like 337 * open(..., O_CREAT|O_TRUNC|O_WRONLY). 338 */ 339 attr->ia_valid &= ATTR_SIZE; 340 if (attr->ia_valid) 341 nfserr = nfsd_setattr(rqstp, newfhp, attr, 0, (time_t)0); 342 } 343 344 out_unlock: 345 /* We don't really need to unlock, as fh_put does it. */ 346 fh_unlock(dirfhp); 347 348 done: 349 fh_put(dirfhp); 350 return nfsd_return_dirop(nfserr, resp); 351 } 352 353 static __be32 354 nfsd_proc_remove(struct svc_rqst *rqstp, struct nfsd_diropargs *argp, 355 void *resp) 356 { 357 __be32 nfserr; 358 359 dprintk("nfsd: REMOVE %s %.*s\n", SVCFH_fmt(&argp->fh), 360 argp->len, argp->name); 361 362 /* Unlink. -SIFDIR means file must not be a directory */ 363 nfserr = nfsd_unlink(rqstp, &argp->fh, -S_IFDIR, argp->name, argp->len); 364 fh_put(&argp->fh); 365 return nfserr; 366 } 367 368 static __be32 369 nfsd_proc_rename(struct svc_rqst *rqstp, struct nfsd_renameargs *argp, 370 void *resp) 371 { 372 __be32 nfserr; 373 374 dprintk("nfsd: RENAME %s %.*s -> \n", 375 SVCFH_fmt(&argp->ffh), argp->flen, argp->fname); 376 dprintk("nfsd: -> %s %.*s\n", 377 SVCFH_fmt(&argp->tfh), argp->tlen, argp->tname); 378 379 nfserr = nfsd_rename(rqstp, &argp->ffh, argp->fname, argp->flen, 380 &argp->tfh, argp->tname, argp->tlen); 381 fh_put(&argp->ffh); 382 fh_put(&argp->tfh); 383 return nfserr; 384 } 385 386 static __be32 387 nfsd_proc_link(struct svc_rqst *rqstp, struct nfsd_linkargs *argp, 388 void *resp) 389 { 390 __be32 nfserr; 391 392 dprintk("nfsd: LINK %s ->\n", 393 SVCFH_fmt(&argp->ffh)); 394 dprintk("nfsd: %s %.*s\n", 395 SVCFH_fmt(&argp->tfh), 396 argp->tlen, 397 argp->tname); 398 399 nfserr = nfsd_link(rqstp, &argp->tfh, argp->tname, argp->tlen, 400 &argp->ffh); 401 fh_put(&argp->ffh); 402 fh_put(&argp->tfh); 403 return nfserr; 404 } 405 406 static __be32 407 nfsd_proc_symlink(struct svc_rqst *rqstp, struct nfsd_symlinkargs *argp, 408 void *resp) 409 { 410 struct svc_fh newfh; 411 __be32 nfserr; 412 413 dprintk("nfsd: SYMLINK %s %.*s -> %.*s\n", 414 SVCFH_fmt(&argp->ffh), argp->flen, argp->fname, 415 argp->tlen, argp->tname); 416 417 fh_init(&newfh, NFS_FHSIZE); 418 /* 419 * Create the link, look up new file and set attrs. 420 */ 421 nfserr = nfsd_symlink(rqstp, &argp->ffh, argp->fname, argp->flen, 422 argp->tname, argp->tlen, 423 &newfh, &argp->attrs); 424 425 426 fh_put(&argp->ffh); 427 fh_put(&newfh); 428 return nfserr; 429 } 430 431 /* 432 * Make directory. This operation is not idempotent. 433 * N.B. After this call resp->fh needs an fh_put 434 */ 435 static __be32 436 nfsd_proc_mkdir(struct svc_rqst *rqstp, struct nfsd_createargs *argp, 437 struct nfsd_diropres *resp) 438 { 439 __be32 nfserr; 440 441 dprintk("nfsd: MKDIR %s %.*s\n", SVCFH_fmt(&argp->fh), argp->len, argp->name); 442 443 if (resp->fh.fh_dentry) { 444 printk(KERN_WARNING 445 "nfsd_proc_mkdir: response already verified??\n"); 446 } 447 448 argp->attrs.ia_valid &= ~ATTR_SIZE; 449 fh_init(&resp->fh, NFS_FHSIZE); 450 nfserr = nfsd_create(rqstp, &argp->fh, argp->name, argp->len, 451 &argp->attrs, S_IFDIR, 0, &resp->fh); 452 fh_put(&argp->fh); 453 return nfsd_return_dirop(nfserr, resp); 454 } 455 456 /* 457 * Remove a directory 458 */ 459 static __be32 460 nfsd_proc_rmdir(struct svc_rqst *rqstp, struct nfsd_diropargs *argp, 461 void *resp) 462 { 463 __be32 nfserr; 464 465 dprintk("nfsd: RMDIR %s %.*s\n", SVCFH_fmt(&argp->fh), argp->len, argp->name); 466 467 nfserr = nfsd_unlink(rqstp, &argp->fh, S_IFDIR, argp->name, argp->len); 468 fh_put(&argp->fh); 469 return nfserr; 470 } 471 472 /* 473 * Read a portion of a directory. 474 */ 475 static __be32 476 nfsd_proc_readdir(struct svc_rqst *rqstp, struct nfsd_readdirargs *argp, 477 struct nfsd_readdirres *resp) 478 { 479 int count; 480 __be32 nfserr; 481 loff_t offset; 482 483 dprintk("nfsd: READDIR %s %d bytes at %d\n", 484 SVCFH_fmt(&argp->fh), 485 argp->count, argp->cookie); 486 487 /* Shrink to the client read size */ 488 count = (argp->count >> 2) - 2; 489 490 /* Make sure we've room for the NULL ptr & eof flag */ 491 count -= 2; 492 if (count < 0) 493 count = 0; 494 495 resp->buffer = argp->buffer; 496 resp->offset = NULL; 497 resp->buflen = count; 498 resp->common.err = nfs_ok; 499 /* Read directory and encode entries on the fly */ 500 offset = argp->cookie; 501 nfserr = nfsd_readdir(rqstp, &argp->fh, &offset, 502 &resp->common, nfssvc_encode_entry); 503 504 resp->count = resp->buffer - argp->buffer; 505 if (resp->offset) 506 *resp->offset = htonl(offset); 507 508 fh_put(&argp->fh); 509 return nfserr; 510 } 511 512 /* 513 * Get file system info 514 */ 515 static __be32 516 nfsd_proc_statfs(struct svc_rqst * rqstp, struct nfsd_fhandle *argp, 517 struct nfsd_statfsres *resp) 518 { 519 __be32 nfserr; 520 521 dprintk("nfsd: STATFS %s\n", SVCFH_fmt(&argp->fh)); 522 523 nfserr = nfsd_statfs(rqstp, &argp->fh, &resp->stats); 524 fh_put(&argp->fh); 525 return nfserr; 526 } 527 528 /* 529 * NFSv2 Server procedures. 530 * Only the results of non-idempotent operations are cached. 531 */ 532 #define nfsd_proc_none NULL 533 #define nfssvc_release_none NULL 534 struct nfsd_void { int dummy; }; 535 536 #define PROC(name, argt, rest, relt, cache, respsize) \ 537 { (svc_procfunc) nfsd_proc_##name, \ 538 (kxdrproc_t) nfssvc_decode_##argt, \ 539 (kxdrproc_t) nfssvc_encode_##rest, \ 540 (kxdrproc_t) nfssvc_release_##relt, \ 541 sizeof(struct nfsd_##argt), \ 542 sizeof(struct nfsd_##rest), \ 543 0, \ 544 cache, \ 545 respsize, \ 546 } 547 548 #define ST 1 /* status */ 549 #define FH 8 /* filehandle */ 550 #define AT 18 /* attributes */ 551 552 static struct svc_procedure nfsd_procedures2[18] = { 553 PROC(null, void, void, none, RC_NOCACHE, ST), 554 PROC(getattr, fhandle, attrstat, fhandle, RC_NOCACHE, ST+AT), 555 PROC(setattr, sattrargs, attrstat, fhandle, RC_REPLBUFF, ST+AT), 556 PROC(none, void, void, none, RC_NOCACHE, ST), 557 PROC(lookup, diropargs, diropres, fhandle, RC_NOCACHE, ST+FH+AT), 558 PROC(readlink, readlinkargs, readlinkres, none, RC_NOCACHE, ST+1+NFS_MAXPATHLEN/4), 559 PROC(read, readargs, readres, fhandle, RC_NOCACHE, ST+AT+1+NFSSVC_MAXBLKSIZE_V2/4), 560 PROC(none, void, void, none, RC_NOCACHE, ST), 561 PROC(write, writeargs, attrstat, fhandle, RC_REPLBUFF, ST+AT), 562 PROC(create, createargs, diropres, fhandle, RC_REPLBUFF, ST+FH+AT), 563 PROC(remove, diropargs, void, none, RC_REPLSTAT, ST), 564 PROC(rename, renameargs, void, none, RC_REPLSTAT, ST), 565 PROC(link, linkargs, void, none, RC_REPLSTAT, ST), 566 PROC(symlink, symlinkargs, void, none, RC_REPLSTAT, ST), 567 PROC(mkdir, createargs, diropres, fhandle, RC_REPLBUFF, ST+FH+AT), 568 PROC(rmdir, diropargs, void, none, RC_REPLSTAT, ST), 569 PROC(readdir, readdirargs, readdirres, none, RC_NOCACHE, 0), 570 PROC(statfs, fhandle, statfsres, none, RC_NOCACHE, ST+5), 571 }; 572 573 574 struct svc_version nfsd_version2 = { 575 .vs_vers = 2, 576 .vs_nproc = 18, 577 .vs_proc = nfsd_procedures2, 578 .vs_dispatch = nfsd_dispatch, 579 .vs_xdrsize = NFS2_SVC_XDRSIZE, 580 }; 581 582 /* 583 * Map errnos to NFS errnos. 584 */ 585 __be32 586 nfserrno (int errno) 587 { 588 static struct { 589 __be32 nfserr; 590 int syserr; 591 } nfs_errtbl[] = { 592 { nfs_ok, 0 }, 593 { nfserr_perm, -EPERM }, 594 { nfserr_noent, -ENOENT }, 595 { nfserr_io, -EIO }, 596 { nfserr_nxio, -ENXIO }, 597 { nfserr_acces, -EACCES }, 598 { nfserr_exist, -EEXIST }, 599 { nfserr_xdev, -EXDEV }, 600 { nfserr_mlink, -EMLINK }, 601 { nfserr_nodev, -ENODEV }, 602 { nfserr_notdir, -ENOTDIR }, 603 { nfserr_isdir, -EISDIR }, 604 { nfserr_inval, -EINVAL }, 605 { nfserr_fbig, -EFBIG }, 606 { nfserr_nospc, -ENOSPC }, 607 { nfserr_rofs, -EROFS }, 608 { nfserr_mlink, -EMLINK }, 609 { nfserr_nametoolong, -ENAMETOOLONG }, 610 { nfserr_notempty, -ENOTEMPTY }, 611 #ifdef EDQUOT 612 { nfserr_dquot, -EDQUOT }, 613 #endif 614 { nfserr_stale, -ESTALE }, 615 { nfserr_jukebox, -ETIMEDOUT }, 616 { nfserr_dropit, -EAGAIN }, 617 { nfserr_dropit, -ENOMEM }, 618 { nfserr_badname, -ESRCH }, 619 { nfserr_io, -ETXTBSY }, 620 { nfserr_notsupp, -EOPNOTSUPP }, 621 }; 622 int i; 623 624 for (i = 0; i < ARRAY_SIZE(nfs_errtbl); i++) { 625 if (nfs_errtbl[i].syserr == errno) 626 return nfs_errtbl[i].nfserr; 627 } 628 printk (KERN_INFO "nfsd: non-standard errno: %d\n", errno); 629 return nfserr_io; 630 } 631 632