blob: 2e7579626cf01dfa66ce8b980357b9b59e0c0db6 [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;
Peter Zijlstraf5d39b02022-08-22 13:18:22 +020039 __set_current_state(TASK_KILLABLE|TASK_FREEZABLE_UNSAFE);
40 schedule_timeout(NFS_JUKEBOX_RETRY_TIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 res = -ERESTARTSYS;
Matthew Wilcox150030b2007-12-06 16:24:39 -050042 } while (!fatal_signal_pending(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 return res;
44}
45
Chuck Leverdead28d2006-03-20 13:44:23 -050046#define rpc_call_sync(clnt, msg, flags) nfs3_rpc_wrapper(clnt, msg, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48static int
Chuck Lever006ea732006-03-20 13:44:14 -050049nfs3_async_handle_jukebox(struct rpc_task *task, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050{
Andy Adamsoneb96d5c2012-11-27 10:34:19 -050051 if (task->tk_status != -EJUKEBOX)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 return 0;
Ye Bin79d534f2021-08-18 10:02:52 +080053 nfs_inc_stats(inode, NFSIOS_DELAY);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 task->tk_status = 0;
55 rpc_restart_call(task);
56 rpc_delay(task, NFS_JUKEBOX_RETRY_TIME);
57 return 1;
58}
59
J. Bruce Fields03c21732006-01-03 09:55:48 +010060static int
61do_proc_get_root(struct rpc_clnt *client, struct nfs_fh *fhandle,
62 struct nfs_fsinfo *info)
63{
Chuck Leverdead28d2006-03-20 13:44:23 -050064 struct rpc_message msg = {
65 .rpc_proc = &nfs3_procedures[NFS3PROC_FSINFO],
66 .rpc_argp = fhandle,
67 .rpc_resp = info,
68 };
J. Bruce Fields03c21732006-01-03 09:55:48 +010069 int status;
70
Harvey Harrison3110ff82008-05-02 13:42:44 -070071 dprintk("%s: call fsinfo\n", __func__);
J. Bruce Fields03c21732006-01-03 09:55:48 +010072 nfs_fattr_init(info->fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -050073 status = rpc_call_sync(client, &msg, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -070074 dprintk("%s: reply fsinfo: %d\n", __func__, status);
Trond Myklebust08660042012-08-20 12:42:15 -040075 if (status == 0 && !(info->fattr->valid & NFS_ATTR_FATTR)) {
Chuck Leverdead28d2006-03-20 13:44:23 -050076 msg.rpc_proc = &nfs3_procedures[NFS3PROC_GETATTR];
77 msg.rpc_resp = info->fattr;
78 status = rpc_call_sync(client, &msg, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -070079 dprintk("%s: reply getattr: %d\n", __func__, status);
J. Bruce Fields03c21732006-01-03 09:55:48 +010080 }
81 return status;
82}
83
Linus Torvalds1da177e2005-04-16 15:20:36 -070084/*
David Howells54ceac42006-08-22 20:06:13 -040085 * Bare-bones access to getattr: this is for nfs_get_root/nfs_get_sb
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 */
87static int
88nfs3_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
89 struct nfs_fsinfo *info)
90{
91 int status;
92
J. Bruce Fields03c21732006-01-03 09:55:48 +010093 status = do_proc_get_root(server->client, fhandle, info);
David Howells5006a762006-08-22 20:06:12 -040094 if (status && server->nfs_client->cl_rpcclient != server->client)
95 status = do_proc_get_root(server->nfs_client->cl_rpcclient, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 return status;
97}
98
99/*
100 * One function for each procedure in the NFS protocol.
101 */
102static int
103nfs3_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
Anna Schumaker2ef61e02021-10-22 13:11:07 -0400104 struct nfs_fattr *fattr, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
Chuck Leverdead28d2006-03-20 13:44:23 -0500106 struct rpc_message msg = {
107 .rpc_proc = &nfs3_procedures[NFS3PROC_GETATTR],
108 .rpc_argp = fhandle,
109 .rpc_resp = fattr,
110 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 int status;
Trond Myklebustc74dfe92020-01-06 15:39:37 -0500112 unsigned short task_flags = 0;
113
114 /* Is this is an attribute revalidation, subject to softreval? */
115 if (inode && (server->flags & NFS_MOUNT_SOFTREVAL))
116 task_flags |= RPC_TASK_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118 dprintk("NFS call getattr\n");
Trond Myklebust0e574af2005-10-27 22:12:38 -0400119 nfs_fattr_init(fattr);
Trond Myklebustc74dfe92020-01-06 15:39:37 -0500120 status = rpc_call_sync(server->client, &msg, task_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 dprintk("NFS reply getattr: %d\n", status);
122 return status;
123}
124
125static int
126nfs3_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
127 struct iattr *sattr)
128{
David Howells2b0143b2015-03-17 22:25:59 +0000129 struct inode *inode = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 struct nfs3_sattrargs arg = {
131 .fh = NFS_FH(inode),
132 .sattr = sattr,
133 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500134 struct rpc_message msg = {
135 .rpc_proc = &nfs3_procedures[NFS3PROC_SETATTR],
136 .rpc_argp = &arg,
137 .rpc_resp = fattr,
138 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 int status;
140
141 dprintk("NFS call setattr\n");
Trond Myklebust659bfcd2008-06-10 19:39:41 -0400142 if (sattr->ia_valid & ATTR_FILE)
143 msg.rpc_cred = nfs_file_cred(sattr->ia_file);
Trond Myklebust0e574af2005-10-27 22:12:38 -0400144 nfs_fattr_init(fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500145 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
chendtdbc898a2018-03-29 16:13:09 +0800146 if (status == 0) {
Su Yanjunfe1e8dbe2019-12-25 11:37:57 +0800147 nfs_setattr_update_inode(inode, sattr, fattr);
chendtdbc898a2018-03-29 16:13:09 +0800148 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
149 nfs_zap_acl_cache(inode);
chendtdbc898a2018-03-29 16:13:09 +0800150 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 dprintk("NFS reply setattr: %d\n", status);
152 return status;
153}
154
155static int
Trond Myklebust5f447cb2020-10-19 12:40:59 -0400156__nfs3_proc_lookup(struct inode *dir, const char *name, size_t len,
157 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
158 unsigned short task_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 struct nfs3_diropargs arg = {
161 .fh = NFS_FH(dir),
Trond Myklebust5f447cb2020-10-19 12:40:59 -0400162 .name = name,
163 .len = len
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 };
165 struct nfs3_diropres res = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 .fh = fhandle,
167 .fattr = fattr
168 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500169 struct rpc_message msg = {
170 .rpc_proc = &nfs3_procedures[NFS3PROC_LOOKUP],
171 .rpc_argp = &arg,
172 .rpc_resp = &res,
173 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 int status;
Trond Myklebustf7b37b82020-01-14 12:06:34 -0500175
Trond Myklebuste1fb4d02010-04-16 16:22:47 -0400176 res.dir_attr = nfs_alloc_fattr();
177 if (res.dir_attr == NULL)
178 return -ENOMEM;
179
Trond Myklebust0e574af2005-10-27 22:12:38 -0400180 nfs_fattr_init(fattr);
Trond Myklebustf7b37b82020-01-14 12:06:34 -0500181 status = rpc_call_sync(NFS_CLIENT(dir), &msg, task_flags);
Trond Myklebuste1fb4d02010-04-16 16:22:47 -0400182 nfs_refresh_inode(dir, res.dir_attr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500183 if (status >= 0 && !(fattr->valid & NFS_ATTR_FATTR)) {
184 msg.rpc_proc = &nfs3_procedures[NFS3PROC_GETATTR];
185 msg.rpc_argp = fhandle;
186 msg.rpc_resp = fattr;
Trond Myklebustf7b37b82020-01-14 12:06:34 -0500187 status = rpc_call_sync(NFS_CLIENT(dir), &msg, task_flags);
Chuck Leverdead28d2006-03-20 13:44:23 -0500188 }
Trond Myklebuste1fb4d02010-04-16 16:22:47 -0400189 nfs_free_fattr(res.dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 dprintk("NFS reply lookup: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 return status;
192}
193
Trond Myklebust5f447cb2020-10-19 12:40:59 -0400194static int
195nfs3_proc_lookup(struct inode *dir, struct dentry *dentry,
Anna Schumaker9558a002021-10-22 13:11:04 -0400196 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Trond Myklebust5f447cb2020-10-19 12:40:59 -0400197{
198 unsigned short task_flags = 0;
199
200 /* Is this is an attribute revalidation, subject to softreval? */
201 if (nfs_lookup_is_soft_revalidate(dentry))
202 task_flags |= RPC_TASK_TIMEOUT;
203
204 dprintk("NFS call lookup %pd2\n", dentry);
205 return __nfs3_proc_lookup(dir, dentry->d_name.name,
206 dentry->d_name.len, fhandle, fattr,
207 task_flags);
208}
209
Trond Myklebust3c5e9a52020-10-19 12:45:53 -0400210static int nfs3_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
Anna Schumakerba4bc8d2021-10-22 13:11:05 -0400211 struct nfs_fattr *fattr)
Trond Myklebust3c5e9a52020-10-19 12:45:53 -0400212{
213 const char dotdot[] = "..";
214 const size_t len = strlen(dotdot);
215 unsigned short task_flags = 0;
216
217 if (NFS_SERVER(inode)->flags & NFS_MOUNT_SOFTREVAL)
218 task_flags |= RPC_TASK_TIMEOUT;
219
220 return __nfs3_proc_lookup(inode, dotdot, len, fhandle, fattr,
221 task_flags);
222}
223
NeilBrown73fbb3f2021-09-28 09:47:57 +1000224static int nfs3_proc_access(struct inode *inode, struct nfs_access_entry *entry,
225 const struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 struct nfs3_accessargs arg = {
228 .fh = NFS_FH(inode),
Anna Schumaker1750d922017-07-26 12:00:21 -0400229 .access = entry->mask,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 };
Trond Myklebustc407d412010-04-16 16:22:48 -0400231 struct nfs3_accessres res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 struct rpc_message msg = {
233 .rpc_proc = &nfs3_procedures[NFS3PROC_ACCESS],
234 .rpc_argp = &arg,
235 .rpc_resp = &res,
NeilBrown73fbb3f2021-09-28 09:47:57 +1000236 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 };
Trond Myklebustc407d412010-04-16 16:22:48 -0400238 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
240 dprintk("NFS call access\n");
Trond Myklebustc407d412010-04-16 16:22:48 -0400241 res.fattr = nfs_alloc_fattr();
242 if (res.fattr == NULL)
243 goto out;
244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
Trond Myklebustc407d412010-04-16 16:22:48 -0400246 nfs_refresh_inode(inode, res.fattr);
Trond Myklebusteda3e202017-07-11 17:54:33 -0400247 if (status == 0)
248 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -0400249 nfs_free_fattr(res.fattr);
250out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 dprintk("NFS reply access: %d\n", status);
252 return status;
253}
254
255static int nfs3_proc_readlink(struct inode *inode, struct page *page,
256 unsigned int pgbase, unsigned int pglen)
257{
Trond Myklebust3b14d652010-04-16 16:22:50 -0400258 struct nfs_fattr *fattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 struct nfs3_readlinkargs args = {
260 .fh = NFS_FH(inode),
261 .pgbase = pgbase,
262 .pglen = pglen,
263 .pages = &page
264 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500265 struct rpc_message msg = {
266 .rpc_proc = &nfs3_procedures[NFS3PROC_READLINK],
267 .rpc_argp = &args,
Chuck Leverdead28d2006-03-20 13:44:23 -0500268 };
Trond Myklebust3b14d652010-04-16 16:22:50 -0400269 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
271 dprintk("NFS call readlink\n");
Trond Myklebust3b14d652010-04-16 16:22:50 -0400272 fattr = nfs_alloc_fattr();
273 if (fattr == NULL)
274 goto out;
275 msg.rpc_resp = fattr;
276
Chuck Leverdead28d2006-03-20 13:44:23 -0500277 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
Trond Myklebust3b14d652010-04-16 16:22:50 -0400278 nfs_refresh_inode(inode, fattr);
279 nfs_free_fattr(fattr);
280out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 dprintk("NFS reply readlink: %d\n", status);
282 return status;
283}
284
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400285struct nfs3_createdata {
286 struct rpc_message msg;
287 union {
288 struct nfs3_createargs create;
289 struct nfs3_mkdirargs mkdir;
290 struct nfs3_symlinkargs symlink;
291 struct nfs3_mknodargs mknod;
292 } arg;
293 struct nfs3_diropres res;
294 struct nfs_fh fh;
295 struct nfs_fattr fattr;
296 struct nfs_fattr dir_attr;
297};
298
299static struct nfs3_createdata *nfs3_alloc_createdata(void)
300{
301 struct nfs3_createdata *data;
302
303 data = kzalloc(sizeof(*data), GFP_KERNEL);
304 if (data != NULL) {
305 data->msg.rpc_argp = &data->arg;
306 data->msg.rpc_resp = &data->res;
307 data->res.fh = &data->fh;
308 data->res.fattr = &data->fattr;
309 data->res.dir_attr = &data->dir_attr;
310 nfs_fattr_init(data->res.fattr);
311 nfs_fattr_init(data->res.dir_attr);
312 }
313 return data;
314}
315
Benjamin Coddington17fd6e42019-09-13 08:29:03 -0400316static struct dentry *
317nfs3_do_create(struct inode *dir, struct dentry *dentry, struct nfs3_createdata *data)
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400318{
319 int status;
320
321 status = rpc_call_sync(NFS_CLIENT(dir), &data->msg, 0);
322 nfs_post_op_update_inode(dir, data->res.dir_attr);
Benjamin Coddington17fd6e42019-09-13 08:29:03 -0400323 if (status != 0)
324 return ERR_PTR(status);
325
Anna Schumakercc6f3292021-10-22 13:11:10 -0400326 return nfs_add_or_obtain(dentry, data->res.fh, data->res.fattr);
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400327}
328
329static void nfs3_free_createdata(struct nfs3_createdata *data)
330{
331 kfree(data);
332}
333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334/*
335 * Create a regular file.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 */
337static int
338nfs3_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +0200339 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340{
Christoph Hellwig013cdf12013-12-20 05:16:53 -0800341 struct posix_acl *default_acl, *acl;
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400342 struct nfs3_createdata *data;
Benjamin Coddington17fd6e42019-09-13 08:29:03 -0400343 struct dentry *d_alias;
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400344 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Al Viro6de14722013-09-16 10:53:17 -0400346 dprintk("NFS call create %pd\n", dentry);
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400347
348 data = nfs3_alloc_createdata();
349 if (data == NULL)
350 goto out;
351
352 data->msg.rpc_proc = &nfs3_procedures[NFS3PROC_CREATE];
353 data->arg.create.fh = NFS_FH(dir);
354 data->arg.create.name = dentry->d_name.name;
355 data->arg.create.len = dentry->d_name.len;
356 data->arg.create.sattr = sattr;
357
358 data->arg.create.createmode = NFS3_CREATE_UNCHECKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 if (flags & O_EXCL) {
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400360 data->arg.create.createmode = NFS3_CREATE_EXCLUSIVE;
Trond Myklebusta9943d12013-08-20 21:04:11 -0400361 data->arg.create.verifier[0] = cpu_to_be32(jiffies);
362 data->arg.create.verifier[1] = cpu_to_be32(current->pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 }
364
Christoph Hellwig013cdf12013-12-20 05:16:53 -0800365 status = posix_acl_create(dir, &sattr->ia_mode, &default_acl, &acl);
366 if (status)
367 goto out;
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000368
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400369 for (;;) {
Benjamin Coddington17fd6e42019-09-13 08:29:03 -0400370 d_alias = nfs3_do_create(dir, dentry, data);
371 status = PTR_ERR_OR_ZERO(d_alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400373 if (status != -ENOTSUPP)
374 break;
375 /* If the server doesn't support the exclusive creation
376 * semantics, try again with simple 'guarded' mode. */
377 switch (data->arg.create.createmode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 case NFS3_CREATE_EXCLUSIVE:
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400379 data->arg.create.createmode = NFS3_CREATE_GUARDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 break;
381
382 case NFS3_CREATE_GUARDED:
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400383 data->arg.create.createmode = NFS3_CREATE_UNCHECKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 break;
385
386 case NFS3_CREATE_UNCHECKED:
Gao Xiang1fcb6fc2021-06-18 12:20:55 +0800387 goto out_release_acls;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 }
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400389 nfs_fattr_init(data->res.dir_attr);
390 nfs_fattr_init(data->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 }
392
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 if (status != 0)
Christoph Hellwig013cdf12013-12-20 05:16:53 -0800394 goto out_release_acls;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Benjamin Coddington17fd6e42019-09-13 08:29:03 -0400396 if (d_alias)
397 dentry = d_alias;
398
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 /* When we created the file with exclusive semantics, make
400 * sure we set the attributes afterwards. */
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400401 if (data->arg.create.createmode == NFS3_CREATE_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 dprintk("NFS call setattr (post-create)\n");
403
404 if (!(sattr->ia_valid & ATTR_ATIME_SET))
405 sattr->ia_valid |= ATTR_ATIME;
406 if (!(sattr->ia_valid & ATTR_MTIME_SET))
407 sattr->ia_valid |= ATTR_MTIME;
408
409 /* Note: we could use a guarded setattr here, but I'm
410 * not sure this buys us anything (and I'd have
411 * to revamp the NFSv3 XDR code) */
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400412 status = nfs3_proc_setattr(dentry, data->res.fattr, sattr);
David Howells2b0143b2015-03-17 22:25:59 +0000413 nfs_post_op_update_inode(d_inode(dentry), data->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 dprintk("NFS reply setattr (post-create): %d\n", status);
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400415 if (status != 0)
Benjamin Coddington17fd6e42019-09-13 08:29:03 -0400416 goto out_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 }
Christoph Hellwig013cdf12013-12-20 05:16:53 -0800418
David Howells2b0143b2015-03-17 22:25:59 +0000419 status = nfs3_proc_setacls(d_inode(dentry), acl, default_acl);
Christoph Hellwig013cdf12013-12-20 05:16:53 -0800420
Benjamin Coddington17fd6e42019-09-13 08:29:03 -0400421out_dput:
422 dput(d_alias);
Christoph Hellwig013cdf12013-12-20 05:16:53 -0800423out_release_acls:
424 posix_acl_release(acl);
425 posix_acl_release(default_acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426out:
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400427 nfs3_free_createdata(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 dprintk("NFS reply create: %d\n", status);
429 return status;
430}
431
432static int
Trond Myklebust912678d2018-03-20 16:43:15 -0400433nfs3_proc_remove(struct inode *dir, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434{
Trond Myklebust4fdc17b2007-07-14 15:39:57 -0400435 struct nfs_removeargs arg = {
436 .fh = NFS_FH(dir),
Trond Myklebust912678d2018-03-20 16:43:15 -0400437 .name = dentry->d_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -0400439 struct nfs_removeres res;
440 struct rpc_message msg = {
441 .rpc_proc = &nfs3_procedures[NFS3PROC_REMOVE],
442 .rpc_argp = &arg,
443 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 };
Trond Myklebustd3468902010-04-16 16:22:50 -0400445 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
Trond Myklebust912678d2018-03-20 16:43:15 -0400447 dprintk("NFS call remove %pd2\n", dentry);
Trond Myklebustd3468902010-04-16 16:22:50 -0400448 res.dir_attr = nfs_alloc_fattr();
449 if (res.dir_attr == NULL)
450 goto out;
451
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
Trond Myklebustd3468902010-04-16 16:22:50 -0400453 nfs_post_op_update_inode(dir, res.dir_attr);
454 nfs_free_fattr(res.dir_attr);
455out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 dprintk("NFS reply remove: %d\n", status);
457 return status;
458}
459
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400460static void
Trond Myklebusted7e9ad2018-05-30 16:11:52 -0400461nfs3_proc_unlink_setup(struct rpc_message *msg,
462 struct dentry *dentry,
463 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 msg->rpc_proc = &nfs3_procedures[NFS3PROC_REMOVE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466}
467
Bryan Schumaker34e137c2012-03-19 14:54:41 -0400468static void nfs3_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
469{
470 rpc_call_start(task);
471}
472
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473static int
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400474nfs3_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400476 struct nfs_removeres *res;
477 if (nfs3_async_handle_jukebox(task, dir))
478 return 0;
479 res = task->tk_msg.rpc_resp;
Trond Myklebustd3468902010-04-16 16:22:50 -0400480 nfs_post_op_update_inode(dir, res->dir_attr);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400481 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482}
483
Jeff Laytond3d41522010-09-17 17:31:57 -0400484static void
Trond Myklebustf2c2c552018-03-20 16:43:16 -0400485nfs3_proc_rename_setup(struct rpc_message *msg,
486 struct dentry *old_dentry,
487 struct dentry *new_dentry)
Jeff Laytond3d41522010-09-17 17:31:57 -0400488{
489 msg->rpc_proc = &nfs3_procedures[NFS3PROC_RENAME];
490}
491
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -0400492static void nfs3_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
493{
494 rpc_call_start(task);
495}
496
Jeff Laytond3d41522010-09-17 17:31:57 -0400497static int
498nfs3_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
499 struct inode *new_dir)
500{
501 struct nfs_renameres *res;
502
503 if (nfs3_async_handle_jukebox(task, old_dir))
504 return 0;
505 res = task->tk_msg.rpc_resp;
506
507 nfs_post_op_update_inode(old_dir, res->old_fattr);
508 nfs_post_op_update_inode(new_dir, res->new_fattr);
509 return 1;
510}
511
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512static int
Al Virobeffb8f2016-07-20 16:34:42 -0400513nfs3_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 struct nfs3_linkargs arg = {
516 .fromfh = NFS_FH(inode),
517 .tofh = NFS_FH(dir),
518 .toname = name->name,
519 .tolen = name->len
520 };
Trond Myklebust136f2622010-04-16 16:22:49 -0400521 struct nfs3_linkres res;
Chuck Leverdead28d2006-03-20 13:44:23 -0500522 struct rpc_message msg = {
523 .rpc_proc = &nfs3_procedures[NFS3PROC_LINK],
524 .rpc_argp = &arg,
525 .rpc_resp = &res,
526 };
Trond Myklebust136f2622010-04-16 16:22:49 -0400527 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
529 dprintk("NFS call link %s\n", name->name);
Trond Myklebust136f2622010-04-16 16:22:49 -0400530 res.fattr = nfs_alloc_fattr();
531 res.dir_attr = nfs_alloc_fattr();
532 if (res.fattr == NULL || res.dir_attr == NULL)
533 goto out;
534
Chuck Leverdead28d2006-03-20 13:44:23 -0500535 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
Trond Myklebust136f2622010-04-16 16:22:49 -0400536 nfs_post_op_update_inode(dir, res.dir_attr);
537 nfs_post_op_update_inode(inode, res.fattr);
538out:
539 nfs_free_fattr(res.dir_attr);
540 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 dprintk("NFS reply link: %d\n", status);
542 return status;
543}
544
545static int
Chuck Lever94a6d752006-08-22 20:06:23 -0400546nfs3_proc_symlink(struct inode *dir, struct dentry *dentry, struct page *page,
547 unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548{
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400549 struct nfs3_createdata *data;
Benjamin Coddington17fd6e42019-09-13 08:29:03 -0400550 struct dentry *d_alias;
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400551 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Chuck Lever94a6d752006-08-22 20:06:23 -0400553 if (len > NFS3_MAXPATHLEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 return -ENAMETOOLONG;
Chuck Lever4f390c12006-08-22 20:06:22 -0400555
Al Viro6de14722013-09-16 10:53:17 -0400556 dprintk("NFS call symlink %pd\n", dentry);
Chuck Lever94a6d752006-08-22 20:06:23 -0400557
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400558 data = nfs3_alloc_createdata();
559 if (data == NULL)
Chuck Lever4f390c12006-08-22 20:06:22 -0400560 goto out;
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400561 data->msg.rpc_proc = &nfs3_procedures[NFS3PROC_SYMLINK];
562 data->arg.symlink.fromfh = NFS_FH(dir);
563 data->arg.symlink.fromname = dentry->d_name.name;
564 data->arg.symlink.fromlen = dentry->d_name.len;
565 data->arg.symlink.pages = &page;
566 data->arg.symlink.pathlen = len;
567 data->arg.symlink.sattr = sattr;
568
Benjamin Coddington17fd6e42019-09-13 08:29:03 -0400569 d_alias = nfs3_do_create(dir, dentry, data);
570 status = PTR_ERR_OR_ZERO(d_alias);
571
572 if (status == 0)
573 dput(d_alias);
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400574
575 nfs3_free_createdata(data);
Chuck Lever4f390c12006-08-22 20:06:22 -0400576out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 dprintk("NFS reply symlink: %d\n", status);
578 return status;
579}
580
581static int
582nfs3_proc_mkdir(struct inode *dir, struct dentry *dentry, struct iattr *sattr)
583{
Christoph Hellwig013cdf12013-12-20 05:16:53 -0800584 struct posix_acl *default_acl, *acl;
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400585 struct nfs3_createdata *data;
Benjamin Coddington17fd6e42019-09-13 08:29:03 -0400586 struct dentry *d_alias;
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400587 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
Al Viro6de14722013-09-16 10:53:17 -0400589 dprintk("NFS call mkdir %pd\n", dentry);
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000590
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400591 data = nfs3_alloc_createdata();
592 if (data == NULL)
593 goto out;
594
Christoph Hellwig013cdf12013-12-20 05:16:53 -0800595 status = posix_acl_create(dir, &sattr->ia_mode, &default_acl, &acl);
596 if (status)
597 goto out;
598
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400599 data->msg.rpc_proc = &nfs3_procedures[NFS3PROC_MKDIR];
600 data->arg.mkdir.fh = NFS_FH(dir);
601 data->arg.mkdir.name = dentry->d_name.name;
602 data->arg.mkdir.len = dentry->d_name.len;
603 data->arg.mkdir.sattr = sattr;
604
Benjamin Coddington17fd6e42019-09-13 08:29:03 -0400605 d_alias = nfs3_do_create(dir, dentry, data);
606 status = PTR_ERR_OR_ZERO(d_alias);
607
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000608 if (status != 0)
Christoph Hellwig013cdf12013-12-20 05:16:53 -0800609 goto out_release_acls;
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400610
Benjamin Coddington17fd6e42019-09-13 08:29:03 -0400611 if (d_alias)
612 dentry = d_alias;
613
David Howells2b0143b2015-03-17 22:25:59 +0000614 status = nfs3_proc_setacls(d_inode(dentry), acl, default_acl);
Christoph Hellwig013cdf12013-12-20 05:16:53 -0800615
Benjamin Coddington17fd6e42019-09-13 08:29:03 -0400616 dput(d_alias);
Christoph Hellwig013cdf12013-12-20 05:16:53 -0800617out_release_acls:
618 posix_acl_release(acl);
619 posix_acl_release(default_acl);
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000620out:
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400621 nfs3_free_createdata(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 dprintk("NFS reply mkdir: %d\n", status);
623 return status;
624}
625
626static int
Al Virobeffb8f2016-07-20 16:34:42 -0400627nfs3_proc_rmdir(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628{
Trond Myklebust39967dd2010-04-16 16:22:50 -0400629 struct nfs_fattr *dir_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 struct nfs3_diropargs arg = {
631 .fh = NFS_FH(dir),
632 .name = name->name,
633 .len = name->len
634 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500635 struct rpc_message msg = {
636 .rpc_proc = &nfs3_procedures[NFS3PROC_RMDIR],
637 .rpc_argp = &arg,
Chuck Leverdead28d2006-03-20 13:44:23 -0500638 };
Trond Myklebust39967dd2010-04-16 16:22:50 -0400639 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
641 dprintk("NFS call rmdir %s\n", name->name);
Trond Myklebust39967dd2010-04-16 16:22:50 -0400642 dir_attr = nfs_alloc_fattr();
643 if (dir_attr == NULL)
644 goto out;
645
646 msg.rpc_resp = dir_attr;
Chuck Leverdead28d2006-03-20 13:44:23 -0500647 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
Trond Myklebust39967dd2010-04-16 16:22:50 -0400648 nfs_post_op_update_inode(dir, dir_attr);
649 nfs_free_fattr(dir_attr);
650out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 dprintk("NFS reply rmdir: %d\n", status);
652 return status;
653}
654
655/*
656 * The READDIR implementation is somewhat hackish - we pass the user buffer
657 * to the encode function, which installs it in the receive iovec.
658 * The decode function itself doesn't perform any decoding, it just makes
659 * sure the reply is syntactically correct.
660 *
661 * Also note that this implementation handles both plain readdir and
662 * readdirplus.
663 */
Trond Myklebust82e22a52020-11-02 17:34:23 -0500664static int nfs3_proc_readdir(struct nfs_readdir_arg *nr_arg,
665 struct nfs_readdir_res *nr_res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666{
Trond Myklebust82e22a52020-11-02 17:34:23 -0500667 struct inode *dir = d_inode(nr_arg->dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 struct nfs3_readdirargs arg = {
669 .fh = NFS_FH(dir),
Trond Myklebust82e22a52020-11-02 17:34:23 -0500670 .cookie = nr_arg->cookie,
671 .plus = nr_arg->plus,
672 .count = nr_arg->page_len,
673 .pages = nr_arg->pages
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 };
675 struct nfs3_readdirres res = {
Trond Myklebust82e22a52020-11-02 17:34:23 -0500676 .verf = nr_res->verf,
677 .plus = nr_arg->plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 };
679 struct rpc_message msg = {
680 .rpc_proc = &nfs3_procedures[NFS3PROC_READDIR],
681 .rpc_argp = &arg,
682 .rpc_resp = &res,
Trond Myklebust82e22a52020-11-02 17:34:23 -0500683 .rpc_cred = nr_arg->cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 };
Trond Myklebustaa49b4c2010-04-16 16:22:49 -0400685 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
Trond Myklebust82e22a52020-11-02 17:34:23 -0500687 if (nr_arg->plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 msg.rpc_proc = &nfs3_procedures[NFS3PROC_READDIRPLUS];
Trond Myklebust82e22a52020-11-02 17:34:23 -0500689 if (arg.cookie)
690 memcpy(arg.verf, nr_arg->verf, sizeof(arg.verf));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
Trond Myklebust82e22a52020-11-02 17:34:23 -0500692 dprintk("NFS call readdir%s %llu\n", nr_arg->plus ? "plus" : "",
693 (unsigned long long)nr_arg->cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
Trond Myklebustaa49b4c2010-04-16 16:22:49 -0400695 res.dir_attr = nfs_alloc_fattr();
696 if (res.dir_attr == NULL)
697 goto out;
698
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
Trond Myklebustc4812992007-09-28 17:11:45 -0400700
701 nfs_invalidate_atime(dir);
Trond Myklebustaa49b4c2010-04-16 16:22:49 -0400702 nfs_refresh_inode(dir, res.dir_attr);
Trond Myklebustc4812992007-09-28 17:11:45 -0400703
Trond Myklebustaa49b4c2010-04-16 16:22:49 -0400704 nfs_free_fattr(res.dir_attr);
705out:
Trond Myklebust82e22a52020-11-02 17:34:23 -0500706 dprintk("NFS reply readdir%s: %d\n", nr_arg->plus ? "plus" : "",
707 status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 return status;
709}
710
711static int
712nfs3_proc_mknod(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
713 dev_t rdev)
714{
Christoph Hellwig013cdf12013-12-20 05:16:53 -0800715 struct posix_acl *default_acl, *acl;
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400716 struct nfs3_createdata *data;
Benjamin Coddington17fd6e42019-09-13 08:29:03 -0400717 struct dentry *d_alias;
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400718 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Al Viro6de14722013-09-16 10:53:17 -0400720 dprintk("NFS call mknod %pd %u:%u\n", dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 MAJOR(rdev), MINOR(rdev));
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000722
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400723 data = nfs3_alloc_createdata();
724 if (data == NULL)
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000725 goto out;
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400726
Christoph Hellwig013cdf12013-12-20 05:16:53 -0800727 status = posix_acl_create(dir, &sattr->ia_mode, &default_acl, &acl);
728 if (status)
729 goto out;
730
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400731 data->msg.rpc_proc = &nfs3_procedures[NFS3PROC_MKNOD];
732 data->arg.mknod.fh = NFS_FH(dir);
733 data->arg.mknod.name = dentry->d_name.name;
734 data->arg.mknod.len = dentry->d_name.len;
735 data->arg.mknod.sattr = sattr;
736 data->arg.mknod.rdev = rdev;
737
738 switch (sattr->ia_mode & S_IFMT) {
739 case S_IFBLK:
740 data->arg.mknod.type = NF3BLK;
741 break;
742 case S_IFCHR:
743 data->arg.mknod.type = NF3CHR;
744 break;
745 case S_IFIFO:
746 data->arg.mknod.type = NF3FIFO;
747 break;
748 case S_IFSOCK:
749 data->arg.mknod.type = NF3SOCK;
750 break;
751 default:
752 status = -EINVAL;
Gao Xiang1fcb6fc2021-06-18 12:20:55 +0800753 goto out_release_acls;
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400754 }
755
Benjamin Coddington17fd6e42019-09-13 08:29:03 -0400756 d_alias = nfs3_do_create(dir, dentry, data);
757 status = PTR_ERR_OR_ZERO(d_alias);
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000758 if (status != 0)
Christoph Hellwig013cdf12013-12-20 05:16:53 -0800759 goto out_release_acls;
760
Benjamin Coddington17fd6e42019-09-13 08:29:03 -0400761 if (d_alias)
762 dentry = d_alias;
763
David Howells2b0143b2015-03-17 22:25:59 +0000764 status = nfs3_proc_setacls(d_inode(dentry), acl, default_acl);
Christoph Hellwig013cdf12013-12-20 05:16:53 -0800765
Benjamin Coddington17fd6e42019-09-13 08:29:03 -0400766 dput(d_alias);
Christoph Hellwig013cdf12013-12-20 05:16:53 -0800767out_release_acls:
768 posix_acl_release(acl);
769 posix_acl_release(default_acl);
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000770out:
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400771 nfs3_free_createdata(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 dprintk("NFS reply mknod: %d\n", status);
773 return status;
774}
775
776static int
777nfs3_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
778 struct nfs_fsstat *stat)
779{
Chuck Leverdead28d2006-03-20 13:44:23 -0500780 struct rpc_message msg = {
781 .rpc_proc = &nfs3_procedures[NFS3PROC_FSSTAT],
782 .rpc_argp = fhandle,
783 .rpc_resp = stat,
784 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 int status;
786
787 dprintk("NFS call fsstat\n");
Trond Myklebust0e574af2005-10-27 22:12:38 -0400788 nfs_fattr_init(stat->fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500789 status = rpc_call_sync(server->client, &msg, 0);
Chuck Leverd141d972010-09-21 16:55:47 -0400790 dprintk("NFS reply fsstat: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 return status;
792}
793
794static int
EG Keizer37ca8f52008-08-19 16:34:36 -0400795do_proc_fsinfo(struct rpc_clnt *client, struct nfs_fh *fhandle,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 struct nfs_fsinfo *info)
797{
Chuck Leverdead28d2006-03-20 13:44:23 -0500798 struct rpc_message msg = {
799 .rpc_proc = &nfs3_procedures[NFS3PROC_FSINFO],
800 .rpc_argp = fhandle,
801 .rpc_resp = info,
802 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 int status;
804
805 dprintk("NFS call fsinfo\n");
Trond Myklebust0e574af2005-10-27 22:12:38 -0400806 nfs_fattr_init(info->fattr);
EG Keizer37ca8f52008-08-19 16:34:36 -0400807 status = rpc_call_sync(client, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 dprintk("NFS reply fsinfo: %d\n", status);
809 return status;
810}
811
EG Keizer37ca8f52008-08-19 16:34:36 -0400812/*
813 * Bare-bones access to fsinfo: this is for nfs_get_root/nfs_get_sb via
814 * nfs_create_server
815 */
816static int
817nfs3_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
818 struct nfs_fsinfo *info)
819{
820 int status;
821
822 status = do_proc_fsinfo(server->client, fhandle, info);
823 if (status && server->nfs_client->cl_rpcclient != server->client)
824 status = do_proc_fsinfo(server->nfs_client->cl_rpcclient, fhandle, info);
825 return status;
826}
827
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828static int
829nfs3_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
830 struct nfs_pathconf *info)
831{
Chuck Leverdead28d2006-03-20 13:44:23 -0500832 struct rpc_message msg = {
833 .rpc_proc = &nfs3_procedures[NFS3PROC_PATHCONF],
834 .rpc_argp = fhandle,
835 .rpc_resp = info,
836 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 int status;
838
839 dprintk("NFS call pathconf\n");
Trond Myklebust0e574af2005-10-27 22:12:38 -0400840 nfs_fattr_init(info->fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500841 status = rpc_call_sync(server->client, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 dprintk("NFS reply pathconf: %d\n", status);
843 return status;
844}
845
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400846static int nfs3_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400848 struct inode *inode = hdr->inode;
Trond Myklebust8d8928d2018-03-05 12:03:00 -0500849 struct nfs_server *server = NFS_SERVER(inode);
Fred Isamancd841602012-04-20 14:47:44 -0400850
Trond Myklebust16cecdf2014-06-22 12:55:11 -0400851 if (hdr->pgio_done_cb != NULL)
852 return hdr->pgio_done_cb(task, hdr);
853
Fred Isamancd841602012-04-20 14:47:44 -0400854 if (nfs3_async_handle_jukebox(task, inode))
Trond Myklebustec06c092006-03-20 13:44:27 -0500855 return -EAGAIN;
Trond Myklebust8850df92007-09-28 17:20:07 -0400856
Trond Myklebust8d8928d2018-03-05 12:03:00 -0500857 if (task->tk_status >= 0 && !server->read_hdrsize)
858 cmpxchg(&server->read_hdrsize, 0, hdr->res.replen);
859
Fred Isamancd841602012-04-20 14:47:44 -0400860 nfs_invalidate_atime(inode);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400861 nfs_refresh_inode(inode, &hdr->fattr);
Trond Myklebustec06c092006-03-20 13:44:27 -0500862 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863}
864
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400865static void nfs3_proc_read_setup(struct nfs_pgio_header *hdr,
866 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867{
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400868 msg->rpc_proc = &nfs3_procedures[NFS3PROC_READ];
Trond Myklebust8d8928d2018-03-05 12:03:00 -0500869 hdr->args.replen = NFS_SERVER(hdr->inode)->read_hdrsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870}
871
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400872static int nfs3_proc_pgio_rpc_prepare(struct rpc_task *task,
873 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -0400874{
875 rpc_call_start(task);
NeilBrownef1820f2013-09-04 17:04:49 +1000876 return 0;
Bryan Schumakerea7c3302012-03-19 14:54:40 -0400877}
878
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400879static int nfs3_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400881 struct inode *inode = hdr->inode;
Fred Isamancd841602012-04-20 14:47:44 -0400882
Trond Myklebust16cecdf2014-06-22 12:55:11 -0400883 if (hdr->pgio_done_cb != NULL)
884 return hdr->pgio_done_cb(task, hdr);
885
Fred Isamancd841602012-04-20 14:47:44 -0400886 if (nfs3_async_handle_jukebox(task, inode))
Trond Myklebust788e7a82006-03-20 13:44:27 -0500887 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 if (task->tk_status >= 0)
Trond Myklebusta08a8cd2015-02-26 17:36:09 -0500889 nfs_writeback_update_inode(hdr);
Trond Myklebust788e7a82006-03-20 13:44:27 -0500890 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891}
892
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400893static void nfs3_proc_write_setup(struct nfs_pgio_header *hdr,
Anna Schumakerfb91fb02018-05-04 16:22:48 -0400894 struct rpc_message *msg,
895 struct rpc_clnt **clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896{
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400897 msg->rpc_proc = &nfs3_procedures[NFS3PROC_WRITE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898}
899
Fred Isaman0b7c0152012-04-20 14:47:39 -0400900static void nfs3_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
901{
902 rpc_call_start(task);
903}
904
905static int nfs3_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906{
Trond Myklebust16cecdf2014-06-22 12:55:11 -0400907 if (data->commit_done_cb != NULL)
908 return data->commit_done_cb(task, data);
909
Chuck Lever006ea732006-03-20 13:44:14 -0500910 if (nfs3_async_handle_jukebox(task, data->inode))
Trond Myklebust788e7a82006-03-20 13:44:27 -0500911 return -EAGAIN;
Trond Myklebust9e08a3c2007-10-08 14:10:31 -0400912 nfs_refresh_inode(data->inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -0500913 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914}
915
Anna Schumakere9ae1ee22018-05-04 16:22:49 -0400916static void nfs3_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg,
917 struct rpc_clnt **clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918{
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400919 msg->rpc_proc = &nfs3_procedures[NFS3PROC_COMMIT];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920}
921
Trond Myklebustbb3393d2017-04-25 16:25:06 -0400922static void nfs3_nlm_alloc_call(void *data)
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -0400923{
924 struct nfs_lock_context *l_ctx = data;
925 if (l_ctx && test_bit(NFS_CONTEXT_UNLOCK, &l_ctx->open_context->flags)) {
926 get_nfs_open_context(l_ctx->open_context);
927 nfs_get_lock_context(l_ctx->open_context);
928 }
929}
930
Trond Myklebustbb3393d2017-04-25 16:25:06 -0400931static bool nfs3_nlm_unlock_prepare(struct rpc_task *task, void *data)
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -0400932{
933 struct nfs_lock_context *l_ctx = data;
934 if (l_ctx && test_bit(NFS_CONTEXT_UNLOCK, &l_ctx->open_context->flags))
935 return nfs_async_iocounter_wait(task, l_ctx);
936 return false;
937
938}
939
Trond Myklebustbb3393d2017-04-25 16:25:06 -0400940static void nfs3_nlm_release_call(void *data)
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -0400941{
942 struct nfs_lock_context *l_ctx = data;
943 struct nfs_open_context *ctx;
944 if (l_ctx && test_bit(NFS_CONTEXT_UNLOCK, &l_ctx->open_context->flags)) {
945 ctx = l_ctx->open_context;
946 nfs_put_lock_context(l_ctx);
947 put_nfs_open_context(ctx);
948 }
949}
950
Colin Ian King1b720402018-02-07 11:27:54 +0000951static const struct nlmclnt_operations nlmclnt_fl_close_lock_ops = {
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -0400952 .nlmclnt_alloc_call = nfs3_nlm_alloc_call,
953 .nlmclnt_unlock_prepare = nfs3_nlm_unlock_prepare,
954 .nlmclnt_release_call = nfs3_nlm_release_call,
955};
956
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957static int
958nfs3_proc_lock(struct file *filp, int cmd, struct file_lock *fl)
959{
Al Viro496ad9a2013-01-23 17:07:38 -0500960 struct inode *inode = file_inode(filp);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -0400961 struct nfs_lock_context *l_ctx = NULL;
962 struct nfs_open_context *ctx = nfs_file_open_context(filp);
963 int status;
Chuck Lever1093a602008-01-11 17:09:59 -0500964
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -0400965 if (fl->fl_flags & FL_CLOSE) {
966 l_ctx = nfs_get_lock_context(ctx);
967 if (IS_ERR(l_ctx))
968 l_ctx = NULL;
969 else
970 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
971 }
972
973 status = nlmclnt_proc(NFS_SERVER(inode)->nlm_host, cmd, fl, l_ctx);
974
975 if (l_ctx)
976 nfs_put_lock_context(l_ctx);
977
978 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979}
980
Bryan Schumaker011e2a72012-06-20 15:53:43 -0400981static int nfs3_have_delegation(struct inode *inode, fmode_t flags)
982{
983 return 0;
984}
985
Bryan Schumakerab962912012-07-16 16:39:11 -0400986static const struct inode_operations nfs3_dir_inode_operations = {
987 .create = nfs_create,
988 .lookup = nfs_lookup,
989 .link = nfs_link,
990 .unlink = nfs_unlink,
991 .symlink = nfs_symlink,
992 .mkdir = nfs_mkdir,
993 .rmdir = nfs_rmdir,
994 .mknod = nfs_mknod,
995 .rename = nfs_rename,
996 .permission = nfs_permission,
997 .getattr = nfs_getattr,
998 .setattr = nfs_setattr,
Christoph Hellwig5f13ee92014-01-30 06:01:52 -0800999#ifdef CONFIG_NFS_V3_ACL
Christoph Hellwig74adf832014-06-18 11:07:03 +02001000 .listxattr = nfs3_listxattr,
Christoph Hellwig013cdf12013-12-20 05:16:53 -08001001 .get_acl = nfs3_get_acl,
1002 .set_acl = nfs3_set_acl,
1003#endif
Bryan Schumakerab962912012-07-16 16:39:11 -04001004};
1005
1006static const struct inode_operations nfs3_file_inode_operations = {
1007 .permission = nfs_permission,
1008 .getattr = nfs_getattr,
1009 .setattr = nfs_setattr,
Christoph Hellwig5f13ee92014-01-30 06:01:52 -08001010#ifdef CONFIG_NFS_V3_ACL
Christoph Hellwig74adf832014-06-18 11:07:03 +02001011 .listxattr = nfs3_listxattr,
Christoph Hellwig013cdf12013-12-20 05:16:53 -08001012 .get_acl = nfs3_get_acl,
1013 .set_acl = nfs3_set_acl,
1014#endif
Bryan Schumakerab962912012-07-16 16:39:11 -04001015};
1016
David Howells509de812006-08-22 20:06:11 -04001017const struct nfs_rpc_ops nfs_v3_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 .version = 3, /* protocol version */
1019 .dentry_ops = &nfs_dentry_operations,
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +00001020 .dir_inode_ops = &nfs3_dir_inode_operations,
1021 .file_inode_ops = &nfs3_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04001022 .file_ops = &nfs_file_operations,
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04001023 .nlmclnt_ops = &nlmclnt_fl_close_lock_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 .getroot = nfs3_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04001025 .submount = nfs_submount,
David Howellsf2aedb72019-12-10 07:31:13 -05001026 .try_get_tree = nfs_try_get_tree,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 .getattr = nfs3_proc_getattr,
1028 .setattr = nfs3_proc_setattr,
1029 .lookup = nfs3_proc_lookup,
Trond Myklebust3c5e9a52020-10-19 12:45:53 -04001030 .lookupp = nfs3_proc_lookupp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 .access = nfs3_proc_access,
1032 .readlink = nfs3_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 .create = nfs3_proc_create,
1034 .remove = nfs3_proc_remove,
1035 .unlink_setup = nfs3_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04001036 .unlink_rpc_prepare = nfs3_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 .unlink_done = nfs3_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04001038 .rename_setup = nfs3_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04001039 .rename_rpc_prepare = nfs3_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04001040 .rename_done = nfs3_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 .link = nfs3_proc_link,
1042 .symlink = nfs3_proc_symlink,
1043 .mkdir = nfs3_proc_mkdir,
1044 .rmdir = nfs3_proc_rmdir,
1045 .readdir = nfs3_proc_readdir,
1046 .mknod = nfs3_proc_mknod,
1047 .statfs = nfs3_proc_statfs,
1048 .fsinfo = nfs3_proc_fsinfo,
1049 .pathconf = nfs3_proc_pathconf,
1050 .decode_dirent = nfs3_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04001051 .pgio_rpc_prepare = nfs3_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 .read_setup = nfs3_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05001053 .read_done = nfs3_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 .write_setup = nfs3_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05001055 .write_done = nfs3_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 .commit_setup = nfs3_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04001057 .commit_rpc_prepare = nfs3_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05001058 .commit_done = nfs3_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 .lock = nfs3_proc_lock,
Christoph Hellwig013cdf12013-12-20 05:16:53 -08001060 .clear_acl_cache = forget_all_cached_acls,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04001061 .close_context = nfs_close_context,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04001062 .have_delegation = nfs3_have_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04001063 .alloc_client = nfs_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00001064 .init_client = nfs_init_client,
Bryan Schumakercdb7eced2012-06-20 15:53:45 -04001065 .free_client = nfs_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04001066 .create_server = nfs3_create_server,
1067 .clone_server = nfs3_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068};