1 /* 2 * linux/fs/nfs/proc.c 3 * 4 * Copyright (C) 1992, 1993, 1994 Rick Sladkey 5 * 6 * OS-independent nfs remote procedure call functions 7 * 8 * Tuned by Alan Cox <A.Cox@swansea.ac.uk> for >3K buffers 9 * so at last we can have decent(ish) throughput off a 10 * Sun server. 11 * 12 * Coding optimized and cleaned up by Florian La Roche. 13 * Note: Error returns are optimized for NFS_OK, which isn't translated via 14 * nfs_stat_to_errno(), but happens to be already the right return code. 15 * 16 * Also, the code currently doesn't check the size of the packet, when 17 * it decodes the packet. 18 * 19 * Feel free to fix it and mail me the diffs if it worries you. 20 * 21 * Completely rewritten to support the new RPC call interface; 22 * rewrote and moved the entire XDR stuff to xdr.c 23 * --Olaf Kirch June 1996 24 * 25 * The code below initializes all auto variables explicitly, otherwise 26 * it will fail to work as a module (gcc generates a memset call for an 27 * incomplete struct). 28 */ 29 30 #include <linux/types.h> 31 #include <linux/param.h> 32 #include <linux/slab.h> 33 #include <linux/time.h> 34 #include <linux/mm.h> 35 #include <linux/utsname.h> 36 #include <linux/errno.h> 37 #include <linux/string.h> 38 #include <linux/in.h> 39 #include <linux/pagemap.h> 40 #include <linux/sunrpc/clnt.h> 41 #include <linux/nfs.h> 42 #include <linux/nfs2.h> 43 #include <linux/nfs_fs.h> 44 #include <linux/nfs_page.h> 45 #include <linux/lockd/bind.h> 46 #include <linux/smp_lock.h> 47 #include "internal.h" 48 49 #define NFSDBG_FACILITY NFSDBG_PROC 50 51 /* 52 * Bare-bones access to getattr: this is for nfs_read_super. 53 */ 54 static int 55 nfs_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle, 56 struct nfs_fsinfo *info) 57 { 58 struct nfs_fattr *fattr = info->fattr; 59 struct nfs2_fsstat fsinfo; 60 struct rpc_message msg = { 61 .rpc_proc = &nfs_procedures[NFSPROC_GETATTR], 62 .rpc_argp = fhandle, 63 .rpc_resp = fattr, 64 }; 65 int status; 66 67 dprintk("%s: call getattr\n", __FUNCTION__); 68 nfs_fattr_init(fattr); 69 status = rpc_call_sync(server->nfs_client->cl_rpcclient, &msg, 0); 70 dprintk("%s: reply getattr: %d\n", __FUNCTION__, status); 71 if (status) 72 return status; 73 dprintk("%s: call statfs\n", __FUNCTION__); 74 msg.rpc_proc = &nfs_procedures[NFSPROC_STATFS]; 75 msg.rpc_resp = &fsinfo; 76 status = rpc_call_sync(server->nfs_client->cl_rpcclient, &msg, 0); 77 dprintk("%s: reply statfs: %d\n", __FUNCTION__, status); 78 if (status) 79 return status; 80 info->rtmax = NFS_MAXDATA; 81 info->rtpref = fsinfo.tsize; 82 info->rtmult = fsinfo.bsize; 83 info->wtmax = NFS_MAXDATA; 84 info->wtpref = fsinfo.tsize; 85 info->wtmult = fsinfo.bsize; 86 info->dtpref = fsinfo.tsize; 87 info->maxfilesize = 0x7FFFFFFF; 88 info->lease_time = 0; 89 return 0; 90 } 91 92 /* 93 * One function for each procedure in the NFS protocol. 94 */ 95 static int 96 nfs_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, 97 struct nfs_fattr *fattr) 98 { 99 struct rpc_message msg = { 100 .rpc_proc = &nfs_procedures[NFSPROC_GETATTR], 101 .rpc_argp = fhandle, 102 .rpc_resp = fattr, 103 }; 104 int status; 105 106 dprintk("NFS call getattr\n"); 107 nfs_fattr_init(fattr); 108 status = rpc_call_sync(server->client, &msg, 0); 109 dprintk("NFS reply getattr: %d\n", status); 110 return status; 111 } 112 113 static int 114 nfs_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr, 115 struct iattr *sattr) 116 { 117 struct inode *inode = dentry->d_inode; 118 struct nfs_sattrargs arg = { 119 .fh = NFS_FH(inode), 120 .sattr = sattr 121 }; 122 struct rpc_message msg = { 123 .rpc_proc = &nfs_procedures[NFSPROC_SETATTR], 124 .rpc_argp = &arg, 125 .rpc_resp = fattr, 126 }; 127 int status; 128 129 /* Mask out the non-modebit related stuff from attr->ia_mode */ 130 sattr->ia_mode &= S_IALLUGO; 131 132 dprintk("NFS call setattr\n"); 133 nfs_fattr_init(fattr); 134 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0); 135 if (status == 0) 136 nfs_setattr_update_inode(inode, sattr); 137 dprintk("NFS reply setattr: %d\n", status); 138 return status; 139 } 140 141 static int 142 nfs_proc_lookup(struct inode *dir, struct qstr *name, 143 struct nfs_fh *fhandle, struct nfs_fattr *fattr) 144 { 145 struct nfs_diropargs arg = { 146 .fh = NFS_FH(dir), 147 .name = name->name, 148 .len = name->len 149 }; 150 struct nfs_diropok res = { 151 .fh = fhandle, 152 .fattr = fattr 153 }; 154 struct rpc_message msg = { 155 .rpc_proc = &nfs_procedures[NFSPROC_LOOKUP], 156 .rpc_argp = &arg, 157 .rpc_resp = &res, 158 }; 159 int status; 160 161 dprintk("NFS call lookup %s\n", name->name); 162 nfs_fattr_init(fattr); 163 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0); 164 dprintk("NFS reply lookup: %d\n", status); 165 return status; 166 } 167 168 static int nfs_proc_readlink(struct inode *inode, struct page *page, 169 unsigned int pgbase, unsigned int pglen) 170 { 171 struct nfs_readlinkargs args = { 172 .fh = NFS_FH(inode), 173 .pgbase = pgbase, 174 .pglen = pglen, 175 .pages = &page 176 }; 177 struct rpc_message msg = { 178 .rpc_proc = &nfs_procedures[NFSPROC_READLINK], 179 .rpc_argp = &args, 180 }; 181 int status; 182 183 dprintk("NFS call readlink\n"); 184 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0); 185 dprintk("NFS reply readlink: %d\n", status); 186 return status; 187 } 188 189 static int 190 nfs_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr, 191 int flags, struct nameidata *nd) 192 { 193 struct nfs_fh fhandle; 194 struct nfs_fattr fattr; 195 struct nfs_createargs arg = { 196 .fh = NFS_FH(dir), 197 .name = dentry->d_name.name, 198 .len = dentry->d_name.len, 199 .sattr = sattr 200 }; 201 struct nfs_diropok res = { 202 .fh = &fhandle, 203 .fattr = &fattr 204 }; 205 struct rpc_message msg = { 206 .rpc_proc = &nfs_procedures[NFSPROC_CREATE], 207 .rpc_argp = &arg, 208 .rpc_resp = &res, 209 }; 210 int status; 211 212 nfs_fattr_init(&fattr); 213 dprintk("NFS call create %s\n", dentry->d_name.name); 214 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0); 215 if (status == 0) 216 status = nfs_instantiate(dentry, &fhandle, &fattr); 217 dprintk("NFS reply create: %d\n", status); 218 return status; 219 } 220 221 /* 222 * In NFSv2, mknod is grafted onto the create call. 223 */ 224 static int 225 nfs_proc_mknod(struct inode *dir, struct dentry *dentry, struct iattr *sattr, 226 dev_t rdev) 227 { 228 struct nfs_fh fhandle; 229 struct nfs_fattr fattr; 230 struct nfs_createargs arg = { 231 .fh = NFS_FH(dir), 232 .name = dentry->d_name.name, 233 .len = dentry->d_name.len, 234 .sattr = sattr 235 }; 236 struct nfs_diropok res = { 237 .fh = &fhandle, 238 .fattr = &fattr 239 }; 240 struct rpc_message msg = { 241 .rpc_proc = &nfs_procedures[NFSPROC_CREATE], 242 .rpc_argp = &arg, 243 .rpc_resp = &res, 244 }; 245 int status, mode; 246 247 dprintk("NFS call mknod %s\n", dentry->d_name.name); 248 249 mode = sattr->ia_mode; 250 if (S_ISFIFO(mode)) { 251 sattr->ia_mode = (mode & ~S_IFMT) | S_IFCHR; 252 sattr->ia_valid &= ~ATTR_SIZE; 253 } else if (S_ISCHR(mode) || S_ISBLK(mode)) { 254 sattr->ia_valid |= ATTR_SIZE; 255 sattr->ia_size = new_encode_dev(rdev);/* get out your barf bag */ 256 } 257 258 nfs_fattr_init(&fattr); 259 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0); 260 nfs_mark_for_revalidate(dir); 261 262 if (status == -EINVAL && S_ISFIFO(mode)) { 263 sattr->ia_mode = mode; 264 nfs_fattr_init(&fattr); 265 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0); 266 } 267 if (status == 0) 268 status = nfs_instantiate(dentry, &fhandle, &fattr); 269 dprintk("NFS reply mknod: %d\n", status); 270 return status; 271 } 272 273 static int 274 nfs_proc_remove(struct inode *dir, struct qstr *name) 275 { 276 struct nfs_diropargs arg = { 277 .fh = NFS_FH(dir), 278 .name = name->name, 279 .len = name->len 280 }; 281 struct rpc_message msg = { 282 .rpc_proc = &nfs_procedures[NFSPROC_REMOVE], 283 .rpc_argp = &arg, 284 }; 285 int status; 286 287 dprintk("NFS call remove %s\n", name->name); 288 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0); 289 nfs_mark_for_revalidate(dir); 290 291 dprintk("NFS reply remove: %d\n", status); 292 return status; 293 } 294 295 static int 296 nfs_proc_unlink_setup(struct rpc_message *msg, struct dentry *dir, struct qstr *name) 297 { 298 struct nfs_diropargs *arg; 299 300 arg = kmalloc(sizeof(*arg), GFP_KERNEL); 301 if (!arg) 302 return -ENOMEM; 303 arg->fh = NFS_FH(dir->d_inode); 304 arg->name = name->name; 305 arg->len = name->len; 306 msg->rpc_proc = &nfs_procedures[NFSPROC_REMOVE]; 307 msg->rpc_argp = arg; 308 return 0; 309 } 310 311 static int 312 nfs_proc_unlink_done(struct dentry *dir, struct rpc_task *task) 313 { 314 struct rpc_message *msg = &task->tk_msg; 315 316 if (msg->rpc_argp) { 317 nfs_mark_for_revalidate(dir->d_inode); 318 kfree(msg->rpc_argp); 319 } 320 return 0; 321 } 322 323 static int 324 nfs_proc_rename(struct inode *old_dir, struct qstr *old_name, 325 struct inode *new_dir, struct qstr *new_name) 326 { 327 struct nfs_renameargs arg = { 328 .fromfh = NFS_FH(old_dir), 329 .fromname = old_name->name, 330 .fromlen = old_name->len, 331 .tofh = NFS_FH(new_dir), 332 .toname = new_name->name, 333 .tolen = new_name->len 334 }; 335 struct rpc_message msg = { 336 .rpc_proc = &nfs_procedures[NFSPROC_RENAME], 337 .rpc_argp = &arg, 338 }; 339 int status; 340 341 dprintk("NFS call rename %s -> %s\n", old_name->name, new_name->name); 342 status = rpc_call_sync(NFS_CLIENT(old_dir), &msg, 0); 343 nfs_mark_for_revalidate(old_dir); 344 nfs_mark_for_revalidate(new_dir); 345 dprintk("NFS reply rename: %d\n", status); 346 return status; 347 } 348 349 static int 350 nfs_proc_link(struct inode *inode, struct inode *dir, struct qstr *name) 351 { 352 struct nfs_linkargs arg = { 353 .fromfh = NFS_FH(inode), 354 .tofh = NFS_FH(dir), 355 .toname = name->name, 356 .tolen = name->len 357 }; 358 struct rpc_message msg = { 359 .rpc_proc = &nfs_procedures[NFSPROC_LINK], 360 .rpc_argp = &arg, 361 }; 362 int status; 363 364 dprintk("NFS call link %s\n", name->name); 365 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0); 366 nfs_mark_for_revalidate(inode); 367 nfs_mark_for_revalidate(dir); 368 dprintk("NFS reply link: %d\n", status); 369 return status; 370 } 371 372 static int 373 nfs_proc_symlink(struct inode *dir, struct dentry *dentry, struct page *page, 374 unsigned int len, struct iattr *sattr) 375 { 376 struct nfs_fh fhandle; 377 struct nfs_fattr fattr; 378 struct nfs_symlinkargs arg = { 379 .fromfh = NFS_FH(dir), 380 .fromname = dentry->d_name.name, 381 .fromlen = dentry->d_name.len, 382 .pages = &page, 383 .pathlen = len, 384 .sattr = sattr 385 }; 386 struct rpc_message msg = { 387 .rpc_proc = &nfs_procedures[NFSPROC_SYMLINK], 388 .rpc_argp = &arg, 389 }; 390 int status; 391 392 if (len > NFS2_MAXPATHLEN) 393 return -ENAMETOOLONG; 394 395 dprintk("NFS call symlink %s\n", dentry->d_name.name); 396 397 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0); 398 nfs_mark_for_revalidate(dir); 399 400 /* 401 * V2 SYMLINK requests don't return any attributes. Setting the 402 * filehandle size to zero indicates to nfs_instantiate that it 403 * should fill in the data with a LOOKUP call on the wire. 404 */ 405 if (status == 0) { 406 nfs_fattr_init(&fattr); 407 fhandle.size = 0; 408 status = nfs_instantiate(dentry, &fhandle, &fattr); 409 } 410 411 dprintk("NFS reply symlink: %d\n", status); 412 return status; 413 } 414 415 static int 416 nfs_proc_mkdir(struct inode *dir, struct dentry *dentry, struct iattr *sattr) 417 { 418 struct nfs_fh fhandle; 419 struct nfs_fattr fattr; 420 struct nfs_createargs arg = { 421 .fh = NFS_FH(dir), 422 .name = dentry->d_name.name, 423 .len = dentry->d_name.len, 424 .sattr = sattr 425 }; 426 struct nfs_diropok res = { 427 .fh = &fhandle, 428 .fattr = &fattr 429 }; 430 struct rpc_message msg = { 431 .rpc_proc = &nfs_procedures[NFSPROC_MKDIR], 432 .rpc_argp = &arg, 433 .rpc_resp = &res, 434 }; 435 int status; 436 437 dprintk("NFS call mkdir %s\n", dentry->d_name.name); 438 nfs_fattr_init(&fattr); 439 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0); 440 nfs_mark_for_revalidate(dir); 441 if (status == 0) 442 status = nfs_instantiate(dentry, &fhandle, &fattr); 443 dprintk("NFS reply mkdir: %d\n", status); 444 return status; 445 } 446 447 static int 448 nfs_proc_rmdir(struct inode *dir, struct qstr *name) 449 { 450 struct nfs_diropargs arg = { 451 .fh = NFS_FH(dir), 452 .name = name->name, 453 .len = name->len 454 }; 455 struct rpc_message msg = { 456 .rpc_proc = &nfs_procedures[NFSPROC_RMDIR], 457 .rpc_argp = &arg, 458 }; 459 int status; 460 461 dprintk("NFS call rmdir %s\n", name->name); 462 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0); 463 nfs_mark_for_revalidate(dir); 464 dprintk("NFS reply rmdir: %d\n", status); 465 return status; 466 } 467 468 /* 469 * The READDIR implementation is somewhat hackish - we pass a temporary 470 * buffer to the encode function, which installs it in the receive 471 * the receive iovec. The decode function just parses the reply to make 472 * sure it is syntactically correct; the entries itself are decoded 473 * from nfs_readdir by calling the decode_entry function directly. 474 */ 475 static int 476 nfs_proc_readdir(struct dentry *dentry, struct rpc_cred *cred, 477 u64 cookie, struct page *page, unsigned int count, int plus) 478 { 479 struct inode *dir = dentry->d_inode; 480 struct nfs_readdirargs arg = { 481 .fh = NFS_FH(dir), 482 .cookie = cookie, 483 .count = count, 484 .pages = &page, 485 }; 486 struct rpc_message msg = { 487 .rpc_proc = &nfs_procedures[NFSPROC_READDIR], 488 .rpc_argp = &arg, 489 .rpc_cred = cred, 490 }; 491 int status; 492 493 dprintk("NFS call readdir %d\n", (unsigned int)cookie); 494 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0); 495 496 dprintk("NFS reply readdir: %d\n", status); 497 return status; 498 } 499 500 static int 501 nfs_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, 502 struct nfs_fsstat *stat) 503 { 504 struct nfs2_fsstat fsinfo; 505 struct rpc_message msg = { 506 .rpc_proc = &nfs_procedures[NFSPROC_STATFS], 507 .rpc_argp = fhandle, 508 .rpc_resp = &fsinfo, 509 }; 510 int status; 511 512 dprintk("NFS call statfs\n"); 513 nfs_fattr_init(stat->fattr); 514 status = rpc_call_sync(server->client, &msg, 0); 515 dprintk("NFS reply statfs: %d\n", status); 516 if (status) 517 goto out; 518 stat->tbytes = (u64)fsinfo.blocks * fsinfo.bsize; 519 stat->fbytes = (u64)fsinfo.bfree * fsinfo.bsize; 520 stat->abytes = (u64)fsinfo.bavail * fsinfo.bsize; 521 stat->tfiles = 0; 522 stat->ffiles = 0; 523 stat->afiles = 0; 524 out: 525 return status; 526 } 527 528 static int 529 nfs_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, 530 struct nfs_fsinfo *info) 531 { 532 struct nfs2_fsstat fsinfo; 533 struct rpc_message msg = { 534 .rpc_proc = &nfs_procedures[NFSPROC_STATFS], 535 .rpc_argp = fhandle, 536 .rpc_resp = &fsinfo, 537 }; 538 int status; 539 540 dprintk("NFS call fsinfo\n"); 541 nfs_fattr_init(info->fattr); 542 status = rpc_call_sync(server->client, &msg, 0); 543 dprintk("NFS reply fsinfo: %d\n", status); 544 if (status) 545 goto out; 546 info->rtmax = NFS_MAXDATA; 547 info->rtpref = fsinfo.tsize; 548 info->rtmult = fsinfo.bsize; 549 info->wtmax = NFS_MAXDATA; 550 info->wtpref = fsinfo.tsize; 551 info->wtmult = fsinfo.bsize; 552 info->dtpref = fsinfo.tsize; 553 info->maxfilesize = 0x7FFFFFFF; 554 info->lease_time = 0; 555 out: 556 return status; 557 } 558 559 static int 560 nfs_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle, 561 struct nfs_pathconf *info) 562 { 563 info->max_link = 0; 564 info->max_namelen = NFS2_MAXNAMLEN; 565 return 0; 566 } 567 568 static int nfs_read_done(struct rpc_task *task, struct nfs_read_data *data) 569 { 570 if (task->tk_status >= 0) { 571 nfs_refresh_inode(data->inode, data->res.fattr); 572 /* Emulate the eof flag, which isn't normally needed in NFSv2 573 * as it is guaranteed to always return the file attributes 574 */ 575 if (data->args.offset + data->args.count >= data->res.fattr->size) 576 data->res.eof = 1; 577 } 578 return 0; 579 } 580 581 static void nfs_proc_read_setup(struct nfs_read_data *data) 582 { 583 struct rpc_message msg = { 584 .rpc_proc = &nfs_procedures[NFSPROC_READ], 585 .rpc_argp = &data->args, 586 .rpc_resp = &data->res, 587 .rpc_cred = data->cred, 588 }; 589 590 rpc_call_setup(&data->task, &msg, 0); 591 } 592 593 static int nfs_write_done(struct rpc_task *task, struct nfs_write_data *data) 594 { 595 if (task->tk_status >= 0) 596 nfs_post_op_update_inode(data->inode, data->res.fattr); 597 return 0; 598 } 599 600 static void nfs_proc_write_setup(struct nfs_write_data *data, int how) 601 { 602 struct rpc_message msg = { 603 .rpc_proc = &nfs_procedures[NFSPROC_WRITE], 604 .rpc_argp = &data->args, 605 .rpc_resp = &data->res, 606 .rpc_cred = data->cred, 607 }; 608 609 /* Note: NFSv2 ignores @stable and always uses NFS_FILE_SYNC */ 610 data->args.stable = NFS_FILE_SYNC; 611 612 /* Finalize the task. */ 613 rpc_call_setup(&data->task, &msg, 0); 614 } 615 616 static void 617 nfs_proc_commit_setup(struct nfs_write_data *data, int how) 618 { 619 BUG(); 620 } 621 622 static int 623 nfs_proc_lock(struct file *filp, int cmd, struct file_lock *fl) 624 { 625 return nlmclnt_proc(filp->f_path.dentry->d_inode, cmd, fl); 626 } 627 628 629 const struct nfs_rpc_ops nfs_v2_clientops = { 630 .version = 2, /* protocol version */ 631 .dentry_ops = &nfs_dentry_operations, 632 .dir_inode_ops = &nfs_dir_inode_operations, 633 .file_inode_ops = &nfs_file_inode_operations, 634 .getroot = nfs_proc_get_root, 635 .getattr = nfs_proc_getattr, 636 .setattr = nfs_proc_setattr, 637 .lookup = nfs_proc_lookup, 638 .access = NULL, /* access */ 639 .readlink = nfs_proc_readlink, 640 .create = nfs_proc_create, 641 .remove = nfs_proc_remove, 642 .unlink_setup = nfs_proc_unlink_setup, 643 .unlink_done = nfs_proc_unlink_done, 644 .rename = nfs_proc_rename, 645 .link = nfs_proc_link, 646 .symlink = nfs_proc_symlink, 647 .mkdir = nfs_proc_mkdir, 648 .rmdir = nfs_proc_rmdir, 649 .readdir = nfs_proc_readdir, 650 .mknod = nfs_proc_mknod, 651 .statfs = nfs_proc_statfs, 652 .fsinfo = nfs_proc_fsinfo, 653 .pathconf = nfs_proc_pathconf, 654 .decode_dirent = nfs_decode_dirent, 655 .read_setup = nfs_proc_read_setup, 656 .read_done = nfs_read_done, 657 .write_setup = nfs_proc_write_setup, 658 .write_done = nfs_write_done, 659 .commit_setup = nfs_proc_commit_setup, 660 .file_open = nfs_open, 661 .file_release = nfs_release, 662 .lock = nfs_proc_lock, 663 }; 664