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 dprintk("NFS call create %s\n", name->name);
248 status = rpc_call(NFS_CLIENT(dir), NFSPROC_CREATE, &arg, &res, 0);
249 dprintk("NFS reply create: %d\n", status);
252 inode = nfs_fhget(dir->i_sb, &fhandle, &fattr);
257 return ERR_PTR(status);
261 * In NFSv2, mknod is grafted onto the create call.
264 nfs_proc_mknod(struct inode *dir, struct qstr *name, struct iattr *sattr,
265 dev_t rdev, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
267 struct nfs_createargs arg = {
273 struct nfs_diropok res = {
279 dprintk("NFS call mknod %s\n", name->name);
281 mode = sattr->ia_mode;
282 if (S_ISFIFO(mode)) {
283 sattr->ia_mode = (mode & ~S_IFMT) | S_IFCHR;
284 sattr->ia_valid &= ~ATTR_SIZE;
285 } else if (S_ISCHR(mode) || S_ISBLK(mode)) {
286 sattr->ia_valid |= ATTR_SIZE;
287 sattr->ia_size = new_encode_dev(rdev);/* get out your barf bag */
291 status = rpc_call(NFS_CLIENT(dir), NFSPROC_CREATE, &arg, &res, 0);
293 if (status == -EINVAL && S_ISFIFO(mode)) {
294 sattr->ia_mode = mode;
296 status = rpc_call(NFS_CLIENT(dir), NFSPROC_CREATE, &arg, &res, 0);
298 dprintk("NFS reply mknod: %d\n", status);
303 nfs_proc_remove(struct inode *dir, struct qstr *name)
305 struct nfs_diropargs arg = {
310 struct rpc_message msg = {
311 .rpc_proc = &nfs_procedures[NFSPROC_REMOVE],
318 dprintk("NFS call remove %s\n", name->name);
319 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
321 dprintk("NFS reply remove: %d\n", status);
326 nfs_proc_unlink_setup(struct rpc_message *msg, struct dentry *dir, struct qstr *name)
328 struct nfs_diropargs *arg;
330 arg = (struct nfs_diropargs *)kmalloc(sizeof(*arg), GFP_KERNEL);
333 arg->fh = NFS_FH(dir->d_inode);
334 arg->name = name->name;
335 arg->len = name->len;
336 msg->rpc_proc = &nfs_procedures[NFSPROC_REMOVE];
342 nfs_proc_unlink_done(struct dentry *dir, struct rpc_task *task)
344 struct rpc_message *msg = &task->tk_msg;
347 kfree(msg->rpc_argp);
352 nfs_proc_rename(struct inode *old_dir, struct qstr *old_name,
353 struct inode *new_dir, struct qstr *new_name)
355 struct nfs_renameargs arg = {
356 .fromfh = NFS_FH(old_dir),
357 .fromname = old_name->name,
358 .fromlen = old_name->len,
359 .tofh = NFS_FH(new_dir),
360 .toname = new_name->name,
361 .tolen = new_name->len
365 dprintk("NFS call rename %s -> %s\n", old_name->name, new_name->name);
366 status = rpc_call(NFS_CLIENT(old_dir), NFSPROC_RENAME, &arg, NULL, 0);
367 dprintk("NFS reply rename: %d\n", status);
372 nfs_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
374 struct nfs_linkargs arg = {
375 .fromfh = NFS_FH(inode),
377 .toname = name->name,
382 dprintk("NFS call link %s\n", name->name);
383 status = rpc_call(NFS_CLIENT(inode), NFSPROC_LINK, &arg, NULL, 0);
384 dprintk("NFS reply link: %d\n", status);
389 nfs_proc_symlink(struct inode *dir, struct qstr *name, struct qstr *path,
390 struct iattr *sattr, struct nfs_fh *fhandle,
391 struct nfs_fattr *fattr)
393 struct nfs_symlinkargs arg = {
394 .fromfh = NFS_FH(dir),
395 .fromname = name->name,
396 .fromlen = name->len,
397 .topath = path->name,
403 dprintk("NFS call symlink %s -> %s\n", name->name, path->name);
405 status = rpc_call(NFS_CLIENT(dir), NFSPROC_SYMLINK, &arg, NULL, 0);
406 dprintk("NFS reply symlink: %d\n", status);
411 nfs_proc_mkdir(struct inode *dir, struct qstr *name, struct iattr *sattr,
412 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
414 struct nfs_createargs arg = {
420 struct nfs_diropok res = {
426 dprintk("NFS call mkdir %s\n", name->name);
428 status = rpc_call(NFS_CLIENT(dir), NFSPROC_MKDIR, &arg, &res, 0);
429 dprintk("NFS reply mkdir: %d\n", status);
434 nfs_proc_rmdir(struct inode *dir, struct qstr *name)
436 struct nfs_diropargs arg = {
443 dprintk("NFS call rmdir %s\n", name->name);
444 status = rpc_call(NFS_CLIENT(dir), NFSPROC_RMDIR, &arg, NULL, 0);
445 dprintk("NFS reply rmdir: %d\n", status);
450 * The READDIR implementation is somewhat hackish - we pass a temporary
451 * buffer to the encode function, which installs it in the receive
452 * the receive iovec. The decode function just parses the reply to make
453 * sure it is syntactically correct; the entries itself are decoded
454 * from nfs_readdir by calling the decode_entry function directly.
457 nfs_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
458 u64 cookie, struct page *page, unsigned int count, int plus)
460 struct inode *dir = dentry->d_inode;
461 struct nfs_readdirargs arg = {
467 struct rpc_message msg = {
468 .rpc_proc = &nfs_procedures[NFSPROC_READDIR],
477 dprintk("NFS call readdir %d\n", (unsigned int)cookie);
478 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
480 dprintk("NFS reply readdir: %d\n", status);
486 nfs_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
487 struct nfs_fsstat *stat)
489 struct nfs2_fsstat fsinfo;
492 dprintk("NFS call statfs\n");
493 stat->fattr->valid = 0;
494 status = rpc_call(server->client, NFSPROC_STATFS, fhandle, &fsinfo, 0);
495 dprintk("NFS reply statfs: %d\n", status);
498 stat->tbytes = (u64)fsinfo.blocks * fsinfo.bsize;
499 stat->fbytes = (u64)fsinfo.bfree * fsinfo.bsize;
500 stat->abytes = (u64)fsinfo.bavail * fsinfo.bsize;
509 nfs_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
510 struct nfs_fsinfo *info)
512 struct nfs2_fsstat fsinfo;
515 dprintk("NFS call fsinfo\n");
516 info->fattr->valid = 0;
517 status = rpc_call(server->client, NFSPROC_STATFS, fhandle, &fsinfo, 0);
518 dprintk("NFS reply fsinfo: %d\n", status);
521 info->rtmax = NFS_MAXDATA;
522 info->rtpref = fsinfo.tsize;
523 info->rtmult = fsinfo.bsize;
524 info->wtmax = NFS_MAXDATA;
525 info->wtpref = fsinfo.tsize;
526 info->wtmult = fsinfo.bsize;
527 info->dtpref = fsinfo.tsize;
528 info->maxfilesize = 0x7FFFFFFF;
529 info->lease_time = 0;
535 nfs_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
536 struct nfs_pathconf *info)
539 info->max_namelen = NFS2_MAXNAMLEN;
543 extern u32 * nfs_decode_dirent(u32 *, struct nfs_entry *, int);
546 nfs_read_done(struct rpc_task *task)
548 struct nfs_read_data *data = (struct nfs_read_data *) task->tk_calldata;
550 if (task->tk_status >= 0) {
551 nfs_refresh_inode(data->inode, data->res.fattr);
552 /* Emulate the eof flag, which isn't normally needed in NFSv2
553 * as it is guaranteed to always return the file attributes
555 if (data->args.offset + data->args.count >= data->res.fattr->size)
558 nfs_readpage_result(task);
562 nfs_proc_read_setup(struct nfs_read_data *data)
564 struct rpc_task *task = &data->task;
565 struct inode *inode = data->inode;
567 struct rpc_message msg = {
568 .rpc_proc = &nfs_procedures[NFSPROC_READ],
569 .rpc_argp = &data->args,
570 .rpc_resp = &data->res,
571 .rpc_cred = data->cred,
574 /* N.B. Do we need to test? Never called for swapfile inode */
575 flags = RPC_TASK_ASYNC | (IS_SWAPFILE(inode)? NFS_RPC_SWAPFLAGS : 0);
577 /* Finalize the task. */
578 rpc_init_task(task, NFS_CLIENT(inode), nfs_read_done, flags);
579 rpc_call_setup(task, &msg, 0);
583 nfs_write_done(struct rpc_task *task)
585 struct nfs_write_data *data = (struct nfs_write_data *) task->tk_calldata;
587 if (task->tk_status >= 0)
588 nfs_refresh_inode(data->inode, data->res.fattr);
589 nfs_writeback_done(task);
593 nfs_proc_write_setup(struct nfs_write_data *data, int how)
595 struct rpc_task *task = &data->task;
596 struct inode *inode = data->inode;
598 struct rpc_message msg = {
599 .rpc_proc = &nfs_procedures[NFSPROC_WRITE],
600 .rpc_argp = &data->args,
601 .rpc_resp = &data->res,
602 .rpc_cred = data->cred,
605 /* Note: NFSv2 ignores @stable and always uses NFS_FILE_SYNC */
606 data->args.stable = NFS_FILE_SYNC;
608 /* Set the initial flags for the task. */
609 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
611 /* Finalize the task. */
612 rpc_init_task(task, NFS_CLIENT(inode), nfs_write_done, flags);
613 rpc_call_setup(task, &msg, 0);
617 nfs_proc_commit_setup(struct nfs_write_data *data, int how)
623 * Set up the nfspage struct with the right credentials
626 nfs_request_init(struct nfs_page *req, struct file *filp)
628 req->wb_cred = get_rpccred(nfs_cred(req->wb_inode, filp));
632 nfs_request_compatible(struct nfs_page *req, struct file *filp, struct page *page)
634 if (req->wb_file != filp)
636 if (req->wb_page != page)
638 if (req->wb_cred != nfs_file_cred(filp))
644 nfs_proc_lock(struct file *filp, int cmd, struct file_lock *fl)
646 return nlmclnt_proc(filp->f_dentry->d_inode, cmd, fl);
650 struct nfs_rpc_ops nfs_v2_clientops = {
651 .version = 2, /* protocol version */
652 .dentry_ops = &nfs_dentry_operations,
653 .dir_inode_ops = &nfs_dir_inode_operations,
654 .getroot = nfs_proc_get_root,
655 .getattr = nfs_proc_getattr,
656 .setattr = nfs_proc_setattr,
657 .lookup = nfs_proc_lookup,
658 .access = NULL, /* access */
659 .readlink = nfs_proc_readlink,
660 .read = nfs_proc_read,
661 .write = nfs_proc_write,
662 .commit = NULL, /* commit */
663 .create = nfs_proc_create,
664 .remove = nfs_proc_remove,
665 .unlink_setup = nfs_proc_unlink_setup,
666 .unlink_done = nfs_proc_unlink_done,
667 .rename = nfs_proc_rename,
668 .link = nfs_proc_link,
669 .symlink = nfs_proc_symlink,
670 .mkdir = nfs_proc_mkdir,
671 .rmdir = nfs_proc_rmdir,
672 .readdir = nfs_proc_readdir,
673 .mknod = nfs_proc_mknod,
674 .statfs = nfs_proc_statfs,
675 .fsinfo = nfs_proc_fsinfo,
676 .pathconf = nfs_proc_pathconf,
677 .decode_dirent = nfs_decode_dirent,
678 .read_setup = nfs_proc_read_setup,
679 .write_setup = nfs_proc_write_setup,
680 .commit_setup = nfs_proc_commit_setup,
681 .file_open = nfs_open,
682 .file_release = nfs_release,
683 .request_init = nfs_request_init,
684 .request_compatible = nfs_request_compatible,
685 .lock = nfs_proc_lock,