1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 1992, 1993, 1994 Rick Sladkey
7 * OS-independent nfs remote procedure call functions
9 * Tuned by Alan Cox <A.Cox@swansea.ac.uk> for >3K buffers
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;
95 info->xattr_support = 0;
100 * One function for each procedure in the NFS protocol.
103 nfs_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
104 struct nfs_fattr *fattr, struct nfs4_label *label,
107 struct rpc_message msg = {
108 .rpc_proc = &nfs_procedures[NFSPROC_GETATTR],
113 unsigned short task_flags = 0;
115 /* Is this is an attribute revalidation, subject to softreval? */
116 if (inode && (server->flags & NFS_MOUNT_SOFTREVAL))
117 task_flags |= RPC_TASK_TIMEOUT;
119 dprintk("NFS call getattr\n");
120 nfs_fattr_init(fattr);
121 status = rpc_call_sync(server->client, &msg, task_flags);
122 dprintk("NFS reply getattr: %d\n", status);
127 nfs_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
130 struct inode *inode = d_inode(dentry);
131 struct nfs_sattrargs arg = {
135 struct rpc_message msg = {
136 .rpc_proc = &nfs_procedures[NFSPROC_SETATTR],
142 /* Mask out the non-modebit related stuff from attr->ia_mode */
143 sattr->ia_mode &= S_IALLUGO;
145 dprintk("NFS call setattr\n");
146 if (sattr->ia_valid & ATTR_FILE)
147 msg.rpc_cred = nfs_file_cred(sattr->ia_file);
148 nfs_fattr_init(fattr);
149 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
151 nfs_setattr_update_inode(inode, sattr, fattr);
152 dprintk("NFS reply setattr: %d\n", status);
157 nfs_proc_lookup(struct inode *dir, struct dentry *dentry,
158 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
159 struct nfs4_label *label)
161 struct nfs_diropargs arg = {
163 .name = dentry->d_name.name,
164 .len = dentry->d_name.len
166 struct nfs_diropok res = {
170 struct rpc_message msg = {
171 .rpc_proc = &nfs_procedures[NFSPROC_LOOKUP],
176 unsigned short task_flags = 0;
178 /* Is this is an attribute revalidation, subject to softreval? */
179 if (nfs_lookup_is_soft_revalidate(dentry))
180 task_flags |= RPC_TASK_TIMEOUT;
182 dprintk("NFS call lookup %pd2\n", dentry);
183 nfs_fattr_init(fattr);
184 status = rpc_call_sync(NFS_CLIENT(dir), &msg, task_flags);
185 dprintk("NFS reply lookup: %d\n", status);
189 static int nfs_proc_readlink(struct inode *inode, struct page *page,
190 unsigned int pgbase, unsigned int pglen)
192 struct nfs_readlinkargs args = {
198 struct rpc_message msg = {
199 .rpc_proc = &nfs_procedures[NFSPROC_READLINK],
204 dprintk("NFS call readlink\n");
205 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
206 dprintk("NFS reply readlink: %d\n", status);
210 struct nfs_createdata {
211 struct nfs_createargs arg;
212 struct nfs_diropok res;
213 struct nfs_fh fhandle;
214 struct nfs_fattr fattr;
217 static struct nfs_createdata *nfs_alloc_createdata(struct inode *dir,
218 struct dentry *dentry, struct iattr *sattr)
220 struct nfs_createdata *data;
222 data = kmalloc(sizeof(*data), GFP_KERNEL);
225 data->arg.fh = NFS_FH(dir);
226 data->arg.name = dentry->d_name.name;
227 data->arg.len = dentry->d_name.len;
228 data->arg.sattr = sattr;
229 nfs_fattr_init(&data->fattr);
230 data->fhandle.size = 0;
231 data->res.fh = &data->fhandle;
232 data->res.fattr = &data->fattr;
237 static void nfs_free_createdata(const struct nfs_createdata *data)
243 nfs_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
246 struct nfs_createdata *data;
247 struct rpc_message msg = {
248 .rpc_proc = &nfs_procedures[NFSPROC_CREATE],
250 int status = -ENOMEM;
252 dprintk("NFS call create %pd\n", dentry);
253 data = nfs_alloc_createdata(dir, dentry, sattr);
256 msg.rpc_argp = &data->arg;
257 msg.rpc_resp = &data->res;
258 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
259 nfs_mark_for_revalidate(dir);
261 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, NULL);
262 nfs_free_createdata(data);
264 dprintk("NFS reply create: %d\n", status);
269 * In NFSv2, mknod is grafted onto the create call.
272 nfs_proc_mknod(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
275 struct nfs_createdata *data;
276 struct rpc_message msg = {
277 .rpc_proc = &nfs_procedures[NFSPROC_CREATE],
280 int status = -ENOMEM;
282 dprintk("NFS call mknod %pd\n", dentry);
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 */
293 data = nfs_alloc_createdata(dir, dentry, sattr);
296 msg.rpc_argp = &data->arg;
297 msg.rpc_resp = &data->res;
299 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
300 nfs_mark_for_revalidate(dir);
302 if (status == -EINVAL && S_ISFIFO(mode)) {
303 sattr->ia_mode = mode;
304 nfs_fattr_init(data->res.fattr);
305 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
308 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, NULL);
309 nfs_free_createdata(data);
311 dprintk("NFS reply mknod: %d\n", status);
316 nfs_proc_remove(struct inode *dir, struct dentry *dentry)
318 struct nfs_removeargs arg = {
320 .name = dentry->d_name,
322 struct rpc_message msg = {
323 .rpc_proc = &nfs_procedures[NFSPROC_REMOVE],
328 dprintk("NFS call remove %pd2\n",dentry);
329 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
330 nfs_mark_for_revalidate(dir);
332 dprintk("NFS reply remove: %d\n", status);
337 nfs_proc_unlink_setup(struct rpc_message *msg,
338 struct dentry *dentry,
341 msg->rpc_proc = &nfs_procedures[NFSPROC_REMOVE];
344 static void nfs_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
346 rpc_call_start(task);
349 static int nfs_proc_unlink_done(struct rpc_task *task, struct inode *dir)
351 nfs_mark_for_revalidate(dir);
356 nfs_proc_rename_setup(struct rpc_message *msg,
357 struct dentry *old_dentry,
358 struct dentry *new_dentry)
360 msg->rpc_proc = &nfs_procedures[NFSPROC_RENAME];
363 static void nfs_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
365 rpc_call_start(task);
369 nfs_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
370 struct inode *new_dir)
372 nfs_mark_for_revalidate(old_dir);
373 nfs_mark_for_revalidate(new_dir);
378 nfs_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
380 struct nfs_linkargs arg = {
381 .fromfh = NFS_FH(inode),
383 .toname = name->name,
386 struct rpc_message msg = {
387 .rpc_proc = &nfs_procedures[NFSPROC_LINK],
392 dprintk("NFS call link %s\n", name->name);
393 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
394 nfs_mark_for_revalidate(inode);
395 nfs_mark_for_revalidate(dir);
396 dprintk("NFS reply link: %d\n", status);
401 nfs_proc_symlink(struct inode *dir, struct dentry *dentry, struct page *page,
402 unsigned int len, struct iattr *sattr)
405 struct nfs_fattr *fattr;
406 struct nfs_symlinkargs arg = {
407 .fromfh = NFS_FH(dir),
408 .fromname = dentry->d_name.name,
409 .fromlen = dentry->d_name.len,
414 struct rpc_message msg = {
415 .rpc_proc = &nfs_procedures[NFSPROC_SYMLINK],
418 int status = -ENAMETOOLONG;
420 dprintk("NFS call symlink %pd\n", dentry);
422 if (len > NFS2_MAXPATHLEN)
425 fh = nfs_alloc_fhandle();
426 fattr = nfs_alloc_fattr();
428 if (fh == NULL || fattr == NULL)
431 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
432 nfs_mark_for_revalidate(dir);
435 * V2 SYMLINK requests don't return any attributes. Setting the
436 * filehandle size to zero indicates to nfs_instantiate that it
437 * should fill in the data with a LOOKUP call on the wire.
440 status = nfs_instantiate(dentry, fh, fattr, NULL);
443 nfs_free_fattr(fattr);
444 nfs_free_fhandle(fh);
446 dprintk("NFS reply symlink: %d\n", status);
451 nfs_proc_mkdir(struct inode *dir, struct dentry *dentry, struct iattr *sattr)
453 struct nfs_createdata *data;
454 struct rpc_message msg = {
455 .rpc_proc = &nfs_procedures[NFSPROC_MKDIR],
457 int status = -ENOMEM;
459 dprintk("NFS call mkdir %pd\n", dentry);
460 data = nfs_alloc_createdata(dir, dentry, sattr);
463 msg.rpc_argp = &data->arg;
464 msg.rpc_resp = &data->res;
466 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
467 nfs_mark_for_revalidate(dir);
469 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, NULL);
470 nfs_free_createdata(data);
472 dprintk("NFS reply mkdir: %d\n", status);
477 nfs_proc_rmdir(struct inode *dir, const struct qstr *name)
479 struct nfs_diropargs arg = {
484 struct rpc_message msg = {
485 .rpc_proc = &nfs_procedures[NFSPROC_RMDIR],
490 dprintk("NFS call rmdir %s\n", name->name);
491 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
492 nfs_mark_for_revalidate(dir);
493 dprintk("NFS reply rmdir: %d\n", status);
498 * The READDIR implementation is somewhat hackish - we pass a temporary
499 * buffer to the encode function, which installs it in the receive
500 * the receive iovec. The decode function just parses the reply to make
501 * sure it is syntactically correct; the entries itself are decoded
502 * from nfs_readdir by calling the decode_entry function directly.
504 static int nfs_proc_readdir(struct nfs_readdir_arg *nr_arg,
505 struct nfs_readdir_res *nr_res)
507 struct inode *dir = d_inode(nr_arg->dentry);
508 struct nfs_readdirargs arg = {
510 .cookie = nr_arg->cookie,
511 .count = nr_arg->page_len,
512 .pages = nr_arg->pages,
514 struct rpc_message msg = {
515 .rpc_proc = &nfs_procedures[NFSPROC_READDIR],
517 .rpc_cred = nr_arg->cred,
521 dprintk("NFS call readdir %llu\n", (unsigned long long)nr_arg->cookie);
522 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
523 nr_res->verf[0] = nr_res->verf[1] = 0;
525 nfs_invalidate_atime(dir);
527 dprintk("NFS reply readdir: %d\n", status);
532 nfs_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
533 struct nfs_fsstat *stat)
535 struct nfs2_fsstat fsinfo;
536 struct rpc_message msg = {
537 .rpc_proc = &nfs_procedures[NFSPROC_STATFS],
543 dprintk("NFS call statfs\n");
544 nfs_fattr_init(stat->fattr);
545 status = rpc_call_sync(server->client, &msg, 0);
546 dprintk("NFS reply statfs: %d\n", status);
549 stat->tbytes = (u64)fsinfo.blocks * fsinfo.bsize;
550 stat->fbytes = (u64)fsinfo.bfree * fsinfo.bsize;
551 stat->abytes = (u64)fsinfo.bavail * fsinfo.bsize;
560 nfs_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
561 struct nfs_fsinfo *info)
563 struct nfs2_fsstat fsinfo;
564 struct rpc_message msg = {
565 .rpc_proc = &nfs_procedures[NFSPROC_STATFS],
571 dprintk("NFS call fsinfo\n");
572 nfs_fattr_init(info->fattr);
573 status = rpc_call_sync(server->client, &msg, 0);
574 dprintk("NFS reply fsinfo: %d\n", status);
577 info->rtmax = NFS_MAXDATA;
578 info->rtpref = fsinfo.tsize;
579 info->rtmult = fsinfo.bsize;
580 info->wtmax = NFS_MAXDATA;
581 info->wtpref = fsinfo.tsize;
582 info->wtmult = fsinfo.bsize;
583 info->dtpref = fsinfo.tsize;
584 info->maxfilesize = 0x7FFFFFFF;
585 info->lease_time = 0;
591 nfs_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
592 struct nfs_pathconf *info)
595 info->max_namelen = NFS2_MAXNAMLEN;
599 static int nfs_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
601 struct inode *inode = hdr->inode;
603 nfs_invalidate_atime(inode);
604 if (task->tk_status >= 0) {
605 nfs_refresh_inode(inode, hdr->res.fattr);
606 /* Emulate the eof flag, which isn't normally needed in NFSv2
607 * as it is guaranteed to always return the file attributes
609 if ((hdr->res.count == 0 && hdr->args.count > 0) ||
610 hdr->args.offset + hdr->res.count >= hdr->res.fattr->size)
616 static void nfs_proc_read_setup(struct nfs_pgio_header *hdr,
617 struct rpc_message *msg)
619 msg->rpc_proc = &nfs_procedures[NFSPROC_READ];
622 static int nfs_proc_pgio_rpc_prepare(struct rpc_task *task,
623 struct nfs_pgio_header *hdr)
625 rpc_call_start(task);
629 static int nfs_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
631 if (task->tk_status >= 0) {
632 hdr->res.count = hdr->args.count;
633 nfs_writeback_update_inode(hdr);
638 static void nfs_proc_write_setup(struct nfs_pgio_header *hdr,
639 struct rpc_message *msg,
640 struct rpc_clnt **clnt)
642 /* Note: NFSv2 ignores @stable and always uses NFS_FILE_SYNC */
643 hdr->args.stable = NFS_FILE_SYNC;
644 msg->rpc_proc = &nfs_procedures[NFSPROC_WRITE];
647 static void nfs_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
653 nfs_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg,
654 struct rpc_clnt **clnt)
660 nfs_proc_lock(struct file *filp, int cmd, struct file_lock *fl)
662 struct inode *inode = file_inode(filp);
664 return nlmclnt_proc(NFS_SERVER(inode)->nlm_host, cmd, fl, NULL);
667 /* Helper functions for NFS lock bounds checking */
668 #define NFS_LOCK32_OFFSET_MAX ((__s32)0x7fffffffUL)
669 static int nfs_lock_check_bounds(const struct file_lock *fl)
673 start = (__s32)fl->fl_start;
674 if ((loff_t)start != fl->fl_start)
677 if (fl->fl_end != OFFSET_MAX) {
678 end = (__s32)fl->fl_end;
679 if ((loff_t)end != fl->fl_end)
682 end = NFS_LOCK32_OFFSET_MAX;
684 if (start < 0 || start > end)
691 static int nfs_have_delegation(struct inode *inode, fmode_t flags)
696 static const struct inode_operations nfs_dir_inode_operations = {
697 .create = nfs_create,
698 .lookup = nfs_lookup,
700 .unlink = nfs_unlink,
701 .symlink = nfs_symlink,
705 .rename = nfs_rename,
706 .permission = nfs_permission,
707 .getattr = nfs_getattr,
708 .setattr = nfs_setattr,
711 static const struct inode_operations nfs_file_inode_operations = {
712 .permission = nfs_permission,
713 .getattr = nfs_getattr,
714 .setattr = nfs_setattr,
717 const struct nfs_rpc_ops nfs_v2_clientops = {
718 .version = 2, /* protocol version */
719 .dentry_ops = &nfs_dentry_operations,
720 .dir_inode_ops = &nfs_dir_inode_operations,
721 .file_inode_ops = &nfs_file_inode_operations,
722 .file_ops = &nfs_file_operations,
723 .getroot = nfs_proc_get_root,
724 .submount = nfs_submount,
725 .try_get_tree = nfs_try_get_tree,
726 .getattr = nfs_proc_getattr,
727 .setattr = nfs_proc_setattr,
728 .lookup = nfs_proc_lookup,
729 .access = NULL, /* access */
730 .readlink = nfs_proc_readlink,
731 .create = nfs_proc_create,
732 .remove = nfs_proc_remove,
733 .unlink_setup = nfs_proc_unlink_setup,
734 .unlink_rpc_prepare = nfs_proc_unlink_rpc_prepare,
735 .unlink_done = nfs_proc_unlink_done,
736 .rename_setup = nfs_proc_rename_setup,
737 .rename_rpc_prepare = nfs_proc_rename_rpc_prepare,
738 .rename_done = nfs_proc_rename_done,
739 .link = nfs_proc_link,
740 .symlink = nfs_proc_symlink,
741 .mkdir = nfs_proc_mkdir,
742 .rmdir = nfs_proc_rmdir,
743 .readdir = nfs_proc_readdir,
744 .mknod = nfs_proc_mknod,
745 .statfs = nfs_proc_statfs,
746 .fsinfo = nfs_proc_fsinfo,
747 .pathconf = nfs_proc_pathconf,
748 .decode_dirent = nfs2_decode_dirent,
749 .pgio_rpc_prepare = nfs_proc_pgio_rpc_prepare,
750 .read_setup = nfs_proc_read_setup,
751 .read_done = nfs_read_done,
752 .write_setup = nfs_proc_write_setup,
753 .write_done = nfs_write_done,
754 .commit_setup = nfs_proc_commit_setup,
755 .commit_rpc_prepare = nfs_proc_commit_rpc_prepare,
756 .lock = nfs_proc_lock,
757 .lock_check_bounds = nfs_lock_check_bounds,
758 .close_context = nfs_close_context,
759 .have_delegation = nfs_have_delegation,
760 .alloc_client = nfs_alloc_client,
761 .init_client = nfs_init_client,
762 .free_client = nfs_free_client,
763 .create_server = nfs_create_server,
764 .clone_server = nfs_clone_server,