4 * Copyright (C) 1992, 1993, 1994 Rick Sladkey
6 * OS-independent nfs remote procedure call functions
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
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.
16 * Also, the code currently doesn't check the size of the packet, when
17 * it decodes the packet.
19 * Feel free to fix it and mail me the diffs if it worries you.
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
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
30 #include <linux/types.h>
31 #include <linux/param.h>
32 #include <linux/slab.h>
33 #include <linux/time.h>
35 #include <linux/utsname.h>
36 #include <linux/errno.h>
37 #include <linux/string.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>
48 #define NFSDBG_FACILITY NFSDBG_PROC
50 extern struct rpc_procinfo nfs_procedures[];
52 static struct rpc_cred *
53 nfs_cred(struct inode *inode, struct file *filp)
55 struct rpc_cred *cred = NULL;
58 cred = (struct rpc_cred *)filp->private_data;
60 cred = NFS_I(inode)->mm_cred;
65 * Bare-bones access to getattr: this is for nfs_read_super.
68 nfs_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
69 struct nfs_fsinfo *info)
71 struct nfs_fattr *fattr = info->fattr;
72 struct nfs2_fsstat fsinfo;
75 dprintk("%s: call getattr\n", __FUNCTION__);
77 status = rpc_call(server->client_sys, NFSPROC_GETATTR, fhandle, fattr, 0);
78 dprintk("%s: reply getattr %d\n", __FUNCTION__, status);
81 dprintk("%s: call statfs\n", __FUNCTION__);
82 status = rpc_call(server->client_sys, NFSPROC_STATFS, fhandle, &fsinfo, 0);
83 dprintk("%s: reply statfs %d\n", __FUNCTION__, status);
86 info->rtmax = NFS_MAXDATA;
87 info->rtpref = fsinfo.tsize;
88 info->rtmult = fsinfo.bsize;
89 info->wtmax = NFS_MAXDATA;
90 info->wtpref = fsinfo.tsize;
91 info->wtmult = fsinfo.bsize;
92 info->dtpref = fsinfo.tsize;
93 info->maxfilesize = 0x7FFFFFFF;
99 * One function for each procedure in the NFS protocol.
102 nfs_proc_getattr(struct inode *inode, struct nfs_fattr *fattr)
106 dprintk("NFS call getattr\n");
108 status = rpc_call(NFS_CLIENT(inode), NFSPROC_GETATTR,
109 NFS_FH(inode), fattr, 0);
110 dprintk("NFS reply getattr\n");
115 nfs_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
118 struct inode *inode = dentry->d_inode;
119 struct nfs_sattrargs arg = {
125 dprintk("NFS call setattr\n");
127 status = rpc_call(NFS_CLIENT(inode), NFSPROC_SETATTR, &arg, fattr, 0);
128 dprintk("NFS reply setattr\n");
133 nfs_proc_lookup(struct inode *dir, struct qstr *name,
134 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
136 struct nfs_diropargs arg = {
141 struct nfs_diropok res = {
147 dprintk("NFS call lookup %s\n", name->name);
149 status = rpc_call(NFS_CLIENT(dir), NFSPROC_LOOKUP, &arg, &res, 0);
150 dprintk("NFS reply lookup: %d\n", status);
155 nfs_proc_readlink(struct inode *inode, struct page *page)
157 struct nfs_readlinkargs args = {
159 .count = PAGE_CACHE_SIZE,
164 dprintk("NFS call readlink\n");
165 status = rpc_call(NFS_CLIENT(inode), NFSPROC_READLINK, &args, NULL, 0);
166 dprintk("NFS reply readlink: %d\n", status);
171 nfs_proc_read(struct nfs_read_data *rdata, struct file *filp)
173 int flags = rdata->flags;
174 struct inode * inode = rdata->inode;
175 struct nfs_fattr * fattr = rdata->res.fattr;
176 struct rpc_message msg = {
177 .rpc_proc = &nfs_procedures[NFSPROC_READ],
178 .rpc_argp = &rdata->args,
179 .rpc_resp = &rdata->res,
183 dprintk("NFS call read %d @ %Ld\n", rdata->args.count,
184 (long long) rdata->args.offset);
186 msg.rpc_cred = nfs_cred(inode, filp);
187 status = rpc_call_sync(NFS_CLIENT(inode), &msg, flags);
190 nfs_refresh_inode(inode, fattr);
191 /* Emulate the eof flag, which isn't normally needed in NFSv2
192 * as it is guaranteed to always return the file attributes
194 if (rdata->args.offset + rdata->args.count >= fattr->size)
197 dprintk("NFS reply read: %d\n", status);
202 nfs_proc_write(struct nfs_write_data *wdata, struct file *filp)
204 int flags = wdata->flags;
205 struct inode * inode = wdata->inode;
206 struct nfs_fattr * fattr = wdata->res.fattr;
207 struct rpc_message msg = {
208 .rpc_proc = &nfs_procedures[NFSPROC_WRITE],
209 .rpc_argp = &wdata->args,
210 .rpc_resp = &wdata->res,
214 dprintk("NFS call write %d @ %Ld\n", wdata->args.count,
215 (long long) wdata->args.offset);
217 msg.rpc_cred = nfs_cred(inode, filp);
218 status = rpc_call_sync(NFS_CLIENT(inode), &msg, flags);
220 nfs_refresh_inode(inode, fattr);
221 wdata->res.count = wdata->args.count;
222 wdata->verf.committed = NFS_FILE_SYNC;
224 dprintk("NFS reply write: %d\n", status);
225 return status < 0? status : wdata->res.count;
228 static struct inode *
229 nfs_proc_create(struct inode *dir, struct qstr *name, struct iattr *sattr,
232 struct nfs_fh fhandle;
233 struct nfs_fattr fattr;
234 struct nfs_createargs arg = {
240 struct nfs_diropok res = {
247 memset(&fattr, 0, sizeof(struct nfs_fattr));
250 dprintk("NFS call create %s\n", name->name);
251 status = rpc_call(NFS_CLIENT(dir), NFSPROC_CREATE, &arg, &res, 0);
252 dprintk("NFS reply create: %d\n", status);
255 inode = nfs_fhget(dir->i_sb, &fhandle, &fattr);
260 return ERR_PTR(status);
264 * In NFSv2, mknod is grafted onto the create call.
267 nfs_proc_mknod(struct inode *dir, struct qstr *name, struct iattr *sattr,
268 dev_t rdev, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
270 struct nfs_createargs arg = {
276 struct nfs_diropok res = {
282 dprintk("NFS call mknod %s\n", name->name);
284 mode = sattr->ia_mode;
285 if (S_ISFIFO(mode)) {
286 sattr->ia_mode = (mode & ~S_IFMT) | S_IFCHR;
287 sattr->ia_valid &= ~ATTR_SIZE;
288 } else if (S_ISCHR(mode) || S_ISBLK(mode)) {
289 sattr->ia_valid |= ATTR_SIZE;
290 sattr->ia_size = new_encode_dev(rdev);/* get out your barf bag */
294 status = rpc_call(NFS_CLIENT(dir), NFSPROC_CREATE, &arg, &res, 0);
296 if (status == -EINVAL && S_ISFIFO(mode)) {
297 sattr->ia_mode = mode;
299 status = rpc_call(NFS_CLIENT(dir), NFSPROC_CREATE, &arg, &res, 0);
301 dprintk("NFS reply mknod: %d\n", status);
306 nfs_proc_remove(struct inode *dir, struct qstr *name)
308 struct nfs_diropargs arg = {
313 struct rpc_message msg = {
314 .rpc_proc = &nfs_procedures[NFSPROC_REMOVE],
321 dprintk("NFS call remove %s\n", name->name);
322 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
324 dprintk("NFS reply remove: %d\n", status);
329 nfs_proc_unlink_setup(struct rpc_message *msg, struct dentry *dir, struct qstr *name)
331 struct nfs_diropargs *arg;
333 arg = (struct nfs_diropargs *)kmalloc(sizeof(*arg), GFP_KERNEL);
336 arg->fh = NFS_FH(dir->d_inode);
337 arg->name = name->name;
338 arg->len = name->len;
339 msg->rpc_proc = &nfs_procedures[NFSPROC_REMOVE];
345 nfs_proc_unlink_done(struct dentry *dir, struct rpc_task *task)
347 struct rpc_message *msg = &task->tk_msg;
350 kfree(msg->rpc_argp);
355 nfs_proc_rename(struct inode *old_dir, struct qstr *old_name,
356 struct inode *new_dir, struct qstr *new_name)
358 struct nfs_renameargs arg = {
359 .fromfh = NFS_FH(old_dir),
360 .fromname = old_name->name,
361 .fromlen = old_name->len,
362 .tofh = NFS_FH(new_dir),
363 .toname = new_name->name,
364 .tolen = new_name->len
368 dprintk("NFS call rename %s -> %s\n", old_name->name, new_name->name);
369 status = rpc_call(NFS_CLIENT(old_dir), NFSPROC_RENAME, &arg, NULL, 0);
370 dprintk("NFS reply rename: %d\n", status);
375 nfs_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
377 struct nfs_linkargs arg = {
378 .fromfh = NFS_FH(inode),
380 .toname = name->name,
385 dprintk("NFS call link %s\n", name->name);
386 status = rpc_call(NFS_CLIENT(inode), NFSPROC_LINK, &arg, NULL, 0);
387 dprintk("NFS reply link: %d\n", status);
392 nfs_proc_symlink(struct inode *dir, struct qstr *name, struct qstr *path,
393 struct iattr *sattr, struct nfs_fh *fhandle,
394 struct nfs_fattr *fattr)
396 struct nfs_symlinkargs arg = {
397 .fromfh = NFS_FH(dir),
398 .fromname = name->name,
399 .fromlen = name->len,
400 .topath = path->name,
406 dprintk("NFS call symlink %s -> %s\n", name->name, path->name);
408 status = rpc_call(NFS_CLIENT(dir), NFSPROC_SYMLINK, &arg, NULL, 0);
409 dprintk("NFS reply symlink: %d\n", status);
414 nfs_proc_mkdir(struct inode *dir, struct qstr *name, struct iattr *sattr,
415 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
417 struct nfs_createargs arg = {
423 struct nfs_diropok res = {
429 dprintk("NFS call mkdir %s\n", name->name);
431 status = rpc_call(NFS_CLIENT(dir), NFSPROC_MKDIR, &arg, &res, 0);
432 dprintk("NFS reply mkdir: %d\n", status);
437 nfs_proc_rmdir(struct inode *dir, struct qstr *name)
439 struct nfs_diropargs arg = {
446 dprintk("NFS call rmdir %s\n", name->name);
447 status = rpc_call(NFS_CLIENT(dir), NFSPROC_RMDIR, &arg, NULL, 0);
448 dprintk("NFS reply rmdir: %d\n", status);
453 * The READDIR implementation is somewhat hackish - we pass a temporary
454 * buffer to the encode function, which installs it in the receive
455 * the receive iovec. The decode function just parses the reply to make
456 * sure it is syntactically correct; the entries itself are decoded
457 * from nfs_readdir by calling the decode_entry function directly.
460 nfs_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
461 u64 cookie, struct page *page, unsigned int count, int plus)
463 struct inode *dir = dentry->d_inode;
464 struct nfs_readdirargs arg = {
470 struct rpc_message msg = {
471 .rpc_proc = &nfs_procedures[NFSPROC_READDIR],
480 dprintk("NFS call readdir %d\n", (unsigned int)cookie);
481 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
483 dprintk("NFS reply readdir: %d\n", status);
489 nfs_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
490 struct nfs_fsstat *stat)
492 struct nfs2_fsstat fsinfo;
495 dprintk("NFS call statfs\n");
496 stat->fattr->valid = 0;
497 status = rpc_call(server->client, NFSPROC_STATFS, fhandle, &fsinfo, 0);
498 dprintk("NFS reply statfs: %d\n", status);
501 stat->tbytes = (u64)fsinfo.blocks * fsinfo.bsize;
502 stat->fbytes = (u64)fsinfo.bfree * fsinfo.bsize;
503 stat->abytes = (u64)fsinfo.bavail * fsinfo.bsize;
512 nfs_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
513 struct nfs_fsinfo *info)
515 struct nfs2_fsstat fsinfo;
518 dprintk("NFS call fsinfo\n");
519 info->fattr->valid = 0;
520 status = rpc_call(server->client, NFSPROC_STATFS, fhandle, &fsinfo, 0);
521 dprintk("NFS reply fsinfo: %d\n", status);
524 info->rtmax = NFS_MAXDATA;
525 info->rtpref = fsinfo.tsize;
526 info->rtmult = fsinfo.bsize;
527 info->wtmax = NFS_MAXDATA;
528 info->wtpref = fsinfo.tsize;
529 info->wtmult = fsinfo.bsize;
530 info->dtpref = fsinfo.tsize;
531 info->maxfilesize = 0x7FFFFFFF;
532 info->lease_time = 0;
538 nfs_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
539 struct nfs_pathconf *info)
542 info->max_namelen = NFS2_MAXNAMLEN;
546 extern u32 * nfs_decode_dirent(u32 *, struct nfs_entry *, int);
549 nfs_read_done(struct rpc_task *task)
551 struct nfs_read_data *data = (struct nfs_read_data *) task->tk_calldata;
553 if (task->tk_status >= 0) {
554 nfs_refresh_inode(data->inode, data->res.fattr);
555 /* Emulate the eof flag, which isn't normally needed in NFSv2
556 * as it is guaranteed to always return the file attributes
558 if (data->args.offset + data->args.count >= data->res.fattr->size)
561 nfs_readpage_result(task);
565 nfs_proc_read_setup(struct nfs_read_data *data)
567 struct rpc_task *task = &data->task;
568 struct inode *inode = data->inode;
570 struct rpc_message msg = {
571 .rpc_proc = &nfs_procedures[NFSPROC_READ],
572 .rpc_argp = &data->args,
573 .rpc_resp = &data->res,
574 .rpc_cred = data->cred,
577 /* N.B. Do we need to test? Never called for swapfile inode */
578 flags = RPC_TASK_ASYNC | (IS_SWAPFILE(inode)? NFS_RPC_SWAPFLAGS : 0);
580 /* Finalize the task. */
581 rpc_init_task(task, NFS_CLIENT(inode), nfs_read_done, flags);
582 rpc_call_setup(task, &msg, 0);
586 nfs_write_done(struct rpc_task *task)
588 struct nfs_write_data *data = (struct nfs_write_data *) task->tk_calldata;
590 if (task->tk_status >= 0)
591 nfs_refresh_inode(data->inode, data->res.fattr);
592 nfs_writeback_done(task);
596 nfs_proc_write_setup(struct nfs_write_data *data, int how)
598 struct rpc_task *task = &data->task;
599 struct inode *inode = data->inode;
601 struct rpc_message msg = {
602 .rpc_proc = &nfs_procedures[NFSPROC_WRITE],
603 .rpc_argp = &data->args,
604 .rpc_resp = &data->res,
605 .rpc_cred = data->cred,
608 /* Note: NFSv2 ignores @stable and always uses NFS_FILE_SYNC */
609 data->args.stable = NFS_FILE_SYNC;
611 /* Set the initial flags for the task. */
612 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
614 /* Finalize the task. */
615 rpc_init_task(task, NFS_CLIENT(inode), nfs_write_done, flags);
616 rpc_call_setup(task, &msg, 0);
620 nfs_proc_commit_setup(struct nfs_write_data *data, int how)
626 * Set up the nfspage struct with the right credentials
629 nfs_request_init(struct nfs_page *req, struct file *filp)
631 req->wb_cred = get_rpccred(nfs_cred(req->wb_inode, filp));
635 nfs_request_compatible(struct nfs_page *req, struct file *filp, struct page *page)
637 if (req->wb_file != filp)
639 if (req->wb_page != page)
641 if (req->wb_cred != nfs_file_cred(filp))
647 nfs_proc_lock(struct file *filp, int cmd, struct file_lock *fl)
649 return nlmclnt_proc(filp->f_dentry->d_inode, cmd, fl);
653 struct nfs_rpc_ops nfs_v2_clientops = {
654 .version = 2, /* protocol version */
655 .dentry_ops = &nfs_dentry_operations,
656 .dir_inode_ops = &nfs_dir_inode_operations,
657 .getroot = nfs_proc_get_root,
658 .getattr = nfs_proc_getattr,
659 .setattr = nfs_proc_setattr,
660 .lookup = nfs_proc_lookup,
661 .access = NULL, /* access */
662 .readlink = nfs_proc_readlink,
663 .read = nfs_proc_read,
664 .write = nfs_proc_write,
665 .commit = NULL, /* commit */
666 .create = nfs_proc_create,
667 .remove = nfs_proc_remove,
668 .unlink_setup = nfs_proc_unlink_setup,
669 .unlink_done = nfs_proc_unlink_done,
670 .rename = nfs_proc_rename,
671 .link = nfs_proc_link,
672 .symlink = nfs_proc_symlink,
673 .mkdir = nfs_proc_mkdir,
674 .rmdir = nfs_proc_rmdir,
675 .readdir = nfs_proc_readdir,
676 .mknod = nfs_proc_mknod,
677 .statfs = nfs_proc_statfs,
678 .fsinfo = nfs_proc_fsinfo,
679 .pathconf = nfs_proc_pathconf,
680 .decode_dirent = nfs_decode_dirent,
681 .read_setup = nfs_proc_read_setup,
682 .write_setup = nfs_proc_write_setup,
683 .commit_setup = nfs_proc_commit_setup,
684 .file_open = nfs_open,
685 .file_release = nfs_release,
686 .request_init = nfs_request_init,
687 .request_compatible = nfs_request_compatible,
688 .lock = nfs_proc_lock,