blob: acbdf7496d312fe5a95d7d23a485d96ac6437148 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212static int nfs3_proc_access(struct inode *inode, struct nfs_access_entry *entry)
213{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 struct nfs3_accessargs arg = {
215 .fh = NFS_FH(inode),
Anna Schumaker1750d922017-07-26 12:00:21 -0400216 .access = entry->mask,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 };
Trond Myklebustc407d412010-04-16 16:22:48 -0400218 struct nfs3_accessres res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 struct rpc_message msg = {
220 .rpc_proc = &nfs3_procedures[NFS3PROC_ACCESS],
221 .rpc_argp = &arg,
222 .rpc_resp = &res,
NeilBrowna52458b2018-12-03 11:30:31 +1100223 .rpc_cred = entry->cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 };
Trond Myklebustc407d412010-04-16 16:22:48 -0400225 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
227 dprintk("NFS call access\n");
Trond Myklebustc407d412010-04-16 16:22:48 -0400228 res.fattr = nfs_alloc_fattr();
229 if (res.fattr == NULL)
230 goto out;
231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
Trond Myklebustc407d412010-04-16 16:22:48 -0400233 nfs_refresh_inode(inode, res.fattr);
Trond Myklebusteda3e202017-07-11 17:54:33 -0400234 if (status == 0)
235 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -0400236 nfs_free_fattr(res.fattr);
237out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 dprintk("NFS reply access: %d\n", status);
239 return status;
240}
241
242static int nfs3_proc_readlink(struct inode *inode, struct page *page,
243 unsigned int pgbase, unsigned int pglen)
244{
Trond Myklebust3b14d652010-04-16 16:22:50 -0400245 struct nfs_fattr *fattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 struct nfs3_readlinkargs args = {
247 .fh = NFS_FH(inode),
248 .pgbase = pgbase,
249 .pglen = pglen,
250 .pages = &page
251 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500252 struct rpc_message msg = {
253 .rpc_proc = &nfs3_procedures[NFS3PROC_READLINK],
254 .rpc_argp = &args,
Chuck Leverdead28d2006-03-20 13:44:23 -0500255 };
Trond Myklebust3b14d652010-04-16 16:22:50 -0400256 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
258 dprintk("NFS call readlink\n");
Trond Myklebust3b14d652010-04-16 16:22:50 -0400259 fattr = nfs_alloc_fattr();
260 if (fattr == NULL)
261 goto out;
262 msg.rpc_resp = fattr;
263
Chuck Leverdead28d2006-03-20 13:44:23 -0500264 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
Trond Myklebust3b14d652010-04-16 16:22:50 -0400265 nfs_refresh_inode(inode, fattr);
266 nfs_free_fattr(fattr);
267out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 dprintk("NFS reply readlink: %d\n", status);
269 return status;
270}
271
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400272struct nfs3_createdata {
273 struct rpc_message msg;
274 union {
275 struct nfs3_createargs create;
276 struct nfs3_mkdirargs mkdir;
277 struct nfs3_symlinkargs symlink;
278 struct nfs3_mknodargs mknod;
279 } arg;
280 struct nfs3_diropres res;
281 struct nfs_fh fh;
282 struct nfs_fattr fattr;
283 struct nfs_fattr dir_attr;
284};
285
286static struct nfs3_createdata *nfs3_alloc_createdata(void)
287{
288 struct nfs3_createdata *data;
289
290 data = kzalloc(sizeof(*data), GFP_KERNEL);
291 if (data != NULL) {
292 data->msg.rpc_argp = &data->arg;
293 data->msg.rpc_resp = &data->res;
294 data->res.fh = &data->fh;
295 data->res.fattr = &data->fattr;
296 data->res.dir_attr = &data->dir_attr;
297 nfs_fattr_init(data->res.fattr);
298 nfs_fattr_init(data->res.dir_attr);
299 }
300 return data;
301}
302
Benjamin Coddington17fd6e42019-09-13 08:29:03 -0400303static struct dentry *
304nfs3_do_create(struct inode *dir, struct dentry *dentry, struct nfs3_createdata *data)
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400305{
306 int status;
307
308 status = rpc_call_sync(NFS_CLIENT(dir), &data->msg, 0);
309 nfs_post_op_update_inode(dir, data->res.dir_attr);
Benjamin Coddington17fd6e42019-09-13 08:29:03 -0400310 if (status != 0)
311 return ERR_PTR(status);
312
313 return nfs_add_or_obtain(dentry, data->res.fh, data->res.fattr, NULL);
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400314}
315
316static void nfs3_free_createdata(struct nfs3_createdata *data)
317{
318 kfree(data);
319}
320
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321/*
322 * Create a regular file.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 */
324static int
325nfs3_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +0200326 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327{
Christoph Hellwig013cdf12013-12-20 05:16:53 -0800328 struct posix_acl *default_acl, *acl;
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400329 struct nfs3_createdata *data;
Benjamin Coddington17fd6e42019-09-13 08:29:03 -0400330 struct dentry *d_alias;
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400331 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
Al Viro6de14722013-09-16 10:53:17 -0400333 dprintk("NFS call create %pd\n", dentry);
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400334
335 data = nfs3_alloc_createdata();
336 if (data == NULL)
337 goto out;
338
339 data->msg.rpc_proc = &nfs3_procedures[NFS3PROC_CREATE];
340 data->arg.create.fh = NFS_FH(dir);
341 data->arg.create.name = dentry->d_name.name;
342 data->arg.create.len = dentry->d_name.len;
343 data->arg.create.sattr = sattr;
344
345 data->arg.create.createmode = NFS3_CREATE_UNCHECKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 if (flags & O_EXCL) {
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400347 data->arg.create.createmode = NFS3_CREATE_EXCLUSIVE;
Trond Myklebusta9943d12013-08-20 21:04:11 -0400348 data->arg.create.verifier[0] = cpu_to_be32(jiffies);
349 data->arg.create.verifier[1] = cpu_to_be32(current->pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 }
351
Christoph Hellwig013cdf12013-12-20 05:16:53 -0800352 status = posix_acl_create(dir, &sattr->ia_mode, &default_acl, &acl);
353 if (status)
354 goto out;
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000355
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400356 for (;;) {
Benjamin Coddington17fd6e42019-09-13 08:29:03 -0400357 d_alias = nfs3_do_create(dir, dentry, data);
358 status = PTR_ERR_OR_ZERO(d_alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400360 if (status != -ENOTSUPP)
361 break;
362 /* If the server doesn't support the exclusive creation
363 * semantics, try again with simple 'guarded' mode. */
364 switch (data->arg.create.createmode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 case NFS3_CREATE_EXCLUSIVE:
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400366 data->arg.create.createmode = NFS3_CREATE_GUARDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 break;
368
369 case NFS3_CREATE_GUARDED:
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400370 data->arg.create.createmode = NFS3_CREATE_UNCHECKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 break;
372
373 case NFS3_CREATE_UNCHECKED:
374 goto out;
375 }
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400376 nfs_fattr_init(data->res.dir_attr);
377 nfs_fattr_init(data->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 }
379
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 if (status != 0)
Christoph Hellwig013cdf12013-12-20 05:16:53 -0800381 goto out_release_acls;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Benjamin Coddington17fd6e42019-09-13 08:29:03 -0400383 if (d_alias)
384 dentry = d_alias;
385
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 /* When we created the file with exclusive semantics, make
387 * sure we set the attributes afterwards. */
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400388 if (data->arg.create.createmode == NFS3_CREATE_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 dprintk("NFS call setattr (post-create)\n");
390
391 if (!(sattr->ia_valid & ATTR_ATIME_SET))
392 sattr->ia_valid |= ATTR_ATIME;
393 if (!(sattr->ia_valid & ATTR_MTIME_SET))
394 sattr->ia_valid |= ATTR_MTIME;
395
396 /* Note: we could use a guarded setattr here, but I'm
397 * not sure this buys us anything (and I'd have
398 * to revamp the NFSv3 XDR code) */
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400399 status = nfs3_proc_setattr(dentry, data->res.fattr, sattr);
David Howells2b0143b2015-03-17 22:25:59 +0000400 nfs_post_op_update_inode(d_inode(dentry), data->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 dprintk("NFS reply setattr (post-create): %d\n", status);
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400402 if (status != 0)
Benjamin Coddington17fd6e42019-09-13 08:29:03 -0400403 goto out_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 }
Christoph Hellwig013cdf12013-12-20 05:16:53 -0800405
David Howells2b0143b2015-03-17 22:25:59 +0000406 status = nfs3_proc_setacls(d_inode(dentry), acl, default_acl);
Christoph Hellwig013cdf12013-12-20 05:16:53 -0800407
Benjamin Coddington17fd6e42019-09-13 08:29:03 -0400408out_dput:
409 dput(d_alias);
Christoph Hellwig013cdf12013-12-20 05:16:53 -0800410out_release_acls:
411 posix_acl_release(acl);
412 posix_acl_release(default_acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413out:
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400414 nfs3_free_createdata(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 dprintk("NFS reply create: %d\n", status);
416 return status;
417}
418
419static int
Trond Myklebust912678d2018-03-20 16:43:15 -0400420nfs3_proc_remove(struct inode *dir, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
Trond Myklebust4fdc17b2007-07-14 15:39:57 -0400422 struct nfs_removeargs arg = {
423 .fh = NFS_FH(dir),
Trond Myklebust912678d2018-03-20 16:43:15 -0400424 .name = dentry->d_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -0400426 struct nfs_removeres res;
427 struct rpc_message msg = {
428 .rpc_proc = &nfs3_procedures[NFS3PROC_REMOVE],
429 .rpc_argp = &arg,
430 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 };
Trond Myklebustd3468902010-04-16 16:22:50 -0400432 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
Trond Myklebust912678d2018-03-20 16:43:15 -0400434 dprintk("NFS call remove %pd2\n", dentry);
Trond Myklebustd3468902010-04-16 16:22:50 -0400435 res.dir_attr = nfs_alloc_fattr();
436 if (res.dir_attr == NULL)
437 goto out;
438
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
Trond Myklebustd3468902010-04-16 16:22:50 -0400440 nfs_post_op_update_inode(dir, res.dir_attr);
441 nfs_free_fattr(res.dir_attr);
442out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 dprintk("NFS reply remove: %d\n", status);
444 return status;
445}
446
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400447static void
Trond Myklebusted7e9ad2018-05-30 16:11:52 -0400448nfs3_proc_unlink_setup(struct rpc_message *msg,
449 struct dentry *dentry,
450 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 msg->rpc_proc = &nfs3_procedures[NFS3PROC_REMOVE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453}
454
Bryan Schumaker34e137c2012-03-19 14:54:41 -0400455static void nfs3_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
456{
457 rpc_call_start(task);
458}
459
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460static int
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400461nfs3_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400463 struct nfs_removeres *res;
464 if (nfs3_async_handle_jukebox(task, dir))
465 return 0;
466 res = task->tk_msg.rpc_resp;
Trond Myklebustd3468902010-04-16 16:22:50 -0400467 nfs_post_op_update_inode(dir, res->dir_attr);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400468 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469}
470
Jeff Laytond3d41522010-09-17 17:31:57 -0400471static void
Trond Myklebustf2c2c552018-03-20 16:43:16 -0400472nfs3_proc_rename_setup(struct rpc_message *msg,
473 struct dentry *old_dentry,
474 struct dentry *new_dentry)
Jeff Laytond3d41522010-09-17 17:31:57 -0400475{
476 msg->rpc_proc = &nfs3_procedures[NFS3PROC_RENAME];
477}
478
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -0400479static void nfs3_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
480{
481 rpc_call_start(task);
482}
483
Jeff Laytond3d41522010-09-17 17:31:57 -0400484static int
485nfs3_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
486 struct inode *new_dir)
487{
488 struct nfs_renameres *res;
489
490 if (nfs3_async_handle_jukebox(task, old_dir))
491 return 0;
492 res = task->tk_msg.rpc_resp;
493
494 nfs_post_op_update_inode(old_dir, res->old_fattr);
495 nfs_post_op_update_inode(new_dir, res->new_fattr);
496 return 1;
497}
498
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499static int
Al Virobeffb8f2016-07-20 16:34:42 -0400500nfs3_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 struct nfs3_linkargs arg = {
503 .fromfh = NFS_FH(inode),
504 .tofh = NFS_FH(dir),
505 .toname = name->name,
506 .tolen = name->len
507 };
Trond Myklebust136f2622010-04-16 16:22:49 -0400508 struct nfs3_linkres res;
Chuck Leverdead28d2006-03-20 13:44:23 -0500509 struct rpc_message msg = {
510 .rpc_proc = &nfs3_procedures[NFS3PROC_LINK],
511 .rpc_argp = &arg,
512 .rpc_resp = &res,
513 };
Trond Myklebust136f2622010-04-16 16:22:49 -0400514 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
516 dprintk("NFS call link %s\n", name->name);
Trond Myklebust136f2622010-04-16 16:22:49 -0400517 res.fattr = nfs_alloc_fattr();
518 res.dir_attr = nfs_alloc_fattr();
519 if (res.fattr == NULL || res.dir_attr == NULL)
520 goto out;
521
Chuck Leverdead28d2006-03-20 13:44:23 -0500522 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
Trond Myklebust136f2622010-04-16 16:22:49 -0400523 nfs_post_op_update_inode(dir, res.dir_attr);
524 nfs_post_op_update_inode(inode, res.fattr);
525out:
526 nfs_free_fattr(res.dir_attr);
527 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 dprintk("NFS reply link: %d\n", status);
529 return status;
530}
531
532static int
Chuck Lever94a6d752006-08-22 20:06:23 -0400533nfs3_proc_symlink(struct inode *dir, struct dentry *dentry, struct page *page,
534 unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535{
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400536 struct nfs3_createdata *data;
Benjamin Coddington17fd6e42019-09-13 08:29:03 -0400537 struct dentry *d_alias;
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400538 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Chuck Lever94a6d752006-08-22 20:06:23 -0400540 if (len > NFS3_MAXPATHLEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 return -ENAMETOOLONG;
Chuck Lever4f390c12006-08-22 20:06:22 -0400542
Al Viro6de14722013-09-16 10:53:17 -0400543 dprintk("NFS call symlink %pd\n", dentry);
Chuck Lever94a6d752006-08-22 20:06:23 -0400544
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400545 data = nfs3_alloc_createdata();
546 if (data == NULL)
Chuck Lever4f390c12006-08-22 20:06:22 -0400547 goto out;
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400548 data->msg.rpc_proc = &nfs3_procedures[NFS3PROC_SYMLINK];
549 data->arg.symlink.fromfh = NFS_FH(dir);
550 data->arg.symlink.fromname = dentry->d_name.name;
551 data->arg.symlink.fromlen = dentry->d_name.len;
552 data->arg.symlink.pages = &page;
553 data->arg.symlink.pathlen = len;
554 data->arg.symlink.sattr = sattr;
555
Benjamin Coddington17fd6e42019-09-13 08:29:03 -0400556 d_alias = nfs3_do_create(dir, dentry, data);
557 status = PTR_ERR_OR_ZERO(d_alias);
558
559 if (status == 0)
560 dput(d_alias);
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400561
562 nfs3_free_createdata(data);
Chuck Lever4f390c12006-08-22 20:06:22 -0400563out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 dprintk("NFS reply symlink: %d\n", status);
565 return status;
566}
567
568static int
569nfs3_proc_mkdir(struct inode *dir, struct dentry *dentry, struct iattr *sattr)
570{
Christoph Hellwig013cdf12013-12-20 05:16:53 -0800571 struct posix_acl *default_acl, *acl;
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400572 struct nfs3_createdata *data;
Benjamin Coddington17fd6e42019-09-13 08:29:03 -0400573 struct dentry *d_alias;
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400574 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Al Viro6de14722013-09-16 10:53:17 -0400576 dprintk("NFS call mkdir %pd\n", dentry);
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000577
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400578 data = nfs3_alloc_createdata();
579 if (data == NULL)
580 goto out;
581
Christoph Hellwig013cdf12013-12-20 05:16:53 -0800582 status = posix_acl_create(dir, &sattr->ia_mode, &default_acl, &acl);
583 if (status)
584 goto out;
585
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400586 data->msg.rpc_proc = &nfs3_procedures[NFS3PROC_MKDIR];
587 data->arg.mkdir.fh = NFS_FH(dir);
588 data->arg.mkdir.name = dentry->d_name.name;
589 data->arg.mkdir.len = dentry->d_name.len;
590 data->arg.mkdir.sattr = sattr;
591
Benjamin Coddington17fd6e42019-09-13 08:29:03 -0400592 d_alias = nfs3_do_create(dir, dentry, data);
593 status = PTR_ERR_OR_ZERO(d_alias);
594
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000595 if (status != 0)
Christoph Hellwig013cdf12013-12-20 05:16:53 -0800596 goto out_release_acls;
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400597
Benjamin Coddington17fd6e42019-09-13 08:29:03 -0400598 if (d_alias)
599 dentry = d_alias;
600
David Howells2b0143b2015-03-17 22:25:59 +0000601 status = nfs3_proc_setacls(d_inode(dentry), acl, default_acl);
Christoph Hellwig013cdf12013-12-20 05:16:53 -0800602
Benjamin Coddington17fd6e42019-09-13 08:29:03 -0400603 dput(d_alias);
Christoph Hellwig013cdf12013-12-20 05:16:53 -0800604out_release_acls:
605 posix_acl_release(acl);
606 posix_acl_release(default_acl);
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000607out:
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400608 nfs3_free_createdata(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 dprintk("NFS reply mkdir: %d\n", status);
610 return status;
611}
612
613static int
Al Virobeffb8f2016-07-20 16:34:42 -0400614nfs3_proc_rmdir(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615{
Trond Myklebust39967dd2010-04-16 16:22:50 -0400616 struct nfs_fattr *dir_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 struct nfs3_diropargs arg = {
618 .fh = NFS_FH(dir),
619 .name = name->name,
620 .len = name->len
621 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500622 struct rpc_message msg = {
623 .rpc_proc = &nfs3_procedures[NFS3PROC_RMDIR],
624 .rpc_argp = &arg,
Chuck Leverdead28d2006-03-20 13:44:23 -0500625 };
Trond Myklebust39967dd2010-04-16 16:22:50 -0400626 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
628 dprintk("NFS call rmdir %s\n", name->name);
Trond Myklebust39967dd2010-04-16 16:22:50 -0400629 dir_attr = nfs_alloc_fattr();
630 if (dir_attr == NULL)
631 goto out;
632
633 msg.rpc_resp = dir_attr;
Chuck Leverdead28d2006-03-20 13:44:23 -0500634 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
Trond Myklebust39967dd2010-04-16 16:22:50 -0400635 nfs_post_op_update_inode(dir, dir_attr);
636 nfs_free_fattr(dir_attr);
637out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 dprintk("NFS reply rmdir: %d\n", status);
639 return status;
640}
641
642/*
643 * The READDIR implementation is somewhat hackish - we pass the user buffer
644 * to the encode function, which installs it in the receive iovec.
645 * The decode function itself doesn't perform any decoding, it just makes
646 * sure the reply is syntactically correct.
647 *
648 * Also note that this implementation handles both plain readdir and
649 * readdirplus.
650 */
651static int
NeilBrown684f39b2018-12-03 11:30:30 +1100652nfs3_proc_readdir(struct dentry *dentry, const struct cred *cred,
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -0400653 u64 cookie, struct page **pages, unsigned int count, bool plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654{
David Howells2b0143b2015-03-17 22:25:59 +0000655 struct inode *dir = d_inode(dentry);
Trond Myklebustc3f52af2012-09-03 14:56:02 -0400656 __be32 *verf = NFS_I(dir)->cookieverf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 struct nfs3_readdirargs arg = {
658 .fh = NFS_FH(dir),
659 .cookie = cookie,
660 .verf = {verf[0], verf[1]},
661 .plus = plus,
662 .count = count,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400663 .pages = pages
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 };
665 struct nfs3_readdirres res = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 .verf = verf,
667 .plus = plus
668 };
669 struct rpc_message msg = {
670 .rpc_proc = &nfs3_procedures[NFS3PROC_READDIR],
671 .rpc_argp = &arg,
672 .rpc_resp = &res,
NeilBrowna52458b2018-12-03 11:30:31 +1100673 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 };
Trond Myklebustaa49b4c2010-04-16 16:22:49 -0400675 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 if (plus)
678 msg.rpc_proc = &nfs3_procedures[NFS3PROC_READDIRPLUS];
679
680 dprintk("NFS call readdir%s %d\n",
681 plus? "plus" : "", (unsigned int) cookie);
682
Trond Myklebustaa49b4c2010-04-16 16:22:49 -0400683 res.dir_attr = nfs_alloc_fattr();
684 if (res.dir_attr == NULL)
685 goto out;
686
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
Trond Myklebustc4812992007-09-28 17:11:45 -0400688
689 nfs_invalidate_atime(dir);
Trond Myklebustaa49b4c2010-04-16 16:22:49 -0400690 nfs_refresh_inode(dir, res.dir_attr);
Trond Myklebustc4812992007-09-28 17:11:45 -0400691
Trond Myklebustaa49b4c2010-04-16 16:22:49 -0400692 nfs_free_fattr(res.dir_attr);
693out:
Chuck Leverd141d972010-09-21 16:55:47 -0400694 dprintk("NFS reply readdir%s: %d\n",
695 plus? "plus" : "", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 return status;
697}
698
699static int
700nfs3_proc_mknod(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
701 dev_t rdev)
702{
Christoph Hellwig013cdf12013-12-20 05:16:53 -0800703 struct posix_acl *default_acl, *acl;
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400704 struct nfs3_createdata *data;
Benjamin Coddington17fd6e42019-09-13 08:29:03 -0400705 struct dentry *d_alias;
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400706 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
Al Viro6de14722013-09-16 10:53:17 -0400708 dprintk("NFS call mknod %pd %u:%u\n", dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 MAJOR(rdev), MINOR(rdev));
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000710
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400711 data = nfs3_alloc_createdata();
712 if (data == NULL)
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000713 goto out;
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400714
Christoph Hellwig013cdf12013-12-20 05:16:53 -0800715 status = posix_acl_create(dir, &sattr->ia_mode, &default_acl, &acl);
716 if (status)
717 goto out;
718
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400719 data->msg.rpc_proc = &nfs3_procedures[NFS3PROC_MKNOD];
720 data->arg.mknod.fh = NFS_FH(dir);
721 data->arg.mknod.name = dentry->d_name.name;
722 data->arg.mknod.len = dentry->d_name.len;
723 data->arg.mknod.sattr = sattr;
724 data->arg.mknod.rdev = rdev;
725
726 switch (sattr->ia_mode & S_IFMT) {
727 case S_IFBLK:
728 data->arg.mknod.type = NF3BLK;
729 break;
730 case S_IFCHR:
731 data->arg.mknod.type = NF3CHR;
732 break;
733 case S_IFIFO:
734 data->arg.mknod.type = NF3FIFO;
735 break;
736 case S_IFSOCK:
737 data->arg.mknod.type = NF3SOCK;
738 break;
739 default:
740 status = -EINVAL;
741 goto out;
742 }
743
Benjamin Coddington17fd6e42019-09-13 08:29:03 -0400744 d_alias = nfs3_do_create(dir, dentry, data);
745 status = PTR_ERR_OR_ZERO(d_alias);
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000746 if (status != 0)
Christoph Hellwig013cdf12013-12-20 05:16:53 -0800747 goto out_release_acls;
748
Benjamin Coddington17fd6e42019-09-13 08:29:03 -0400749 if (d_alias)
750 dentry = d_alias;
751
David Howells2b0143b2015-03-17 22:25:59 +0000752 status = nfs3_proc_setacls(d_inode(dentry), acl, default_acl);
Christoph Hellwig013cdf12013-12-20 05:16:53 -0800753
Benjamin Coddington17fd6e42019-09-13 08:29:03 -0400754 dput(d_alias);
Christoph Hellwig013cdf12013-12-20 05:16:53 -0800755out_release_acls:
756 posix_acl_release(acl);
757 posix_acl_release(default_acl);
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000758out:
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400759 nfs3_free_createdata(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 dprintk("NFS reply mknod: %d\n", status);
761 return status;
762}
763
764static int
765nfs3_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
766 struct nfs_fsstat *stat)
767{
Chuck Leverdead28d2006-03-20 13:44:23 -0500768 struct rpc_message msg = {
769 .rpc_proc = &nfs3_procedures[NFS3PROC_FSSTAT],
770 .rpc_argp = fhandle,
771 .rpc_resp = stat,
772 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 int status;
774
775 dprintk("NFS call fsstat\n");
Trond Myklebust0e574af2005-10-27 22:12:38 -0400776 nfs_fattr_init(stat->fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500777 status = rpc_call_sync(server->client, &msg, 0);
Chuck Leverd141d972010-09-21 16:55:47 -0400778 dprintk("NFS reply fsstat: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 return status;
780}
781
782static int
EG Keizer37ca8f52008-08-19 16:34:36 -0400783do_proc_fsinfo(struct rpc_clnt *client, struct nfs_fh *fhandle,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 struct nfs_fsinfo *info)
785{
Chuck Leverdead28d2006-03-20 13:44:23 -0500786 struct rpc_message msg = {
787 .rpc_proc = &nfs3_procedures[NFS3PROC_FSINFO],
788 .rpc_argp = fhandle,
789 .rpc_resp = info,
790 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 int status;
792
793 dprintk("NFS call fsinfo\n");
Trond Myklebust0e574af2005-10-27 22:12:38 -0400794 nfs_fattr_init(info->fattr);
EG Keizer37ca8f52008-08-19 16:34:36 -0400795 status = rpc_call_sync(client, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 dprintk("NFS reply fsinfo: %d\n", status);
797 return status;
798}
799
EG Keizer37ca8f52008-08-19 16:34:36 -0400800/*
801 * Bare-bones access to fsinfo: this is for nfs_get_root/nfs_get_sb via
802 * nfs_create_server
803 */
804static int
805nfs3_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
806 struct nfs_fsinfo *info)
807{
808 int status;
809
810 status = do_proc_fsinfo(server->client, fhandle, info);
811 if (status && server->nfs_client->cl_rpcclient != server->client)
812 status = do_proc_fsinfo(server->nfs_client->cl_rpcclient, fhandle, info);
813 return status;
814}
815
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816static int
817nfs3_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
818 struct nfs_pathconf *info)
819{
Chuck Leverdead28d2006-03-20 13:44:23 -0500820 struct rpc_message msg = {
821 .rpc_proc = &nfs3_procedures[NFS3PROC_PATHCONF],
822 .rpc_argp = fhandle,
823 .rpc_resp = info,
824 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 int status;
826
827 dprintk("NFS call pathconf\n");
Trond Myklebust0e574af2005-10-27 22:12:38 -0400828 nfs_fattr_init(info->fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500829 status = rpc_call_sync(server->client, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 dprintk("NFS reply pathconf: %d\n", status);
831 return status;
832}
833
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400834static int nfs3_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400836 struct inode *inode = hdr->inode;
Trond Myklebust8d8928d2018-03-05 12:03:00 -0500837 struct nfs_server *server = NFS_SERVER(inode);
Fred Isamancd841602012-04-20 14:47:44 -0400838
Trond Myklebust16cecdf2014-06-22 12:55:11 -0400839 if (hdr->pgio_done_cb != NULL)
840 return hdr->pgio_done_cb(task, hdr);
841
Fred Isamancd841602012-04-20 14:47:44 -0400842 if (nfs3_async_handle_jukebox(task, inode))
Trond Myklebustec06c092006-03-20 13:44:27 -0500843 return -EAGAIN;
Trond Myklebust8850df92007-09-28 17:20:07 -0400844
Trond Myklebust8d8928d2018-03-05 12:03:00 -0500845 if (task->tk_status >= 0 && !server->read_hdrsize)
846 cmpxchg(&server->read_hdrsize, 0, hdr->res.replen);
847
Fred Isamancd841602012-04-20 14:47:44 -0400848 nfs_invalidate_atime(inode);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400849 nfs_refresh_inode(inode, &hdr->fattr);
Trond Myklebustec06c092006-03-20 13:44:27 -0500850 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851}
852
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400853static void nfs3_proc_read_setup(struct nfs_pgio_header *hdr,
854 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855{
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400856 msg->rpc_proc = &nfs3_procedures[NFS3PROC_READ];
Trond Myklebust8d8928d2018-03-05 12:03:00 -0500857 hdr->args.replen = NFS_SERVER(hdr->inode)->read_hdrsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858}
859
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400860static int nfs3_proc_pgio_rpc_prepare(struct rpc_task *task,
861 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -0400862{
863 rpc_call_start(task);
NeilBrownef1820f2013-09-04 17:04:49 +1000864 return 0;
Bryan Schumakerea7c3302012-03-19 14:54:40 -0400865}
866
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400867static int nfs3_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400869 struct inode *inode = hdr->inode;
Fred Isamancd841602012-04-20 14:47:44 -0400870
Trond Myklebust16cecdf2014-06-22 12:55:11 -0400871 if (hdr->pgio_done_cb != NULL)
872 return hdr->pgio_done_cb(task, hdr);
873
Fred Isamancd841602012-04-20 14:47:44 -0400874 if (nfs3_async_handle_jukebox(task, inode))
Trond Myklebust788e7a82006-03-20 13:44:27 -0500875 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 if (task->tk_status >= 0)
Trond Myklebusta08a8cd2015-02-26 17:36:09 -0500877 nfs_writeback_update_inode(hdr);
Trond Myklebust788e7a82006-03-20 13:44:27 -0500878 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879}
880
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400881static void nfs3_proc_write_setup(struct nfs_pgio_header *hdr,
Anna Schumakerfb91fb02018-05-04 16:22:48 -0400882 struct rpc_message *msg,
883 struct rpc_clnt **clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884{
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400885 msg->rpc_proc = &nfs3_procedures[NFS3PROC_WRITE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886}
887
Fred Isaman0b7c0152012-04-20 14:47:39 -0400888static void nfs3_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
889{
890 rpc_call_start(task);
891}
892
893static int nfs3_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894{
Trond Myklebust16cecdf2014-06-22 12:55:11 -0400895 if (data->commit_done_cb != NULL)
896 return data->commit_done_cb(task, data);
897
Chuck Lever006ea732006-03-20 13:44:14 -0500898 if (nfs3_async_handle_jukebox(task, data->inode))
Trond Myklebust788e7a82006-03-20 13:44:27 -0500899 return -EAGAIN;
Trond Myklebust9e08a3c2007-10-08 14:10:31 -0400900 nfs_refresh_inode(data->inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -0500901 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902}
903
Anna Schumakere9ae1ee2018-05-04 16:22:49 -0400904static void nfs3_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg,
905 struct rpc_clnt **clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906{
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400907 msg->rpc_proc = &nfs3_procedures[NFS3PROC_COMMIT];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908}
909
Trond Myklebustbb3393d2017-04-25 16:25:06 -0400910static void nfs3_nlm_alloc_call(void *data)
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -0400911{
912 struct nfs_lock_context *l_ctx = data;
913 if (l_ctx && test_bit(NFS_CONTEXT_UNLOCK, &l_ctx->open_context->flags)) {
914 get_nfs_open_context(l_ctx->open_context);
915 nfs_get_lock_context(l_ctx->open_context);
916 }
917}
918
Trond Myklebustbb3393d2017-04-25 16:25:06 -0400919static bool nfs3_nlm_unlock_prepare(struct rpc_task *task, void *data)
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -0400920{
921 struct nfs_lock_context *l_ctx = data;
922 if (l_ctx && test_bit(NFS_CONTEXT_UNLOCK, &l_ctx->open_context->flags))
923 return nfs_async_iocounter_wait(task, l_ctx);
924 return false;
925
926}
927
Trond Myklebustbb3393d2017-04-25 16:25:06 -0400928static void nfs3_nlm_release_call(void *data)
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -0400929{
930 struct nfs_lock_context *l_ctx = data;
931 struct nfs_open_context *ctx;
932 if (l_ctx && test_bit(NFS_CONTEXT_UNLOCK, &l_ctx->open_context->flags)) {
933 ctx = l_ctx->open_context;
934 nfs_put_lock_context(l_ctx);
935 put_nfs_open_context(ctx);
936 }
937}
938
Colin Ian King1b720402018-02-07 11:27:54 +0000939static const struct nlmclnt_operations nlmclnt_fl_close_lock_ops = {
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -0400940 .nlmclnt_alloc_call = nfs3_nlm_alloc_call,
941 .nlmclnt_unlock_prepare = nfs3_nlm_unlock_prepare,
942 .nlmclnt_release_call = nfs3_nlm_release_call,
943};
944
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945static int
946nfs3_proc_lock(struct file *filp, int cmd, struct file_lock *fl)
947{
Al Viro496ad9a2013-01-23 17:07:38 -0500948 struct inode *inode = file_inode(filp);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -0400949 struct nfs_lock_context *l_ctx = NULL;
950 struct nfs_open_context *ctx = nfs_file_open_context(filp);
951 int status;
Chuck Lever1093a602008-01-11 17:09:59 -0500952
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -0400953 if (fl->fl_flags & FL_CLOSE) {
954 l_ctx = nfs_get_lock_context(ctx);
955 if (IS_ERR(l_ctx))
956 l_ctx = NULL;
957 else
958 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
959 }
960
961 status = nlmclnt_proc(NFS_SERVER(inode)->nlm_host, cmd, fl, l_ctx);
962
963 if (l_ctx)
964 nfs_put_lock_context(l_ctx);
965
966 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967}
968
Bryan Schumaker011e2a72012-06-20 15:53:43 -0400969static int nfs3_have_delegation(struct inode *inode, fmode_t flags)
970{
971 return 0;
972}
973
Bryan Schumakerab962912012-07-16 16:39:11 -0400974static const struct inode_operations nfs3_dir_inode_operations = {
975 .create = nfs_create,
976 .lookup = nfs_lookup,
977 .link = nfs_link,
978 .unlink = nfs_unlink,
979 .symlink = nfs_symlink,
980 .mkdir = nfs_mkdir,
981 .rmdir = nfs_rmdir,
982 .mknod = nfs_mknod,
983 .rename = nfs_rename,
984 .permission = nfs_permission,
985 .getattr = nfs_getattr,
986 .setattr = nfs_setattr,
Christoph Hellwig5f13ee92014-01-30 06:01:52 -0800987#ifdef CONFIG_NFS_V3_ACL
Christoph Hellwig74adf832014-06-18 11:07:03 +0200988 .listxattr = nfs3_listxattr,
Christoph Hellwig013cdf12013-12-20 05:16:53 -0800989 .get_acl = nfs3_get_acl,
990 .set_acl = nfs3_set_acl,
991#endif
Bryan Schumakerab962912012-07-16 16:39:11 -0400992};
993
994static const struct inode_operations nfs3_file_inode_operations = {
995 .permission = nfs_permission,
996 .getattr = nfs_getattr,
997 .setattr = nfs_setattr,
Christoph Hellwig5f13ee92014-01-30 06:01:52 -0800998#ifdef CONFIG_NFS_V3_ACL
Christoph Hellwig74adf832014-06-18 11:07:03 +0200999 .listxattr = nfs3_listxattr,
Christoph Hellwig013cdf12013-12-20 05:16:53 -08001000 .get_acl = nfs3_get_acl,
1001 .set_acl = nfs3_set_acl,
1002#endif
Bryan Schumakerab962912012-07-16 16:39:11 -04001003};
1004
David Howells509de812006-08-22 20:06:11 -04001005const struct nfs_rpc_ops nfs_v3_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 .version = 3, /* protocol version */
1007 .dentry_ops = &nfs_dentry_operations,
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +00001008 .dir_inode_ops = &nfs3_dir_inode_operations,
1009 .file_inode_ops = &nfs3_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04001010 .file_ops = &nfs_file_operations,
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04001011 .nlmclnt_ops = &nlmclnt_fl_close_lock_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 .getroot = nfs3_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04001013 .submount = nfs_submount,
David Howellsf2aedb72019-12-10 07:31:13 -05001014 .try_get_tree = nfs_try_get_tree,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 .getattr = nfs3_proc_getattr,
1016 .setattr = nfs3_proc_setattr,
1017 .lookup = nfs3_proc_lookup,
1018 .access = nfs3_proc_access,
1019 .readlink = nfs3_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 .create = nfs3_proc_create,
1021 .remove = nfs3_proc_remove,
1022 .unlink_setup = nfs3_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04001023 .unlink_rpc_prepare = nfs3_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 .unlink_done = nfs3_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04001025 .rename_setup = nfs3_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04001026 .rename_rpc_prepare = nfs3_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04001027 .rename_done = nfs3_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 .link = nfs3_proc_link,
1029 .symlink = nfs3_proc_symlink,
1030 .mkdir = nfs3_proc_mkdir,
1031 .rmdir = nfs3_proc_rmdir,
1032 .readdir = nfs3_proc_readdir,
1033 .mknod = nfs3_proc_mknod,
1034 .statfs = nfs3_proc_statfs,
1035 .fsinfo = nfs3_proc_fsinfo,
1036 .pathconf = nfs3_proc_pathconf,
1037 .decode_dirent = nfs3_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04001038 .pgio_rpc_prepare = nfs3_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 .read_setup = nfs3_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05001040 .read_done = nfs3_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 .write_setup = nfs3_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05001042 .write_done = nfs3_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 .commit_setup = nfs3_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04001044 .commit_rpc_prepare = nfs3_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05001045 .commit_done = nfs3_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 .lock = nfs3_proc_lock,
Christoph Hellwig013cdf12013-12-20 05:16:53 -08001047 .clear_acl_cache = forget_all_cached_acls,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04001048 .close_context = nfs_close_context,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04001049 .have_delegation = nfs3_have_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04001050 .alloc_client = nfs_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00001051 .init_client = nfs_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04001052 .free_client = nfs_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04001053 .create_server = nfs3_create_server,
1054 .clone_server = nfs3_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055};