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