blob: 0451a094e89ee1364af21b8ad3de0d29b869338a [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * linux/fs/nfs/proc.c
4 *
5 * Copyright (C) 1992, 1993, 1994 Rick Sladkey
6 *
7 * OS-independent nfs remote procedure call functions
8 *
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
11 * Sun server.
12 *
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.
16 *
17 * Also, the code currently doesn't check the size of the packet, when
18 * it decodes the packet.
19 *
20 * Feel free to fix it and mail me the diffs if it worries you.
21 *
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
25 *
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
28 * incomplete struct).
29 */
30
31#include <linux/types.h>
32#include <linux/param.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/time.h>
34#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/errno.h>
36#include <linux/string.h>
37#include <linux/in.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>
Jeff Laytond3103102011-12-01 22:44:39 +010045#include <linux/freezer.h>
David Howellsf7b422b2006-06-09 09:34:33 -040046#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48#define NFSDBG_FACILITY NFSDBG_PROC
49
Linus Torvalds1da177e2005-04-16 15:20:36 -070050/*
51 * Bare-bones access to getattr: this is for nfs_read_super.
52 */
53static int
54nfs_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
55 struct nfs_fsinfo *info)
56{
57 struct nfs_fattr *fattr = info->fattr;
58 struct nfs2_fsstat fsinfo;
Chuck Leverdead28d2006-03-20 13:44:23 -050059 struct rpc_message msg = {
60 .rpc_proc = &nfs_procedures[NFSPROC_GETATTR],
61 .rpc_argp = fhandle,
62 .rpc_resp = fattr,
63 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 int status;
65
Harvey Harrison3110ff82008-05-02 13:42:44 -070066 dprintk("%s: call getattr\n", __func__);
Trond Myklebust0e574af2005-10-27 22:12:38 -040067 nfs_fattr_init(fattr);
EG Keizer37ca8f52008-08-19 16:34:36 -040068 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);
Harvey Harrison3110ff82008-05-02 13:42:44 -070072 dprintk("%s: reply getattr: %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 if (status)
74 return status;
Harvey Harrison3110ff82008-05-02 13:42:44 -070075 dprintk("%s: call statfs\n", __func__);
Chuck Leverdead28d2006-03-20 13:44:23 -050076 msg.rpc_proc = &nfs_procedures[NFSPROC_STATFS];
77 msg.rpc_resp = &fsinfo;
EG Keizer37ca8f52008-08-19 16:34:36 -040078 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);
Harvey Harrison3110ff82008-05-02 13:42:44 -070082 dprintk("%s: reply statfs: %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 if (status)
84 return 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;
93 info->lease_time = 0;
94 return 0;
95}
96
97/*
98 * One function for each procedure in the NFS protocol.
99 */
100static int
101nfs_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebusta841b542018-04-07 13:50:59 -0400102 struct nfs_fattr *fattr, struct nfs4_label *label,
103 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
Chuck Leverdead28d2006-03-20 13:44:23 -0500105 struct rpc_message msg = {
106 .rpc_proc = &nfs_procedures[NFSPROC_GETATTR],
107 .rpc_argp = fhandle,
108 .rpc_resp = fattr,
109 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 int status;
Trond Myklebustc74dfe92020-01-06 15:39:37 -0500111 unsigned short task_flags = 0;
112
113 /* Is this is an attribute revalidation, subject to softreval? */
114 if (inode && (server->flags & NFS_MOUNT_SOFTREVAL))
115 task_flags |= RPC_TASK_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
117 dprintk("NFS call getattr\n");
Trond Myklebust0e574af2005-10-27 22:12:38 -0400118 nfs_fattr_init(fattr);
Trond Myklebustc74dfe92020-01-06 15:39:37 -0500119 status = rpc_call_sync(server->client, &msg, task_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 dprintk("NFS reply getattr: %d\n", status);
121 return status;
122}
123
124static int
125nfs_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
126 struct iattr *sattr)
127{
David Howells2b0143b2015-03-17 22:25:59 +0000128 struct inode *inode = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 struct nfs_sattrargs arg = {
130 .fh = NFS_FH(inode),
131 .sattr = sattr
132 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500133 struct rpc_message msg = {
134 .rpc_proc = &nfs_procedures[NFSPROC_SETATTR],
135 .rpc_argp = &arg,
136 .rpc_resp = fattr,
137 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 int status;
139
Trond Myklebustcf3fff52006-01-03 09:55:53 +0100140 /* Mask out the non-modebit related stuff from attr->ia_mode */
141 sattr->ia_mode &= S_IALLUGO;
142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 dprintk("NFS call setattr\n");
Trond Myklebust659bfcd2008-06-10 19:39:41 -0400144 if (sattr->ia_valid & ATTR_FILE)
145 msg.rpc_cred = nfs_file_cred(sattr->ia_file);
Trond Myklebust0e574af2005-10-27 22:12:38 -0400146 nfs_fattr_init(fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500147 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
Trond Myklebust65e43082005-08-16 11:49:44 -0400148 if (status == 0)
Trond Myklebustf0446362015-02-26 16:09:04 -0500149 nfs_setattr_update_inode(inode, sattr, fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 dprintk("NFS reply setattr: %d\n", status);
151 return status;
152}
153
154static int
Al Virobeffb8f2016-07-20 16:34:42 -0400155nfs_proc_lookup(struct inode *dir, const struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -0400156 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
157 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
159 struct nfs_diropargs arg = {
160 .fh = NFS_FH(dir),
161 .name = name->name,
162 .len = name->len
163 };
164 struct nfs_diropok res = {
165 .fh = fhandle,
166 .fattr = fattr
167 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500168 struct rpc_message msg = {
169 .rpc_proc = &nfs_procedures[NFSPROC_LOOKUP],
170 .rpc_argp = &arg,
171 .rpc_resp = &res,
172 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 int status;
174
175 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0e574af2005-10-27 22:12:38 -0400176 nfs_fattr_init(fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500177 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 dprintk("NFS reply lookup: %d\n", status);
179 return status;
180}
181
182static int nfs_proc_readlink(struct inode *inode, struct page *page,
183 unsigned int pgbase, unsigned int pglen)
184{
185 struct nfs_readlinkargs args = {
186 .fh = NFS_FH(inode),
187 .pgbase = pgbase,
188 .pglen = pglen,
189 .pages = &page
190 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500191 struct rpc_message msg = {
192 .rpc_proc = &nfs_procedures[NFSPROC_READLINK],
193 .rpc_argp = &args,
194 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 int status;
196
197 dprintk("NFS call readlink\n");
Chuck Leverdead28d2006-03-20 13:44:23 -0500198 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 dprintk("NFS reply readlink: %d\n", status);
200 return status;
201}
202
Trond Myklebusteb872f02010-04-16 16:22:51 -0400203struct nfs_createdata {
204 struct nfs_createargs arg;
205 struct nfs_diropok res;
206 struct nfs_fh fhandle;
207 struct nfs_fattr fattr;
208};
209
210static struct nfs_createdata *nfs_alloc_createdata(struct inode *dir,
211 struct dentry *dentry, struct iattr *sattr)
212{
213 struct nfs_createdata *data;
214
215 data = kmalloc(sizeof(*data), GFP_KERNEL);
216
217 if (data != NULL) {
218 data->arg.fh = NFS_FH(dir);
219 data->arg.name = dentry->d_name.name;
220 data->arg.len = dentry->d_name.len;
221 data->arg.sattr = sattr;
222 nfs_fattr_init(&data->fattr);
223 data->fhandle.size = 0;
224 data->res.fh = &data->fhandle;
225 data->res.fattr = &data->fattr;
226 }
227 return data;
228};
229
230static void nfs_free_createdata(const struct nfs_createdata *data)
231{
232 kfree(data);
233}
234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235static int
236nfs_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +0200237 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238{
Trond Myklebusteb872f02010-04-16 16:22:51 -0400239 struct nfs_createdata *data;
Chuck Leverdead28d2006-03-20 13:44:23 -0500240 struct rpc_message msg = {
241 .rpc_proc = &nfs_procedures[NFSPROC_CREATE],
Chuck Leverdead28d2006-03-20 13:44:23 -0500242 };
Trond Myklebusteb872f02010-04-16 16:22:51 -0400243 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Al Viro6de14722013-09-16 10:53:17 -0400245 dprintk("NFS call create %pd\n", dentry);
Trond Myklebusteb872f02010-04-16 16:22:51 -0400246 data = nfs_alloc_createdata(dir, dentry, sattr);
247 if (data == NULL)
248 goto out;
249 msg.rpc_argp = &data->arg;
250 msg.rpc_resp = &data->res;
Chuck Leverdead28d2006-03-20 13:44:23 -0500251 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
Trond Myklebustbad2a522007-10-20 13:07:21 -0400252 nfs_mark_for_revalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 if (status == 0)
David Quigley1775fd32013-05-22 12:50:42 -0400254 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, NULL);
Trond Myklebusteb872f02010-04-16 16:22:51 -0400255 nfs_free_createdata(data);
256out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 dprintk("NFS reply create: %d\n", status);
258 return status;
259}
260
261/*
262 * In NFSv2, mknod is grafted onto the create call.
263 */
264static int
265nfs_proc_mknod(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
266 dev_t rdev)
267{
Trond Myklebusteb872f02010-04-16 16:22:51 -0400268 struct nfs_createdata *data;
Chuck Leverdead28d2006-03-20 13:44:23 -0500269 struct rpc_message msg = {
270 .rpc_proc = &nfs_procedures[NFSPROC_CREATE],
Chuck Leverdead28d2006-03-20 13:44:23 -0500271 };
Trond Myklebusteb872f02010-04-16 16:22:51 -0400272 umode_t mode;
273 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Al Viro6de14722013-09-16 10:53:17 -0400275 dprintk("NFS call mknod %pd\n", dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
277 mode = sattr->ia_mode;
278 if (S_ISFIFO(mode)) {
279 sattr->ia_mode = (mode & ~S_IFMT) | S_IFCHR;
280 sattr->ia_valid &= ~ATTR_SIZE;
281 } else if (S_ISCHR(mode) || S_ISBLK(mode)) {
282 sattr->ia_valid |= ATTR_SIZE;
283 sattr->ia_size = new_encode_dev(rdev);/* get out your barf bag */
284 }
285
Trond Myklebusteb872f02010-04-16 16:22:51 -0400286 data = nfs_alloc_createdata(dir, dentry, sattr);
287 if (data == NULL)
288 goto out;
289 msg.rpc_argp = &data->arg;
290 msg.rpc_resp = &data->res;
291
Chuck Leverdead28d2006-03-20 13:44:23 -0500292 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
Trond Myklebustdecf4912005-10-27 22:12:39 -0400293 nfs_mark_for_revalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
295 if (status == -EINVAL && S_ISFIFO(mode)) {
296 sattr->ia_mode = mode;
Trond Myklebusteb872f02010-04-16 16:22:51 -0400297 nfs_fattr_init(data->res.fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500298 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 }
300 if (status == 0)
David Quigley1775fd32013-05-22 12:50:42 -0400301 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, NULL);
Trond Myklebusteb872f02010-04-16 16:22:51 -0400302 nfs_free_createdata(data);
303out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 dprintk("NFS reply mknod: %d\n", status);
305 return status;
306}
307
308static int
Trond Myklebust912678d2018-03-20 16:43:15 -0400309nfs_proc_remove(struct inode *dir, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
Trond Myklebust4fdc17b2007-07-14 15:39:57 -0400311 struct nfs_removeargs arg = {
312 .fh = NFS_FH(dir),
Trond Myklebust912678d2018-03-20 16:43:15 -0400313 .name = dentry->d_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -0400315 struct rpc_message msg = {
316 .rpc_proc = &nfs_procedures[NFSPROC_REMOVE],
317 .rpc_argp = &arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 };
319 int status;
320
Trond Myklebust912678d2018-03-20 16:43:15 -0400321 dprintk("NFS call remove %pd2\n",dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
Trond Myklebustdecf4912005-10-27 22:12:39 -0400323 nfs_mark_for_revalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
325 dprintk("NFS reply remove: %d\n", status);
326 return status;
327}
328
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400329static void
Trond Myklebusted7e9ad2018-05-30 16:11:52 -0400330nfs_proc_unlink_setup(struct rpc_message *msg,
331 struct dentry *dentry,
332 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 msg->rpc_proc = &nfs_procedures[NFSPROC_REMOVE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335}
336
Bryan Schumaker34e137c2012-03-19 14:54:41 -0400337static void nfs_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
338{
339 rpc_call_start(task);
340}
341
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400342static int nfs_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400344 nfs_mark_for_revalidate(dir);
345 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346}
347
Jeff Laytond3d41522010-09-17 17:31:57 -0400348static void
Trond Myklebustf2c2c552018-03-20 16:43:16 -0400349nfs_proc_rename_setup(struct rpc_message *msg,
350 struct dentry *old_dentry,
351 struct dentry *new_dentry)
Jeff Laytond3d41522010-09-17 17:31:57 -0400352{
353 msg->rpc_proc = &nfs_procedures[NFSPROC_RENAME];
354}
355
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -0400356static void nfs_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
357{
358 rpc_call_start(task);
359}
360
Jeff Laytond3d41522010-09-17 17:31:57 -0400361static int
362nfs_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
363 struct inode *new_dir)
364{
Jeff Laytond3d41522010-09-17 17:31:57 -0400365 nfs_mark_for_revalidate(old_dir);
366 nfs_mark_for_revalidate(new_dir);
367 return 1;
368}
369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370static int
Al Virobeffb8f2016-07-20 16:34:42 -0400371nfs_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372{
373 struct nfs_linkargs arg = {
374 .fromfh = NFS_FH(inode),
375 .tofh = NFS_FH(dir),
376 .toname = name->name,
377 .tolen = name->len
378 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500379 struct rpc_message msg = {
380 .rpc_proc = &nfs_procedures[NFSPROC_LINK],
381 .rpc_argp = &arg,
382 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 int status;
384
385 dprintk("NFS call link %s\n", name->name);
Chuck Leverdead28d2006-03-20 13:44:23 -0500386 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
Trond Myklebust5ba7cc42005-12-03 15:20:17 -0500387 nfs_mark_for_revalidate(inode);
Trond Myklebustdecf4912005-10-27 22:12:39 -0400388 nfs_mark_for_revalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 dprintk("NFS reply link: %d\n", status);
390 return status;
391}
392
393static int
Chuck Lever94a6d752006-08-22 20:06:23 -0400394nfs_proc_symlink(struct inode *dir, struct dentry *dentry, struct page *page,
395 unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396{
Trond Myklebust23a30612010-04-16 16:22:51 -0400397 struct nfs_fh *fh;
398 struct nfs_fattr *fattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 struct nfs_symlinkargs arg = {
400 .fromfh = NFS_FH(dir),
Chuck Lever4f390c12006-08-22 20:06:22 -0400401 .fromname = dentry->d_name.name,
402 .fromlen = dentry->d_name.len,
Chuck Lever94a6d752006-08-22 20:06:23 -0400403 .pages = &page,
404 .pathlen = len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 .sattr = sattr
406 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500407 struct rpc_message msg = {
408 .rpc_proc = &nfs_procedures[NFSPROC_SYMLINK],
409 .rpc_argp = &arg,
410 };
Trond Myklebust23a30612010-04-16 16:22:51 -0400411 int status = -ENAMETOOLONG;
Chuck Lever4f390c12006-08-22 20:06:22 -0400412
Al Viro6de14722013-09-16 10:53:17 -0400413 dprintk("NFS call symlink %pd\n", dentry);
Chuck Lever94a6d752006-08-22 20:06:23 -0400414
Trond Myklebust23a30612010-04-16 16:22:51 -0400415 if (len > NFS2_MAXPATHLEN)
416 goto out;
417
418 fh = nfs_alloc_fhandle();
419 fattr = nfs_alloc_fattr();
420 status = -ENOMEM;
421 if (fh == NULL || fattr == NULL)
Jesper Juhl878215f2010-12-24 22:22:37 +0000422 goto out_free;
Trond Myklebust23a30612010-04-16 16:22:51 -0400423
Chuck Leverdead28d2006-03-20 13:44:23 -0500424 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
Trond Myklebustdecf4912005-10-27 22:12:39 -0400425 nfs_mark_for_revalidate(dir);
Chuck Lever4f390c12006-08-22 20:06:22 -0400426
427 /*
428 * V2 SYMLINK requests don't return any attributes. Setting the
429 * filehandle size to zero indicates to nfs_instantiate that it
430 * should fill in the data with a LOOKUP call on the wire.
431 */
Trond Myklebust23a30612010-04-16 16:22:51 -0400432 if (status == 0)
David Quigley1775fd32013-05-22 12:50:42 -0400433 status = nfs_instantiate(dentry, fh, fattr, NULL);
Chuck Lever4f390c12006-08-22 20:06:22 -0400434
Jesper Juhl878215f2010-12-24 22:22:37 +0000435out_free:
Trond Myklebust23a30612010-04-16 16:22:51 -0400436 nfs_free_fattr(fattr);
437 nfs_free_fhandle(fh);
438out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 dprintk("NFS reply symlink: %d\n", status);
440 return status;
441}
442
443static int
444nfs_proc_mkdir(struct inode *dir, struct dentry *dentry, struct iattr *sattr)
445{
Trond Myklebusteb872f02010-04-16 16:22:51 -0400446 struct nfs_createdata *data;
Chuck Leverdead28d2006-03-20 13:44:23 -0500447 struct rpc_message msg = {
448 .rpc_proc = &nfs_procedures[NFSPROC_MKDIR],
Chuck Leverdead28d2006-03-20 13:44:23 -0500449 };
Trond Myklebusteb872f02010-04-16 16:22:51 -0400450 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
Al Viro6de14722013-09-16 10:53:17 -0400452 dprintk("NFS call mkdir %pd\n", dentry);
Trond Myklebusteb872f02010-04-16 16:22:51 -0400453 data = nfs_alloc_createdata(dir, dentry, sattr);
454 if (data == NULL)
455 goto out;
456 msg.rpc_argp = &data->arg;
457 msg.rpc_resp = &data->res;
458
Chuck Leverdead28d2006-03-20 13:44:23 -0500459 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
Trond Myklebustdecf4912005-10-27 22:12:39 -0400460 nfs_mark_for_revalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 if (status == 0)
David Quigley1775fd32013-05-22 12:50:42 -0400462 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, NULL);
Trond Myklebusteb872f02010-04-16 16:22:51 -0400463 nfs_free_createdata(data);
464out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 dprintk("NFS reply mkdir: %d\n", status);
466 return status;
467}
468
469static int
Al Virobeffb8f2016-07-20 16:34:42 -0400470nfs_proc_rmdir(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
472 struct nfs_diropargs arg = {
473 .fh = NFS_FH(dir),
474 .name = name->name,
475 .len = name->len
476 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500477 struct rpc_message msg = {
478 .rpc_proc = &nfs_procedures[NFSPROC_RMDIR],
479 .rpc_argp = &arg,
480 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 int status;
482
483 dprintk("NFS call rmdir %s\n", name->name);
Chuck Leverdead28d2006-03-20 13:44:23 -0500484 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
Trond Myklebustdecf4912005-10-27 22:12:39 -0400485 nfs_mark_for_revalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 dprintk("NFS reply rmdir: %d\n", status);
487 return status;
488}
489
490/*
491 * The READDIR implementation is somewhat hackish - we pass a temporary
492 * buffer to the encode function, which installs it in the receive
493 * the receive iovec. The decode function just parses the reply to make
494 * sure it is syntactically correct; the entries itself are decoded
495 * from nfs_readdir by calling the decode_entry function directly.
496 */
497static int
NeilBrown684f39b2018-12-03 11:30:30 +1100498nfs_proc_readdir(struct dentry *dentry, const struct cred *cred,
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -0400499 u64 cookie, struct page **pages, unsigned int count, bool plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500{
David Howells2b0143b2015-03-17 22:25:59 +0000501 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 struct nfs_readdirargs arg = {
503 .fh = NFS_FH(dir),
504 .cookie = cookie,
505 .count = count,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400506 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 };
508 struct rpc_message msg = {
509 .rpc_proc = &nfs_procedures[NFSPROC_READDIR],
510 .rpc_argp = &arg,
NeilBrowna52458b2018-12-03 11:30:31 +1100511 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 };
513 int status;
514
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 dprintk("NFS call readdir %d\n", (unsigned int)cookie);
516 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
517
Trond Myklebustc4812992007-09-28 17:11:45 -0400518 nfs_invalidate_atime(dir);
519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 dprintk("NFS reply readdir: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 return status;
522}
523
524static int
525nfs_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
526 struct nfs_fsstat *stat)
527{
528 struct nfs2_fsstat fsinfo;
Chuck Leverdead28d2006-03-20 13:44:23 -0500529 struct rpc_message msg = {
530 .rpc_proc = &nfs_procedures[NFSPROC_STATFS],
531 .rpc_argp = fhandle,
532 .rpc_resp = &fsinfo,
533 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 int status;
535
536 dprintk("NFS call statfs\n");
Trond Myklebust0e574af2005-10-27 22:12:38 -0400537 nfs_fattr_init(stat->fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500538 status = rpc_call_sync(server->client, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 dprintk("NFS reply statfs: %d\n", status);
540 if (status)
541 goto out;
542 stat->tbytes = (u64)fsinfo.blocks * fsinfo.bsize;
543 stat->fbytes = (u64)fsinfo.bfree * fsinfo.bsize;
544 stat->abytes = (u64)fsinfo.bavail * fsinfo.bsize;
545 stat->tfiles = 0;
546 stat->ffiles = 0;
547 stat->afiles = 0;
548out:
549 return status;
550}
551
552static int
553nfs_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
554 struct nfs_fsinfo *info)
555{
556 struct nfs2_fsstat fsinfo;
Chuck Leverdead28d2006-03-20 13:44:23 -0500557 struct rpc_message msg = {
558 .rpc_proc = &nfs_procedures[NFSPROC_STATFS],
559 .rpc_argp = fhandle,
560 .rpc_resp = &fsinfo,
561 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 int status;
563
564 dprintk("NFS call fsinfo\n");
Trond Myklebust0e574af2005-10-27 22:12:38 -0400565 nfs_fattr_init(info->fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500566 status = rpc_call_sync(server->client, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 dprintk("NFS reply fsinfo: %d\n", status);
568 if (status)
569 goto out;
570 info->rtmax = NFS_MAXDATA;
571 info->rtpref = fsinfo.tsize;
572 info->rtmult = fsinfo.bsize;
573 info->wtmax = NFS_MAXDATA;
574 info->wtpref = fsinfo.tsize;
575 info->wtmult = fsinfo.bsize;
576 info->dtpref = fsinfo.tsize;
577 info->maxfilesize = 0x7FFFFFFF;
578 info->lease_time = 0;
579out:
580 return status;
581}
582
583static int
584nfs_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
585 struct nfs_pathconf *info)
586{
587 info->max_link = 0;
588 info->max_namelen = NFS2_MAXNAMLEN;
589 return 0;
590}
591
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400592static int nfs_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400594 struct inode *inode = hdr->inode;
Fred Isamancd841602012-04-20 14:47:44 -0400595
Fred Isamancd841602012-04-20 14:47:44 -0400596 nfs_invalidate_atime(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400598 nfs_refresh_inode(inode, hdr->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 /* Emulate the eof flag, which isn't normally needed in NFSv2
600 * as it is guaranteed to always return the file attributes
601 */
Trond Myklebust71affe92019-08-26 20:41:16 -0400602 if ((hdr->res.count == 0 && hdr->args.count > 0) ||
603 hdr->args.offset + hdr->res.count >= hdr->res.fattr->size)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400604 hdr->res.eof = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 }
Trond Myklebustec06c092006-03-20 13:44:27 -0500606 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607}
608
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400609static void nfs_proc_read_setup(struct nfs_pgio_header *hdr,
610 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611{
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400612 msg->rpc_proc = &nfs_procedures[NFSPROC_READ];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613}
614
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400615static int nfs_proc_pgio_rpc_prepare(struct rpc_task *task,
616 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -0400617{
618 rpc_call_start(task);
NeilBrownef1820f2013-09-04 17:04:49 +1000619 return 0;
Bryan Schumakerea7c3302012-03-19 14:54:40 -0400620}
621
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400622static int nfs_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623{
Trond Myklebustd33d4be2019-08-27 07:03:28 -0400624 if (task->tk_status >= 0) {
625 hdr->res.count = hdr->args.count;
Trond Myklebusta08a8cd2015-02-26 17:36:09 -0500626 nfs_writeback_update_inode(hdr);
Trond Myklebustd33d4be2019-08-27 07:03:28 -0400627 }
Trond Myklebust788e7a82006-03-20 13:44:27 -0500628 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629}
630
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400631static void nfs_proc_write_setup(struct nfs_pgio_header *hdr,
Anna Schumakerfb91fb02018-05-04 16:22:48 -0400632 struct rpc_message *msg,
633 struct rpc_clnt **clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 /* Note: NFSv2 ignores @stable and always uses NFS_FILE_SYNC */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400636 hdr->args.stable = NFS_FILE_SYNC;
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400637 msg->rpc_proc = &nfs_procedures[NFSPROC_WRITE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638}
639
Fred Isaman0b7c0152012-04-20 14:47:39 -0400640static void nfs_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
641{
642 BUG();
643}
644
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645static void
Anna Schumakere9ae1ee2018-05-04 16:22:49 -0400646nfs_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg,
647 struct rpc_clnt **clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648{
649 BUG();
650}
651
652static int
653nfs_proc_lock(struct file *filp, int cmd, struct file_lock *fl)
654{
Al Viro496ad9a2013-01-23 17:07:38 -0500655 struct inode *inode = file_inode(filp);
Chuck Lever1093a602008-01-11 17:09:59 -0500656
Benjamin Coddingtonb1ece732017-04-11 12:50:11 -0400657 return nlmclnt_proc(NFS_SERVER(inode)->nlm_host, cmd, fl, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658}
659
Trond Myklebust21162712008-05-20 19:34:39 -0400660/* Helper functions for NFS lock bounds checking */
661#define NFS_LOCK32_OFFSET_MAX ((__s32)0x7fffffffUL)
662static int nfs_lock_check_bounds(const struct file_lock *fl)
663{
664 __s32 start, end;
665
666 start = (__s32)fl->fl_start;
667 if ((loff_t)start != fl->fl_start)
668 goto out_einval;
669
670 if (fl->fl_end != OFFSET_MAX) {
671 end = (__s32)fl->fl_end;
672 if ((loff_t)end != fl->fl_end)
673 goto out_einval;
674 } else
675 end = NFS_LOCK32_OFFSET_MAX;
676
677 if (start < 0 || start > end)
678 goto out_einval;
679 return 0;
680out_einval:
681 return -EINVAL;
682}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
Bryan Schumaker011e2a72012-06-20 15:53:43 -0400684static int nfs_have_delegation(struct inode *inode, fmode_t flags)
685{
686 return 0;
687}
688
Bryan Schumaker597d9282012-07-16 16:39:10 -0400689static const struct inode_operations nfs_dir_inode_operations = {
690 .create = nfs_create,
691 .lookup = nfs_lookup,
692 .link = nfs_link,
693 .unlink = nfs_unlink,
694 .symlink = nfs_symlink,
695 .mkdir = nfs_mkdir,
696 .rmdir = nfs_rmdir,
697 .mknod = nfs_mknod,
698 .rename = nfs_rename,
699 .permission = nfs_permission,
700 .getattr = nfs_getattr,
701 .setattr = nfs_setattr,
702};
703
704static const struct inode_operations nfs_file_inode_operations = {
705 .permission = nfs_permission,
706 .getattr = nfs_getattr,
707 .setattr = nfs_setattr,
708};
709
David Howells509de812006-08-22 20:06:11 -0400710const struct nfs_rpc_ops nfs_v2_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 .version = 2, /* protocol version */
712 .dentry_ops = &nfs_dentry_operations,
713 .dir_inode_ops = &nfs_dir_inode_operations,
J. Bruce Fields92cfc622005-06-22 17:16:22 +0000714 .file_inode_ops = &nfs_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -0400715 .file_ops = &nfs_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 .getroot = nfs_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -0400717 .submount = nfs_submount,
David Howellsf2aedb72019-12-10 07:31:13 -0500718 .try_get_tree = nfs_try_get_tree,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 .getattr = nfs_proc_getattr,
720 .setattr = nfs_proc_setattr,
721 .lookup = nfs_proc_lookup,
722 .access = NULL, /* access */
723 .readlink = nfs_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 .create = nfs_proc_create,
725 .remove = nfs_proc_remove,
726 .unlink_setup = nfs_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -0400727 .unlink_rpc_prepare = nfs_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 .unlink_done = nfs_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -0400729 .rename_setup = nfs_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -0400730 .rename_rpc_prepare = nfs_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -0400731 .rename_done = nfs_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 .link = nfs_proc_link,
733 .symlink = nfs_proc_symlink,
734 .mkdir = nfs_proc_mkdir,
735 .rmdir = nfs_proc_rmdir,
736 .readdir = nfs_proc_readdir,
737 .mknod = nfs_proc_mknod,
738 .statfs = nfs_proc_statfs,
739 .fsinfo = nfs_proc_fsinfo,
740 .pathconf = nfs_proc_pathconf,
Chuck Leverf796f8b2010-12-14 14:55:10 +0000741 .decode_dirent = nfs2_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -0400742 .pgio_rpc_prepare = nfs_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 .read_setup = nfs_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -0500744 .read_done = nfs_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 .write_setup = nfs_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -0500746 .write_done = nfs_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 .commit_setup = nfs_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -0400748 .commit_rpc_prepare = nfs_proc_commit_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 .lock = nfs_proc_lock,
Trond Myklebust21162712008-05-20 19:34:39 -0400750 .lock_check_bounds = nfs_lock_check_bounds,
Trond Myklebust7fe5c392009-03-19 15:35:50 -0400751 .close_context = nfs_close_context,
Bryan Schumaker011e2a72012-06-20 15:53:43 -0400752 .have_delegation = nfs_have_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -0400753 .alloc_client = nfs_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +0000754 .init_client = nfs_init_client,
Bryan Schumakercdb7eced2012-06-20 15:53:45 -0400755 .free_client = nfs_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -0400756 .create_server = nfs_create_server,
757 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758};