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