1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 1992, 1993, 1994 Rick Sladkey
7 * OS-independent nfs remote procedure call functions
10 * so at last we can have decent(ish) throughput off a
13 * Coding optimized and cleaned up by Florian La Roche.
14 * Note: Error returns are optimized for NFS_OK, which isn't translated via
15 * nfs_stat_to_errno(), but happens to be already the right return code.
17 * Also, the code currently doesn't check the size of the packet, when
18 * it decodes the packet.
20 * Feel free to fix it and mail me the diffs if it worries you.
22 * Completely rewritten to support the new RPC call interface;
23 * rewrote and moved the entire XDR stuff to xdr.c
24 * --Olaf Kirch June 1996
26 * The code below initializes all auto variables explicitly, otherwise
27 * it will fail to work as a module (gcc generates a memset call for an
31 #include <linux/types.h>
32 #include <linux/param.h>
33 #include <linux/time.h>
35 #include <linux/errno.h>
36 #include <linux/string.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 <linux/freezer.h>
48 #define NFSDBG_FACILITY NFSDBG_PROC
51 * Bare-bones access to getattr: this is for nfs_read_super.
54 nfs_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
55 struct nfs_fsinfo *info)
57 struct nfs_fattr *fattr = info->fattr;
58 struct nfs2_fsstat fsinfo;
59 struct rpc_message msg = {
60 .rpc_proc = &nfs_procedures[NFSPROC_GETATTR],
66 dprintk("%s: call getattr\n", __func__);
67 nfs_fattr_init(fattr);
68 status = rpc_call_sync(server->client, &msg, 0);
69 /* Retry with default authentication if different */
70 if (status && server->nfs_client->cl_rpcclient != server->client)
71 status = rpc_call_sync(server->nfs_client->cl_rpcclient, &msg, 0);
72 dprintk("%s: reply getattr: %d\n", __func__, status);
75 dprintk("%s: call statfs\n", __func__);
76 msg.rpc_proc = &nfs_procedures[NFSPROC_STATFS];
77 msg.rpc_resp = &fsinfo;
78 status = rpc_call_sync(server->client, &msg, 0);
79 /* Retry with default authentication if different */
80 if (status && server->nfs_client->cl_rpcclient != server->client)
81 status = rpc_call_sync(server->nfs_client->cl_rpcclient, &msg, 0);
82 dprintk("%s: reply statfs: %d\n", __func__, status);
85 info->rtmax = NFS_MAXDATA;
86 info->rtpref = fsinfo.tsize;
87 info->rtmult = fsinfo.bsize;
88 info->wtmax = NFS_MAXDATA;
89 info->wtpref = fsinfo.tsize;
90 info->wtmult = fsinfo.bsize;
91 info->dtpref = fsinfo.tsize;
92 info->maxfilesize = 0x7FFFFFFF;
94 info->change_attr_type = NFS4_CHANGE_TYPE_IS_UNDEFINED;
99 * One function for each procedure in the NFS protocol.
102 nfs_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
103 struct nfs_fattr *fattr, struct inode *inode)
105 struct rpc_message msg = {
106 .rpc_proc = &nfs_procedures[NFSPROC_GETATTR],
111 unsigned short task_flags = 0;
113 /* Is this is an attribute revalidation, subject to softreval? */
114 if (inode && (server->flags & NFS_MOUNT_SOFTREVAL))
115 task_flags |= RPC_TASK_TIMEOUT;
117 dprintk("NFS call getattr\n");
118 nfs_fattr_init(fattr);
119 status = rpc_call_sync(server->client, &msg, task_flags);
120 dprintk("NFS reply getattr: %d\n", status);
125 nfs_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
128 struct inode *inode = d_inode(dentry);
129 struct nfs_sattrargs arg = {
133 struct rpc_message msg = {
134 .rpc_proc = &nfs_procedures[NFSPROC_SETATTR],
140 /* Mask out the non-modebit related stuff from attr->ia_mode */
141 sattr->ia_mode &= S_IALLUGO;
143 dprintk("NFS call setattr\n");
144 if (sattr->ia_valid & ATTR_FILE)
145 msg.rpc_cred = nfs_file_cred(sattr->ia_file);
146 nfs_fattr_init(fattr);
147 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
149 nfs_setattr_update_inode(inode, sattr, fattr);
150 dprintk("NFS reply setattr: %d\n", status);
155 nfs_proc_lookup(struct inode *dir, struct dentry *dentry,
156 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
158 struct nfs_diropargs arg = {
160 .name = dentry->d_name.name,
161 .len = dentry->d_name.len
163 struct nfs_diropok res = {
167 struct rpc_message msg = {
168 .rpc_proc = &nfs_procedures[NFSPROC_LOOKUP],
173 unsigned short task_flags = 0;
175 /* Is this is an attribute revalidation, subject to softreval? */
176 if (nfs_lookup_is_soft_revalidate(dentry))
177 task_flags |= RPC_TASK_TIMEOUT;
179 dprintk("NFS call lookup %pd2\n", dentry);
180 nfs_fattr_init(fattr);
181 status = rpc_call_sync(NFS_CLIENT(dir), &msg, task_flags);
182 dprintk("NFS reply lookup: %d\n", status);
186 static int nfs_proc_readlink(struct inode *inode, struct page *page,
187 unsigned int pgbase, unsigned int pglen)
189 struct nfs_readlinkargs args = {
195 struct rpc_message msg = {
196 .rpc_proc = &nfs_procedures[NFSPROC_READLINK],
201 dprintk("NFS call readlink\n");
202 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
203 dprintk("NFS reply readlink: %d\n", status);
207 struct nfs_createdata {
208 struct nfs_createargs arg;
209 struct nfs_diropok res;
210 struct nfs_fh fhandle;
211 struct nfs_fattr fattr;
214 static struct nfs_createdata *nfs_alloc_createdata(struct inode *dir,
215 struct dentry *dentry, struct iattr *sattr)
217 struct nfs_createdata *data;
219 data = kmalloc(sizeof(*data), GFP_KERNEL);
222 data->arg.fh = NFS_FH(dir);
223 data->arg.name = dentry->d_name.name;
224 data->arg.len = dentry->d_name.len;
225 data->arg.sattr = sattr;
226 nfs_fattr_init(&data->fattr);
227 data->fhandle.size = 0;
228 data->res.fh = &data->fhandle;
229 data->res.fattr = &data->fattr;
234 static void nfs_free_createdata(const struct nfs_createdata *data)
240 nfs_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
243 struct nfs_createdata *data;
244 struct rpc_message msg = {
245 .rpc_proc = &nfs_procedures[NFSPROC_CREATE],
247 int status = -ENOMEM;
249 dprintk("NFS call create %pd\n", dentry);
250 data = nfs_alloc_createdata(dir, dentry, sattr);
253 msg.rpc_argp = &data->arg;
254 msg.rpc_resp = &data->res;
255 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
256 nfs_mark_for_revalidate(dir);
258 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
259 nfs_free_createdata(data);
261 dprintk("NFS reply create: %d\n", status);
266 * In NFSv2, mknod is grafted onto the create call.
269 nfs_proc_mknod(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
272 struct nfs_createdata *data;
273 struct rpc_message msg = {
274 .rpc_proc = &nfs_procedures[NFSPROC_CREATE],
277 int status = -ENOMEM;
279 dprintk("NFS call mknod %pd\n", dentry);
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 */
290 data = nfs_alloc_createdata(dir, dentry, sattr);
293 msg.rpc_argp = &data->arg;
294 msg.rpc_resp = &data->res;
296 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
297 nfs_mark_for_revalidate(dir);
299 if (status == -EINVAL && S_ISFIFO(mode)) {
300 sattr->ia_mode = mode;
301 nfs_fattr_init(data->res.fattr);
302 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
305 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
306 nfs_free_createdata(data);
308 dprintk("NFS reply mknod: %d\n", status);
313 nfs_proc_remove(struct inode *dir, struct dentry *dentry)
315 struct nfs_removeargs arg = {
317 .name = dentry->d_name,
319 struct rpc_message msg = {
320 .rpc_proc = &nfs_procedures[NFSPROC_REMOVE],
325 dprintk("NFS call remove %pd2\n",dentry);
326 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
327 nfs_mark_for_revalidate(dir);
329 dprintk("NFS reply remove: %d\n", status);
334 nfs_proc_unlink_setup(struct rpc_message *msg,
335 struct dentry *dentry,
338 msg->rpc_proc = &nfs_procedures[NFSPROC_REMOVE];
341 static void nfs_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
343 rpc_call_start(task);
346 static int nfs_proc_unlink_done(struct rpc_task *task, struct inode *dir)
348 nfs_mark_for_revalidate(dir);
353 nfs_proc_rename_setup(struct rpc_message *msg,
354 struct dentry *old_dentry,
355 struct dentry *new_dentry)
357 msg->rpc_proc = &nfs_procedures[NFSPROC_RENAME];
360 static void nfs_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
362 rpc_call_start(task);
366 nfs_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
367 struct inode *new_dir)
369 nfs_mark_for_revalidate(old_dir);
370 nfs_mark_for_revalidate(new_dir);
375 nfs_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
377 struct nfs_linkargs arg = {
378 .fromfh = NFS_FH(inode),
380 .toname = name->name,
383 struct rpc_message msg = {
384 .rpc_proc = &nfs_procedures[NFSPROC_LINK],
389 dprintk("NFS call link %s\n", name->name);
390 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
391 nfs_mark_for_revalidate(inode);
392 nfs_mark_for_revalidate(dir);
393 dprintk("NFS reply link: %d\n", status);
398 nfs_proc_symlink(struct inode *dir, struct dentry *dentry, struct page *page,
399 unsigned int len, struct iattr *sattr)
402 struct nfs_fattr *fattr;
403 struct nfs_symlinkargs arg = {
404 .fromfh = NFS_FH(dir),
405 .fromname = dentry->d_name.name,
406 .fromlen = dentry->d_name.len,
411 struct rpc_message msg = {
412 .rpc_proc = &nfs_procedures[NFSPROC_SYMLINK],
415 int status = -ENAMETOOLONG;
417 dprintk("NFS call symlink %pd\n", dentry);
419 if (len > NFS2_MAXPATHLEN)
422 fh = nfs_alloc_fhandle();
423 fattr = nfs_alloc_fattr();
425 if (fh == NULL || fattr == NULL)
428 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
429 nfs_mark_for_revalidate(dir);
432 * V2 SYMLINK requests don't return any attributes. Setting the
433 * filehandle size to zero indicates to nfs_instantiate that it
434 * should fill in the data with a LOOKUP call on the wire.
437 status = nfs_instantiate(dentry, fh, fattr);
440 nfs_free_fattr(fattr);
441 nfs_free_fhandle(fh);
443 dprintk("NFS reply symlink: %d\n", status);
448 nfs_proc_mkdir(struct inode *dir, struct dentry *dentry, struct iattr *sattr)
450 struct nfs_createdata *data;
451 struct rpc_message msg = {
452 .rpc_proc = &nfs_procedures[NFSPROC_MKDIR],
454 int status = -ENOMEM;
456 dprintk("NFS call mkdir %pd\n", dentry);
457 data = nfs_alloc_createdata(dir, dentry, sattr);
460 msg.rpc_argp = &data->arg;
461 msg.rpc_resp = &data->res;
463 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
464 nfs_mark_for_revalidate(dir);
466 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
467 nfs_free_createdata(data);
469 dprintk("NFS reply mkdir: %d\n", status);
474 nfs_proc_rmdir(struct inode *dir, const struct qstr *name)
476 struct nfs_diropargs arg = {
481 struct rpc_message msg = {
482 .rpc_proc = &nfs_procedures[NFSPROC_RMDIR],
487 dprintk("NFS call rmdir %s\n", name->name);
488 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
489 nfs_mark_for_revalidate(dir);
490 dprintk("NFS reply rmdir: %d\n", status);
495 * The READDIR implementation is somewhat hackish - we pass a temporary
496 * buffer to the encode function, which installs it in the receive
497 * the receive iovec. The decode function just parses the reply to make
498 * sure it is syntactically correct; the entries itself are decoded
499 * from nfs_readdir by calling the decode_entry function directly.
501 static int nfs_proc_readdir(struct nfs_readdir_arg *nr_arg,
502 struct nfs_readdir_res *nr_res)
504 struct inode *dir = d_inode(nr_arg->dentry);
505 struct nfs_readdirargs arg = {
507 .cookie = nr_arg->cookie,
508 .count = nr_arg->page_len,
509 .pages = nr_arg->pages,
511 struct rpc_message msg = {
512 .rpc_proc = &nfs_procedures[NFSPROC_READDIR],
514 .rpc_cred = nr_arg->cred,
518 dprintk("NFS call readdir %llu\n", (unsigned long long)nr_arg->cookie);
519 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
520 nr_res->verf[0] = nr_res->verf[1] = 0;
522 nfs_invalidate_atime(dir);
524 dprintk("NFS reply readdir: %d\n", status);
529 nfs_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
530 struct nfs_fsstat *stat)
532 struct nfs2_fsstat fsinfo;
533 struct rpc_message msg = {
534 .rpc_proc = &nfs_procedures[NFSPROC_STATFS],
540 dprintk("NFS call statfs\n");
541 nfs_fattr_init(stat->fattr);
542 status = rpc_call_sync(server->client, &msg, 0);
543 dprintk("NFS reply statfs: %d\n", status);
546 stat->tbytes = (u64)fsinfo.blocks * fsinfo.bsize;
547 stat->fbytes = (u64)fsinfo.bfree * fsinfo.bsize;
548 stat->abytes = (u64)fsinfo.bavail * fsinfo.bsize;
557 nfs_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
558 struct nfs_fsinfo *info)
560 struct nfs2_fsstat fsinfo;
561 struct rpc_message msg = {
562 .rpc_proc = &nfs_procedures[NFSPROC_STATFS],
568 dprintk("NFS call fsinfo\n");
569 nfs_fattr_init(info->fattr);
570 status = rpc_call_sync(server->client, &msg, 0);
571 dprintk("NFS reply fsinfo: %d\n", status);
574 info->rtmax = NFS_MAXDATA;
575 info->rtpref = fsinfo.tsize;
576 info->rtmult = fsinfo.bsize;
577 info->wtmax = NFS_MAXDATA;
578 info->wtpref = fsinfo.tsize;
579 info->wtmult = fsinfo.bsize;
580 info->dtpref = fsinfo.tsize;
581 info->maxfilesize = 0x7FFFFFFF;
582 info->lease_time = 0;
588 nfs_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
589 struct nfs_pathconf *info)
592 info->max_namelen = NFS2_MAXNAMLEN;
596 static int nfs_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
598 struct inode *inode = hdr->inode;
600 nfs_invalidate_atime(inode);
601 if (task->tk_status >= 0) {
602 nfs_refresh_inode(inode, hdr->res.fattr);
603 /* Emulate the eof flag, which isn't normally needed in NFSv2
604 * as it is guaranteed to always return the file attributes
606 if ((hdr->res.count == 0 && hdr->args.count > 0) ||
607 hdr->args.offset + hdr->res.count >= hdr->res.fattr->size)
613 static void nfs_proc_read_setup(struct nfs_pgio_header *hdr,
614 struct rpc_message *msg)
616 msg->rpc_proc = &nfs_procedures[NFSPROC_READ];
619 static int nfs_proc_pgio_rpc_prepare(struct rpc_task *task,
620 struct nfs_pgio_header *hdr)
622 rpc_call_start(task);
626 static int nfs_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
628 if (task->tk_status >= 0) {
629 hdr->res.count = hdr->args.count;
630 nfs_writeback_update_inode(hdr);
635 static void nfs_proc_write_setup(struct nfs_pgio_header *hdr,
636 struct rpc_message *msg,
637 struct rpc_clnt **clnt)
639 /* Note: NFSv2 ignores @stable and always uses NFS_FILE_SYNC */
640 hdr->args.stable = NFS_FILE_SYNC;
641 msg->rpc_proc = &nfs_procedures[NFSPROC_WRITE];
644 static void nfs_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
650 nfs_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg,
651 struct rpc_clnt **clnt)
657 nfs_proc_lock(struct file *filp, int cmd, struct file_lock *fl)
659 struct inode *inode = file_inode(filp);
661 return nlmclnt_proc(NFS_SERVER(inode)->nlm_host, cmd, fl, NULL);
664 /* Helper functions for NFS lock bounds checking */
665 #define NFS_LOCK32_OFFSET_MAX ((__s32)0x7fffffffUL)
666 static int nfs_lock_check_bounds(const struct file_lock *fl)
670 start = (__s32)fl->fl_start;
671 if ((loff_t)start != fl->fl_start)
674 if (fl->fl_end != OFFSET_MAX) {
675 end = (__s32)fl->fl_end;
676 if ((loff_t)end != fl->fl_end)
679 end = NFS_LOCK32_OFFSET_MAX;
681 if (start < 0 || start > end)
688 static int nfs_have_delegation(struct inode *inode, fmode_t flags)
693 static const struct inode_operations nfs_dir_inode_operations = {
694 .create = nfs_create,
695 .lookup = nfs_lookup,
697 .unlink = nfs_unlink,
698 .symlink = nfs_symlink,
702 .rename = nfs_rename,
703 .permission = nfs_permission,
704 .getattr = nfs_getattr,
705 .setattr = nfs_setattr,
708 static const struct inode_operations nfs_file_inode_operations = {
709 .permission = nfs_permission,
710 .getattr = nfs_getattr,
711 .setattr = nfs_setattr,
714 const struct nfs_rpc_ops nfs_v2_clientops = {
715 .version = 2, /* protocol version */
716 .dentry_ops = &nfs_dentry_operations,
717 .dir_inode_ops = &nfs_dir_inode_operations,
718 .file_inode_ops = &nfs_file_inode_operations,
719 .file_ops = &nfs_file_operations,
720 .getroot = nfs_proc_get_root,
721 .submount = nfs_submount,
722 .try_get_tree = nfs_try_get_tree,
723 .getattr = nfs_proc_getattr,
724 .setattr = nfs_proc_setattr,
725 .lookup = nfs_proc_lookup,
726 .access = NULL, /* access */
727 .readlink = nfs_proc_readlink,
728 .create = nfs_proc_create,
729 .remove = nfs_proc_remove,
730 .unlink_setup = nfs_proc_unlink_setup,
731 .unlink_rpc_prepare = nfs_proc_unlink_rpc_prepare,
732 .unlink_done = nfs_proc_unlink_done,
733 .rename_setup = nfs_proc_rename_setup,
734 .rename_rpc_prepare = nfs_proc_rename_rpc_prepare,
735 .rename_done = nfs_proc_rename_done,
736 .link = nfs_proc_link,
737 .symlink = nfs_proc_symlink,
738 .mkdir = nfs_proc_mkdir,
739 .rmdir = nfs_proc_rmdir,
740 .readdir = nfs_proc_readdir,
741 .mknod = nfs_proc_mknod,
742 .statfs = nfs_proc_statfs,
743 .fsinfo = nfs_proc_fsinfo,
744 .pathconf = nfs_proc_pathconf,
745 .decode_dirent = nfs2_decode_dirent,
746 .pgio_rpc_prepare = nfs_proc_pgio_rpc_prepare,
747 .read_setup = nfs_proc_read_setup,
748 .read_done = nfs_read_done,
749 .write_setup = nfs_proc_write_setup,
750 .write_done = nfs_write_done,
751 .commit_setup = nfs_proc_commit_setup,
752 .commit_rpc_prepare = nfs_proc_commit_rpc_prepare,
753 .lock = nfs_proc_lock,
754 .lock_check_bounds = nfs_lock_check_bounds,
755 .close_context = nfs_close_context,
756 .have_delegation = nfs_have_delegation,
757 .alloc_client = nfs_alloc_client,
758 .init_client = nfs_init_client,
759 .free_client = nfs_free_client,
760 .create_server = nfs_create_server,
761 .clone_server = nfs_clone_server,