blob: 19f3d1b2807ee82b2c030f4837a55613767b077a [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) {
148 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
149 nfs_zap_acl_cache(inode);
Trond Myklebustf0446362015-02-26 16:09:04 -0500150 nfs_setattr_update_inode(inode, sattr, fattr);
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
Al Virobeffb8f2016-07-20 16:34:42 -0400157nfs3_proc_lookup(struct inode *dir, const struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -0400158 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
159 struct nfs4_label *label)
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),
163 .name = name->name,
164 .len = name->len
165 };
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;
176
Trond Myklebuste1fb4d02010-04-16 16:22:47 -0400177 res.dir_attr = nfs_alloc_fattr();
178 if (res.dir_attr == NULL)
179 return -ENOMEM;
180
zhengbin6ed21442019-12-19 14:14:18 +0800181 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0e574af2005-10-27 22:12:38 -0400182 nfs_fattr_init(fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500183 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
Trond Myklebuste1fb4d02010-04-16 16:22:47 -0400184 nfs_refresh_inode(dir, res.dir_attr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500185 if (status >= 0 && !(fattr->valid & NFS_ATTR_FATTR)) {
186 msg.rpc_proc = &nfs3_procedures[NFS3PROC_GETATTR];
187 msg.rpc_argp = fhandle;
188 msg.rpc_resp = fattr;
189 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
190 }
Trond Myklebuste1fb4d02010-04-16 16:22:47 -0400191 nfs_free_fattr(res.dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 dprintk("NFS reply lookup: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 return status;
194}
195
196static int nfs3_proc_access(struct inode *inode, struct nfs_access_entry *entry)
197{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 struct nfs3_accessargs arg = {
199 .fh = NFS_FH(inode),
Anna Schumaker1750d922017-07-26 12:00:21 -0400200 .access = entry->mask,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 };
Trond Myklebustc407d412010-04-16 16:22:48 -0400202 struct nfs3_accessres res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 struct rpc_message msg = {
204 .rpc_proc = &nfs3_procedures[NFS3PROC_ACCESS],
205 .rpc_argp = &arg,
206 .rpc_resp = &res,
NeilBrowna52458b2018-12-03 11:30:31 +1100207 .rpc_cred = entry->cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 };
Trond Myklebustc407d412010-04-16 16:22:48 -0400209 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
211 dprintk("NFS call access\n");
Trond Myklebustc407d412010-04-16 16:22:48 -0400212 res.fattr = nfs_alloc_fattr();
213 if (res.fattr == NULL)
214 goto out;
215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
Trond Myklebustc407d412010-04-16 16:22:48 -0400217 nfs_refresh_inode(inode, res.fattr);
Trond Myklebusteda3e202017-07-11 17:54:33 -0400218 if (status == 0)
219 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -0400220 nfs_free_fattr(res.fattr);
221out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 dprintk("NFS reply access: %d\n", status);
223 return status;
224}
225
226static int nfs3_proc_readlink(struct inode *inode, struct page *page,
227 unsigned int pgbase, unsigned int pglen)
228{
Trond Myklebust3b14d652010-04-16 16:22:50 -0400229 struct nfs_fattr *fattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 struct nfs3_readlinkargs args = {
231 .fh = NFS_FH(inode),
232 .pgbase = pgbase,
233 .pglen = pglen,
234 .pages = &page
235 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500236 struct rpc_message msg = {
237 .rpc_proc = &nfs3_procedures[NFS3PROC_READLINK],
238 .rpc_argp = &args,
Chuck Leverdead28d2006-03-20 13:44:23 -0500239 };
Trond Myklebust3b14d652010-04-16 16:22:50 -0400240 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
242 dprintk("NFS call readlink\n");
Trond Myklebust3b14d652010-04-16 16:22:50 -0400243 fattr = nfs_alloc_fattr();
244 if (fattr == NULL)
245 goto out;
246 msg.rpc_resp = fattr;
247
Chuck Leverdead28d2006-03-20 13:44:23 -0500248 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
Trond Myklebust3b14d652010-04-16 16:22:50 -0400249 nfs_refresh_inode(inode, fattr);
250 nfs_free_fattr(fattr);
251out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 dprintk("NFS reply readlink: %d\n", status);
253 return status;
254}
255
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400256struct nfs3_createdata {
257 struct rpc_message msg;
258 union {
259 struct nfs3_createargs create;
260 struct nfs3_mkdirargs mkdir;
261 struct nfs3_symlinkargs symlink;
262 struct nfs3_mknodargs mknod;
263 } arg;
264 struct nfs3_diropres res;
265 struct nfs_fh fh;
266 struct nfs_fattr fattr;
267 struct nfs_fattr dir_attr;
268};
269
270static struct nfs3_createdata *nfs3_alloc_createdata(void)
271{
272 struct nfs3_createdata *data;
273
274 data = kzalloc(sizeof(*data), GFP_KERNEL);
275 if (data != NULL) {
276 data->msg.rpc_argp = &data->arg;
277 data->msg.rpc_resp = &data->res;
278 data->res.fh = &data->fh;
279 data->res.fattr = &data->fattr;
280 data->res.dir_attr = &data->dir_attr;
281 nfs_fattr_init(data->res.fattr);
282 nfs_fattr_init(data->res.dir_attr);
283 }
284 return data;
285}
286
Benjamin Coddington17fd6e42019-09-13 08:29:03 -0400287static struct dentry *
288nfs3_do_create(struct inode *dir, struct dentry *dentry, struct nfs3_createdata *data)
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400289{
290 int status;
291
292 status = rpc_call_sync(NFS_CLIENT(dir), &data->msg, 0);
293 nfs_post_op_update_inode(dir, data->res.dir_attr);
Benjamin Coddington17fd6e42019-09-13 08:29:03 -0400294 if (status != 0)
295 return ERR_PTR(status);
296
297 return nfs_add_or_obtain(dentry, data->res.fh, data->res.fattr, NULL);
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400298}
299
300static void nfs3_free_createdata(struct nfs3_createdata *data)
301{
302 kfree(data);
303}
304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305/*
306 * Create a regular file.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 */
308static int
309nfs3_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +0200310 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
Christoph Hellwig013cdf12013-12-20 05:16:53 -0800312 struct posix_acl *default_acl, *acl;
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400313 struct nfs3_createdata *data;
Benjamin Coddington17fd6e42019-09-13 08:29:03 -0400314 struct dentry *d_alias;
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400315 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Al Viro6de14722013-09-16 10:53:17 -0400317 dprintk("NFS call create %pd\n", dentry);
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400318
319 data = nfs3_alloc_createdata();
320 if (data == NULL)
321 goto out;
322
323 data->msg.rpc_proc = &nfs3_procedures[NFS3PROC_CREATE];
324 data->arg.create.fh = NFS_FH(dir);
325 data->arg.create.name = dentry->d_name.name;
326 data->arg.create.len = dentry->d_name.len;
327 data->arg.create.sattr = sattr;
328
329 data->arg.create.createmode = NFS3_CREATE_UNCHECKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 if (flags & O_EXCL) {
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400331 data->arg.create.createmode = NFS3_CREATE_EXCLUSIVE;
Trond Myklebusta9943d12013-08-20 21:04:11 -0400332 data->arg.create.verifier[0] = cpu_to_be32(jiffies);
333 data->arg.create.verifier[1] = cpu_to_be32(current->pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 }
335
Christoph Hellwig013cdf12013-12-20 05:16:53 -0800336 status = posix_acl_create(dir, &sattr->ia_mode, &default_acl, &acl);
337 if (status)
338 goto out;
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000339
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400340 for (;;) {
Benjamin Coddington17fd6e42019-09-13 08:29:03 -0400341 d_alias = nfs3_do_create(dir, dentry, data);
342 status = PTR_ERR_OR_ZERO(d_alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400344 if (status != -ENOTSUPP)
345 break;
346 /* If the server doesn't support the exclusive creation
347 * semantics, try again with simple 'guarded' mode. */
348 switch (data->arg.create.createmode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 case NFS3_CREATE_EXCLUSIVE:
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400350 data->arg.create.createmode = NFS3_CREATE_GUARDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 break;
352
353 case NFS3_CREATE_GUARDED:
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400354 data->arg.create.createmode = NFS3_CREATE_UNCHECKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 break;
356
357 case NFS3_CREATE_UNCHECKED:
358 goto out;
359 }
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400360 nfs_fattr_init(data->res.dir_attr);
361 nfs_fattr_init(data->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 }
363
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 if (status != 0)
Christoph Hellwig013cdf12013-12-20 05:16:53 -0800365 goto out_release_acls;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
Benjamin Coddington17fd6e42019-09-13 08:29:03 -0400367 if (d_alias)
368 dentry = d_alias;
369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 /* When we created the file with exclusive semantics, make
371 * sure we set the attributes afterwards. */
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400372 if (data->arg.create.createmode == NFS3_CREATE_EXCLUSIVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 dprintk("NFS call setattr (post-create)\n");
374
375 if (!(sattr->ia_valid & ATTR_ATIME_SET))
376 sattr->ia_valid |= ATTR_ATIME;
377 if (!(sattr->ia_valid & ATTR_MTIME_SET))
378 sattr->ia_valid |= ATTR_MTIME;
379
380 /* Note: we could use a guarded setattr here, but I'm
381 * not sure this buys us anything (and I'd have
382 * to revamp the NFSv3 XDR code) */
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400383 status = nfs3_proc_setattr(dentry, data->res.fattr, sattr);
David Howells2b0143b2015-03-17 22:25:59 +0000384 nfs_post_op_update_inode(d_inode(dentry), data->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 dprintk("NFS reply setattr (post-create): %d\n", status);
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400386 if (status != 0)
Benjamin Coddington17fd6e42019-09-13 08:29:03 -0400387 goto out_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 }
Christoph Hellwig013cdf12013-12-20 05:16:53 -0800389
David Howells2b0143b2015-03-17 22:25:59 +0000390 status = nfs3_proc_setacls(d_inode(dentry), acl, default_acl);
Christoph Hellwig013cdf12013-12-20 05:16:53 -0800391
Benjamin Coddington17fd6e42019-09-13 08:29:03 -0400392out_dput:
393 dput(d_alias);
Christoph Hellwig013cdf12013-12-20 05:16:53 -0800394out_release_acls:
395 posix_acl_release(acl);
396 posix_acl_release(default_acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397out:
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400398 nfs3_free_createdata(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 dprintk("NFS reply create: %d\n", status);
400 return status;
401}
402
403static int
Trond Myklebust912678d2018-03-20 16:43:15 -0400404nfs3_proc_remove(struct inode *dir, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405{
Trond Myklebust4fdc17b2007-07-14 15:39:57 -0400406 struct nfs_removeargs arg = {
407 .fh = NFS_FH(dir),
Trond Myklebust912678d2018-03-20 16:43:15 -0400408 .name = dentry->d_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -0400410 struct nfs_removeres res;
411 struct rpc_message msg = {
412 .rpc_proc = &nfs3_procedures[NFS3PROC_REMOVE],
413 .rpc_argp = &arg,
414 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 };
Trond Myklebustd3468902010-04-16 16:22:50 -0400416 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
Trond Myklebust912678d2018-03-20 16:43:15 -0400418 dprintk("NFS call remove %pd2\n", dentry);
Trond Myklebustd3468902010-04-16 16:22:50 -0400419 res.dir_attr = nfs_alloc_fattr();
420 if (res.dir_attr == NULL)
421 goto out;
422
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
Trond Myklebustd3468902010-04-16 16:22:50 -0400424 nfs_post_op_update_inode(dir, res.dir_attr);
425 nfs_free_fattr(res.dir_attr);
426out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 dprintk("NFS reply remove: %d\n", status);
428 return status;
429}
430
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400431static void
Trond Myklebusted7e9ad2018-05-30 16:11:52 -0400432nfs3_proc_unlink_setup(struct rpc_message *msg,
433 struct dentry *dentry,
434 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 msg->rpc_proc = &nfs3_procedures[NFS3PROC_REMOVE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437}
438
Bryan Schumaker34e137c2012-03-19 14:54:41 -0400439static void nfs3_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
440{
441 rpc_call_start(task);
442}
443
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444static int
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400445nfs3_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400447 struct nfs_removeres *res;
448 if (nfs3_async_handle_jukebox(task, dir))
449 return 0;
450 res = task->tk_msg.rpc_resp;
Trond Myklebustd3468902010-04-16 16:22:50 -0400451 nfs_post_op_update_inode(dir, res->dir_attr);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -0400452 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453}
454
Jeff Laytond3d41522010-09-17 17:31:57 -0400455static void
Trond Myklebustf2c2c552018-03-20 16:43:16 -0400456nfs3_proc_rename_setup(struct rpc_message *msg,
457 struct dentry *old_dentry,
458 struct dentry *new_dentry)
Jeff Laytond3d41522010-09-17 17:31:57 -0400459{
460 msg->rpc_proc = &nfs3_procedures[NFS3PROC_RENAME];
461}
462
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -0400463static void nfs3_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
464{
465 rpc_call_start(task);
466}
467
Jeff Laytond3d41522010-09-17 17:31:57 -0400468static int
469nfs3_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
470 struct inode *new_dir)
471{
472 struct nfs_renameres *res;
473
474 if (nfs3_async_handle_jukebox(task, old_dir))
475 return 0;
476 res = task->tk_msg.rpc_resp;
477
478 nfs_post_op_update_inode(old_dir, res->old_fattr);
479 nfs_post_op_update_inode(new_dir, res->new_fattr);
480 return 1;
481}
482
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483static int
Al Virobeffb8f2016-07-20 16:34:42 -0400484nfs3_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 struct nfs3_linkargs arg = {
487 .fromfh = NFS_FH(inode),
488 .tofh = NFS_FH(dir),
489 .toname = name->name,
490 .tolen = name->len
491 };
Trond Myklebust136f2622010-04-16 16:22:49 -0400492 struct nfs3_linkres res;
Chuck Leverdead28d2006-03-20 13:44:23 -0500493 struct rpc_message msg = {
494 .rpc_proc = &nfs3_procedures[NFS3PROC_LINK],
495 .rpc_argp = &arg,
496 .rpc_resp = &res,
497 };
Trond Myklebust136f2622010-04-16 16:22:49 -0400498 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
500 dprintk("NFS call link %s\n", name->name);
Trond Myklebust136f2622010-04-16 16:22:49 -0400501 res.fattr = nfs_alloc_fattr();
502 res.dir_attr = nfs_alloc_fattr();
503 if (res.fattr == NULL || res.dir_attr == NULL)
504 goto out;
505
Chuck Leverdead28d2006-03-20 13:44:23 -0500506 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
Trond Myklebust136f2622010-04-16 16:22:49 -0400507 nfs_post_op_update_inode(dir, res.dir_attr);
508 nfs_post_op_update_inode(inode, res.fattr);
509out:
510 nfs_free_fattr(res.dir_attr);
511 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 dprintk("NFS reply link: %d\n", status);
513 return status;
514}
515
516static int
Chuck Lever94a6d752006-08-22 20:06:23 -0400517nfs3_proc_symlink(struct inode *dir, struct dentry *dentry, struct page *page,
518 unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519{
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400520 struct nfs3_createdata *data;
Benjamin Coddington17fd6e42019-09-13 08:29:03 -0400521 struct dentry *d_alias;
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400522 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
Chuck Lever94a6d752006-08-22 20:06:23 -0400524 if (len > NFS3_MAXPATHLEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 return -ENAMETOOLONG;
Chuck Lever4f390c12006-08-22 20:06:22 -0400526
Al Viro6de14722013-09-16 10:53:17 -0400527 dprintk("NFS call symlink %pd\n", dentry);
Chuck Lever94a6d752006-08-22 20:06:23 -0400528
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400529 data = nfs3_alloc_createdata();
530 if (data == NULL)
Chuck Lever4f390c12006-08-22 20:06:22 -0400531 goto out;
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400532 data->msg.rpc_proc = &nfs3_procedures[NFS3PROC_SYMLINK];
533 data->arg.symlink.fromfh = NFS_FH(dir);
534 data->arg.symlink.fromname = dentry->d_name.name;
535 data->arg.symlink.fromlen = dentry->d_name.len;
536 data->arg.symlink.pages = &page;
537 data->arg.symlink.pathlen = len;
538 data->arg.symlink.sattr = sattr;
539
Benjamin Coddington17fd6e42019-09-13 08:29:03 -0400540 d_alias = nfs3_do_create(dir, dentry, data);
541 status = PTR_ERR_OR_ZERO(d_alias);
542
543 if (status == 0)
544 dput(d_alias);
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400545
546 nfs3_free_createdata(data);
Chuck Lever4f390c12006-08-22 20:06:22 -0400547out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 dprintk("NFS reply symlink: %d\n", status);
549 return status;
550}
551
552static int
553nfs3_proc_mkdir(struct inode *dir, struct dentry *dentry, struct iattr *sattr)
554{
Christoph Hellwig013cdf12013-12-20 05:16:53 -0800555 struct posix_acl *default_acl, *acl;
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400556 struct nfs3_createdata *data;
Benjamin Coddington17fd6e42019-09-13 08:29:03 -0400557 struct dentry *d_alias;
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400558 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Al Viro6de14722013-09-16 10:53:17 -0400560 dprintk("NFS call mkdir %pd\n", dentry);
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000561
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400562 data = nfs3_alloc_createdata();
563 if (data == NULL)
564 goto out;
565
Christoph Hellwig013cdf12013-12-20 05:16:53 -0800566 status = posix_acl_create(dir, &sattr->ia_mode, &default_acl, &acl);
567 if (status)
568 goto out;
569
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400570 data->msg.rpc_proc = &nfs3_procedures[NFS3PROC_MKDIR];
571 data->arg.mkdir.fh = NFS_FH(dir);
572 data->arg.mkdir.name = dentry->d_name.name;
573 data->arg.mkdir.len = dentry->d_name.len;
574 data->arg.mkdir.sattr = sattr;
575
Benjamin Coddington17fd6e42019-09-13 08:29:03 -0400576 d_alias = nfs3_do_create(dir, dentry, data);
577 status = PTR_ERR_OR_ZERO(d_alias);
578
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000579 if (status != 0)
Christoph Hellwig013cdf12013-12-20 05:16:53 -0800580 goto out_release_acls;
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400581
Benjamin Coddington17fd6e42019-09-13 08:29:03 -0400582 if (d_alias)
583 dentry = d_alias;
584
David Howells2b0143b2015-03-17 22:25:59 +0000585 status = nfs3_proc_setacls(d_inode(dentry), acl, default_acl);
Christoph Hellwig013cdf12013-12-20 05:16:53 -0800586
Benjamin Coddington17fd6e42019-09-13 08:29:03 -0400587 dput(d_alias);
Christoph Hellwig013cdf12013-12-20 05:16:53 -0800588out_release_acls:
589 posix_acl_release(acl);
590 posix_acl_release(default_acl);
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000591out:
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400592 nfs3_free_createdata(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 dprintk("NFS reply mkdir: %d\n", status);
594 return status;
595}
596
597static int
Al Virobeffb8f2016-07-20 16:34:42 -0400598nfs3_proc_rmdir(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599{
Trond Myklebust39967dd2010-04-16 16:22:50 -0400600 struct nfs_fattr *dir_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 struct nfs3_diropargs arg = {
602 .fh = NFS_FH(dir),
603 .name = name->name,
604 .len = name->len
605 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500606 struct rpc_message msg = {
607 .rpc_proc = &nfs3_procedures[NFS3PROC_RMDIR],
608 .rpc_argp = &arg,
Chuck Leverdead28d2006-03-20 13:44:23 -0500609 };
Trond Myklebust39967dd2010-04-16 16:22:50 -0400610 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
612 dprintk("NFS call rmdir %s\n", name->name);
Trond Myklebust39967dd2010-04-16 16:22:50 -0400613 dir_attr = nfs_alloc_fattr();
614 if (dir_attr == NULL)
615 goto out;
616
617 msg.rpc_resp = dir_attr;
Chuck Leverdead28d2006-03-20 13:44:23 -0500618 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
Trond Myklebust39967dd2010-04-16 16:22:50 -0400619 nfs_post_op_update_inode(dir, dir_attr);
620 nfs_free_fattr(dir_attr);
621out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 dprintk("NFS reply rmdir: %d\n", status);
623 return status;
624}
625
626/*
627 * The READDIR implementation is somewhat hackish - we pass the user buffer
628 * to the encode function, which installs it in the receive iovec.
629 * The decode function itself doesn't perform any decoding, it just makes
630 * sure the reply is syntactically correct.
631 *
632 * Also note that this implementation handles both plain readdir and
633 * readdirplus.
634 */
635static int
NeilBrown684f39b2018-12-03 11:30:30 +1100636nfs3_proc_readdir(struct dentry *dentry, const struct cred *cred,
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -0400637 u64 cookie, struct page **pages, unsigned int count, bool plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638{
David Howells2b0143b2015-03-17 22:25:59 +0000639 struct inode *dir = d_inode(dentry);
Trond Myklebustc3f52af2012-09-03 14:56:02 -0400640 __be32 *verf = NFS_I(dir)->cookieverf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 struct nfs3_readdirargs arg = {
642 .fh = NFS_FH(dir),
643 .cookie = cookie,
644 .verf = {verf[0], verf[1]},
645 .plus = plus,
646 .count = count,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400647 .pages = pages
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 };
649 struct nfs3_readdirres res = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 .verf = verf,
651 .plus = plus
652 };
653 struct rpc_message msg = {
654 .rpc_proc = &nfs3_procedures[NFS3PROC_READDIR],
655 .rpc_argp = &arg,
656 .rpc_resp = &res,
NeilBrowna52458b2018-12-03 11:30:31 +1100657 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 };
Trond Myklebustaa49b4c2010-04-16 16:22:49 -0400659 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 if (plus)
662 msg.rpc_proc = &nfs3_procedures[NFS3PROC_READDIRPLUS];
663
664 dprintk("NFS call readdir%s %d\n",
665 plus? "plus" : "", (unsigned int) cookie);
666
Trond Myklebustaa49b4c2010-04-16 16:22:49 -0400667 res.dir_attr = nfs_alloc_fattr();
668 if (res.dir_attr == NULL)
669 goto out;
670
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
Trond Myklebustc4812992007-09-28 17:11:45 -0400672
673 nfs_invalidate_atime(dir);
Trond Myklebustaa49b4c2010-04-16 16:22:49 -0400674 nfs_refresh_inode(dir, res.dir_attr);
Trond Myklebustc4812992007-09-28 17:11:45 -0400675
Trond Myklebustaa49b4c2010-04-16 16:22:49 -0400676 nfs_free_fattr(res.dir_attr);
677out:
Chuck Leverd141d972010-09-21 16:55:47 -0400678 dprintk("NFS reply readdir%s: %d\n",
679 plus? "plus" : "", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 return status;
681}
682
683static int
684nfs3_proc_mknod(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
685 dev_t rdev)
686{
Christoph Hellwig013cdf12013-12-20 05:16:53 -0800687 struct posix_acl *default_acl, *acl;
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400688 struct nfs3_createdata *data;
Benjamin Coddington17fd6e42019-09-13 08:29:03 -0400689 struct dentry *d_alias;
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400690 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
Al Viro6de14722013-09-16 10:53:17 -0400692 dprintk("NFS call mknod %pd %u:%u\n", dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 MAJOR(rdev), MINOR(rdev));
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000694
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400695 data = nfs3_alloc_createdata();
696 if (data == NULL)
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000697 goto out;
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400698
Christoph Hellwig013cdf12013-12-20 05:16:53 -0800699 status = posix_acl_create(dir, &sattr->ia_mode, &default_acl, &acl);
700 if (status)
701 goto out;
702
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400703 data->msg.rpc_proc = &nfs3_procedures[NFS3PROC_MKNOD];
704 data->arg.mknod.fh = NFS_FH(dir);
705 data->arg.mknod.name = dentry->d_name.name;
706 data->arg.mknod.len = dentry->d_name.len;
707 data->arg.mknod.sattr = sattr;
708 data->arg.mknod.rdev = rdev;
709
710 switch (sattr->ia_mode & S_IFMT) {
711 case S_IFBLK:
712 data->arg.mknod.type = NF3BLK;
713 break;
714 case S_IFCHR:
715 data->arg.mknod.type = NF3CHR;
716 break;
717 case S_IFIFO:
718 data->arg.mknod.type = NF3FIFO;
719 break;
720 case S_IFSOCK:
721 data->arg.mknod.type = NF3SOCK;
722 break;
723 default:
724 status = -EINVAL;
725 goto out;
726 }
727
Benjamin Coddington17fd6e42019-09-13 08:29:03 -0400728 d_alias = nfs3_do_create(dir, dentry, data);
729 status = PTR_ERR_OR_ZERO(d_alias);
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000730 if (status != 0)
Christoph Hellwig013cdf12013-12-20 05:16:53 -0800731 goto out_release_acls;
732
Benjamin Coddington17fd6e42019-09-13 08:29:03 -0400733 if (d_alias)
734 dentry = d_alias;
735
David Howells2b0143b2015-03-17 22:25:59 +0000736 status = nfs3_proc_setacls(d_inode(dentry), acl, default_acl);
Christoph Hellwig013cdf12013-12-20 05:16:53 -0800737
Benjamin Coddington17fd6e42019-09-13 08:29:03 -0400738 dput(d_alias);
Christoph Hellwig013cdf12013-12-20 05:16:53 -0800739out_release_acls:
740 posix_acl_release(acl);
741 posix_acl_release(default_acl);
Andreas Gruenbacher055ffbe2005-06-22 17:16:27 +0000742out:
Trond Myklebust0b4aae72008-06-20 17:00:23 -0400743 nfs3_free_createdata(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 dprintk("NFS reply mknod: %d\n", status);
745 return status;
746}
747
748static int
749nfs3_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
750 struct nfs_fsstat *stat)
751{
Chuck Leverdead28d2006-03-20 13:44:23 -0500752 struct rpc_message msg = {
753 .rpc_proc = &nfs3_procedures[NFS3PROC_FSSTAT],
754 .rpc_argp = fhandle,
755 .rpc_resp = stat,
756 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 int status;
758
759 dprintk("NFS call fsstat\n");
Trond Myklebust0e574af2005-10-27 22:12:38 -0400760 nfs_fattr_init(stat->fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500761 status = rpc_call_sync(server->client, &msg, 0);
Chuck Leverd141d972010-09-21 16:55:47 -0400762 dprintk("NFS reply fsstat: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 return status;
764}
765
766static int
EG Keizer37ca8f52008-08-19 16:34:36 -0400767do_proc_fsinfo(struct rpc_clnt *client, struct nfs_fh *fhandle,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 struct nfs_fsinfo *info)
769{
Chuck Leverdead28d2006-03-20 13:44:23 -0500770 struct rpc_message msg = {
771 .rpc_proc = &nfs3_procedures[NFS3PROC_FSINFO],
772 .rpc_argp = fhandle,
773 .rpc_resp = info,
774 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 int status;
776
777 dprintk("NFS call fsinfo\n");
Trond Myklebust0e574af2005-10-27 22:12:38 -0400778 nfs_fattr_init(info->fattr);
EG Keizer37ca8f52008-08-19 16:34:36 -0400779 status = rpc_call_sync(client, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 dprintk("NFS reply fsinfo: %d\n", status);
781 return status;
782}
783
EG Keizer37ca8f52008-08-19 16:34:36 -0400784/*
785 * Bare-bones access to fsinfo: this is for nfs_get_root/nfs_get_sb via
786 * nfs_create_server
787 */
788static int
789nfs3_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
790 struct nfs_fsinfo *info)
791{
792 int status;
793
794 status = do_proc_fsinfo(server->client, fhandle, info);
795 if (status && server->nfs_client->cl_rpcclient != server->client)
796 status = do_proc_fsinfo(server->nfs_client->cl_rpcclient, fhandle, info);
797 return status;
798}
799
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800static int
801nfs3_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
802 struct nfs_pathconf *info)
803{
Chuck Leverdead28d2006-03-20 13:44:23 -0500804 struct rpc_message msg = {
805 .rpc_proc = &nfs3_procedures[NFS3PROC_PATHCONF],
806 .rpc_argp = fhandle,
807 .rpc_resp = info,
808 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 int status;
810
811 dprintk("NFS call pathconf\n");
Trond Myklebust0e574af2005-10-27 22:12:38 -0400812 nfs_fattr_init(info->fattr);
Chuck Leverdead28d2006-03-20 13:44:23 -0500813 status = rpc_call_sync(server->client, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 dprintk("NFS reply pathconf: %d\n", status);
815 return status;
816}
817
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400818static int nfs3_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400820 struct inode *inode = hdr->inode;
Trond Myklebust8d8928d2018-03-05 12:03:00 -0500821 struct nfs_server *server = NFS_SERVER(inode);
Fred Isamancd841602012-04-20 14:47:44 -0400822
Trond Myklebust16cecdf2014-06-22 12:55:11 -0400823 if (hdr->pgio_done_cb != NULL)
824 return hdr->pgio_done_cb(task, hdr);
825
Fred Isamancd841602012-04-20 14:47:44 -0400826 if (nfs3_async_handle_jukebox(task, inode))
Trond Myklebustec06c092006-03-20 13:44:27 -0500827 return -EAGAIN;
Trond Myklebust8850df92007-09-28 17:20:07 -0400828
Trond Myklebust8d8928d2018-03-05 12:03:00 -0500829 if (task->tk_status >= 0 && !server->read_hdrsize)
830 cmpxchg(&server->read_hdrsize, 0, hdr->res.replen);
831
Fred Isamancd841602012-04-20 14:47:44 -0400832 nfs_invalidate_atime(inode);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400833 nfs_refresh_inode(inode, &hdr->fattr);
Trond Myklebustec06c092006-03-20 13:44:27 -0500834 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835}
836
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400837static void nfs3_proc_read_setup(struct nfs_pgio_header *hdr,
838 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839{
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400840 msg->rpc_proc = &nfs3_procedures[NFS3PROC_READ];
Trond Myklebust8d8928d2018-03-05 12:03:00 -0500841 hdr->args.replen = NFS_SERVER(hdr->inode)->read_hdrsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842}
843
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400844static int nfs3_proc_pgio_rpc_prepare(struct rpc_task *task,
845 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -0400846{
847 rpc_call_start(task);
NeilBrownef1820f2013-09-04 17:04:49 +1000848 return 0;
Bryan Schumakerea7c3302012-03-19 14:54:40 -0400849}
850
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400851static int nfs3_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400853 struct inode *inode = hdr->inode;
Fred Isamancd841602012-04-20 14:47:44 -0400854
Trond Myklebust16cecdf2014-06-22 12:55:11 -0400855 if (hdr->pgio_done_cb != NULL)
856 return hdr->pgio_done_cb(task, hdr);
857
Fred Isamancd841602012-04-20 14:47:44 -0400858 if (nfs3_async_handle_jukebox(task, inode))
Trond Myklebust788e7a82006-03-20 13:44:27 -0500859 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 if (task->tk_status >= 0)
Trond Myklebusta08a8cd2015-02-26 17:36:09 -0500861 nfs_writeback_update_inode(hdr);
Trond Myklebust788e7a82006-03-20 13:44:27 -0500862 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863}
864
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -0400865static void nfs3_proc_write_setup(struct nfs_pgio_header *hdr,
Anna Schumakerfb91fb02018-05-04 16:22:48 -0400866 struct rpc_message *msg,
867 struct rpc_clnt **clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868{
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400869 msg->rpc_proc = &nfs3_procedures[NFS3PROC_WRITE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870}
871
Fred Isaman0b7c0152012-04-20 14:47:39 -0400872static void nfs3_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
873{
874 rpc_call_start(task);
875}
876
877static int nfs3_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878{
Trond Myklebust16cecdf2014-06-22 12:55:11 -0400879 if (data->commit_done_cb != NULL)
880 return data->commit_done_cb(task, data);
881
Chuck Lever006ea732006-03-20 13:44:14 -0500882 if (nfs3_async_handle_jukebox(task, data->inode))
Trond Myklebust788e7a82006-03-20 13:44:27 -0500883 return -EAGAIN;
Trond Myklebust9e08a3c2007-10-08 14:10:31 -0400884 nfs_refresh_inode(data->inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -0500885 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886}
887
Anna Schumakere9ae1ee2018-05-04 16:22:49 -0400888static void nfs3_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg,
889 struct rpc_clnt **clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890{
Trond Myklebustbdc7f022007-07-14 15:40:00 -0400891 msg->rpc_proc = &nfs3_procedures[NFS3PROC_COMMIT];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892}
893
Trond Myklebustbb3393d2017-04-25 16:25:06 -0400894static void nfs3_nlm_alloc_call(void *data)
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -0400895{
896 struct nfs_lock_context *l_ctx = data;
897 if (l_ctx && test_bit(NFS_CONTEXT_UNLOCK, &l_ctx->open_context->flags)) {
898 get_nfs_open_context(l_ctx->open_context);
899 nfs_get_lock_context(l_ctx->open_context);
900 }
901}
902
Trond Myklebustbb3393d2017-04-25 16:25:06 -0400903static bool nfs3_nlm_unlock_prepare(struct rpc_task *task, void *data)
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -0400904{
905 struct nfs_lock_context *l_ctx = data;
906 if (l_ctx && test_bit(NFS_CONTEXT_UNLOCK, &l_ctx->open_context->flags))
907 return nfs_async_iocounter_wait(task, l_ctx);
908 return false;
909
910}
911
Trond Myklebustbb3393d2017-04-25 16:25:06 -0400912static void nfs3_nlm_release_call(void *data)
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -0400913{
914 struct nfs_lock_context *l_ctx = data;
915 struct nfs_open_context *ctx;
916 if (l_ctx && test_bit(NFS_CONTEXT_UNLOCK, &l_ctx->open_context->flags)) {
917 ctx = l_ctx->open_context;
918 nfs_put_lock_context(l_ctx);
919 put_nfs_open_context(ctx);
920 }
921}
922
Colin Ian King1b720402018-02-07 11:27:54 +0000923static const struct nlmclnt_operations nlmclnt_fl_close_lock_ops = {
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -0400924 .nlmclnt_alloc_call = nfs3_nlm_alloc_call,
925 .nlmclnt_unlock_prepare = nfs3_nlm_unlock_prepare,
926 .nlmclnt_release_call = nfs3_nlm_release_call,
927};
928
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929static int
930nfs3_proc_lock(struct file *filp, int cmd, struct file_lock *fl)
931{
Al Viro496ad9a2013-01-23 17:07:38 -0500932 struct inode *inode = file_inode(filp);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -0400933 struct nfs_lock_context *l_ctx = NULL;
934 struct nfs_open_context *ctx = nfs_file_open_context(filp);
935 int status;
Chuck Lever1093a602008-01-11 17:09:59 -0500936
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -0400937 if (fl->fl_flags & FL_CLOSE) {
938 l_ctx = nfs_get_lock_context(ctx);
939 if (IS_ERR(l_ctx))
940 l_ctx = NULL;
941 else
942 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
943 }
944
945 status = nlmclnt_proc(NFS_SERVER(inode)->nlm_host, cmd, fl, l_ctx);
946
947 if (l_ctx)
948 nfs_put_lock_context(l_ctx);
949
950 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951}
952
Bryan Schumaker011e2a72012-06-20 15:53:43 -0400953static int nfs3_have_delegation(struct inode *inode, fmode_t flags)
954{
955 return 0;
956}
957
Bryan Schumakerab962912012-07-16 16:39:11 -0400958static const struct inode_operations nfs3_dir_inode_operations = {
959 .create = nfs_create,
960 .lookup = nfs_lookup,
961 .link = nfs_link,
962 .unlink = nfs_unlink,
963 .symlink = nfs_symlink,
964 .mkdir = nfs_mkdir,
965 .rmdir = nfs_rmdir,
966 .mknod = nfs_mknod,
967 .rename = nfs_rename,
968 .permission = nfs_permission,
969 .getattr = nfs_getattr,
970 .setattr = nfs_setattr,
Christoph Hellwig5f13ee92014-01-30 06:01:52 -0800971#ifdef CONFIG_NFS_V3_ACL
Christoph Hellwig74adf832014-06-18 11:07:03 +0200972 .listxattr = nfs3_listxattr,
Christoph Hellwig013cdf12013-12-20 05:16:53 -0800973 .get_acl = nfs3_get_acl,
974 .set_acl = nfs3_set_acl,
975#endif
Bryan Schumakerab962912012-07-16 16:39:11 -0400976};
977
978static const struct inode_operations nfs3_file_inode_operations = {
979 .permission = nfs_permission,
980 .getattr = nfs_getattr,
981 .setattr = nfs_setattr,
Christoph Hellwig5f13ee92014-01-30 06:01:52 -0800982#ifdef CONFIG_NFS_V3_ACL
Christoph Hellwig74adf832014-06-18 11:07:03 +0200983 .listxattr = nfs3_listxattr,
Christoph Hellwig013cdf12013-12-20 05:16:53 -0800984 .get_acl = nfs3_get_acl,
985 .set_acl = nfs3_set_acl,
986#endif
Bryan Schumakerab962912012-07-16 16:39:11 -0400987};
988
David Howells509de812006-08-22 20:06:11 -0400989const struct nfs_rpc_ops nfs_v3_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 .version = 3, /* protocol version */
991 .dentry_ops = &nfs_dentry_operations,
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +0000992 .dir_inode_ops = &nfs3_dir_inode_operations,
993 .file_inode_ops = &nfs3_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -0400994 .file_ops = &nfs_file_operations,
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -0400995 .nlmclnt_ops = &nlmclnt_fl_close_lock_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 .getroot = nfs3_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -0400997 .submount = nfs_submount,
David Howellsf2aedb72019-12-10 07:31:13 -0500998 .try_get_tree = nfs_try_get_tree,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 .getattr = nfs3_proc_getattr,
1000 .setattr = nfs3_proc_setattr,
1001 .lookup = nfs3_proc_lookup,
1002 .access = nfs3_proc_access,
1003 .readlink = nfs3_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 .create = nfs3_proc_create,
1005 .remove = nfs3_proc_remove,
1006 .unlink_setup = nfs3_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04001007 .unlink_rpc_prepare = nfs3_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 .unlink_done = nfs3_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04001009 .rename_setup = nfs3_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04001010 .rename_rpc_prepare = nfs3_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04001011 .rename_done = nfs3_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 .link = nfs3_proc_link,
1013 .symlink = nfs3_proc_symlink,
1014 .mkdir = nfs3_proc_mkdir,
1015 .rmdir = nfs3_proc_rmdir,
1016 .readdir = nfs3_proc_readdir,
1017 .mknod = nfs3_proc_mknod,
1018 .statfs = nfs3_proc_statfs,
1019 .fsinfo = nfs3_proc_fsinfo,
1020 .pathconf = nfs3_proc_pathconf,
1021 .decode_dirent = nfs3_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04001022 .pgio_rpc_prepare = nfs3_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 .read_setup = nfs3_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05001024 .read_done = nfs3_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 .write_setup = nfs3_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05001026 .write_done = nfs3_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 .commit_setup = nfs3_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04001028 .commit_rpc_prepare = nfs3_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05001029 .commit_done = nfs3_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 .lock = nfs3_proc_lock,
Christoph Hellwig013cdf12013-12-20 05:16:53 -08001031 .clear_acl_cache = forget_all_cached_acls,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04001032 .close_context = nfs_close_context,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04001033 .have_delegation = nfs3_have_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04001034 .alloc_client = nfs_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00001035 .init_client = nfs_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04001036 .free_client = nfs_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04001037 .create_server = nfs3_create_server,
1038 .clone_server = nfs3_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039};