blob: 1597eef40d54f6f7005d9326e154323b7f104fd3 [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/nfs3proc.c
4 *
5 * Client-side NFSv3 procedures stubs.
6 *
7 * Copyright (C) 1997, Olaf Kirch
8 */
9
10#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/errno.h>
12#include <linux/string.h>
13#include <linux/sunrpc/clnt.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/nfs.h>
16#include <linux/nfs3.h>
17#include <linux/nfs_fs.h>
18#include <linux/nfs_page.h>
19#include <linux/lockd/bind.h>
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +000020#include <linux/nfs_mount.h>
Jeff Laytond3103102011-12-01 22:44:39 +010021#include <linux/freezer.h>
Tejun Heo0a6be652014-02-03 14:31:07 -050022#include <linux/xattr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Chuck Lever006ea732006-03-20 13:44:14 -050024#include "iostat.h"
David Howellsf7b422b2006-06-09 09:34:33 -040025#include "internal.h"
Anna Schumaker00a36a102014-09-03 12:19:08 -040026#include "nfs3_fs.h"
Chuck Lever006ea732006-03-20 13:44:14 -050027
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#define NFSDBG_FACILITY NFSDBG_PROC
29
Andy Adamsoneb96d5c2012-11-27 10:34:19 -050030/* A wrapper to handle the EJUKEBOX error messages */
Linus Torvalds1da177e2005-04-16 15:20:36 -070031static int
32nfs3_rpc_wrapper(struct rpc_clnt *clnt, struct rpc_message *msg, int flags)
33{
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 do {
36 res = rpc_call_sync(clnt, msg, flags);
Andy Adamsoneb96d5c2012-11-27 10:34:19 -050037 if (res != -EJUKEBOX)
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 break;
Colin Cross416ad3c2013-05-06 23:50:06 +000039 freezable_schedule_timeout_killable_unsafe(NFS_JUKEBOX_RETRY_TIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 res = -ERESTARTSYS;
Matthew Wilcox150030b2007-12-06 16:24:39 -050041 } while (!fatal_signal_pending(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 return res;
43}
44
Chuck Leverdead28d2006-03-20 13:44:23 -050045#define rpc_call_sync(clnt, msg, flags) nfs3_rpc_wrapper(clnt, msg, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47static int
Chuck Lever006ea732006-03-20 13:44:14 -050048nfs3_async_handle_jukebox(struct rpc_task *task, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
Andy Adamsoneb96d5c2012-11-27 10:34:19 -050050 if (task->tk_status != -EJUKEBOX)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 return 0;
Ye Bin79d534f2021-08-18 10:02:52 +080052 nfs_inc_stats(inode, NFSIOS_DELAY);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 task->tk_status = 0;
54 rpc_restart_call(task);
55 rpc_delay(task, NFS_JUKEBOX_RETRY_TIME);
56 return 1;
57}
58
J. Bruce Fields03c21732006-01-03 09:55:48 +010059static int
60do_proc_get_root(struct rpc_clnt *client, struct nfs_fh *fhandle,
61 struct nfs_fsinfo *info)
62{
Chuck Leverdead28d2006-03-20 13:44:23 -050063 struct rpc_message msg = {
64 .rpc_proc = &nfs3_procedures[NFS3PROC_FSINFO],
65 .rpc_argp = fhandle,
66 .rpc_resp = info,
67 };
J. Bruce Fields03c21732006-01-03 09:55:48 +010068 int status;
69
Harvey Harrison3110ff82008-05-02 13:42:44 -070070 dprintk("%s: call fsinfo\n", __func__);
J. Bruce Fields03c21732006-01-03 09:55:48 +010071 nfs_fattr_init(info->fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -050072 status = rpc_call_sync(client, &msg, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -070073 dprintk("%s: reply fsinfo: %d\n", __func__, status);
Trond Myklebust08660042012-08-20 12:42:15 -040074 if (status == 0 && !(info->fattr->valid & NFS_ATTR_FATTR)) {
Chuck Leverdead28d2006-03-20 13:44:23 -050075 msg.rpc_proc = &nfs3_procedures[NFS3PROC_GETATTR];
76 msg.rpc_resp = info->fattr;
77 status = rpc_call_sync(client, &msg, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -070078 dprintk("%s: reply getattr: %d\n", __func__, status);
J. Bruce Fields03c21732006-01-03 09:55:48 +010079 }
80 return status;
81}
82
Linus Torvalds1da177e2005-04-16 15:20:36 -070083/*
David Howells54ceac42006-08-22 20:06:13 -040084 * Bare-bones access to getattr: this is for nfs_get_root/nfs_get_sb
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 */
86static int
87nfs3_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
88 struct nfs_fsinfo *info)
89{
90 int status;
91
J. Bruce Fields03c21732006-01-03 09:55:48 +010092 status = do_proc_get_root(server->client, fhandle, info);
David Howells5006a762006-08-22 20:06:12 -040093 if (status && server->nfs_client->cl_rpcclient != server->client)
94 status = do_proc_get_root(server->nfs_client->cl_rpcclient, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 return status;
96}
97
98/*
99 * One function for each procedure in the NFS protocol.
100 */
101static int
102nfs3_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
Anna Schumaker2ef61e02021-10-22 13:11:07 -0400103 struct nfs_fattr *fattr, 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 = &nfs3_procedures[NFS3PROC_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
125nfs3_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 nfs3_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 = &nfs3_procedures[NFS3PROC_SETATTR],
135 .rpc_argp = &arg,
136 .rpc_resp = fattr,
137 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 int status;
139
140 dprintk("NFS call setattr\n");
Trond Myklebust659bfcd2008-06-10 19:39:41 -0400141 if (sattr->ia_valid & ATTR_FILE)
142 msg.rpc_cred = nfs_file_cred(sattr->ia_file);
Trond Myklebust0e574af2005-10-27 22:12:38 -0400143 nfs_fattr_init(fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500144 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
chendtdbc898a2018-03-29 16:13:09 +0800145 if (status == 0) {
Su Yanjunfe1e8dbe2019-12-25 11:37:57 +0800146 nfs_setattr_update_inode(inode, sattr, fattr);
chendtdbc898a2018-03-29 16:13:09 +0800147 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
148 nfs_zap_acl_cache(inode);
chendtdbc898a2018-03-29 16:13:09 +0800149 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 dprintk("NFS reply setattr: %d\n", status);
151 return status;
152}
153
154static int
Trond Myklebust5f447cb2020-10-19 12:40:59 -0400155__nfs3_proc_lookup(struct inode *dir, const char *name, size_t len,
156 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
157 unsigned short task_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 struct nfs3_diropargs arg = {
160 .fh = NFS_FH(dir),
Trond Myklebust5f447cb2020-10-19 12:40:59 -0400161 .name = name,
162 .len = len
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 };
164 struct nfs3_diropres res = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 .fh = fhandle,
166 .fattr = fattr
167 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500168 struct rpc_message msg = {
169 .rpc_proc = &nfs3_procedures[NFS3PROC_LOOKUP],
170 .rpc_argp = &arg,
171 .rpc_resp = &res,
172 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 int status;
Trond Myklebustf7b37b82020-01-14 12:06:34 -0500174
Trond Myklebuste1fb4d02010-04-16 16:22:47 -0400175 res.dir_attr = nfs_alloc_fattr();
176 if (res.dir_attr == NULL)
177 return -ENOMEM;
178
Trond Myklebust0e574af2005-10-27 22:12:38 -0400179 nfs_fattr_init(fattr);
Trond Myklebustf7b37b82020-01-14 12:06:34 -0500180 status = rpc_call_sync(NFS_CLIENT(dir), &msg, task_flags);
Trond Myklebuste1fb4d02010-04-16 16:22:47 -0400181 nfs_refresh_inode(dir, res.dir_attr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500182 if (status >= 0 && !(fattr->valid & NFS_ATTR_FATTR)) {
183 msg.rpc_proc = &nfs3_procedures[NFS3PROC_GETATTR];
184 msg.rpc_argp = fhandle;
185 msg.rpc_resp = fattr;
Trond Myklebustf7b37b82020-01-14 12:06:34 -0500186 status = rpc_call_sync(NFS_CLIENT(dir), &msg, task_flags);
Chuck Leverdead28d2006-03-20 13:44:23 -0500187 }
Trond Myklebuste1fb4d02010-04-16 16:22:47 -0400188 nfs_free_fattr(res.dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 dprintk("NFS reply lookup: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 return status;
191}
192
Trond Myklebust5f447cb2020-10-19 12:40:59 -0400193static int
194nfs3_proc_lookup(struct inode *dir, struct dentry *dentry,
Anna Schumaker9558a002021-10-22 13:11:04 -0400195 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Trond Myklebust5f447cb2020-10-19 12:40:59 -0400196{
197 unsigned short task_flags = 0;
198
199 /* Is this is an attribute revalidation, subject to softreval? */
200 if (nfs_lookup_is_soft_revalidate(dentry))
201 task_flags |= RPC_TASK_TIMEOUT;
202
203 dprintk("NFS call lookup %pd2\n", dentry);
204 return __nfs3_proc_lookup(dir, dentry->d_name.name,
205 dentry->d_name.len, fhandle, fattr,
206 task_flags);
207}
208
Trond Myklebust3c5e9a52020-10-19 12:45:53 -0400209static int nfs3_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
Anna Schumakerba4bc8d2021-10-22 13:11:05 -0400210 struct nfs_fattr *fattr)
Trond Myklebust3c5e9a52020-10-19 12:45:53 -0400211{
212 const char dotdot[] = "..";
213 const size_t len = strlen(dotdot);
214 unsigned short task_flags = 0;
215
216 if (NFS_SERVER(inode)->flags & NFS_MOUNT_SOFTREVAL)
217 task_flags |= RPC_TASK_TIMEOUT;
218
219 return __nfs3_proc_lookup(inode, dotdot, len, fhandle, fattr,
220 task_flags);
221}
222
NeilBrown73fbb3f2021-09-28 09:47:57 +1000223static int nfs3_proc_access(struct inode *inode, struct nfs_access_entry *entry,
224 const struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 struct nfs3_accessargs arg = {
227 .fh = NFS_FH(inode),
Anna Schumaker1750d922017-07-26 12:00:21 -0400228 .access = entry->mask,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 };
Trond Myklebustc407d412010-04-16 16:22:48 -0400230 struct nfs3_accessres res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 struct rpc_message msg = {
232 .rpc_proc = &nfs3_procedures[NFS3PROC_ACCESS],
233 .rpc_argp = &arg,
234 .rpc_resp = &res,
NeilBrown73fbb3f2021-09-28 09:47:57 +1000235 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 };
Trond Myklebustc407d412010-04-16 16:22:48 -0400237 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
239 dprintk("NFS call access\n");
Trond Myklebustc407d412010-04-16 16:22:48 -0400240 res.fattr = nfs_alloc_fattr();
241 if (res.fattr == NULL)
242 goto out;
243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
Trond Myklebustc407d412010-04-16 16:22:48 -0400245 nfs_refresh_inode(inode, res.fattr);
Trond Myklebusteda3e202017-07-11 17:54:33 -0400246 if (status == 0)
247 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -0400248 nfs_free_fattr(res.fattr);
249out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 dprintk("NFS reply access: %d\n", status);
251 return status;
252}
253
254static int nfs3_proc_readlink(struct inode *inode, struct page *page,
255 unsigned int pgbase, unsigned int pglen)
256{
Trond Myklebust3b14d652010-04-16 16:22:50 -0400257 struct nfs_fattr *fattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 struct nfs3_readlinkargs args = {
259 .fh = NFS_FH(inode),
260 .pgbase = pgbase,
261 .pglen = pglen,
262 .pages = &page
263 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500264 struct rpc_message msg = {
265 .rpc_proc = &nfs3_procedures[NFS3PROC_READLINK],
266 .rpc_argp = &args,
Chuck Leverdead28d2006-03-20 13:44:23 -0500267 };
Trond Myklebust3b14d652010-04-16 16:22:50 -0400268 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
270 dprintk("NFS call readlink\n");
Trond Myklebust3b14d652010-04-16 16:22:50 -0400271 fattr = nfs_alloc_fattr();
272 if (fattr == NULL)
273 goto out;
274 msg.rpc_resp = fattr;
275
Chuck Leverdead28d2006-03-20 13:44:23 -0500276 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
Trond Myklebust3b14d652010-04-16 16:22:50 -0400277 nfs_refresh_inode(inode, fattr);
278 nfs_free_fattr(fattr);
279out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 dprintk("NFS reply readlink: %d\n", status);
281 return status;
282}
283
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400284struct nfs3_createdata {
285 struct rpc_message msg;
286 union {
287 struct nfs3_createargs create;
288 struct nfs3_mkdirargs mkdir;
289 struct nfs3_symlinkargs symlink;
290 struct nfs3_mknodargs mknod;
291 } arg;
292 struct nfs3_diropres res;
293 struct nfs_fh fh;
294 struct nfs_fattr fattr;
295 struct nfs_fattr dir_attr;
296};
297
298static struct nfs3_createdata *nfs3_alloc_createdata(void)
299{
300 struct nfs3_createdata *data;
301
302 data = kzalloc(sizeof(*data), GFP_KERNEL);
303 if (data != NULL) {
304 data->msg.rpc_argp = &data->arg;
305 data->msg.rpc_resp = &data->res;
306 data->res.fh = &data->fh;
307 data->res.fattr = &data->fattr;
308 data->res.dir_attr = &data->dir_attr;
309 nfs_fattr_init(data->res.fattr);
310 nfs_fattr_init(data->res.dir_attr);
311 }
312 return data;
313}
314
Benjamin Coddington17fd6e42019-09-13 08:29:03 -0400315static struct dentry *
316nfs3_do_create(struct inode *dir, struct dentry *dentry, struct nfs3_createdata *data)
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400317{
318 int status;
319
320 status = rpc_call_sync(NFS_CLIENT(dir), &data->msg, 0);
321 nfs_post_op_update_inode(dir, data->res.dir_attr);
Benjamin Coddington17fd6e42019-09-13 08:29:03 -0400322 if (status != 0)
323 return ERR_PTR(status);
324
Anna Schumakercc6f3292021-10-22 13:11:10 -0400325 return nfs_add_or_obtain(dentry, data->res.fh, data->res.fattr);
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400326}
327
328static void nfs3_free_createdata(struct nfs3_createdata *data)
329{
330 kfree(data);
331}
332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333/*
334 * Create a regular file.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 */
336static int
337nfs3_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +0200338 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339{
Christoph Hellwig013cdf12013-12-20 05:16:53 -0800340 struct posix_acl *default_acl, *acl;
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400341 struct nfs3_createdata *data;
Benjamin Coddington17fd6e42019-09-13 08:29:03 -0400342 struct dentry *d_alias;
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400343 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
Al Viro6de14722013-09-16 10:53:17 -0400345 dprintk("NFS call create %pd\n", dentry);
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400346
347 data = nfs3_alloc_createdata();
348 if (data == NULL)
349 goto out;
350
351 data->msg.rpc_proc = &nfs3_procedures[NFS3PROC_CREATE];
352 data->arg.create.fh = NFS_FH(dir);
353 data->arg.create.name = dentry->d_name.name;
354 data->arg.create.len = dentry->d_name.len;
355 data->arg.create.sattr = sattr;
356
357 data->arg.create.createmode = NFS3_CREATE_UNCHECKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 if (flags & O_EXCL) {
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400359 data->arg.create.createmode = NFS3_CREATE_EXCLUSIVE;
Trond Myklebusta9943d12013-08-20 21:04:11 -0400360 data->arg.create.verifier[0] = cpu_to_be32(jiffies);
361 data->arg.create.verifier[1] = cpu_to_be32(current->pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 }
363
Christoph Hellwig013cdf12013-12-20 05:16:53 -0800364 status = posix_acl_create(dir, &sattr->ia_mode, &default_acl, &acl);
365 if (status)
366 goto out;
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000367
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400368 for (;;) {
Benjamin Coddington17fd6e42019-09-13 08:29:03 -0400369 d_alias = nfs3_do_create(dir, dentry, data);
370 status = PTR_ERR_OR_ZERO(d_alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400372 if (status != -ENOTSUPP)
373 break;
374 /* If the server doesn't support the exclusive creation
375 * semantics, try again with simple 'guarded' mode. */
376 switch (data->arg.create.createmode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 case NFS3_CREATE_EXCLUSIVE:
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400378 data->arg.create.createmode = NFS3_CREATE_GUARDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 break;
380
381 case NFS3_CREATE_GUARDED:
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400382 data->arg.create.createmode = NFS3_CREATE_UNCHECKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 break;
384
385 case NFS3_CREATE_UNCHECKED:
Gao Xiang1fcb6fc2021-06-18 12:20:55 +0800386 goto out_release_acls;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 }
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400388 nfs_fattr_init(data->res.dir_attr);
389 nfs_fattr_init(data->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 }
391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 if (status != 0)
Christoph Hellwig013cdf12013-12-20 05:16:53 -0800393 goto out_release_acls;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Benjamin Coddington17fd6e42019-09-13 08:29:03 -0400395 if (d_alias)
396 dentry = d_alias;
397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 /* When we created the file with exclusive semantics, make
399 * sure we set the attributes afterwards. */
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400400 if (data->arg.create.createmode == NFS3_CREATE_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 dprintk("NFS call setattr (post-create)\n");
402
403 if (!(sattr->ia_valid & ATTR_ATIME_SET))
404 sattr->ia_valid |= ATTR_ATIME;
405 if (!(sattr->ia_valid & ATTR_MTIME_SET))
406 sattr->ia_valid |= ATTR_MTIME;
407
408 /* Note: we could use a guarded setattr here, but I'm
409 * not sure this buys us anything (and I'd have
410 * to revamp the NFSv3 XDR code) */
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400411 status = nfs3_proc_setattr(dentry, data->res.fattr, sattr);
David Howells2b0143b2015-03-17 22:25:59 +0000412 nfs_post_op_update_inode(d_inode(dentry), data->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 dprintk("NFS reply setattr (post-create): %d\n", status);
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400414 if (status != 0)
Benjamin Coddington17fd6e42019-09-13 08:29:03 -0400415 goto out_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 }
Christoph Hellwig013cdf12013-12-20 05:16:53 -0800417
David Howells2b0143b2015-03-17 22:25:59 +0000418 status = nfs3_proc_setacls(d_inode(dentry), acl, default_acl);
Christoph Hellwig013cdf12013-12-20 05:16:53 -0800419
Benjamin Coddington17fd6e42019-09-13 08:29:03 -0400420out_dput:
421 dput(d_alias);
Christoph Hellwig013cdf12013-12-20 05:16:53 -0800422out_release_acls:
423 posix_acl_release(acl);
424 posix_acl_release(default_acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425out:
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400426 nfs3_free_createdata(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 dprintk("NFS reply create: %d\n", status);
428 return status;
429}
430
431static int
Trond Myklebust912678d2018-03-20 16:43:15 -0400432nfs3_proc_remove(struct inode *dir, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433{
Trond Myklebust4fdc17b2007-07-14 15:39:57 -0400434 struct nfs_removeargs arg = {
435 .fh = NFS_FH(dir),
Trond Myklebust912678d2018-03-20 16:43:15 -0400436 .name = dentry->d_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -0400438 struct nfs_removeres res;
439 struct rpc_message msg = {
440 .rpc_proc = &nfs3_procedures[NFS3PROC_REMOVE],
441 .rpc_argp = &arg,
442 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 };
Trond Myklebustd3468902010-04-16 16:22:50 -0400444 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
Trond Myklebust912678d2018-03-20 16:43:15 -0400446 dprintk("NFS call remove %pd2\n", dentry);
Trond Myklebustd3468902010-04-16 16:22:50 -0400447 res.dir_attr = nfs_alloc_fattr();
448 if (res.dir_attr == NULL)
449 goto out;
450
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
Trond Myklebustd3468902010-04-16 16:22:50 -0400452 nfs_post_op_update_inode(dir, res.dir_attr);
453 nfs_free_fattr(res.dir_attr);
454out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 dprintk("NFS reply remove: %d\n", status);
456 return status;
457}
458
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400459static void
Trond Myklebusted7e9ad2018-05-30 16:11:52 -0400460nfs3_proc_unlink_setup(struct rpc_message *msg,
461 struct dentry *dentry,
462 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 msg->rpc_proc = &nfs3_procedures[NFS3PROC_REMOVE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465}
466
Bryan Schumaker34e137c2012-03-19 14:54:41 -0400467static void nfs3_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
468{
469 rpc_call_start(task);
470}
471
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472static int
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400473nfs3_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400475 struct nfs_removeres *res;
476 if (nfs3_async_handle_jukebox(task, dir))
477 return 0;
478 res = task->tk_msg.rpc_resp;
Trond Myklebustd3468902010-04-16 16:22:50 -0400479 nfs_post_op_update_inode(dir, res->dir_attr);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400480 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481}
482
Jeff Laytond3d41522010-09-17 17:31:57 -0400483static void
Trond Myklebustf2c2c552018-03-20 16:43:16 -0400484nfs3_proc_rename_setup(struct rpc_message *msg,
485 struct dentry *old_dentry,
486 struct dentry *new_dentry)
Jeff Laytond3d41522010-09-17 17:31:57 -0400487{
488 msg->rpc_proc = &nfs3_procedures[NFS3PROC_RENAME];
489}
490
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -0400491static void nfs3_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
492{
493 rpc_call_start(task);
494}
495
Jeff Laytond3d41522010-09-17 17:31:57 -0400496static int
497nfs3_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
498 struct inode *new_dir)
499{
500 struct nfs_renameres *res;
501
502 if (nfs3_async_handle_jukebox(task, old_dir))
503 return 0;
504 res = task->tk_msg.rpc_resp;
505
506 nfs_post_op_update_inode(old_dir, res->old_fattr);
507 nfs_post_op_update_inode(new_dir, res->new_fattr);
508 return 1;
509}
510
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511static int
Al Virobeffb8f2016-07-20 16:34:42 -0400512nfs3_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 struct nfs3_linkargs arg = {
515 .fromfh = NFS_FH(inode),
516 .tofh = NFS_FH(dir),
517 .toname = name->name,
518 .tolen = name->len
519 };
Trond Myklebust136f2622010-04-16 16:22:49 -0400520 struct nfs3_linkres res;
Chuck Leverdead28d2006-03-20 13:44:23 -0500521 struct rpc_message msg = {
522 .rpc_proc = &nfs3_procedures[NFS3PROC_LINK],
523 .rpc_argp = &arg,
524 .rpc_resp = &res,
525 };
Trond Myklebust136f2622010-04-16 16:22:49 -0400526 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
528 dprintk("NFS call link %s\n", name->name);
Trond Myklebust136f2622010-04-16 16:22:49 -0400529 res.fattr = nfs_alloc_fattr();
530 res.dir_attr = nfs_alloc_fattr();
531 if (res.fattr == NULL || res.dir_attr == NULL)
532 goto out;
533
Chuck Leverdead28d2006-03-20 13:44:23 -0500534 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
Trond Myklebust136f2622010-04-16 16:22:49 -0400535 nfs_post_op_update_inode(dir, res.dir_attr);
536 nfs_post_op_update_inode(inode, res.fattr);
537out:
538 nfs_free_fattr(res.dir_attr);
539 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 dprintk("NFS reply link: %d\n", status);
541 return status;
542}
543
544static int
Chuck Lever94a6d752006-08-22 20:06:23 -0400545nfs3_proc_symlink(struct inode *dir, struct dentry *dentry, struct page *page,
546 unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547{
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400548 struct nfs3_createdata *data;
Benjamin Coddington17fd6e42019-09-13 08:29:03 -0400549 struct dentry *d_alias;
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400550 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
Chuck Lever94a6d752006-08-22 20:06:23 -0400552 if (len > NFS3_MAXPATHLEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 return -ENAMETOOLONG;
Chuck Lever4f390c12006-08-22 20:06:22 -0400554
Al Viro6de14722013-09-16 10:53:17 -0400555 dprintk("NFS call symlink %pd\n", dentry);
Chuck Lever94a6d752006-08-22 20:06:23 -0400556
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400557 data = nfs3_alloc_createdata();
558 if (data == NULL)
Chuck Lever4f390c12006-08-22 20:06:22 -0400559 goto out;
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400560 data->msg.rpc_proc = &nfs3_procedures[NFS3PROC_SYMLINK];
561 data->arg.symlink.fromfh = NFS_FH(dir);
562 data->arg.symlink.fromname = dentry->d_name.name;
563 data->arg.symlink.fromlen = dentry->d_name.len;
564 data->arg.symlink.pages = &page;
565 data->arg.symlink.pathlen = len;
566 data->arg.symlink.sattr = sattr;
567
Benjamin Coddington17fd6e42019-09-13 08:29:03 -0400568 d_alias = nfs3_do_create(dir, dentry, data);
569 status = PTR_ERR_OR_ZERO(d_alias);
570
571 if (status == 0)
572 dput(d_alias);
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400573
574 nfs3_free_createdata(data);
Chuck Lever4f390c12006-08-22 20:06:22 -0400575out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 dprintk("NFS reply symlink: %d\n", status);
577 return status;
578}
579
580static int
581nfs3_proc_mkdir(struct inode *dir, struct dentry *dentry, struct iattr *sattr)
582{
Christoph Hellwig013cdf12013-12-20 05:16:53 -0800583 struct posix_acl *default_acl, *acl;
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400584 struct nfs3_createdata *data;
Benjamin Coddington17fd6e42019-09-13 08:29:03 -0400585 struct dentry *d_alias;
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400586 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
Al Viro6de14722013-09-16 10:53:17 -0400588 dprintk("NFS call mkdir %pd\n", dentry);
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000589
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400590 data = nfs3_alloc_createdata();
591 if (data == NULL)
592 goto out;
593
Christoph Hellwig013cdf12013-12-20 05:16:53 -0800594 status = posix_acl_create(dir, &sattr->ia_mode, &default_acl, &acl);
595 if (status)
596 goto out;
597
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400598 data->msg.rpc_proc = &nfs3_procedures[NFS3PROC_MKDIR];
599 data->arg.mkdir.fh = NFS_FH(dir);
600 data->arg.mkdir.name = dentry->d_name.name;
601 data->arg.mkdir.len = dentry->d_name.len;
602 data->arg.mkdir.sattr = sattr;
603
Benjamin Coddington17fd6e42019-09-13 08:29:03 -0400604 d_alias = nfs3_do_create(dir, dentry, data);
605 status = PTR_ERR_OR_ZERO(d_alias);
606
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000607 if (status != 0)
Christoph Hellwig013cdf12013-12-20 05:16:53 -0800608 goto out_release_acls;
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400609
Benjamin Coddington17fd6e42019-09-13 08:29:03 -0400610 if (d_alias)
611 dentry = d_alias;
612
David Howells2b0143b2015-03-17 22:25:59 +0000613 status = nfs3_proc_setacls(d_inode(dentry), acl, default_acl);
Christoph Hellwig013cdf12013-12-20 05:16:53 -0800614
Benjamin Coddington17fd6e42019-09-13 08:29:03 -0400615 dput(d_alias);
Christoph Hellwig013cdf12013-12-20 05:16:53 -0800616out_release_acls:
617 posix_acl_release(acl);
618 posix_acl_release(default_acl);
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000619out:
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400620 nfs3_free_createdata(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 dprintk("NFS reply mkdir: %d\n", status);
622 return status;
623}
624
625static int
Al Virobeffb8f2016-07-20 16:34:42 -0400626nfs3_proc_rmdir(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627{
Trond Myklebust39967dd2010-04-16 16:22:50 -0400628 struct nfs_fattr *dir_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 struct nfs3_diropargs arg = {
630 .fh = NFS_FH(dir),
631 .name = name->name,
632 .len = name->len
633 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500634 struct rpc_message msg = {
635 .rpc_proc = &nfs3_procedures[NFS3PROC_RMDIR],
636 .rpc_argp = &arg,
Chuck Leverdead28d2006-03-20 13:44:23 -0500637 };
Trond Myklebust39967dd2010-04-16 16:22:50 -0400638 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
640 dprintk("NFS call rmdir %s\n", name->name);
Trond Myklebust39967dd2010-04-16 16:22:50 -0400641 dir_attr = nfs_alloc_fattr();
642 if (dir_attr == NULL)
643 goto out;
644
645 msg.rpc_resp = dir_attr;
Chuck Leverdead28d2006-03-20 13:44:23 -0500646 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
Trond Myklebust39967dd2010-04-16 16:22:50 -0400647 nfs_post_op_update_inode(dir, dir_attr);
648 nfs_free_fattr(dir_attr);
649out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 dprintk("NFS reply rmdir: %d\n", status);
651 return status;
652}
653
654/*
655 * The READDIR implementation is somewhat hackish - we pass the user buffer
656 * to the encode function, which installs it in the receive iovec.
657 * The decode function itself doesn't perform any decoding, it just makes
658 * sure the reply is syntactically correct.
659 *
660 * Also note that this implementation handles both plain readdir and
661 * readdirplus.
662 */
Trond Myklebust82e22a52020-11-02 17:34:23 -0500663static int nfs3_proc_readdir(struct nfs_readdir_arg *nr_arg,
664 struct nfs_readdir_res *nr_res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665{
Trond Myklebust82e22a52020-11-02 17:34:23 -0500666 struct inode *dir = d_inode(nr_arg->dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 struct nfs3_readdirargs arg = {
668 .fh = NFS_FH(dir),
Trond Myklebust82e22a52020-11-02 17:34:23 -0500669 .cookie = nr_arg->cookie,
670 .plus = nr_arg->plus,
671 .count = nr_arg->page_len,
672 .pages = nr_arg->pages
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 };
674 struct nfs3_readdirres res = {
Trond Myklebust82e22a52020-11-02 17:34:23 -0500675 .verf = nr_res->verf,
676 .plus = nr_arg->plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 };
678 struct rpc_message msg = {
679 .rpc_proc = &nfs3_procedures[NFS3PROC_READDIR],
680 .rpc_argp = &arg,
681 .rpc_resp = &res,
Trond Myklebust82e22a52020-11-02 17:34:23 -0500682 .rpc_cred = nr_arg->cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 };
Trond Myklebustaa49b4c2010-04-16 16:22:49 -0400684 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Trond Myklebust82e22a52020-11-02 17:34:23 -0500686 if (nr_arg->plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 msg.rpc_proc = &nfs3_procedures[NFS3PROC_READDIRPLUS];
Trond Myklebust82e22a52020-11-02 17:34:23 -0500688 if (arg.cookie)
689 memcpy(arg.verf, nr_arg->verf, sizeof(arg.verf));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
Trond Myklebust82e22a52020-11-02 17:34:23 -0500691 dprintk("NFS call readdir%s %llu\n", nr_arg->plus ? "plus" : "",
692 (unsigned long long)nr_arg->cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Trond Myklebustaa49b4c2010-04-16 16:22:49 -0400694 res.dir_attr = nfs_alloc_fattr();
695 if (res.dir_attr == NULL)
696 goto out;
697
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
Trond Myklebustc4812992007-09-28 17:11:45 -0400699
700 nfs_invalidate_atime(dir);
Trond Myklebustaa49b4c2010-04-16 16:22:49 -0400701 nfs_refresh_inode(dir, res.dir_attr);
Trond Myklebustc4812992007-09-28 17:11:45 -0400702
Trond Myklebustaa49b4c2010-04-16 16:22:49 -0400703 nfs_free_fattr(res.dir_attr);
704out:
Trond Myklebust82e22a52020-11-02 17:34:23 -0500705 dprintk("NFS reply readdir%s: %d\n", nr_arg->plus ? "plus" : "",
706 status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 return status;
708}
709
710static int
711nfs3_proc_mknod(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
712 dev_t rdev)
713{
Christoph Hellwig013cdf12013-12-20 05:16:53 -0800714 struct posix_acl *default_acl, *acl;
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400715 struct nfs3_createdata *data;
Benjamin Coddington17fd6e42019-09-13 08:29:03 -0400716 struct dentry *d_alias;
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400717 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
Al Viro6de14722013-09-16 10:53:17 -0400719 dprintk("NFS call mknod %pd %u:%u\n", dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 MAJOR(rdev), MINOR(rdev));
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000721
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400722 data = nfs3_alloc_createdata();
723 if (data == NULL)
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000724 goto out;
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400725
Christoph Hellwig013cdf12013-12-20 05:16:53 -0800726 status = posix_acl_create(dir, &sattr->ia_mode, &default_acl, &acl);
727 if (status)
728 goto out;
729
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400730 data->msg.rpc_proc = &nfs3_procedures[NFS3PROC_MKNOD];
731 data->arg.mknod.fh = NFS_FH(dir);
732 data->arg.mknod.name = dentry->d_name.name;
733 data->arg.mknod.len = dentry->d_name.len;
734 data->arg.mknod.sattr = sattr;
735 data->arg.mknod.rdev = rdev;
736
737 switch (sattr->ia_mode & S_IFMT) {
738 case S_IFBLK:
739 data->arg.mknod.type = NF3BLK;
740 break;
741 case S_IFCHR:
742 data->arg.mknod.type = NF3CHR;
743 break;
744 case S_IFIFO:
745 data->arg.mknod.type = NF3FIFO;
746 break;
747 case S_IFSOCK:
748 data->arg.mknod.type = NF3SOCK;
749 break;
750 default:
751 status = -EINVAL;
Gao Xiang1fcb6fc2021-06-18 12:20:55 +0800752 goto out_release_acls;
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400753 }
754
Benjamin Coddington17fd6e42019-09-13 08:29:03 -0400755 d_alias = nfs3_do_create(dir, dentry, data);
756 status = PTR_ERR_OR_ZERO(d_alias);
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000757 if (status != 0)
Christoph Hellwig013cdf12013-12-20 05:16:53 -0800758 goto out_release_acls;
759
Benjamin Coddington17fd6e42019-09-13 08:29:03 -0400760 if (d_alias)
761 dentry = d_alias;
762
David Howells2b0143b2015-03-17 22:25:59 +0000763 status = nfs3_proc_setacls(d_inode(dentry), acl, default_acl);
Christoph Hellwig013cdf12013-12-20 05:16:53 -0800764
Benjamin Coddington17fd6e42019-09-13 08:29:03 -0400765 dput(d_alias);
Christoph Hellwig013cdf12013-12-20 05:16:53 -0800766out_release_acls:
767 posix_acl_release(acl);
768 posix_acl_release(default_acl);
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000769out:
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400770 nfs3_free_createdata(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 dprintk("NFS reply mknod: %d\n", status);
772 return status;
773}
774
775static int
776nfs3_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
777 struct nfs_fsstat *stat)
778{
Chuck Leverdead28d2006-03-20 13:44:23 -0500779 struct rpc_message msg = {
780 .rpc_proc = &nfs3_procedures[NFS3PROC_FSSTAT],
781 .rpc_argp = fhandle,
782 .rpc_resp = stat,
783 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 int status;
785
786 dprintk("NFS call fsstat\n");
Trond Myklebust0e574af2005-10-27 22:12:38 -0400787 nfs_fattr_init(stat->fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500788 status = rpc_call_sync(server->client, &msg, 0);
Chuck Leverd141d972010-09-21 16:55:47 -0400789 dprintk("NFS reply fsstat: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 return status;
791}
792
793static int
EG Keizer37ca8f52008-08-19 16:34:36 -0400794do_proc_fsinfo(struct rpc_clnt *client, struct nfs_fh *fhandle,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 struct nfs_fsinfo *info)
796{
Chuck Leverdead28d2006-03-20 13:44:23 -0500797 struct rpc_message msg = {
798 .rpc_proc = &nfs3_procedures[NFS3PROC_FSINFO],
799 .rpc_argp = fhandle,
800 .rpc_resp = info,
801 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 int status;
803
804 dprintk("NFS call fsinfo\n");
Trond Myklebust0e574af2005-10-27 22:12:38 -0400805 nfs_fattr_init(info->fattr);
EG Keizer37ca8f52008-08-19 16:34:36 -0400806 status = rpc_call_sync(client, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 dprintk("NFS reply fsinfo: %d\n", status);
808 return status;
809}
810
EG Keizer37ca8f52008-08-19 16:34:36 -0400811/*
812 * Bare-bones access to fsinfo: this is for nfs_get_root/nfs_get_sb via
813 * nfs_create_server
814 */
815static int
816nfs3_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
817 struct nfs_fsinfo *info)
818{
819 int status;
820
821 status = do_proc_fsinfo(server->client, fhandle, info);
822 if (status && server->nfs_client->cl_rpcclient != server->client)
823 status = do_proc_fsinfo(server->nfs_client->cl_rpcclient, fhandle, info);
824 return status;
825}
826
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827static int
828nfs3_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
829 struct nfs_pathconf *info)
830{
Chuck Leverdead28d2006-03-20 13:44:23 -0500831 struct rpc_message msg = {
832 .rpc_proc = &nfs3_procedures[NFS3PROC_PATHCONF],
833 .rpc_argp = fhandle,
834 .rpc_resp = info,
835 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 int status;
837
838 dprintk("NFS call pathconf\n");
Trond Myklebust0e574af2005-10-27 22:12:38 -0400839 nfs_fattr_init(info->fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500840 status = rpc_call_sync(server->client, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 dprintk("NFS reply pathconf: %d\n", status);
842 return status;
843}
844
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400845static int nfs3_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400847 struct inode *inode = hdr->inode;
Trond Myklebust8d8928d2018-03-05 12:03:00 -0500848 struct nfs_server *server = NFS_SERVER(inode);
Fred Isamancd841602012-04-20 14:47:44 -0400849
Trond Myklebust16cecdf2014-06-22 12:55:11 -0400850 if (hdr->pgio_done_cb != NULL)
851 return hdr->pgio_done_cb(task, hdr);
852
Fred Isamancd841602012-04-20 14:47:44 -0400853 if (nfs3_async_handle_jukebox(task, inode))
Trond Myklebustec06c092006-03-20 13:44:27 -0500854 return -EAGAIN;
Trond Myklebust8850df92007-09-28 17:20:07 -0400855
Trond Myklebust8d8928d2018-03-05 12:03:00 -0500856 if (task->tk_status >= 0 && !server->read_hdrsize)
857 cmpxchg(&server->read_hdrsize, 0, hdr->res.replen);
858
Fred Isamancd841602012-04-20 14:47:44 -0400859 nfs_invalidate_atime(inode);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400860 nfs_refresh_inode(inode, &hdr->fattr);
Trond Myklebustec06c092006-03-20 13:44:27 -0500861 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862}
863
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400864static void nfs3_proc_read_setup(struct nfs_pgio_header *hdr,
865 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866{
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400867 msg->rpc_proc = &nfs3_procedures[NFS3PROC_READ];
Trond Myklebust8d8928d2018-03-05 12:03:00 -0500868 hdr->args.replen = NFS_SERVER(hdr->inode)->read_hdrsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869}
870
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400871static int nfs3_proc_pgio_rpc_prepare(struct rpc_task *task,
872 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -0400873{
874 rpc_call_start(task);
NeilBrownef1820f2013-09-04 17:04:49 +1000875 return 0;
Bryan Schumakerea7c3302012-03-19 14:54:40 -0400876}
877
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400878static int nfs3_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400880 struct inode *inode = hdr->inode;
Fred Isamancd841602012-04-20 14:47:44 -0400881
Trond Myklebust16cecdf2014-06-22 12:55:11 -0400882 if (hdr->pgio_done_cb != NULL)
883 return hdr->pgio_done_cb(task, hdr);
884
Fred Isamancd841602012-04-20 14:47:44 -0400885 if (nfs3_async_handle_jukebox(task, inode))
Trond Myklebust788e7a82006-03-20 13:44:27 -0500886 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 if (task->tk_status >= 0)
Trond Myklebusta08a8cd2015-02-26 17:36:09 -0500888 nfs_writeback_update_inode(hdr);
Trond Myklebust788e7a82006-03-20 13:44:27 -0500889 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890}
891
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400892static void nfs3_proc_write_setup(struct nfs_pgio_header *hdr,
Anna Schumakerfb91fb02018-05-04 16:22:48 -0400893 struct rpc_message *msg,
894 struct rpc_clnt **clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895{
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400896 msg->rpc_proc = &nfs3_procedures[NFS3PROC_WRITE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897}
898
Fred Isaman0b7c0152012-04-20 14:47:39 -0400899static void nfs3_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
900{
901 rpc_call_start(task);
902}
903
904static int nfs3_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905{
Trond Myklebust16cecdf2014-06-22 12:55:11 -0400906 if (data->commit_done_cb != NULL)
907 return data->commit_done_cb(task, data);
908
Chuck Lever006ea732006-03-20 13:44:14 -0500909 if (nfs3_async_handle_jukebox(task, data->inode))
Trond Myklebust788e7a82006-03-20 13:44:27 -0500910 return -EAGAIN;
Trond Myklebust9e08a3c2007-10-08 14:10:31 -0400911 nfs_refresh_inode(data->inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -0500912 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913}
914
Anna Schumakere9ae1ee2018-05-04 16:22:49 -0400915static void nfs3_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg,
916 struct rpc_clnt **clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917{
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400918 msg->rpc_proc = &nfs3_procedures[NFS3PROC_COMMIT];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919}
920
Trond Myklebustbb3393d2017-04-25 16:25:06 -0400921static void nfs3_nlm_alloc_call(void *data)
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -0400922{
923 struct nfs_lock_context *l_ctx = data;
924 if (l_ctx && test_bit(NFS_CONTEXT_UNLOCK, &l_ctx->open_context->flags)) {
925 get_nfs_open_context(l_ctx->open_context);
926 nfs_get_lock_context(l_ctx->open_context);
927 }
928}
929
Trond Myklebustbb3393d2017-04-25 16:25:06 -0400930static bool nfs3_nlm_unlock_prepare(struct rpc_task *task, void *data)
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -0400931{
932 struct nfs_lock_context *l_ctx = data;
933 if (l_ctx && test_bit(NFS_CONTEXT_UNLOCK, &l_ctx->open_context->flags))
934 return nfs_async_iocounter_wait(task, l_ctx);
935 return false;
936
937}
938
Trond Myklebustbb3393d2017-04-25 16:25:06 -0400939static void nfs3_nlm_release_call(void *data)
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -0400940{
941 struct nfs_lock_context *l_ctx = data;
942 struct nfs_open_context *ctx;
943 if (l_ctx && test_bit(NFS_CONTEXT_UNLOCK, &l_ctx->open_context->flags)) {
944 ctx = l_ctx->open_context;
945 nfs_put_lock_context(l_ctx);
946 put_nfs_open_context(ctx);
947 }
948}
949
Colin Ian King1b720402018-02-07 11:27:54 +0000950static const struct nlmclnt_operations nlmclnt_fl_close_lock_ops = {
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -0400951 .nlmclnt_alloc_call = nfs3_nlm_alloc_call,
952 .nlmclnt_unlock_prepare = nfs3_nlm_unlock_prepare,
953 .nlmclnt_release_call = nfs3_nlm_release_call,
954};
955
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956static int
957nfs3_proc_lock(struct file *filp, int cmd, struct file_lock *fl)
958{
Al Viro496ad9a2013-01-23 17:07:38 -0500959 struct inode *inode = file_inode(filp);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -0400960 struct nfs_lock_context *l_ctx = NULL;
961 struct nfs_open_context *ctx = nfs_file_open_context(filp);
962 int status;
Chuck Lever1093a602008-01-11 17:09:59 -0500963
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -0400964 if (fl->fl_flags & FL_CLOSE) {
965 l_ctx = nfs_get_lock_context(ctx);
966 if (IS_ERR(l_ctx))
967 l_ctx = NULL;
968 else
969 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
970 }
971
972 status = nlmclnt_proc(NFS_SERVER(inode)->nlm_host, cmd, fl, l_ctx);
973
974 if (l_ctx)
975 nfs_put_lock_context(l_ctx);
976
977 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978}
979
Bryan Schumaker011e2a72012-06-20 15:53:43 -0400980static int nfs3_have_delegation(struct inode *inode, fmode_t flags)
981{
982 return 0;
983}
984
Bryan Schumakerab962912012-07-16 16:39:11 -0400985static const struct inode_operations nfs3_dir_inode_operations = {
986 .create = nfs_create,
987 .lookup = nfs_lookup,
988 .link = nfs_link,
989 .unlink = nfs_unlink,
990 .symlink = nfs_symlink,
991 .mkdir = nfs_mkdir,
992 .rmdir = nfs_rmdir,
993 .mknod = nfs_mknod,
994 .rename = nfs_rename,
995 .permission = nfs_permission,
996 .getattr = nfs_getattr,
997 .setattr = nfs_setattr,
Christoph Hellwig5f13ee92014-01-30 06:01:52 -0800998#ifdef CONFIG_NFS_V3_ACL
Christoph Hellwig74adf832014-06-18 11:07:03 +0200999 .listxattr = nfs3_listxattr,
Christoph Hellwig013cdf12013-12-20 05:16:53 -08001000 .get_acl = nfs3_get_acl,
1001 .set_acl = nfs3_set_acl,
1002#endif
Bryan Schumakerab962912012-07-16 16:39:11 -04001003};
1004
1005static const struct inode_operations nfs3_file_inode_operations = {
1006 .permission = nfs_permission,
1007 .getattr = nfs_getattr,
1008 .setattr = nfs_setattr,
Christoph Hellwig5f13ee92014-01-30 06:01:52 -08001009#ifdef CONFIG_NFS_V3_ACL
Christoph Hellwig74adf832014-06-18 11:07:03 +02001010 .listxattr = nfs3_listxattr,
Christoph Hellwig013cdf12013-12-20 05:16:53 -08001011 .get_acl = nfs3_get_acl,
1012 .set_acl = nfs3_set_acl,
1013#endif
Bryan Schumakerab962912012-07-16 16:39:11 -04001014};
1015
David Howells509de812006-08-22 20:06:11 -04001016const struct nfs_rpc_ops nfs_v3_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 .version = 3, /* protocol version */
1018 .dentry_ops = &nfs_dentry_operations,
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +00001019 .dir_inode_ops = &nfs3_dir_inode_operations,
1020 .file_inode_ops = &nfs3_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04001021 .file_ops = &nfs_file_operations,
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04001022 .nlmclnt_ops = &nlmclnt_fl_close_lock_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 .getroot = nfs3_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04001024 .submount = nfs_submount,
David Howellsf2aedb72019-12-10 07:31:13 -05001025 .try_get_tree = nfs_try_get_tree,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 .getattr = nfs3_proc_getattr,
1027 .setattr = nfs3_proc_setattr,
1028 .lookup = nfs3_proc_lookup,
Trond Myklebust3c5e9a52020-10-19 12:45:53 -04001029 .lookupp = nfs3_proc_lookupp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 .access = nfs3_proc_access,
1031 .readlink = nfs3_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 .create = nfs3_proc_create,
1033 .remove = nfs3_proc_remove,
1034 .unlink_setup = nfs3_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04001035 .unlink_rpc_prepare = nfs3_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 .unlink_done = nfs3_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04001037 .rename_setup = nfs3_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04001038 .rename_rpc_prepare = nfs3_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04001039 .rename_done = nfs3_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 .link = nfs3_proc_link,
1041 .symlink = nfs3_proc_symlink,
1042 .mkdir = nfs3_proc_mkdir,
1043 .rmdir = nfs3_proc_rmdir,
1044 .readdir = nfs3_proc_readdir,
1045 .mknod = nfs3_proc_mknod,
1046 .statfs = nfs3_proc_statfs,
1047 .fsinfo = nfs3_proc_fsinfo,
1048 .pathconf = nfs3_proc_pathconf,
1049 .decode_dirent = nfs3_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04001050 .pgio_rpc_prepare = nfs3_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 .read_setup = nfs3_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05001052 .read_done = nfs3_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 .write_setup = nfs3_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05001054 .write_done = nfs3_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 .commit_setup = nfs3_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04001056 .commit_rpc_prepare = nfs3_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05001057 .commit_done = nfs3_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 .lock = nfs3_proc_lock,
Christoph Hellwig013cdf12013-12-20 05:16:53 -08001059 .clear_acl_cache = forget_all_cached_acls,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04001060 .close_context = nfs_close_context,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04001061 .have_delegation = nfs3_have_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04001062 .alloc_client = nfs_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00001063 .init_client = nfs_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04001064 .free_client = nfs_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04001065 .create_server = nfs3_create_server,
1066 .clone_server = nfs3_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067};