blob: 5c4e23abc3451bc6a47fe9f0b7e5c956ce7e3de6 [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;
Jeff Laytonb68d69b2010-01-07 09:42:04 -050052 if (task->tk_status == -EJUKEBOX)
53 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,
Trond Myklebusta841b542018-04-07 13:50:59 -0400104 struct nfs_fattr *fattr, struct nfs4_label *label,
105 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106{
Chuck Leverdead28d2006-03-20 13:44:23 -0500107 struct rpc_message msg = {
108 .rpc_proc = &nfs3_procedures[NFS3PROC_GETATTR],
109 .rpc_argp = fhandle,
110 .rpc_resp = fattr,
111 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 int status;
Trond Myklebustc74dfe92020-01-06 15:39:37 -0500113 unsigned short task_flags = 0;
114
115 /* Is this is an attribute revalidation, subject to softreval? */
116 if (inode && (server->flags & NFS_MOUNT_SOFTREVAL))
117 task_flags |= RPC_TASK_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
119 dprintk("NFS call getattr\n");
Trond Myklebust0e574af2005-10-27 22:12:38 -0400120 nfs_fattr_init(fattr);
Trond Myklebustc74dfe92020-01-06 15:39:37 -0500121 status = rpc_call_sync(server->client, &msg, task_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 dprintk("NFS reply getattr: %d\n", status);
123 return status;
124}
125
126static int
127nfs3_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
128 struct iattr *sattr)
129{
David Howells2b0143b2015-03-17 22:25:59 +0000130 struct inode *inode = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 struct nfs3_sattrargs arg = {
132 .fh = NFS_FH(inode),
133 .sattr = sattr,
134 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500135 struct rpc_message msg = {
136 .rpc_proc = &nfs3_procedures[NFS3PROC_SETATTR],
137 .rpc_argp = &arg,
138 .rpc_resp = fattr,
139 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 int status;
141
142 dprintk("NFS call setattr\n");
Trond Myklebust659bfcd2008-06-10 19:39:41 -0400143 if (sattr->ia_valid & ATTR_FILE)
144 msg.rpc_cred = nfs_file_cred(sattr->ia_file);
Trond Myklebust0e574af2005-10-27 22:12:38 -0400145 nfs_fattr_init(fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500146 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
chendtdbc898a2018-03-29 16:13:09 +0800147 if (status == 0) {
Su Yanjunfe1e8dbe2019-12-25 11:37:57 +0800148 nfs_setattr_update_inode(inode, sattr, fattr);
chendtdbc898a2018-03-29 16:13:09 +0800149 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
150 nfs_zap_acl_cache(inode);
chendtdbc898a2018-03-29 16:13:09 +0800151 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 dprintk("NFS reply setattr: %d\n", status);
153 return status;
154}
155
156static int
Trond Myklebust5f447cb2020-10-19 12:40:59 -0400157__nfs3_proc_lookup(struct inode *dir, const char *name, size_t len,
158 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
159 unsigned short task_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 struct nfs3_diropargs arg = {
162 .fh = NFS_FH(dir),
Trond Myklebust5f447cb2020-10-19 12:40:59 -0400163 .name = name,
164 .len = len
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 };
166 struct nfs3_diropres res = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 .fh = fhandle,
168 .fattr = fattr
169 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500170 struct rpc_message msg = {
171 .rpc_proc = &nfs3_procedures[NFS3PROC_LOOKUP],
172 .rpc_argp = &arg,
173 .rpc_resp = &res,
174 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 int status;
Trond Myklebustf7b37b82020-01-14 12:06:34 -0500176
Trond Myklebuste1fb4d02010-04-16 16:22:47 -0400177 res.dir_attr = nfs_alloc_fattr();
178 if (res.dir_attr == NULL)
179 return -ENOMEM;
180
Trond Myklebust0e574af2005-10-27 22:12:38 -0400181 nfs_fattr_init(fattr);
Trond Myklebustf7b37b82020-01-14 12:06:34 -0500182 status = rpc_call_sync(NFS_CLIENT(dir), &msg, task_flags);
Trond Myklebuste1fb4d02010-04-16 16:22:47 -0400183 nfs_refresh_inode(dir, res.dir_attr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500184 if (status >= 0 && !(fattr->valid & NFS_ATTR_FATTR)) {
185 msg.rpc_proc = &nfs3_procedures[NFS3PROC_GETATTR];
186 msg.rpc_argp = fhandle;
187 msg.rpc_resp = fattr;
Trond Myklebustf7b37b82020-01-14 12:06:34 -0500188 status = rpc_call_sync(NFS_CLIENT(dir), &msg, task_flags);
Chuck Leverdead28d2006-03-20 13:44:23 -0500189 }
Trond Myklebuste1fb4d02010-04-16 16:22:47 -0400190 nfs_free_fattr(res.dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 dprintk("NFS reply lookup: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 return status;
193}
194
Trond Myklebust5f447cb2020-10-19 12:40:59 -0400195static int
196nfs3_proc_lookup(struct inode *dir, struct dentry *dentry,
197 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
198 struct nfs4_label *label)
199{
200 unsigned short task_flags = 0;
201
202 /* Is this is an attribute revalidation, subject to softreval? */
203 if (nfs_lookup_is_soft_revalidate(dentry))
204 task_flags |= RPC_TASK_TIMEOUT;
205
206 dprintk("NFS call lookup %pd2\n", dentry);
207 return __nfs3_proc_lookup(dir, dentry->d_name.name,
208 dentry->d_name.len, fhandle, fattr,
209 task_flags);
210}
211
Trond Myklebust3c5e9a52020-10-19 12:45:53 -0400212static int nfs3_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
213 struct nfs_fattr *fattr, struct nfs4_label *label)
214{
215 const char dotdot[] = "..";
216 const size_t len = strlen(dotdot);
217 unsigned short task_flags = 0;
218
219 if (NFS_SERVER(inode)->flags & NFS_MOUNT_SOFTREVAL)
220 task_flags |= RPC_TASK_TIMEOUT;
221
222 return __nfs3_proc_lookup(inode, dotdot, len, fhandle, fattr,
223 task_flags);
224}
225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226static int nfs3_proc_access(struct inode *inode, struct nfs_access_entry *entry)
227{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 struct nfs3_accessargs arg = {
229 .fh = NFS_FH(inode),
Anna Schumaker1750d922017-07-26 12:00:21 -0400230 .access = entry->mask,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 };
Trond Myklebustc407d412010-04-16 16:22:48 -0400232 struct nfs3_accessres res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 struct rpc_message msg = {
234 .rpc_proc = &nfs3_procedures[NFS3PROC_ACCESS],
235 .rpc_argp = &arg,
236 .rpc_resp = &res,
NeilBrowna52458b2018-12-03 11:30:31 +1100237 .rpc_cred = entry->cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 };
Trond Myklebustc407d412010-04-16 16:22:48 -0400239 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
241 dprintk("NFS call access\n");
Trond Myklebustc407d412010-04-16 16:22:48 -0400242 res.fattr = nfs_alloc_fattr();
243 if (res.fattr == NULL)
244 goto out;
245
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
Trond Myklebustc407d412010-04-16 16:22:48 -0400247 nfs_refresh_inode(inode, res.fattr);
Trond Myklebusteda3e202017-07-11 17:54:33 -0400248 if (status == 0)
249 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -0400250 nfs_free_fattr(res.fattr);
251out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 dprintk("NFS reply access: %d\n", status);
253 return status;
254}
255
256static int nfs3_proc_readlink(struct inode *inode, struct page *page,
257 unsigned int pgbase, unsigned int pglen)
258{
Trond Myklebust3b14d652010-04-16 16:22:50 -0400259 struct nfs_fattr *fattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 struct nfs3_readlinkargs args = {
261 .fh = NFS_FH(inode),
262 .pgbase = pgbase,
263 .pglen = pglen,
264 .pages = &page
265 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500266 struct rpc_message msg = {
267 .rpc_proc = &nfs3_procedures[NFS3PROC_READLINK],
268 .rpc_argp = &args,
Chuck Leverdead28d2006-03-20 13:44:23 -0500269 };
Trond Myklebust3b14d652010-04-16 16:22:50 -0400270 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272 dprintk("NFS call readlink\n");
Trond Myklebust3b14d652010-04-16 16:22:50 -0400273 fattr = nfs_alloc_fattr();
274 if (fattr == NULL)
275 goto out;
276 msg.rpc_resp = fattr;
277
Chuck Leverdead28d2006-03-20 13:44:23 -0500278 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
Trond Myklebust3b14d652010-04-16 16:22:50 -0400279 nfs_refresh_inode(inode, fattr);
280 nfs_free_fattr(fattr);
281out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 dprintk("NFS reply readlink: %d\n", status);
283 return status;
284}
285
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400286struct nfs3_createdata {
287 struct rpc_message msg;
288 union {
289 struct nfs3_createargs create;
290 struct nfs3_mkdirargs mkdir;
291 struct nfs3_symlinkargs symlink;
292 struct nfs3_mknodargs mknod;
293 } arg;
294 struct nfs3_diropres res;
295 struct nfs_fh fh;
296 struct nfs_fattr fattr;
297 struct nfs_fattr dir_attr;
298};
299
300static struct nfs3_createdata *nfs3_alloc_createdata(void)
301{
302 struct nfs3_createdata *data;
303
304 data = kzalloc(sizeof(*data), GFP_KERNEL);
305 if (data != NULL) {
306 data->msg.rpc_argp = &data->arg;
307 data->msg.rpc_resp = &data->res;
308 data->res.fh = &data->fh;
309 data->res.fattr = &data->fattr;
310 data->res.dir_attr = &data->dir_attr;
311 nfs_fattr_init(data->res.fattr);
312 nfs_fattr_init(data->res.dir_attr);
313 }
314 return data;
315}
316
Benjamin Coddington17fd6e42019-09-13 08:29:03 -0400317static struct dentry *
318nfs3_do_create(struct inode *dir, struct dentry *dentry, struct nfs3_createdata *data)
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400319{
320 int status;
321
322 status = rpc_call_sync(NFS_CLIENT(dir), &data->msg, 0);
323 nfs_post_op_update_inode(dir, data->res.dir_attr);
Benjamin Coddington17fd6e42019-09-13 08:29:03 -0400324 if (status != 0)
325 return ERR_PTR(status);
326
327 return nfs_add_or_obtain(dentry, data->res.fh, data->res.fattr, NULL);
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400328}
329
330static void nfs3_free_createdata(struct nfs3_createdata *data)
331{
332 kfree(data);
333}
334
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335/*
336 * Create a regular file.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 */
338static int
339nfs3_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +0200340 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341{
Christoph Hellwig013cdf12013-12-20 05:16:53 -0800342 struct posix_acl *default_acl, *acl;
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400343 struct nfs3_createdata *data;
Benjamin Coddington17fd6e42019-09-13 08:29:03 -0400344 struct dentry *d_alias;
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400345 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
Al Viro6de14722013-09-16 10:53:17 -0400347 dprintk("NFS call create %pd\n", dentry);
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400348
349 data = nfs3_alloc_createdata();
350 if (data == NULL)
351 goto out;
352
353 data->msg.rpc_proc = &nfs3_procedures[NFS3PROC_CREATE];
354 data->arg.create.fh = NFS_FH(dir);
355 data->arg.create.name = dentry->d_name.name;
356 data->arg.create.len = dentry->d_name.len;
357 data->arg.create.sattr = sattr;
358
359 data->arg.create.createmode = NFS3_CREATE_UNCHECKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 if (flags & O_EXCL) {
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400361 data->arg.create.createmode = NFS3_CREATE_EXCLUSIVE;
Trond Myklebusta9943d12013-08-20 21:04:11 -0400362 data->arg.create.verifier[0] = cpu_to_be32(jiffies);
363 data->arg.create.verifier[1] = cpu_to_be32(current->pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 }
365
Christoph Hellwig013cdf12013-12-20 05:16:53 -0800366 status = posix_acl_create(dir, &sattr->ia_mode, &default_acl, &acl);
367 if (status)
368 goto out;
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000369
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400370 for (;;) {
Benjamin Coddington17fd6e42019-09-13 08:29:03 -0400371 d_alias = nfs3_do_create(dir, dentry, data);
372 status = PTR_ERR_OR_ZERO(d_alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400374 if (status != -ENOTSUPP)
375 break;
376 /* If the server doesn't support the exclusive creation
377 * semantics, try again with simple 'guarded' mode. */
378 switch (data->arg.create.createmode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 case NFS3_CREATE_EXCLUSIVE:
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400380 data->arg.create.createmode = NFS3_CREATE_GUARDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 break;
382
383 case NFS3_CREATE_GUARDED:
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400384 data->arg.create.createmode = NFS3_CREATE_UNCHECKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 break;
386
387 case NFS3_CREATE_UNCHECKED:
388 goto out;
389 }
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400390 nfs_fattr_init(data->res.dir_attr);
391 nfs_fattr_init(data->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 }
393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 if (status != 0)
Christoph Hellwig013cdf12013-12-20 05:16:53 -0800395 goto out_release_acls;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
Benjamin Coddington17fd6e42019-09-13 08:29:03 -0400397 if (d_alias)
398 dentry = d_alias;
399
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 /* When we created the file with exclusive semantics, make
401 * sure we set the attributes afterwards. */
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400402 if (data->arg.create.createmode == NFS3_CREATE_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 dprintk("NFS call setattr (post-create)\n");
404
405 if (!(sattr->ia_valid & ATTR_ATIME_SET))
406 sattr->ia_valid |= ATTR_ATIME;
407 if (!(sattr->ia_valid & ATTR_MTIME_SET))
408 sattr->ia_valid |= ATTR_MTIME;
409
410 /* Note: we could use a guarded setattr here, but I'm
411 * not sure this buys us anything (and I'd have
412 * to revamp the NFSv3 XDR code) */
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400413 status = nfs3_proc_setattr(dentry, data->res.fattr, sattr);
David Howells2b0143b2015-03-17 22:25:59 +0000414 nfs_post_op_update_inode(d_inode(dentry), data->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 dprintk("NFS reply setattr (post-create): %d\n", status);
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400416 if (status != 0)
Benjamin Coddington17fd6e42019-09-13 08:29:03 -0400417 goto out_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 }
Christoph Hellwig013cdf12013-12-20 05:16:53 -0800419
David Howells2b0143b2015-03-17 22:25:59 +0000420 status = nfs3_proc_setacls(d_inode(dentry), acl, default_acl);
Christoph Hellwig013cdf12013-12-20 05:16:53 -0800421
Benjamin Coddington17fd6e42019-09-13 08:29:03 -0400422out_dput:
423 dput(d_alias);
Christoph Hellwig013cdf12013-12-20 05:16:53 -0800424out_release_acls:
425 posix_acl_release(acl);
426 posix_acl_release(default_acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427out:
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400428 nfs3_free_createdata(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 dprintk("NFS reply create: %d\n", status);
430 return status;
431}
432
433static int
Trond Myklebust912678d2018-03-20 16:43:15 -0400434nfs3_proc_remove(struct inode *dir, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{
Trond Myklebust4fdc17b2007-07-14 15:39:57 -0400436 struct nfs_removeargs arg = {
437 .fh = NFS_FH(dir),
Trond Myklebust912678d2018-03-20 16:43:15 -0400438 .name = dentry->d_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -0400440 struct nfs_removeres res;
441 struct rpc_message msg = {
442 .rpc_proc = &nfs3_procedures[NFS3PROC_REMOVE],
443 .rpc_argp = &arg,
444 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 };
Trond Myklebustd3468902010-04-16 16:22:50 -0400446 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Trond Myklebust912678d2018-03-20 16:43:15 -0400448 dprintk("NFS call remove %pd2\n", dentry);
Trond Myklebustd3468902010-04-16 16:22:50 -0400449 res.dir_attr = nfs_alloc_fattr();
450 if (res.dir_attr == NULL)
451 goto out;
452
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
Trond Myklebustd3468902010-04-16 16:22:50 -0400454 nfs_post_op_update_inode(dir, res.dir_attr);
455 nfs_free_fattr(res.dir_attr);
456out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 dprintk("NFS reply remove: %d\n", status);
458 return status;
459}
460
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400461static void
Trond Myklebusted7e9ad2018-05-30 16:11:52 -0400462nfs3_proc_unlink_setup(struct rpc_message *msg,
463 struct dentry *dentry,
464 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 msg->rpc_proc = &nfs3_procedures[NFS3PROC_REMOVE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467}
468
Bryan Schumaker34e137c2012-03-19 14:54:41 -0400469static void nfs3_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
470{
471 rpc_call_start(task);
472}
473
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474static int
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400475nfs3_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400477 struct nfs_removeres *res;
478 if (nfs3_async_handle_jukebox(task, dir))
479 return 0;
480 res = task->tk_msg.rpc_resp;
Trond Myklebustd3468902010-04-16 16:22:50 -0400481 nfs_post_op_update_inode(dir, res->dir_attr);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400482 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483}
484
Jeff Laytond3d41522010-09-17 17:31:57 -0400485static void
Trond Myklebustf2c2c552018-03-20 16:43:16 -0400486nfs3_proc_rename_setup(struct rpc_message *msg,
487 struct dentry *old_dentry,
488 struct dentry *new_dentry)
Jeff Laytond3d41522010-09-17 17:31:57 -0400489{
490 msg->rpc_proc = &nfs3_procedures[NFS3PROC_RENAME];
491}
492
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -0400493static void nfs3_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
494{
495 rpc_call_start(task);
496}
497
Jeff Laytond3d41522010-09-17 17:31:57 -0400498static int
499nfs3_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
500 struct inode *new_dir)
501{
502 struct nfs_renameres *res;
503
504 if (nfs3_async_handle_jukebox(task, old_dir))
505 return 0;
506 res = task->tk_msg.rpc_resp;
507
508 nfs_post_op_update_inode(old_dir, res->old_fattr);
509 nfs_post_op_update_inode(new_dir, res->new_fattr);
510 return 1;
511}
512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513static int
Al Virobeffb8f2016-07-20 16:34:42 -0400514nfs3_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 struct nfs3_linkargs arg = {
517 .fromfh = NFS_FH(inode),
518 .tofh = NFS_FH(dir),
519 .toname = name->name,
520 .tolen = name->len
521 };
Trond Myklebust136f2622010-04-16 16:22:49 -0400522 struct nfs3_linkres res;
Chuck Leverdead28d2006-03-20 13:44:23 -0500523 struct rpc_message msg = {
524 .rpc_proc = &nfs3_procedures[NFS3PROC_LINK],
525 .rpc_argp = &arg,
526 .rpc_resp = &res,
527 };
Trond Myklebust136f2622010-04-16 16:22:49 -0400528 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
530 dprintk("NFS call link %s\n", name->name);
Trond Myklebust136f2622010-04-16 16:22:49 -0400531 res.fattr = nfs_alloc_fattr();
532 res.dir_attr = nfs_alloc_fattr();
533 if (res.fattr == NULL || res.dir_attr == NULL)
534 goto out;
535
Chuck Leverdead28d2006-03-20 13:44:23 -0500536 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
Trond Myklebust136f2622010-04-16 16:22:49 -0400537 nfs_post_op_update_inode(dir, res.dir_attr);
538 nfs_post_op_update_inode(inode, res.fattr);
539out:
540 nfs_free_fattr(res.dir_attr);
541 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 dprintk("NFS reply link: %d\n", status);
543 return status;
544}
545
546static int
Chuck Lever94a6d752006-08-22 20:06:23 -0400547nfs3_proc_symlink(struct inode *dir, struct dentry *dentry, struct page *page,
548 unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549{
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400550 struct nfs3_createdata *data;
Benjamin Coddington17fd6e42019-09-13 08:29:03 -0400551 struct dentry *d_alias;
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400552 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
Chuck Lever94a6d752006-08-22 20:06:23 -0400554 if (len > NFS3_MAXPATHLEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 return -ENAMETOOLONG;
Chuck Lever4f390c12006-08-22 20:06:22 -0400556
Al Viro6de14722013-09-16 10:53:17 -0400557 dprintk("NFS call symlink %pd\n", dentry);
Chuck Lever94a6d752006-08-22 20:06:23 -0400558
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400559 data = nfs3_alloc_createdata();
560 if (data == NULL)
Chuck Lever4f390c12006-08-22 20:06:22 -0400561 goto out;
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400562 data->msg.rpc_proc = &nfs3_procedures[NFS3PROC_SYMLINK];
563 data->arg.symlink.fromfh = NFS_FH(dir);
564 data->arg.symlink.fromname = dentry->d_name.name;
565 data->arg.symlink.fromlen = dentry->d_name.len;
566 data->arg.symlink.pages = &page;
567 data->arg.symlink.pathlen = len;
568 data->arg.symlink.sattr = sattr;
569
Benjamin Coddington17fd6e42019-09-13 08:29:03 -0400570 d_alias = nfs3_do_create(dir, dentry, data);
571 status = PTR_ERR_OR_ZERO(d_alias);
572
573 if (status == 0)
574 dput(d_alias);
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400575
576 nfs3_free_createdata(data);
Chuck Lever4f390c12006-08-22 20:06:22 -0400577out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 dprintk("NFS reply symlink: %d\n", status);
579 return status;
580}
581
582static int
583nfs3_proc_mkdir(struct inode *dir, struct dentry *dentry, struct iattr *sattr)
584{
Christoph Hellwig013cdf12013-12-20 05:16:53 -0800585 struct posix_acl *default_acl, *acl;
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400586 struct nfs3_createdata *data;
Benjamin Coddington17fd6e42019-09-13 08:29:03 -0400587 struct dentry *d_alias;
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400588 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
Al Viro6de14722013-09-16 10:53:17 -0400590 dprintk("NFS call mkdir %pd\n", dentry);
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000591
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400592 data = nfs3_alloc_createdata();
593 if (data == NULL)
594 goto out;
595
Christoph Hellwig013cdf12013-12-20 05:16:53 -0800596 status = posix_acl_create(dir, &sattr->ia_mode, &default_acl, &acl);
597 if (status)
598 goto out;
599
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400600 data->msg.rpc_proc = &nfs3_procedures[NFS3PROC_MKDIR];
601 data->arg.mkdir.fh = NFS_FH(dir);
602 data->arg.mkdir.name = dentry->d_name.name;
603 data->arg.mkdir.len = dentry->d_name.len;
604 data->arg.mkdir.sattr = sattr;
605
Benjamin Coddington17fd6e42019-09-13 08:29:03 -0400606 d_alias = nfs3_do_create(dir, dentry, data);
607 status = PTR_ERR_OR_ZERO(d_alias);
608
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000609 if (status != 0)
Christoph Hellwig013cdf12013-12-20 05:16:53 -0800610 goto out_release_acls;
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400611
Benjamin Coddington17fd6e42019-09-13 08:29:03 -0400612 if (d_alias)
613 dentry = d_alias;
614
David Howells2b0143b2015-03-17 22:25:59 +0000615 status = nfs3_proc_setacls(d_inode(dentry), acl, default_acl);
Christoph Hellwig013cdf12013-12-20 05:16:53 -0800616
Benjamin Coddington17fd6e42019-09-13 08:29:03 -0400617 dput(d_alias);
Christoph Hellwig013cdf12013-12-20 05:16:53 -0800618out_release_acls:
619 posix_acl_release(acl);
620 posix_acl_release(default_acl);
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000621out:
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400622 nfs3_free_createdata(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 dprintk("NFS reply mkdir: %d\n", status);
624 return status;
625}
626
627static int
Al Virobeffb8f2016-07-20 16:34:42 -0400628nfs3_proc_rmdir(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629{
Trond Myklebust39967dd2010-04-16 16:22:50 -0400630 struct nfs_fattr *dir_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 struct nfs3_diropargs arg = {
632 .fh = NFS_FH(dir),
633 .name = name->name,
634 .len = name->len
635 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500636 struct rpc_message msg = {
637 .rpc_proc = &nfs3_procedures[NFS3PROC_RMDIR],
638 .rpc_argp = &arg,
Chuck Leverdead28d2006-03-20 13:44:23 -0500639 };
Trond Myklebust39967dd2010-04-16 16:22:50 -0400640 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
642 dprintk("NFS call rmdir %s\n", name->name);
Trond Myklebust39967dd2010-04-16 16:22:50 -0400643 dir_attr = nfs_alloc_fattr();
644 if (dir_attr == NULL)
645 goto out;
646
647 msg.rpc_resp = dir_attr;
Chuck Leverdead28d2006-03-20 13:44:23 -0500648 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
Trond Myklebust39967dd2010-04-16 16:22:50 -0400649 nfs_post_op_update_inode(dir, dir_attr);
650 nfs_free_fattr(dir_attr);
651out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 dprintk("NFS reply rmdir: %d\n", status);
653 return status;
654}
655
656/*
657 * The READDIR implementation is somewhat hackish - we pass the user buffer
658 * to the encode function, which installs it in the receive iovec.
659 * The decode function itself doesn't perform any decoding, it just makes
660 * sure the reply is syntactically correct.
661 *
662 * Also note that this implementation handles both plain readdir and
663 * readdirplus.
664 */
Trond Myklebust82e22a52020-11-02 17:34:23 -0500665static int nfs3_proc_readdir(struct nfs_readdir_arg *nr_arg,
666 struct nfs_readdir_res *nr_res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
Trond Myklebust82e22a52020-11-02 17:34:23 -0500668 struct inode *dir = d_inode(nr_arg->dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 struct nfs3_readdirargs arg = {
670 .fh = NFS_FH(dir),
Trond Myklebust82e22a52020-11-02 17:34:23 -0500671 .cookie = nr_arg->cookie,
672 .plus = nr_arg->plus,
673 .count = nr_arg->page_len,
674 .pages = nr_arg->pages
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 };
676 struct nfs3_readdirres res = {
Trond Myklebust82e22a52020-11-02 17:34:23 -0500677 .verf = nr_res->verf,
678 .plus = nr_arg->plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 };
680 struct rpc_message msg = {
681 .rpc_proc = &nfs3_procedures[NFS3PROC_READDIR],
682 .rpc_argp = &arg,
683 .rpc_resp = &res,
Trond Myklebust82e22a52020-11-02 17:34:23 -0500684 .rpc_cred = nr_arg->cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 };
Trond Myklebustaa49b4c2010-04-16 16:22:49 -0400686 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
Trond Myklebust82e22a52020-11-02 17:34:23 -0500688 if (nr_arg->plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 msg.rpc_proc = &nfs3_procedures[NFS3PROC_READDIRPLUS];
Trond Myklebust82e22a52020-11-02 17:34:23 -0500690 if (arg.cookie)
691 memcpy(arg.verf, nr_arg->verf, sizeof(arg.verf));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
Trond Myklebust82e22a52020-11-02 17:34:23 -0500693 dprintk("NFS call readdir%s %llu\n", nr_arg->plus ? "plus" : "",
694 (unsigned long long)nr_arg->cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Trond Myklebustaa49b4c2010-04-16 16:22:49 -0400696 res.dir_attr = nfs_alloc_fattr();
697 if (res.dir_attr == NULL)
698 goto out;
699
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
Trond Myklebustc4812992007-09-28 17:11:45 -0400701
702 nfs_invalidate_atime(dir);
Trond Myklebustaa49b4c2010-04-16 16:22:49 -0400703 nfs_refresh_inode(dir, res.dir_attr);
Trond Myklebustc4812992007-09-28 17:11:45 -0400704
Trond Myklebustaa49b4c2010-04-16 16:22:49 -0400705 nfs_free_fattr(res.dir_attr);
706out:
Trond Myklebust82e22a52020-11-02 17:34:23 -0500707 dprintk("NFS reply readdir%s: %d\n", nr_arg->plus ? "plus" : "",
708 status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 return status;
710}
711
712static int
713nfs3_proc_mknod(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
714 dev_t rdev)
715{
Christoph Hellwig013cdf12013-12-20 05:16:53 -0800716 struct posix_acl *default_acl, *acl;
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400717 struct nfs3_createdata *data;
Benjamin Coddington17fd6e42019-09-13 08:29:03 -0400718 struct dentry *d_alias;
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400719 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
Al Viro6de14722013-09-16 10:53:17 -0400721 dprintk("NFS call mknod %pd %u:%u\n", dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 MAJOR(rdev), MINOR(rdev));
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000723
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400724 data = nfs3_alloc_createdata();
725 if (data == NULL)
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000726 goto out;
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400727
Christoph Hellwig013cdf12013-12-20 05:16:53 -0800728 status = posix_acl_create(dir, &sattr->ia_mode, &default_acl, &acl);
729 if (status)
730 goto out;
731
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400732 data->msg.rpc_proc = &nfs3_procedures[NFS3PROC_MKNOD];
733 data->arg.mknod.fh = NFS_FH(dir);
734 data->arg.mknod.name = dentry->d_name.name;
735 data->arg.mknod.len = dentry->d_name.len;
736 data->arg.mknod.sattr = sattr;
737 data->arg.mknod.rdev = rdev;
738
739 switch (sattr->ia_mode & S_IFMT) {
740 case S_IFBLK:
741 data->arg.mknod.type = NF3BLK;
742 break;
743 case S_IFCHR:
744 data->arg.mknod.type = NF3CHR;
745 break;
746 case S_IFIFO:
747 data->arg.mknod.type = NF3FIFO;
748 break;
749 case S_IFSOCK:
750 data->arg.mknod.type = NF3SOCK;
751 break;
752 default:
753 status = -EINVAL;
754 goto out;
755 }
756
Benjamin Coddington17fd6e42019-09-13 08:29:03 -0400757 d_alias = nfs3_do_create(dir, dentry, data);
758 status = PTR_ERR_OR_ZERO(d_alias);
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000759 if (status != 0)
Christoph Hellwig013cdf12013-12-20 05:16:53 -0800760 goto out_release_acls;
761
Benjamin Coddington17fd6e42019-09-13 08:29:03 -0400762 if (d_alias)
763 dentry = d_alias;
764
David Howells2b0143b2015-03-17 22:25:59 +0000765 status = nfs3_proc_setacls(d_inode(dentry), acl, default_acl);
Christoph Hellwig013cdf12013-12-20 05:16:53 -0800766
Benjamin Coddington17fd6e42019-09-13 08:29:03 -0400767 dput(d_alias);
Christoph Hellwig013cdf12013-12-20 05:16:53 -0800768out_release_acls:
769 posix_acl_release(acl);
770 posix_acl_release(default_acl);
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000771out:
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400772 nfs3_free_createdata(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 dprintk("NFS reply mknod: %d\n", status);
774 return status;
775}
776
777static int
778nfs3_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
779 struct nfs_fsstat *stat)
780{
Chuck Leverdead28d2006-03-20 13:44:23 -0500781 struct rpc_message msg = {
782 .rpc_proc = &nfs3_procedures[NFS3PROC_FSSTAT],
783 .rpc_argp = fhandle,
784 .rpc_resp = stat,
785 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 int status;
787
788 dprintk("NFS call fsstat\n");
Trond Myklebust0e574af2005-10-27 22:12:38 -0400789 nfs_fattr_init(stat->fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500790 status = rpc_call_sync(server->client, &msg, 0);
Chuck Leverd141d972010-09-21 16:55:47 -0400791 dprintk("NFS reply fsstat: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 return status;
793}
794
795static int
EG Keizer37ca8f52008-08-19 16:34:36 -0400796do_proc_fsinfo(struct rpc_clnt *client, struct nfs_fh *fhandle,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 struct nfs_fsinfo *info)
798{
Chuck Leverdead28d2006-03-20 13:44:23 -0500799 struct rpc_message msg = {
800 .rpc_proc = &nfs3_procedures[NFS3PROC_FSINFO],
801 .rpc_argp = fhandle,
802 .rpc_resp = info,
803 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 int status;
805
806 dprintk("NFS call fsinfo\n");
Trond Myklebust0e574af2005-10-27 22:12:38 -0400807 nfs_fattr_init(info->fattr);
EG Keizer37ca8f52008-08-19 16:34:36 -0400808 status = rpc_call_sync(client, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 dprintk("NFS reply fsinfo: %d\n", status);
810 return status;
811}
812
EG Keizer37ca8f52008-08-19 16:34:36 -0400813/*
814 * Bare-bones access to fsinfo: this is for nfs_get_root/nfs_get_sb via
815 * nfs_create_server
816 */
817static int
818nfs3_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
819 struct nfs_fsinfo *info)
820{
821 int status;
822
823 status = do_proc_fsinfo(server->client, fhandle, info);
824 if (status && server->nfs_client->cl_rpcclient != server->client)
825 status = do_proc_fsinfo(server->nfs_client->cl_rpcclient, fhandle, info);
826 return status;
827}
828
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829static int
830nfs3_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
831 struct nfs_pathconf *info)
832{
Chuck Leverdead28d2006-03-20 13:44:23 -0500833 struct rpc_message msg = {
834 .rpc_proc = &nfs3_procedures[NFS3PROC_PATHCONF],
835 .rpc_argp = fhandle,
836 .rpc_resp = info,
837 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 int status;
839
840 dprintk("NFS call pathconf\n");
Trond Myklebust0e574af2005-10-27 22:12:38 -0400841 nfs_fattr_init(info->fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500842 status = rpc_call_sync(server->client, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 dprintk("NFS reply pathconf: %d\n", status);
844 return status;
845}
846
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400847static int nfs3_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400849 struct inode *inode = hdr->inode;
Trond Myklebust8d8928d2018-03-05 12:03:00 -0500850 struct nfs_server *server = NFS_SERVER(inode);
Fred Isamancd841602012-04-20 14:47:44 -0400851
Trond Myklebust16cecdf2014-06-22 12:55:11 -0400852 if (hdr->pgio_done_cb != NULL)
853 return hdr->pgio_done_cb(task, hdr);
854
Fred Isamancd841602012-04-20 14:47:44 -0400855 if (nfs3_async_handle_jukebox(task, inode))
Trond Myklebustec06c092006-03-20 13:44:27 -0500856 return -EAGAIN;
Trond Myklebust8850df92007-09-28 17:20:07 -0400857
Trond Myklebust8d8928d2018-03-05 12:03:00 -0500858 if (task->tk_status >= 0 && !server->read_hdrsize)
859 cmpxchg(&server->read_hdrsize, 0, hdr->res.replen);
860
Fred Isamancd841602012-04-20 14:47:44 -0400861 nfs_invalidate_atime(inode);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400862 nfs_refresh_inode(inode, &hdr->fattr);
Trond Myklebustec06c092006-03-20 13:44:27 -0500863 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864}
865
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400866static void nfs3_proc_read_setup(struct nfs_pgio_header *hdr,
867 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868{
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400869 msg->rpc_proc = &nfs3_procedures[NFS3PROC_READ];
Trond Myklebust8d8928d2018-03-05 12:03:00 -0500870 hdr->args.replen = NFS_SERVER(hdr->inode)->read_hdrsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871}
872
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400873static int nfs3_proc_pgio_rpc_prepare(struct rpc_task *task,
874 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -0400875{
876 rpc_call_start(task);
NeilBrownef1820f2013-09-04 17:04:49 +1000877 return 0;
Bryan Schumakerea7c3302012-03-19 14:54:40 -0400878}
879
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400880static int nfs3_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400882 struct inode *inode = hdr->inode;
Fred Isamancd841602012-04-20 14:47:44 -0400883
Trond Myklebust16cecdf2014-06-22 12:55:11 -0400884 if (hdr->pgio_done_cb != NULL)
885 return hdr->pgio_done_cb(task, hdr);
886
Fred Isamancd841602012-04-20 14:47:44 -0400887 if (nfs3_async_handle_jukebox(task, inode))
Trond Myklebust788e7a82006-03-20 13:44:27 -0500888 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 if (task->tk_status >= 0)
Trond Myklebusta08a8cd2015-02-26 17:36:09 -0500890 nfs_writeback_update_inode(hdr);
Trond Myklebust788e7a82006-03-20 13:44:27 -0500891 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892}
893
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400894static void nfs3_proc_write_setup(struct nfs_pgio_header *hdr,
Anna Schumakerfb91fb02018-05-04 16:22:48 -0400895 struct rpc_message *msg,
896 struct rpc_clnt **clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897{
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400898 msg->rpc_proc = &nfs3_procedures[NFS3PROC_WRITE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899}
900
Fred Isaman0b7c0152012-04-20 14:47:39 -0400901static void nfs3_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
902{
903 rpc_call_start(task);
904}
905
906static int nfs3_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907{
Trond Myklebust16cecdf2014-06-22 12:55:11 -0400908 if (data->commit_done_cb != NULL)
909 return data->commit_done_cb(task, data);
910
Chuck Lever006ea732006-03-20 13:44:14 -0500911 if (nfs3_async_handle_jukebox(task, data->inode))
Trond Myklebust788e7a82006-03-20 13:44:27 -0500912 return -EAGAIN;
Trond Myklebust9e08a3c2007-10-08 14:10:31 -0400913 nfs_refresh_inode(data->inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -0500914 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915}
916
Anna Schumakere9ae1ee22018-05-04 16:22:49 -0400917static void nfs3_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg,
918 struct rpc_clnt **clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919{
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400920 msg->rpc_proc = &nfs3_procedures[NFS3PROC_COMMIT];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921}
922
Trond Myklebustbb3393d2017-04-25 16:25:06 -0400923static void nfs3_nlm_alloc_call(void *data)
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -0400924{
925 struct nfs_lock_context *l_ctx = data;
926 if (l_ctx && test_bit(NFS_CONTEXT_UNLOCK, &l_ctx->open_context->flags)) {
927 get_nfs_open_context(l_ctx->open_context);
928 nfs_get_lock_context(l_ctx->open_context);
929 }
930}
931
Trond Myklebustbb3393d2017-04-25 16:25:06 -0400932static bool nfs3_nlm_unlock_prepare(struct rpc_task *task, void *data)
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -0400933{
934 struct nfs_lock_context *l_ctx = data;
935 if (l_ctx && test_bit(NFS_CONTEXT_UNLOCK, &l_ctx->open_context->flags))
936 return nfs_async_iocounter_wait(task, l_ctx);
937 return false;
938
939}
940
Trond Myklebustbb3393d2017-04-25 16:25:06 -0400941static void nfs3_nlm_release_call(void *data)
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -0400942{
943 struct nfs_lock_context *l_ctx = data;
944 struct nfs_open_context *ctx;
945 if (l_ctx && test_bit(NFS_CONTEXT_UNLOCK, &l_ctx->open_context->flags)) {
946 ctx = l_ctx->open_context;
947 nfs_put_lock_context(l_ctx);
948 put_nfs_open_context(ctx);
949 }
950}
951
Colin Ian King1b720402018-02-07 11:27:54 +0000952static const struct nlmclnt_operations nlmclnt_fl_close_lock_ops = {
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -0400953 .nlmclnt_alloc_call = nfs3_nlm_alloc_call,
954 .nlmclnt_unlock_prepare = nfs3_nlm_unlock_prepare,
955 .nlmclnt_release_call = nfs3_nlm_release_call,
956};
957
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958static int
959nfs3_proc_lock(struct file *filp, int cmd, struct file_lock *fl)
960{
Al Viro496ad9a2013-01-23 17:07:38 -0500961 struct inode *inode = file_inode(filp);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -0400962 struct nfs_lock_context *l_ctx = NULL;
963 struct nfs_open_context *ctx = nfs_file_open_context(filp);
964 int status;
Chuck Lever1093a602008-01-11 17:09:59 -0500965
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -0400966 if (fl->fl_flags & FL_CLOSE) {
967 l_ctx = nfs_get_lock_context(ctx);
968 if (IS_ERR(l_ctx))
969 l_ctx = NULL;
970 else
971 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
972 }
973
974 status = nlmclnt_proc(NFS_SERVER(inode)->nlm_host, cmd, fl, l_ctx);
975
976 if (l_ctx)
977 nfs_put_lock_context(l_ctx);
978
979 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980}
981
Bryan Schumaker011e2a72012-06-20 15:53:43 -0400982static int nfs3_have_delegation(struct inode *inode, fmode_t flags)
983{
984 return 0;
985}
986
Bryan Schumakerab962912012-07-16 16:39:11 -0400987static const struct inode_operations nfs3_dir_inode_operations = {
988 .create = nfs_create,
989 .lookup = nfs_lookup,
990 .link = nfs_link,
991 .unlink = nfs_unlink,
992 .symlink = nfs_symlink,
993 .mkdir = nfs_mkdir,
994 .rmdir = nfs_rmdir,
995 .mknod = nfs_mknod,
996 .rename = nfs_rename,
997 .permission = nfs_permission,
998 .getattr = nfs_getattr,
999 .setattr = nfs_setattr,
Christoph Hellwig5f13ee92014-01-30 06:01:52 -08001000#ifdef CONFIG_NFS_V3_ACL
Christoph Hellwig74adf832014-06-18 11:07:03 +02001001 .listxattr = nfs3_listxattr,
Christoph Hellwig013cdf12013-12-20 05:16:53 -08001002 .get_acl = nfs3_get_acl,
1003 .set_acl = nfs3_set_acl,
1004#endif
Bryan Schumakerab962912012-07-16 16:39:11 -04001005};
1006
1007static const struct inode_operations nfs3_file_inode_operations = {
1008 .permission = nfs_permission,
1009 .getattr = nfs_getattr,
1010 .setattr = nfs_setattr,
Christoph Hellwig5f13ee92014-01-30 06:01:52 -08001011#ifdef CONFIG_NFS_V3_ACL
Christoph Hellwig74adf832014-06-18 11:07:03 +02001012 .listxattr = nfs3_listxattr,
Christoph Hellwig013cdf12013-12-20 05:16:53 -08001013 .get_acl = nfs3_get_acl,
1014 .set_acl = nfs3_set_acl,
1015#endif
Bryan Schumakerab962912012-07-16 16:39:11 -04001016};
1017
David Howells509de812006-08-22 20:06:11 -04001018const struct nfs_rpc_ops nfs_v3_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 .version = 3, /* protocol version */
1020 .dentry_ops = &nfs_dentry_operations,
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +00001021 .dir_inode_ops = &nfs3_dir_inode_operations,
1022 .file_inode_ops = &nfs3_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04001023 .file_ops = &nfs_file_operations,
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04001024 .nlmclnt_ops = &nlmclnt_fl_close_lock_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 .getroot = nfs3_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04001026 .submount = nfs_submount,
David Howellsf2aedb72019-12-10 07:31:13 -05001027 .try_get_tree = nfs_try_get_tree,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 .getattr = nfs3_proc_getattr,
1029 .setattr = nfs3_proc_setattr,
1030 .lookup = nfs3_proc_lookup,
Trond Myklebust3c5e9a52020-10-19 12:45:53 -04001031 .lookupp = nfs3_proc_lookupp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 .access = nfs3_proc_access,
1033 .readlink = nfs3_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 .create = nfs3_proc_create,
1035 .remove = nfs3_proc_remove,
1036 .unlink_setup = nfs3_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04001037 .unlink_rpc_prepare = nfs3_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 .unlink_done = nfs3_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04001039 .rename_setup = nfs3_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04001040 .rename_rpc_prepare = nfs3_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04001041 .rename_done = nfs3_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 .link = nfs3_proc_link,
1043 .symlink = nfs3_proc_symlink,
1044 .mkdir = nfs3_proc_mkdir,
1045 .rmdir = nfs3_proc_rmdir,
1046 .readdir = nfs3_proc_readdir,
1047 .mknod = nfs3_proc_mknod,
1048 .statfs = nfs3_proc_statfs,
1049 .fsinfo = nfs3_proc_fsinfo,
1050 .pathconf = nfs3_proc_pathconf,
1051 .decode_dirent = nfs3_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04001052 .pgio_rpc_prepare = nfs3_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 .read_setup = nfs3_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05001054 .read_done = nfs3_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 .write_setup = nfs3_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05001056 .write_done = nfs3_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 .commit_setup = nfs3_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04001058 .commit_rpc_prepare = nfs3_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05001059 .commit_done = nfs3_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 .lock = nfs3_proc_lock,
Christoph Hellwig013cdf12013-12-20 05:16:53 -08001061 .clear_acl_cache = forget_all_cached_acls,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04001062 .close_context = nfs_close_context,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04001063 .have_delegation = nfs3_have_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04001064 .alloc_client = nfs_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00001065 .init_client = nfs_init_client,
Bryan Schumakercdb7eced2012-06-20 15:53:45 -04001066 .free_client = nfs_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04001067 .create_server = nfs3_create_server,
1068 .clone_server = nfs3_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069};