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>
49 #define NFSDBG_FACILITY NFSDBG_PROC
52 * Bare-bones access to getattr: this is for nfs_read_super.
55 nfs_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
56 struct nfs_fsinfo *info)
58 struct nfs_fattr *fattr = info->fattr;
59 struct nfs2_fsstat fsinfo;
60 struct rpc_message msg = {
61 .rpc_proc = &nfs_procedures[NFSPROC_GETATTR],
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);
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);
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;
93 * One function for each procedure in the NFS protocol.
96 nfs_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
97 struct nfs_fattr *fattr)
99 struct rpc_message msg = {
100 .rpc_proc = &nfs_procedures[NFSPROC_GETATTR],
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);
114 nfs_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
117 struct inode *inode = dentry->d_inode;
118 struct nfs_sattrargs arg = {
122 struct rpc_message msg = {
123 .rpc_proc = &nfs_procedures[NFSPROC_SETATTR],
129 /* Mask out the non-modebit related stuff from attr->ia_mode */
130 sattr->ia_mode &= S_IALLUGO;
132 dprintk("NFS call setattr\n");
133 nfs_fattr_init(fattr);
134 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
136 nfs_setattr_update_inode(inode, sattr);
137 dprintk("NFS reply setattr: %d\n", status);
142 nfs_proc_lookup(struct inode *dir, struct qstr *name,
143 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
145 struct nfs_diropargs arg = {
150 struct nfs_diropok res = {
154 struct rpc_message msg = {
155 .rpc_proc = &nfs_procedures[NFSPROC_LOOKUP],
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);
168 static int nfs_proc_readlink(struct inode *inode, struct page *page,
169 unsigned int pgbase, unsigned int pglen)
171 struct nfs_readlinkargs args = {
177 struct rpc_message msg = {
178 .rpc_proc = &nfs_procedures[NFSPROC_READLINK],
183 dprintk("NFS call readlink\n");
184 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
185 dprintk("NFS reply readlink: %d\n", status);
190 nfs_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
191 int flags, struct nameidata *nd)
193 struct nfs_fh fhandle;
194 struct nfs_fattr fattr;
195 struct nfs_createargs arg = {
197 .name = dentry->d_name.name,
198 .len = dentry->d_name.len,
201 struct nfs_diropok res = {
205 struct rpc_message msg = {
206 .rpc_proc = &nfs_procedures[NFSPROC_CREATE],
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);
216 status = nfs_instantiate(dentry, &fhandle, &fattr);
217 dprintk("NFS reply create: %d\n", status);
222 * In NFSv2, mknod is grafted onto the create call.
225 nfs_proc_mknod(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
228 struct nfs_fh fhandle;
229 struct nfs_fattr fattr;
230 struct nfs_createargs arg = {
232 .name = dentry->d_name.name,
233 .len = dentry->d_name.len,
236 struct nfs_diropok res = {
240 struct rpc_message msg = {
241 .rpc_proc = &nfs_procedures[NFSPROC_CREATE],
247 dprintk("NFS call mknod %s\n", dentry->d_name.name);
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 */
258 nfs_fattr_init(&fattr);
259 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
260 nfs_mark_for_revalidate(dir);
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);
268 status = nfs_instantiate(dentry, &fhandle, &fattr);
269 dprintk("NFS reply mknod: %d\n", status);
274 nfs_proc_remove(struct inode *dir, struct qstr *name)
276 struct nfs_diropargs arg = {
281 struct rpc_message msg = {
282 .rpc_proc = &nfs_procedures[NFSPROC_REMOVE],
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);
291 dprintk("NFS reply remove: %d\n", status);
296 nfs_proc_unlink_setup(struct rpc_message *msg, struct dentry *dir, struct qstr *name)
298 struct nfs_diropargs *arg;
300 arg = kmalloc(sizeof(*arg), GFP_KERNEL);
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];
312 nfs_proc_unlink_done(struct dentry *dir, struct rpc_task *task)
314 struct rpc_message *msg = &task->tk_msg;
317 nfs_mark_for_revalidate(dir->d_inode);
318 kfree(msg->rpc_argp);
324 nfs_proc_rename(struct inode *old_dir, struct qstr *old_name,
325 struct inode *new_dir, struct qstr *new_name)
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
335 struct rpc_message msg = {
336 .rpc_proc = &nfs_procedures[NFSPROC_RENAME],
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);
350 nfs_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
352 struct nfs_linkargs arg = {
353 .fromfh = NFS_FH(inode),
355 .toname = name->name,
358 struct rpc_message msg = {
359 .rpc_proc = &nfs_procedures[NFSPROC_LINK],
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);
373 nfs_proc_symlink(struct inode *dir, struct dentry *dentry, struct page *page,
374 unsigned int len, struct iattr *sattr)
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,
386 struct rpc_message msg = {
387 .rpc_proc = &nfs_procedures[NFSPROC_SYMLINK],
392 if (len > NFS2_MAXPATHLEN)
393 return -ENAMETOOLONG;
395 dprintk("NFS call symlink %s\n", dentry->d_name.name);
397 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
398 nfs_mark_for_revalidate(dir);
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.
406 nfs_fattr_init(&fattr);
408 status = nfs_instantiate(dentry, &fhandle, &fattr);
411 dprintk("NFS reply symlink: %d\n", status);
416 nfs_proc_mkdir(struct inode *dir, struct dentry *dentry, struct iattr *sattr)
418 struct nfs_fh fhandle;
419 struct nfs_fattr fattr;
420 struct nfs_createargs arg = {
422 .name = dentry->d_name.name,
423 .len = dentry->d_name.len,
426 struct nfs_diropok res = {
430 struct rpc_message msg = {
431 .rpc_proc = &nfs_procedures[NFSPROC_MKDIR],
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);
442 status = nfs_instantiate(dentry, &fhandle, &fattr);
443 dprintk("NFS reply mkdir: %d\n", status);
448 nfs_proc_rmdir(struct inode *dir, struct qstr *name)
450 struct nfs_diropargs arg = {
455 struct rpc_message msg = {
456 .rpc_proc = &nfs_procedures[NFSPROC_RMDIR],
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);
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.
476 nfs_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
477 u64 cookie, struct page *page, unsigned int count, int plus)
479 struct inode *dir = dentry->d_inode;
480 struct nfs_readdirargs arg = {
486 struct rpc_message msg = {
487 .rpc_proc = &nfs_procedures[NFSPROC_READDIR],
493 dprintk("NFS call readdir %d\n", (unsigned int)cookie);
494 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
496 dprintk("NFS reply readdir: %d\n", status);
501 nfs_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
502 struct nfs_fsstat *stat)
504 struct nfs2_fsstat fsinfo;
505 struct rpc_message msg = {
506 .rpc_proc = &nfs_procedures[NFSPROC_STATFS],
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);
518 stat->tbytes = (u64)fsinfo.blocks * fsinfo.bsize;
519 stat->fbytes = (u64)fsinfo.bfree * fsinfo.bsize;
520 stat->abytes = (u64)fsinfo.bavail * fsinfo.bsize;
529 nfs_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
530 struct nfs_fsinfo *info)
532 struct nfs2_fsstat fsinfo;
533 struct rpc_message msg = {
534 .rpc_proc = &nfs_procedures[NFSPROC_STATFS],
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);
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;
560 nfs_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
561 struct nfs_pathconf *info)
564 info->max_namelen = NFS2_MAXNAMLEN;
568 static int nfs_read_done(struct rpc_task *task, struct nfs_read_data *data)
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
575 if (data->args.offset + data->args.count >= data->res.fattr->size)
581 static void nfs_proc_read_setup(struct nfs_read_data *data)
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,
590 rpc_call_setup(&data->task, &msg, 0);
593 static int nfs_write_done(struct rpc_task *task, struct nfs_write_data *data)
595 if (task->tk_status >= 0)
596 nfs_post_op_update_inode(data->inode, data->res.fattr);
600 static void nfs_proc_write_setup(struct nfs_write_data *data, int how)
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,
609 /* Note: NFSv2 ignores @stable and always uses NFS_FILE_SYNC */
610 data->args.stable = NFS_FILE_SYNC;
612 /* Finalize the task. */
613 rpc_call_setup(&data->task, &msg, 0);
617 nfs_proc_commit_setup(struct nfs_write_data *data, int how)
623 nfs_proc_lock(struct file *filp, int cmd, struct file_lock *fl)
625 return nlmclnt_proc(filp->f_path.dentry->d_inode, cmd, fl);
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,