blob: b18f31b2c9e7c9c38cff1a90d6736cc26d2d16cb [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/nfs/nfs4proc.c
3 *
4 * Client-side procedure declarations for NFSv4.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/delay.h>
40#include <linux/errno.h>
41#include <linux/string.h>
Trond Myklebust652f89f2011-12-09 19:05:58 -050042#include <linux/ratelimit.h>
43#include <linux/printk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/sunrpc/clnt.h>
46#include <linux/nfs.h>
47#include <linux/nfs4.h>
48#include <linux/nfs_fs.h>
49#include <linux/nfs_page.h>
Bryan Schumaker9b7160c2011-04-13 14:31:30 -040050#include <linux/nfs_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070052#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040053#include <linux/module.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000054#include <linux/xattr.h>
Andy Adamsonc7a360b2011-01-25 19:15:32 -050055#include <linux/utsname.h>
Jeff Laytond3103102011-12-01 22:44:39 +010056#include <linux/freezer.h>
Jeff Layton1eb5d982018-01-09 08:21:17 -050057#include <linux/iversion.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Trond Myklebust4ce79712005-06-22 17:16:21 +000059#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050061#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050062#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040063#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040064#include "pnfs.h"
Chuck Leverf0920752012-05-21 22:45:41 -040065#include "netns.h"
Trond Myklebust39d43d12020-10-07 18:24:18 -040066#include "sysfs.h"
Anna Schumaker40c64c22015-04-15 13:00:05 -040067#include "nfs4idmap.h"
Trond Myklebust73e39aa2012-11-26 12:49:34 -050068#include "nfs4session.h"
David Howellsde242c02012-12-20 21:52:38 +000069#include "fscache.h"
Frank van der Linden012a2112020-06-23 22:39:03 +000070#include "nfs42.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Trond Myklebustc6d01c62013-08-09 11:51:26 -040072#include "nfs4trace.h"
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#define NFSDBG_FACILITY NFSDBG_PROC
75
Trond Myklebust30846df2018-04-07 13:44:28 -040076#define NFS4_BITMASK_SZ 3
77
Trond Myklebust2066fe82006-09-15 08:30:46 -040078#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079#define NFS4_POLL_RETRY_MAX (15*HZ)
80
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +020081/* file attributes which can be mapped to nfs attributes */
82#define NFS4_VALID_ATTRS (ATTR_MODE \
83 | ATTR_UID \
84 | ATTR_GID \
85 | ATTR_SIZE \
86 | ATTR_ATIME \
87 | ATTR_MTIME \
88 | ATTR_CTIME \
89 | ATTR_ATIME_SET \
90 | ATTR_MTIME_SET)
91
Trond Myklebustcdd4e682006-01-03 09:55:12 +010092struct nfs4_opendata;
Alexandros Batsakisb2579572009-12-14 21:27:57 -080093static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Chuck Lever81934dd2012-03-01 17:01:57 -050095static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
Anna Schumaker2ef61e02021-10-22 13:11:07 -040096static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
97 struct nfs_fattr *fattr, struct inode *inode);
NeilBrowna52458b2018-12-03 11:30:31 +110098static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
Trond Myklebust0ab64e02010-04-16 16:22:51 -040099 struct nfs_fattr *fattr, struct iattr *sattr,
Anna Schumaker1b00ad62021-10-22 13:11:08 -0400100 struct nfs_open_context *ctx, struct nfs4_label *ilabel);
Bryan Schumakerf062eb62011-06-02 14:59:10 -0400101#ifdef CONFIG_NFS_V4_1
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400102static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +1100103 const struct cred *cred,
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400104 struct nfs4_slot *slot,
105 bool is_privileged);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -0400106static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
NeilBrowna52458b2018-12-03 11:30:31 +1100107 const struct cred *);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -0400108static int nfs41_free_stateid(struct nfs_server *, const nfs4_stateid *,
NeilBrowna52458b2018-12-03 11:30:31 +1100109 const struct cred *, bool);
Bryan Schumakerf062eb62011-06-02 14:59:10 -0400110#endif
David Quigleyaa9c2662013-05-22 12:50:44 -0400111
112#ifdef CONFIG_NFS_V4_SECURITY_LABEL
113static inline struct nfs4_label *
114nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
115 struct iattr *sattr, struct nfs4_label *label)
116{
117 int err;
118
119 if (label == NULL)
120 return NULL;
121
122 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
123 return NULL;
124
David Quigleyaa9c2662013-05-22 12:50:44 -0400125 err = security_dentry_init_security(dentry, sattr->ia_mode,
Vivek Goyal15bf3232021-10-12 09:23:07 -0400126 &dentry->d_name, NULL,
127 (void **)&label->label, &label->len);
David Quigleyaa9c2662013-05-22 12:50:44 -0400128 if (err == 0)
129 return label;
130
131 return NULL;
132}
133static inline void
134nfs4_label_release_security(struct nfs4_label *label)
135{
136 if (label)
137 security_release_secctx(label->label, label->len);
138}
139static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
140{
141 if (label)
142 return server->attr_bitmask;
143
144 return server->attr_bitmask_nl;
145}
146#else
147static inline struct nfs4_label *
148nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
149 struct iattr *sattr, struct nfs4_label *l)
150{ return NULL; }
151static inline void
152nfs4_label_release_security(struct nfs4_label *label)
153{ return; }
154static inline u32 *
155nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
156{ return server->attr_bitmask; }
157#endif
158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -0500160static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
Trond Myklebust52567b02009-10-23 14:46:42 -0400162 if (err >= -1000)
163 return err;
164 switch (err) {
165 case -NFS4ERR_RESOURCE:
Weston Andros Adamson30005122013-02-28 20:30:10 -0500166 case -NFS4ERR_LAYOUTTRYLATER:
167 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebust52567b02009-10-23 14:46:42 -0400168 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000169 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson88975382013-08-13 16:37:38 -0400170 case -NFS4ERR_WRONG_CRED:
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000171 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800172 case -NFS4ERR_BADOWNER:
173 case -NFS4ERR_BADNAME:
174 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400175 case -NFS4ERR_SHARE_DENIED:
176 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400177 case -NFS4ERR_MINOR_VERS_MISMATCH:
178 return -EPROTONOSUPPORT;
Trond Myklebust6e3cf242013-03-23 15:22:45 -0400179 case -NFS4ERR_FILE_OPEN:
180 return -EBUSY;
Trond Myklebust9fff59e2020-11-02 20:11:32 -0500181 case -NFS4ERR_NOT_SAME:
182 return -ENOTSYNC;
Trond Myklebust52567b02009-10-23 14:46:42 -0400183 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700185 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400186 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400188 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189}
190
191/*
192 * This is our standard bitmap for GETATTR requests.
193 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400194const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 FATTR4_WORD0_TYPE
196 | FATTR4_WORD0_CHANGE
197 | FATTR4_WORD0_SIZE
198 | FATTR4_WORD0_FSID
199 | FATTR4_WORD0_FILEID,
200 FATTR4_WORD1_MODE
201 | FATTR4_WORD1_NUMLINKS
202 | FATTR4_WORD1_OWNER
203 | FATTR4_WORD1_OWNER_GROUP
204 | FATTR4_WORD1_RAWDEV
205 | FATTR4_WORD1_SPACE_USED
206 | FATTR4_WORD1_TIME_ACCESS
207 | FATTR4_WORD1_TIME_METADATA
Anna Schumakerea96d1e2015-04-03 14:35:59 -0400208 | FATTR4_WORD1_TIME_MODIFY
209 | FATTR4_WORD1_MOUNTED_ON_FILEID,
David Quigleyaa9c2662013-05-22 12:50:44 -0400210#ifdef CONFIG_NFS_V4_SECURITY_LABEL
211 FATTR4_WORD2_SECURITY_LABEL
212#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213};
214
Trond Myklebust1549210f2012-06-05 09:16:47 -0400215static const u32 nfs4_pnfs_open_bitmap[3] = {
216 FATTR4_WORD0_TYPE
217 | FATTR4_WORD0_CHANGE
218 | FATTR4_WORD0_SIZE
219 | FATTR4_WORD0_FSID
220 | FATTR4_WORD0_FILEID,
221 FATTR4_WORD1_MODE
222 | FATTR4_WORD1_NUMLINKS
223 | FATTR4_WORD1_OWNER
224 | FATTR4_WORD1_OWNER_GROUP
225 | FATTR4_WORD1_RAWDEV
226 | FATTR4_WORD1_SPACE_USED
227 | FATTR4_WORD1_TIME_ACCESS
228 | FATTR4_WORD1_TIME_METADATA
229 | FATTR4_WORD1_TIME_MODIFY,
230 FATTR4_WORD2_MDSTHRESHOLD
Trond Myklebust95864c92015-12-26 15:06:03 -0500231#ifdef CONFIG_NFS_V4_SECURITY_LABEL
232 | FATTR4_WORD2_SECURITY_LABEL
233#endif
Trond Myklebust1549210f2012-06-05 09:16:47 -0400234};
235
Andy Adamsone23008e2012-10-02 21:07:32 -0400236static const u32 nfs4_open_noattr_bitmap[3] = {
237 FATTR4_WORD0_TYPE
Andy Adamsone23008e2012-10-02 21:07:32 -0400238 | FATTR4_WORD0_FILEID,
239};
240
David Quigleya09df2c2013-05-22 12:50:41 -0400241const u32 nfs4_statfs_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 FATTR4_WORD0_FILES_AVAIL
243 | FATTR4_WORD0_FILES_FREE
244 | FATTR4_WORD0_FILES_TOTAL,
245 FATTR4_WORD1_SPACE_AVAIL
246 | FATTR4_WORD1_SPACE_FREE
247 | FATTR4_WORD1_SPACE_TOTAL
248};
249
David Quigleya09df2c2013-05-22 12:50:41 -0400250const u32 nfs4_pathconf_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 FATTR4_WORD0_MAXLINK
252 | FATTR4_WORD0_MAXNAME,
253 0
254};
255
Fred Isamandae100c2011-07-30 20:52:37 -0400256const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 | FATTR4_WORD0_MAXREAD
258 | FATTR4_WORD0_MAXWRITE
259 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700260 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400261 | FATTR4_WORD1_FS_LAYOUT_TYPES,
262 FATTR4_WORD2_LAYOUT_BLKSIZE
Peng Tao2a92ee92015-09-26 02:24:37 +0800263 | FATTR4_WORD2_CLONE_BLKSIZE
Trond Myklebust7f08a332021-03-26 09:50:19 -0400264 | FATTR4_WORD2_CHANGE_ATTR_TYPE
Frank van der Lindenb78ef842020-06-23 22:38:55 +0000265 | FATTR4_WORD2_XATTR_SUPPORT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266};
267
David Quigleya09df2c2013-05-22 12:50:41 -0400268const u32 nfs4_fs_locations_bitmap[3] = {
Chuck Leverc05cefc2017-11-05 15:45:22 -0500269 FATTR4_WORD0_CHANGE
Manoj Naik830b8e32006-06-09 09:34:25 -0400270 | FATTR4_WORD0_SIZE
271 | FATTR4_WORD0_FSID
272 | FATTR4_WORD0_FILEID
273 | FATTR4_WORD0_FS_LOCATIONS,
Chuck Leverc05cefc2017-11-05 15:45:22 -0500274 FATTR4_WORD1_OWNER
Manoj Naik830b8e32006-06-09 09:34:25 -0400275 | FATTR4_WORD1_OWNER_GROUP
276 | FATTR4_WORD1_RAWDEV
277 | FATTR4_WORD1_SPACE_USED
278 | FATTR4_WORD1_TIME_ACCESS
279 | FATTR4_WORD1_TIME_METADATA
280 | FATTR4_WORD1_TIME_MODIFY
David Quigleya09df2c2013-05-22 12:50:41 -0400281 | FATTR4_WORD1_MOUNTED_ON_FILEID,
Manoj Naik830b8e32006-06-09 09:34:25 -0400282};
283
Trond Myklebust30846df2018-04-07 13:44:28 -0400284static void nfs4_bitmap_copy_adjust(__u32 *dst, const __u32 *src,
Trond Myklebusta71029b2021-04-10 00:23:03 -0400285 struct inode *inode, unsigned long flags)
Trond Myklebust30846df2018-04-07 13:44:28 -0400286{
287 unsigned long cache_validity;
288
289 memcpy(dst, src, NFS4_BITMASK_SZ*sizeof(*dst));
290 if (!inode || !nfs4_have_delegation(inode, FMODE_READ))
291 return;
292
Trond Myklebusta71029b2021-04-10 00:23:03 -0400293 cache_validity = READ_ONCE(NFS_I(inode)->cache_validity) | flags;
Trond Myklebust30846df2018-04-07 13:44:28 -0400294
Trond Myklebusta71029b2021-04-10 00:23:03 -0400295 /* Remove the attributes over which we have full control */
296 dst[1] &= ~FATTR4_WORD1_RAWDEV;
Trond Myklebust30846df2018-04-07 13:44:28 -0400297 if (!(cache_validity & NFS_INO_INVALID_SIZE))
298 dst[0] &= ~FATTR4_WORD0_SIZE;
299
300 if (!(cache_validity & NFS_INO_INVALID_CHANGE))
301 dst[0] &= ~FATTR4_WORD0_CHANGE;
Trond Myklebust30846df2018-04-07 13:44:28 -0400302
Trond Myklebust720869e2021-04-13 09:41:16 -0400303 if (!(cache_validity & NFS_INO_INVALID_MODE))
304 dst[1] &= ~FATTR4_WORD1_MODE;
Trond Myklebusta71029b2021-04-10 00:23:03 -0400305 if (!(cache_validity & NFS_INO_INVALID_OTHER))
Trond Myklebust720869e2021-04-13 09:41:16 -0400306 dst[1] &= ~(FATTR4_WORD1_OWNER | FATTR4_WORD1_OWNER_GROUP);
Trond Myklebust30846df2018-04-07 13:44:28 -0400307}
308
Al Virobc4785c2006-10-19 23:28:51 -0700309static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 struct nfs4_readdir_arg *readdir)
311{
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400312 unsigned int attrs = FATTR4_WORD0_FILEID | FATTR4_WORD0_TYPE;
Al Viro0dbb4c62006-10-19 23:28:49 -0700313 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000316 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
318 return;
319 }
320
321 readdir->cookie = 0;
322 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
323 if (cookie == 2)
324 return;
325
326 /*
327 * NFSv4 servers do not return entries for '.' and '..'
328 * Therefore, we fake these entries here. We let '.'
329 * have cookie 0 and '..' have cookie 1. Note that
330 * when talking to the server, we always send cookie 0
331 * instead of 1 or 2.
332 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800333 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
335 if (cookie == 0) {
336 *p++ = xdr_one; /* next */
337 *p++ = xdr_zero; /* cookie, first word */
338 *p++ = xdr_one; /* cookie, second word */
339 *p++ = xdr_one; /* entry len */
340 memcpy(p, ".\0\0\0", 4); /* entry */
341 p++;
342 *p++ = xdr_one; /* bitmap length */
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400343 *p++ = htonl(attrs); /* bitmap */
344 *p++ = htonl(12); /* attribute buffer length */
345 *p++ = htonl(NF4DIR);
David Howells2b0143b2015-03-17 22:25:59 +0000346 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 }
348
349 *p++ = xdr_one; /* next */
350 *p++ = xdr_zero; /* cookie, first word */
351 *p++ = xdr_two; /* cookie, second word */
352 *p++ = xdr_two; /* entry len */
353 memcpy(p, "..\0\0", 4); /* entry */
354 p++;
355 *p++ = xdr_one; /* bitmap length */
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400356 *p++ = htonl(attrs); /* bitmap */
357 *p++ = htonl(12); /* attribute buffer length */
358 *p++ = htonl(NF4DIR);
David Howells2b0143b2015-03-17 22:25:59 +0000359 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
361 readdir->pgbase = (char *)p - (char *)start;
362 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800363 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364}
365
Trond Myklebust26d36302016-09-22 13:39:05 -0400366static void nfs4_test_and_free_stateid(struct nfs_server *server,
367 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +1100368 const struct cred *cred)
Trond Myklebust26d36302016-09-22 13:39:05 -0400369{
370 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
371
372 ops->test_and_free_expired(server, stateid, cred);
373}
374
375static void __nfs4_free_revoked_stateid(struct nfs_server *server,
376 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +1100377 const struct cred *cred)
Trond Myklebust26d36302016-09-22 13:39:05 -0400378{
379 stateid->type = NFS4_REVOKED_STATEID_TYPE;
380 nfs4_test_and_free_stateid(server, stateid, cred);
381}
382
383static void nfs4_free_revoked_stateid(struct nfs_server *server,
384 const nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +1100385 const struct cred *cred)
Trond Myklebust26d36302016-09-22 13:39:05 -0400386{
387 nfs4_stateid tmp;
388
389 nfs4_stateid_copy(&tmp, stateid);
390 __nfs4_free_revoked_stateid(server, &tmp, cred);
391}
392
NeilBrown8478eaa2014-09-18 16:09:27 +1000393static long nfs4_update_delay(long *timeout)
394{
395 long ret;
396 if (!timeout)
397 return NFS4_POLL_RETRY_MAX;
398 if (*timeout <= 0)
399 *timeout = NFS4_POLL_RETRY_MIN;
400 if (*timeout > NFS4_POLL_RETRY_MAX)
401 *timeout = NFS4_POLL_RETRY_MAX;
402 ret = *timeout;
403 *timeout <<= 1;
404 return ret;
405}
406
Trond Myklebust0688e642019-04-07 13:59:09 -0400407static int nfs4_delay_killable(long *timeout)
Trond Myklebust65de8722008-12-23 15:21:44 -0500408{
Trond Myklebust65de8722008-12-23 15:21:44 -0500409 might_sleep();
410
NeilBrown8478eaa2014-09-18 16:09:27 +1000411 freezable_schedule_timeout_killable_unsafe(
412 nfs4_update_delay(timeout));
Trond Myklebust0688e642019-04-07 13:59:09 -0400413 if (!__fatal_signal_pending(current))
414 return 0;
415 return -EINTR;
416}
417
418static int nfs4_delay_interruptible(long *timeout)
419{
420 might_sleep();
421
He Zhe59679d92020-07-06 17:52:24 +0800422 freezable_schedule_timeout_interruptible_unsafe(nfs4_update_delay(timeout));
Trond Myklebust0688e642019-04-07 13:59:09 -0400423 if (!signal_pending(current))
424 return 0;
425 return __fatal_signal_pending(current) ? -EINTR :-ERESTARTSYS;
426}
427
428static int nfs4_delay(long *timeout, bool interruptible)
429{
430 if (interruptible)
431 return nfs4_delay_interruptible(timeout);
432 return nfs4_delay_killable(timeout);
Trond Myklebust65de8722008-12-23 15:21:44 -0500433}
434
Trond Myklebust50c80002019-07-11 19:02:18 -0400435static const nfs4_stateid *
436nfs4_recoverable_stateid(const nfs4_stateid *stateid)
437{
438 if (!stateid)
439 return NULL;
440 switch (stateid->type) {
441 case NFS4_OPEN_STATEID_TYPE:
442 case NFS4_LOCK_STATEID_TYPE:
443 case NFS4_DELEGATION_STATEID_TYPE:
444 return stateid;
445 default:
446 break;
447 }
448 return NULL;
449}
450
Trond Myklebust65de8722008-12-23 15:21:44 -0500451/* This is the error handling routine for processes that are allowed
452 * to sleep.
453 */
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400454static int nfs4_do_handle_exception(struct nfs_server *server,
455 int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500456{
457 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500458 struct nfs4_state *state = exception->state;
Trond Myklebust50c80002019-07-11 19:02:18 -0400459 const nfs4_stateid *stateid;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500460 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500461 int ret = errorcode;
462
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400463 exception->delay = 0;
464 exception->recovering = 0;
Trond Myklebust65de8722008-12-23 15:21:44 -0500465 exception->retry = 0;
Trond Myklebust272289a2016-09-22 13:39:15 -0400466
Trond Myklebust50c80002019-07-11 19:02:18 -0400467 stateid = nfs4_recoverable_stateid(exception->stateid);
Trond Myklebust272289a2016-09-22 13:39:15 -0400468 if (stateid == NULL && state != NULL)
Trond Myklebust50c80002019-07-11 19:02:18 -0400469 stateid = nfs4_recoverable_stateid(&state->stateid);
Trond Myklebust272289a2016-09-22 13:39:15 -0400470
Trond Myklebust65de8722008-12-23 15:21:44 -0500471 switch(errorcode) {
472 case 0:
473 return 0;
Trond Myklebustcf61eb22018-05-29 22:06:08 -0400474 case -NFS4ERR_BADHANDLE:
475 case -ESTALE:
476 if (inode != NULL && S_ISREG(inode->i_mode))
477 pnfs_destroy_layout(NFS_I(inode));
478 break;
Trond Myklebust5ba12442015-06-16 11:26:35 -0400479 case -NFS4ERR_DELEG_REVOKED:
480 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust272289a2016-09-22 13:39:15 -0400481 case -NFS4ERR_EXPIRED:
Trond Myklebust5ba12442015-06-16 11:26:35 -0400482 case -NFS4ERR_BAD_STATEID:
Olga Kornievskaiafefa1a82019-06-14 14:22:12 -0400483 case -NFS4ERR_PARTNER_NO_AUTH:
Trond Myklebust272289a2016-09-22 13:39:15 -0400484 if (inode != NULL && stateid != NULL) {
485 nfs_inode_find_state_and_recover(inode,
486 stateid);
487 goto wait_on_recovery;
488 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500489 fallthrough;
Trond Myklebust272289a2016-09-22 13:39:15 -0400490 case -NFS4ERR_OPENMODE:
Trond Myklebust8487c472016-06-26 08:44:35 -0400491 if (inode) {
492 int err;
493
494 err = nfs_async_inode_return_delegation(inode,
495 stateid);
496 if (err == 0)
497 goto wait_on_recovery;
498 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
499 exception->retry = 1;
500 break;
501 }
502 }
Trond Myklebust3114ea72012-03-07 16:39:06 -0500503 if (state == NULL)
504 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400505 ret = nfs4_schedule_stateid_recovery(server, state);
506 if (ret < 0)
507 break;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500508 goto wait_on_recovery;
Trond Myklebust65de8722008-12-23 15:21:44 -0500509 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500510 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500511 nfs4_schedule_lease_recovery(clp);
512 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -0400513 case -NFS4ERR_MOVED:
514 ret = nfs4_schedule_migration_recovery(server);
515 if (ret < 0)
516 break;
517 goto wait_on_recovery;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -0400518 case -NFS4ERR_LEASE_MOVED:
519 nfs4_schedule_lease_moved_recovery(clp);
520 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500521#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400522 case -NFS4ERR_BADSESSION:
523 case -NFS4ERR_BADSLOT:
524 case -NFS4ERR_BAD_HIGH_SLOT:
525 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
526 case -NFS4ERR_DEADSESSION:
527 case -NFS4ERR_SEQ_FALSE_RETRY:
528 case -NFS4ERR_SEQ_MISORDERED:
Trond Myklebust5c441542019-11-13 08:34:00 +0100529 /* Handled in nfs41_sequence_process() */
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400530 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500531#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500532 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500533 if (exception->timeout > HZ) {
534 /* We have retried a decent amount, time to
535 * fail
536 */
537 ret = -EBUSY;
538 break;
539 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500540 fallthrough;
Trond Myklebust65de8722008-12-23 15:21:44 -0500541 case -NFS4ERR_DELAY:
Trond Myklebust2598ed32015-09-20 16:10:18 -0400542 nfs_inc_server_stats(server, NFSIOS_DELAY);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500543 fallthrough;
Trond Myklebust2598ed32015-09-20 16:10:18 -0400544 case -NFS4ERR_GRACE:
Trond Myklebuste85d7ee2016-07-14 18:46:24 -0400545 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -0400546 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400547 exception->delay = 1;
548 return 0;
549
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400550 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500551 case -NFS4ERR_OLD_STATEID:
552 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800553 break;
554 case -NFS4ERR_BADOWNER:
555 /* The following works around a Linux server bug! */
556 case -NFS4ERR_BADNAME:
557 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
558 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
559 exception->retry = 1;
560 printk(KERN_WARNING "NFS: v4 server %s "
561 "does not accept raw "
562 "uid/gids. "
563 "Reenabling the idmapper.\n",
564 server->nfs_client->cl_hostname);
565 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500566 }
567 /* We failed to handle the error */
568 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500569wait_on_recovery:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400570 exception->recovering = 1;
571 return 0;
572}
573
574/* This is the error handling routine for processes that are allowed
575 * to sleep.
576 */
577int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
578{
579 struct nfs_client *clp = server->nfs_client;
580 int ret;
581
582 ret = nfs4_do_handle_exception(server, errorcode, exception);
583 if (exception->delay) {
Trond Myklebust0688e642019-04-07 13:59:09 -0400584 ret = nfs4_delay(&exception->timeout,
585 exception->interruptible);
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400586 goto out_retry;
587 }
588 if (exception->recovering) {
Trond Myklebustdfe1fe72021-06-01 11:10:05 -0400589 if (exception->task_is_privileged)
590 return -EDEADLOCK;
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400591 ret = nfs4_wait_clnt_recover(clp);
592 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
593 return -EIO;
594 goto out_retry;
595 }
596 return ret;
597out_retry:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500598 if (ret == 0)
599 exception->retry = 1;
600 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500601}
602
Trond Myklebust037fc982015-09-20 15:51:00 -0400603static int
604nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
605 int errorcode, struct nfs4_exception *exception)
606{
607 struct nfs_client *clp = server->nfs_client;
608 int ret;
609
610 ret = nfs4_do_handle_exception(server, errorcode, exception);
611 if (exception->delay) {
612 rpc_delay(task, nfs4_update_delay(&exception->timeout));
613 goto out_retry;
614 }
615 if (exception->recovering) {
Trond Myklebustdfe1fe72021-06-01 11:10:05 -0400616 if (exception->task_is_privileged)
617 return -EDEADLOCK;
Trond Myklebust037fc982015-09-20 15:51:00 -0400618 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
619 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
620 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
621 goto out_retry;
622 }
623 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
624 ret = -EIO;
625 return ret;
626out_retry:
Bill Baker0f90be12018-06-19 16:24:58 -0500627 if (ret == 0) {
Trond Myklebust037fc982015-09-20 15:51:00 -0400628 exception->retry = 1;
Bill Baker0f90be12018-06-19 16:24:58 -0500629 /*
630 * For NFS4ERR_MOVED, the client transport will need to
631 * be recomputed after migration recovery has completed.
632 */
633 if (errorcode == -NFS4ERR_MOVED)
634 rpc_task_release_transport(task);
635 }
Trond Myklebust037fc982015-09-20 15:51:00 -0400636 return ret;
637}
638
Olga Kornievskaia0f913a52018-07-09 15:13:33 -0400639int
Trond Myklebust037fc982015-09-20 15:51:00 -0400640nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
641 struct nfs4_state *state, long *timeout)
642{
643 struct nfs4_exception exception = {
644 .state = state,
645 };
646
647 if (task->tk_status >= 0)
648 return 0;
649 if (timeout)
650 exception.timeout = *timeout;
651 task->tk_status = nfs4_async_handle_exception(task, server,
652 task->tk_status,
653 &exception);
654 if (exception.delay && timeout)
655 *timeout = exception.timeout;
656 if (exception.retry)
657 return -EAGAIN;
658 return 0;
659}
660
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400661/*
662 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
663 * or 'false' otherwise.
664 */
665static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
666{
667 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
Anna Schumakereeea5362017-01-11 16:01:21 -0500668 return (flavor == RPC_AUTH_GSS_KRB5I) || (flavor == RPC_AUTH_GSS_KRB5P);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400669}
Trond Myklebust65de8722008-12-23 15:21:44 -0500670
Trond Myklebust452e9352010-07-31 14:29:06 -0400671static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 spin_lock(&clp->cl_lock);
674 if (time_before(clp->cl_last_renewal,timestamp))
675 clp->cl_last_renewal = timestamp;
676 spin_unlock(&clp->cl_lock);
677}
678
Trond Myklebust452e9352010-07-31 14:29:06 -0400679static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
680{
Trond Myklebustbe824162015-07-05 14:50:46 -0400681 struct nfs_client *clp = server->nfs_client;
682
683 if (!nfs4_has_session(clp))
684 do_renew_lease(clp, timestamp);
Trond Myklebust452e9352010-07-31 14:29:06 -0400685}
686
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400687struct nfs4_call_sync_data {
688 const struct nfs_server *seq_server;
689 struct nfs4_sequence_args *seq_args;
690 struct nfs4_sequence_res *seq_res;
691};
692
Trond Myklebustbe3a5d22015-06-23 19:51:55 +0800693void nfs4_init_sequence(struct nfs4_sequence_args *args,
Anna Schumakerfba83f32018-05-04 16:22:50 -0400694 struct nfs4_sequence_res *res, int cache_reply,
695 int privileged)
Chuck Levera9c92d62013-08-09 12:48:18 -0400696{
697 args->sa_slot = NULL;
698 args->sa_cache_this = cache_reply;
Anna Schumakerfba83f32018-05-04 16:22:50 -0400699 args->sa_privileged = privileged;
Chuck Levera9c92d62013-08-09 12:48:18 -0400700
701 res->sr_slot = NULL;
702}
703
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400704static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
Chuck Lever3bd23842013-08-09 12:49:19 -0400705{
706 struct nfs4_slot *slot = res->sr_slot;
707 struct nfs4_slot_table *tbl;
708
Chuck Lever3bd23842013-08-09 12:49:19 -0400709 tbl = slot->table;
710 spin_lock(&tbl->slot_tbl_lock);
711 if (!nfs41_wake_and_assign_slot(tbl, slot))
712 nfs4_free_slot(tbl, slot);
713 spin_unlock(&tbl->slot_tbl_lock);
714
715 res->sr_slot = NULL;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400716}
717
718static int nfs40_sequence_done(struct rpc_task *task,
719 struct nfs4_sequence_res *res)
720{
721 if (res->sr_slot != NULL)
722 nfs40_sequence_free_slot(res);
Chuck Lever3bd23842013-08-09 12:49:19 -0400723 return 1;
724}
725
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400726#if defined(CONFIG_NFS_V4_1)
727
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400728static void nfs41_release_slot(struct nfs4_slot *slot)
Andy Adamson13615872009-04-01 09:22:17 -0400729{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500730 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400731 struct nfs4_slot_table *tbl;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500732 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400733
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400734 if (!slot)
735 return;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500736 tbl = slot->table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500737 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500738
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400739 /* Bump the slot sequence number */
740 if (slot->seq_done)
741 slot->seq_nr++;
742 slot->seq_done = 0;
743
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500744 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500745 /* Be nice to the server: try to ensure that the last transmitted
746 * value for highest_user_slotid <= target_highest_slotid
747 */
748 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
749 send_new_highest_used_slotid = true;
750
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500751 if (nfs41_wake_and_assign_slot(tbl, slot)) {
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500752 send_new_highest_used_slotid = false;
753 goto out_unlock;
754 }
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500755 nfs4_free_slot(tbl, slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500756
757 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
758 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500759out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500760 spin_unlock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500761 if (send_new_highest_used_slotid)
Anna Schumaker3f10a6a2015-07-13 14:01:31 -0400762 nfs41_notify_server(session->clp);
Trond Myklebust045d2a62016-08-28 13:25:43 -0400763 if (waitqueue_active(&tbl->slot_waitq))
764 wake_up_all(&tbl->slot_waitq);
Andy Adamson13615872009-04-01 09:22:17 -0400765}
766
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400767static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
768{
769 nfs41_release_slot(res->sr_slot);
770 res->sr_slot = NULL;
771}
772
Trond Myklebust3453d572018-06-20 17:53:34 -0400773static void nfs4_slot_sequence_record_sent(struct nfs4_slot *slot,
774 u32 seqnr)
775{
776 if ((s32)(seqnr - slot->seq_nr_highest_sent) > 0)
777 slot->seq_nr_highest_sent = seqnr;
778}
779static void nfs4_slot_sequence_acked(struct nfs4_slot *slot,
780 u32 seqnr)
781{
782 slot->seq_nr_highest_sent = seqnr;
783 slot->seq_nr_last_acked = seqnr;
784}
785
Anna Schumaker913fadc2020-07-08 10:33:40 -0400786static void nfs4_probe_sequence(struct nfs_client *client, const struct cred *cred,
787 struct nfs4_slot *slot)
788{
789 struct rpc_task *task = _nfs41_proc_sequence(client, cred, slot, true);
790 if (!IS_ERR(task))
791 rpc_put_task_async(task);
792}
793
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400794static int nfs41_sequence_process(struct rpc_task *task,
795 struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400796{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500797 struct nfs4_session *session;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500798 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400799 struct nfs_client *clp;
Trond Myklebust5c441542019-11-13 08:34:00 +0100800 int status;
Trond Myklebust85563072012-12-11 10:31:12 -0500801 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400802
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500803 if (slot == NULL)
804 goto out_noaction;
Bryan Schumaker468f8612011-04-18 15:57:32 -0400805 /* don't increment the sequence number if the task wasn't sent */
Trond Myklebustc71c46f2019-03-01 11:40:05 -0500806 if (!RPC_WAS_SENT(task) || slot->seq_done)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400807 goto out;
808
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500809 session = slot->table->session;
Anna Schumaker913fadc2020-07-08 10:33:40 -0400810 clp = session->clp;
Trond Myklebust933602e2012-11-16 12:12:38 -0500811
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400812 trace_nfs4_sequence_done(session, res);
Trond Myklebust5c441542019-11-13 08:34:00 +0100813
814 status = res->sr_status;
815 if (task->tk_status == -NFS4ERR_DEADSESSION)
816 status = -NFS4ERR_DEADSESSION;
817
Andy Adamson691daf32009-12-04 15:55:39 -0500818 /* Check the SEQUENCE operation status */
Trond Myklebust5c441542019-11-13 08:34:00 +0100819 switch (status) {
Trond Myklebust14516c32010-07-31 14:29:06 -0400820 case 0:
Trond Myklebust3453d572018-06-20 17:53:34 -0400821 /* Mark this sequence number as having been acked */
822 nfs4_slot_sequence_acked(slot, slot->seq_nr);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400823 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400824 slot->seq_done = 1;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500825 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500826 /* Check sequence flags */
Trond Myklebust0a014a42016-09-22 13:38:51 -0400827 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
828 !!slot->privileged);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500829 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400830 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500831 case 1:
832 /*
833 * sr_status remains 1 if an RPC level error occurred.
834 * The server may or may not have processed the sequence
835 * operation..
Trond Myklebustac20d162012-12-15 15:36:07 -0500836 */
Trond Myklebust3453d572018-06-20 17:53:34 -0400837 nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
838 slot->seq_done = 1;
Trond Myklebustac20d162012-12-15 15:36:07 -0500839 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400840 case -NFS4ERR_DELAY:
841 /* The server detected a resend of the RPC call and
842 * returned NFS4ERR_DELAY as per Section 2.10.6.2
843 * of RFC5661.
844 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500845 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400846 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500847 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500848 slot->seq_nr);
Trond Myklebust3453d572018-06-20 17:53:34 -0400849 nfs4_slot_sequence_acked(slot, slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400850 goto out_retry;
Trond Myklebustf9312a52018-06-09 19:10:31 -0400851 case -NFS4ERR_RETRY_UNCACHED_REP:
852 case -NFS4ERR_SEQ_FALSE_RETRY:
853 /*
854 * The server thinks we tried to replay a request.
855 * Retry the call after bumping the sequence ID.
856 */
Trond Myklebust3453d572018-06-20 17:53:34 -0400857 nfs4_slot_sequence_acked(slot, slot->seq_nr);
Trond Myklebustf9312a52018-06-09 19:10:31 -0400858 goto retry_new_seq;
Trond Myklebust85563072012-12-11 10:31:12 -0500859 case -NFS4ERR_BADSLOT:
860 /*
861 * The slot id we used was probably retired. Try again
862 * using a different slot id.
863 */
Trond Myklebust99589102018-06-09 12:50:50 -0400864 if (slot->slot_nr < slot->table->target_highest_slotid)
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400865 goto session_recover;
Trond Myklebuste8794442012-12-15 13:56:18 -0500866 goto retry_nowait;
867 case -NFS4ERR_SEQ_MISORDERED:
Trond Myklebust3453d572018-06-20 17:53:34 -0400868 nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
Trond Myklebuste8794442012-12-15 13:56:18 -0500869 /*
Trond Myklebust3453d572018-06-20 17:53:34 -0400870 * Were one or more calls using this slot interrupted?
871 * If the server never received the request, then our
Anna Schumaker913fadc2020-07-08 10:33:40 -0400872 * transmitted slot sequence number may be too high. However,
873 * if the server did receive the request then it might
874 * accidentally give us a reply with a mismatched operation.
875 * We can sort this out by sending a lone sequence operation
876 * to the server on the same slot.
Trond Myklebustac20d162012-12-15 15:36:07 -0500877 */
Trond Myklebust3453d572018-06-20 17:53:34 -0400878 if ((s32)(slot->seq_nr - slot->seq_nr_last_acked) > 1) {
879 slot->seq_nr--;
Anna Schumaker913fadc2020-07-08 10:33:40 -0400880 if (task->tk_msg.rpc_proc != &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE]) {
881 nfs4_probe_sequence(clp, task->tk_msg.rpc_cred, slot);
882 res->sr_slot = NULL;
883 }
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500884 goto retry_nowait;
885 }
Trond Myklebust3453d572018-06-20 17:53:34 -0400886 /*
887 * RFC5661:
888 * A retry might be sent while the original request is
889 * still in progress on the replier. The replier SHOULD
890 * deal with the issue by returning NFS4ERR_DELAY as the
891 * reply to SEQUENCE or CB_SEQUENCE operation, but
892 * implementations MAY return NFS4ERR_SEQ_MISORDERED.
893 *
894 * Restart the search after a delay.
895 */
896 slot->seq_nr = slot->seq_nr_highest_sent;
897 goto out_retry;
Trond Myklebust5c441542019-11-13 08:34:00 +0100898 case -NFS4ERR_BADSESSION:
899 case -NFS4ERR_DEADSESSION:
900 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
901 goto session_recover;
Trond Myklebust14516c32010-07-31 14:29:06 -0400902 default:
903 /* Just update the slot sequence no. */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400904 slot->seq_done = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400905 }
906out:
907 /* The session may be reset by one of the error handlers. */
908 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500909out_noaction:
Trond Myklebust85563072012-12-11 10:31:12 -0500910 return ret;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400911session_recover:
Trond Myklebust5c441542019-11-13 08:34:00 +0100912 nfs4_schedule_session_recovery(session, status);
913 dprintk("%s ERROR: %d Reset session\n", __func__, status);
914 nfs41_sequence_free_slot(res);
915 goto out;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400916retry_new_seq:
917 ++slot->seq_nr;
Trond Myklebuste8794442012-12-15 13:56:18 -0500918retry_nowait:
919 if (rpc_restart_call_prepare(task)) {
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400920 nfs41_sequence_free_slot(res);
Trond Myklebuste8794442012-12-15 13:56:18 -0500921 task->tk_status = 0;
922 ret = 0;
923 }
924 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400925out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400926 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400927 goto out;
928 rpc_delay(task, NFS4_POLL_RETRY_MAX);
929 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400930}
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400931
932int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
933{
934 if (!nfs41_sequence_process(task, res))
935 return 0;
936 if (res->sr_slot != NULL)
937 nfs41_sequence_free_slot(res);
938 return 1;
939
940}
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500941EXPORT_SYMBOL_GPL(nfs41_sequence_done);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400942
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400943static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
944{
945 if (res->sr_slot == NULL)
946 return 1;
947 if (res->sr_slot->table->session != NULL)
948 return nfs41_sequence_process(task, res);
949 return nfs40_sequence_done(task, res);
950}
951
952static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
953{
954 if (res->sr_slot != NULL) {
955 if (res->sr_slot->table->session != NULL)
956 nfs41_sequence_free_slot(res);
957 else
958 nfs40_sequence_free_slot(res);
959 }
960}
961
Peng Tao2c4b1312014-06-11 05:24:16 +0800962int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400963{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500964 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400965 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400966 if (!res->sr_slot->table->session)
967 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400968 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400969}
Peng Tao2c4b1312014-06-11 05:24:16 +0800970EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Trond Myklebustdf896452010-06-16 09:52:26 -0400971
Andy Adamsonce5039c2009-04-01 09:22:13 -0400972static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
973{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400974 struct nfs4_call_sync_data *data = calldata;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400975
Trond Myklebust035168ab2010-06-16 09:52:26 -0400976 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
977
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500978 nfs4_setup_sequence(data->seq_server->nfs_client,
979 data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400980}
981
Andy Adamson69ab40c2009-04-01 09:22:19 -0400982static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
983{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400984 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400985
Trond Myklebust14516c32010-07-31 14:29:06 -0400986 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400987}
988
Trond Myklebust17280172012-03-11 13:11:00 -0400989static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400990 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400991 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400992};
993
Chuck Lever3bd23842013-08-09 12:49:19 -0400994#else /* !CONFIG_NFS_V4_1 */
995
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400996static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
997{
998 return nfs40_sequence_done(task, res);
999}
1000
1001static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
1002{
1003 if (res->sr_slot != NULL)
1004 nfs40_sequence_free_slot(res);
1005}
1006
Peng Tao2c4b1312014-06-11 05:24:16 +08001007int nfs4_sequence_done(struct rpc_task *task,
1008 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -04001009{
Chuck Lever3bd23842013-08-09 12:49:19 -04001010 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -04001011}
Peng Tao2c4b1312014-06-11 05:24:16 +08001012EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Chuck Lever3bd23842013-08-09 12:49:19 -04001013
1014#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001015
Trond Myklebustc1dffe02019-03-01 12:13:34 -05001016static void nfs41_sequence_res_init(struct nfs4_sequence_res *res)
1017{
1018 res->sr_timestamp = jiffies;
1019 res->sr_status_flags = 0;
1020 res->sr_status = 1;
1021}
1022
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04001023static
1024void nfs4_sequence_attach_slot(struct nfs4_sequence_args *args,
1025 struct nfs4_sequence_res *res,
1026 struct nfs4_slot *slot)
1027{
1028 if (!slot)
1029 return;
1030 slot->privileged = args->sa_privileged ? 1 : 0;
1031 args->sa_slot = slot;
1032
1033 res->sr_slot = slot;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04001034}
1035
1036int nfs4_setup_sequence(struct nfs_client *client,
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001037 struct nfs4_sequence_args *args,
1038 struct nfs4_sequence_res *res,
1039 struct rpc_task *task)
1040{
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001041 struct nfs4_session *session = nfs4_get_session(client);
Anna Schumaker76ee0352017-01-10 16:49:31 -05001042 struct nfs4_slot_table *tbl = client->cl_slot_tbl;
Anna Schumaker3d358082017-01-11 10:54:04 -05001043 struct nfs4_slot *slot;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001044
Anna Schumaker9dd9107f2017-01-10 12:01:46 -05001045 /* slot already allocated? */
1046 if (res->sr_slot != NULL)
1047 goto out_start;
1048
Trond Myklebust6b2e6852019-04-07 13:58:49 -04001049 if (session)
Anna Schumaker76ee0352017-01-10 16:49:31 -05001050 tbl = &session->fc_slot_table;
Anna Schumaker76ee0352017-01-10 16:49:31 -05001051
Trond Myklebust3453d572018-06-20 17:53:34 -04001052 spin_lock(&tbl->slot_tbl_lock);
1053 /* The state manager will wait until the slot table is empty */
1054 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
1055 goto out_sleep;
Anna Schumaker6994cdd2017-01-10 16:13:27 -05001056
Trond Myklebust3453d572018-06-20 17:53:34 -04001057 slot = nfs4_alloc_slot(tbl);
1058 if (IS_ERR(slot)) {
Trond Myklebust3453d572018-06-20 17:53:34 -04001059 if (slot == ERR_PTR(-ENOMEM))
Trond Myklebust6b2e6852019-04-07 13:58:49 -04001060 goto out_sleep_timeout;
Trond Myklebust3453d572018-06-20 17:53:34 -04001061 goto out_sleep;
Anna Schumaker3d358082017-01-11 10:54:04 -05001062 }
Trond Myklebust3453d572018-06-20 17:53:34 -04001063 spin_unlock(&tbl->slot_tbl_lock);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001064
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04001065 nfs4_sequence_attach_slot(args, res, slot);
Anna Schumaker3d358082017-01-11 10:54:04 -05001066
Anna Schumakerad05cc02017-01-11 13:37:06 -05001067 trace_nfs4_setup_sequence(session, args);
Anna Schumaker9dd9107f2017-01-10 12:01:46 -05001068out_start:
Trond Myklebustc1dffe02019-03-01 12:13:34 -05001069 nfs41_sequence_res_init(res);
Anna Schumaker9dd9107f2017-01-10 12:01:46 -05001070 rpc_call_start(task);
1071 return 0;
Trond Myklebust6b2e6852019-04-07 13:58:49 -04001072out_sleep_timeout:
1073 /* Try again in 1/4 second */
1074 if (args->sa_privileged)
1075 rpc_sleep_on_priority_timeout(&tbl->slot_tbl_waitq, task,
1076 jiffies + (HZ >> 2), RPC_PRIORITY_PRIVILEGED);
1077 else
1078 rpc_sleep_on_timeout(&tbl->slot_tbl_waitq, task,
1079 NULL, jiffies + (HZ >> 2));
1080 spin_unlock(&tbl->slot_tbl_lock);
1081 return -EAGAIN;
Anna Schumaker0dcee8b2017-01-10 16:29:54 -05001082out_sleep:
1083 if (args->sa_privileged)
1084 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
Trond Myklebust8357a9b2019-04-07 13:58:48 -04001085 RPC_PRIORITY_PRIVILEGED);
Anna Schumaker0dcee8b2017-01-10 16:29:54 -05001086 else
1087 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
1088 spin_unlock(&tbl->slot_tbl_lock);
1089 return -EAGAIN;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001090}
1091EXPORT_SYMBOL_GPL(nfs4_setup_sequence);
1092
Chuck Lever9915ea72013-08-09 12:48:27 -04001093static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
1094{
1095 struct nfs4_call_sync_data *data = calldata;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05001096 nfs4_setup_sequence(data->seq_server->nfs_client,
Chuck Lever9915ea72013-08-09 12:48:27 -04001097 data->seq_args, data->seq_res, task);
1098}
1099
1100static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
1101{
1102 struct nfs4_call_sync_data *data = calldata;
1103 nfs4_sequence_done(task, data->seq_res);
1104}
1105
1106static const struct rpc_call_ops nfs40_call_sync_ops = {
1107 .rpc_call_prepare = nfs40_call_sync_prepare,
1108 .rpc_call_done = nfs40_call_sync_done,
1109};
1110
Anna Schumaker48c05852019-08-14 15:27:00 -04001111static int nfs4_call_sync_custom(struct rpc_task_setup *task_setup)
1112{
1113 int ret;
1114 struct rpc_task *task;
1115
1116 task = rpc_run_task(task_setup);
1117 if (IS_ERR(task))
1118 return PTR_ERR(task);
1119
1120 ret = task->tk_status;
1121 rpc_put_task(task);
1122 return ret;
1123}
1124
Trond Myklebustc74dfe92020-01-06 15:39:37 -05001125static int nfs4_do_call_sync(struct rpc_clnt *clnt,
1126 struct nfs_server *server,
1127 struct rpc_message *msg,
1128 struct nfs4_sequence_args *args,
1129 struct nfs4_sequence_res *res,
1130 unsigned short task_flags)
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001131{
Chuck Lever9915ea72013-08-09 12:48:27 -04001132 struct nfs_client *clp = server->nfs_client;
1133 struct nfs4_call_sync_data data = {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001134 .seq_server = server,
Trond Myklebustad389da2007-06-05 12:30:00 -04001135 .seq_args = args,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001136 .seq_res = res,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001137 };
1138 struct rpc_task_setup task_setup = {
Trond Myklebustad389da2007-06-05 12:30:00 -04001139 .rpc_client = clnt,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001140 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -04001141 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebustc74dfe92020-01-06 15:39:37 -05001142 .callback_data = &data,
1143 .flags = task_flags,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001144 };
1145
Anna Schumaker48c05852019-08-14 15:27:00 -04001146 return nfs4_call_sync_custom(&task_setup);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001147}
1148
Trond Myklebustc74dfe92020-01-06 15:39:37 -05001149static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
1150 struct nfs_server *server,
1151 struct rpc_message *msg,
1152 struct nfs4_sequence_args *args,
1153 struct nfs4_sequence_res *res)
1154{
Olga Kornievskaia85e39fe2021-06-23 23:28:51 -04001155 unsigned short task_flags = 0;
1156
1157 if (server->nfs_client->cl_minorversion)
1158 task_flags = RPC_TASK_MOVEABLE;
1159 return nfs4_do_call_sync(clnt, server, msg, args, res, task_flags);
Trond Myklebustc74dfe92020-01-06 15:39:37 -05001160}
1161
1162
Bryan Schumaker7c513052011-03-24 17:12:24 +00001163int nfs4_call_sync(struct rpc_clnt *clnt,
1164 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001165 struct rpc_message *msg,
1166 struct nfs4_sequence_args *args,
1167 struct nfs4_sequence_res *res,
1168 int cache_reply)
1169{
Anna Schumakerfba83f32018-05-04 16:22:50 -04001170 nfs4_init_sequence(args, res, cache_reply, 0);
Chuck Lever9915ea72013-08-09 12:48:27 -04001171 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001172}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
Trond Myklebust3c591172018-07-31 15:54:10 -04001174static void
1175nfs4_inc_nlink_locked(struct inode *inode)
1176{
Trond Myklebust82eae5a2021-04-01 14:59:59 -04001177 nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE |
1178 NFS_INO_INVALID_CTIME |
1179 NFS_INO_INVALID_NLINK);
Trond Myklebust3c591172018-07-31 15:54:10 -04001180 inc_nlink(inode);
1181}
1182
1183static void
Trond Myklebust1301e422021-04-01 14:57:56 -04001184nfs4_inc_nlink(struct inode *inode)
1185{
1186 spin_lock(&inode->i_lock);
1187 nfs4_inc_nlink_locked(inode);
1188 spin_unlock(&inode->i_lock);
1189}
1190
1191static void
Trond Myklebust3c591172018-07-31 15:54:10 -04001192nfs4_dec_nlink_locked(struct inode *inode)
1193{
Trond Myklebust82eae5a2021-04-01 14:59:59 -04001194 nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE |
1195 NFS_INO_INVALID_CTIME |
1196 NFS_INO_INVALID_NLINK);
Trond Myklebust3c591172018-07-31 15:54:10 -04001197 drop_nlink(inode);
1198}
1199
1200static void
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001201nfs4_update_changeattr_locked(struct inode *inode,
1202 struct nfs4_change_info *cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04001203 unsigned long timestamp, unsigned long cache_validity)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204{
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001205 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust6f9be832021-03-26 11:01:19 -04001206 u64 change_attr = inode_peek_iversion_raw(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
Trond Myklebustb6f80a22021-03-08 14:42:55 -05001208 cache_validity |= NFS_INO_INVALID_CTIME | NFS_INO_INVALID_MTIME;
Trond Myklebust20cf7d42021-06-25 15:49:31 -04001209 if (S_ISDIR(inode->i_mode))
1210 cache_validity |= NFS_INO_INVALID_DATA;
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001211
Trond Myklebust6f9be832021-03-26 11:01:19 -04001212 switch (NFS_SERVER(inode)->change_attr_type) {
1213 case NFS4_CHANGE_TYPE_IS_UNDEFINED:
Trond Myklebust20cf7d42021-06-25 15:49:31 -04001214 if (cinfo->after == change_attr)
Trond Myklebust6f9be832021-03-26 11:01:19 -04001215 goto out;
1216 break;
1217 default:
1218 if ((s64)(change_attr - cinfo->after) >= 0)
1219 goto out;
1220 }
1221
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001222 inode_set_iversion_raw(inode, cinfo->after);
Trond Myklebust20cf7d42021-06-25 15:49:31 -04001223 if (!cinfo->atomic || cinfo->before != change_attr) {
1224 if (S_ISDIR(inode->i_mode))
1225 nfs_force_lookup_revalidate(inode);
1226
1227 if (!NFS_PROTO(inode)->have_delegation(inode, FMODE_READ))
1228 cache_validity |=
1229 NFS_INO_INVALID_ACCESS | NFS_INO_INVALID_ACL |
1230 NFS_INO_INVALID_SIZE | NFS_INO_INVALID_OTHER |
1231 NFS_INO_INVALID_BLOCKS | NFS_INO_INVALID_NLINK |
1232 NFS_INO_INVALID_MODE | NFS_INO_INVALID_XATTR |
1233 NFS_INO_REVAL_PAGECACHE;
1234 nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
1235 }
1236 nfsi->attrtimeo_timestamp = jiffies;
Trond Myklebustd3129ef2017-01-11 22:07:28 -05001237 nfsi->read_cache_jiffies = timestamp;
Trond Myklebust3235b402015-02-26 19:52:06 -05001238 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
Trond Myklebustc8d07152018-07-31 15:54:12 -04001239 nfsi->cache_validity &= ~NFS_INO_INVALID_CHANGE;
Trond Myklebust6f9be832021-03-26 11:01:19 -04001240out:
1241 nfs_set_cache_invalid(inode, cache_validity);
Trond Myklebust3c591172018-07-31 15:54:10 -04001242}
1243
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001244void
1245nfs4_update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04001246 unsigned long timestamp, unsigned long cache_validity)
Trond Myklebust3c591172018-07-31 15:54:10 -04001247{
1248 spin_lock(&dir->i_lock);
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001249 nfs4_update_changeattr_locked(dir, cinfo, timestamp, cache_validity);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 spin_unlock(&dir->i_lock);
1251}
1252
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001253struct nfs4_open_createattrs {
1254 struct nfs4_label *label;
1255 struct iattr *sattr;
1256 const __u32 verf[2];
1257};
1258
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001259static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1260 int err, struct nfs4_exception *exception)
1261{
1262 if (err != -EINVAL)
1263 return false;
1264 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1265 return false;
1266 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1267 exception->retry = 1;
1268 return true;
1269}
1270
Trond Myklebust1bf85d82019-06-27 06:30:48 -04001271static fmode_t _nfs4_ctx_to_accessmode(const struct nfs_open_context *ctx)
1272{
1273 return ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
1274}
1275
1276static fmode_t _nfs4_ctx_to_openmode(const struct nfs_open_context *ctx)
1277{
1278 fmode_t ret = ctx->mode & (FMODE_READ|FMODE_WRITE);
1279
1280 return (ctx->mode & FMODE_EXEC) ? FMODE_READ | ret : ret;
1281}
1282
Trond Myklebust6ae37332015-01-30 14:21:14 -05001283static u32
1284nfs4_map_atomic_open_share(struct nfs_server *server,
1285 fmode_t fmode, int openflags)
1286{
1287 u32 res = 0;
1288
1289 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1290 case FMODE_READ:
1291 res = NFS4_SHARE_ACCESS_READ;
1292 break;
1293 case FMODE_WRITE:
1294 res = NFS4_SHARE_ACCESS_WRITE;
1295 break;
1296 case FMODE_READ|FMODE_WRITE:
1297 res = NFS4_SHARE_ACCESS_BOTH;
1298 }
1299 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1300 goto out;
1301 /* Want no delegation if we're using O_DIRECT */
1302 if (openflags & O_DIRECT)
1303 res |= NFS4_SHARE_WANT_NO_DELEG;
1304out:
1305 return res;
1306}
1307
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001308static enum open_claim_type4
1309nfs4_map_atomic_open_claim(struct nfs_server *server,
1310 enum open_claim_type4 claim)
1311{
1312 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1313 return claim;
1314 switch (claim) {
1315 default:
1316 return claim;
1317 case NFS4_OPEN_CLAIM_FH:
1318 return NFS4_OPEN_CLAIM_NULL;
1319 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1320 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1321 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1322 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1323 }
1324}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
1326static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001327{
1328 p->o_res.f_attr = &p->f_attr;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001329 p->o_res.seqid = p->o_arg.seqid;
1330 p->c_res.seqid = p->c_arg.seqid;
1331 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -04001332 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001333 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd12012-01-07 13:22:46 -05001334 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001335}
1336
Al Viro82a2c1b2011-06-22 18:30:55 -04001337static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001338 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001339 const struct nfs4_open_createattrs *c,
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001340 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001341 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001342{
Al Viro82a2c1b2011-06-22 18:30:55 -04001343 struct dentry *parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001344 struct inode *dir = d_inode(parent);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001345 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust63f5f792015-01-23 19:19:25 -05001346 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001347 struct nfs4_label *label = (c != NULL) ? c->label : NULL;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001348 struct nfs4_opendata *p;
1349
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001350 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001351 if (p == NULL)
1352 goto err;
David Quigley14c43f72013-05-22 12:50:43 -04001353
Anna Schumaker76baa2b2021-10-22 13:11:06 -04001354 p->f_attr.label = nfs4_label_alloc(server, gfp_mask);
1355 if (IS_ERR(p->f_attr.label))
David Quigley14c43f72013-05-22 12:50:43 -04001356 goto err_free_p;
1357
Kinglong Meea49c2692015-07-27 15:31:38 +08001358 p->a_label = nfs4_label_alloc(server, gfp_mask);
1359 if (IS_ERR(p->a_label))
1360 goto err_free_f;
1361
Trond Myklebust63f5f792015-01-23 19:19:25 -05001362 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1363 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05001364 if (IS_ERR(p->o_arg.seqid))
David Quigley14c43f72013-05-22 12:50:43 -04001365 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -04001366 nfs_sb_active(dentry->d_sb);
1367 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001368 p->dir = parent;
1369 p->owner = sp;
1370 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001371 p->o_arg.open_flags = flags;
1372 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Trond Myklebust536585c2016-11-10 15:40:34 -05001373 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
Trond Myklebust6ae37332015-01-30 14:21:14 -05001374 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1375 fmode, flags);
Benjamin Coddington90910512019-06-07 06:37:30 -04001376 if (flags & O_CREAT) {
1377 p->o_arg.umask = current_umask();
1378 p->o_arg.label = nfs4_label_copy(p->a_label, label);
1379 if (c->sattr != NULL && c->sattr->ia_valid != 0) {
1380 p->o_arg.u.attrs = &p->attrs;
1381 memcpy(&p->attrs, c->sattr, sizeof(p->attrs));
1382
1383 memcpy(p->o_arg.u.verifier.data, c->verf,
1384 sizeof(p->o_arg.u.verifier.data));
1385 }
1386 }
Trond Myklebusta1e7f302021-07-14 13:00:58 -04001387 /* ask server to check for all possible rights as results
1388 * are cached */
1389 switch (p->o_arg.claim) {
1390 default:
1391 break;
1392 case NFS4_OPEN_CLAIM_NULL:
1393 case NFS4_OPEN_CLAIM_FH:
1394 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
1395 NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE |
1396 NFS4_ACCESS_EXECUTE;
Frank van der Linden72832a22020-06-23 22:38:58 +00001397#ifdef CONFIG_NFS_V4_2
Trond Myklebusta1e7f302021-07-14 13:00:58 -04001398 if (!(server->caps & NFS_CAP_XATTR))
1399 break;
1400 p->o_arg.access |= NFS4_ACCESS_XAREAD | NFS4_ACCESS_XAWRITE |
1401 NFS4_ACCESS_XALIST;
Frank van der Linden72832a22020-06-23 22:38:58 +00001402#endif
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001403 }
David Howells7539bba2006-08-22 20:06:09 -04001404 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001405 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1406 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001407 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001408 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001409 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001410 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001411 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001412 case NFS4_OPEN_CLAIM_NULL:
1413 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1414 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1415 p->o_arg.fh = NFS_FH(dir);
1416 break;
1417 case NFS4_OPEN_CLAIM_PREVIOUS:
1418 case NFS4_OPEN_CLAIM_FH:
1419 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1420 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
David Howells2b0143b2015-03-17 22:25:59 +00001421 p->o_arg.fh = NFS_FH(d_inode(dentry));
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001422 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001423 p->c_arg.fh = &p->o_res.fh;
1424 p->c_arg.stateid = &p->o_res.stateid;
1425 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001426 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001427 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001428 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001429
1430err_free_label:
Kinglong Meea49c2692015-07-27 15:31:38 +08001431 nfs4_label_free(p->a_label);
1432err_free_f:
Anna Schumaker76baa2b2021-10-22 13:11:06 -04001433 nfs4_label_free(p->f_attr.label);
David Quigley14c43f72013-05-22 12:50:43 -04001434err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001435 kfree(p);
1436err:
1437 dput(parent);
1438 return NULL;
1439}
1440
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001441static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001442{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001443 struct nfs4_opendata *p = container_of(kref,
1444 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001445 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001446
Fred Isaman30ae2412016-10-18 13:39:51 -04001447 nfs4_lgopen_release(p->lgp);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001448 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001449 nfs4_sequence_free_slot(&p->o_res.seq_res);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001450 if (p->state != NULL)
1451 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001452 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001453
Kinglong Meea49c2692015-07-27 15:31:38 +08001454 nfs4_label_free(p->a_label);
Anna Schumaker76baa2b2021-10-22 13:11:06 -04001455 nfs4_label_free(p->f_attr.label);
David Quigley14c43f72013-05-22 12:50:43 -04001456
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001457 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001458 dput(p->dentry);
1459 nfs_sb_deactive(sb);
Trond Myklebust6926afd12012-01-07 13:22:46 -05001460 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001461 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001462 kfree(p);
1463}
1464
1465static void nfs4_opendata_put(struct nfs4_opendata *p)
1466{
1467 if (p != NULL)
1468 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001469}
1470
Trond Myklebust24311f82015-09-20 10:50:17 -04001471static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1472 fmode_t fmode)
1473{
1474 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1475 case FMODE_READ|FMODE_WRITE:
1476 return state->n_rdwr != 0;
1477 case FMODE_WRITE:
1478 return state->n_wronly != 0;
1479 case FMODE_READ:
1480 return state->n_rdonly != 0;
1481 }
1482 WARN_ON_ONCE(1);
1483 return false;
1484}
1485
Trond Myklebustbe189f72018-09-27 17:12:33 -04001486static int can_open_cached(struct nfs4_state *state, fmode_t mode,
1487 int open_mode, enum open_claim_type4 claim)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001488{
1489 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001490
Trond Myklebust536e43d2012-01-17 22:04:26 -05001491 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001492 goto out;
Trond Myklebustbe189f72018-09-27 17:12:33 -04001493 switch (claim) {
1494 case NFS4_OPEN_CLAIM_NULL:
1495 case NFS4_OPEN_CLAIM_FH:
1496 goto out;
1497 default:
1498 break;
1499 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001500 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001501 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001502 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1503 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001504 break;
1505 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001506 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1507 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001508 break;
1509 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001510 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1511 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001512 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001513out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001514 return ret;
1515}
1516
Trond Myklebust2a606182015-08-19 22:30:00 -05001517static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1518 enum open_claim_type4 claim)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001519{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001520 if (delegation == NULL)
1521 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001522 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001523 return 0;
Trond Myklebust2a606182015-08-19 22:30:00 -05001524 switch (claim) {
1525 case NFS4_OPEN_CLAIM_NULL:
1526 case NFS4_OPEN_CLAIM_FH:
1527 break;
1528 case NFS4_OPEN_CLAIM_PREVIOUS:
1529 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1530 break;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001531 fallthrough;
Trond Myklebust2a606182015-08-19 22:30:00 -05001532 default:
1533 return 0;
1534 }
Trond Myklebustb7391f42008-12-23 15:21:52 -05001535 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001536 return 1;
1537}
1538
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001539static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001540{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001541 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001542 case FMODE_WRITE:
1543 state->n_wronly++;
1544 break;
1545 case FMODE_READ:
1546 state->n_rdonly++;
1547 break;
1548 case FMODE_READ|FMODE_WRITE:
1549 state->n_rdwr++;
1550 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001551 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001552}
1553
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04001554#ifdef CONFIG_NFS_V4_1
1555static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1556{
1557 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1558 return true;
1559 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1560 return true;
1561 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1562 return true;
1563 return false;
1564}
1565#endif /* CONFIG_NFS_V4_1 */
1566
Trond Myklebustc9399f22017-11-06 15:28:01 -05001567static void nfs_state_log_update_open_stateid(struct nfs4_state *state)
1568{
1569 if (test_and_clear_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1570 wake_up_all(&state->waitq);
1571}
1572
Trond Myklebust4f14c192014-02-12 19:15:06 -05001573static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001574{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001575 struct nfs_client *clp = state->owner->so_server->nfs_client;
1576 bool need_recover = false;
1577
1578 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1579 need_recover = true;
1580 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1581 need_recover = true;
1582 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1583 need_recover = true;
1584 if (need_recover)
1585 nfs4_state_mark_reclaim_nograce(clp, state);
1586}
1587
Trond Myklebustc9399f22017-11-06 15:28:01 -05001588/*
1589 * Check for whether or not the caller may update the open stateid
1590 * to the value passed in by stateid.
1591 *
1592 * Note: This function relies heavily on the server implementing
1593 * RFC7530 Section 9.1.4.2, and RFC5661 Section 8.2.2
1594 * correctly.
1595 * i.e. The stateid seqids have to be initialised to 1, and
1596 * are then incremented on every state transition.
1597 */
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04001598static bool nfs_stateid_is_sequential(struct nfs4_state *state,
Trond Myklebustc9399f22017-11-06 15:28:01 -05001599 const nfs4_stateid *stateid)
Trond Myklebuste999e802014-02-10 18:20:47 -05001600{
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04001601 if (test_bit(NFS_OPEN_STATE, &state->flags)) {
1602 /* The common case - we're updating to a new sequence number */
Trond Myklebust01d29f82021-10-26 21:56:40 -04001603 if (nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1604 if (nfs4_stateid_is_next(&state->open_stateid, stateid))
1605 return true;
1606 return false;
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04001607 }
Trond Myklebust01d29f82021-10-26 21:56:40 -04001608 /* The server returned a new stateid */
Trond Myklebustc9399f22017-11-06 15:28:01 -05001609 }
Trond Myklebust01d29f82021-10-26 21:56:40 -04001610 /* This is the first OPEN in this generation */
1611 if (stateid->seqid == cpu_to_be32(1))
1612 return true;
Trond Myklebuste999e802014-02-10 18:20:47 -05001613 return false;
1614}
1615
Trond Myklebustf95549c2015-01-23 18:06:09 -05001616static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1617{
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001618 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1619 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001620 if (state->n_wronly)
1621 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1622 if (state->n_rdonly)
1623 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1624 if (state->n_rdwr)
1625 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001626 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustf95549c2015-01-23 18:06:09 -05001627}
1628
Trond Myklebust226056c2014-02-11 10:41:07 -05001629static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1630 nfs4_stateid *stateid, fmode_t fmode)
1631{
1632 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1633 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1634 case FMODE_WRITE:
1635 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1636 break;
1637 case FMODE_READ:
1638 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1639 break;
1640 case 0:
1641 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1642 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1643 clear_bit(NFS_OPEN_STATE, &state->flags);
1644 }
1645 if (stateid == NULL)
1646 return;
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001647 /* Handle OPEN+OPEN_DOWNGRADE races */
1648 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1649 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
Trond Myklebustf95549c2015-01-23 18:06:09 -05001650 nfs_resync_open_stateid_locked(state);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001651 goto out;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001652 }
Trond Myklebust003707c2007-07-05 18:07:55 -04001653 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001654 nfs4_stateid_copy(&state->stateid, stateid);
1655 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001656 trace_nfs4_open_stateid_update(state->inode, stateid, 0);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001657out:
1658 nfs_state_log_update_open_stateid(state);
Trond Myklebust226056c2014-02-11 10:41:07 -05001659}
1660
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001661static void nfs_clear_open_stateid(struct nfs4_state *state,
1662 nfs4_stateid *arg_stateid,
1663 nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust226056c2014-02-11 10:41:07 -05001664{
1665 write_seqlock(&state->seqlock);
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001666 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1667 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1668 nfs_clear_open_stateid_locked(state, stateid, fmode);
Trond Myklebust226056c2014-02-11 10:41:07 -05001669 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001670 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1671 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001672}
1673
Trond Myklebust1393d962016-09-22 13:39:13 -04001674static void nfs_set_open_stateid_locked(struct nfs4_state *state,
Trond Myklebustc9399f22017-11-06 15:28:01 -05001675 const nfs4_stateid *stateid, nfs4_stateid *freeme)
Trond Myklebuste9acf212019-01-22 14:01:16 -05001676 __must_hold(&state->owner->so_lock)
1677 __must_hold(&state->seqlock)
1678 __must_hold(RCU)
1679
Trond Myklebust003707c2007-07-05 18:07:55 -04001680{
Trond Myklebustc9399f22017-11-06 15:28:01 -05001681 DEFINE_WAIT(wait);
1682 int status = 0;
1683 for (;;) {
1684
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04001685 if (nfs_stateid_is_sequential(state, stateid))
Trond Myklebust003707c2007-07-05 18:07:55 -04001686 break;
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04001687
Trond Myklebustc9399f22017-11-06 15:28:01 -05001688 if (status)
Trond Myklebust003707c2007-07-05 18:07:55 -04001689 break;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001690 /* Rely on seqids for serialisation with NFSv4.0 */
1691 if (!nfs4_has_session(NFS_SERVER(state->inode)->nfs_client))
1692 break;
1693
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04001694 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001695 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
1696 /*
1697 * Ensure we process the state changes in the same order
1698 * in which the server processed them by delaying the
1699 * update of the stateid until we are in sequence.
1700 */
1701 write_sequnlock(&state->seqlock);
1702 spin_unlock(&state->owner->so_lock);
1703 rcu_read_unlock();
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001704 trace_nfs4_open_stateid_update_wait(state->inode, stateid, 0);
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04001705
zhouchuangaobb002382021-05-09 19:34:37 -07001706 if (!fatal_signal_pending(current)) {
Trond Myklebustc9399f22017-11-06 15:28:01 -05001707 if (schedule_timeout(5*HZ) == 0)
1708 status = -EAGAIN;
1709 else
1710 status = 0;
1711 } else
1712 status = -EINTR;
1713 finish_wait(&state->waitq, &wait);
1714 rcu_read_lock();
1715 spin_lock(&state->owner->so_lock);
1716 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001717 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001718
Trond Myklebuste1fff5d2017-11-07 13:10:46 -05001719 if (test_bit(NFS_OPEN_STATE, &state->flags) &&
1720 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
Trond Myklebustc9399f22017-11-06 15:28:01 -05001721 nfs4_stateid_copy(freeme, &state->open_stateid);
1722 nfs_test_and_clear_all_open_stateid(state);
1723 }
1724
Trond Myklebuste999e802014-02-10 18:20:47 -05001725 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1726 nfs4_stateid_copy(&state->stateid, stateid);
1727 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001728 trace_nfs4_open_stateid_update(state->inode, stateid, status);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001729 nfs_state_log_update_open_stateid(state);
Trond Myklebust003707c2007-07-05 18:07:55 -04001730}
1731
Trond Myklebustc9399f22017-11-06 15:28:01 -05001732static void nfs_state_set_open_stateid(struct nfs4_state *state,
Trond Myklebust1393d962016-09-22 13:39:13 -04001733 const nfs4_stateid *open_stateid,
Trond Myklebust1393d962016-09-22 13:39:13 -04001734 fmode_t fmode,
1735 nfs4_stateid *freeme)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001737 /*
1738 * Protect the call to nfs4_state_set_mode_locked and
1739 * serialise the stateid update
1740 */
1741 write_seqlock(&state->seqlock);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001742 nfs_set_open_stateid_locked(state, open_stateid, freeme);
1743 switch (fmode) {
1744 case FMODE_READ:
1745 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1746 break;
1747 case FMODE_WRITE:
1748 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1749 break;
1750 case FMODE_READ|FMODE_WRITE:
1751 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust003707c2007-07-05 18:07:55 -04001752 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001753 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001754 write_sequnlock(&state->seqlock);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001755}
1756
Trond Myklebust27a30cf2019-07-22 18:32:59 +01001757static void nfs_state_clear_open_state_flags(struct nfs4_state *state)
1758{
1759 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1760 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1761 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1762 clear_bit(NFS_OPEN_STATE, &state->flags);
1763}
1764
Trond Myklebustc9399f22017-11-06 15:28:01 -05001765static void nfs_state_set_delegation(struct nfs4_state *state,
1766 const nfs4_stateid *deleg_stateid,
1767 fmode_t fmode)
1768{
1769 /*
1770 * Protect the call to nfs4_state_set_mode_locked and
1771 * serialise the stateid update
1772 */
1773 write_seqlock(&state->seqlock);
1774 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1775 set_bit(NFS_DELEGATED_STATE, &state->flags);
1776 write_sequnlock(&state->seqlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777}
1778
Trond Myklebust9f0c5122018-09-05 14:07:15 -04001779static void nfs_state_clear_delegation(struct nfs4_state *state)
1780{
1781 write_seqlock(&state->seqlock);
1782 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1783 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1784 write_sequnlock(&state->seqlock);
1785}
1786
Olga Kornievskaiaec4b0922019-10-08 16:33:53 -04001787int update_open_stateid(struct nfs4_state *state,
Trond Myklebust1393d962016-09-22 13:39:13 -04001788 const nfs4_stateid *open_stateid,
1789 const nfs4_stateid *delegation,
1790 fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001791{
Trond Myklebust1393d962016-09-22 13:39:13 -04001792 struct nfs_server *server = NFS_SERVER(state->inode);
1793 struct nfs_client *clp = server->nfs_client;
Trond Myklebust34310432008-12-23 15:21:38 -05001794 struct nfs_inode *nfsi = NFS_I(state->inode);
1795 struct nfs_delegation *deleg_cur;
Arnd Bergmann83aa3e02016-10-18 17:21:30 +02001796 nfs4_stateid freeme = { };
Trond Myklebust34310432008-12-23 15:21:38 -05001797 int ret = 0;
1798
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001799 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001800
1801 rcu_read_lock();
Trond Myklebustc9399f22017-11-06 15:28:01 -05001802 spin_lock(&state->owner->so_lock);
1803 if (open_stateid != NULL) {
1804 nfs_state_set_open_stateid(state, open_stateid, fmode, &freeme);
1805 ret = 1;
1806 }
1807
Trond Myklebust333ac782019-10-22 12:12:17 -04001808 deleg_cur = nfs4_get_valid_delegation(state->inode);
Trond Myklebust34310432008-12-23 15:21:38 -05001809 if (deleg_cur == NULL)
1810 goto no_delegation;
1811
1812 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001813 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001814 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001815 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001816 goto no_delegation_unlock;
1817
1818 if (delegation == NULL)
1819 delegation = &deleg_cur->stateid;
Trond Myklebust333ac782019-10-22 12:12:17 -04001820 else if (!nfs4_stateid_match_other(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001821 goto no_delegation_unlock;
1822
Trond Myklebustb7391f42008-12-23 15:21:52 -05001823 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001824 nfs_state_set_delegation(state, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001825 ret = 1;
1826no_delegation_unlock:
1827 spin_unlock(&deleg_cur->lock);
1828no_delegation:
Trond Myklebustc9399f22017-11-06 15:28:01 -05001829 if (ret)
1830 update_open_stateflags(state, fmode);
1831 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001832 rcu_read_unlock();
1833
Trond Myklebust4f14c192014-02-12 19:15:06 -05001834 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
Trond Myklebust1393d962016-09-22 13:39:13 -04001835 nfs4_schedule_state_manager(clp);
1836 if (freeme.type != 0)
1837 nfs4_test_and_free_stateid(server, &freeme,
1838 state->owner->so_cred);
Trond Myklebust34310432008-12-23 15:21:38 -05001839
1840 return ret;
1841}
1842
Trond Myklebust39071e62015-01-24 15:07:56 -05001843static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1844 const nfs4_stateid *stateid)
1845{
1846 struct nfs4_state *state = lsp->ls_state;
1847 bool ret = false;
1848
1849 spin_lock(&state->state_lock);
1850 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1851 goto out_noupdate;
1852 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1853 goto out_noupdate;
1854 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1855 ret = true;
1856out_noupdate:
1857 spin_unlock(&state->state_lock);
1858 return ret;
1859}
Trond Myklebust34310432008-12-23 15:21:38 -05001860
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001861static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001862{
1863 struct nfs_delegation *delegation;
1864
Trond Myklebustf5086242018-03-20 16:43:13 -04001865 fmode &= FMODE_READ|FMODE_WRITE;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001866 rcu_read_lock();
Trond Myklebust40e6aa12019-10-27 13:38:45 -04001867 delegation = nfs4_get_valid_delegation(inode);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001868 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001869 rcu_read_unlock();
1870 return;
1871 }
1872 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001873 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001874}
1875
Trond Myklebust6ee41262007-07-08 14:11:36 -04001876static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001877{
1878 struct nfs4_state *state = opendata->state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001879 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001880 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001881 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebust2a606182015-08-19 22:30:00 -05001882 enum open_claim_type4 claim = opendata->o_arg.claim;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001883 nfs4_stateid stateid;
1884 int ret = -EAGAIN;
1885
Trond Myklebustaac00a82007-07-05 19:02:21 -04001886 for (;;) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001887 spin_lock(&state->owner->so_lock);
Trond Myklebustbe189f72018-09-27 17:12:33 -04001888 if (can_open_cached(state, fmode, open_mode, claim)) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001889 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001890 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef72014-09-03 14:15:40 -04001891 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001892 }
Anna Schumaker61beef72014-09-03 14:15:40 -04001893 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001894 rcu_read_lock();
Trond Myklebustbe3df3d2019-10-31 18:40:32 -04001895 delegation = nfs4_get_valid_delegation(state->inode);
Trond Myklebust2a606182015-08-19 22:30:00 -05001896 if (!can_open_delegated(delegation, fmode, claim)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001897 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001898 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001899 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001900 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001901 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001902 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001903 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001904 if (!opendata->is_recover) {
1905 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1906 if (ret != 0)
1907 goto out;
1908 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001909 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001910
1911 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001912 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001913 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001914 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001915out:
1916 return ERR_PTR(ret);
1917out_return_state:
Trond Myklebustace9fad2018-09-02 19:19:07 -04001918 refcount_inc(&state->count);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001919 return state;
1920}
1921
Andy Adamsone23008e2012-10-02 21:07:32 -04001922static void
1923nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1924{
1925 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1926 struct nfs_delegation *delegation;
1927 int delegation_flags = 0;
1928
1929 rcu_read_lock();
1930 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1931 if (delegation)
1932 delegation_flags = delegation->flags;
1933 rcu_read_unlock();
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001934 switch (data->o_arg.claim) {
1935 default:
1936 break;
1937 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1938 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001939 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1940 "returning a delegation for "
1941 "OPEN(CLAIM_DELEGATE_CUR)\n",
1942 clp->cl_hostname);
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001943 return;
1944 }
1945 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Andy Adamsone23008e2012-10-02 21:07:32 -04001946 nfs_inode_set_delegation(state->inode,
Trond Myklebust35156bf2018-03-20 17:03:13 -04001947 data->owner->so_cred,
1948 data->o_res.delegation_type,
1949 &data->o_res.delegation,
1950 data->o_res.pagemod_limit);
Andy Adamsone23008e2012-10-02 21:07:32 -04001951 else
1952 nfs_inode_reclaim_delegation(state->inode,
Trond Myklebust35156bf2018-03-20 17:03:13 -04001953 data->owner->so_cred,
1954 data->o_res.delegation_type,
1955 &data->o_res.delegation,
1956 data->o_res.pagemod_limit);
Jeff Layton8b199e52018-07-05 05:48:14 -04001957
1958 if (data->o_res.do_recall)
1959 nfs_async_inode_return_delegation(state->inode,
1960 &data->o_res.delegation);
Andy Adamsone23008e2012-10-02 21:07:32 -04001961}
1962
1963/*
1964 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1965 * and update the nfs4_state.
1966 */
1967static struct nfs4_state *
1968_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1969{
1970 struct inode *inode = data->state->inode;
1971 struct nfs4_state *state = data->state;
1972 int ret;
1973
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001974 if (!data->rpc_done) {
Anna Schumaker37a84842017-01-11 16:08:35 -05001975 if (data->rpc_status)
1976 return ERR_PTR(data->rpc_status);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001977 /* cached opens have already been processed */
1978 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001979 }
1980
Andy Adamsone23008e2012-10-02 21:07:32 -04001981 ret = nfs_refresh_inode(inode, &data->f_attr);
1982 if (ret)
Anna Schumaker37a84842017-01-11 16:08:35 -05001983 return ERR_PTR(ret);
Andy Adamsone23008e2012-10-02 21:07:32 -04001984
1985 if (data->o_res.delegation_type != 0)
1986 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001987update:
Trond Myklebuste3c8dc72019-07-29 18:25:00 +01001988 if (!update_open_stateid(state, &data->o_res.stateid,
1989 NULL, data->o_arg.fmode))
1990 return ERR_PTR(-EAGAIN);
Trond Myklebustace9fad2018-09-02 19:19:07 -04001991 refcount_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001992
1993 return state;
Andy Adamsone23008e2012-10-02 21:07:32 -04001994}
1995
Trond Myklebust4e2fcac2017-08-08 09:06:18 -04001996static struct inode *
1997nfs4_opendata_get_inode(struct nfs4_opendata *data)
1998{
1999 struct inode *inode;
2000
2001 switch (data->o_arg.claim) {
2002 case NFS4_OPEN_CLAIM_NULL:
2003 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
2004 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
2005 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
2006 return ERR_PTR(-EAGAIN);
2007 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh,
Anna Schumakercf7ab002021-10-22 13:11:11 -04002008 &data->f_attr);
Trond Myklebust4e2fcac2017-08-08 09:06:18 -04002009 break;
2010 default:
2011 inode = d_inode(data->dentry);
2012 ihold(inode);
2013 nfs_refresh_inode(inode, &data->f_attr);
2014 }
2015 return inode;
2016}
2017
Andy Adamsone23008e2012-10-02 21:07:32 -04002018static struct nfs4_state *
Trond Myklebust75e8c482017-08-08 10:38:07 -04002019nfs4_opendata_find_nfs4_state(struct nfs4_opendata *data)
2020{
2021 struct nfs4_state *state;
2022 struct inode *inode;
2023
2024 inode = nfs4_opendata_get_inode(data);
2025 if (IS_ERR(inode))
2026 return ERR_CAST(inode);
2027 if (data->state != NULL && data->state->inode == inode) {
2028 state = data->state;
Trond Myklebustace9fad2018-09-02 19:19:07 -04002029 refcount_inc(&state->count);
Trond Myklebust75e8c482017-08-08 10:38:07 -04002030 } else
2031 state = nfs4_get_open_state(inode, data->owner);
2032 iput(inode);
2033 if (state == NULL)
2034 state = ERR_PTR(-ENOMEM);
2035 return state;
2036}
2037
Andy Adamsone23008e2012-10-02 21:07:32 -04002038static struct nfs4_state *
2039_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002040{
Trond Myklebust75e8c482017-08-08 10:38:07 -04002041 struct nfs4_state *state;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002042
Trond Myklebustaac00a82007-07-05 19:02:21 -04002043 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04002044 state = nfs4_try_open_cached(data);
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002045 trace_nfs4_cached_open(data->state);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002046 goto out;
2047 }
2048
Trond Myklebust75e8c482017-08-08 10:38:07 -04002049 state = nfs4_opendata_find_nfs4_state(data);
2050 if (IS_ERR(state))
2051 goto out;
2052
Andy Adamsone23008e2012-10-02 21:07:32 -04002053 if (data->o_res.delegation_type != 0)
2054 nfs4_opendata_check_deleg(data, state);
Trond Myklebuste3c8dc72019-07-29 18:25:00 +01002055 if (!update_open_stateid(state, &data->o_res.stateid,
2056 NULL, data->o_arg.fmode)) {
2057 nfs4_put_open_state(state);
2058 state = ERR_PTR(-EAGAIN);
2059 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002060out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08002061 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002062 return state;
2063}
2064
Andy Adamsone23008e2012-10-02 21:07:32 -04002065static struct nfs4_state *
2066nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
2067{
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002068 struct nfs4_state *ret;
2069
Andy Adamsone23008e2012-10-02 21:07:32 -04002070 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002071 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
2072 else
2073 ret = _nfs4_opendata_to_nfs4_state(data);
2074 nfs4_sequence_free_slot(&data->o_res.seq_res);
2075 return ret;
Andy Adamsone23008e2012-10-02 21:07:32 -04002076}
2077
Trond Myklebust0de43972018-09-02 15:57:01 -04002078static struct nfs_open_context *
2079nfs4_state_find_open_context_mode(struct nfs4_state *state, fmode_t mode)
Trond Myklebust864472e2006-01-03 09:55:15 +01002080{
2081 struct nfs_inode *nfsi = NFS_I(state->inode);
2082 struct nfs_open_context *ctx;
2083
Trond Myklebust0de43972018-09-02 15:57:01 -04002084 rcu_read_lock();
2085 list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
Trond Myklebust864472e2006-01-03 09:55:15 +01002086 if (ctx->state != state)
2087 continue;
Trond Myklebust0de43972018-09-02 15:57:01 -04002088 if ((ctx->mode & mode) != mode)
2089 continue;
2090 if (!get_nfs_open_context(ctx))
2091 continue;
2092 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01002093 return ctx;
2094 }
Trond Myklebust0de43972018-09-02 15:57:01 -04002095 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01002096 return ERR_PTR(-ENOENT);
2097}
2098
Trond Myklebust0de43972018-09-02 15:57:01 -04002099static struct nfs_open_context *
2100nfs4_state_find_open_context(struct nfs4_state *state)
2101{
2102 struct nfs_open_context *ctx;
2103
2104 ctx = nfs4_state_find_open_context_mode(state, FMODE_READ|FMODE_WRITE);
2105 if (!IS_ERR(ctx))
2106 return ctx;
2107 ctx = nfs4_state_find_open_context_mode(state, FMODE_WRITE);
2108 if (!IS_ERR(ctx))
2109 return ctx;
2110 return nfs4_state_find_open_context_mode(state, FMODE_READ);
2111}
2112
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002113static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
2114 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002115{
2116 struct nfs4_opendata *opendata;
2117
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002118 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002119 NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002120 if (opendata == NULL)
2121 return ERR_PTR(-ENOMEM);
2122 opendata->state = state;
Trond Myklebustace9fad2018-09-02 19:19:07 -04002123 refcount_inc(&state->count);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002124 return opendata;
2125}
2126
Trond Myklebust24311f82015-09-20 10:50:17 -04002127static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
2128 fmode_t fmode)
Trond Myklebust864472e2006-01-03 09:55:15 +01002129{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002130 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01002131 int ret;
2132
Trond Myklebust24311f82015-09-20 10:50:17 -04002133 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
NeilBrown39f897f2015-06-29 14:28:54 +10002134 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002135 opendata->o_arg.open_flags = 0;
2136 opendata->o_arg.fmode = fmode;
Trond Myklebustbe36e182015-02-27 17:04:17 -05002137 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
2138 NFS_SB(opendata->dentry->d_sb),
2139 fmode, 0);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002140 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
2141 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
2142 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002143 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002144 if (ret != 0)
2145 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002146 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002147 if (IS_ERR(newstate))
2148 return PTR_ERR(newstate);
Trond Myklebust24311f82015-09-20 10:50:17 -04002149 if (newstate != opendata->state)
2150 ret = -ESTALE;
Al Viro643168c2011-06-22 18:20:23 -04002151 nfs4_close_state(newstate, fmode);
Trond Myklebust24311f82015-09-20 10:50:17 -04002152 return ret;
Trond Myklebust864472e2006-01-03 09:55:15 +01002153}
2154
2155static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
2156{
Trond Myklebust864472e2006-01-03 09:55:15 +01002157 int ret;
2158
2159 /* memory barrier prior to reading state->n_* */
2160 smp_rmb();
Trond Myklebust24311f82015-09-20 10:50:17 -04002161 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2162 if (ret != 0)
2163 return ret;
2164 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2165 if (ret != 0)
2166 return ret;
2167 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
2168 if (ret != 0)
2169 return ret;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002170 /*
2171 * We may have performed cached opens for all three recoveries.
2172 * Check if we need to update the current stateid.
2173 */
2174 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05002175 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04002176 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002177 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05002178 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04002179 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002180 }
Trond Myklebust864472e2006-01-03 09:55:15 +01002181 return 0;
2182}
2183
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184/*
2185 * OPEN_RECLAIM:
2186 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002188static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002190 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01002191 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002192 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 int status;
2194
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002195 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2196 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002197 if (IS_ERR(opendata))
2198 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002199 rcu_read_lock();
2200 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05002201 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04002202 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002203 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01002204 opendata->o_arg.u.delegation_type = delegation_type;
2205 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002206 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 return status;
2208}
2209
Trond Myklebust539cd032007-06-05 11:46:42 -04002210static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211{
2212 struct nfs_server *server = NFS_SERVER(state->inode);
2213 struct nfs4_exception exception = { };
2214 int err;
2215 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002216 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002217 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002218 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2219 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04002220 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002221 break;
2222 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 } while (exception.retry);
2224 return err;
2225}
2226
Trond Myklebust864472e2006-01-03 09:55:15 +01002227static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
2228{
2229 struct nfs_open_context *ctx;
2230 int ret;
2231
2232 ctx = nfs4_state_find_open_context(state);
2233 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002234 return -EAGAIN;
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002235 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2236 nfs_state_clear_open_state_flags(state);
Trond Myklebust539cd032007-06-05 11:46:42 -04002237 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002238 put_nfs_open_context(ctx);
2239 return ret;
2240}
2241
NeilBrowndce26302017-12-13 09:57:09 +11002242static int nfs4_handle_delegation_recall_error(struct nfs_server *server, struct nfs4_state *state, const nfs4_stateid *stateid, struct file_lock *fl, int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002244 switch (err) {
2245 default:
2246 printk(KERN_ERR "NFS: %s: unhandled error "
2247 "%d.\n", __func__, err);
Gustavo A. R. Silvaffb81712020-11-20 12:26:46 -06002248 fallthrough;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002249 case 0:
2250 case -ENOENT:
Trond Myklebust8eee52a2015-06-04 13:51:13 -04002251 case -EAGAIN:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002252 case -ESTALE:
Trond Myklebust67e7b522019-08-07 07:31:27 -04002253 case -ETIMEDOUT:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002254 break;
2255 case -NFS4ERR_BADSESSION:
2256 case -NFS4ERR_BADSLOT:
2257 case -NFS4ERR_BAD_HIGH_SLOT:
2258 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
2259 case -NFS4ERR_DEADSESSION:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002260 return -EAGAIN;
2261 case -NFS4ERR_STALE_CLIENTID:
2262 case -NFS4ERR_STALE_STATEID:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002263 /* Don't recall a delegation if it was lost */
2264 nfs4_schedule_lease_recovery(server->nfs_client);
2265 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04002266 case -NFS4ERR_MOVED:
2267 nfs4_schedule_migration_recovery(server);
2268 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04002269 case -NFS4ERR_LEASE_MOVED:
2270 nfs4_schedule_lease_moved_recovery(server->nfs_client);
2271 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002272 case -NFS4ERR_DELEG_REVOKED:
2273 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust404ea3562016-09-22 13:39:08 -04002274 case -NFS4ERR_EXPIRED:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002275 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002276 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002277 nfs_inode_find_state_and_recover(state->inode,
2278 stateid);
2279 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05002280 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002281 case -NFS4ERR_DELAY:
2282 case -NFS4ERR_GRACE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002283 ssleep(1);
2284 return -EAGAIN;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002285 case -ENOMEM:
2286 case -NFS4ERR_DENIED:
NeilBrowndce26302017-12-13 09:57:09 +11002287 if (fl) {
2288 struct nfs4_lock_state *lsp = fl->fl_u.nfs4_fl.owner;
2289 if (lsp)
2290 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2291 }
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002292 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002293 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 return err;
2295}
2296
Trond Myklebust24311f82015-09-20 10:50:17 -04002297int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002298 struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002299{
2300 struct nfs_server *server = NFS_SERVER(state->inode);
2301 struct nfs4_opendata *opendata;
Trond Myklebust24311f82015-09-20 10:50:17 -04002302 int err = 0;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002303
2304 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2305 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2306 if (IS_ERR(opendata))
2307 return PTR_ERR(opendata);
2308 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002309 if (!test_bit(NFS_O_RDWR_STATE, &state->flags)) {
Trond Myklebust24311f82015-09-20 10:50:17 -04002310 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2311 if (err)
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002312 goto out;
2313 }
2314 if (!test_bit(NFS_O_WRONLY_STATE, &state->flags)) {
Trond Myklebust24311f82015-09-20 10:50:17 -04002315 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2316 if (err)
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002317 goto out;
Trond Myklebust24311f82015-09-20 10:50:17 -04002318 }
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002319 if (!test_bit(NFS_O_RDONLY_STATE, &state->flags)) {
2320 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2321 if (err)
2322 goto out;
2323 }
2324 nfs_state_clear_delegation(state);
2325out:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002326 nfs4_opendata_put(opendata);
NeilBrowndce26302017-12-13 09:57:09 +11002327 return nfs4_handle_delegation_recall_error(server, state, stateid, NULL, err);
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002328}
2329
Chuck Leverbe05c862013-08-09 12:49:47 -04002330static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2331{
2332 struct nfs4_opendata *data = calldata;
2333
Anna Schumaker7981c8a2017-01-10 11:39:53 -05002334 nfs4_setup_sequence(data->o_arg.server->nfs_client,
2335 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04002336}
2337
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002338static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2339{
2340 struct nfs4_opendata *data = calldata;
2341
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002342 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04002343
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002344 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002345 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05002346 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05002347 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002348 renew_lease(data->o_res.server, data->timestamp);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002349 data->rpc_done = true;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002350 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002351}
2352
2353static void nfs4_open_confirm_release(void *calldata)
2354{
2355 struct nfs4_opendata *data = calldata;
2356 struct nfs4_state *state = NULL;
2357
2358 /* If this request hasn't been cancelled, do nothing */
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002359 if (!data->cancelled)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002360 goto out_free;
2361 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002362 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002363 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002364 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002365 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002366 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002367out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002368 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002369}
2370
2371static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04002372 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002373 .rpc_call_done = nfs4_open_confirm_done,
2374 .rpc_release = nfs4_open_confirm_release,
2375};
2376
2377/*
2378 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2379 */
2380static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2381{
David Howells2b0143b2015-03-17 22:25:59 +00002382 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002383 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002384 struct rpc_message msg = {
2385 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2386 .rpc_argp = &data->c_arg,
2387 .rpc_resp = &data->c_res,
2388 .rpc_cred = data->owner->so_cred,
2389 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002390 struct rpc_task_setup task_setup_data = {
2391 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002392 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002393 .callback_ops = &nfs4_open_confirm_ops,
2394 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002395 .workqueue = nfsiod_workqueue,
Trond Myklebust61296502020-02-07 19:38:12 -05002396 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002397 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 int status;
2399
Anna Schumakerfba83f32018-05-04 16:22:50 -04002400 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1,
2401 data->is_recover);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002402 kref_get(&data->kref);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002403 data->rpc_done = false;
Trond Myklebust3e309912007-07-07 13:19:59 -04002404 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002405 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002406 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05002407 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002408 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002409 status = rpc_wait_for_completion_task(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002410 if (status != 0) {
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002411 data->cancelled = true;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002412 smp_wmb();
2413 } else
2414 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05002415 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 return status;
2417}
2418
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002419static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002421 struct nfs4_opendata *data = calldata;
2422 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04002423 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust2a606182015-08-19 22:30:00 -05002424 enum open_claim_type4 claim = data->o_arg.claim;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002425
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002426 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002427 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002428 /*
2429 * Check if we still need to send an OPEN call, or if we can use
2430 * a delegation instead.
2431 */
2432 if (data->state != NULL) {
2433 struct nfs_delegation *delegation;
2434
Trond Myklebustbe189f72018-09-27 17:12:33 -04002435 if (can_open_cached(data->state, data->o_arg.fmode,
2436 data->o_arg.open_flags, claim))
Trond Myklebust6ee41262007-07-08 14:11:36 -04002437 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002438 rcu_read_lock();
Trond Myklebustbe3df3d2019-10-31 18:40:32 -04002439 delegation = nfs4_get_valid_delegation(data->state->inode);
Trond Myklebust2a606182015-08-19 22:30:00 -05002440 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
Trond Myklebust652f89f2011-12-09 19:05:58 -05002441 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002442 rcu_read_unlock();
2443 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04002444 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04002445 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust2a606182015-08-19 22:30:00 -05002446 switch (claim) {
2447 default:
2448 break;
Trond Myklebust8188df12013-04-23 14:31:19 -04002449 case NFS4_OPEN_CLAIM_PREVIOUS:
2450 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2451 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04002452 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002453 fallthrough;
Trond Myklebust8188df12013-04-23 14:31:19 -04002454 case NFS4_OPEN_CLAIM_FH:
2455 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002456 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002457 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05002458 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
Andy Adamsond8985282009-04-01 09:22:21 -04002459 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002460 &data->o_res.seq_res,
2461 task) != 0)
2462 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04002463
2464 /* Set the create mode (note dependency on the session type) */
2465 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2466 if (data->o_arg.open_flags & O_EXCL) {
Trond Myklebusta1e7f302021-07-14 13:00:58 -04002467 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2468 if (clp->cl_mvops->minor_version == 0) {
2469 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2470 /* don't put an ACCESS op in OPEN compound if O_EXCL,
2471 * because ACCESS will return permission denied for
2472 * all bits until close */
2473 data->o_res.access_request = data->o_arg.access = 0;
2474 } else if (nfs4_has_persistent_session(clp))
Trond Myklebust549b19c2013-04-16 18:42:34 -04002475 data->o_arg.createmode = NFS4_CREATE_GUARDED;
Trond Myklebust549b19c2013-04-16 18:42:34 -04002476 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04002477 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05002478unlock_no_action:
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002479 trace_nfs4_cached_open(data->state);
Trond Myklebust652f89f2011-12-09 19:05:58 -05002480 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04002481out_no_action:
2482 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002483out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002484 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002485}
2486
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002487static void nfs4_open_done(struct rpc_task *task, void *calldata)
2488{
2489 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002491 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04002492
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002493 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04002494 return;
Andy Adamsond8985282009-04-01 09:22:21 -04002495
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002496 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07002497 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2498 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07002499 case S_IFREG:
2500 break;
2501 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002502 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002503 break;
2504 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002505 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002506 break;
2507 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002508 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07002509 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07002510 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002511 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04002512 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2513 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07002514 }
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002515 data->rpc_done = true;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002516}
Trond Myklebust6f926b52005-10-18 14:20:18 -07002517
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002518static void nfs4_open_release(void *calldata)
2519{
2520 struct nfs4_opendata *data = calldata;
2521 struct nfs4_state *state = NULL;
2522
2523 /* If this request hasn't been cancelled, do nothing */
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002524 if (!data->cancelled)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002525 goto out_free;
2526 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002527 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002528 goto out_free;
2529 /* In case we need an open_confirm, no cleanup! */
2530 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2531 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002532 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002533 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002534 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002535out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002536 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002537}
2538
2539static const struct rpc_call_ops nfs4_open_ops = {
2540 .rpc_call_prepare = nfs4_open_prepare,
2541 .rpc_call_done = nfs4_open_done,
2542 .rpc_release = nfs4_open_release,
2543};
2544
Fred Isaman3b65a302016-09-19 10:06:49 -04002545static int nfs4_run_open_task(struct nfs4_opendata *data,
2546 struct nfs_open_context *ctx)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002547{
David Howells2b0143b2015-03-17 22:25:59 +00002548 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002549 struct nfs_server *server = NFS_SERVER(dir);
2550 struct nfs_openargs *o_arg = &data->o_arg;
2551 struct nfs_openres *o_res = &data->o_res;
2552 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002553 struct rpc_message msg = {
2554 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2555 .rpc_argp = o_arg,
2556 .rpc_resp = o_res,
2557 .rpc_cred = data->owner->so_cred,
2558 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002559 struct rpc_task_setup task_setup_data = {
2560 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002561 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002562 .callback_ops = &nfs4_open_ops,
2563 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002564 .workqueue = nfsiod_workqueue,
Trond Myklebust61296502020-02-07 19:38:12 -05002565 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002566 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002567 int status;
2568
Olga Kornievskaia85e39fe2021-06-23 23:28:51 -04002569 if (server->nfs_client->cl_minorversion)
2570 task_setup_data.flags |= RPC_TASK_MOVEABLE;
2571
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002572 kref_get(&data->kref);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002573 data->rpc_done = false;
Trond Myklebust3e309912007-07-07 13:19:59 -04002574 data->rpc_status = 0;
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002575 data->cancelled = false;
2576 data->is_recover = false;
Fred Isaman3b65a302016-09-19 10:06:49 -04002577 if (!ctx) {
2578 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 1);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002579 data->is_recover = true;
Trond Myklebust67e7b522019-08-07 07:31:27 -04002580 task_setup_data.flags |= RPC_TASK_TIMEOUT;
Fred Isaman2409a972016-10-06 12:11:21 -04002581 } else {
Fred Isaman3b65a302016-09-19 10:06:49 -04002582 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 0);
Fred Isaman2409a972016-10-06 12:11:21 -04002583 pnfs_lgopen_prepare(data, ctx);
2584 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04002585 task = rpc_run_task(&task_setup_data);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002586 if (IS_ERR(task))
2587 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002588 status = rpc_wait_for_completion_task(task);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002589 if (status != 0) {
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002590 data->cancelled = true;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002591 smp_wmb();
2592 } else
2593 status = data->rpc_status;
2594 rpc_put_task(task);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002595
2596 return status;
2597}
2598
2599static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2600{
David Howells2b0143b2015-03-17 22:25:59 +00002601 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002602 struct nfs_openres *o_res = &data->o_res;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002603 int status;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002604
Fred Isaman3b65a302016-09-19 10:06:49 -04002605 status = nfs4_run_open_task(data, NULL);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002606 if (status != 0 || !data->rpc_done)
2607 return status;
2608
Trond Myklebust6926afd12012-01-07 13:22:46 -05002609 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2610
Anna Schumakerd7e98252017-01-11 16:13:29 -05002611 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002612 status = _nfs4_proc_open_confirm(data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002613
2614 return status;
2615}
2616
Trond Myklebustf3792d62014-07-10 08:54:32 -04002617/*
2618 * Additional permission checks in order to distinguish between an
2619 * open for read, and an open for execute. This works around the
2620 * fact that NFSv4 OPEN treats read and execute permissions as being
2621 * the same.
2622 * Note that in the non-execute case, we want to turn off permission
2623 * checking if we just created a new file (POSIX open() semantics).
2624 */
NeilBrowna52458b2018-12-03 11:30:31 +11002625static int nfs4_opendata_access(const struct cred *cred,
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002626 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002627 struct nfs4_state *state, fmode_t fmode,
2628 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002629{
2630 struct nfs_access_entry cache;
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002631 u32 mask, flags;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002632
2633 /* access call failed or for some reason the server doesn't
2634 * support any access modes -- defer access call until later */
2635 if (opendata->o_res.access_supported == 0)
2636 return 0;
2637
2638 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002639 /*
2640 * Use openflags to check for exec, because fmode won't
2641 * always have FMODE_EXEC set when file open for exec.
2642 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002643 if (openflags & __FMODE_EXEC) {
2644 /* ONLY check for exec rights */
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002645 if (S_ISDIR(state->inode->i_mode))
2646 mask = NFS4_ACCESS_LOOKUP;
2647 else
2648 mask = NFS4_ACCESS_EXECUTE;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002649 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002650 mask = NFS4_ACCESS_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002651
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002652 nfs_access_set_mask(&cache, opendata->o_res.access_result);
NeilBrown73fbb3f2021-09-28 09:47:57 +10002653 nfs_access_add_cache(state->inode, &cache, cred);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002654
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002655 flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP;
2656 if ((mask & ~cache.mask & flags) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002657 return 0;
2658
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002659 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002660}
2661
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002662/*
2663 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2664 */
Fred Isaman3b65a302016-09-19 10:06:49 -04002665static int _nfs4_proc_open(struct nfs4_opendata *data,
2666 struct nfs_open_context *ctx)
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002667{
David Howells2b0143b2015-03-17 22:25:59 +00002668 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002669 struct nfs_server *server = NFS_SERVER(dir);
2670 struct nfs_openargs *o_arg = &data->o_arg;
2671 struct nfs_openres *o_res = &data->o_res;
2672 int status;
2673
Fred Isaman3b65a302016-09-19 10:06:49 -04002674 status = nfs4_run_open_task(data, ctx);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002675 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002676 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002677 if (status != 0) {
2678 if (status == -NFS4ERR_BADNAME &&
2679 !(o_arg->open_flags & O_CREAT))
2680 return -ENOENT;
2681 return status;
2682 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002683
Trond Myklebust6926afd12012-01-07 13:22:46 -05002684 nfs_fattr_map_and_free_names(server, &data->f_attr);
2685
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002686 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002687 if (o_arg->open_flags & O_EXCL)
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002688 data->file_created = true;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002689 else if (o_res->cinfo.before != o_res->cinfo.after)
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002690 data->file_created = true;
Jeff Layton1eb5d982018-01-09 08:21:17 -05002691 if (data->file_created ||
2692 inode_peek_iversion_raw(dir) != o_res->cinfo.after)
Frank van der Linden1b523ca2020-06-23 22:38:59 +00002693 nfs4_update_changeattr(dir, &o_res->cinfo,
2694 o_res->f_attr->time_start,
2695 NFS_INO_INVALID_DATA);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002696 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002697 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2698 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002700 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002702 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 }
Trond Myklebust56e0d712017-04-15 19:20:01 -04002704 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2705 nfs4_sequence_free_slot(&o_res->seq_res);
Anna Schumaker2ef61e02021-10-22 13:11:07 -04002706 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, NULL);
Trond Myklebust56e0d712017-04-15 19:20:01 -04002707 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002708 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709}
2710
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711/*
2712 * OPEN_EXPIRED:
2713 * reclaim state on the server after a network partition.
2714 * Assumes caller holds the appropriate lock
2715 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002716static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002718 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002719 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002721 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002722 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002723 if (IS_ERR(opendata))
2724 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002725 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002726 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002727 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002728 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002729 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730}
2731
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002732static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002733{
Trond Myklebust539cd032007-06-05 11:46:42 -04002734 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002735 struct nfs4_exception exception = { };
2736 int err;
2737
2738 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002739 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002740 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002741 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2742 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002743 switch (err) {
2744 default:
2745 goto out;
2746 case -NFS4ERR_GRACE:
2747 case -NFS4ERR_DELAY:
2748 nfs4_handle_exception(server, err, &exception);
2749 err = 0;
2750 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002751 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002752out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002753 return err;
2754}
2755
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2757{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002759 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760
Trond Myklebust864472e2006-01-03 09:55:15 +01002761 ctx = nfs4_state_find_open_context(state);
2762 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002763 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002764 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002765 put_nfs_open_context(ctx);
2766 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767}
2768
Trond Myklebust41020b62016-09-22 13:38:58 -04002769static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2770 const nfs4_stateid *stateid)
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002771{
Trond Myklebust41020b62016-09-22 13:38:58 -04002772 nfs_remove_bad_delegation(state->inode, stateid);
Trond Myklebust9f0c5122018-09-05 14:07:15 -04002773 nfs_state_clear_delegation(state);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002774}
2775
2776static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2777{
2778 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
Trond Myklebust41020b62016-09-22 13:38:58 -04002779 nfs_finish_clear_delegation_stateid(state, NULL);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002780}
2781
2782static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2783{
2784 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2785 nfs40_clear_delegation_stateid(state);
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002786 nfs_state_clear_open_state_flags(state);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002787 return nfs4_open_expired(sp, state);
2788}
2789
Trond Myklebust45870d62016-09-22 13:38:59 -04002790static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2791 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11002792 const struct cred *cred)
Trond Myklebust45870d62016-09-22 13:38:59 -04002793{
2794 return -NFS4ERR_BAD_STATEID;
2795}
2796
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002797#if defined(CONFIG_NFS_V4_1)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002798static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2799 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11002800 const struct cred *cred)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002801{
2802 int status;
2803
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002804 switch (stateid->type) {
2805 default:
2806 break;
2807 case NFS4_INVALID_STATEID_TYPE:
2808 case NFS4_SPECIAL_STATEID_TYPE:
2809 return -NFS4ERR_BAD_STATEID;
2810 case NFS4_REVOKED_STATEID_TYPE:
2811 goto out_free;
2812 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002813
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002814 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002815 switch (status) {
2816 case -NFS4ERR_EXPIRED:
2817 case -NFS4ERR_ADMIN_REVOKED:
2818 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002819 break;
2820 default:
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002821 return status;
2822 }
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002823out_free:
2824 /* Ack the revoked state to the server */
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04002825 nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002826 return -NFS4ERR_EXPIRED;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002827}
2828
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002829static int nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002830{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002831 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002832 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002833 struct nfs_delegation *delegation;
NeilBrowna52458b2018-12-03 11:30:31 +11002834 const struct cred *cred = NULL;
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002835 int status, ret = NFS_OK;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002836
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002837 /* Get the delegation credential for use by test/free_stateid */
2838 rcu_read_lock();
2839 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002840 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002841 rcu_read_unlock();
Trond Myklebust9f0c5122018-09-05 14:07:15 -04002842 nfs_state_clear_delegation(state);
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002843 return NFS_OK;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002844 }
2845
Trond Myklebustfc51b1c2020-04-02 15:27:09 -04002846 spin_lock(&delegation->lock);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002847 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002848
Trond Myklebust994b15b2018-09-05 14:07:14 -04002849 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2850 &delegation->flags)) {
Trond Myklebustfc51b1c2020-04-02 15:27:09 -04002851 spin_unlock(&delegation->lock);
Trond Myklebust994b15b2018-09-05 14:07:14 -04002852 rcu_read_unlock();
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002853 return NFS_OK;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002854 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002855
NeilBrowna52458b2018-12-03 11:30:31 +11002856 if (delegation->cred)
2857 cred = get_cred(delegation->cred);
Trond Myklebustfc51b1c2020-04-02 15:27:09 -04002858 spin_unlock(&delegation->lock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002859 rcu_read_unlock();
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002860 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002861 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002862 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
Trond Myklebust41020b62016-09-22 13:38:58 -04002863 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002864 else
2865 ret = status;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002866
Trond Myklebust8c39a392019-07-19 13:48:44 -04002867 put_cred(cred);
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002868 return ret;
2869}
2870
2871static void nfs41_delegation_recover_stateid(struct nfs4_state *state)
2872{
2873 nfs4_stateid tmp;
2874
2875 if (test_bit(NFS_DELEGATED_STATE, &state->flags) &&
2876 nfs4_copy_delegation_stateid(state->inode, state->state,
2877 &tmp, NULL) &&
2878 nfs4_stateid_match_other(&state->stateid, &tmp))
2879 nfs_state_set_delegation(state, &tmp, state->state);
2880 else
2881 nfs_state_clear_delegation(state);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002882}
2883
2884/**
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002885 * nfs41_check_expired_locks - possibly free a lock stateid
2886 *
2887 * @state: NFSv4 state for an inode
2888 *
2889 * Returns NFS_OK if recovery for this stateid is now finished.
2890 * Otherwise a negative NFS4ERR value is returned.
2891 */
2892static int nfs41_check_expired_locks(struct nfs4_state *state)
2893{
2894 int status, ret = NFS_OK;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002895 struct nfs4_lock_state *lsp, *prev = NULL;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002896 struct nfs_server *server = NFS_SERVER(state->inode);
2897
2898 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2899 goto out;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002900
2901 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002902 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2903 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
NeilBrowna52458b2018-12-03 11:30:31 +11002904 const struct cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002905
Elena Reshetova194bc1f2017-10-20 12:53:36 +03002906 refcount_inc(&lsp->ls_count);
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002907 spin_unlock(&state->state_lock);
2908
2909 nfs4_put_lock_state(prev);
2910 prev = lsp;
2911
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002912 status = nfs41_test_and_free_expired_stateid(server,
2913 &lsp->ls_stateid,
2914 cred);
2915 trace_nfs4_test_lock_stateid(state, lsp, status);
2916 if (status == -NFS4ERR_EXPIRED ||
2917 status == -NFS4ERR_BAD_STATEID) {
2918 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002919 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002920 if (!recover_lost_locks)
2921 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2922 } else if (status != NFS_OK) {
2923 ret = status;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002924 nfs4_put_lock_state(prev);
2925 goto out;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002926 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002927 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002928 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002929 }
2930 spin_unlock(&state->state_lock);
2931 nfs4_put_lock_state(prev);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002932out:
2933 return ret;
2934}
2935
2936/**
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002937 * nfs41_check_open_stateid - possibly free an open stateid
2938 *
2939 * @state: NFSv4 state for an inode
2940 *
2941 * Returns NFS_OK if recovery for this stateid is now finished.
2942 * Otherwise a negative NFS4ERR value is returned.
2943 */
2944static int nfs41_check_open_stateid(struct nfs4_state *state)
2945{
2946 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002947 nfs4_stateid *stateid = &state->open_stateid;
NeilBrowna52458b2018-12-03 11:30:31 +11002948 const struct cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002949 int status;
2950
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002951 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002952 return -NFS4ERR_BAD_STATEID;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002953 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002954 trace_nfs4_test_open_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002955 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002956 nfs_state_clear_open_state_flags(state);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002957 stateid->type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002958 return status;
Trond Myklebustc0ca0e52017-08-08 21:39:28 -04002959 }
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002960 if (nfs_open_stateid_recover_openmode(state))
2961 return -NFS4ERR_OPENMODE;
2962 return NFS_OK;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002963}
2964
2965static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2966{
Chuck Levereb64cf92012-07-11 16:30:05 -04002967 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002968
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002969 status = nfs41_check_delegation_stateid(state);
2970 if (status != NFS_OK)
2971 return status;
2972 nfs41_delegation_recover_stateid(state);
2973
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002974 status = nfs41_check_expired_locks(state);
2975 if (status != NFS_OK)
2976 return status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002977 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002978 if (status != NFS_OK)
2979 status = nfs4_open_expired(sp, state);
2980 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002981}
2982#endif
2983
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002985 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2986 * fields corresponding to attributes that were used to store the verifier.
2987 * Make sure we clobber those fields in the later setattr call
2988 */
Trond Myklebust609339c2018-03-28 16:18:17 -04002989static unsigned nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002990 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002991{
Trond Myklebust609339c2018-03-28 16:18:17 -04002992 const __u32 *bitmask = opendata->o_arg.server->exclcreat_bitmask;
2993 __u32 attrset[3];
2994 unsigned ret;
2995 unsigned i;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002996
Trond Myklebust609339c2018-03-28 16:18:17 -04002997 for (i = 0; i < ARRAY_SIZE(attrset); i++) {
2998 attrset[i] = opendata->o_res.attrset[i];
2999 if (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE4_1)
3000 attrset[i] &= ~bitmask[i];
3001 }
Jeff Laytonaa53ed52007-06-05 14:49:03 -04003002
Trond Myklebust609339c2018-03-28 16:18:17 -04003003 ret = (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE) ?
3004 sattr->ia_valid : 0;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08003005
Trond Myklebust609339c2018-03-28 16:18:17 -04003006 if ((attrset[1] & (FATTR4_WORD1_TIME_ACCESS|FATTR4_WORD1_TIME_ACCESS_SET))) {
3007 if (sattr->ia_valid & ATTR_ATIME_SET)
3008 ret |= ATTR_ATIME_SET;
3009 else
3010 ret |= ATTR_ATIME;
3011 }
Kinglong Mee5334c5b2015-08-26 21:13:37 +08003012
Trond Myklebust609339c2018-03-28 16:18:17 -04003013 if ((attrset[1] & (FATTR4_WORD1_TIME_MODIFY|FATTR4_WORD1_TIME_MODIFY_SET))) {
3014 if (sattr->ia_valid & ATTR_MTIME_SET)
3015 ret |= ATTR_MTIME_SET;
3016 else
3017 ret |= ATTR_MTIME;
3018 }
3019
3020 if (!(attrset[2] & FATTR4_WORD2_SECURITY_LABEL))
Kinglong Mee5334c5b2015-08-26 21:13:37 +08003021 *label = NULL;
Trond Myklebust609339c2018-03-28 16:18:17 -04003022 return ret;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04003023}
3024
Trond Myklebustc21443c2013-02-07 14:26:21 -05003025static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003026 int flags, struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05003027{
3028 struct nfs4_state_owner *sp = opendata->owner;
3029 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04003030 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05003031 struct nfs4_state *state;
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003032 fmode_t acc_mode = _nfs4_ctx_to_accessmode(ctx);
Trond Myklebustcf5b4052020-02-05 09:01:53 -05003033 struct inode *dir = d_inode(opendata->dir);
3034 unsigned long dir_verifier;
Trond Myklebustc21443c2013-02-07 14:26:21 -05003035 unsigned int seq;
3036 int ret;
3037
3038 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
Trond Myklebustcf5b4052020-02-05 09:01:53 -05003039 dir_verifier = nfs_save_change_attribute(dir);
Trond Myklebustc21443c2013-02-07 14:26:21 -05003040
Fred Isaman3b65a302016-09-19 10:06:49 -04003041 ret = _nfs4_proc_open(opendata, ctx);
Trond Myklebustdca780012014-10-23 19:23:03 +03003042 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05003043 goto out;
3044
Trond Myklebustae55e592018-05-22 11:17:16 -04003045 state = _nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebustc21443c2013-02-07 14:26:21 -05003046 ret = PTR_ERR(state);
3047 if (IS_ERR(state))
3048 goto out;
Trond Myklebusta974dee2017-02-08 11:29:46 -05003049 ctx->state = state;
Trond Myklebustc21443c2013-02-07 14:26:21 -05003050 if (server->caps & NFS_CAP_POSIX_LOCK)
3051 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Jeff Laytona8ce3772016-09-17 18:17:35 -04003052 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
3053 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05003054
Trond Myklebust275bb302013-05-29 13:11:28 -04003055 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00003056 if (d_really_is_negative(dentry)) {
Al Viro668d0cd2016-03-08 12:44:17 -05003057 struct dentry *alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04003058 d_drop(dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05003059 alias = d_exact_alias(dentry, state->inode);
3060 if (!alias)
3061 alias = d_splice_alias(igrab(state->inode), dentry);
3062 /* d_splice_alias() can't fail here - it's a non-directory */
3063 if (alias) {
Trond Myklebust275bb302013-05-29 13:11:28 -04003064 dput(ctx->dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05003065 ctx->dentry = dentry = alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04003066 }
Trond Myklebustcf5b4052020-02-05 09:01:53 -05003067 }
3068
3069 switch(opendata->o_arg.claim) {
3070 default:
3071 break;
3072 case NFS4_OPEN_CLAIM_NULL:
3073 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
3074 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
3075 if (!opendata->rpc_done)
3076 break;
3077 if (opendata->o_res.delegation_type != 0)
3078 dir_verifier = nfs_save_change_attribute(dir);
3079 nfs_set_verifier(dentry, dir_verifier);
Trond Myklebust275bb302013-05-29 13:11:28 -04003080 }
3081
Trond Myklebustaf9b6d72018-06-29 12:45:53 -04003082 /* Parse layoutget results before we check for access */
3083 pnfs_parse_lgopen(state->inode, opendata->lgp, ctx);
3084
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003085 ret = nfs4_opendata_access(sp->so_cred, opendata, state,
3086 acc_mode, flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05003087 if (ret != 0)
3088 goto out;
3089
David Howells2b0143b2015-03-17 22:25:59 +00003090 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04003091 nfs_inode_attach_open_context(ctx);
3092 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
3093 nfs4_schedule_stateid_recovery(server, state);
3094 }
Fred Isaman2409a972016-10-06 12:11:21 -04003095
Trond Myklebustc21443c2013-02-07 14:26:21 -05003096out:
Olga Kornievskaia0cb98ab2019-03-19 12:12:13 -04003097 if (!opendata->cancelled)
3098 nfs4_sequence_free_slot(&opendata->o_res.seq_res);
Trond Myklebustc21443c2013-02-07 14:26:21 -05003099 return ret;
3100}
3101
Jeff Laytonaa53ed52007-06-05 14:49:03 -04003102/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01003103 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 */
Andy Adamson82be4172012-05-23 05:02:35 -04003105static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04003106 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04003107 int flags,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003108 const struct nfs4_open_createattrs *c,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003109 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110{
3111 struct nfs4_state_owner *sp;
3112 struct nfs4_state *state = NULL;
3113 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01003114 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04003115 struct dentry *dentry = ctx->dentry;
NeilBrowna52458b2018-12-03 11:30:31 +11003116 const struct cred *cred = ctx->cred;
Trond Myklebust4197a052013-05-29 12:37:49 -04003117 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003118 fmode_t fmode = _nfs4_ctx_to_openmode(ctx);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003119 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003120 struct iattr *sattr = c->sattr;
3121 struct nfs4_label *label = c->label;
Trond Myklebustaac00a82007-07-05 19:02:21 -04003122 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123
3124 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05003126 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
3127 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
3129 goto out_err;
3130 }
Anna Schumaker334f87d2017-01-11 16:17:17 -05003131 status = nfs4_client_recover_expired_lease(server->nfs_client);
Trond Myklebust58d97142006-01-03 09:55:24 +01003132 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003133 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00003134 if (d_really_is_positive(dentry))
3135 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01003136 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00003137 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003138 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003139 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
3140 c, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01003141 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05003142 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143
Trond Myklebuste911b812014-03-26 13:24:37 -07003144 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
3145 if (!opendata->f_attr.mdsthreshold) {
3146 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
3147 if (!opendata->f_attr.mdsthreshold)
Anna Schumaker76baa2b2021-10-22 13:11:06 -04003148 goto err_opendata_put;
Trond Myklebuste911b812014-03-26 13:24:37 -07003149 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04003150 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04003151 }
David Howells2b0143b2015-03-17 22:25:59 +00003152 if (d_really_is_positive(dentry))
3153 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04003154
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003155 status = _nfs4_open_and_get_state(opendata, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003156 if (status != 0)
Anna Schumaker76baa2b2021-10-22 13:11:06 -04003157 goto err_opendata_put;
Trond Myklebust3efb9722013-05-29 13:17:04 -04003158 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003159
NeilBrownefcbc042015-07-30 13:00:56 +10003160 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04003161 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Trond Myklebust609339c2018-03-28 16:18:17 -04003162 unsigned attrs = nfs4_exclusive_attrset(opendata, sattr, &label);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02003163 /*
3164 * send create attributes which was not set by open
3165 * with an extra setattr.
3166 */
Trond Myklebust609339c2018-03-28 16:18:17 -04003167 if (attrs || label) {
3168 unsigned ia_old = sattr->ia_valid;
3169
3170 sattr->ia_valid = attrs;
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02003171 nfs_fattr_init(opendata->o_res.f_attr);
3172 status = nfs4_do_setattr(state->inode, cred,
3173 opendata->o_res.f_attr, sattr,
Anna Schumaker1b00ad62021-10-22 13:11:08 -04003174 ctx, label);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02003175 if (status == 0) {
3176 nfs_setattr_update_inode(state->inode, sattr,
3177 opendata->o_res.f_attr);
Anna Schumakerdd225cb2021-10-22 13:11:12 -04003178 nfs_setsecurity(state->inode, opendata->o_res.f_attr);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02003179 }
Trond Myklebust609339c2018-03-28 16:18:17 -04003180 sattr->ia_valid = ia_old;
David Quigley1775fd32013-05-22 12:50:42 -04003181 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04003182 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08003183 if (opened && opendata->file_created)
Al Viro73a09dd2018-06-08 13:22:02 -04003184 *opened = 1;
Andy Adamson82be4172012-05-23 05:02:35 -04003185
Trond Myklebuste911b812014-03-26 13:24:37 -07003186 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04003187 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07003188 opendata->f_attr.mdsthreshold = NULL;
3189 }
Andy Adamson82be4172012-05-23 05:02:35 -04003190
Trond Myklebustc6d00e62007-06-17 16:02:44 -04003191 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04003194err_opendata_put:
3195 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01003196err_put_state_owner:
3197 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 return status;
3200}
3201
3202
Andy Adamson82be4172012-05-23 05:02:35 -04003203static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04003204 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04003205 int flags,
3206 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003207 struct nfs4_label *label,
3208 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003210 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust0688e642019-04-07 13:59:09 -04003211 struct nfs4_exception exception = {
3212 .interruptible = true,
3213 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 struct nfs4_state *res;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003215 struct nfs4_open_createattrs c = {
3216 .label = label,
3217 .sattr = sattr,
3218 .verf = {
3219 [0] = (__u32)jiffies,
3220 [1] = (__u32)current->pid,
3221 },
3222 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 int status;
3224
3225 do {
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003226 status = _nfs4_do_open(dir, ctx, flags, &c, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04003227 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04003228 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 if (status == 0)
3230 break;
3231 /* NOTE: BAD_SEQID means the server and client disagree about the
3232 * book-keeping w.r.t. state-changing operations
3233 * (OPEN/CLOSE/LOCK/LOCKU...)
3234 * It is actually a sign of a bug on the client or on the server.
3235 *
3236 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07003237 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238 * have unhashed the old state_owner for us, and that we can
3239 * therefore safely retry using a new one. We should still warn
3240 * the user though...
3241 */
3242 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04003243 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04003244 " returned a bad sequence-id error!\n",
3245 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 exception.retry = 1;
3247 continue;
3248 }
Trond Myklebust550f5742005-10-18 14:20:21 -07003249 /*
3250 * BAD_STATEID on OPEN means that the server cancelled our
3251 * state before it received the OPEN_CONFIRM.
3252 * Recover by retrying the request as per the discussion
3253 * on Page 181 of RFC3530.
3254 */
3255 if (status == -NFS4ERR_BAD_STATEID) {
3256 exception.retry = 1;
3257 continue;
3258 }
Robert Milkowski924491f2020-01-28 08:37:47 +00003259 if (status == -NFS4ERR_EXPIRED) {
3260 nfs4_schedule_lease_recovery(server->nfs_client);
3261 exception.retry = 1;
3262 continue;
3263 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04003264 if (status == -EAGAIN) {
3265 /* We must have found a delegation */
3266 exception.retry = 1;
3267 continue;
3268 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003269 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
3270 continue;
3271 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 status, &exception));
3273 } while (exception.retry);
3274 return res;
3275}
3276
Trond Myklebust8487c472016-06-26 08:44:35 -04003277static int _nfs4_do_setattr(struct inode *inode,
3278 struct nfs_setattrargs *arg,
3279 struct nfs_setattrres *res,
NeilBrowna52458b2018-12-03 11:30:31 +11003280 const struct cred *cred,
NeilBrown29b59f92016-10-13 15:26:47 +11003281 struct nfs_open_context *ctx)
Trond Myklebust8487c472016-06-26 08:44:35 -04003282{
3283 struct nfs_server *server = NFS_SERVER(inode);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003284 struct rpc_message msg = {
Trond Myklebust8487c472016-06-26 08:44:35 -04003285 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
3286 .rpc_argp = arg,
3287 .rpc_resp = res,
3288 .rpc_cred = cred,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003289 };
NeilBrowna52458b2018-12-03 11:30:31 +11003290 const struct cred *delegation_cred = NULL;
Trond Myklebust8487c472016-06-26 08:44:35 -04003291 unsigned long timestamp = jiffies;
Trond Myklebust8487c472016-06-26 08:44:35 -04003292 bool truncate;
3293 int status;
3294
3295 nfs_fattr_init(res->fattr);
3296
3297 /* Servers should only apply open mode checks for file size changes */
3298 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
Chuck Lever644c9f42020-09-04 17:39:12 -04003299 if (!truncate) {
3300 nfs4_inode_make_writeable(inode);
Trond Myklebust991eedb2018-04-09 11:15:30 -04003301 goto zero_stateid;
Chuck Lever644c9f42020-09-04 17:39:12 -04003302 }
Trond Myklebust8487c472016-06-26 08:44:35 -04003303
Trond Myklebust991eedb2018-04-09 11:15:30 -04003304 if (nfs4_copy_delegation_stateid(inode, FMODE_WRITE, &arg->stateid, &delegation_cred)) {
Trond Myklebust8487c472016-06-26 08:44:35 -04003305 /* Use that stateid */
Trond Myklebust09a54f02019-08-03 10:28:18 -04003306 } else if (ctx != NULL && ctx->state) {
NeilBrown17393472016-10-13 15:26:47 +11003307 struct nfs_lock_context *l_ctx;
NeilBrown29b59f92016-10-13 15:26:47 +11003308 if (!nfs4_valid_open_stateid(ctx->state))
Trond Myklebust8487c472016-06-26 08:44:35 -04003309 return -EBADF;
NeilBrown17393472016-10-13 15:26:47 +11003310 l_ctx = nfs_get_lock_context(ctx);
3311 if (IS_ERR(l_ctx))
3312 return PTR_ERR(l_ctx);
NeilBrown7a0566b2016-12-06 15:50:06 -05003313 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
3314 &arg->stateid, &delegation_cred);
3315 nfs_put_lock_context(l_ctx);
3316 if (status == -EIO)
Trond Myklebust8487c472016-06-26 08:44:35 -04003317 return -EBADF;
Olga Kornievskaiad826e5b2019-12-18 16:50:42 -05003318 else if (status == -EAGAIN)
3319 goto zero_stateid;
Trond Myklebust991eedb2018-04-09 11:15:30 -04003320 } else {
3321zero_stateid:
Trond Myklebust8487c472016-06-26 08:44:35 -04003322 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
Trond Myklebust991eedb2018-04-09 11:15:30 -04003323 }
Trond Myklebust8487c472016-06-26 08:44:35 -04003324 if (delegation_cred)
3325 msg.rpc_cred = delegation_cred;
3326
3327 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
3328
NeilBrowna52458b2018-12-03 11:30:31 +11003329 put_cred(delegation_cred);
NeilBrown29b59f92016-10-13 15:26:47 +11003330 if (status == 0 && ctx != NULL)
Trond Myklebust8487c472016-06-26 08:44:35 -04003331 renew_lease(server, timestamp);
3332 trace_nfs4_setattr(inode, &arg->stateid, status);
3333 return status;
3334}
3335
NeilBrowna52458b2018-12-03 11:30:31 +11003336static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
Trond Myklebust8487c472016-06-26 08:44:35 -04003337 struct nfs_fattr *fattr, struct iattr *sattr,
Anna Schumaker1b00ad62021-10-22 13:11:08 -04003338 struct nfs_open_context *ctx, struct nfs4_label *ilabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05003340 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebust30846df2018-04-07 13:44:28 -04003341 __u32 bitmask[NFS4_BITMASK_SZ];
NeilBrown29b59f92016-10-13 15:26:47 +11003342 struct nfs4_state *state = ctx ? ctx->state : NULL;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003343 struct nfs_setattrargs arg = {
3344 .fh = NFS_FH(inode),
3345 .iap = sattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346 .server = server,
Trond Myklebust30846df2018-04-07 13:44:28 -04003347 .bitmask = bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04003348 .label = ilabel,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003349 };
3350 struct nfs_setattrres res = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351 .fattr = fattr,
3352 .server = server,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003353 };
Trond Myklebust8487c472016-06-26 08:44:35 -04003354 struct nfs4_exception exception = {
3355 .state = state,
3356 .inode = inode,
3357 .stateid = &arg.stateid,
3358 };
Trond Myklebusta71029b2021-04-10 00:23:03 -04003359 unsigned long adjust_flags = NFS_INO_INVALID_CHANGE;
Trond Myklebust8487c472016-06-26 08:44:35 -04003360 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361
Trond Myklebust720869e2021-04-13 09:41:16 -04003362 if (sattr->ia_valid & (ATTR_MODE | ATTR_KILL_SUID | ATTR_KILL_SGID))
3363 adjust_flags |= NFS_INO_INVALID_MODE;
3364 if (sattr->ia_valid & (ATTR_UID | ATTR_GID))
Trond Myklebusta71029b2021-04-10 00:23:03 -04003365 adjust_flags |= NFS_INO_INVALID_OTHER;
3366
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367 do {
Anna Schumaker1b00ad62021-10-22 13:11:08 -04003368 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, fattr->label),
Trond Myklebusta71029b2021-04-10 00:23:03 -04003369 inode, adjust_flags);
Trond Myklebust30846df2018-04-07 13:44:28 -04003370
NeilBrown29b59f92016-10-13 15:26:47 +11003371 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
Trond Myklebust451146b2012-04-18 16:29:11 -04003372 switch (err) {
3373 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04003374 if (!(sattr->ia_valid & ATTR_SIZE)) {
3375 pr_warn_once("NFSv4: server %s is incorrectly "
3376 "applying open mode checks to "
3377 "a SETATTR that is not "
3378 "changing file size.\n",
3379 server->nfs_client->cl_hostname);
3380 }
Trond Myklebust451146b2012-04-18 16:29:11 -04003381 if (state && !(state->state & FMODE_WRITE)) {
3382 err = -EBADF;
3383 if (sattr->ia_valid & ATTR_OPEN)
3384 err = -EACCES;
3385 goto out;
3386 }
3387 }
3388 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04003390out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391 return err;
3392}
3393
Peng Tao500d7012015-09-22 11:35:22 +08003394static bool
3395nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3396{
3397 if (inode == NULL || !nfs_have_layout(inode))
3398 return false;
3399
3400 return pnfs_wait_on_layoutreturn(inode, task);
3401}
3402
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003403/*
3404 * Update the seqid of an open stateid
3405 */
3406static void nfs4_sync_open_stateid(nfs4_stateid *dst,
3407 struct nfs4_state *state)
3408{
3409 __be32 seqid_open;
3410 u32 dst_seqid;
3411 int seq;
3412
3413 for (;;) {
3414 if (!nfs4_valid_open_stateid(state))
3415 break;
3416 seq = read_seqbegin(&state->seqlock);
3417 if (!nfs4_state_match_open_stateid_other(state, dst)) {
3418 nfs4_stateid_copy(dst, &state->open_stateid);
3419 if (read_seqretry(&state->seqlock, seq))
3420 continue;
3421 break;
3422 }
3423 seqid_open = state->open_stateid.seqid;
3424 if (read_seqretry(&state->seqlock, seq))
3425 continue;
3426
3427 dst_seqid = be32_to_cpu(dst->seqid);
3428 if ((s32)(dst_seqid - be32_to_cpu(seqid_open)) < 0)
3429 dst->seqid = seqid_open;
3430 break;
3431 }
3432}
3433
3434/*
3435 * Update the seqid of an open stateid after receiving
3436 * NFS4ERR_OLD_STATEID
3437 */
3438static bool nfs4_refresh_open_old_stateid(nfs4_stateid *dst,
3439 struct nfs4_state *state)
3440{
3441 __be32 seqid_open;
3442 u32 dst_seqid;
3443 bool ret;
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04003444 int seq, status = -EAGAIN;
3445 DEFINE_WAIT(wait);
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003446
3447 for (;;) {
3448 ret = false;
3449 if (!nfs4_valid_open_stateid(state))
3450 break;
3451 seq = read_seqbegin(&state->seqlock);
3452 if (!nfs4_state_match_open_stateid_other(state, dst)) {
3453 if (read_seqretry(&state->seqlock, seq))
3454 continue;
3455 break;
3456 }
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04003457
3458 write_seqlock(&state->seqlock);
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003459 seqid_open = state->open_stateid.seqid;
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003460
3461 dst_seqid = be32_to_cpu(dst->seqid);
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04003462
3463 /* Did another OPEN bump the state's seqid? try again: */
3464 if ((s32)(be32_to_cpu(seqid_open) - dst_seqid) > 0) {
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003465 dst->seqid = seqid_open;
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04003466 write_sequnlock(&state->seqlock);
3467 ret = true;
3468 break;
3469 }
3470
3471 /* server says we're behind but we haven't seen the update yet */
3472 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
3473 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
3474 write_sequnlock(&state->seqlock);
3475 trace_nfs4_close_stateid_update_wait(state->inode, dst, 0);
3476
zhouchuangaobb002382021-05-09 19:34:37 -07003477 if (fatal_signal_pending(current))
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04003478 status = -EINTR;
3479 else
3480 if (schedule_timeout(5*HZ) != 0)
3481 status = 0;
3482
3483 finish_wait(&state->waitq, &wait);
3484
3485 if (!status)
3486 continue;
3487 if (status == -EINTR)
3488 break;
3489
3490 /* we slept the whole 5 seconds, we must have lost a seqid */
3491 dst->seqid = cpu_to_be32(dst_seqid + 1);
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003492 ret = true;
3493 break;
3494 }
3495
3496 return ret;
3497}
3498
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499struct nfs4_closedata {
3500 struct inode *inode;
3501 struct nfs4_state *state;
3502 struct nfs_closeargs arg;
3503 struct nfs_closeres res;
Trond Myklebustcf805162016-11-15 14:56:07 -05003504 struct {
3505 struct nfs4_layoutreturn_args arg;
3506 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003507 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebustcf805162016-11-15 14:56:07 -05003508 u32 roc_barrier;
3509 bool roc;
3510 } lr;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003511 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003512 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513};
3514
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003515static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07003516{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003517 struct nfs4_closedata *calldata = data;
3518 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04003519 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07003520
Trond Myklebustcf805162016-11-15 14:56:07 -05003521 if (calldata->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003522 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3523 calldata->res.lr_ret);
Trond Myklebust95121352005-10-18 14:20:12 -07003524 nfs4_put_open_state(calldata->state);
3525 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07003526 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05003527 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003528 kfree(calldata);
3529}
3530
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003531static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003533 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003536 nfs4_stateid *res_stateid = NULL;
Trond Myklebustb8b8d222017-11-07 10:51:37 -05003537 struct nfs4_exception exception = {
3538 .state = state,
3539 .inode = calldata->inode,
3540 .stateid = &calldata->arg.stateid,
3541 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542
Trond Myklebust14516c32010-07-31 14:29:06 -04003543 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3544 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04003545 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Trond Myklebustcf805162016-11-15 14:56:07 -05003546
3547 /* Handle Layoutreturn errors */
Trond Myklebust078000d2021-01-04 13:18:03 -05003548 if (pnfs_roc_done(task, &calldata->arg.lr_args, &calldata->res.lr_res,
3549 &calldata->res.lr_ret) == -EAGAIN)
Trond Myklebust287a9c52019-09-20 07:23:41 -04003550 goto out_restart;
Trond Myklebustcf805162016-11-15 14:56:07 -05003551
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003552 /* hmm. we are done with the inode, and in the process of freeing
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553 * the state_owner. we keep this around to process errors
3554 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555 switch (task->tk_status) {
3556 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04003557 res_stateid = &calldata->res.stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003558 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003559 break;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003560 case -NFS4ERR_ACCESS:
3561 if (calldata->arg.bitmask != NULL) {
3562 calldata->arg.bitmask = NULL;
3563 calldata->res.fattr = NULL;
Trond Myklebust91b30d22017-11-06 15:28:09 -05003564 goto out_restart;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003565
3566 }
3567 break;
Trond Myklebust12f275c2017-11-06 15:28:05 -05003568 case -NFS4ERR_OLD_STATEID:
3569 /* Did we race with OPEN? */
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003570 if (nfs4_refresh_open_old_stateid(&calldata->arg.stateid,
Trond Myklebust91b30d22017-11-06 15:28:09 -05003571 state))
3572 goto out_restart;
Trond Myklebust12f275c2017-11-06 15:28:05 -05003573 goto out_release;
Trond Myklebust69794ad2013-11-20 12:57:19 -05003574 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575 case -NFS4ERR_STALE_STATEID:
Trond Myklebust26d36302016-09-22 13:39:05 -04003576 case -NFS4ERR_EXPIRED:
3577 nfs4_free_revoked_stateid(server,
3578 &calldata->arg.stateid,
3579 task->tk_msg.rpc_cred);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05003580 fallthrough;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003581 case -NFS4ERR_BAD_STATEID:
Trond Myklebuste217e822019-09-20 07:23:46 -04003582 if (calldata->arg.fmode == 0)
3583 break;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05003584 fallthrough;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585 default:
Trond Myklebustb8b8d222017-11-07 10:51:37 -05003586 task->tk_status = nfs4_async_handle_exception(task,
3587 server, task->tk_status, &exception);
3588 if (exception.retry)
Trond Myklebust91b30d22017-11-06 15:28:09 -05003589 goto out_restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07003591 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3592 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003593out_release:
Trond Myklebust91b30d22017-11-06 15:28:09 -05003594 task->tk_status = 0;
Trond Myklebust72211db2009-12-15 14:47:36 -05003595 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustd8d84982016-12-19 12:14:44 -05003596 nfs_refresh_inode(calldata->inode, &calldata->fattr);
Chuck Lever86882c72021-10-16 18:03:04 -04003597 dprintk("%s: ret = %d\n", __func__, task->tk_status);
Trond Myklebust91b30d22017-11-06 15:28:09 -05003598 return;
Trond Myklebust91b30d22017-11-06 15:28:09 -05003599out_restart:
3600 task->tk_status = 0;
3601 rpc_restart_call_prepare(task);
3602 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603}
3604
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003605static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003607 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07003608 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003609 struct inode *inode = calldata->inode;
Trond Myklebust332d1a02021-03-25 18:15:36 -04003610 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustc8bf7072018-06-15 16:31:02 -04003611 struct pnfs_layout_hdr *lo;
Trond Myklebustaee7af32014-08-25 22:33:12 -04003612 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05003613 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07003614
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003615 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003616 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04003617
Trond Myklebust88069f72009-12-08 08:33:16 -05003618 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05003619 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04003620 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3621 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3622 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust003707c2007-07-05 18:07:55 -04003623 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003624 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01003625 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003626 if (state->n_rdonly == 0)
3627 call_close |= is_rdonly;
3628 else if (is_rdonly)
3629 calldata->arg.fmode |= FMODE_READ;
3630 if (state->n_wronly == 0)
3631 call_close |= is_wronly;
3632 else if (is_wronly)
3633 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebuste547f262016-06-25 19:19:28 -04003634 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3635 call_close |= is_rdwr;
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003636 } else if (is_rdwr)
3637 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3638
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003639 nfs4_sync_open_stateid(&calldata->arg.stateid, state);
3640 if (!nfs4_valid_open_stateid(state))
Trond Myklebust5d422302013-03-14 16:57:48 -04003641 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05003642 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05003643
3644 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003645 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003646 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07003647 }
Trond Myklebust88069f72009-12-08 08:33:16 -05003648
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003649 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
Peng Tao500d7012015-09-22 11:35:22 +08003650 nfs_release_seqid(calldata->arg.seqid);
3651 goto out_wait;
3652 }
3653
Trond Myklebustc8bf7072018-06-15 16:31:02 -04003654 lo = calldata->arg.lr_args ? calldata->arg.lr_args->layout : NULL;
3655 if (lo && !pnfs_layout_is_valid(lo)) {
3656 calldata->arg.lr_args = NULL;
3657 calldata->res.lr_res = NULL;
3658 }
3659
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003660 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05003661 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003662
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003663 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003664 /* Close-to-open cache consistency revalidation */
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04003665 if (!nfs4_have_delegation(inode, FMODE_READ)) {
Trond Myklebust332d1a02021-03-25 18:15:36 -04003666 nfs4_bitmask_set(calldata->arg.bitmask_store,
3667 server->cache_consistency_bitmask,
Trond Myklebust85847282021-12-27 14:40:51 -05003668 inode, 0);
Trond Myklebust332d1a02021-03-25 18:15:36 -04003669 calldata->arg.bitmask = calldata->arg.bitmask_store;
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04003670 } else
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003671 calldata->arg.bitmask = NULL;
3672 }
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003673
Trond Myklebust6ae37332015-01-30 14:21:14 -05003674 calldata->arg.share_access =
3675 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3676 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05003677
Trond Myklebustd8d84982016-12-19 12:14:44 -05003678 if (calldata->res.fattr == NULL)
3679 calldata->arg.bitmask = NULL;
3680 else if (calldata->arg.bitmask == NULL)
3681 calldata->res.fattr = NULL;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003682 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05003683 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
Trond Myklebust9d12b212012-01-17 22:04:25 -05003684 &calldata->arg.seq_args,
3685 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04003686 task) != 0)
3687 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003688 return;
3689out_no_action:
3690 task->tk_action = NULL;
3691out_wait:
3692 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693}
3694
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003695static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003696 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003697 .rpc_call_done = nfs4_close_done,
3698 .rpc_release = nfs4_free_closedata,
3699};
3700
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701/*
3702 * It is possible for data to be read/written from a mem-mapped file
3703 * after the sys_close call (which hits the vfs layer as a flush).
3704 * This means that we can't safely call nfsv4 close on a file until
3705 * the inode is cleared. This in turn means that we are not good
3706 * NFSv4 citizens - we do not indicate to the server to update the file's
3707 * share state even when we are done with one of the three share
3708 * stateid's in the inode.
3709 *
3710 * NOTE: Caller must be holding the sp->so_owner semaphore!
3711 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04003712int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003714 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05003715 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04003717 struct nfs4_state_owner *sp = state->owner;
3718 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003719 struct rpc_message msg = {
3720 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3721 .rpc_cred = state->owner->so_cred,
3722 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003723 struct rpc_task_setup task_setup_data = {
3724 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003725 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003726 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003727 .workqueue = nfsiod_workqueue,
Trond Myklebust61296502020-02-07 19:38:12 -05003728 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003729 };
Trond Myklebust95121352005-10-18 14:20:12 -07003730 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731
Olga Kornievskaia85e39fe2021-06-23 23:28:51 -04003732 if (server->nfs_client->cl_minorversion)
3733 task_setup_data.flags |= RPC_TASK_MOVEABLE;
3734
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04003735 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3736 &task_setup_data.rpc_client, &msg);
3737
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003738 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07003740 goto out;
Anna Schumakerfba83f32018-05-04 16:22:50 -04003741 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1, 0);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003742 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003744 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebustc82bac62017-11-06 15:28:06 -05003745 if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state))
3746 goto out_free_calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05003748 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3749 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05003750 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07003751 goto out_free_calldata;
Trond Myklebustd8d84982016-12-19 12:14:44 -05003752 nfs_fattr_init(&calldata->fattr);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003753 calldata->arg.fmode = 0;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003754 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003755 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003756 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003757 calldata->res.server = server;
Trond Myklebustcf805162016-11-15 14:56:07 -05003758 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003759 calldata->lr.roc = pnfs_roc(state->inode,
3760 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3761 if (calldata->lr.roc) {
3762 calldata->arg.lr_args = &calldata->lr.arg;
3763 calldata->res.lr_res = &calldata->lr.res;
3764 }
Al Viro643168c2011-06-22 18:20:23 -04003765 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003766
Trond Myklebust1174dd12010-12-21 10:52:24 -05003767 msg.rpc_argp = &calldata->arg;
3768 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003769 task_setup_data.callback_data = calldata;
3770 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003771 if (IS_ERR(task))
3772 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003773 status = 0;
3774 if (wait)
3775 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003776 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003777 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003778out_free_calldata:
3779 kfree(calldata);
3780out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003781 nfs4_put_open_state(state);
3782 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003783 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784}
3785
Trond Myklebust2b484292010-09-17 10:56:51 -04003786static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003787nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3788 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003791 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3792
3793 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794
Trond Myklebust565277f2007-10-15 18:17:53 -04003795 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003796 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003797
3798 nfs4_label_release_security(label);
3799
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003800 if (IS_ERR(state))
3801 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003802 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803}
3804
Trond Myklebust1185a552009-12-03 15:54:02 -05003805static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003806{
3807 if (ctx->state == NULL)
3808 return;
3809 if (is_sync)
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003810 nfs4_close_sync(ctx->state, _nfs4_ctx_to_openmode(ctx));
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003811 else
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003812 nfs4_close_state(ctx->state, _nfs4_ctx_to_openmode(ctx));
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003813}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814
Trond Myklebustb944dba2013-11-04 15:20:20 -05003815#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3816#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
Frank van der Lindenb78ef842020-06-23 22:38:55 +00003817#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_XATTR_SUPPORT - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003818
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3820{
Kinglong Mee8c612822015-08-26 21:12:58 +08003821 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003822 struct nfs4_server_caps_arg args = {
3823 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003824 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003825 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826 struct nfs4_server_caps_res res = {};
3827 struct rpc_message msg = {
3828 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003829 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830 .rpc_resp = &res,
3831 };
3832 int status;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003833 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834
Kinglong Mee8c612822015-08-26 21:12:58 +08003835 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3836 FATTR4_WORD0_FH_EXPIRE_TYPE |
3837 FATTR4_WORD0_LINK_SUPPORT |
3838 FATTR4_WORD0_SYMLINK_SUPPORT |
Trond Myklebust1ab5be42021-12-17 15:36:54 -05003839 FATTR4_WORD0_ACLSUPPORT |
3840 FATTR4_WORD0_CASE_INSENSITIVE |
3841 FATTR4_WORD0_CASE_PRESERVING;
Kinglong Mee8c612822015-08-26 21:12:58 +08003842 if (minorversion)
3843 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3844
Bryan Schumaker7c513052011-03-24 17:12:24 +00003845 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003846 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003847 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003848 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003849 case 0:
3850 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3851 res.attr_bitmask[2] = 0;
3852 break;
3853 case 1:
3854 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3855 break;
3856 case 2:
3857 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3858 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003859 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebustce62b112018-03-05 15:01:18 -05003860 server->caps &= ~(NFS_CAP_ACLS | NFS_CAP_HARDLINKS |
3861 NFS_CAP_SYMLINKS| NFS_CAP_SECURITY_LABEL);
3862 server->fattr_valid = NFS_ATTR_FATTR_V4;
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003863 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3864 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865 server->caps |= NFS_CAP_ACLS;
3866 if (res.has_links != 0)
3867 server->caps |= NFS_CAP_HARDLINKS;
3868 if (res.has_symlinks != 0)
3869 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust1ab5be42021-12-17 15:36:54 -05003870 if (res.case_insensitive)
3871 server->caps |= NFS_CAP_CASE_INSENSITIVE;
3872 if (res.case_preserving)
3873 server->caps |= NFS_CAP_CASE_PRESERVING;
Scott Mayhew0b4f1322021-06-02 13:13:11 -04003874#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3875 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3876 server->caps |= NFS_CAP_SECURITY_LABEL;
3877#endif
Olga Kornievskaia8a59bb92021-12-09 14:53:30 -05003878 if (res.attr_bitmask[0] & FATTR4_WORD0_FS_LOCATIONS)
3879 server->caps |= NFS_CAP_FS_LOCATIONS;
Trond Myklebustce62b112018-03-05 15:01:18 -05003880 if (!(res.attr_bitmask[0] & FATTR4_WORD0_FILEID))
3881 server->fattr_valid &= ~NFS_ATTR_FATTR_FILEID;
3882 if (!(res.attr_bitmask[1] & FATTR4_WORD1_MODE))
3883 server->fattr_valid &= ~NFS_ATTR_FATTR_MODE;
3884 if (!(res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS))
3885 server->fattr_valid &= ~NFS_ATTR_FATTR_NLINK;
3886 if (!(res.attr_bitmask[1] & FATTR4_WORD1_OWNER))
3887 server->fattr_valid &= ~(NFS_ATTR_FATTR_OWNER |
3888 NFS_ATTR_FATTR_OWNER_NAME);
3889 if (!(res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP))
3890 server->fattr_valid &= ~(NFS_ATTR_FATTR_GROUP |
3891 NFS_ATTR_FATTR_GROUP_NAME);
3892 if (!(res.attr_bitmask[1] & FATTR4_WORD1_SPACE_USED))
3893 server->fattr_valid &= ~NFS_ATTR_FATTR_SPACE_USED;
3894 if (!(res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS))
3895 server->fattr_valid &= ~NFS_ATTR_FATTR_ATIME;
3896 if (!(res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA))
3897 server->fattr_valid &= ~NFS_ATTR_FATTR_CTIME;
3898 if (!(res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY))
3899 server->fattr_valid &= ~NFS_ATTR_FATTR_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003900 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3901 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003902 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003903
Trond Myklebusta65318b2009-03-11 14:10:28 -04003904 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3905 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3906 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003907 server->cache_consistency_bitmask[2] = 0;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003908
3909 /* Avoid a regression due to buggy server */
3910 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3911 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
Kinglong Mee8c612822015-08-26 21:12:58 +08003912 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3913 sizeof(server->exclcreat_bitmask));
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003914
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003916 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003918
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919 return status;
3920}
3921
Trond Myklebust55a97592006-06-09 09:34:19 -04003922int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923{
Trond Myklebust0688e642019-04-07 13:59:09 -04003924 struct nfs4_exception exception = {
3925 .interruptible = true,
3926 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927 int err;
Anna Schumaker01dde762021-10-14 13:55:04 -04003928
3929 nfs4_server_set_init_caps(server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930 do {
3931 err = nfs4_handle_exception(server,
3932 _nfs4_server_capabilities(server, fhandle),
3933 &exception);
3934 } while (exception.retry);
3935 return err;
3936}
3937
Olga Kornievskaia4ca9f312021-12-09 14:53:35 -05003938static void test_fs_location_for_trunking(struct nfs4_fs_location *location,
3939 struct nfs_client *clp,
3940 struct nfs_server *server)
3941{
3942 int i;
3943
3944 for (i = 0; i < location->nservers; i++) {
3945 struct nfs4_string *srv_loc = &location->servers[i];
3946 struct sockaddr addr;
3947 size_t addrlen;
3948 struct xprt_create xprt_args = {
3949 .ident = 0,
3950 .net = clp->cl_net,
3951 };
3952 struct nfs4_add_xprt_data xprtdata = {
3953 .clp = clp,
3954 };
3955 struct rpc_add_xprt_test rpcdata = {
3956 .add_xprt_test = clp->cl_mvops->session_trunk,
3957 .data = &xprtdata,
3958 };
3959 char *servername = NULL;
3960
3961 if (!srv_loc->len)
3962 continue;
3963
3964 addrlen = nfs_parse_server_name(srv_loc->data, srv_loc->len,
3965 &addr, sizeof(addr),
3966 clp->cl_net, server->port);
3967 if (!addrlen)
3968 return;
3969 xprt_args.dstaddr = &addr;
3970 xprt_args.addrlen = addrlen;
3971 servername = kmalloc(srv_loc->len + 1, GFP_KERNEL);
3972 if (!servername)
3973 return;
3974 memcpy(servername, srv_loc->data, srv_loc->len);
3975 servername[srv_loc->len] = '\0';
3976 xprt_args.servername = servername;
3977
3978 xprtdata.cred = nfs4_get_clid_cred(clp);
3979 rpc_clnt_add_xprt(clp->cl_rpcclient, &xprt_args,
3980 rpc_clnt_setup_test_and_add_xprt,
3981 &rpcdata);
3982 if (xprtdata.cred)
3983 put_cred(xprtdata.cred);
3984 kfree(servername);
3985 }
3986}
3987
Olga Kornievskaia1976b2b2022-01-12 10:27:38 -05003988static int _nfs4_discover_trunking(struct nfs_server *server,
3989 struct nfs_fh *fhandle)
3990{
3991 struct nfs4_fs_locations *locations = NULL;
3992 struct page *page;
3993 const struct cred *cred;
3994 struct nfs_client *clp = server->nfs_client;
3995 const struct nfs4_state_maintenance_ops *ops =
3996 clp->cl_mvops->state_renewal_ops;
Olga Kornievskaia4ca9f312021-12-09 14:53:35 -05003997 int status = -ENOMEM, i;
Olga Kornievskaia1976b2b2022-01-12 10:27:38 -05003998
3999 cred = ops->get_state_renewal_cred(clp);
4000 if (cred == NULL) {
4001 cred = nfs4_get_clid_cred(clp);
4002 if (cred == NULL)
4003 return -ENOKEY;
4004 }
4005
4006 page = alloc_page(GFP_KERNEL);
4007 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
4008 if (page == NULL || locations == NULL)
4009 goto out;
4010
4011 status = nfs4_proc_get_locations(server, fhandle, locations, page,
4012 cred);
4013 if (status)
4014 goto out;
Olga Kornievskaia4ca9f312021-12-09 14:53:35 -05004015
4016 for (i = 0; i < locations->nlocations; i++)
4017 test_fs_location_for_trunking(&locations->locations[i], clp,
4018 server);
Olga Kornievskaia1976b2b2022-01-12 10:27:38 -05004019out:
4020 if (page)
4021 __free_page(page);
4022 kfree(locations);
4023 return status;
4024}
4025
4026static int nfs4_discover_trunking(struct nfs_server *server,
4027 struct nfs_fh *fhandle)
4028{
4029 struct nfs4_exception exception = {
4030 .interruptible = true,
4031 };
4032 struct nfs_client *clp = server->nfs_client;
4033 int err = 0;
4034
4035 if (!nfs4_has_session(clp))
4036 goto out;
4037 do {
4038 err = nfs4_handle_exception(server,
4039 _nfs4_discover_trunking(server, fhandle),
4040 &exception);
4041 } while (exception.retry);
4042out:
4043 return err;
4044}
4045
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
4047 struct nfs_fsinfo *info)
4048{
David Quigleyaa9c2662013-05-22 12:50:44 -04004049 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04004051 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052 };
4053 struct nfs4_lookup_res res = {
4054 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04004055 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056 .fh = fhandle,
4057 };
4058 struct rpc_message msg = {
4059 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
4060 .rpc_argp = &args,
4061 .rpc_resp = &res,
4062 };
Benny Halevy008f55d2009-04-01 09:22:50 -04004063
David Quigleyaa9c2662013-05-22 12:50:44 -04004064 bitmask[0] = nfs4_fattr_bitmap[0];
4065 bitmask[1] = nfs4_fattr_bitmap[1];
4066 /*
4067 * Process the label in the upcoming getfattr
4068 */
4069 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
4070
Trond Myklebust0e574af2005-10-27 22:12:38 -04004071 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004072 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073}
4074
4075static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
4076 struct nfs_fsinfo *info)
4077{
Trond Myklebust0688e642019-04-07 13:59:09 -04004078 struct nfs4_exception exception = {
4079 .interruptible = true,
4080 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081 int err;
4082 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04004083 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004084 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04004085 switch (err) {
4086 case 0:
4087 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04004088 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04004089 default:
4090 err = nfs4_handle_exception(server, err, &exception);
4091 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04004093out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094 return err;
4095}
4096
Bryan Schumaker8f70e952011-03-24 17:12:31 +00004097static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
4098 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
4099{
Trond Myklebustc2190662013-08-26 19:23:04 -04004100 struct rpc_auth_create_args auth_args = {
4101 .pseudoflavor = flavor,
4102 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00004103 struct rpc_auth *auth;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00004104
Trond Myklebustc2190662013-08-26 19:23:04 -04004105 auth = rpcauth_create(&auth_args, server->client);
Anna Schumaker9df13362017-01-11 16:30:08 -05004106 if (IS_ERR(auth))
4107 return -EACCES;
4108 return nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00004109}
4110
Chuck Lever9a744ba2013-03-16 15:56:02 -04004111/*
4112 * Retry pseudoroot lookup with various security flavors. We do this when:
4113 *
4114 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
4115 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
4116 *
4117 * Returns zero on success, or a negative NFS4ERR value, or a
4118 * negative errno value.
4119 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04004120static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04004121 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122{
Chuck Lever9a744ba2013-03-16 15:56:02 -04004123 /* Per 3530bis 15.33.5 */
4124 static const rpc_authflavor_t flav_array[] = {
4125 RPC_AUTH_GSS_KRB5P,
4126 RPC_AUTH_GSS_KRB5I,
4127 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04004128 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04004129 RPC_AUTH_NULL,
4130 };
4131 int status = -EPERM;
4132 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04004134 if (server->auth_info.flavor_len > 0) {
4135 /* try each flavor specified by user */
4136 for (i = 0; i < server->auth_info.flavor_len; i++) {
4137 status = nfs4_lookup_root_sec(server, fhandle, info,
4138 server->auth_info.flavors[i]);
4139 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
4140 continue;
4141 break;
4142 }
4143 } else {
4144 /* no flavors specified by user, try default list */
4145 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
4146 status = nfs4_lookup_root_sec(server, fhandle, info,
4147 flav_array[i]);
4148 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
4149 continue;
4150 break;
4151 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00004152 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04004153
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04004154 /*
Colin Ian Kingd3787af2018-10-26 19:10:31 +01004155 * -EACCES could mean that the user doesn't have correct permissions
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04004156 * to access the mount. It could also mean that we tried to mount
4157 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
4158 * existing mount programs don't handle -EACCES very well so it should
4159 * be mapped to -EPERM instead.
4160 */
4161 if (status == -EACCES)
4162 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04004163 return status;
4164}
4165
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04004166/**
4167 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
4168 * @server: initialized nfs_server handle
4169 * @fhandle: we fill in the pseudo-fs root file handle
4170 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04004171 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04004172 *
4173 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04004174 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04004175int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04004176 struct nfs_fsinfo *info,
4177 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04004178{
Andre Przywarac7757072015-04-23 17:17:40 +01004179 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04004180
Andre Przywarac7757072015-04-23 17:17:40 +01004181 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04004182 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01004183
4184 if (auth_probe || status == NFS4ERR_WRONGSEC)
Trond Myklebust698c9372016-07-25 13:31:14 -04004185 status = server->nfs_client->cl_mvops->find_root_sec(server,
4186 fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04004187
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188 if (status == 0)
4189 status = nfs4_server_capabilities(server, fhandle);
4190 if (status == 0)
4191 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04004192
Trond Myklebustc12e87f2006-03-13 21:20:47 -08004193 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194}
4195
Bryan Schumakerbae36242012-05-10 15:07:31 -04004196static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
4197 struct nfs_fsinfo *info)
4198{
4199 int error;
4200 struct nfs_fattr *fattr = info->fattr;
4201
4202 error = nfs4_server_capabilities(server, mntfh);
4203 if (error < 0) {
4204 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
4205 return error;
4206 }
4207
Anna Schumaker2ef61e02021-10-22 13:11:07 -04004208 error = nfs4_proc_getattr(server, mntfh, fattr, NULL);
Bryan Schumakerbae36242012-05-10 15:07:31 -04004209 if (error < 0) {
4210 dprintk("nfs4_get_root: getattr error = %d\n", -error);
Scott Mayhew779df6a2020-03-03 17:58:37 -05004211 goto out;
Bryan Schumakerbae36242012-05-10 15:07:31 -04004212 }
4213
4214 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
4215 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
4216 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
4217
Scott Mayhew779df6a2020-03-03 17:58:37 -05004218out:
Bryan Schumakerbae36242012-05-10 15:07:31 -04004219 return error;
4220}
4221
Manoj Naik6b97fd32006-06-09 09:34:29 -04004222/*
4223 * Get locations and (maybe) other attributes of a referral.
4224 * Note that we'll actually follow the referral later when
4225 * we detect fsid mismatch in inode revalidation
4226 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004227static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
4228 const struct qstr *name, struct nfs_fattr *fattr,
4229 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04004230{
4231 int status = -ENOMEM;
4232 struct page *page = NULL;
4233 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04004234
4235 page = alloc_page(GFP_KERNEL);
4236 if (page == NULL)
4237 goto out;
4238 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
4239 if (locations == NULL)
4240 goto out;
4241
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004242 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04004243 if (status != 0)
4244 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04004245
4246 /*
4247 * If the fsid didn't change, this is a migration event, not a
4248 * referral. Cause us to drop into the exception handler, which
4249 * will kick off migration recovery.
4250 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04004251 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04004252 dprintk("%s: server did not return a different fsid for"
4253 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04004254 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04004255 goto out;
4256 }
Andy Adamson533eb462011-06-13 18:25:56 -04004257 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
4258 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04004259
Andy Adamson533eb462011-06-13 18:25:56 -04004260 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04004261 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04004262 memset(fhandle, 0, sizeof(struct nfs_fh));
4263out:
4264 if (page)
4265 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04004266 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04004267 return status;
4268}
4269
David Quigley1775fd32013-05-22 12:50:42 -04004270static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
Anna Schumaker2ef61e02021-10-22 13:11:07 -04004271 struct nfs_fattr *fattr, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272{
Trond Myklebust771734f2018-04-07 13:54:23 -04004273 __u32 bitmask[NFS4_BITMASK_SZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274 struct nfs4_getattr_arg args = {
4275 .fh = fhandle,
Trond Myklebust771734f2018-04-07 13:54:23 -04004276 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277 };
4278 struct nfs4_getattr_res res = {
4279 .fattr = fattr,
4280 .server = server,
4281 };
4282 struct rpc_message msg = {
4283 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
4284 .rpc_argp = &args,
4285 .rpc_resp = &res,
4286 };
Trond Myklebustc74dfe92020-01-06 15:39:37 -05004287 unsigned short task_flags = 0;
4288
Olga Kornievskaia85e39fe2021-06-23 23:28:51 -04004289 if (nfs4_has_session(server->nfs_client))
4290 task_flags = RPC_TASK_MOVEABLE;
4291
Trond Myklebustc74dfe92020-01-06 15:39:37 -05004292 /* Is this is an attribute revalidation, subject to softreval? */
4293 if (inode && (server->flags & NFS_MOUNT_SOFTREVAL))
4294 task_flags |= RPC_TASK_TIMEOUT;
David Quigleyaa9c2662013-05-22 12:50:44 -04004295
Anna Schumaker2ef61e02021-10-22 13:11:07 -04004296 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, fattr->label), inode, 0);
Trond Myklebust0e574af2005-10-27 22:12:38 -04004297 nfs_fattr_init(fattr);
Trond Myklebustc74dfe92020-01-06 15:39:37 -05004298 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
4299 return nfs4_do_call_sync(server->client, server, &msg,
4300 &args.seq_args, &res.seq_res, task_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301}
4302
Olga Kornievskaiaec4b0922019-10-08 16:33:53 -04004303int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
Anna Schumaker2ef61e02021-10-22 13:11:07 -04004304 struct nfs_fattr *fattr, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305{
Trond Myklebust0688e642019-04-07 13:59:09 -04004306 struct nfs4_exception exception = {
4307 .interruptible = true,
4308 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309 int err;
4310 do {
Anna Schumaker2ef61e02021-10-22 13:11:07 -04004311 err = _nfs4_proc_getattr(server, fhandle, fattr, inode);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004312 trace_nfs4_getattr(server, fhandle, fattr, err);
4313 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314 &exception);
4315 } while (exception.retry);
4316 return err;
4317}
4318
4319/*
4320 * The file is not closed if it is opened due to the a request to change
4321 * the size of the file. The open call will not be needed once the
4322 * VFS layer lookup-intents are implemented.
4323 *
4324 * Close is called when the inode is destroyed.
4325 * If we haven't opened the file for O_WRONLY, we
4326 * need to in the size_change case to obtain a stateid.
4327 *
4328 * Got race?
4329 * Because OPEN is always done by name in nfsv4, it is
4330 * possible that we opened a different file by the same
4331 * name. We can recognize this race condition, but we
4332 * can't do anything about it besides returning an error.
4333 *
4334 * This will be fixed with VFS changes (lookup-intent).
4335 */
4336static int
4337nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
4338 struct iattr *sattr)
4339{
David Howells2b0143b2015-03-17 22:25:59 +00004340 struct inode *inode = d_inode(dentry);
NeilBrowna52458b2018-12-03 11:30:31 +11004341 const struct cred *cred = NULL;
NeilBrown29b59f92016-10-13 15:26:47 +11004342 struct nfs_open_context *ctx = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343 int status;
4344
Peng Tao88ac8152014-09-12 11:04:10 +08004345 if (pnfs_ld_layoutret_on_setattr(inode) &&
4346 sattr->ia_valid & ATTR_SIZE &&
4347 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04004348 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04004349
Trond Myklebust0e574af2005-10-27 22:12:38 -04004350 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351
Andy Adamson26699402012-05-30 16:12:24 -04004352 /* Deal with open(O_TRUNC) */
4353 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03004354 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04004355
4356 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03004357 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04004358 return 0;
4359
Trond Myklebustd5308382005-11-04 15:33:38 -05004360 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04004361 if (sattr->ia_valid & ATTR_FILE) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00004362
Trond Myklebust659bfcd2008-06-10 19:39:41 -04004363 ctx = nfs_file_open_context(sattr->ia_file);
NeilBrown29b59f92016-10-13 15:26:47 +11004364 if (ctx)
Neil Brown504e5182008-10-16 14:15:16 +11004365 cred = ctx->cred;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04004366 }
4367
Trond Myklebust199366f2018-03-20 16:43:18 -04004368 /* Return any delegations if we're going to change ACLs */
4369 if ((sattr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
Trond Myklebustc01d3642018-03-20 16:43:20 -04004370 nfs4_inode_make_writeable(inode);
Trond Myklebust199366f2018-03-20 16:43:18 -04004371
Anna Schumaker1b00ad62021-10-22 13:11:08 -04004372 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL);
David Quigleyaa9c2662013-05-22 12:50:44 -04004373 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05004374 nfs_setattr_update_inode(inode, sattr, fattr);
Anna Schumakerdd225cb2021-10-22 13:11:12 -04004375 nfs_setsecurity(inode, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04004376 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377 return status;
4378}
4379
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004380static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004381 struct dentry *dentry, struct nfs_fh *fhandle,
Anna Schumaker9558a002021-10-22 13:11:04 -04004382 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04004383{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004384 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04004385 int status;
4386 struct nfs4_lookup_arg args = {
4387 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004388 .dir_fh = NFS_FH(dir),
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004389 .name = &dentry->d_name,
David Howells2b3de442006-08-22 20:06:09 -04004390 };
4391 struct nfs4_lookup_res res = {
4392 .server = server,
4393 .fattr = fattr,
4394 .fh = fhandle,
4395 };
4396 struct rpc_message msg = {
4397 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
4398 .rpc_argp = &args,
4399 .rpc_resp = &res,
4400 };
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004401 unsigned short task_flags = 0;
4402
Olga Kornievskaia85e39fe2021-06-23 23:28:51 -04004403 if (server->nfs_client->cl_minorversion)
4404 task_flags = RPC_TASK_MOVEABLE;
4405
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004406 /* Is this is an attribute revalidation, subject to softreval? */
4407 if (nfs_lookup_is_soft_revalidate(dentry))
4408 task_flags |= RPC_TASK_TIMEOUT;
David Howells2b3de442006-08-22 20:06:09 -04004409
Anna Schumaker9558a002021-10-22 13:11:04 -04004410 args.bitmask = nfs4_bitmask(server, fattr->label);
David Quigleyaa9c2662013-05-22 12:50:44 -04004411
David Howells2b3de442006-08-22 20:06:09 -04004412 nfs_fattr_init(fattr);
4413
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004414 dprintk("NFS call lookup %pd2\n", dentry);
4415 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
4416 status = nfs4_do_call_sync(clnt, server, &msg,
4417 &args.seq_args, &res.seq_res, task_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418 dprintk("NFS reply lookup: %d\n", status);
4419 return status;
4420}
4421
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004422static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00004423{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00004424 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004425 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00004426 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4427 fattr->nlink = 2;
4428}
4429
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004430static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004431 struct dentry *dentry, struct nfs_fh *fhandle,
Anna Schumaker9558a002021-10-22 13:11:04 -04004432 struct nfs_fattr *fattr)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004433{
Trond Myklebust0688e642019-04-07 13:59:09 -04004434 struct nfs4_exception exception = {
4435 .interruptible = true,
4436 };
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004437 struct rpc_clnt *client = *clnt;
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004438 const struct qstr *name = &dentry->d_name;
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004439 int err;
4440 do {
Anna Schumaker9558a002021-10-22 13:11:04 -04004441 err = _nfs4_proc_lookup(client, dir, dentry, fhandle, fattr);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004442 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004443 switch (err) {
4444 case -NFS4ERR_BADNAME:
4445 err = -ENOENT;
4446 goto out;
4447 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004448 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02004449 if (err == -NFS4ERR_MOVED)
4450 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004451 goto out;
4452 case -NFS4ERR_WRONGSEC:
4453 err = -EPERM;
4454 if (client != *clnt)
4455 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04004456 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004457 if (IS_ERR(client))
4458 return PTR_ERR(client);
4459
4460 exception.retry = 1;
4461 break;
4462 default:
4463 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4464 }
4465 } while (exception.retry);
4466
4467out:
4468 if (err == 0)
4469 *clnt = client;
4470 else if (client != *clnt)
4471 rpc_shutdown_client(client);
4472
4473 return err;
4474}
4475
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004476static int nfs4_proc_lookup(struct inode *dir, struct dentry *dentry,
Anna Schumaker9558a002021-10-22 13:11:04 -04004477 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004479 int status;
4480 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004481
Anna Schumaker9558a002021-10-22 13:11:04 -04004482 status = nfs4_proc_lookup_common(&client, dir, dentry, fhandle, fattr);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004483 if (client != NFS_CLIENT(dir)) {
4484 rpc_shutdown_client(client);
4485 nfs_fixup_secinfo_attributes(fattr);
4486 }
4487 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488}
4489
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004490struct rpc_clnt *
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004491nfs4_proc_lookup_mountpoint(struct inode *dir, struct dentry *dentry,
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004492 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4493{
Trond Myklebustb72888c2013-08-07 20:38:07 -04004494 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004495 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004496
Anna Schumaker9558a002021-10-22 13:11:04 -04004497 status = nfs4_proc_lookup_common(&client, dir, dentry, fhandle, fattr);
Trond Myklebustb72888c2013-08-07 20:38:07 -04004498 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004499 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04004500 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004501}
4502
Jeff Layton5b5faaf2017-06-29 06:34:52 -07004503static int _nfs4_proc_lookupp(struct inode *inode,
Anna Schumakerba4bc8d2021-10-22 13:11:05 -04004504 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Jeff Layton5b5faaf2017-06-29 06:34:52 -07004505{
4506 struct rpc_clnt *clnt = NFS_CLIENT(inode);
4507 struct nfs_server *server = NFS_SERVER(inode);
4508 int status;
4509 struct nfs4_lookupp_arg args = {
4510 .bitmask = server->attr_bitmask,
4511 .fh = NFS_FH(inode),
4512 };
4513 struct nfs4_lookupp_res res = {
4514 .server = server,
4515 .fattr = fattr,
Jeff Layton5b5faaf2017-06-29 06:34:52 -07004516 .fh = fhandle,
4517 };
4518 struct rpc_message msg = {
4519 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
4520 .rpc_argp = &args,
4521 .rpc_resp = &res,
4522 };
Trond Myklebust76998eb2020-10-20 14:30:35 -04004523 unsigned short task_flags = 0;
4524
4525 if (NFS_SERVER(inode)->flags & NFS_MOUNT_SOFTREVAL)
4526 task_flags |= RPC_TASK_TIMEOUT;
Jeff Layton5b5faaf2017-06-29 06:34:52 -07004527
Anna Schumakerba4bc8d2021-10-22 13:11:05 -04004528 args.bitmask = nfs4_bitmask(server, fattr->label);
Jeff Layton5b5faaf2017-06-29 06:34:52 -07004529
4530 nfs_fattr_init(fattr);
4531
4532 dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino);
4533 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
Trond Myklebust76998eb2020-10-20 14:30:35 -04004534 &res.seq_res, task_flags);
Jeff Layton5b5faaf2017-06-29 06:34:52 -07004535 dprintk("NFS reply lookupp: %d\n", status);
4536 return status;
4537}
4538
4539static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
Anna Schumakerba4bc8d2021-10-22 13:11:05 -04004540 struct nfs_fattr *fattr)
Jeff Layton5b5faaf2017-06-29 06:34:52 -07004541{
Trond Myklebust0688e642019-04-07 13:59:09 -04004542 struct nfs4_exception exception = {
4543 .interruptible = true,
4544 };
Jeff Layton5b5faaf2017-06-29 06:34:52 -07004545 int err;
4546 do {
Anna Schumakerba4bc8d2021-10-22 13:11:05 -04004547 err = _nfs4_proc_lookupp(inode, fhandle, fattr);
Jeff Layton5b5faaf2017-06-29 06:34:52 -07004548 trace_nfs4_lookupp(inode, err);
4549 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4550 &exception);
4551 } while (exception.retry);
4552 return err;
4553}
4554
NeilBrown73fbb3f2021-09-28 09:47:57 +10004555static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry,
4556 const struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557{
Trond Myklebust76b32992007-08-10 17:45:11 -04004558 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559 struct nfs4_accessargs args = {
4560 .fh = NFS_FH(inode),
Anna Schumaker1750d922017-07-26 12:00:21 -04004561 .access = entry->mask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562 };
Trond Myklebust76b32992007-08-10 17:45:11 -04004563 struct nfs4_accessres res = {
4564 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04004565 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566 struct rpc_message msg = {
4567 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
4568 .rpc_argp = &args,
4569 .rpc_resp = &res,
NeilBrown73fbb3f2021-09-28 09:47:57 +10004570 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004571 };
David Quigleyaa9c2662013-05-22 12:50:44 -04004572 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004573
Trond Myklebust7c672652018-06-04 15:00:53 -04004574 if (!nfs4_have_delegation(inode, FMODE_READ)) {
Trond Myklebust8bcbe7d2018-03-20 17:03:11 -04004575 res.fattr = nfs_alloc_fattr();
4576 if (res.fattr == NULL)
4577 return -ENOMEM;
4578 args.bitmask = server->cache_consistency_bitmask;
4579 }
Bryan Schumaker7c513052011-03-24 17:12:24 +00004580 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04004582 nfs_access_set_mask(entry, res.access);
Trond Myklebust8bcbe7d2018-03-20 17:03:11 -04004583 if (res.fattr)
4584 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585 }
Trond Myklebustc407d412010-04-16 16:22:48 -04004586 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004587 return status;
4588}
4589
NeilBrown73fbb3f2021-09-28 09:47:57 +10004590static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry,
4591 const struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592{
Trond Myklebust0688e642019-04-07 13:59:09 -04004593 struct nfs4_exception exception = {
4594 .interruptible = true,
4595 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596 int err;
4597 do {
NeilBrown73fbb3f2021-09-28 09:47:57 +10004598 err = _nfs4_proc_access(inode, entry, cred);
Trond Myklebustc1578b72013-08-12 16:58:42 -04004599 trace_nfs4_access(inode, err);
4600 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601 &exception);
4602 } while (exception.retry);
4603 return err;
4604}
4605
4606/*
4607 * TODO: For the time being, we don't try to get any attributes
4608 * along with any of the zero-copy operations READ, READDIR,
4609 * READLINK, WRITE.
4610 *
4611 * In the case of the first three, we want to put the GETATTR
4612 * after the read-type operation -- this is because it is hard
4613 * to predict the length of a GETATTR response in v4, and thus
4614 * align the READ data correctly. This means that the GETATTR
4615 * may end up partially falling into the page cache, and we should
4616 * shift it into the 'tail' of the xdr_buf before processing.
4617 * To do this efficiently, we need to know the total length
4618 * of data received, which doesn't seem to be available outside
4619 * of the RPC layer.
4620 *
4621 * In the case of WRITE, we also want to put the GETATTR after
4622 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04004623 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624 *
4625 * Both of these changes to the XDR layer would in fact be quite
4626 * minor, but I decided to leave them for a subsequent patch.
4627 */
4628static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4629 unsigned int pgbase, unsigned int pglen)
4630{
4631 struct nfs4_readlink args = {
4632 .fh = NFS_FH(inode),
4633 .pgbase = pgbase,
4634 .pglen = pglen,
4635 .pages = &page,
4636 };
Benny Halevyf50c7002009-04-01 09:21:55 -04004637 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004638 struct rpc_message msg = {
4639 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4640 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04004641 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642 };
4643
Bryan Schumaker7c513052011-03-24 17:12:24 +00004644 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004645}
4646
4647static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4648 unsigned int pgbase, unsigned int pglen)
4649{
Trond Myklebust0688e642019-04-07 13:59:09 -04004650 struct nfs4_exception exception = {
4651 .interruptible = true,
4652 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653 int err;
4654 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004655 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4656 trace_nfs4_readlink(inode, err);
4657 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658 &exception);
4659 } while (exception.retry);
4660 return err;
4661}
4662
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004664 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666static int
4667nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004668 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004670 struct nfs_server *server = NFS_SERVER(dir);
David Quigleyaa9c2662013-05-22 12:50:44 -04004671 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004672 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674 int status = 0;
4675
NeilBrown532d4de2016-10-13 15:26:47 +11004676 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004677 if (IS_ERR(ctx))
4678 return PTR_ERR(ctx);
4679
David Quigleyaa9c2662013-05-22 12:50:44 -04004680 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4681
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004682 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4683 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08004684 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685 if (IS_ERR(state)) {
4686 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04004687 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689out:
David Quigleyaa9c2662013-05-22 12:50:44 -04004690 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004691 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004692 return status;
4693}
4694
Trond Myklebust3c591172018-07-31 15:54:10 -04004695static int
4696_nfs4_proc_remove(struct inode *dir, const struct qstr *name, u32 ftype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697{
Trond Myklebust16e42952005-10-27 22:12:44 -04004698 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004699 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07004701 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004703 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04004704 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04004705 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004707 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4708 .rpc_argp = &args,
4709 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710 };
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004711 unsigned long timestamp = jiffies;
Trond Myklebust778d2812012-04-27 13:48:19 -04004712 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04004713
Bryan Schumaker7c513052011-03-24 17:12:24 +00004714 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust3c591172018-07-31 15:54:10 -04004715 if (status == 0) {
4716 spin_lock(&dir->i_lock);
Trond Myklebust3c591172018-07-31 15:54:10 -04004717 /* Removing a directory decrements nlink in the parent */
4718 if (ftype == NF4DIR && dir->i_nlink > 2)
4719 nfs4_dec_nlink_locked(dir);
Trond Myklebust82eae5a2021-04-01 14:59:59 -04004720 nfs4_update_changeattr_locked(dir, &res.cinfo, timestamp,
4721 NFS_INO_INVALID_DATA);
Trond Myklebust3c591172018-07-31 15:54:10 -04004722 spin_unlock(&dir->i_lock);
4723 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004724 return status;
4725}
4726
Trond Myklebust912678d2018-03-20 16:43:15 -04004727static int nfs4_proc_remove(struct inode *dir, struct dentry *dentry)
4728{
Trond Myklebust0688e642019-04-07 13:59:09 -04004729 struct nfs4_exception exception = {
4730 .interruptible = true,
4731 };
Trond Myklebust912678d2018-03-20 16:43:15 -04004732 struct inode *inode = d_inode(dentry);
4733 int err;
4734
Trond Myklebustc01d3642018-03-20 16:43:20 -04004735 if (inode) {
4736 if (inode->i_nlink == 1)
4737 nfs4_inode_return_delegation(inode);
4738 else
4739 nfs4_inode_make_writeable(inode);
4740 }
Trond Myklebust912678d2018-03-20 16:43:15 -04004741 do {
Trond Myklebust3c591172018-07-31 15:54:10 -04004742 err = _nfs4_proc_remove(dir, &dentry->d_name, NF4REG);
Trond Myklebust912678d2018-03-20 16:43:15 -04004743 trace_nfs4_remove(dir, &dentry->d_name, err);
4744 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4745 &exception);
4746 } while (exception.retry);
4747 return err;
4748}
4749
4750static int nfs4_proc_rmdir(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004751{
Trond Myklebust0688e642019-04-07 13:59:09 -04004752 struct nfs4_exception exception = {
4753 .interruptible = true,
4754 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755 int err;
Trond Myklebust912678d2018-03-20 16:43:15 -04004756
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757 do {
Trond Myklebust3c591172018-07-31 15:54:10 -04004758 err = _nfs4_proc_remove(dir, name, NF4DIR);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004759 trace_nfs4_remove(dir, name, err);
4760 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004761 &exception);
4762 } while (exception.retry);
4763 return err;
4764}
4765
Trond Myklebusted7e9ad2018-05-30 16:11:52 -04004766static void nfs4_proc_unlink_setup(struct rpc_message *msg,
4767 struct dentry *dentry,
4768 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004770 struct nfs_removeargs *args = msg->rpc_argp;
4771 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772
Trond Myklebust977fcc22018-03-20 16:43:17 -04004773 res->server = NFS_SB(dentry->d_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Anna Schumakerfba83f32018-05-04 16:22:50 -04004775 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04004776
4777 nfs_fattr_init(res->dir_attr);
Trond Myklebust977fcc22018-03-20 16:43:17 -04004778
Trond Myklebust00bdadc2021-12-17 15:36:57 -05004779 if (inode) {
Trond Myklebust977fcc22018-03-20 16:43:17 -04004780 nfs4_inode_return_delegation(inode);
Trond Myklebust00bdadc2021-12-17 15:36:57 -05004781 nfs_d_prune_case_insensitive_aliases(inode);
4782 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783}
4784
Bryan Schumaker34e137c2012-03-19 14:54:41 -04004785static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4786{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004787 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004788 &data->args.seq_args,
4789 &data->res.seq_res,
4790 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791}
4792
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004793static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004795 struct nfs_unlinkdata *data = task->tk_calldata;
4796 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004797
Trond Myklebust14516c32010-07-31 14:29:06 -04004798 if (!nfs4_sequence_done(task, &res->seq_res))
4799 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004800 if (nfs4_async_handle_error(task, res->server, NULL,
4801 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004802 return 0;
Trond Myklebustc40d52f2017-01-11 12:36:11 -05004803 if (task->tk_status == 0)
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004804 nfs4_update_changeattr(dir, &res->cinfo,
4805 res->dir_attr->time_start,
4806 NFS_INO_INVALID_DATA);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004807 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004808}
4809
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004810static void nfs4_proc_rename_setup(struct rpc_message *msg,
4811 struct dentry *old_dentry,
4812 struct dentry *new_dentry)
Jeff Laytond3d41522010-09-17 17:31:57 -04004813{
Jeff Laytond3d41522010-09-17 17:31:57 -04004814 struct nfs_renameargs *arg = msg->rpc_argp;
4815 struct nfs_renameres *res = msg->rpc_resp;
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004816 struct inode *old_inode = d_inode(old_dentry);
4817 struct inode *new_inode = d_inode(new_dentry);
Jeff Laytond3d41522010-09-17 17:31:57 -04004818
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004819 if (old_inode)
Trond Myklebustc01d3642018-03-20 16:43:20 -04004820 nfs4_inode_make_writeable(old_inode);
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004821 if (new_inode)
4822 nfs4_inode_return_delegation(new_inode);
Jeff Laytond3d41522010-09-17 17:31:57 -04004823 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004824 res->server = NFS_SB(old_dentry->d_sb);
Anna Schumakerfba83f32018-05-04 16:22:50 -04004825 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1, 0);
Jeff Laytond3d41522010-09-17 17:31:57 -04004826}
4827
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04004828static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4829{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004830 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004831 &data->args.seq_args,
4832 &data->res.seq_res,
4833 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04004834}
4835
4836static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4837 struct inode *new_dir)
4838{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04004839 struct nfs_renamedata *data = task->tk_calldata;
4840 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04004841
4842 if (!nfs4_sequence_done(task, &res->seq_res))
4843 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004844 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04004845 return 0;
4846
Trond Myklebustc733c492017-01-11 12:32:26 -05004847 if (task->tk_status == 0) {
Trond Myklebust00bdadc2021-12-17 15:36:57 -05004848 nfs_d_prune_case_insensitive_aliases(d_inode(data->old_dentry));
Trond Myklebust5636ec42018-07-31 15:54:11 -04004849 if (new_dir != old_dir) {
4850 /* Note: If we moved a directory, nlink will change */
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004851 nfs4_update_changeattr(old_dir, &res->old_cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04004852 res->old_fattr->time_start,
Trond Myklebustfabf2b32021-03-25 13:14:42 -04004853 NFS_INO_INVALID_NLINK |
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004854 NFS_INO_INVALID_DATA);
4855 nfs4_update_changeattr(new_dir, &res->new_cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04004856 res->new_fattr->time_start,
Trond Myklebustfabf2b32021-03-25 13:14:42 -04004857 NFS_INO_INVALID_NLINK |
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004858 NFS_INO_INVALID_DATA);
Trond Myklebust5636ec42018-07-31 15:54:11 -04004859 } else
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004860 nfs4_update_changeattr(old_dir, &res->old_cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04004861 res->old_fattr->time_start,
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004862 NFS_INO_INVALID_DATA);
Trond Myklebustc733c492017-01-11 12:32:26 -05004863 }
Jeff Laytond3d41522010-09-17 17:31:57 -04004864 return 1;
4865}
4866
Al Virobeffb8f2016-07-20 16:34:42 -04004867static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004868{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004869 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebust2f28dc32018-04-08 21:06:40 -04004870 __u32 bitmask[NFS4_BITMASK_SZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871 struct nfs4_link_arg arg = {
4872 .fh = NFS_FH(inode),
4873 .dir_fh = NFS_FH(dir),
4874 .name = name,
Trond Myklebust2f28dc32018-04-08 21:06:40 -04004875 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004877 struct nfs4_link_res res = {
4878 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004879 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880 struct rpc_message msg = {
4881 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4882 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004883 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004884 };
Trond Myklebust136f2622010-04-16 16:22:49 -04004885 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004886
Anna Schumakeraa7ca3b2021-10-22 13:11:03 -04004887 res.fattr = nfs_alloc_fattr_with_label(server);
Trond Myklebust778d2812012-04-27 13:48:19 -04004888 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04004889 goto out;
4890
Trond Myklebustc01d3642018-03-20 16:43:20 -04004891 nfs4_inode_make_writeable(inode);
Anna Schumakeraa7ca3b2021-10-22 13:11:03 -04004892 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, res.fattr->label), inode,
Trond Myklebusta71029b2021-04-10 00:23:03 -04004893 NFS_INO_INVALID_CHANGE);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004894 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004895 if (!status) {
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004896 nfs4_update_changeattr(dir, &res.cinfo, res.fattr->time_start,
4897 NFS_INO_INVALID_DATA);
Trond Myklebust1301e422021-04-01 14:57:56 -04004898 nfs4_inc_nlink(inode);
David Quigleyaa9c2662013-05-22 12:50:44 -04004899 status = nfs_post_op_update_inode(inode, res.fattr);
4900 if (!status)
Anna Schumakerdd225cb2021-10-22 13:11:12 -04004901 nfs_setsecurity(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004902 }
David Quigley14c43f72013-05-22 12:50:43 -04004903
Trond Myklebust136f2622010-04-16 16:22:49 -04004904out:
Trond Myklebust136f2622010-04-16 16:22:49 -04004905 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004906 return status;
4907}
4908
Al Virobeffb8f2016-07-20 16:34:42 -04004909static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004910{
Trond Myklebust0688e642019-04-07 13:59:09 -04004911 struct nfs4_exception exception = {
4912 .interruptible = true,
4913 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914 int err;
4915 do {
4916 err = nfs4_handle_exception(NFS_SERVER(inode),
4917 _nfs4_proc_link(inode, dir, name),
4918 &exception);
4919 } while (exception.retry);
4920 return err;
4921}
4922
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004923struct nfs4_createdata {
4924 struct rpc_message msg;
4925 struct nfs4_create_arg arg;
4926 struct nfs4_create_res res;
4927 struct nfs_fh fh;
4928 struct nfs_fattr fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004929};
4930
4931static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004932 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004933{
4934 struct nfs4_createdata *data;
4935
4936 data = kzalloc(sizeof(*data), GFP_KERNEL);
4937 if (data != NULL) {
4938 struct nfs_server *server = NFS_SERVER(dir);
4939
Anna Schumaker68be1742021-10-22 13:11:02 -04004940 data->fattr.label = nfs4_label_alloc(server, GFP_KERNEL);
4941 if (IS_ERR(data->fattr.label))
David Quigley14c43f72013-05-22 12:50:43 -04004942 goto out_free;
4943
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004944 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4945 data->msg.rpc_argp = &data->arg;
4946 data->msg.rpc_resp = &data->res;
4947 data->arg.dir_fh = NFS_FH(dir);
4948 data->arg.server = server;
4949 data->arg.name = name;
4950 data->arg.attrs = sattr;
4951 data->arg.ftype = ftype;
Anna Schumaker68be1742021-10-22 13:11:02 -04004952 data->arg.bitmask = nfs4_bitmask(server, data->fattr.label);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004953 data->arg.umask = current_umask();
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004954 data->res.server = server;
4955 data->res.fh = &data->fh;
4956 data->res.fattr = &data->fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004957 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004958 }
4959 return data;
David Quigley14c43f72013-05-22 12:50:43 -04004960out_free:
4961 kfree(data);
4962 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004963}
4964
4965static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4966{
Bryan Schumaker7c513052011-03-24 17:12:24 +00004967 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00004968 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004969 if (status == 0) {
Trond Myklebust3c591172018-07-31 15:54:10 -04004970 spin_lock(&dir->i_lock);
Trond Myklebust3c591172018-07-31 15:54:10 -04004971 /* Creating a directory bumps nlink in the parent */
4972 if (data->arg.ftype == NF4DIR)
4973 nfs4_inc_nlink_locked(dir);
Trond Myklebust82eae5a2021-04-01 14:59:59 -04004974 nfs4_update_changeattr_locked(dir, &data->res.dir_cinfo,
4975 data->res.fattr->time_start,
4976 NFS_INO_INVALID_DATA);
Trond Myklebust3c591172018-07-31 15:54:10 -04004977 spin_unlock(&dir->i_lock);
Anna Schumakerd91bfc42021-10-22 13:11:09 -04004978 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004979 }
4980 return status;
4981}
4982
4983static void nfs4_free_createdata(struct nfs4_createdata *data)
4984{
Anna Schumaker68be1742021-10-22 13:11:02 -04004985 nfs4_label_free(data->fattr.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004986 kfree(data);
4987}
4988
Chuck Lever4f390c12006-08-22 20:06:22 -04004989static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004990 struct page *page, unsigned int len, struct iattr *sattr,
4991 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004992{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004993 struct nfs4_createdata *data;
4994 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995
Chuck Lever94a6d752006-08-22 20:06:23 -04004996 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004997 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04004998
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004999 status = -ENOMEM;
5000 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
5001 if (data == NULL)
5002 goto out;
5003
5004 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
5005 data->arg.u.symlink.pages = &page;
5006 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04005007 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005008
Trond Myklebust57dc9a52008-06-20 15:35:32 -04005009 status = nfs4_do_create(dir, dentry, data);
5010
5011 nfs4_free_createdata(data);
5012out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005013 return status;
5014}
5015
Chuck Lever4f390c12006-08-22 20:06:22 -04005016static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04005017 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005018{
Trond Myklebust0688e642019-04-07 13:59:09 -04005019 struct nfs4_exception exception = {
5020 .interruptible = true,
5021 };
David Quigleyaa9c2662013-05-22 12:50:44 -04005022 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04005024
5025 label = nfs4_label_init_security(dir, dentry, sattr, &l);
5026
Linus Torvalds1da177e2005-04-16 15:20:36 -07005027 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04005028 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
5029 trace_nfs4_symlink(dir, &dentry->d_name, err);
5030 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005031 &exception);
5032 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04005033
5034 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005035 return err;
5036}
5037
5038static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04005039 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005040{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04005041 struct nfs4_createdata *data;
5042 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005043
Trond Myklebust57dc9a52008-06-20 15:35:32 -04005044 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
5045 if (data == NULL)
5046 goto out;
5047
David Quigley1775fd32013-05-22 12:50:42 -04005048 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04005049 status = nfs4_do_create(dir, dentry, data);
5050
5051 nfs4_free_createdata(data);
5052out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005053 return status;
5054}
5055
5056static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
5057 struct iattr *sattr)
5058{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05005059 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust0688e642019-04-07 13:59:09 -04005060 struct nfs4_exception exception = {
5061 .interruptible = true,
5062 };
David Quigleyaa9c2662013-05-22 12:50:44 -04005063 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005064 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00005065
David Quigleyaa9c2662013-05-22 12:50:44 -04005066 label = nfs4_label_init_security(dir, dentry, sattr, &l);
5067
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05005068 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
5069 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005070 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04005071 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
5072 trace_nfs4_mkdir(dir, &dentry->d_name, err);
5073 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005074 &exception);
5075 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04005076 nfs4_label_release_security(label);
5077
Linus Torvalds1da177e2005-04-16 15:20:36 -07005078 return err;
5079}
5080
Trond Myklebust82e22a52020-11-02 17:34:23 -05005081static int _nfs4_proc_readdir(struct nfs_readdir_arg *nr_arg,
5082 struct nfs_readdir_res *nr_res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005083{
Trond Myklebust82e22a52020-11-02 17:34:23 -05005084 struct inode *dir = d_inode(nr_arg->dentry);
Olga Kornievskaia05ad9172020-11-06 16:03:38 -05005085 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005086 struct nfs4_readdir_arg args = {
5087 .fh = NFS_FH(dir),
Trond Myklebust82e22a52020-11-02 17:34:23 -05005088 .pages = nr_arg->pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005089 .pgbase = 0,
Trond Myklebust82e22a52020-11-02 17:34:23 -05005090 .count = nr_arg->page_len,
5091 .plus = nr_arg->plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005092 };
5093 struct nfs4_readdir_res res;
5094 struct rpc_message msg = {
5095 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
5096 .rpc_argp = &args,
5097 .rpc_resp = &res,
Trond Myklebust82e22a52020-11-02 17:34:23 -05005098 .rpc_cred = nr_arg->cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005099 };
5100 int status;
5101
Trond Myklebust82e22a52020-11-02 17:34:23 -05005102 dprintk("%s: dentry = %pd2, cookie = %llu\n", __func__,
5103 nr_arg->dentry, (unsigned long long)nr_arg->cookie);
Olga Kornievskaia05ad9172020-11-06 16:03:38 -05005104 if (!(server->caps & NFS_CAP_SECURITY_LABEL))
5105 args.bitmask = server->attr_bitmask_nl;
5106 else
5107 args.bitmask = server->attr_bitmask;
5108
Trond Myklebust82e22a52020-11-02 17:34:23 -05005109 nfs4_setup_readdir(nr_arg->cookie, nr_arg->verf, nr_arg->dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005110 res.pgbase = args.pgbase;
Olga Kornievskaia05ad9172020-11-06 16:03:38 -05005111 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
5112 &res.seq_res, 0);
Trond Myklebustac396122010-11-15 20:26:22 -05005113 if (status >= 0) {
Trond Myklebust82e22a52020-11-02 17:34:23 -05005114 memcpy(nr_res->verf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05005115 status += args.pgbase;
5116 }
Trond Myklebustc4812992007-09-28 17:11:45 -04005117
5118 nfs_invalidate_atime(dir);
5119
Harvey Harrison3110ff82008-05-02 13:42:44 -07005120 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005121 return status;
5122}
5123
Trond Myklebust82e22a52020-11-02 17:34:23 -05005124static int nfs4_proc_readdir(struct nfs_readdir_arg *arg,
5125 struct nfs_readdir_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005126{
Trond Myklebust0688e642019-04-07 13:59:09 -04005127 struct nfs4_exception exception = {
5128 .interruptible = true,
5129 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005130 int err;
5131 do {
Trond Myklebust82e22a52020-11-02 17:34:23 -05005132 err = _nfs4_proc_readdir(arg, res);
5133 trace_nfs4_readdir(d_inode(arg->dentry), err);
5134 err = nfs4_handle_exception(NFS_SERVER(d_inode(arg->dentry)),
5135 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005136 } while (exception.retry);
5137 return err;
5138}
5139
5140static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04005141 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005142{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04005143 struct nfs4_createdata *data;
5144 int mode = sattr->ia_mode;
5145 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005146
Trond Myklebust57dc9a52008-06-20 15:35:32 -04005147 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
5148 if (data == NULL)
5149 goto out;
5150
Linus Torvalds1da177e2005-04-16 15:20:36 -07005151 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04005152 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005153 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04005154 data->arg.ftype = NF4BLK;
5155 data->arg.u.device.specdata1 = MAJOR(rdev);
5156 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005157 }
5158 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04005159 data->arg.ftype = NF4CHR;
5160 data->arg.u.device.specdata1 = MAJOR(rdev);
5161 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04005162 } else if (!S_ISSOCK(mode)) {
5163 status = -EINVAL;
5164 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005165 }
David Quigley1775fd32013-05-22 12:50:42 -04005166
David Quigleyaa9c2662013-05-22 12:50:44 -04005167 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04005168 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04005169out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04005170 nfs4_free_createdata(data);
5171out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005172 return status;
5173}
5174
5175static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
5176 struct iattr *sattr, dev_t rdev)
5177{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05005178 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust0688e642019-04-07 13:59:09 -04005179 struct nfs4_exception exception = {
5180 .interruptible = true,
5181 };
David Quigleyaa9c2662013-05-22 12:50:44 -04005182 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005183 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00005184
David Quigleyaa9c2662013-05-22 12:50:44 -04005185 label = nfs4_label_init_security(dir, dentry, sattr, &l);
5186
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05005187 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
5188 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005189 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04005190 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
5191 trace_nfs4_mknod(dir, &dentry->d_name, err);
5192 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005193 &exception);
5194 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04005195
5196 nfs4_label_release_security(label);
5197
Linus Torvalds1da177e2005-04-16 15:20:36 -07005198 return err;
5199}
5200
5201static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
5202 struct nfs_fsstat *fsstat)
5203{
5204 struct nfs4_statfs_arg args = {
5205 .fh = fhandle,
5206 .bitmask = server->attr_bitmask,
5207 };
Benny Halevy24ad1482009-04-01 09:21:56 -04005208 struct nfs4_statfs_res res = {
5209 .fsstat = fsstat,
5210 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005211 struct rpc_message msg = {
5212 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
5213 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04005214 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005215 };
5216
Trond Myklebust0e574af2005-10-27 22:12:38 -04005217 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005218 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005219}
5220
5221static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
5222{
Trond Myklebust0688e642019-04-07 13:59:09 -04005223 struct nfs4_exception exception = {
5224 .interruptible = true,
5225 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005226 int err;
5227 do {
5228 err = nfs4_handle_exception(server,
5229 _nfs4_proc_statfs(server, fhandle, fsstat),
5230 &exception);
5231 } while (exception.retry);
5232 return err;
5233}
5234
5235static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
5236 struct nfs_fsinfo *fsinfo)
5237{
5238 struct nfs4_fsinfo_arg args = {
5239 .fh = fhandle,
5240 .bitmask = server->attr_bitmask,
5241 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04005242 struct nfs4_fsinfo_res res = {
5243 .fsinfo = fsinfo,
5244 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005245 struct rpc_message msg = {
5246 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
5247 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04005248 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005249 };
5250
Bryan Schumaker7c513052011-03-24 17:12:24 +00005251 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005252}
5253
5254static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5255{
Trond Myklebust0688e642019-04-07 13:59:09 -04005256 struct nfs4_exception exception = {
5257 .interruptible = true,
5258 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005259 int err;
5260
5261 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04005262 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04005263 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04005264 if (err == 0) {
Robert Milkowski7dc29932020-01-30 09:43:25 +00005265 nfs4_set_lease_period(server->nfs_client, fsinfo->lease_time * HZ);
Chuck Lever83ca7f52013-03-16 15:55:53 -04005266 break;
5267 }
5268 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005269 } while (exception.retry);
5270 return err;
5271}
5272
5273static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5274{
Bryan Schumakere38eb652012-06-20 15:53:40 -04005275 int error;
5276
Trond Myklebust0e574af2005-10-27 22:12:38 -04005277 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04005278 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08005279 if (error == 0) {
5280 /* block layout checks this! */
5281 server->pnfs_blksize = fsinfo->blksize;
Jeff Laytonca440c32016-09-15 14:40:49 -04005282 set_pnfs_layoutdriver(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08005283 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04005284
5285 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005286}
5287
5288static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5289 struct nfs_pathconf *pathconf)
5290{
5291 struct nfs4_pathconf_arg args = {
5292 .fh = fhandle,
5293 .bitmask = server->attr_bitmask,
5294 };
Benny Halevyd45b2982009-04-01 09:21:58 -04005295 struct nfs4_pathconf_res res = {
5296 .pathconf = pathconf,
5297 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005298 struct rpc_message msg = {
5299 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
5300 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04005301 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005302 };
5303
5304 /* None of the pathconf attributes are mandatory to implement */
5305 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
5306 memset(pathconf, 0, sizeof(*pathconf));
5307 return 0;
5308 }
5309
Trond Myklebust0e574af2005-10-27 22:12:38 -04005310 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005311 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005312}
5313
5314static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5315 struct nfs_pathconf *pathconf)
5316{
Trond Myklebust0688e642019-04-07 13:59:09 -04005317 struct nfs4_exception exception = {
5318 .interruptible = true,
5319 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005320 int err;
5321
5322 do {
5323 err = nfs4_handle_exception(server,
5324 _nfs4_proc_pathconf(server, fhandle, pathconf),
5325 &exception);
5326 } while (exception.retry);
5327 return err;
5328}
5329
Trond Myklebust5521abf2013-03-16 20:54:34 -04005330int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04005331 const struct nfs_open_context *ctx,
5332 const struct nfs_lock_context *l_ctx,
5333 fmode_t fmode)
5334{
NeilBrown17393472016-10-13 15:26:47 +11005335 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04005336}
5337EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
5338
Trond Myklebust5521abf2013-03-16 20:54:34 -04005339static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
5340 const struct nfs_open_context *ctx,
5341 const struct nfs_lock_context *l_ctx,
5342 fmode_t fmode)
5343{
Ben Dooksd49dd112019-10-16 17:28:21 +01005344 nfs4_stateid _current_stateid;
Trond Myklebust5521abf2013-03-16 20:54:34 -04005345
Trond Myklebuste1253be2014-03-05 08:44:23 -05005346 /* If the current stateid represents a lost lock, then exit */
Ben Dooksd49dd112019-10-16 17:28:21 +01005347 if (nfs4_set_rw_stateid(&_current_stateid, ctx, l_ctx, fmode) == -EIO)
Trond Myklebuste1253be2014-03-05 08:44:23 -05005348 return true;
Ben Dooksd49dd112019-10-16 17:28:21 +01005349 return nfs4_stateid_match(stateid, &_current_stateid);
Trond Myklebust5521abf2013-03-16 20:54:34 -04005350}
5351
5352static bool nfs4_error_stateid_expired(int err)
5353{
5354 switch (err) {
5355 case -NFS4ERR_DELEG_REVOKED:
5356 case -NFS4ERR_ADMIN_REVOKED:
5357 case -NFS4ERR_BAD_STATEID:
5358 case -NFS4ERR_STALE_STATEID:
5359 case -NFS4ERR_OLD_STATEID:
5360 case -NFS4ERR_OPENMODE:
5361 case -NFS4ERR_EXPIRED:
5362 return true;
5363 }
5364 return false;
5365}
5366
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005367static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005368{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005369 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005370
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005371 trace_nfs4_read(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04005372 if (task->tk_status < 0) {
5373 struct nfs4_exception exception = {
5374 .inode = hdr->inode,
5375 .state = hdr->args.context->state,
5376 .stateid = &hdr->args.stateid,
5377 };
5378 task->tk_status = nfs4_async_handle_exception(task,
5379 server, task->tk_status, &exception);
5380 if (exception.retry) {
5381 rpc_restart_call_prepare(task);
5382 return -EAGAIN;
5383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005384 }
Trond Myklebust8850df92007-09-28 17:20:07 -04005385
Linus Torvalds1da177e2005-04-16 15:20:36 -07005386 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005387 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05005388 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005389}
5390
Trond Myklebust5521abf2013-03-16 20:54:34 -04005391static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04005392 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04005393{
5394
5395 if (!nfs4_error_stateid_expired(task->tk_status) ||
5396 nfs4_stateid_is_current(&args->stateid,
5397 args->context,
5398 args->lock_context,
5399 FMODE_READ))
5400 return false;
5401 rpc_restart_call_prepare(task);
5402 return true;
5403}
5404
Anna Schumakerc5675522014-05-28 13:41:22 -04005405static bool nfs4_read_plus_not_supported(struct rpc_task *task,
5406 struct nfs_pgio_header *hdr)
5407{
5408 struct nfs_server *server = NFS_SERVER(hdr->inode);
5409 struct rpc_message *msg = &task->tk_msg;
5410
5411 if (msg->rpc_proc == &nfs4_procedures[NFSPROC4_CLNT_READ_PLUS] &&
5412 server->caps & NFS_CAP_READ_PLUS && task->tk_status == -ENOTSUPP) {
5413 server->caps &= ~NFS_CAP_READ_PLUS;
5414 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
5415 rpc_restart_call_prepare(task);
5416 return true;
5417 }
5418 return false;
5419}
5420
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005421static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc72011-03-01 01:34:20 +00005422{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005423 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc72011-03-01 01:34:20 +00005424 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005425 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04005426 return -EAGAIN;
Anna Schumakerc5675522014-05-28 13:41:22 -04005427 if (nfs4_read_plus_not_supported(task, hdr))
5428 return -EAGAIN;
Trond Myklebustbfc505d2016-09-15 18:26:05 -04005429 if (task->tk_status > 0)
5430 nfs_invalidate_atime(hdr->inode);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005431 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5432 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00005433}
5434
Anna Schumaker21e31402020-12-03 15:18:39 -05005435#if defined CONFIG_NFS_V4_2 && defined CONFIG_NFS_V4_2_READ_PLUS
Trond Myklebust5c3485b2020-12-10 09:34:34 -05005436static void nfs42_read_plus_support(struct nfs_pgio_header *hdr,
5437 struct rpc_message *msg)
Anna Schumakerc5675522014-05-28 13:41:22 -04005438{
Trond Myklebust5c3485b2020-12-10 09:34:34 -05005439 /* Note: We don't use READ_PLUS with pNFS yet */
5440 if (nfs_server_capable(hdr->inode, NFS_CAP_READ_PLUS) && !hdr->ds_clp)
Anna Schumakerc5675522014-05-28 13:41:22 -04005441 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ_PLUS];
Anna Schumakerc5675522014-05-28 13:41:22 -04005442}
5443#else
Trond Myklebust5c3485b2020-12-10 09:34:34 -05005444static void nfs42_read_plus_support(struct nfs_pgio_header *hdr,
5445 struct rpc_message *msg)
Anna Schumakerc5675522014-05-28 13:41:22 -04005446{
Anna Schumakerc5675522014-05-28 13:41:22 -04005447}
5448#endif /* CONFIG_NFS_V4_2 */
5449
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005450static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
5451 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005452{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005453 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04005454 if (!hdr->pgio_done_cb)
5455 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebust5c3485b2020-12-10 09:34:34 -05005456 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
5457 nfs42_read_plus_support(hdr, msg);
Anna Schumakerfba83f32018-05-04 16:22:50 -04005458 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005459}
5460
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005461static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
5462 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04005463{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005464 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005465 &hdr->args.seq_args,
5466 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04005467 task))
NeilBrownef1820f2013-09-04 17:04:49 +10005468 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005469 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
5470 hdr->args.lock_context,
Benjamin Coddingtonfbe77c32017-04-19 10:11:35 -04005471 hdr->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10005472 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005473 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10005474 return -EIO;
5475 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005476}
5477
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005478static int nfs4_write_done_cb(struct rpc_task *task,
5479 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005480{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005481 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10005482
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005483 trace_nfs4_write(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04005484 if (task->tk_status < 0) {
5485 struct nfs4_exception exception = {
5486 .inode = hdr->inode,
5487 .state = hdr->args.context->state,
5488 .stateid = &hdr->args.stateid,
5489 };
5490 task->tk_status = nfs4_async_handle_exception(task,
5491 NFS_SERVER(inode), task->tk_status,
5492 &exception);
5493 if (exception.retry) {
5494 rpc_restart_call_prepare(task);
5495 return -EAGAIN;
5496 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005497 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04005498 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005499 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05005500 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04005501 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05005502 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005503}
5504
Trond Myklebust5521abf2013-03-16 20:54:34 -04005505static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04005506 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04005507{
5508
5509 if (!nfs4_error_stateid_expired(task->tk_status) ||
5510 nfs4_stateid_is_current(&args->stateid,
5511 args->context,
5512 args->lock_context,
5513 FMODE_WRITE))
5514 return false;
5515 rpc_restart_call_prepare(task);
5516 return true;
5517}
5518
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005519static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00005520{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005521 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00005522 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005523 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04005524 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005525 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5526 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00005527}
5528
Trond Myklebust5a37f852012-04-28 14:55:16 -04005529static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005530bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00005531{
Trond Myklebust5a37f852012-04-28 14:55:16 -04005532 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005533 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04005534 return false;
5535 /* Otherwise, request attributes if and only if we don't hold
5536 * a delegation
5537 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04005538 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00005539}
Fred Isamana69aef12011-03-03 15:13:47 +00005540
Trond Myklebust85847282021-12-27 14:40:51 -05005541void nfs4_bitmask_set(__u32 bitmask[], const __u32 src[],
5542 struct inode *inode, unsigned long cache_validity)
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005543{
Trond Myklebust85847282021-12-27 14:40:51 -05005544 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebust332d1a02021-03-25 18:15:36 -04005545 unsigned int i;
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005546
Trond Myklebust332d1a02021-03-25 18:15:36 -04005547 memcpy(bitmask, src, sizeof(*bitmask) * NFS4_BITMASK_SZ);
Trond Myklebust85847282021-12-27 14:40:51 -05005548 cache_validity |= READ_ONCE(NFS_I(inode)->cache_validity);
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005549
Trond Myklebust45901a22021-02-08 08:49:32 -05005550 if (cache_validity & NFS_INO_INVALID_CHANGE)
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005551 bitmask[0] |= FATTR4_WORD0_CHANGE;
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005552 if (cache_validity & NFS_INO_INVALID_ATIME)
5553 bitmask[1] |= FATTR4_WORD1_TIME_ACCESS;
Trond Myklebust720869e2021-04-13 09:41:16 -04005554 if (cache_validity & NFS_INO_INVALID_MODE)
5555 bitmask[1] |= FATTR4_WORD1_MODE;
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005556 if (cache_validity & NFS_INO_INVALID_OTHER)
Trond Myklebust720869e2021-04-13 09:41:16 -04005557 bitmask[1] |= FATTR4_WORD1_OWNER | FATTR4_WORD1_OWNER_GROUP;
Trond Myklebustfabf2b32021-03-25 13:14:42 -04005558 if (cache_validity & NFS_INO_INVALID_NLINK)
5559 bitmask[1] |= FATTR4_WORD1_NUMLINKS;
Trond Myklebust45901a22021-02-08 08:49:32 -05005560 if (cache_validity & NFS_INO_INVALID_CTIME)
5561 bitmask[1] |= FATTR4_WORD1_TIME_METADATA;
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005562 if (cache_validity & NFS_INO_INVALID_MTIME)
5563 bitmask[1] |= FATTR4_WORD1_TIME_MODIFY;
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005564 if (cache_validity & NFS_INO_INVALID_BLOCKS)
5565 bitmask[1] |= FATTR4_WORD1_SPACE_USED;
Trond Myklebust332d1a02021-03-25 18:15:36 -04005566
Trond Myklebustcc7f2da2021-04-11 14:31:24 -04005567 if (cache_validity & NFS_INO_INVALID_SIZE)
Trond Myklebust332d1a02021-03-25 18:15:36 -04005568 bitmask[0] |= FATTR4_WORD0_SIZE;
5569
5570 for (i = 0; i < NFS4_BITMASK_SZ; i++)
5571 bitmask[i] &= server->attr_bitmask[i];
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005572}
5573
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005574static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
Anna Schumakerfb91fb02018-05-04 16:22:48 -04005575 struct rpc_message *msg,
5576 struct rpc_clnt **clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005577{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005578 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005579
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005580 if (!nfs4_write_need_cache_consistency_data(hdr)) {
5581 hdr->args.bitmask = NULL;
5582 hdr->res.fattr = NULL;
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005583 } else {
Trond Myklebust332d1a02021-03-25 18:15:36 -04005584 nfs4_bitmask_set(hdr->args.bitmask_store,
5585 server->cache_consistency_bitmask,
Trond Myklebust85847282021-12-27 14:40:51 -05005586 hdr->inode, NFS_INO_INVALID_BLOCKS);
Trond Myklebust332d1a02021-03-25 18:15:36 -04005587 hdr->args.bitmask = hdr->args.bitmask_store;
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005588 }
Trond Myklebust5a37f852012-04-28 14:55:16 -04005589
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005590 if (!hdr->pgio_done_cb)
5591 hdr->pgio_done_cb = nfs4_write_done_cb;
5592 hdr->res.server = server;
5593 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005594
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005595 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Olga Kornievskaiacd1b6592020-02-12 17:32:12 -05005596 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
Anna Schumakerfb91fb02018-05-04 16:22:48 -04005597 nfs4_state_protect_write(server->nfs_client, clnt, msg, hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005598}
5599
Fred Isaman0b7c0152012-04-20 14:47:39 -04005600static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
5601{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005602 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005603 &data->args.seq_args,
5604 &data->res.seq_res,
5605 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005606}
5607
Fred Isaman0b7c0152012-04-20 14:47:39 -04005608static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005609{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005610 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04005611
Trond Myklebustcc668ab2013-08-14 15:31:28 -04005612 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10005613 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
5614 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005615 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05005616 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005617 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05005618 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005619}
5620
Fred Isaman0b7c0152012-04-20 14:47:39 -04005621static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00005622{
5623 if (!nfs4_sequence_done(task, &data->res.seq_res))
5624 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04005625 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00005626}
5627
Anna Schumakere9ae1ee2018-05-04 16:22:49 -04005628static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg,
5629 struct rpc_clnt **clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005630{
Trond Myklebust788e7a82006-03-20 13:44:27 -05005631 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00005632
Fred Isaman0b7c0152012-04-20 14:47:39 -04005633 if (data->commit_done_cb == NULL)
5634 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04005635 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005636 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Anna Schumakerfba83f32018-05-04 16:22:50 -04005637 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Anna Schumakere9ae1ee2018-05-04 16:22:49 -04005638 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_COMMIT, clnt, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005639}
5640
Olga Kornievskaia6b8d84e2018-07-09 15:13:36 -04005641static int _nfs4_proc_commit(struct file *dst, struct nfs_commitargs *args,
5642 struct nfs_commitres *res)
5643{
5644 struct inode *dst_inode = file_inode(dst);
5645 struct nfs_server *server = NFS_SERVER(dst_inode);
5646 struct rpc_message msg = {
5647 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
5648 .rpc_argp = args,
5649 .rpc_resp = res,
5650 };
5651
5652 args->fh = NFS_FH(dst_inode);
5653 return nfs4_call_sync(server->client, server, &msg,
5654 &args->seq_args, &res->seq_res, 1);
5655}
5656
5657int nfs4_proc_commit(struct file *dst, __u64 offset, __u32 count, struct nfs_commitres *res)
5658{
5659 struct nfs_commitargs args = {
5660 .offset = offset,
5661 .count = count,
5662 };
5663 struct nfs_server *dst_server = NFS_SERVER(file_inode(dst));
5664 struct nfs4_exception exception = { };
5665 int status;
5666
5667 do {
5668 status = _nfs4_proc_commit(dst, &args, res);
5669 status = nfs4_handle_exception(dst_server, status, &exception);
5670 } while (exception.retry);
5671
5672 return status;
5673}
5674
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005675struct nfs4_renewdata {
5676 struct nfs_client *client;
5677 unsigned long timestamp;
5678};
5679
Linus Torvalds1da177e2005-04-16 15:20:36 -07005680/*
5681 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
5682 * standalone procedure for queueing an asynchronous RENEW.
5683 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005684static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005685{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005686 struct nfs4_renewdata *data = calldata;
5687 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005688
Elena Reshetova212bf412017-10-20 12:53:38 +03005689 if (refcount_read(&clp->cl_count) > 1)
Alexandros Batsakis0851de062010-02-05 03:45:06 -08005690 nfs4_schedule_state_renewal(clp);
5691 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005692 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005693}
5694
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005695static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005696{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005697 struct nfs4_renewdata *data = calldata;
5698 struct nfs_client *clp = data->client;
5699 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005700
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005701 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04005702 switch (task->tk_status) {
5703 case 0:
5704 break;
5705 case -NFS4ERR_LEASE_MOVED:
5706 nfs4_schedule_lease_moved_recovery(clp);
5707 break;
5708 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04005709 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04005710 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
5711 return;
5712 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005713 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04005714 return;
5715 }
5716 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005717 }
Trond Myklebust452e9352010-07-31 14:29:06 -04005718 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005719}
5720
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005721static const struct rpc_call_ops nfs4_renew_ops = {
5722 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005723 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005724};
5725
NeilBrowna52458b2018-12-03 11:30:31 +11005726static int nfs4_proc_async_renew(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005727{
5728 struct rpc_message msg = {
5729 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5730 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01005731 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005732 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005733 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005734
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005735 if (renew_flags == 0)
5736 return 0;
Elena Reshetova212bf412017-10-20 12:53:38 +03005737 if (!refcount_inc_not_zero(&clp->cl_count))
Alexandros Batsakis0851de062010-02-05 03:45:06 -08005738 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04005739 data = kmalloc(sizeof(*data), GFP_NOFS);
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04005740 if (data == NULL) {
5741 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005742 return -ENOMEM;
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04005743 }
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005744 data->client = clp;
5745 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005746 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005747 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005748}
5749
NeilBrowna52458b2018-12-03 11:30:31 +11005750static int nfs4_proc_renew(struct nfs_client *clp, const struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005751{
5752 struct rpc_message msg = {
5753 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5754 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01005755 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005756 };
5757 unsigned long now = jiffies;
5758 int status;
5759
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005760 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005761 if (status < 0)
5762 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04005763 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005764 return 0;
5765}
5766
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005767static inline int nfs4_server_supports_acls(struct nfs_server *server)
5768{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06005769 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005770}
5771
Trond Myklebust21f498c2012-08-24 10:59:25 -04005772/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
5773 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005774 * the stack.
5775 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04005776#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005777
Frank van der Lindenccde1e92020-06-23 22:39:01 +00005778int nfs4_buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005779 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05005780{
5781 struct page *newpage, **spages;
5782 int rc = 0;
5783 size_t len;
5784 spages = pages;
5785
5786 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04005787 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05005788 newpage = alloc_page(GFP_KERNEL);
5789
5790 if (newpage == NULL)
5791 goto unwind;
5792 memcpy(page_address(newpage), buf, len);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005793 buf += len;
5794 buflen -= len;
Neil Hormane9e3d722011-03-04 19:26:03 -05005795 *pages++ = newpage;
5796 rc++;
5797 } while (buflen != 0);
5798
5799 return rc;
5800
5801unwind:
5802 for(; rc > 0; rc--)
5803 __free_page(spages[rc-1]);
5804 return -ENOMEM;
5805}
5806
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005807struct nfs4_cached_acl {
5808 int cached;
5809 size_t len;
Gustavo A. R. Silva5601cda2020-03-09 13:24:42 -05005810 char data[];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005811};
5812
5813static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005814{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005815 struct nfs_inode *nfsi = NFS_I(inode);
5816
5817 spin_lock(&inode->i_lock);
5818 kfree(nfsi->nfs4_acl);
5819 nfsi->nfs4_acl = acl;
5820 spin_unlock(&inode->i_lock);
5821}
5822
5823static void nfs4_zap_acl_attr(struct inode *inode)
5824{
5825 nfs4_set_cached_acl(inode, NULL);
5826}
5827
5828static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
5829{
5830 struct nfs_inode *nfsi = NFS_I(inode);
5831 struct nfs4_cached_acl *acl;
5832 int ret = -ENOENT;
5833
5834 spin_lock(&inode->i_lock);
5835 acl = nfsi->nfs4_acl;
5836 if (acl == NULL)
5837 goto out;
5838 if (buf == NULL) /* user is just asking for length */
5839 goto out_len;
5840 if (acl->cached == 0)
5841 goto out;
5842 ret = -ERANGE; /* see getxattr(2) man page */
5843 if (acl->len > buflen)
5844 goto out;
5845 memcpy(buf, acl->data, acl->len);
5846out_len:
5847 ret = acl->len;
5848out:
5849 spin_unlock(&inode->i_lock);
5850 return ret;
5851}
5852
Sachin Prabhu5794d212012-04-17 14:36:40 +01005853static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005854{
5855 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005856 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005857
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005858 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005859 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005860 if (acl == NULL)
5861 goto out;
5862 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01005863 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005864 } else {
5865 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5866 if (acl == NULL)
5867 goto out;
5868 acl->cached = 0;
5869 }
5870 acl->len = acl_len;
5871out:
5872 nfs4_set_cached_acl(inode, acl);
5873}
5874
Andy Adamsonbf118a32011-12-07 11:55:27 -05005875/*
5876 * The getxattr API returns the required buffer length when called with a
5877 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5878 * the required buf. On a NULL buf, we send a page of data to the server
5879 * guessing that the ACL request can be serviced by a page. If so, we cache
5880 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5881 * the cache. If not so, we throw away the page, and cache the required
5882 * length. The next getxattr call will then produce another round trip to
5883 * the server, this time with the input buf of the required size.
5884 */
Trond Myklebust16b42892006-08-24 12:27:15 -04005885static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005886{
Olga Kornievskaia62a15732020-01-02 17:09:54 -05005887 struct page **pages;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005888 struct nfs_getaclargs args = {
5889 .fh = NFS_FH(inode),
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005890 .acl_len = buflen,
5891 };
Benny Halevy663c79b2009-04-01 09:21:59 -04005892 struct nfs_getaclres res = {
5893 .acl_len = buflen,
5894 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005895 struct rpc_message msg = {
5896 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5897 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04005898 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005899 };
Olga Kornievskaia62a15732020-01-02 17:09:54 -05005900 unsigned int npages;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005901 int ret = -ENOMEM, i;
Olga Kornievskaia62a15732020-01-02 17:09:54 -05005902 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005903
Olga Kornievskaia62a15732020-01-02 17:09:54 -05005904 if (buflen == 0)
5905 buflen = server->rsize;
5906
5907 npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
5908 pages = kmalloc_array(npages, sizeof(struct page *), GFP_NOFS);
5909 if (!pages)
5910 return -ENOMEM;
5911
5912 args.acl_pages = pages;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005913
Andy Adamsonbf118a32011-12-07 11:55:27 -05005914 for (i = 0; i < npages; i++) {
5915 pages[i] = alloc_page(GFP_KERNEL);
5916 if (!pages[i])
5917 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005918 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01005919
5920 /* for decoding across pages */
5921 res.acl_scratch = alloc_page(GFP_KERNEL);
5922 if (!res.acl_scratch)
5923 goto out_free;
5924
Andy Adamsonbf118a32011-12-07 11:55:27 -05005925 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005926
Peng Taode040be2012-01-10 22:42:47 +08005927 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05005928 __func__, buf, buflen, npages, args.acl_len);
5929 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5930 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005931 if (ret)
5932 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05005933
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005934 /* Handle the case where the passed-in buffer is too short */
5935 if (res.acl_flags & NFS4_ACL_TRUNC) {
5936 /* Did the user only issue a request for the acl length? */
5937 if (buf == NULL)
5938 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005939 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005940 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005941 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005942 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005943 if (buf) {
5944 if (res.acl_len > buflen) {
5945 ret = -ERANGE;
5946 goto out_free;
5947 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005948 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005949 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005950out_ok:
5951 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005952out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05005953 for (i = 0; i < npages; i++)
5954 if (pages[i])
5955 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05005956 if (res.acl_scratch)
5957 __free_page(res.acl_scratch);
Olga Kornievskaia62a15732020-01-02 17:09:54 -05005958 kfree(pages);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005959 return ret;
5960}
5961
Trond Myklebust16b42892006-08-24 12:27:15 -04005962static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5963{
Trond Myklebust0688e642019-04-07 13:59:09 -04005964 struct nfs4_exception exception = {
5965 .interruptible = true,
5966 };
Trond Myklebust16b42892006-08-24 12:27:15 -04005967 ssize_t ret;
5968 do {
5969 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04005970 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b42892006-08-24 12:27:15 -04005971 if (ret >= 0)
5972 break;
5973 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5974 } while (exception.retry);
5975 return ret;
5976}
5977
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005978static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5979{
5980 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005981 int ret;
5982
5983 if (!nfs4_server_supports_acls(server))
5984 return -EOPNOTSUPP;
Trond Myklebust1f3208b2021-03-25 11:04:34 -04005985 ret = nfs_revalidate_inode(inode, NFS_INO_INVALID_CHANGE);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005986 if (ret < 0)
5987 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005988 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5989 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005990 ret = nfs4_read_cached_acl(inode, buf, buflen);
5991 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05005992 /* -ENOENT is returned if there is no ACL or if there is an ACL
5993 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005994 return ret;
5995 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005996}
5997
Trond Myklebust16b42892006-08-24 12:27:15 -04005998static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005999{
6000 struct nfs_server *server = NFS_SERVER(inode);
6001 struct page *pages[NFS4ACL_MAXPAGES];
6002 struct nfs_setaclargs arg = {
6003 .fh = NFS_FH(inode),
6004 .acl_pages = pages,
6005 .acl_len = buflen,
6006 };
Benny Halevy73c403a2009-04-01 09:22:01 -04006007 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00006008 struct rpc_message msg = {
6009 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
6010 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04006011 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00006012 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04006013 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05006014 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00006015
J. Bruce Fields4f8be1f2021-01-28 17:36:38 -05006016 /* You can't remove system.nfs4_acl: */
6017 if (buflen == 0)
6018 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00006019 if (!nfs4_server_supports_acls(server))
6020 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04006021 if (npages > ARRAY_SIZE(pages))
6022 return -ERANGE;
Frank van der Lindenccde1e92020-06-23 22:39:01 +00006023 i = nfs4_buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05006024 if (i < 0)
6025 return i;
Trond Myklebustc01d3642018-03-20 16:43:20 -04006026 nfs4_inode_make_writeable(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00006027 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05006028
6029 /*
6030 * Free each page after tx, so the only ref left is
6031 * held by the network stack
6032 */
6033 for (; i > 0; i--)
6034 put_page(pages[i-1]);
6035
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00006036 /*
6037 * Acl update can result in inode attribute update.
6038 * so mark the attribute cache invalid.
6039 */
6040 spin_lock(&inode->i_lock);
Trond Myklebustb6f80a22021-03-08 14:42:55 -05006041 nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE |
6042 NFS_INO_INVALID_CTIME |
6043 NFS_INO_REVAL_FORCED);
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00006044 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04006045 nfs_access_zap_cache(inode);
6046 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00006047 return ret;
6048}
6049
Trond Myklebust16b42892006-08-24 12:27:15 -04006050static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
6051{
6052 struct nfs4_exception exception = { };
6053 int err;
6054 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04006055 err = __nfs4_proc_set_acl(inode, buf, buflen);
6056 trace_nfs4_set_acl(inode, err);
Dai Ngof8849e22021-05-19 17:15:10 -04006057 if (err == -NFS4ERR_BADOWNER || err == -NFS4ERR_BADNAME) {
6058 /*
6059 * no need to retry since the kernel
6060 * isn't involved in encoding the ACEs.
6061 */
6062 err = -EINVAL;
6063 break;
6064 }
Trond Myklebustc1578b72013-08-12 16:58:42 -04006065 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b42892006-08-24 12:27:15 -04006066 &exception);
6067 } while (exception.retry);
6068 return err;
6069}
6070
David Quigleyaa9c2662013-05-22 12:50:44 -04006071#ifdef CONFIG_NFS_V4_SECURITY_LABEL
6072static int _nfs4_get_security_label(struct inode *inode, void *buf,
6073 size_t buflen)
6074{
6075 struct nfs_server *server = NFS_SERVER(inode);
David Quigleyaa9c2662013-05-22 12:50:44 -04006076 struct nfs4_label label = {0, 0, buflen, buf};
6077
6078 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Anna Schumaker2ef61e02021-10-22 13:11:07 -04006079 struct nfs_fattr fattr = {
6080 .label = &label,
6081 };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04006082 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04006083 .fh = NFS_FH(inode),
6084 .bitmask = bitmask,
6085 };
6086 struct nfs4_getattr_res res = {
6087 .fattr = &fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04006088 .server = server,
6089 };
6090 struct rpc_message msg = {
6091 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04006092 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04006093 .rpc_resp = &res,
6094 };
6095 int ret;
6096
6097 nfs_fattr_init(&fattr);
6098
Trond Myklebustfcb63a92013-11-01 12:42:25 -04006099 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04006100 if (ret)
6101 return ret;
6102 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
6103 return -ENOENT;
Ondrej Mosnacek53cb2452021-01-15 18:43:56 +01006104 return label.len;
David Quigleyaa9c2662013-05-22 12:50:44 -04006105}
6106
6107static int nfs4_get_security_label(struct inode *inode, void *buf,
6108 size_t buflen)
6109{
Trond Myklebust0688e642019-04-07 13:59:09 -04006110 struct nfs4_exception exception = {
6111 .interruptible = true,
6112 };
David Quigleyaa9c2662013-05-22 12:50:44 -04006113 int err;
6114
6115 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
6116 return -EOPNOTSUPP;
6117
6118 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04006119 err = _nfs4_get_security_label(inode, buf, buflen);
6120 trace_nfs4_get_security_label(inode, err);
6121 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04006122 &exception);
6123 } while (exception.retry);
6124 return err;
6125}
6126
6127static int _nfs4_do_set_security_label(struct inode *inode,
6128 struct nfs4_label *ilabel,
Anna Schumaker1b00ad62021-10-22 13:11:08 -04006129 struct nfs_fattr *fattr)
David Quigleyaa9c2662013-05-22 12:50:44 -04006130{
6131
6132 struct iattr sattr = {0};
6133 struct nfs_server *server = NFS_SERVER(inode);
6134 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04006135 struct nfs_setattrargs arg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05006136 .fh = NFS_FH(inode),
6137 .iap = &sattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04006138 .server = server,
6139 .bitmask = bitmask,
6140 .label = ilabel,
6141 };
6142 struct nfs_setattrres res = {
6143 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04006144 .server = server,
6145 };
6146 struct rpc_message msg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05006147 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
6148 .rpc_argp = &arg,
6149 .rpc_resp = &res,
David Quigleyaa9c2662013-05-22 12:50:44 -04006150 };
6151 int status;
6152
Jeff Layton12207f62013-11-01 10:49:32 -04006153 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04006154
Jeff Layton12207f62013-11-01 10:49:32 -04006155 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04006156 if (status)
6157 dprintk("%s failed: %d\n", __func__, status);
6158
6159 return status;
6160}
6161
6162static int nfs4_do_set_security_label(struct inode *inode,
6163 struct nfs4_label *ilabel,
Anna Schumaker1b00ad62021-10-22 13:11:08 -04006164 struct nfs_fattr *fattr)
David Quigleyaa9c2662013-05-22 12:50:44 -04006165{
6166 struct nfs4_exception exception = { };
6167 int err;
6168
6169 do {
Anna Schumaker1b00ad62021-10-22 13:11:08 -04006170 err = _nfs4_do_set_security_label(inode, ilabel, fattr);
Trond Myklebustc1578b72013-08-12 16:58:42 -04006171 trace_nfs4_set_security_label(inode, err);
6172 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04006173 &exception);
6174 } while (exception.retry);
6175 return err;
6176}
6177
6178static int
Al Viro59301222016-05-27 10:19:30 -04006179nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04006180{
Anna Schumaker1b00ad62021-10-22 13:11:08 -04006181 struct nfs4_label ilabel = {0, 0, buflen, (char *)buf };
6182 struct nfs_fattr *fattr;
David Quigleyaa9c2662013-05-22 12:50:44 -04006183 int status;
6184
6185 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
6186 return -EOPNOTSUPP;
6187
Anna Schumaker1b00ad62021-10-22 13:11:08 -04006188 fattr = nfs_alloc_fattr_with_label(NFS_SERVER(inode));
6189 if (fattr == NULL)
6190 return -ENOMEM;
David Quigleyaa9c2662013-05-22 12:50:44 -04006191
Anna Schumaker1b00ad62021-10-22 13:11:08 -04006192 status = nfs4_do_set_security_label(inode, &ilabel, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04006193 if (status == 0)
Anna Schumakerdd225cb2021-10-22 13:11:12 -04006194 nfs_setsecurity(inode, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04006195
David Quigleyaa9c2662013-05-22 12:50:44 -04006196 return status;
6197}
6198#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
6199
6200
Chuck Leverf0920752012-05-21 22:45:41 -04006201static void nfs4_init_boot_verifier(const struct nfs_client *clp,
6202 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05006203{
6204 __be32 verf[2];
6205
Chuck Lever2c820d92012-05-21 22:45:33 -04006206 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
6207 /* An impossible timestamp guarantees this value
6208 * will never match a generated boot time. */
Deepa Dinamani2f86e092016-10-01 16:46:26 -07006209 verf[0] = cpu_to_be32(U32_MAX);
6210 verf[1] = cpu_to_be32(U32_MAX);
Chuck Lever2c820d92012-05-21 22:45:33 -04006211 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04006212 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Deepa Dinamani2f86e092016-10-01 16:46:26 -07006213 u64 ns = ktime_to_ns(nn->boot_time);
6214
6215 verf[0] = cpu_to_be32(ns >> 32);
6216 verf[1] = cpu_to_be32(ns);
Chuck Lever2c820d92012-05-21 22:45:33 -04006217 }
Chuck Levercd937102012-03-02 17:14:31 -05006218 memcpy(bootverf->data, verf, sizeof(bootverf->data));
6219}
6220
Trond Myklebust1aee5512020-10-07 18:24:17 -04006221static size_t
Trond Myklebust39d43d12020-10-07 18:24:18 -04006222nfs4_get_uniquifier(struct nfs_client *clp, char *buf, size_t buflen)
Trond Myklebust1aee5512020-10-07 18:24:17 -04006223{
Trond Myklebust39d43d12020-10-07 18:24:18 -04006224 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
6225 struct nfs_netns_client *nn_clp = nn->nfs_client;
6226 const char *id;
6227
Trond Myklebust1aee5512020-10-07 18:24:17 -04006228 buf[0] = '\0';
6229
Trond Myklebust39d43d12020-10-07 18:24:18 -04006230 if (nn_clp) {
6231 rcu_read_lock();
6232 id = rcu_dereference(nn_clp->identifier);
6233 if (id)
6234 strscpy(buf, id, buflen);
6235 rcu_read_unlock();
6236 }
6237
6238 if (nfs4_client_id_uniquifier[0] != '\0' && buf[0] == '\0')
Trond Myklebust1aee5512020-10-07 18:24:17 -04006239 strscpy(buf, nfs4_client_id_uniquifier, buflen);
6240
6241 return strlen(buf);
6242}
6243
Jeff Laytona3192682015-06-09 19:43:59 -04006244static int
6245nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04006246{
Trond Myklebust1aee5512020-10-07 18:24:17 -04006247 char buf[NFS4_CLIENT_ID_UNIQ_LEN];
6248 size_t buflen;
Jeff Laytona3192682015-06-09 19:43:59 -04006249 size_t len;
6250 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04006251
Trond Myklebustceb3a162015-01-03 15:16:04 -05006252 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04006253 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08006254
Jeff Laytona3192682015-06-09 19:43:59 -04006255 rcu_read_lock();
Chuck Lever848a4eb2018-06-04 10:53:29 -04006256 len = 14 +
6257 strlen(clp->cl_rpcclient->cl_nodename) +
6258 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04006259 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
Jeff Laytona3192682015-06-09 19:43:59 -04006260 1;
6261 rcu_read_unlock();
Trond Myklebust1aee5512020-10-07 18:24:17 -04006262
Trond Myklebust39d43d12020-10-07 18:24:18 -04006263 buflen = nfs4_get_uniquifier(clp, buf, sizeof(buf));
Trond Myklebust1aee5512020-10-07 18:24:17 -04006264 if (buflen)
6265 len += buflen + 1;
6266
Jeff Laytona3192682015-06-09 19:43:59 -04006267 if (len > NFS4_OPAQUE_LIMIT + 1)
6268 return -EINVAL;
6269
6270 /*
6271 * Since this string is allocated at mount time, and held until the
6272 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6273 * about a memory-reclaim deadlock.
6274 */
6275 str = kmalloc(len, GFP_KERNEL);
6276 if (!str)
6277 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05006278
Chuck Levere984a552012-09-14 17:24:21 -04006279 rcu_read_lock();
Trond Myklebust1aee5512020-10-07 18:24:17 -04006280 if (buflen)
Chuck Lever025bb9f2018-06-04 10:53:34 -04006281 scnprintf(str, len, "Linux NFSv4.0 %s/%s/%s",
Trond Myklebust1aee5512020-10-07 18:24:17 -04006282 clp->cl_rpcclient->cl_nodename, buf,
Chuck Lever848a4eb2018-06-04 10:53:29 -04006283 rpc_peeraddr2str(clp->cl_rpcclient,
Chuck Lever025bb9f2018-06-04 10:53:34 -04006284 RPC_DISPLAY_ADDR));
Chuck Lever848a4eb2018-06-04 10:53:29 -04006285 else
Chuck Lever025bb9f2018-06-04 10:53:34 -04006286 scnprintf(str, len, "Linux NFSv4.0 %s/%s",
Chuck Lever848a4eb2018-06-04 10:53:29 -04006287 clp->cl_rpcclient->cl_nodename,
6288 rpc_peeraddr2str(clp->cl_rpcclient,
Chuck Lever025bb9f2018-06-04 10:53:34 -04006289 RPC_DISPLAY_ADDR));
Chuck Levere984a552012-09-14 17:24:21 -04006290 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04006291
Jeff Laytona3192682015-06-09 19:43:59 -04006292 clp->cl_owner_id = str;
6293 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04006294}
6295
Jeff Layton873e3852015-06-09 19:44:00 -04006296static int
Jeff Layton873e3852015-06-09 19:44:00 -04006297nfs4_init_uniform_client_string(struct nfs_client *clp)
6298{
Trond Myklebust1aee5512020-10-07 18:24:17 -04006299 char buf[NFS4_CLIENT_ID_UNIQ_LEN];
6300 size_t buflen;
Jeff Layton873e3852015-06-09 19:44:00 -04006301 size_t len;
6302 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05006303
6304 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04006305 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04006306
Jeff Layton873e3852015-06-09 19:44:00 -04006307 len = 10 + 10 + 1 + 10 + 1 +
6308 strlen(clp->cl_rpcclient->cl_nodename) + 1;
6309
Trond Myklebust39d43d12020-10-07 18:24:18 -04006310 buflen = nfs4_get_uniquifier(clp, buf, sizeof(buf));
Trond Myklebust1aee5512020-10-07 18:24:17 -04006311 if (buflen)
6312 len += buflen + 1;
6313
Jeff Layton873e3852015-06-09 19:44:00 -04006314 if (len > NFS4_OPAQUE_LIMIT + 1)
6315 return -EINVAL;
6316
6317 /*
6318 * Since this string is allocated at mount time, and held until the
6319 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6320 * about a memory-reclaim deadlock.
6321 */
6322 str = kmalloc(len, GFP_KERNEL);
6323 if (!str)
6324 return -ENOMEM;
6325
Trond Myklebust1aee5512020-10-07 18:24:17 -04006326 if (buflen)
6327 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
6328 clp->rpc_ops->version, clp->cl_minorversion,
6329 buf, clp->cl_rpcclient->cl_nodename);
6330 else
6331 scnprintf(str, len, "Linux NFSv%u.%u %s",
6332 clp->rpc_ops->version, clp->cl_minorversion,
6333 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04006334 clp->cl_owner_id = str;
6335 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04006336}
6337
Chuck Lever706cb8d2014-03-12 12:51:47 -04006338/*
6339 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
6340 * services. Advertise one based on the address family of the
6341 * clientaddr.
6342 */
6343static unsigned int
6344nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
6345{
6346 if (strchr(clp->cl_ipaddr, ':') != NULL)
6347 return scnprintf(buf, len, "tcp6");
6348 else
6349 return scnprintf(buf, len, "tcp");
6350}
6351
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006352static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
6353{
6354 struct nfs4_setclientid *sc = calldata;
6355
6356 if (task->tk_status == 0)
6357 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
6358}
6359
6360static const struct rpc_call_ops nfs4_setclientid_ops = {
6361 .rpc_call_done = nfs4_setclientid_done,
6362};
6363
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006364/**
6365 * nfs4_proc_setclientid - Negotiate client ID
6366 * @clp: state data structure
6367 * @program: RPC program for NFSv4 callback service
6368 * @port: IP port number for NFS4 callback service
NeilBrowna52458b2018-12-03 11:30:31 +11006369 * @cred: credential to use for this call
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006370 * @res: where to place the result
6371 *
6372 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6373 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006374int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
NeilBrowna52458b2018-12-03 11:30:31 +11006375 unsigned short port, const struct cred *cred,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006376 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006377{
6378 nfs4_verifier sc_verifier;
6379 struct nfs4_setclientid setclientid = {
6380 .sc_verifier = &sc_verifier,
6381 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04006382 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006383 };
6384 struct rpc_message msg = {
6385 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
6386 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006387 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01006388 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006389 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006390 struct rpc_task_setup task_setup_data = {
6391 .rpc_client = clp->cl_rpcclient,
6392 .rpc_message = &msg,
6393 .callback_ops = &nfs4_setclientid_ops,
6394 .callback_data = &setclientid,
NeilBrown5a0c2572019-05-30 10:41:28 +10006395 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006396 };
Robert Milkowski7dc29932020-01-30 09:43:25 +00006397 unsigned long now = jiffies;
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006398 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006399
Chuck Leverde734832012-07-11 16:30:50 -04006400 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04006401 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04006402
6403 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
6404 status = nfs4_init_uniform_client_string(clp);
6405 else
Jeff Laytona3192682015-06-09 19:43:59 -04006406 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04006407
6408 if (status)
6409 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04006410
Chuck Leverde734832012-07-11 16:30:50 -04006411 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04006412 setclientid.sc_netid_len =
6413 nfs4_init_callback_netid(clp,
6414 setclientid.sc_netid,
6415 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04006416 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05006417 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006418 clp->cl_ipaddr, port >> 8, port & 255);
6419
Jeff Layton3a6bb732015-06-09 19:43:57 -04006420 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006421 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04006422 clp->cl_owner_id);
Anna Schumakerdae40962019-08-14 15:28:28 -04006423
6424 status = nfs4_call_sync_custom(&task_setup_data);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006425 if (setclientid.sc_cred) {
Chuck Lever1047ec82019-10-04 09:58:54 -04006426 kfree(clp->cl_acceptor);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006427 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
6428 put_rpccred(setclientid.sc_cred);
6429 }
Robert Milkowski7dc29932020-01-30 09:43:25 +00006430
6431 if (status == 0)
6432 do_renew_lease(clp, now);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006433out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006434 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006435 dprintk("NFS reply setclientid: %d\n", status);
6436 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006437}
6438
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006439/**
6440 * nfs4_proc_setclientid_confirm - Confirm client ID
6441 * @clp: state data structure
Trond Myklebust302fad72019-02-18 13:32:38 -05006442 * @arg: result of a previous SETCLIENTID
NeilBrowna52458b2018-12-03 11:30:31 +11006443 * @cred: credential to use for this call
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006444 *
6445 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6446 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04006447int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006448 struct nfs4_setclientid_res *arg,
NeilBrowna52458b2018-12-03 11:30:31 +11006449 const struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006450{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006451 struct rpc_message msg = {
6452 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006453 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01006454 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006455 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006456 int status;
6457
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006458 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
6459 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6460 clp->cl_clientid);
NeilBrown5a0c2572019-05-30 10:41:28 +10006461 status = rpc_call_sync(clp->cl_rpcclient, &msg,
6462 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006463 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006464 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006465 return status;
6466}
6467
Trond Myklebustfe650402006-01-03 09:55:18 +01006468struct nfs4_delegreturndata {
6469 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01006470 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01006471 struct nfs_fh fh;
6472 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006473 unsigned long timestamp;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006474 struct {
6475 struct nfs4_layoutreturn_args arg;
6476 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04006477 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006478 u32 roc_barrier;
6479 bool roc;
6480 } lr;
Trond Myklebustfa178f22006-01-03 09:55:38 +01006481 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01006482 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08006483 struct inode *inode;
Trond Myklebustfe650402006-01-03 09:55:18 +01006484};
6485
Trond Myklebustfe650402006-01-03 09:55:18 +01006486static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
6487{
6488 struct nfs4_delegreturndata *data = calldata;
Trond Myklebuste0dba012017-11-07 11:02:32 -05006489 struct nfs4_exception exception = {
6490 .inode = data->inode,
6491 .stateid = &data->stateid,
Trond Myklebustdfe1fe72021-06-01 11:10:05 -04006492 .task_is_privileged = data->args.seq_args.sa_privileged,
Trond Myklebuste0dba012017-11-07 11:02:32 -05006493 };
Andy Adamson938e1012009-04-01 09:22:28 -04006494
Trond Myklebust14516c32010-07-31 14:29:06 -04006495 if (!nfs4_sequence_done(task, &data->res.seq_res))
6496 return;
Andy Adamson938e1012009-04-01 09:22:28 -04006497
Trond Myklebustca8acf82013-08-13 10:36:56 -04006498 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Trond Myklebust586f1c32016-11-15 15:03:33 -05006499
6500 /* Handle Layoutreturn errors */
Trond Myklebust078000d2021-01-04 13:18:03 -05006501 if (pnfs_roc_done(task, &data->args.lr_args, &data->res.lr_res,
6502 &data->res.lr_ret) == -EAGAIN)
Trond Myklebust287a9c52019-09-20 07:23:41 -04006503 goto out_restart;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006504
Ricardo Labiaga79708862009-12-07 09:23:21 -05006505 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05006506 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01006507 renew_lease(data->res.server, data->timestamp);
Trond Myklebust23ea44c2016-11-10 16:06:28 -05006508 break;
Trond Myklebustc97cf602013-11-19 16:34:14 -05006509 case -NFS4ERR_ADMIN_REVOKED:
6510 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust26d36302016-09-22 13:39:05 -04006511 case -NFS4ERR_EXPIRED:
6512 nfs4_free_revoked_stateid(data->res.server,
6513 data->args.stateid,
6514 task->tk_msg.rpc_cred);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05006515 fallthrough;
Trond Myklebustc97cf602013-11-19 16:34:14 -05006516 case -NFS4ERR_BAD_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05006517 case -NFS4ERR_STALE_STATEID:
Trond Myklebust244fcd22019-12-20 10:43:37 -05006518 case -ETIMEDOUT:
Trond Myklebustc97cf602013-11-19 16:34:14 -05006519 task->tk_status = 0;
6520 break;
Trond Myklebust12f275c2017-11-06 15:28:05 -05006521 case -NFS4ERR_OLD_STATEID:
Trond Myklebust246afc0a2019-10-24 18:00:35 -04006522 if (!nfs4_refresh_delegation_stateid(&data->stateid, data->inode))
6523 nfs4_stateid_seqid_inc(&data->stateid);
Trond Myklebust70d136b2019-10-26 22:37:40 -04006524 if (data->args.bitmask) {
6525 data->args.bitmask = NULL;
6526 data->res.fattr = NULL;
6527 }
Trond Myklebust246afc0a2019-10-24 18:00:35 -04006528 goto out_restart;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05006529 case -NFS4ERR_ACCESS:
6530 if (data->args.bitmask) {
6531 data->args.bitmask = NULL;
6532 data->res.fattr = NULL;
Trond Myklebust140087fd2017-11-06 15:28:10 -05006533 goto out_restart;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05006534 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05006535 fallthrough;
Ricardo Labiaga79708862009-12-07 09:23:21 -05006536 default:
Trond Myklebuste0dba012017-11-07 11:02:32 -05006537 task->tk_status = nfs4_async_handle_exception(task,
6538 data->res.server, task->tk_status,
6539 &exception);
6540 if (exception.retry)
Trond Myklebust140087fd2017-11-06 15:28:10 -05006541 goto out_restart;
Ricardo Labiaga79708862009-12-07 09:23:21 -05006542 }
Trond Myklebustd51f91d2019-10-21 14:22:14 -04006543 nfs_delegation_mark_returned(data->inode, data->args.stateid);
Ricardo Labiaga79708862009-12-07 09:23:21 -05006544 data->rpc_status = task->tk_status;
Trond Myklebust140087fd2017-11-06 15:28:10 -05006545 return;
Trond Myklebust140087fd2017-11-06 15:28:10 -05006546out_restart:
6547 task->tk_status = 0;
6548 rpc_restart_call_prepare(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01006549}
6550
6551static void nfs4_delegreturn_release(void *calldata)
6552{
Peng Tao039b7562014-07-03 13:05:02 +08006553 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006554 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08006555
Trond Myklebust078000d2021-01-04 13:18:03 -05006556 if (data->lr.roc)
6557 pnfs_roc_release(&data->lr.arg, &data->lr.res,
6558 data->res.lr_ret);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006559 if (inode) {
Trond Myklebust0bc2c9b2016-12-16 19:48:09 -05006560 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006561 nfs_iput_and_deactive(inode);
6562 }
Trond Myklebustfe650402006-01-03 09:55:18 +01006563 kfree(calldata);
6564}
6565
Andy Adamson938e1012009-04-01 09:22:28 -04006566static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
6567{
6568 struct nfs4_delegreturndata *d_data;
Trond Myklebustc8bf7072018-06-15 16:31:02 -04006569 struct pnfs_layout_hdr *lo;
Andy Adamson938e1012009-04-01 09:22:28 -04006570
6571 d_data = (struct nfs4_delegreturndata *)data;
6572
Trond Myklebust5326de92019-11-13 09:39:36 +01006573 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task)) {
6574 nfs4_sequence_done(task, &d_data->res.seq_res);
Peng Tao500d7012015-09-22 11:35:22 +08006575 return;
Trond Myklebust5326de92019-11-13 09:39:36 +01006576 }
Peng Tao500d7012015-09-22 11:35:22 +08006577
Trond Myklebustc8bf7072018-06-15 16:31:02 -04006578 lo = d_data->args.lr_args ? d_data->args.lr_args->layout : NULL;
6579 if (lo && !pnfs_layout_is_valid(lo)) {
6580 d_data->args.lr_args = NULL;
6581 d_data->res.lr_res = NULL;
6582 }
6583
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006584 nfs4_setup_sequence(d_data->res.server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006585 &d_data->args.seq_args,
6586 &d_data->res.seq_res,
6587 task);
Andy Adamson938e1012009-04-01 09:22:28 -04006588}
Andy Adamson938e1012009-04-01 09:22:28 -04006589
Jesper Juhlc8d149f2006-03-20 13:44:07 -05006590static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04006591 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01006592 .rpc_call_done = nfs4_delegreturn_done,
6593 .rpc_release = nfs4_delegreturn_release,
6594};
6595
NeilBrowna52458b2018-12-03 11:30:31 +11006596static int _nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01006597{
6598 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01006599 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01006600 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006601 struct rpc_message msg = {
6602 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
6603 .rpc_cred = cred,
6604 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006605 struct rpc_task_setup task_setup_data = {
6606 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04006607 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006608 .callback_ops = &nfs4_delegreturn_ops,
Olga Kornievskaia85e39fe2021-06-23 23:28:51 -04006609 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT | RPC_TASK_MOVEABLE,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006610 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05006611 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01006612
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006613 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01006614 if (data == NULL)
6615 return -ENOMEM;
Andrew Elble99ade3c2015-12-02 09:39:51 -05006616
6617 nfs4_state_protect(server->nfs_client,
6618 NFS_SP4_MACH_CRED_CLEANUP,
6619 &task_setup_data.rpc_client, &msg);
6620
Trond Myklebustfe650402006-01-03 09:55:18 +01006621 data->args.fhandle = &data->fh;
6622 data->args.stateid = &data->stateid;
Trond Myklebust332d1a02021-03-25 18:15:36 -04006623 nfs4_bitmask_set(data->args.bitmask_store,
Trond Myklebust85847282021-12-27 14:40:51 -05006624 server->cache_consistency_bitmask, inode, 0);
Trond Myklebust332d1a02021-03-25 18:15:36 -04006625 data->args.bitmask = data->args.bitmask_store;
Trond Myklebustfe650402006-01-03 09:55:18 +01006626 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05006627 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01006628 data->res.fattr = &data->fattr;
6629 data->res.server = server;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006630 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust4d796d72016-09-23 11:38:08 -04006631 data->lr.arg.ld_private = &data->lr.ld_private;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006632 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01006633 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01006634 data->rpc_status = 0;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006635 data->inode = nfs_igrab_and_active(inode);
Trond Myklebust078000d2021-01-04 13:18:03 -05006636 if (data->inode || issync) {
6637 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res,
6638 cred);
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006639 if (data->lr.roc) {
6640 data->args.lr_args = &data->lr.arg;
6641 data->res.lr_res = &data->lr.res;
6642 }
6643 }
Trond Myklebustfe650402006-01-03 09:55:18 +01006644
Trond Myklebustdfe1fe72021-06-01 11:10:05 -04006645 if (!data->inode)
6646 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1,
6647 1);
6648 else
6649 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1,
6650 0);
Trond Myklebustc970aa82007-07-14 15:39:59 -04006651 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05006652 msg.rpc_argp = &data->args;
6653 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006654 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006655 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01006656 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05006657 if (!issync)
6658 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05006659 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05006660 if (status != 0)
6661 goto out;
6662 status = data->rpc_status;
Trond Myklebuste6f81072008-01-24 18:14:34 -05006663out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006664 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01006665 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006666}
6667
NeilBrowna52458b2018-12-03 11:30:31 +11006668int nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006669{
6670 struct nfs_server *server = NFS_SERVER(inode);
6671 struct nfs4_exception exception = { };
6672 int err;
6673 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05006674 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006675 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006676 switch (err) {
6677 case -NFS4ERR_STALE_STATEID:
6678 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006679 case 0:
6680 return 0;
6681 }
6682 err = nfs4_handle_exception(server, err, &exception);
6683 } while (exception.retry);
6684 return err;
6685}
6686
Linus Torvalds1da177e2005-04-16 15:20:36 -07006687static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6688{
6689 struct inode *inode = state->inode;
6690 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04006691 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006692 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006693 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006694 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006695 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006696 struct nfs_lockt_res res = {
6697 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006698 };
6699 struct rpc_message msg = {
6700 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
Anna Schumakerd9b67e12017-01-11 15:04:25 -05006701 .rpc_argp = &arg,
6702 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006703 .rpc_cred = state->owner->so_cred,
6704 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006705 struct nfs4_lock_state *lsp;
6706 int status;
6707
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006708 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00006709 status = nfs4_set_lock_state(state, request);
6710 if (status != 0)
6711 goto out;
6712 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006713 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006714 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00006715 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006716 switch (status) {
6717 case 0:
6718 request->fl_type = F_UNLCK;
6719 break;
6720 case -NFS4ERR_DENIED:
6721 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006722 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05006723 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04006724 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00006725out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006726 return status;
6727}
6728
6729static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6730{
Trond Myklebust0688e642019-04-07 13:59:09 -04006731 struct nfs4_exception exception = {
6732 .interruptible = true,
6733 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006734 int err;
6735
6736 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006737 err = _nfs4_proc_getlk(state, cmd, request);
6738 trace_nfs4_get_lock(request, state, cmd, err);
6739 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006740 &exception);
6741 } while (exception.retry);
6742 return err;
6743}
6744
Trond Myklebust32c6e7e2019-09-20 07:23:48 -04006745/*
6746 * Update the seqid of a lock stateid after receiving
6747 * NFS4ERR_OLD_STATEID
6748 */
6749static bool nfs4_refresh_lock_old_stateid(nfs4_stateid *dst,
6750 struct nfs4_lock_state *lsp)
6751{
6752 struct nfs4_state *state = lsp->ls_state;
6753 bool ret = false;
6754
6755 spin_lock(&state->state_lock);
6756 if (!nfs4_stateid_match_other(dst, &lsp->ls_stateid))
6757 goto out;
6758 if (!nfs4_stateid_is_newer(&lsp->ls_stateid, dst))
6759 nfs4_stateid_seqid_inc(dst);
6760 else
6761 dst->seqid = lsp->ls_stateid.seqid;
6762 ret = true;
6763out:
6764 spin_unlock(&state->state_lock);
6765 return ret;
6766}
6767
6768static bool nfs4_sync_lock_stateid(nfs4_stateid *dst,
6769 struct nfs4_lock_state *lsp)
6770{
6771 struct nfs4_state *state = lsp->ls_state;
6772 bool ret;
6773
6774 spin_lock(&state->state_lock);
6775 ret = !nfs4_stateid_match_other(dst, &lsp->ls_stateid);
6776 nfs4_stateid_copy(dst, &lsp->ls_stateid);
6777 spin_unlock(&state->state_lock);
6778 return ret;
6779}
6780
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006781struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006782 struct nfs_locku_args arg;
6783 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006784 struct nfs4_lock_state *lsp;
6785 struct nfs_open_context *ctx;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006786 struct nfs_lock_context *l_ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006787 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04006788 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006789 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006790};
6791
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006792static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
6793 struct nfs_open_context *ctx,
6794 struct nfs4_lock_state *lsp,
6795 struct nfs_seqid *seqid)
6796{
6797 struct nfs4_unlockdata *p;
Trond Myklebust32c6e7e2019-09-20 07:23:48 -04006798 struct nfs4_state *state = lsp->ls_state;
6799 struct inode *inode = state->inode;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006800
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006801 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006802 if (p == NULL)
6803 return NULL;
6804 p->arg.fh = NFS_FH(inode);
6805 p->arg.fl = &p->fl;
6806 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006807 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006808 p->lsp = lsp;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006809 /* Ensure we don't close file until we're done freeing locks! */
6810 p->ctx = get_nfs_open_context(ctx);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006811 p->l_ctx = nfs_get_lock_context(ctx);
NeilBrown7b587e12018-11-30 10:04:08 +11006812 locks_init_lock(&p->fl);
6813 locks_copy_lock(&p->fl, fl);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006814 p->server = NFS_SERVER(inode);
Trond Myklebust32c6e7e2019-09-20 07:23:48 -04006815 spin_lock(&state->state_lock);
6816 nfs4_stateid_copy(&p->arg.stateid, &lsp->ls_stateid);
6817 spin_unlock(&state->state_lock);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006818 return p;
6819}
6820
Trond Myklebust06f814a2006-01-03 09:55:07 +01006821static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006822{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006823 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006824 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006825 nfs4_put_lock_state(calldata->lsp);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006826 nfs_put_lock_context(calldata->l_ctx);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006827 put_nfs_open_context(calldata->ctx);
6828 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006829}
6830
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006831static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006832{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006833 struct nfs4_unlockdata *calldata = data;
Trond Myklebust82571552017-11-07 11:14:49 -05006834 struct nfs4_exception exception = {
6835 .inode = calldata->lsp->ls_state->inode,
6836 .stateid = &calldata->arg.stateid,
6837 };
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006838
Trond Myklebust14516c32010-07-31 14:29:06 -04006839 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
6840 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006841 switch (task->tk_status) {
6842 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01006843 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton75575dd2016-09-17 18:17:32 -04006844 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006845 if (nfs4_update_lock_stateid(calldata->lsp,
6846 &calldata->res.stateid))
6847 break;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05006848 fallthrough;
Trond Myklebust26d36302016-09-22 13:39:05 -04006849 case -NFS4ERR_ADMIN_REVOKED:
6850 case -NFS4ERR_EXPIRED:
6851 nfs4_free_revoked_stateid(calldata->server,
6852 &calldata->arg.stateid,
6853 task->tk_msg.rpc_cred);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05006854 fallthrough;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05006855 case -NFS4ERR_BAD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006856 case -NFS4ERR_STALE_STATEID:
Trond Myklebust32c6e7e2019-09-20 07:23:48 -04006857 if (nfs4_sync_lock_stateid(&calldata->arg.stateid,
6858 calldata->lsp))
6859 rpc_restart_call_prepare(task);
6860 break;
6861 case -NFS4ERR_OLD_STATEID:
6862 if (nfs4_refresh_lock_old_stateid(&calldata->arg.stateid,
6863 calldata->lsp))
Trond Myklebust425c1d42015-01-24 14:57:53 -05006864 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006865 break;
6866 default:
Trond Myklebust82571552017-11-07 11:14:49 -05006867 task->tk_status = nfs4_async_handle_exception(task,
6868 calldata->server, task->tk_status,
6869 &exception);
6870 if (exception.retry)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006871 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006872 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04006873 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006874}
6875
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006876static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006877{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006878 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006879
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006880 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
6881 nfs_async_iocounter_wait(task, calldata->l_ctx))
6882 return;
6883
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006884 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006885 goto out_wait;
Trond Myklebust795a88c2012-09-10 13:26:49 -04006886 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006887 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006888 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006889 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006890 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006891 if (nfs4_setup_sequence(calldata->server->nfs_client,
Andy Adamsona8936932009-04-01 09:22:23 -04006892 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006893 &calldata->res.seq_res,
6894 task) != 0)
6895 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006896 return;
6897out_no_action:
6898 task->tk_action = NULL;
6899out_wait:
6900 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006901}
6902
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006903static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006904 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006905 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01006906 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006907};
6908
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006909static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6910 struct nfs_open_context *ctx,
6911 struct nfs4_lock_state *lsp,
6912 struct nfs_seqid *seqid)
6913{
6914 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006915 struct rpc_message msg = {
6916 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6917 .rpc_cred = ctx->cred,
6918 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006919 struct rpc_task_setup task_setup_data = {
6920 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006921 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006922 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006923 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006924 .flags = RPC_TASK_ASYNC,
6925 };
Olga Kornievskaia85e39fe2021-06-23 23:28:51 -04006926 struct nfs_client *client =
6927 NFS_SERVER(lsp->ls_state->inode)->nfs_client;
6928
6929 if (client->cl_minorversion)
6930 task_setup_data.flags |= RPC_TASK_MOVEABLE;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006931
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006932 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6933 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6934
Frank Filz137d6ac2007-07-09 15:32:29 -07006935 /* Ensure this is an unlock - when canceling a lock, the
6936 * canceled lock is passed in, and it won't be an unlock.
6937 */
6938 fl->fl_type = F_UNLCK;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006939 if (fl->fl_flags & FL_CLOSE)
6940 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
Frank Filz137d6ac2007-07-09 15:32:29 -07006941
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006942 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6943 if (data == NULL) {
6944 nfs_free_seqid(seqid);
6945 return ERR_PTR(-ENOMEM);
6946 }
6947
Anna Schumakerfba83f32018-05-04 16:22:50 -04006948 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1, 0);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006949 msg.rpc_argp = &data->arg;
6950 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006951 task_setup_data.callback_data = data;
6952 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006953}
6954
Linus Torvalds1da177e2005-04-16 15:20:36 -07006955static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6956{
Trond Myklebust65b62a22013-02-07 10:54:07 -05006957 struct inode *inode = state->inode;
6958 struct nfs4_state_owner *sp = state->owner;
6959 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006960 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006961 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01006962 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006963 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006964 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006965 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006966
Trond Myklebust9b073572006-06-29 16:38:34 -04006967 status = nfs4_set_lock_state(state, request);
6968 /* Unlock _before_ we do the RPC call */
6969 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05006970 /* Exclude nfs_delegation_claim_locks() */
6971 mutex_lock(&sp->so_delegreturn_mutex);
6972 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006973 down_read(&nfsi->rwsem);
Jeff Layton75575dd2016-09-17 18:17:32 -04006974 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05006975 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006976 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006977 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05006978 }
6979 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006980 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006981 if (status != 0)
6982 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006983 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006984 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04006985 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6986 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006987 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6988 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04006989 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006990 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04006991 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006992 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006993 status = PTR_ERR(task);
6994 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04006995 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05006996 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006997 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04006998out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006999 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04007000 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07007001 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007002}
7003
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007004struct nfs4_lockdata {
7005 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01007006 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007007 struct nfs4_lock_state *lsp;
7008 struct nfs_open_context *ctx;
7009 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01007010 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007011 int rpc_status;
7012 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04007013 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007014};
7015
7016static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04007017 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
7018 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007019{
7020 struct nfs4_lockdata *p;
7021 struct inode *inode = lsp->ls_state->inode;
7022 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05007023 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007024
Trond Myklebust8535b2b2010-05-13 12:51:01 -04007025 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007026 if (p == NULL)
7027 return NULL;
7028
7029 p->arg.fh = NFS_FH(inode);
7030 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04007031 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05007032 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05007033 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05007034 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
7035 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05007036 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05007037 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04007038 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05007039 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05007040 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04007041 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007042 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04007043 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007044 p->ctx = get_nfs_open_context(ctx);
NeilBrown7b587e12018-11-30 10:04:08 +11007045 locks_init_lock(&p->fl);
7046 locks_copy_lock(&p->fl, fl);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007047 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05007048out_free_seqid:
7049 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007050out_free:
7051 kfree(p);
7052 return NULL;
7053}
7054
7055static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
7056{
7057 struct nfs4_lockdata *data = calldata;
7058 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007059
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05007060 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05007061 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007062 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05007063 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007064 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04007065 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007066 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05007067 nfs4_stateid_copy(&data->arg.open_stateid,
7068 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007069 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04007070 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05007071 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05007072 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05007073 nfs4_stateid_copy(&data->arg.lock_stateid,
7074 &data->lsp->ls_stateid);
7075 }
Trond Myklebust5d422302013-03-14 16:57:48 -04007076 if (!nfs4_valid_open_stateid(state)) {
7077 data->rpc_status = -EBADF;
7078 task->tk_action = NULL;
7079 goto out_release_open_seqid;
7080 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01007081 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05007082 if (nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust035168ab2010-06-16 09:52:26 -04007083 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04007084 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007085 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04007086 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04007087out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04007088 nfs_release_seqid(data->arg.open_seqid);
7089out_release_lock_seqid:
7090 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05007091out_wait:
7092 nfs4_sequence_done(task, &data->res.seq_res);
Chuck Lever86882c72021-10-16 18:03:04 -04007093 dprintk("%s: ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08007094}
7095
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007096static void nfs4_lock_done(struct rpc_task *task, void *calldata)
7097{
7098 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05007099 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007100
Trond Myklebust14516c32010-07-31 14:29:06 -04007101 if (!nfs4_sequence_done(task, &data->res.seq_res))
7102 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04007103
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007104 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05007105 switch (task->tk_status) {
7106 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00007107 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05007108 data->timestamp);
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04007109 if (data->arg.new_lock && !data->cancelled) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05007110 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04007111 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0)
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04007112 goto out_restart;
Trond Myklebustc69899a2015-01-24 16:03:52 -05007113 }
Trond Myklebust39071e62015-01-24 15:07:56 -05007114 if (data->arg.new_lock_owner != 0) {
7115 nfs_confirm_seqid(&lsp->ls_seqid, 0);
7116 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
7117 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04007118 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
7119 goto out_restart;
Trond Myklebust425c1d42015-01-24 14:57:53 -05007120 break;
7121 case -NFS4ERR_BAD_STATEID:
7122 case -NFS4ERR_OLD_STATEID:
7123 case -NFS4ERR_STALE_STATEID:
7124 case -NFS4ERR_EXPIRED:
7125 if (data->arg.new_lock_owner != 0) {
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04007126 if (!nfs4_stateid_match(&data->arg.open_stateid,
Trond Myklebust425c1d42015-01-24 14:57:53 -05007127 &lsp->ls_state->open_stateid))
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04007128 goto out_restart;
7129 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
Trond Myklebust425c1d42015-01-24 14:57:53 -05007130 &lsp->ls_stateid))
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04007131 goto out_restart;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007132 }
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04007133out_done:
Chuck Lever86882c72021-10-16 18:03:04 -04007134 dprintk("%s: ret = %d!\n", __func__, data->rpc_status);
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04007135 return;
7136out_restart:
7137 if (!data->cancelled)
7138 rpc_restart_call_prepare(task);
7139 goto out_done;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007140}
7141
7142static void nfs4_lock_release(void *calldata)
7143{
7144 struct nfs4_lockdata *data = calldata;
7145
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05007146 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04007147 if (data->cancelled && data->rpc_status == 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007148 struct rpc_task *task;
7149 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
7150 data->arg.lock_seqid);
7151 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08007152 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07007153 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007154 } else
7155 nfs_free_seqid(data->arg.lock_seqid);
7156 nfs4_put_lock_state(data->lsp);
7157 put_nfs_open_context(data->ctx);
7158 kfree(data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007159}
7160
7161static const struct rpc_call_ops nfs4_lock_ops = {
7162 .rpc_call_prepare = nfs4_lock_prepare,
7163 .rpc_call_done = nfs4_lock_done,
7164 .rpc_release = nfs4_lock_release,
7165};
7166
Trond Myklebust2bee72a2010-01-26 15:42:21 -05007167static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
7168{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05007169 switch (error) {
7170 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustd7f3e4b2016-09-22 13:39:09 -04007171 case -NFS4ERR_EXPIRED:
Trond Myklebust2bee72a2010-01-26 15:42:21 -05007172 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05007173 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05007174 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04007175 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05007176 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05007177 break;
7178 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05007179 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05007180 nfs4_schedule_lease_recovery(server->nfs_client);
zhengbin8b98a532019-12-19 18:34:47 +08007181 }
Trond Myklebust2bee72a2010-01-26 15:42:21 -05007182}
7183
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08007184static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007185{
7186 struct nfs4_lockdata *data;
7187 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04007188 struct rpc_message msg = {
7189 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
7190 .rpc_cred = state->owner->so_cred,
7191 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04007192 struct rpc_task_setup task_setup_data = {
7193 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04007194 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04007195 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05007196 .workqueue = nfsiod_workqueue,
Trond Myklebust61296502020-02-07 19:38:12 -05007197 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
Trond Myklebustc970aa82007-07-14 15:39:59 -04007198 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007199 int ret;
Olga Kornievskaia85e39fe2021-06-23 23:28:51 -04007200 struct nfs_client *client = NFS_SERVER(state->inode)->nfs_client;
7201
7202 if (client->cl_minorversion)
7203 task_setup_data.flags |= RPC_TASK_MOVEABLE;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007204
Trond Myklebustcd3758e2007-08-10 17:44:32 -04007205 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04007206 fl->fl_u.nfs4_fl.owner,
7207 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007208 if (data == NULL)
7209 return -ENOMEM;
7210 if (IS_SETLKW(cmd))
7211 data->arg.block = 1;
Anna Schumakerfba83f32018-05-04 16:22:50 -04007212 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1,
7213 recovery_type > NFS_LOCK_NEW);
Trond Myklebust1174dd12010-12-21 10:52:24 -05007214 msg.rpc_argp = &data->arg;
7215 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04007216 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007217 if (recovery_type > NFS_LOCK_NEW) {
7218 if (recovery_type == NFS_LOCK_RECLAIM)
7219 data->arg.reclaim = NFS_LOCK_RECLAIM;
Trond Myklebustc69899a2015-01-24 16:03:52 -05007220 } else
7221 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04007222 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05007223 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007224 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05007225 ret = rpc_wait_for_completion_task(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007226 if (ret == 0) {
7227 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05007228 if (ret)
7229 nfs4_handle_setlk_error(data->server, data->lsp,
7230 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007231 } else
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04007232 data->cancelled = true;
Dave Wysochanski3d1a90a2020-12-11 05:12:51 -05007233 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05007234 rpc_put_task(task);
Chuck Lever86882c72021-10-16 18:03:04 -04007235 dprintk("%s: ret = %d\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007236 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007237}
7238
7239static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
7240{
Trond Myklebust202b50d2005-06-22 17:16:29 +00007241 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04007242 struct nfs4_exception exception = {
7243 .inode = state->inode,
7244 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00007245 int err;
7246
7247 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04007248 /* Cache the lock if possible... */
7249 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
7250 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08007251 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04007252 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00007253 break;
7254 nfs4_handle_exception(server, err, &exception);
7255 } while (exception.retry);
7256 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007257}
7258
7259static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
7260{
Trond Myklebust202b50d2005-06-22 17:16:29 +00007261 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04007262 struct nfs4_exception exception = {
7263 .inode = state->inode,
7264 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00007265 int err;
7266
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05007267 err = nfs4_set_lock_state(state, request);
7268 if (err != 0)
7269 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04007270 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10007271 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
7272 return 0;
7273 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00007274 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04007275 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
7276 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08007277 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05007278 switch (err) {
7279 default:
7280 goto out;
7281 case -NFS4ERR_GRACE:
7282 case -NFS4ERR_DELAY:
7283 nfs4_handle_exception(server, err, &exception);
7284 err = 0;
7285 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00007286 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05007287out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00007288 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007289}
7290
Bryan Schumakerf062eb62011-06-02 14:59:10 -04007291#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05007292static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
7293{
Trond Myklebustc5896fc2016-09-22 13:39:03 -04007294 struct nfs4_lock_state *lsp;
7295 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05007296
Trond Myklebustc5896fc2016-09-22 13:39:03 -04007297 status = nfs4_set_lock_state(state, request);
7298 if (status != 0)
7299 return status;
7300 lsp = request->fl_u.nfs4_fl.owner;
7301 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
7302 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
7303 return 0;
Anna Schumaker81b68de2017-01-11 16:41:34 -05007304 return nfs4_lock_expired(state, request);
Bryan Schumakerf062eb62011-06-02 14:59:10 -04007305}
7306#endif
7307
Linus Torvalds1da177e2005-04-16 15:20:36 -07007308static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7309{
Trond Myklebust19e03c52008-12-23 15:21:44 -05007310 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04007311 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04007312 unsigned char fl_flags = request->fl_flags;
Jeff Layton1ea67db2016-09-17 18:17:37 -04007313 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007314
Trond Myklebust01c3b862006-06-29 16:38:39 -04007315 request->fl_flags |= FL_ACCESS;
Jeff Layton75575dd2016-09-17 18:17:32 -04007316 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04007317 if (status < 0)
7318 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04007319 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05007320 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04007321 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04007322 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04007323 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05007324 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton75575dd2016-09-17 18:17:32 -04007325 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05007326 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04007327 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05007328 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04007329 }
Trond Myklebust9a99af42013-02-04 20:17:49 -05007330 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04007331 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08007332 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04007333out:
7334 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007335 return status;
7336}
7337
7338static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7339{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05007340 struct nfs4_exception exception = {
7341 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04007342 .inode = state->inode,
Trond Myklebust0688e642019-04-07 13:59:09 -04007343 .interruptible = true,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05007344 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07007345 int err;
7346
7347 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07007348 err = _nfs4_proc_setlk(state, cmd, request);
7349 if (err == -NFS4ERR_DENIED)
7350 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007351 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07007352 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007353 } while (exception.retry);
7354 return err;
7355}
7356
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04007357#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
7358#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
7359
7360static int
Jeff Laytona1d617d82016-09-17 18:17:39 -04007361nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
7362 struct file_lock *request)
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04007363{
7364 int status = -ERESTARTSYS;
7365 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
7366
7367 while(!signalled()) {
7368 status = nfs4_proc_setlk(state, cmd, request);
7369 if ((status != -EAGAIN) || IS_SETLK(cmd))
7370 break;
7371 freezable_schedule_timeout_interruptible(timeout);
7372 timeout *= 2;
7373 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
7374 status = -ERESTARTSYS;
7375 }
7376 return status;
7377}
7378
Jeff Laytona1d617d82016-09-17 18:17:39 -04007379#ifdef CONFIG_NFS_V4_1
7380struct nfs4_lock_waiter {
Jeff Laytona1d617d82016-09-17 18:17:39 -04007381 struct inode *inode;
Trond Myklebust86438182021-02-02 14:22:51 -05007382 struct nfs_lowner owner;
7383 wait_queue_entry_t wait;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007384};
7385
7386static int
Ingo Molnarac6424b2017-06-20 12:06:13 +02007387nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
Jeff Laytona1d617d82016-09-17 18:17:39 -04007388{
Trond Myklebust86438182021-02-02 14:22:51 -05007389 struct nfs4_lock_waiter *waiter =
7390 container_of(wait, struct nfs4_lock_waiter, wait);
Jeff Laytona1d617d82016-09-17 18:17:39 -04007391
Jeff Layton57174592018-03-18 08:37:03 -04007392 /* NULL key means to wake up everyone */
7393 if (key) {
7394 struct cb_notify_lock_args *cbnl = key;
7395 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
Trond Myklebust86438182021-02-02 14:22:51 -05007396 *wowner = &waiter->owner;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007397
Jeff Layton57174592018-03-18 08:37:03 -04007398 /* Only wake if the callback was for the same owner. */
7399 if (lowner->id != wowner->id || lowner->s_dev != wowner->s_dev)
7400 return 0;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007401
Jeff Layton57174592018-03-18 08:37:03 -04007402 /* Make sure it's for the right inode */
7403 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
7404 return 0;
Jeff Layton57174592018-03-18 08:37:03 -04007405 }
Jeff Laytona1d617d82016-09-17 18:17:39 -04007406
Trond Myklebust86438182021-02-02 14:22:51 -05007407 return woken_wake_function(wait, mode, flags, key);
Jeff Laytona1d617d82016-09-17 18:17:39 -04007408}
7409
7410static int
7411nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7412{
Jeff Laytona1d617d82016-09-17 18:17:39 -04007413 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
7414 struct nfs_server *server = NFS_SERVER(state->inode);
7415 struct nfs_client *clp = server->nfs_client;
7416 wait_queue_head_t *q = &clp->cl_lock_waitq;
Trond Myklebust86438182021-02-02 14:22:51 -05007417 struct nfs4_lock_waiter waiter = {
7418 .inode = state->inode,
7419 .owner = { .clientid = clp->cl_clientid,
7420 .id = lsp->ls_seqid.owner_id,
7421 .s_dev = server->s_dev },
7422 };
7423 int status;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007424
7425 /* Don't bother with waitqueue if we don't expect a callback */
7426 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
7427 return nfs4_retry_setlk_simple(state, cmd, request);
7428
Trond Myklebust86438182021-02-02 14:22:51 -05007429 init_wait(&waiter.wait);
7430 waiter.wait.func = nfs4_wake_lock_waiter;
7431 add_wait_queue(q, &waiter.wait);
Jeff Laytona1d617d82016-09-17 18:17:39 -04007432
Trond Myklebust86438182021-02-02 14:22:51 -05007433 do {
Jeff Laytona1d617d82016-09-17 18:17:39 -04007434 status = nfs4_proc_setlk(state, cmd, request);
Trond Myklebust86438182021-02-02 14:22:51 -05007435 if (status != -EAGAIN || IS_SETLK(cmd))
Jeff Laytona1d617d82016-09-17 18:17:39 -04007436 break;
7437
7438 status = -ERESTARTSYS;
Yihao Wu52b042a2019-05-22 01:57:10 +08007439 freezer_do_not_count();
Trond Myklebust86438182021-02-02 14:22:51 -05007440 wait_woken(&waiter.wait, TASK_INTERRUPTIBLE,
7441 NFS4_LOCK_MAXTIMEOUT);
Yihao Wu52b042a2019-05-22 01:57:10 +08007442 freezer_count();
Trond Myklebust86438182021-02-02 14:22:51 -05007443 } while (!signalled());
7444
7445 remove_wait_queue(q, &waiter.wait);
Jeff Laytona1d617d82016-09-17 18:17:39 -04007446
Jeff Laytona1d617d82016-09-17 18:17:39 -04007447 return status;
7448}
7449#else /* !CONFIG_NFS_V4_1 */
7450static inline int
7451nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7452{
7453 return nfs4_retry_setlk_simple(state, cmd, request);
7454}
7455#endif
7456
Linus Torvalds1da177e2005-04-16 15:20:36 -07007457static int
7458nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
7459{
7460 struct nfs_open_context *ctx;
7461 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007462 int status;
7463
7464 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04007465 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007466 state = ctx->state;
7467
Trond Myklebustd9531262009-07-21 19:22:38 -04007468 if (IS_GETLK(cmd)) {
7469 if (state != NULL)
7470 return nfs4_proc_getlk(state, F_GETLK, request);
7471 return 0;
7472 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007473
7474 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
7475 return -EINVAL;
7476
Trond Myklebustd9531262009-07-21 19:22:38 -04007477 if (request->fl_type == F_UNLCK) {
7478 if (state != NULL)
7479 return nfs4_proc_unlck(state, cmd, request);
7480 return 0;
7481 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007482
Trond Myklebustd9531262009-07-21 19:22:38 -04007483 if (state == NULL)
7484 return -ENOLCK;
Jeff Layton1ea67db2016-09-17 18:17:37 -04007485
7486 if ((request->fl_flags & FL_POSIX) &&
7487 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
7488 return -ENOLCK;
7489
Benjamin Coddingtonfcfa4472017-11-10 06:27:49 -05007490 /*
7491 * Don't rely on the VFS having checked the file open mode,
7492 * since it won't do this for flock() locks.
7493 */
7494 switch (request->fl_type) {
7495 case F_RDLCK:
7496 if (!(filp->f_mode & FMODE_READ))
7497 return -EBADF;
7498 break;
7499 case F_WRLCK:
7500 if (!(filp->f_mode & FMODE_WRITE))
7501 return -EBADF;
7502 }
7503
Jeff Layton1ea67db2016-09-17 18:17:37 -04007504 status = nfs4_set_lock_state(state, request);
7505 if (status != 0)
7506 return status;
7507
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04007508 return nfs4_retry_setlk(state, cmd, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007509}
7510
Trond Myklebuste93a5e92021-05-07 10:06:13 -04007511static int nfs4_delete_lease(struct file *file, void **priv)
7512{
7513 return generic_setlease(file, F_UNLCK, NULL, priv);
7514}
7515
7516static int nfs4_add_lease(struct file *file, long arg, struct file_lock **lease,
7517 void **priv)
7518{
7519 struct inode *inode = file_inode(file);
7520 fmode_t type = arg == F_RDLCK ? FMODE_READ : FMODE_WRITE;
7521 int ret;
7522
7523 /* No delegation, no lease */
7524 if (!nfs4_have_delegation(inode, type))
Trond Myklebustdf2c7b92021-06-25 15:08:39 -04007525 return -EAGAIN;
Trond Myklebuste93a5e92021-05-07 10:06:13 -04007526 ret = generic_setlease(file, arg, lease, priv);
7527 if (ret || nfs4_have_delegation(inode, type))
7528 return ret;
7529 /* We raced with a delegation return */
7530 nfs4_delete_lease(file, priv);
Trond Myklebustdf2c7b92021-06-25 15:08:39 -04007531 return -EAGAIN;
Trond Myklebuste93a5e92021-05-07 10:06:13 -04007532}
7533
7534int nfs4_proc_setlease(struct file *file, long arg, struct file_lock **lease,
7535 void **priv)
7536{
7537 switch (arg) {
7538 case F_RDLCK:
7539 case F_WRLCK:
7540 return nfs4_add_lease(file, arg, lease, priv);
7541 case F_UNLCK:
7542 return nfs4_delete_lease(file, priv);
7543 default:
7544 return -EINVAL;
7545 }
7546}
7547
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04007548int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05007549{
7550 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05007551 int err;
7552
7553 err = nfs4_set_lock_state(state, fl);
7554 if (err != 0)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04007555 return err;
Olga Kornievskaia3d7a9522020-08-20 18:52:43 -04007556 do {
7557 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
7558 if (err != -NFS4ERR_DELAY)
7559 break;
7560 ssleep(1);
7561 } while (err == -NFS4ERR_DELAY);
NeilBrowndce26302017-12-13 09:57:09 +11007562 return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05007563}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007564
Trond Myklebustcf470c32012-03-07 13:49:12 -05007565struct nfs_release_lockowner_data {
7566 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04007567 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007568 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08007569 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04007570 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007571};
7572
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007573static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
7574{
7575 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08007576 struct nfs_server *server = data->server;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05007577 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
7578 &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08007579 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04007580 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007581}
7582
7583static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
7584{
7585 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04007586 struct nfs_server *server = data->server;
7587
Trond Myklebustb7e63a12014-02-26 11:19:14 -08007588 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04007589
7590 switch (task->tk_status) {
7591 case 0:
7592 renew_lease(server, data->timestamp);
7593 break;
7594 case -NFS4ERR_STALE_CLIENTID:
7595 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08007596 nfs4_schedule_lease_recovery(server->nfs_client);
7597 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04007598 case -NFS4ERR_LEASE_MOVED:
7599 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10007600 if (nfs4_async_handle_error(task, server,
7601 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04007602 rpc_restart_call_prepare(task);
7603 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007604}
7605
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007606static void nfs4_release_lockowner_release(void *calldata)
7607{
Trond Myklebustcf470c32012-03-07 13:49:12 -05007608 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04007609 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007610 kfree(calldata);
7611}
7612
Trond Myklebust17280172012-03-11 13:11:00 -04007613static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007614 .rpc_call_prepare = nfs4_release_lockowner_prepare,
7615 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007616 .rpc_release = nfs4_release_lockowner_release,
7617};
7618
Jeff Laytonf1cdae82014-05-01 06:28:47 -04007619static void
7620nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007621{
Trond Myklebustcf470c32012-03-07 13:49:12 -05007622 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007623 struct rpc_message msg = {
7624 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
7625 };
7626
7627 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04007628 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007629
Trond Myklebustcf470c32012-03-07 13:49:12 -05007630 data = kmalloc(sizeof(*data), GFP_NOFS);
7631 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04007632 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007633 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04007634 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007635 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7636 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
7637 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007638
Trond Myklebustcf470c32012-03-07 13:49:12 -05007639 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08007640 msg.rpc_resp = &data->res;
Anna Schumakerfba83f32018-05-04 16:22:50 -04007641 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05007642 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007643}
7644
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00007645#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
7646
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007647static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Christian Braunere65ce2a2021-01-21 14:19:27 +01007648 struct user_namespace *mnt_userns,
Al Viro59301222016-05-27 10:19:30 -04007649 struct dentry *unused, struct inode *inode,
7650 const char *key, const void *buf,
7651 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007652{
Al Viro59301222016-05-27 10:19:30 -04007653 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007654}
7655
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007656static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04007657 struct dentry *unused, struct inode *inode,
7658 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007659{
Al Virob2968212016-04-10 20:48:24 -04007660 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007661}
7662
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01007663static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007664{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01007665 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007666}
7667
David Quigleyc9bccef2013-05-22 12:50:45 -04007668#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04007669
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007670static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Christian Braunere65ce2a2021-01-21 14:19:27 +01007671 struct user_namespace *mnt_userns,
Al Viro59301222016-05-27 10:19:30 -04007672 struct dentry *unused, struct inode *inode,
7673 const char *key, const void *buf,
7674 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04007675{
7676 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04007677 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04007678
7679 return -EOPNOTSUPP;
7680}
7681
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007682static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04007683 struct dentry *unused, struct inode *inode,
7684 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04007685{
7686 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04007687 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04007688 return -EOPNOTSUPP;
7689}
7690
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007691static ssize_t
7692nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04007693{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007694 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04007695
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007696 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
7697 len = security_inode_listsecurity(inode, list, list_len);
Chengguang Xu82c596e2020-06-17 09:09:39 +08007698 if (len >= 0 && list_len && len > list_len)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007699 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04007700 }
7701 return len;
7702}
7703
7704static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
7705 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04007706 .get = nfs4_xattr_get_nfs4_label,
7707 .set = nfs4_xattr_set_nfs4_label,
7708};
David Quigleyc9bccef2013-05-22 12:50:45 -04007709
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007710#else
7711
7712static ssize_t
7713nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7714{
7715 return 0;
7716}
7717
7718#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04007719
Frank van der Linden012a2112020-06-23 22:39:03 +00007720#ifdef CONFIG_NFS_V4_2
7721static int nfs4_xattr_set_nfs4_user(const struct xattr_handler *handler,
Christian Braunere65ce2a2021-01-21 14:19:27 +01007722 struct user_namespace *mnt_userns,
Frank van der Linden012a2112020-06-23 22:39:03 +00007723 struct dentry *unused, struct inode *inode,
7724 const char *key, const void *buf,
7725 size_t buflen, int flags)
7726{
NeilBrownb5e7b592021-09-28 09:47:57 +10007727 u32 mask;
Frank van der Linden95ad37f2020-06-23 22:39:04 +00007728 int ret;
Frank van der Linden012a2112020-06-23 22:39:03 +00007729
7730 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7731 return -EOPNOTSUPP;
7732
7733 /*
7734 * There is no mapping from the MAY_* flags to the NFS_ACCESS_XA*
7735 * flags right now. Handling of xattr operations use the normal
7736 * file read/write permissions.
7737 *
7738 * Just in case the server has other ideas (which RFC 8276 allows),
7739 * do a cached access check for the XA* flags to possibly avoid
7740 * doing an RPC and getting EACCES back.
7741 */
NeilBrownb5e7b592021-09-28 09:47:57 +10007742 if (!nfs_access_get_cached(inode, current_cred(), &mask, true)) {
7743 if (!(mask & NFS_ACCESS_XAWRITE))
Frank van der Linden012a2112020-06-23 22:39:03 +00007744 return -EACCES;
7745 }
7746
Frank van der Linden95ad37f2020-06-23 22:39:04 +00007747 if (buf == NULL) {
7748 ret = nfs42_proc_removexattr(inode, key);
7749 if (!ret)
7750 nfs4_xattr_cache_remove(inode, key);
7751 } else {
7752 ret = nfs42_proc_setxattr(inode, key, buf, buflen, flags);
7753 if (!ret)
7754 nfs4_xattr_cache_add(inode, key, buf, NULL, buflen);
7755 }
7756
7757 return ret;
Frank van der Linden012a2112020-06-23 22:39:03 +00007758}
7759
7760static int nfs4_xattr_get_nfs4_user(const struct xattr_handler *handler,
7761 struct dentry *unused, struct inode *inode,
7762 const char *key, void *buf, size_t buflen)
7763{
NeilBrownb5e7b592021-09-28 09:47:57 +10007764 u32 mask;
Frank van der Linden95ad37f2020-06-23 22:39:04 +00007765 ssize_t ret;
Frank van der Linden012a2112020-06-23 22:39:03 +00007766
7767 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7768 return -EOPNOTSUPP;
7769
NeilBrownb5e7b592021-09-28 09:47:57 +10007770 if (!nfs_access_get_cached(inode, current_cred(), &mask, true)) {
7771 if (!(mask & NFS_ACCESS_XAREAD))
Frank van der Linden012a2112020-06-23 22:39:03 +00007772 return -EACCES;
7773 }
7774
Trond Myklebust1f3208b2021-03-25 11:04:34 -04007775 ret = nfs_revalidate_inode(inode, NFS_INO_INVALID_CHANGE);
Frank van der Linden95ad37f2020-06-23 22:39:04 +00007776 if (ret)
7777 return ret;
7778
7779 ret = nfs4_xattr_cache_get(inode, key, buf, buflen);
7780 if (ret >= 0 || (ret < 0 && ret != -ENOENT))
7781 return ret;
7782
7783 ret = nfs42_proc_getxattr(inode, key, buf, buflen);
7784
7785 return ret;
Frank van der Linden012a2112020-06-23 22:39:03 +00007786}
7787
7788static ssize_t
7789nfs4_listxattr_nfs4_user(struct inode *inode, char *list, size_t list_len)
7790{
7791 u64 cookie;
7792 bool eof;
Frank van der Linden95ad37f2020-06-23 22:39:04 +00007793 ssize_t ret, size;
Frank van der Linden012a2112020-06-23 22:39:03 +00007794 char *buf;
7795 size_t buflen;
NeilBrownb5e7b592021-09-28 09:47:57 +10007796 u32 mask;
Frank van der Linden012a2112020-06-23 22:39:03 +00007797
7798 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7799 return 0;
7800
NeilBrownb5e7b592021-09-28 09:47:57 +10007801 if (!nfs_access_get_cached(inode, current_cred(), &mask, true)) {
7802 if (!(mask & NFS_ACCESS_XALIST))
Frank van der Linden012a2112020-06-23 22:39:03 +00007803 return 0;
7804 }
7805
Trond Myklebust1f3208b2021-03-25 11:04:34 -04007806 ret = nfs_revalidate_inode(inode, NFS_INO_INVALID_CHANGE);
Frank van der Linden95ad37f2020-06-23 22:39:04 +00007807 if (ret)
7808 return ret;
7809
7810 ret = nfs4_xattr_cache_list(inode, list, list_len);
7811 if (ret >= 0 || (ret < 0 && ret != -ENOENT))
7812 return ret;
7813
Frank van der Linden012a2112020-06-23 22:39:03 +00007814 cookie = 0;
7815 eof = false;
7816 buflen = list_len ? list_len : XATTR_LIST_MAX;
7817 buf = list_len ? list : NULL;
7818 size = 0;
7819
7820 while (!eof) {
7821 ret = nfs42_proc_listxattrs(inode, buf, buflen,
7822 &cookie, &eof);
7823 if (ret < 0)
7824 return ret;
7825
7826 if (list_len) {
7827 buf += ret;
7828 buflen -= ret;
7829 }
7830 size += ret;
7831 }
7832
Frank van der Linden95ad37f2020-06-23 22:39:04 +00007833 if (list_len)
7834 nfs4_xattr_cache_set_list(inode, list, size);
7835
Frank van der Linden012a2112020-06-23 22:39:03 +00007836 return size;
7837}
7838
7839#else
7840
7841static ssize_t
7842nfs4_listxattr_nfs4_user(struct inode *inode, char *list, size_t list_len)
7843{
7844 return 0;
7845}
7846#endif /* CONFIG_NFS_V4_2 */
7847
Andy Adamson533eb462011-06-13 18:25:56 -04007848/*
7849 * nfs_fhget will use either the mounted_on_fileid or the fileid
7850 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007851static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
7852{
Andy Adamson533eb462011-06-13 18:25:56 -04007853 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
7854 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
7855 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05007856 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007857 return;
7858
7859 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05007860 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007861 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
7862 fattr->nlink = 2;
7863}
7864
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007865static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7866 const struct qstr *name,
7867 struct nfs4_fs_locations *fs_locations,
7868 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04007869{
7870 struct nfs_server *server = NFS_SERVER(dir);
Chuck Leverc05cefc2017-11-05 15:45:22 -05007871 u32 bitmask[3];
Trond Myklebust683b57b2006-06-09 09:34:22 -04007872 struct nfs4_fs_locations_arg args = {
7873 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05007874 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04007875 .page = page,
7876 .bitmask = bitmask,
7877 };
Benny Halevy22958462009-04-01 09:22:02 -04007878 struct nfs4_fs_locations_res res = {
7879 .fs_locations = fs_locations,
7880 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04007881 struct rpc_message msg = {
7882 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7883 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04007884 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04007885 };
7886 int status;
7887
Harvey Harrison3110ff82008-05-02 13:42:44 -07007888 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04007889
Chuck Leverc05cefc2017-11-05 15:45:22 -05007890 bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
7891 bitmask[1] = nfs4_fattr_bitmap[1];
7892
Andy Adamson533eb462011-06-13 18:25:56 -04007893 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
7894 * is not supported */
7895 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
Chuck Leverc05cefc2017-11-05 15:45:22 -05007896 bitmask[0] &= ~FATTR4_WORD0_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04007897 else
Chuck Leverc05cefc2017-11-05 15:45:22 -05007898 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04007899
Trond Myklebustc228fd32007-01-13 02:28:11 -05007900 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04007901 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04007902 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007903 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07007904 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04007905 return status;
7906}
7907
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007908int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7909 const struct qstr *name,
7910 struct nfs4_fs_locations *fs_locations,
7911 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007912{
Trond Myklebust0688e642019-04-07 13:59:09 -04007913 struct nfs4_exception exception = {
7914 .interruptible = true,
7915 };
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007916 int err;
7917 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007918 err = _nfs4_proc_fs_locations(client, dir, name,
7919 fs_locations, page);
7920 trace_nfs4_get_fs_locations(dir, name, err);
7921 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007922 &exception);
7923 } while (exception.retry);
7924 return err;
7925}
7926
Chuck Leverb03d7352013-10-17 14:12:50 -04007927/*
7928 * This operation also signals the server that this client is
7929 * performing migration recovery. The server can stop returning
7930 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
7931 * appended to this compound to identify the client ID which is
7932 * performing recovery.
7933 */
Olga Kornievskaia1976b2b2022-01-12 10:27:38 -05007934static int _nfs40_proc_get_locations(struct nfs_server *server,
7935 struct nfs_fh *fhandle,
Chuck Leverb03d7352013-10-17 14:12:50 -04007936 struct nfs4_fs_locations *locations,
NeilBrowna52458b2018-12-03 11:30:31 +11007937 struct page *page, const struct cred *cred)
Chuck Leverb03d7352013-10-17 14:12:50 -04007938{
Chuck Leverb03d7352013-10-17 14:12:50 -04007939 struct rpc_clnt *clnt = server->client;
7940 u32 bitmask[2] = {
7941 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7942 };
7943 struct nfs4_fs_locations_arg args = {
7944 .clientid = server->nfs_client->cl_clientid,
Olga Kornievskaia1976b2b2022-01-12 10:27:38 -05007945 .fh = fhandle,
Chuck Leverb03d7352013-10-17 14:12:50 -04007946 .page = page,
7947 .bitmask = bitmask,
7948 .migration = 1, /* skip LOOKUP */
7949 .renew = 1, /* append RENEW */
7950 };
7951 struct nfs4_fs_locations_res res = {
7952 .fs_locations = locations,
7953 .migration = 1,
7954 .renew = 1,
7955 };
7956 struct rpc_message msg = {
7957 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7958 .rpc_argp = &args,
7959 .rpc_resp = &res,
7960 .rpc_cred = cred,
7961 };
7962 unsigned long now = jiffies;
7963 int status;
7964
7965 nfs_fattr_init(&locations->fattr);
7966 locations->server = server;
7967 locations->nlocations = 0;
7968
Anna Schumakerfba83f32018-05-04 16:22:50 -04007969 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Leverb03d7352013-10-17 14:12:50 -04007970 status = nfs4_call_sync_sequence(clnt, server, &msg,
7971 &args.seq_args, &res.seq_res);
7972 if (status)
7973 return status;
7974
7975 renew_lease(server, now);
7976 return 0;
7977}
7978
7979#ifdef CONFIG_NFS_V4_1
7980
7981/*
7982 * This operation also signals the server that this client is
7983 * performing migration recovery. The server can stop asserting
7984 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
7985 * performing this operation is identified in the SEQUENCE
7986 * operation in this compound.
7987 *
7988 * When the client supports GETATTR(fs_locations_info), it can
7989 * be plumbed in here.
7990 */
Olga Kornievskaia1976b2b2022-01-12 10:27:38 -05007991static int _nfs41_proc_get_locations(struct nfs_server *server,
7992 struct nfs_fh *fhandle,
Chuck Leverb03d7352013-10-17 14:12:50 -04007993 struct nfs4_fs_locations *locations,
NeilBrowna52458b2018-12-03 11:30:31 +11007994 struct page *page, const struct cred *cred)
Chuck Leverb03d7352013-10-17 14:12:50 -04007995{
Chuck Leverb03d7352013-10-17 14:12:50 -04007996 struct rpc_clnt *clnt = server->client;
7997 u32 bitmask[2] = {
7998 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7999 };
8000 struct nfs4_fs_locations_arg args = {
Olga Kornievskaia1976b2b2022-01-12 10:27:38 -05008001 .fh = fhandle,
Chuck Leverb03d7352013-10-17 14:12:50 -04008002 .page = page,
8003 .bitmask = bitmask,
8004 .migration = 1, /* skip LOOKUP */
8005 };
8006 struct nfs4_fs_locations_res res = {
8007 .fs_locations = locations,
8008 .migration = 1,
8009 };
8010 struct rpc_message msg = {
8011 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
8012 .rpc_argp = &args,
8013 .rpc_resp = &res,
8014 .rpc_cred = cred,
8015 };
8016 int status;
8017
8018 nfs_fattr_init(&locations->fattr);
8019 locations->server = server;
8020 locations->nlocations = 0;
8021
Anna Schumakerfba83f32018-05-04 16:22:50 -04008022 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Leverb03d7352013-10-17 14:12:50 -04008023 status = nfs4_call_sync_sequence(clnt, server, &msg,
8024 &args.seq_args, &res.seq_res);
8025 if (status == NFS4_OK &&
8026 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
8027 status = -NFS4ERR_LEASE_MOVED;
8028 return status;
8029}
8030
8031#endif /* CONFIG_NFS_V4_1 */
8032
8033/**
8034 * nfs4_proc_get_locations - discover locations for a migrated FSID
8035 * @inode: inode on FSID that is migrating
8036 * @locations: result of query
8037 * @page: buffer
8038 * @cred: credential to use for this operation
8039 *
8040 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
8041 * operation failed, or a negative errno if a local error occurred.
8042 *
8043 * On success, "locations" is filled in, but if the server has
8044 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
8045 * asserted.
8046 *
8047 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
8048 * from this client that require migration recovery.
8049 */
Olga Kornievskaia1976b2b2022-01-12 10:27:38 -05008050int nfs4_proc_get_locations(struct nfs_server *server,
8051 struct nfs_fh *fhandle,
Chuck Leverb03d7352013-10-17 14:12:50 -04008052 struct nfs4_fs_locations *locations,
NeilBrowna52458b2018-12-03 11:30:31 +11008053 struct page *page, const struct cred *cred)
Chuck Leverb03d7352013-10-17 14:12:50 -04008054{
Chuck Leverb03d7352013-10-17 14:12:50 -04008055 struct nfs_client *clp = server->nfs_client;
8056 const struct nfs4_mig_recovery_ops *ops =
8057 clp->cl_mvops->mig_recovery_ops;
Trond Myklebust0688e642019-04-07 13:59:09 -04008058 struct nfs4_exception exception = {
8059 .interruptible = true,
8060 };
Chuck Leverb03d7352013-10-17 14:12:50 -04008061 int status;
8062
8063 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
8064 (unsigned long long)server->fsid.major,
8065 (unsigned long long)server->fsid.minor,
8066 clp->cl_hostname);
Olga Kornievskaia1976b2b2022-01-12 10:27:38 -05008067 nfs_display_fhandle(fhandle, __func__);
Chuck Leverb03d7352013-10-17 14:12:50 -04008068
8069 do {
Olga Kornievskaia1976b2b2022-01-12 10:27:38 -05008070 status = ops->get_locations(server, fhandle, locations, page,
8071 cred);
Chuck Leverb03d7352013-10-17 14:12:50 -04008072 if (status != -NFS4ERR_DELAY)
8073 break;
8074 nfs4_handle_exception(server, status, &exception);
8075 } while (exception.retry);
8076 return status;
8077}
8078
Chuck Lever44c99932013-10-17 14:13:30 -04008079/*
8080 * This operation also signals the server that this client is
8081 * performing "lease moved" recovery. The server can stop
8082 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
8083 * is appended to this compound to identify the client ID which is
8084 * performing recovery.
8085 */
NeilBrowna52458b2018-12-03 11:30:31 +11008086static int _nfs40_proc_fsid_present(struct inode *inode, const struct cred *cred)
Chuck Lever44c99932013-10-17 14:13:30 -04008087{
8088 struct nfs_server *server = NFS_SERVER(inode);
8089 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
8090 struct rpc_clnt *clnt = server->client;
8091 struct nfs4_fsid_present_arg args = {
8092 .fh = NFS_FH(inode),
8093 .clientid = clp->cl_clientid,
8094 .renew = 1, /* append RENEW */
8095 };
8096 struct nfs4_fsid_present_res res = {
8097 .renew = 1,
8098 };
8099 struct rpc_message msg = {
8100 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
8101 .rpc_argp = &args,
8102 .rpc_resp = &res,
8103 .rpc_cred = cred,
8104 };
8105 unsigned long now = jiffies;
8106 int status;
8107
8108 res.fh = nfs_alloc_fhandle();
8109 if (res.fh == NULL)
8110 return -ENOMEM;
8111
Anna Schumakerfba83f32018-05-04 16:22:50 -04008112 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Lever44c99932013-10-17 14:13:30 -04008113 status = nfs4_call_sync_sequence(clnt, server, &msg,
8114 &args.seq_args, &res.seq_res);
8115 nfs_free_fhandle(res.fh);
8116 if (status)
8117 return status;
8118
8119 do_renew_lease(clp, now);
8120 return 0;
8121}
8122
8123#ifdef CONFIG_NFS_V4_1
8124
8125/*
8126 * This operation also signals the server that this client is
8127 * performing "lease moved" recovery. The server can stop asserting
8128 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
8129 * this operation is identified in the SEQUENCE operation in this
8130 * compound.
8131 */
NeilBrowna52458b2018-12-03 11:30:31 +11008132static int _nfs41_proc_fsid_present(struct inode *inode, const struct cred *cred)
Chuck Lever44c99932013-10-17 14:13:30 -04008133{
8134 struct nfs_server *server = NFS_SERVER(inode);
8135 struct rpc_clnt *clnt = server->client;
8136 struct nfs4_fsid_present_arg args = {
8137 .fh = NFS_FH(inode),
8138 };
8139 struct nfs4_fsid_present_res res = {
8140 };
8141 struct rpc_message msg = {
8142 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
8143 .rpc_argp = &args,
8144 .rpc_resp = &res,
8145 .rpc_cred = cred,
8146 };
8147 int status;
8148
8149 res.fh = nfs_alloc_fhandle();
8150 if (res.fh == NULL)
8151 return -ENOMEM;
8152
Anna Schumakerfba83f32018-05-04 16:22:50 -04008153 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Lever44c99932013-10-17 14:13:30 -04008154 status = nfs4_call_sync_sequence(clnt, server, &msg,
8155 &args.seq_args, &res.seq_res);
8156 nfs_free_fhandle(res.fh);
8157 if (status == NFS4_OK &&
8158 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
8159 status = -NFS4ERR_LEASE_MOVED;
8160 return status;
8161}
8162
8163#endif /* CONFIG_NFS_V4_1 */
8164
8165/**
8166 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
8167 * @inode: inode on FSID to check
8168 * @cred: credential to use for this operation
8169 *
8170 * Server indicates whether the FSID is present, moved, or not
8171 * recognized. This operation is necessary to clear a LEASE_MOVED
8172 * condition for this client ID.
8173 *
8174 * Returns NFS4_OK if the FSID is present on this server,
8175 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
8176 * NFS4ERR code if some error occurred on the server, or a
8177 * negative errno if a local failure occurred.
8178 */
NeilBrowna52458b2018-12-03 11:30:31 +11008179int nfs4_proc_fsid_present(struct inode *inode, const struct cred *cred)
Chuck Lever44c99932013-10-17 14:13:30 -04008180{
8181 struct nfs_server *server = NFS_SERVER(inode);
8182 struct nfs_client *clp = server->nfs_client;
8183 const struct nfs4_mig_recovery_ops *ops =
8184 clp->cl_mvops->mig_recovery_ops;
Trond Myklebust0688e642019-04-07 13:59:09 -04008185 struct nfs4_exception exception = {
8186 .interruptible = true,
8187 };
Chuck Lever44c99932013-10-17 14:13:30 -04008188 int status;
8189
8190 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
8191 (unsigned long long)server->fsid.major,
8192 (unsigned long long)server->fsid.minor,
8193 clp->cl_hostname);
8194 nfs_display_fhandle(NFS_FH(inode), __func__);
8195
8196 do {
8197 status = ops->fsid_present(inode, cred);
8198 if (status != -NFS4ERR_DELAY)
8199 break;
8200 nfs4_handle_exception(server, status, &exception);
8201 } while (exception.retry);
8202 return status;
8203}
8204
Trond Myklebust302fad72019-02-18 13:32:38 -05008205/*
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04008206 * If 'use_integrity' is true and the state managment nfs_client
8207 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
8208 * and the machine credential as per RFC3530bis and RFC5661 Security
8209 * Considerations sections. Otherwise, just use the user cred with the
8210 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04008211 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04008212static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00008213{
8214 int status;
Anna Schumaker50493362019-08-14 15:30:16 -04008215 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
8216 struct nfs_client *clp = NFS_SERVER(dir)->nfs_client;
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00008217 struct nfs4_secinfo_arg args = {
8218 .dir_fh = NFS_FH(dir),
8219 .name = name,
8220 };
8221 struct nfs4_secinfo_res res = {
8222 .flavors = flavors,
8223 };
8224 struct rpc_message msg = {
8225 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
8226 .rpc_argp = &args,
8227 .rpc_resp = &res,
8228 };
Anna Schumaker50493362019-08-14 15:30:16 -04008229 struct nfs4_call_sync_data data = {
8230 .seq_server = NFS_SERVER(dir),
8231 .seq_args = &args.seq_args,
8232 .seq_res = &res.seq_res,
8233 };
8234 struct rpc_task_setup task_setup = {
8235 .rpc_client = clnt,
8236 .rpc_message = &msg,
8237 .callback_ops = clp->cl_mvops->call_sync_ops,
8238 .callback_data = &data,
8239 .flags = RPC_TASK_NO_ROUND_ROBIN,
8240 };
NeilBrowna52458b2018-12-03 11:30:31 +11008241 const struct cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04008242
8243 if (use_integrity) {
Anna Schumaker50493362019-08-14 15:30:16 -04008244 clnt = clp->cl_rpcclient;
8245 task_setup.rpc_client = clnt;
8246
8247 cred = nfs4_get_clid_cred(clp);
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008248 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04008249 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00008250
8251 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008252
Anna Schumaker50493362019-08-14 15:30:16 -04008253 nfs4_state_protect(clp, NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
8254 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
8255 status = nfs4_call_sync_custom(&task_setup);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008256
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00008257 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04008258
NeilBrowna52458b2018-12-03 11:30:31 +11008259 put_cred(cred);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00008260 return status;
8261}
8262
Bryan Schumaker72de53e2012-04-27 13:27:40 -04008263int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
8264 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00008265{
Trond Myklebust0688e642019-04-07 13:59:09 -04008266 struct nfs4_exception exception = {
8267 .interruptible = true,
8268 };
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00008269 int err;
8270 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04008271 err = -NFS4ERR_WRONGSEC;
8272
8273 /* try to use integrity protection with machine cred */
8274 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
8275 err = _nfs4_proc_secinfo(dir, name, flavors, true);
8276
8277 /*
8278 * if unable to use integrity protection, or SECINFO with
8279 * integrity protection returns NFS4ERR_WRONGSEC (which is
8280 * disallowed by spec, but exists in deployed servers) use
8281 * the current filesystem's rpc_client and the user cred.
8282 */
8283 if (err == -NFS4ERR_WRONGSEC)
8284 err = _nfs4_proc_secinfo(dir, name, flavors, false);
8285
Trond Myklebust078ea3d2013-08-12 16:45:55 -04008286 trace_nfs4_secinfo(dir, name, err);
8287 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00008288 &exception);
8289 } while (exception.retry);
8290 return err;
8291}
8292
Andy Adamson557134a2009-04-01 09:21:53 -04008293#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04008294/*
Andy Adamson357f54d2010-12-14 10:11:57 -05008295 * Check the exchange flags returned by the server for invalid flags, having
8296 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
8297 * DS flags set.
8298 */
Olga Kornievskaia8c390762020-10-16 09:25:45 -04008299static int nfs4_check_cl_exchange_flags(u32 flags, u32 version)
Andy Adamson357f54d2010-12-14 10:11:57 -05008300{
Olga Kornievskaia8c390762020-10-16 09:25:45 -04008301 if (version >= 2 && (flags & ~EXCHGID4_2_FLAG_MASK_R))
8302 goto out_inval;
8303 else if (version < 2 && (flags & ~EXCHGID4_FLAG_MASK_R))
Andy Adamson357f54d2010-12-14 10:11:57 -05008304 goto out_inval;
8305 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
8306 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
8307 goto out_inval;
8308 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
8309 goto out_inval;
8310 return NFS_OK;
8311out_inval:
8312 return -NFS4ERR_INVAL;
8313}
8314
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04008315static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04008316nfs41_same_server_scope(struct nfs41_server_scope *a,
8317 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04008318{
Anna Schumaker49ad0142017-01-11 16:51:59 -05008319 if (a->server_scope_sz != b->server_scope_sz)
8320 return false;
8321 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04008322}
8323
Andy Adamson02a95de2016-02-05 16:08:37 -05008324static void
8325nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
8326{
Trond Myklebust5c441542019-11-13 08:34:00 +01008327 struct nfs41_bind_conn_to_session_args *args = task->tk_msg.rpc_argp;
Olga Kornievskaiadff58532020-04-24 17:45:50 -04008328 struct nfs41_bind_conn_to_session_res *res = task->tk_msg.rpc_resp;
Trond Myklebust5c441542019-11-13 08:34:00 +01008329 struct nfs_client *clp = args->client;
8330
8331 switch (task->tk_status) {
8332 case -NFS4ERR_BADSESSION:
8333 case -NFS4ERR_DEADSESSION:
8334 nfs4_schedule_session_recovery(clp->cl_session,
8335 task->tk_status);
8336 }
Olga Kornievskaiadff58532020-04-24 17:45:50 -04008337 if (args->dir == NFS4_CDFC4_FORE_OR_BOTH &&
8338 res->dir != NFS4_CDFS4_BOTH) {
8339 rpc_task_close_connection(task);
8340 if (args->retries++ < MAX_BIND_CONN_TO_SESSION_RETRIES)
8341 rpc_restart_call(task);
8342 }
Andy Adamson02a95de2016-02-05 16:08:37 -05008343}
8344
8345static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
Olga Kornievskaia1c709b72020-04-26 11:30:00 -04008346 .rpc_call_done = nfs4_bind_one_conn_to_session_done,
Andy Adamson02a95de2016-02-05 16:08:37 -05008347};
8348
Andy Adamson357f54d2010-12-14 10:11:57 -05008349/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008350 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008351 *
8352 * The 4.1 client currently uses the same TCP connection for the
8353 * fore and backchannel.
8354 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008355static
8356int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
8357 struct rpc_xprt *xprt,
8358 struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008359 const struct cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008360{
8361 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08008362 struct nfs41_bind_conn_to_session_args args = {
8363 .client = clp,
8364 .dir = NFS4_CDFC4_FORE_OR_BOTH,
Olga Kornievskaiadff58532020-04-24 17:45:50 -04008365 .retries = 0,
Trond Myklebust71a097c2015-02-18 09:27:18 -08008366 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008367 struct nfs41_bind_conn_to_session_res res;
8368 struct rpc_message msg = {
8369 .rpc_proc =
8370 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08008371 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008372 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04008373 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008374 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008375 struct rpc_task_setup task_setup_data = {
8376 .rpc_client = clnt,
8377 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05008378 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008379 .rpc_message = &msg,
8380 .flags = RPC_TASK_TIMEOUT,
8381 };
8382 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008383
Trond Myklebust71a097c2015-02-18 09:27:18 -08008384 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
8385 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
8386 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008387
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008388 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
8389 if (xprt != rcu_access_pointer(clnt->cl_xprt))
8390 args.dir = NFS4_CDFC4_FORE;
8391
8392 task = rpc_run_task(&task_setup_data);
8393 if (!IS_ERR(task)) {
8394 status = task->tk_status;
8395 rpc_put_task(task);
8396 } else
8397 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008398 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008399 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08008400 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008401 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
8402 dprintk("NFS: %s: Session ID mismatch\n", __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04008403 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008404 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08008405 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008406 dprintk("NFS: %s: Unexpected direction from server\n",
8407 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04008408 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008409 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08008410 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008411 dprintk("NFS: %s: Server returned RDMA mode = true\n",
8412 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04008413 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008414 }
8415 }
Anna Schumakerc7ae7632017-04-07 14:15:21 -04008416
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008417 return status;
8418}
8419
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008420struct rpc_bind_conn_calldata {
8421 struct nfs_client *clp;
NeilBrowna52458b2018-12-03 11:30:31 +11008422 const struct cred *cred;
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008423};
8424
8425static int
8426nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
8427 struct rpc_xprt *xprt,
8428 void *calldata)
8429{
8430 struct rpc_bind_conn_calldata *p = calldata;
8431
8432 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
8433}
8434
NeilBrowna52458b2018-12-03 11:30:31 +11008435int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, const struct cred *cred)
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008436{
8437 struct rpc_bind_conn_calldata data = {
8438 .clp = clp,
8439 .cred = cred,
8440 };
8441 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
8442 nfs4_proc_bind_conn_to_session_callback, &data);
8443}
8444
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008445/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04008446 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
8447 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04008448 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008449static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
8450 .how = SP4_MACH_CRED,
8451 .enforce.u.words = {
8452 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
8453 1 << (OP_EXCHANGE_ID - 32) |
8454 1 << (OP_CREATE_SESSION - 32) |
8455 1 << (OP_DESTROY_SESSION - 32) |
8456 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04008457 },
8458 .allow.u.words = {
8459 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05008460 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04008461 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05008462 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04008463 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008464 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008465 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05008466 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008467 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04008468 1 << (OP_FREE_STATEID - 32) |
8469 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008470 }
8471};
8472
8473/*
8474 * Select the state protection mode for client `clp' given the server results
8475 * from exchange_id in `sp'.
8476 *
8477 * Returns 0 on success, negative errno otherwise.
8478 */
8479static int nfs4_sp4_select_mode(struct nfs_client *clp,
8480 struct nfs41_state_protection *sp)
8481{
8482 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
8483 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
8484 1 << (OP_EXCHANGE_ID - 32) |
8485 1 << (OP_CREATE_SESSION - 32) |
8486 1 << (OP_DESTROY_SESSION - 32) |
8487 1 << (OP_DESTROY_CLIENTID - 32)
8488 };
Trond Myklebust937e3132017-08-01 07:32:50 -04008489 unsigned long flags = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008490 unsigned int i;
Trond Myklebust937e3132017-08-01 07:32:50 -04008491 int ret = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008492
8493 if (sp->how == SP4_MACH_CRED) {
8494 /* Print state protect result */
8495 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
8496 for (i = 0; i <= LAST_NFS4_OP; i++) {
8497 if (test_bit(i, sp->enforce.u.longs))
8498 dfprintk(MOUNT, " enforce op %d\n", i);
8499 if (test_bit(i, sp->allow.u.longs))
8500 dfprintk(MOUNT, " allow op %d\n", i);
8501 }
8502
8503 /* make sure nothing is on enforce list that isn't supported */
8504 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
8505 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
8506 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008507 ret = -EINVAL;
8508 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008509 }
8510 }
8511
8512 /*
8513 * Minimal mode - state operations are allowed to use machine
8514 * credential. Note this already happens by default, so the
8515 * client doesn't have to do anything more than the negotiation.
8516 *
8517 * NOTE: we don't care if EXCHANGE_ID is in the list -
8518 * we're already using the machine cred for exchange_id
8519 * and will never use a different cred.
8520 */
8521 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
8522 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
8523 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
8524 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
8525 dfprintk(MOUNT, "sp4_mach_cred:\n");
8526 dfprintk(MOUNT, " minimal mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008527 __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008528 } else {
8529 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008530 ret = -EINVAL;
8531 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008532 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04008533
8534 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05008535 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
8536 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04008537 test_bit(OP_LOCKU, sp->allow.u.longs)) {
8538 dfprintk(MOUNT, " cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008539 __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04008540 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008541
Andrew Elble99ade3c2015-12-02 09:39:51 -05008542 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
8543 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008544 __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
Andrew Elble99ade3c2015-12-02 09:39:51 -05008545 }
8546
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008547 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
8548 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
8549 dfprintk(MOUNT, " secinfo mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008550 __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008551 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008552
8553 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
8554 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
8555 dfprintk(MOUNT, " stateid mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008556 __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008557 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04008558
8559 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
8560 dfprintk(MOUNT, " write mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008561 __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04008562 }
8563
8564 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
8565 dfprintk(MOUNT, " commit mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008566 __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04008567 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008568 }
Trond Myklebust937e3132017-08-01 07:32:50 -04008569out:
8570 clp->cl_sp4_flags = flags;
Wei Yongjun72bf75c2018-08-02 05:42:04 +00008571 return ret;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008572}
8573
Andy Adamson8d89bd72016-09-09 09:22:18 -04008574struct nfs41_exchange_id_data {
8575 struct nfs41_exchange_id_res res;
8576 struct nfs41_exchange_id_args args;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008577};
8578
Andy Adamson8d89bd72016-09-09 09:22:18 -04008579static void nfs4_exchange_id_release(void *data)
8580{
8581 struct nfs41_exchange_id_data *cdata =
8582 (struct nfs41_exchange_id_data *)data;
8583
Olga Kornievskaia63513232017-03-13 10:36:19 -04008584 nfs_put_client(cdata->args.client);
Andy Adamson8d89bd72016-09-09 09:22:18 -04008585 kfree(cdata->res.impl_id);
8586 kfree(cdata->res.server_scope);
8587 kfree(cdata->res.server_owner);
8588 kfree(cdata);
8589}
8590
8591static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
Andy Adamson8d89bd72016-09-09 09:22:18 -04008592 .rpc_release = nfs4_exchange_id_release,
8593};
8594
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008595/*
8596 * _nfs4_proc_exchange_id()
8597 *
8598 * Wrapper for EXCHANGE_ID operation.
8599 */
Trond Myklebust9c760d12017-07-31 18:38:50 -04008600static struct rpc_task *
NeilBrowna52458b2018-12-03 11:30:31 +11008601nfs4_run_exchange_id(struct nfs_client *clp, const struct cred *cred,
Andy Adamsonad0849a2016-09-09 09:22:28 -04008602 u32 sp4_how, struct rpc_xprt *xprt)
Benny Halevy99fe60d2009-04-01 09:22:29 -04008603{
Benny Halevy99fe60d2009-04-01 09:22:29 -04008604 struct rpc_message msg = {
8605 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
Benny Halevy99fe60d2009-04-01 09:22:29 -04008606 .rpc_cred = cred,
8607 };
Andy Adamson8d89bd72016-09-09 09:22:18 -04008608 struct rpc_task_setup task_setup_data = {
8609 .rpc_client = clp->cl_rpcclient,
8610 .callback_ops = &nfs4_exchange_id_call_ops,
8611 .rpc_message = &msg,
NeilBrown5a0c2572019-05-30 10:41:28 +10008612 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
Andy Adamson8d89bd72016-09-09 09:22:18 -04008613 };
8614 struct nfs41_exchange_id_data *calldata;
Anna Schumakere917f0d2017-04-07 14:15:22 -04008615 int status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008616
Elena Reshetova212bf412017-10-20 12:53:38 +03008617 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust9c760d12017-07-31 18:38:50 -04008618 return ERR_PTR(-EIO);
Andy Adamson8d89bd72016-09-09 09:22:18 -04008619
Trond Myklebust9c760d12017-07-31 18:38:50 -04008620 status = -ENOMEM;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008621 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust9c760d12017-07-31 18:38:50 -04008622 if (!calldata)
8623 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04008624
Trond Myklebustfd405592017-08-01 16:02:47 -04008625 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04008626
8627 status = nfs4_init_uniform_client_string(clp);
8628 if (status)
Andy Adamson8d89bd72016-09-09 09:22:18 -04008629 goto out_calldata;
Jeff Layton3a6bb732015-06-09 19:43:57 -04008630
Andy Adamson8d89bd72016-09-09 09:22:18 -04008631 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
8632 GFP_NOFS);
8633 status = -ENOMEM;
8634 if (unlikely(calldata->res.server_owner == NULL))
8635 goto out_calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04008636
Andy Adamson8d89bd72016-09-09 09:22:18 -04008637 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04008638 GFP_NOFS);
Andy Adamson8d89bd72016-09-09 09:22:18 -04008639 if (unlikely(calldata->res.server_scope == NULL))
Chuck Leveracdeb692012-05-21 22:46:16 -04008640 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04008641
Andy Adamson8d89bd72016-09-09 09:22:18 -04008642 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
8643 if (unlikely(calldata->res.impl_id == NULL))
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05008644 goto out_server_scope;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05008645
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008646 switch (sp4_how) {
8647 case SP4_NONE:
Andy Adamson8d89bd72016-09-09 09:22:18 -04008648 calldata->args.state_protect.how = SP4_NONE;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008649 break;
8650
8651 case SP4_MACH_CRED:
Andy Adamson8d89bd72016-09-09 09:22:18 -04008652 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008653 break;
8654
8655 default:
8656 /* unsupported! */
8657 WARN_ON_ONCE(1);
8658 status = -EINVAL;
Kinglong Mee6b559702015-07-01 11:54:53 +08008659 goto out_impl_id;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008660 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04008661 if (xprt) {
Andy Adamsonad0849a2016-09-09 09:22:28 -04008662 task_setup_data.rpc_xprt = xprt;
Trond Myklebustd9cb7332017-08-01 16:02:48 -04008663 task_setup_data.flags |= RPC_TASK_SOFTCONN;
Trond Myklebustfd405592017-08-01 16:02:47 -04008664 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
8665 sizeof(calldata->args.verifier.data));
Andy Adamsonad0849a2016-09-09 09:22:28 -04008666 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04008667 calldata->args.client = clp;
Trond Myklebustbfab2812017-08-01 08:17:34 -04008668 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
8669 EXCHGID4_FLAG_BIND_PRINC_STATEID;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008670#ifdef CONFIG_NFS_V4_1_MIGRATION
Trond Myklebustbfab2812017-08-01 08:17:34 -04008671 calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008672#endif
8673 msg.rpc_argp = &calldata->args;
8674 msg.rpc_resp = &calldata->res;
8675 task_setup_data.callback_data = calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04008676
Trond Myklebust9c760d12017-07-31 18:38:50 -04008677 return rpc_run_task(&task_setup_data);
Andy Adamson8d89bd72016-09-09 09:22:18 -04008678
8679out_impl_id:
8680 kfree(calldata->res.impl_id);
8681out_server_scope:
8682 kfree(calldata->res.server_scope);
8683out_server_owner:
8684 kfree(calldata->res.server_owner);
8685out_calldata:
8686 kfree(calldata);
Trond Myklebust9c760d12017-07-31 18:38:50 -04008687out:
Olga Kornievskaia63513232017-03-13 10:36:19 -04008688 nfs_put_client(clp);
Trond Myklebust9c760d12017-07-31 18:38:50 -04008689 return ERR_PTR(status);
8690}
8691
8692/*
8693 * _nfs4_proc_exchange_id()
8694 *
8695 * Wrapper for EXCHANGE_ID operation.
8696 */
NeilBrowna52458b2018-12-03 11:30:31 +11008697static int _nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred,
Trond Myklebust9c760d12017-07-31 18:38:50 -04008698 u32 sp4_how)
8699{
8700 struct rpc_task *task;
8701 struct nfs41_exchange_id_args *argp;
8702 struct nfs41_exchange_id_res *resp;
Robert Milkowski7dc29932020-01-30 09:43:25 +00008703 unsigned long now = jiffies;
Trond Myklebust9c760d12017-07-31 18:38:50 -04008704 int status;
8705
8706 task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
8707 if (IS_ERR(task))
8708 return PTR_ERR(task);
8709
8710 argp = task->tk_msg.rpc_argp;
8711 resp = task->tk_msg.rpc_resp;
8712 status = task->tk_status;
8713 if (status != 0)
8714 goto out;
8715
Olga Kornievskaia8c390762020-10-16 09:25:45 -04008716 status = nfs4_check_cl_exchange_flags(resp->flags,
8717 clp->cl_mvops->minor_version);
Trond Myklebust9c760d12017-07-31 18:38:50 -04008718 if (status != 0)
8719 goto out;
8720
8721 status = nfs4_sp4_select_mode(clp, &resp->state_protect);
8722 if (status != 0)
8723 goto out;
8724
Robert Milkowski7dc29932020-01-30 09:43:25 +00008725 do_renew_lease(clp, now);
8726
Trond Myklebust9c760d12017-07-31 18:38:50 -04008727 clp->cl_clientid = resp->clientid;
8728 clp->cl_exchange_flags = resp->flags;
8729 clp->cl_seqid = resp->seqid;
8730 /* Client ID is not confirmed */
8731 if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
8732 clear_bit(NFS4_SESSION_ESTABLISHED,
8733 &clp->cl_session->session_state);
8734
8735 if (clp->cl_serverscope != NULL &&
8736 !nfs41_same_server_scope(clp->cl_serverscope,
8737 resp->server_scope)) {
8738 dprintk("%s: server_scope mismatch detected\n",
8739 __func__);
8740 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
8741 }
8742
8743 swap(clp->cl_serverowner, resp->server_owner);
8744 swap(clp->cl_serverscope, resp->server_scope);
8745 swap(clp->cl_implid, resp->impl_id);
8746
8747 /* Save the EXCHANGE_ID verifier session trunk tests */
8748 memcpy(clp->cl_confirm.data, argp->verifier.data,
8749 sizeof(clp->cl_confirm.data));
8750out:
8751 trace_nfs4_exchange_id(clp, status);
8752 rpc_put_task(task);
8753 return status;
Benny Halevy99fe60d2009-04-01 09:22:29 -04008754}
8755
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008756/*
8757 * nfs4_proc_exchange_id()
8758 *
8759 * Returns zero, a negative errno, or a negative NFS4ERR status code.
8760 *
8761 * Since the clientid has expired, all compounds using sessions
8762 * associated with the stale clientid will be returning
8763 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
8764 * be in some phase of session reset.
8765 *
8766 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
8767 */
NeilBrowna52458b2018-12-03 11:30:31 +11008768int nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008769{
8770 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
8771 int status;
8772
8773 /* try SP4_MACH_CRED if krb5i/p */
8774 if (authflavor == RPC_AUTH_GSS_KRB5I ||
8775 authflavor == RPC_AUTH_GSS_KRB5P) {
Trond Myklebust9c760d12017-07-31 18:38:50 -04008776 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008777 if (!status)
8778 return 0;
8779 }
8780
8781 /* try SP4_NONE */
Trond Myklebust9c760d12017-07-31 18:38:50 -04008782 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008783}
8784
Andy Adamson04fa2c62016-09-09 09:22:29 -04008785/**
8786 * nfs4_test_session_trunk
8787 *
8788 * This is an add_xprt_test() test function called from
8789 * rpc_clnt_setup_test_and_add_xprt.
8790 *
8791 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
8792 * and is dereferrenced in nfs4_exchange_id_release
8793 *
8794 * Upon success, add the new transport to the rpc_clnt
8795 *
8796 * @clnt: struct rpc_clnt to get new transport
8797 * @xprt: the rpc_xprt to test
8798 * @data: call data for _nfs4_proc_exchange_id.
8799 */
Santosh kumar pradhan10e037d2018-12-19 12:29:57 +05308800void nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
Andy Adamson04fa2c62016-09-09 09:22:29 -04008801 void *data)
8802{
8803 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
Trond Myklebust9c760d12017-07-31 18:38:50 -04008804 struct rpc_task *task;
8805 int status;
8806
Andy Adamson04fa2c62016-09-09 09:22:29 -04008807 u32 sp4_how;
8808
8809 dprintk("--> %s try %s\n", __func__,
8810 xprt->address_strings[RPC_DISPLAY_ADDR]);
8811
8812 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
8813
8814 /* Test connection for session trunking. Async exchange_id call */
Trond Myklebust9c760d12017-07-31 18:38:50 -04008815 task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
8816 if (IS_ERR(task))
Santosh kumar pradhan10e037d2018-12-19 12:29:57 +05308817 return;
Trond Myklebust9c760d12017-07-31 18:38:50 -04008818
8819 status = task->tk_status;
8820 if (status == 0)
8821 status = nfs4_detect_session_trunking(adata->clp,
8822 task->tk_msg.rpc_resp, xprt);
8823
Santosh kumar pradhan10e037d2018-12-19 12:29:57 +05308824 if (status == 0)
8825 rpc_clnt_xprt_switch_add_xprt(clnt, xprt);
8826
Trond Myklebust9c760d12017-07-31 18:38:50 -04008827 rpc_put_task(task);
Andy Adamson04fa2c62016-09-09 09:22:29 -04008828}
8829EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
8830
Trond Myklebust66245532012-05-25 17:18:09 -04008831static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008832 const struct cred *cred)
Trond Myklebust66245532012-05-25 17:18:09 -04008833{
8834 struct rpc_message msg = {
8835 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
8836 .rpc_argp = clp,
8837 .rpc_cred = cred,
8838 };
8839 int status;
8840
NeilBrown5a0c2572019-05-30 10:41:28 +10008841 status = rpc_call_sync(clp->cl_rpcclient, &msg,
8842 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008843 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04008844 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04008845 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04008846 "DESTROY_CLIENTID.", status, clp->cl_hostname);
8847 return status;
8848}
8849
8850static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008851 const struct cred *cred)
Trond Myklebust66245532012-05-25 17:18:09 -04008852{
8853 unsigned int loop;
8854 int ret;
8855
8856 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
8857 ret = _nfs4_proc_destroy_clientid(clp, cred);
8858 switch (ret) {
8859 case -NFS4ERR_DELAY:
8860 case -NFS4ERR_CLIENTID_BUSY:
8861 ssleep(1);
8862 break;
8863 default:
8864 return ret;
8865 }
8866 }
8867 return 0;
8868}
8869
8870int nfs4_destroy_clientid(struct nfs_client *clp)
8871{
NeilBrowna52458b2018-12-03 11:30:31 +11008872 const struct cred *cred;
Trond Myklebust66245532012-05-25 17:18:09 -04008873 int ret = 0;
8874
8875 if (clp->cl_mvops->minor_version < 1)
8876 goto out;
8877 if (clp->cl_exchange_flags == 0)
8878 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04008879 if (clp->cl_preserve_clid)
8880 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04008881 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04008882 ret = nfs4_proc_destroy_clientid(clp, cred);
NeilBrowna52458b2018-12-03 11:30:31 +11008883 put_cred(cred);
Trond Myklebust66245532012-05-25 17:18:09 -04008884 switch (ret) {
8885 case 0:
8886 case -NFS4ERR_STALE_CLIENTID:
8887 clp->cl_exchange_flags = 0;
8888 }
8889out:
8890 return ret;
8891}
8892
Donald Buczek0efb01b2019-07-07 21:26:08 +02008893#endif /* CONFIG_NFS_V4_1 */
8894
Andy Adamson2050f0c2009-04-01 09:22:30 -04008895struct nfs4_get_lease_time_data {
8896 struct nfs4_get_lease_time_args *args;
8897 struct nfs4_get_lease_time_res *res;
8898 struct nfs_client *clp;
8899};
8900
8901static void nfs4_get_lease_time_prepare(struct rpc_task *task,
8902 void *calldata)
8903{
Andy Adamson2050f0c2009-04-01 09:22:30 -04008904 struct nfs4_get_lease_time_data *data =
8905 (struct nfs4_get_lease_time_data *)calldata;
8906
Andy Adamson2050f0c2009-04-01 09:22:30 -04008907 /* just setup sequence, do not trigger session recovery
8908 since we're invoked within one */
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008909 nfs4_setup_sequence(data->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008910 &data->args->la_seq_args,
8911 &data->res->lr_seq_res,
8912 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008913}
8914
8915/*
8916 * Called from nfs4_state_manager thread for session setup, so don't recover
8917 * from sequence operation or clientid errors.
8918 */
8919static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
8920{
8921 struct nfs4_get_lease_time_data *data =
8922 (struct nfs4_get_lease_time_data *)calldata;
8923
Donald Buczek0efb01b2019-07-07 21:26:08 +02008924 if (!nfs4_sequence_done(task, &data->res->lr_seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04008925 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04008926 switch (task->tk_status) {
8927 case -NFS4ERR_DELAY:
8928 case -NFS4ERR_GRACE:
Andy Adamson2050f0c2009-04-01 09:22:30 -04008929 rpc_delay(task, NFS4_POLL_RETRY_MIN);
8930 task->tk_status = 0;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05008931 fallthrough;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008932 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008933 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008934 return;
8935 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04008936}
8937
Trond Myklebust17280172012-03-11 13:11:00 -04008938static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04008939 .rpc_call_prepare = nfs4_get_lease_time_prepare,
8940 .rpc_call_done = nfs4_get_lease_time_done,
8941};
8942
8943int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
8944{
Andy Adamson2050f0c2009-04-01 09:22:30 -04008945 struct nfs4_get_lease_time_args args;
8946 struct nfs4_get_lease_time_res res = {
8947 .lr_fsinfo = fsinfo,
8948 };
8949 struct nfs4_get_lease_time_data data = {
8950 .args = &args,
8951 .res = &res,
8952 .clp = clp,
8953 };
8954 struct rpc_message msg = {
8955 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
8956 .rpc_argp = &args,
8957 .rpc_resp = &res,
8958 };
8959 struct rpc_task_setup task_setup = {
8960 .rpc_client = clp->cl_rpcclient,
8961 .rpc_message = &msg,
8962 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008963 .callback_data = &data,
8964 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04008965 };
Andy Adamson2050f0c2009-04-01 09:22:30 -04008966
Anna Schumakerfba83f32018-05-04 16:22:50 -04008967 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0, 1);
Anna Schumakerf836b272019-08-19 10:18:28 -04008968 return nfs4_call_sync_custom(&task_setup);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008969}
8970
Donald Buczek0efb01b2019-07-07 21:26:08 +02008971#ifdef CONFIG_NFS_V4_1
8972
Andy Adamsonfc931582009-04-01 09:22:31 -04008973/*
8974 * Initialize the values to be used by the client in CREATE_SESSION
8975 * If nfs4_init_session set the fore channel request and response sizes,
8976 * use them.
8977 *
8978 * Set the back channel max_resp_sz_cached to zero to force the client to
8979 * always set csa_cachethis to FALSE because the current implementation
8980 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
8981 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04008982static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
8983 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04008984{
Andy Adamson18aad3d2013-06-26 12:21:49 -04008985 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04008986 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Trond Myklebust7402a4f2019-07-16 13:51:29 -04008987 unsigned int max_bc_slots = rpc_num_bc_slots(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04008988
Andy Adamson18aad3d2013-06-26 12:21:49 -04008989 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
8990 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
8991
Andy Adamsonfc931582009-04-01 09:22:31 -04008992 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04008993 args->fc_attrs.max_rqst_sz = max_rqst_sz;
8994 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04008995 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05008996 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04008997
8998 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05008999 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04009000 __func__,
9001 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05009002 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04009003
9004 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04009005 args->bc_attrs.max_rqst_sz = max_bc_payload;
9006 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04009007 args->bc_attrs.max_resp_sz_cached = 0;
9008 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust62421cd2018-08-11 11:52:39 -04009009 args->bc_attrs.max_reqs = max_t(unsigned short, max_session_cb_slots, 1);
Trond Myklebust7402a4f2019-07-16 13:51:29 -04009010 if (args->bc_attrs.max_reqs > max_bc_slots)
9011 args->bc_attrs.max_reqs = max_bc_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04009012
9013 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
9014 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
9015 __func__,
9016 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
9017 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
9018 args->bc_attrs.max_reqs);
9019}
9020
Trond Myklebust79969dd2015-02-18 11:30:18 -08009021static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
9022 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04009023{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04009024 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08009025 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04009026
J. Bruce Fields43c2e882010-10-02 15:19:01 -04009027 if (rcvd->max_resp_sz > sent->max_resp_sz)
9028 return -EINVAL;
9029 /*
9030 * Our requested max_ops is the minimum we need; we're not
9031 * prepared to break up compounds into smaller pieces than that.
9032 * So, no point even trying to continue if the server won't
9033 * cooperate:
9034 */
9035 if (rcvd->max_ops < sent->max_ops)
9036 return -EINVAL;
9037 if (rcvd->max_reqs == 0)
9038 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04009039 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
9040 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04009041 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04009042}
9043
Trond Myklebust79969dd2015-02-18 11:30:18 -08009044static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
9045 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04009046{
9047 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08009048 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04009049
Trond Myklebustb1c0df52015-02-18 11:34:58 -08009050 if (!(res->flags & SESSION4_BACK_CHAN))
9051 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04009052 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
9053 return -EINVAL;
9054 if (rcvd->max_resp_sz < sent->max_resp_sz)
9055 return -EINVAL;
9056 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
9057 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04009058 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04009059 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04009060 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04009061 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08009062out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04009063 return 0;
9064}
Andy Adamson8d353012009-04-01 09:22:32 -04009065
Andy Adamson8d353012009-04-01 09:22:32 -04009066static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08009067 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04009068{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04009069 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04009070
Trond Myklebust79969dd2015-02-18 11:30:18 -08009071 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04009072 if (ret)
9073 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08009074 return nfs4_verify_back_channel_attrs(args, res);
9075}
9076
9077static void nfs4_update_session(struct nfs4_session *session,
9078 struct nfs41_create_session_res *res)
9079{
9080 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05009081 /* Mark client id and session as being confirmed */
9082 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
9083 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08009084 session->flags = res->flags;
9085 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08009086 if (res->flags & SESSION4_BACK_CHAN)
9087 memcpy(&session->bc_attrs, &res->bc_attrs,
9088 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04009089}
9090
Trond Myklebust848f5bd2012-05-25 17:51:23 -04009091static int _nfs4_proc_create_session(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11009092 const struct cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04009093{
9094 struct nfs4_session *session = clp->cl_session;
9095 struct nfs41_create_session_args args = {
9096 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08009097 .clientid = clp->cl_clientid,
9098 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04009099 .cb_program = NFS4_CALLBACK,
9100 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08009101 struct nfs41_create_session_res res;
9102
Andy Adamsonfc931582009-04-01 09:22:31 -04009103 struct rpc_message msg = {
9104 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
9105 .rpc_argp = &args,
9106 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04009107 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04009108 };
9109 int status;
9110
Chuck Lever6b26cc82016-05-02 14:40:40 -04009111 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04009112 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04009113
NeilBrown5a0c2572019-05-30 10:41:28 +10009114 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
9115 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04009116 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04009117
Trond Myklebustb519d402016-09-11 14:50:01 -04009118 switch (status) {
9119 case -NFS4ERR_STALE_CLIENTID:
9120 case -NFS4ERR_DELAY:
9121 case -ETIMEDOUT:
9122 case -EACCES:
9123 case -EAGAIN:
9124 goto out;
zhengbin8b98a532019-12-19 18:34:47 +08009125 }
Trond Myklebustb519d402016-09-11 14:50:01 -04009126
9127 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05009128 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04009129 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08009130 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04009131 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08009132 if (status)
9133 goto out;
9134 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04009135 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08009136out:
Andy Adamsonfc931582009-04-01 09:22:31 -04009137 return status;
9138}
9139
9140/*
9141 * Issues a CREATE_SESSION operation to the server.
9142 * It is the responsibility of the caller to verify the session is
9143 * expired before calling this routine.
9144 */
NeilBrowna52458b2018-12-03 11:30:31 +11009145int nfs4_proc_create_session(struct nfs_client *clp, const struct cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04009146{
9147 int status;
9148 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04009149 struct nfs4_session *session = clp->cl_session;
9150
9151 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
9152
Trond Myklebust848f5bd2012-05-25 17:51:23 -04009153 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04009154 if (status)
9155 goto out;
9156
Andy Adamsonaacd5532011-11-09 13:58:21 -05009157 /* Init or reset the session slot tables */
9158 status = nfs4_setup_session_slot_tables(session);
9159 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04009160 if (status)
9161 goto out;
9162
9163 ptr = (unsigned *)&session->sess_id.data[0];
9164 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
9165 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04009166out:
Andy Adamsonfc931582009-04-01 09:22:31 -04009167 return status;
9168}
9169
Andy Adamson0f3e66c2009-04-01 09:22:34 -04009170/*
9171 * Issue the over-the-wire RPC DESTROY_SESSION.
9172 * The caller must serialize access to this routine.
9173 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04009174int nfs4_proc_destroy_session(struct nfs4_session *session,
NeilBrowna52458b2018-12-03 11:30:31 +11009175 const struct cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04009176{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04009177 struct rpc_message msg = {
9178 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
9179 .rpc_argp = session,
9180 .rpc_cred = cred,
9181 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04009182 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04009183
Andy Adamson0f3e66c2009-04-01 09:22:34 -04009184 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05009185 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
9186 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04009187
NeilBrown5a0c2572019-05-30 10:41:28 +10009188 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
9189 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04009190 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04009191
9192 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04009193 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04009194 "Session has been destroyed regardless...\n", status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04009195 return status;
9196}
9197
Trond Myklebust7b38c362012-05-23 13:23:31 -04009198/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009199 * Renew the cl_session lease.
9200 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04009201struct nfs4_sequence_data {
9202 struct nfs_client *clp;
9203 struct nfs4_sequence_args args;
9204 struct nfs4_sequence_res res;
9205};
9206
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08009207static void nfs41_sequence_release(void *data)
9208{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04009209 struct nfs4_sequence_data *calldata = data;
9210 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08009211
Elena Reshetova212bf412017-10-20 12:53:38 +03009212 if (refcount_read(&clp->cl_count) > 1)
Alexandros Batsakis71358402010-02-05 03:45:05 -08009213 nfs4_schedule_state_renewal(clp);
9214 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04009215 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08009216}
9217
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009218static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
9219{
9220 switch(task->tk_status) {
9221 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009222 rpc_delay(task, NFS4_POLL_RETRY_MAX);
9223 return -EAGAIN;
9224 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05009225 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009226 }
9227 return 0;
9228}
9229
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08009230static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009231{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04009232 struct nfs4_sequence_data *calldata = data;
9233 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009234
Trond Myklebust14516c32010-07-31 14:29:06 -04009235 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
9236 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009237
Trond Myklebustc6d01c62013-08-09 11:51:26 -04009238 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009239 if (task->tk_status < 0) {
9240 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Elena Reshetova212bf412017-10-20 12:53:38 +03009241 if (refcount_read(&clp->cl_count) == 1)
Chuck Lever86882c72021-10-16 18:03:04 -04009242 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009243
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009244 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
9245 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009246 return;
9247 }
9248 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009249 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009250}
9251
9252static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
9253{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04009254 struct nfs4_sequence_data *calldata = data;
9255 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009256 struct nfs4_sequence_args *args;
9257 struct nfs4_sequence_res *res;
9258
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009259 args = task->tk_msg.rpc_argp;
9260 res = task->tk_msg.rpc_resp;
9261
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009262 nfs4_setup_sequence(clp, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009263}
9264
9265static const struct rpc_call_ops nfs41_sequence_ops = {
9266 .rpc_call_done = nfs41_sequence_call_done,
9267 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08009268 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009269};
9270
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009271static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11009272 const struct cred *cred,
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009273 struct nfs4_slot *slot,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009274 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009275{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04009276 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009277 struct rpc_message msg = {
9278 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
9279 .rpc_cred = cred,
9280 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009281 struct rpc_task_setup task_setup_data = {
9282 .rpc_client = clp->cl_rpcclient,
9283 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009284 .callback_ops = &nfs41_sequence_ops,
Olga Kornievskaia85e39fe2021-06-23 23:28:51 -04009285 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT | RPC_TASK_MOVEABLE,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009286 };
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009287 struct rpc_task *ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009288
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009289 ret = ERR_PTR(-EIO);
Elena Reshetova212bf412017-10-20 12:53:38 +03009290 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009291 goto out_err;
9292
9293 ret = ERR_PTR(-ENOMEM);
Benny Halevydfb4f3092010-09-24 09:17:01 -04009294 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009295 if (calldata == NULL)
9296 goto out_put_clp;
Anna Schumakerfba83f32018-05-04 16:22:50 -04009297 nfs4_init_sequence(&calldata->args, &calldata->res, 0, is_privileged);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009298 nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04009299 msg.rpc_argp = &calldata->args;
9300 msg.rpc_resp = &calldata->res;
9301 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009302 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009303
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009304 ret = rpc_run_task(&task_setup_data);
9305 if (IS_ERR(ret))
9306 goto out_err;
9307 return ret;
9308out_put_clp:
9309 nfs_put_client(clp);
9310out_err:
9311 nfs41_release_slot(slot);
9312 return ret;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009313}
9314
NeilBrowna52458b2018-12-03 11:30:31 +11009315static int nfs41_proc_async_sequence(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009316{
9317 struct rpc_task *task;
9318 int ret = 0;
9319
Trond Myklebust2f60ea62011-08-24 15:07:37 -04009320 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04009321 return -EAGAIN;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009322 task = _nfs41_proc_sequence(clp, cred, NULL, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009323 if (IS_ERR(task))
9324 ret = PTR_ERR(task);
9325 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08009326 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009327 dprintk("<-- %s status=%d\n", __func__, ret);
9328 return ret;
9329}
9330
NeilBrowna52458b2018-12-03 11:30:31 +11009331static int nfs4_proc_sequence(struct nfs_client *clp, const struct cred *cred)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009332{
9333 struct rpc_task *task;
9334 int ret;
9335
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009336 task = _nfs41_proc_sequence(clp, cred, NULL, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009337 if (IS_ERR(task)) {
9338 ret = PTR_ERR(task);
9339 goto out;
9340 }
9341 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04009342 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009343 ret = task->tk_status;
9344 rpc_put_task(task);
9345out:
9346 dprintk("<-- %s status=%d\n", __func__, ret);
9347 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009348}
9349
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009350struct nfs4_reclaim_complete_data {
9351 struct nfs_client *clp;
9352 struct nfs41_reclaim_complete_args arg;
9353 struct nfs41_reclaim_complete_res res;
9354};
9355
9356static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
9357{
9358 struct nfs4_reclaim_complete_data *calldata = data;
9359
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009360 nfs4_setup_sequence(calldata->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04009361 &calldata->arg.seq_args,
9362 &calldata->res.seq_res,
9363 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009364}
9365
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009366static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
9367{
9368 switch(task->tk_status) {
9369 case 0:
Jeff Layton57174592018-03-18 08:37:03 -04009370 wake_up_all(&clp->cl_lock_waitq);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05009371 fallthrough;
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009372 case -NFS4ERR_COMPLETE_ALREADY:
9373 case -NFS4ERR_WRONG_CRED: /* What to do here? */
9374 break;
9375 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009376 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05009377 fallthrough;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04009378 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009379 return -EAGAIN;
Trond Myklebust0048fdd2017-05-04 13:44:04 -04009380 case -NFS4ERR_BADSESSION:
9381 case -NFS4ERR_DEADSESSION:
9382 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
Trond Myklebust0048fdd2017-05-04 13:44:04 -04009383 break;
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009384 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05009385 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009386 }
9387 return 0;
9388}
9389
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009390static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
9391{
9392 struct nfs4_reclaim_complete_data *calldata = data;
9393 struct nfs_client *clp = calldata->clp;
9394 struct nfs4_sequence_res *res = &calldata->res.seq_res;
9395
Trond Myklebust14516c32010-07-31 14:29:06 -04009396 if (!nfs41_sequence_done(task, res))
9397 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009398
Trond Myklebustc6d01c62013-08-09 11:51:26 -04009399 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009400 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
9401 rpc_restart_call_prepare(task);
9402 return;
9403 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009404}
9405
9406static void nfs4_free_reclaim_complete_data(void *data)
9407{
9408 struct nfs4_reclaim_complete_data *calldata = data;
9409
9410 kfree(calldata);
9411}
9412
9413static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
9414 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
9415 .rpc_call_done = nfs4_reclaim_complete_done,
9416 .rpc_release = nfs4_free_reclaim_complete_data,
9417};
9418
9419/*
9420 * Issue a global reclaim complete.
9421 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04009422static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11009423 const struct cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009424{
9425 struct nfs4_reclaim_complete_data *calldata;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009426 struct rpc_message msg = {
9427 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04009428 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009429 };
9430 struct rpc_task_setup task_setup_data = {
9431 .rpc_client = clp->cl_rpcclient,
9432 .rpc_message = &msg,
9433 .callback_ops = &nfs4_reclaim_complete_call_ops,
Anna Schumaker4c952e32019-08-14 15:46:48 -04009434 .flags = RPC_TASK_NO_ROUND_ROBIN,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009435 };
9436 int status = -ENOMEM;
9437
Trond Myklebust8535b2b2010-05-13 12:51:01 -04009438 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009439 if (calldata == NULL)
9440 goto out;
9441 calldata->clp = clp;
9442 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009443
Anna Schumakerfba83f32018-05-04 16:22:50 -04009444 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0, 1);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009445 msg.rpc_argp = &calldata->arg;
9446 msg.rpc_resp = &calldata->res;
9447 task_setup_data.callback_data = calldata;
Anna Schumaker4c952e32019-08-14 15:46:48 -04009448 status = nfs4_call_sync_custom(&task_setup_data);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009449out:
9450 dprintk("<-- %s status=%d\n", __func__, status);
9451 return status;
9452}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009453
9454static void
9455nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
9456{
9457 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00009458 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009459
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009460 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
Jeff Layton183d9e72016-05-17 12:28:47 -04009461 &lgp->res.seq_res, task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009462}
9463
9464static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
9465{
9466 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04009467
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04009468 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04009469}
9470
9471static int
9472nfs4_layoutget_handle_exception(struct rpc_task *task,
9473 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
9474{
Trond Myklebustee314c22012-10-01 17:25:48 -07009475 struct inode *inode = lgp->args.inode;
9476 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4e89bc2021-07-02 17:24:22 -04009477 struct pnfs_layout_hdr *lo = lgp->lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04009478 int nfs4err = task->tk_status;
9479 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04009480 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009481
Boaz Harroshed7e5422014-01-22 20:34:54 +02009482 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009483
Trond Myklebust2dbf8df2018-06-15 15:58:45 -04009484 nfs4_sequence_free_slot(&lgp->res.seq_res);
9485
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04009486 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009487 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07009488 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08009489
9490 /*
9491 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
9492 * on the file. set tk_status to -ENODATA to tell upper layer to
9493 * retry go inband.
9494 */
9495 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04009496 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08009497 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02009498 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07009499 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
9500 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
9501 */
9502 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04009503 status = -EOVERFLOW;
9504 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07009505 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02009506 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07009507 * (or clients) writing to the same RAID stripe except when
9508 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04009509 *
9510 * Treat it like we would RECALLCONFLICT -- we retry for a little
9511 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02009512 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009513 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04009514 if (lgp->args.minlength == 0) {
9515 status = -EOVERFLOW;
9516 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02009517 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04009518 status = -EBUSY;
9519 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04009520 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04009521 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04009522 break;
Trond Myklebust26f47442016-09-22 13:39:10 -04009523 case -NFS4ERR_DELEG_REVOKED:
9524 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustee314c22012-10-01 17:25:48 -07009525 case -NFS4ERR_EXPIRED:
9526 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04009527 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07009528 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04009529 /* If the open stateid was bad, then recover it. */
9530 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
Trond Myklebuste8fa33a2017-10-04 13:49:12 -04009531 !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07009532 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04009533 exception->state = lgp->args.ctx->state;
Trond Myklebust26f47442016-09-22 13:39:10 -04009534 exception->stateid = &lgp->args.stateid;
Trond Myklebust2259f962015-09-20 13:30:30 -04009535 break;
9536 }
Trond Myklebustee314c22012-10-01 17:25:48 -07009537
Trond Myklebustf7db0b22016-07-14 15:14:02 -04009538 /*
9539 * Mark the bad layout state as invalid, then retry
9540 */
Trond Myklebust668f4552016-07-24 17:08:59 -04009541 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04009542 spin_unlock(&inode->i_lock);
Trond Myklebust1f18b822017-04-29 10:10:17 -04009543 nfs_commit_inode(inode, 0);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04009544 pnfs_free_lseg_list(&head);
9545 status = -EAGAIN;
9546 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009547 }
Jeff Layton183d9e72016-05-17 12:28:47 -04009548
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04009549 err = nfs4_handle_exception(server, nfs4err, exception);
9550 if (!status) {
9551 if (exception->retry)
9552 status = -EAGAIN;
9553 else
9554 status = err;
9555 }
Trond Myklebustee314c22012-10-01 17:25:48 -07009556out:
Jeff Layton183d9e72016-05-17 12:28:47 -04009557 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009558}
9559
Fred Isamandacb4522016-09-19 17:47:09 -04009560size_t max_response_pages(struct nfs_server *server)
Idan Kedar85541162012-08-02 11:47:10 +03009561{
9562 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
9563 return nfs_page_array_len(0, max_resp_sz);
9564}
9565
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009566static void nfs4_layoutget_release(void *calldata)
9567{
9568 struct nfs4_layoutget *lgp = calldata;
9569
Trond Myklebustbd171932017-06-27 17:33:38 -04009570 nfs4_sequence_free_slot(&lgp->res.seq_res);
Trond Myklebust29a8bfe2018-05-30 17:16:20 -04009571 pnfs_layoutget_free(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009572}
9573
9574static const struct rpc_call_ops nfs4_layoutget_call_ops = {
9575 .rpc_call_prepare = nfs4_layoutget_prepare,
9576 .rpc_call_done = nfs4_layoutget_done,
9577 .rpc_release = nfs4_layoutget_release,
9578};
9579
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04009580struct pnfs_layout_segment *
Fred Isamandacb4522016-09-19 17:47:09 -04009581nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009582{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05009583 struct inode *inode = lgp->args.inode;
9584 struct nfs_server *server = NFS_SERVER(inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009585 struct rpc_task *task;
9586 struct rpc_message msg = {
9587 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
9588 .rpc_argp = &lgp->args,
9589 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04009590 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009591 };
9592 struct rpc_task_setup task_setup_data = {
9593 .rpc_client = server->client,
9594 .rpc_message = &msg,
9595 .callback_ops = &nfs4_layoutget_call_ops,
9596 .callback_data = lgp,
Olga Kornievskaia85e39fe2021-06-23 23:28:51 -04009597 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF |
9598 RPC_TASK_MOVEABLE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009599 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04009600 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04009601 struct nfs4_exception exception = {
9602 .inode = inode,
9603 .timeout = *timeout,
9604 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009605 int status = 0;
9606
Anna Schumakerfba83f32018-05-04 16:22:50 -04009607 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05009608
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009609 task = rpc_run_task(&task_setup_data);
Xiyu Yang6e476662020-04-25 21:04:40 +08009610
Anna Schumaker820bf852017-01-11 15:01:43 -05009611 status = rpc_wait_for_completion_task(task);
Trond Myklebust2dbf8df2018-06-15 15:58:45 -04009612 if (status != 0)
9613 goto out;
9614
Trond Myklebust18c07782019-02-13 07:55:31 -05009615 if (task->tk_status < 0) {
Jeff Layton183d9e72016-05-17 12:28:47 -04009616 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
9617 *timeout = exception.timeout;
Trond Myklebust18c07782019-02-13 07:55:31 -05009618 } else if (lgp->res.layoutp->len == 0) {
9619 status = -EAGAIN;
9620 *timeout = nfs4_update_delay(&exception.timeout);
Trond Myklebust2dbf8df2018-06-15 15:58:45 -04009621 } else
9622 lseg = pnfs_layout_process(lgp);
9623out:
Trond Myklebust1037e6e2013-08-14 16:36:51 -04009624 trace_nfs4_layoutget(lgp->args.ctx,
9625 &lgp->args.range,
9626 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05009627 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04009628 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04009629
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009630 rpc_put_task(task);
9631 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04009632 if (status)
9633 return ERR_PTR(status);
9634 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009635}
9636
Benny Halevycbe82602011-05-22 19:52:37 +03009637static void
9638nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
9639{
9640 struct nfs4_layoutreturn *lrp = calldata;
9641
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009642 nfs4_setup_sequence(lrp->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04009643 &lrp->args.seq_args,
9644 &lrp->res.seq_res,
9645 task);
Trond Myklebustc8bf7072018-06-15 16:31:02 -04009646 if (!pnfs_layout_is_valid(lrp->args.layout))
9647 rpc_exit(task, 0);
Benny Halevycbe82602011-05-22 19:52:37 +03009648}
9649
9650static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
9651{
9652 struct nfs4_layoutreturn *lrp = calldata;
9653 struct nfs_server *server;
9654
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04009655 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03009656 return;
9657
Trond Myklebust6109bcf2019-09-20 07:23:43 -04009658 /*
9659 * Was there an RPC level error? Assume the call succeeded,
9660 * and that we need to release the layout
9661 */
9662 if (task->tk_rpc_status != 0 && RPC_WAS_SENT(task)) {
9663 lrp->res.lrs_present = 0;
9664 return;
9665 }
9666
Benny Halevycbe82602011-05-22 19:52:37 +03009667 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05009668 switch (task->tk_status) {
Trond Myklebustff905142017-11-06 15:28:08 -05009669 case -NFS4ERR_OLD_STATEID:
Trond Myklebust30cb3ee2019-09-20 07:23:45 -04009670 if (nfs4_layout_refresh_old_stateid(&lrp->args.stateid,
Trond Myklebustecf84022018-08-15 21:35:46 -04009671 &lrp->args.range,
Trond Myklebustff905142017-11-06 15:28:08 -05009672 lrp->args.inode))
9673 goto out_restart;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05009674 fallthrough;
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05009675 default:
9676 task->tk_status = 0;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05009677 fallthrough;
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05009678 case 0:
9679 break;
9680 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10009681 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05009682 break;
Trond Myklebustff905142017-11-06 15:28:08 -05009683 goto out_restart;
Benny Halevycbe82602011-05-22 19:52:37 +03009684 }
Trond Myklebustff905142017-11-06 15:28:08 -05009685 return;
9686out_restart:
9687 task->tk_status = 0;
9688 nfs4_sequence_free_slot(&lrp->res.seq_res);
9689 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03009690}
9691
9692static void nfs4_layoutreturn_release(void *calldata)
9693{
9694 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04009695 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03009696
Trond Myklebust2a974422016-11-20 13:13:54 -05009697 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
Trond Myklebust68f74472016-10-12 19:50:54 -04009698 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04009699 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebust4d796d72016-09-23 11:38:08 -04009700 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
9701 lrp->ld_private.ops->free(&lrp->ld_private);
Trond Myklebust2f065dd2016-12-07 12:29:26 -05009702 pnfs_put_layout_hdr(lrp->args.layout);
9703 nfs_iput_and_deactive(lrp->inode);
Trond Myklebust44ea8df2020-04-02 15:37:02 -04009704 put_cred(lrp->cred);
Benny Halevycbe82602011-05-22 19:52:37 +03009705 kfree(calldata);
Benny Halevycbe82602011-05-22 19:52:37 +03009706}
9707
9708static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
9709 .rpc_call_prepare = nfs4_layoutreturn_prepare,
9710 .rpc_call_done = nfs4_layoutreturn_done,
9711 .rpc_release = nfs4_layoutreturn_release,
9712};
9713
Peng Tao6c166052014-11-17 09:30:40 +08009714int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03009715{
9716 struct rpc_task *task;
9717 struct rpc_message msg = {
9718 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
9719 .rpc_argp = &lrp->args,
9720 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04009721 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03009722 };
9723 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04009724 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03009725 .rpc_message = &msg,
9726 .callback_ops = &nfs4_layoutreturn_call_ops,
9727 .callback_data = lrp,
Olga Kornievskaia85e39fe2021-06-23 23:28:51 -04009728 .flags = RPC_TASK_MOVEABLE,
Benny Halevycbe82602011-05-22 19:52:37 +03009729 };
Peng Tao6c166052014-11-17 09:30:40 +08009730 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03009731
Andrew Elble99ade3c2015-12-02 09:39:51 -05009732 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
9733 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
9734 &task_setup_data.rpc_client, &msg);
9735
Trond Myklebustc3aba892021-06-01 11:35:56 -04009736 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05009737 if (!sync) {
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05009738 if (!lrp->inode) {
9739 nfs4_layoutreturn_release(lrp);
9740 return -EAGAIN;
9741 }
9742 task_setup_data.flags |= RPC_TASK_ASYNC;
9743 }
Trond Myklebustc3aba892021-06-01 11:35:56 -04009744 if (!lrp->inode)
9745 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1,
9746 1);
9747 else
9748 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1,
9749 0);
Benny Halevycbe82602011-05-22 19:52:37 +03009750 task = rpc_run_task(&task_setup_data);
9751 if (IS_ERR(task))
9752 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05009753 if (sync)
9754 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05009755 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03009756 dprintk("<-- %s status=%d\n", __func__, status);
9757 rpc_put_task(task);
9758 return status;
9759}
9760
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009761static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009762_nfs4_proc_getdeviceinfo(struct nfs_server *server,
9763 struct pnfs_device *pdev,
NeilBrowna52458b2018-12-03 11:30:31 +11009764 const struct cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009765{
9766 struct nfs4_getdeviceinfo_args args = {
9767 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04009768 .notify_types = NOTIFY_DEVICEID4_CHANGE |
9769 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009770 };
9771 struct nfs4_getdeviceinfo_res res = {
9772 .pdev = pdev,
9773 };
9774 struct rpc_message msg = {
9775 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
9776 .rpc_argp = &args,
9777 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009778 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009779 };
9780 int status;
9781
Bryan Schumaker7c513052011-03-24 17:12:24 +00009782 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04009783 if (res.notification & ~args.notify_types)
9784 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04009785 if (res.notification != args.notify_types)
9786 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04009787
Trond Myklebustcac1d3a2020-12-16 16:31:26 -05009788 trace_nfs4_getdeviceinfo(server, &pdev->dev_id, status);
9789
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009790 dprintk("<-- %s status=%d\n", __func__, status);
9791
9792 return status;
9793}
9794
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009795int nfs4_proc_getdeviceinfo(struct nfs_server *server,
9796 struct pnfs_device *pdev,
NeilBrowna52458b2018-12-03 11:30:31 +11009797 const struct cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009798{
9799 struct nfs4_exception exception = { };
9800 int err;
9801
9802 do {
9803 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009804 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009805 &exception);
9806 } while (exception.retry);
9807 return err;
9808}
9809EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
9810
Andy Adamson863a3c62011-03-23 13:27:54 +00009811static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
9812{
9813 struct nfs4_layoutcommit_data *data = calldata;
9814 struct nfs_server *server = NFS_SERVER(data->args.inode);
9815
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009816 nfs4_setup_sequence(server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04009817 &data->args.seq_args,
9818 &data->res.seq_res,
9819 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00009820}
9821
9822static void
9823nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
9824{
9825 struct nfs4_layoutcommit_data *data = calldata;
9826 struct nfs_server *server = NFS_SERVER(data->args.inode);
9827
Trond Myklebust6ba7db32012-10-22 20:07:20 -04009828 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00009829 return;
9830
9831 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009832 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
9833 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
9834 case -NFS4ERR_BADLAYOUT: /* no layout */
9835 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00009836 task->tk_status = 0;
Gustavo A. R. Silvaffb81712020-11-20 12:26:46 -06009837 break;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009838 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009839 break;
9840 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10009841 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009842 rpc_restart_call_prepare(task);
9843 return;
9844 }
9845 }
Andy Adamson863a3c62011-03-23 13:27:54 +00009846}
9847
9848static void nfs4_layoutcommit_release(void *calldata)
9849{
9850 struct nfs4_layoutcommit_data *data = calldata;
9851
Andy Adamsondb29c082011-07-30 20:52:38 -04009852 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05009853 nfs_post_op_update_inode_force_wcc(data->args.inode,
9854 data->res.fattr);
NeilBrowna52458b2018-12-03 11:30:31 +11009855 put_cred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05009856 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00009857 kfree(data);
9858}
9859
9860static const struct rpc_call_ops nfs4_layoutcommit_ops = {
9861 .rpc_call_prepare = nfs4_layoutcommit_prepare,
9862 .rpc_call_done = nfs4_layoutcommit_done,
9863 .rpc_release = nfs4_layoutcommit_release,
9864};
9865
9866int
Andy Adamsonef311532011-03-12 02:58:10 -05009867nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00009868{
9869 struct rpc_message msg = {
9870 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
9871 .rpc_argp = &data->args,
9872 .rpc_resp = &data->res,
9873 .rpc_cred = data->cred,
9874 };
9875 struct rpc_task_setup task_setup_data = {
9876 .task = &data->task,
9877 .rpc_client = NFS_CLIENT(data->args.inode),
9878 .rpc_message = &msg,
9879 .callback_ops = &nfs4_layoutcommit_ops,
9880 .callback_data = data,
Olga Kornievskaia85e39fe2021-06-23 23:28:51 -04009881 .flags = RPC_TASK_MOVEABLE,
Andy Adamson863a3c62011-03-23 13:27:54 +00009882 };
9883 struct rpc_task *task;
9884 int status = 0;
9885
Kinglong Meeb4839eb2015-07-01 12:00:13 +08009886 dprintk("NFS: initiating layoutcommit call. sync %d "
9887 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00009888 data->args.lastbytewritten,
9889 data->args.inode->i_ino);
9890
Trond Myklebust472e2592015-02-05 16:50:30 -05009891 if (!sync) {
9892 data->inode = nfs_igrab_and_active(data->args.inode);
9893 if (data->inode == NULL) {
9894 nfs4_layoutcommit_release(data);
9895 return -EAGAIN;
9896 }
9897 task_setup_data.flags = RPC_TASK_ASYNC;
9898 }
Anna Schumakerfba83f32018-05-04 16:22:50 -04009899 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Andy Adamson863a3c62011-03-23 13:27:54 +00009900 task = rpc_run_task(&task_setup_data);
9901 if (IS_ERR(task))
9902 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05009903 if (sync)
9904 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05009905 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00009906 dprintk("%s: status %d\n", __func__, status);
9907 rpc_put_task(task);
9908 return status;
9909}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009910
Trond Myklebust302fad72019-02-18 13:32:38 -05009911/*
Andy Adamson97431202013-08-08 10:57:56 -04009912 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
9913 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
9914 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009915static int
9916_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009917 struct nfs_fsinfo *info,
9918 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009919{
9920 struct nfs41_secinfo_no_name_args args = {
9921 .style = SECINFO_STYLE_CURRENT_FH,
9922 };
9923 struct nfs4_secinfo_res res = {
9924 .flavors = flavors,
9925 };
9926 struct rpc_message msg = {
9927 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
9928 .rpc_argp = &args,
9929 .rpc_resp = &res,
9930 };
Anna Schumakercc15e242019-08-14 16:22:31 -04009931 struct nfs4_call_sync_data data = {
9932 .seq_server = server,
9933 .seq_args = &args.seq_args,
9934 .seq_res = &res.seq_res,
9935 };
9936 struct rpc_task_setup task_setup = {
9937 .rpc_client = server->client,
9938 .rpc_message = &msg,
9939 .callback_ops = server->nfs_client->cl_mvops->call_sync_ops,
9940 .callback_data = &data,
9941 .flags = RPC_TASK_NO_ROUND_ROBIN,
9942 };
NeilBrowna52458b2018-12-03 11:30:31 +11009943 const struct cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009944 int status;
9945
9946 if (use_integrity) {
Colin Ian King48bb6ec2020-06-10 00:22:57 +01009947 task_setup.rpc_client = server->nfs_client->cl_rpcclient;
Anna Schumakercc15e242019-08-14 16:22:31 -04009948
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04009949 cred = nfs4_get_clid_cred(server->nfs_client);
9950 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009951 }
9952
Anna Schumakercc15e242019-08-14 16:22:31 -04009953 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
9954 status = nfs4_call_sync_custom(&task_setup);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009955 dprintk("<-- %s status=%d\n", __func__, status);
9956
NeilBrowna52458b2018-12-03 11:30:31 +11009957 put_cred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009958
9959 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009960}
9961
9962static int
9963nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9964 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
9965{
Trond Myklebust0688e642019-04-07 13:59:09 -04009966 struct nfs4_exception exception = {
9967 .interruptible = true,
9968 };
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009969 int err;
9970 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009971 /* first try using integrity protection */
9972 err = -NFS4ERR_WRONGSEC;
9973
9974 /* try to use integrity protection with machine cred */
9975 if (_nfs4_is_integrity_protected(server->nfs_client))
9976 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9977 flavors, true);
9978
9979 /*
9980 * if unable to use integrity protection, or SECINFO with
9981 * integrity protection returns NFS4ERR_WRONGSEC (which is
9982 * disallowed by spec, but exists in deployed servers) use
9983 * the current filesystem's rpc_client and the user cred.
9984 */
9985 if (err == -NFS4ERR_WRONGSEC)
9986 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9987 flavors, false);
9988
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009989 switch (err) {
9990 case 0:
9991 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05009992 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009993 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009994 default:
9995 err = nfs4_handle_exception(server, err, &exception);
9996 }
9997 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009998out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009999 return err;
10000}
10001
10002static int
10003nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
10004 struct nfs_fsinfo *info)
10005{
10006 int err;
10007 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -040010008 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -040010009 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -040010010 struct nfs4_secinfo4 *secinfo;
10011 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -040010012
10013 page = alloc_page(GFP_KERNEL);
10014 if (!page) {
10015 err = -ENOMEM;
10016 goto out;
10017 }
10018
10019 flavors = page_address(page);
10020 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
10021
10022 /*
10023 * Fall back on "guess and check" method if
10024 * the server doesn't support SECINFO_NO_NAME
10025 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -050010026 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -040010027 err = nfs4_find_root_sec(server, fhandle, info);
10028 goto out_freepage;
10029 }
10030 if (err)
10031 goto out_freepage;
10032
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -040010033 for (i = 0; i < flavors->num_flavors; i++) {
10034 secinfo = &flavors->flavors[i];
10035
10036 switch (secinfo->flavor) {
10037 case RPC_AUTH_NULL:
10038 case RPC_AUTH_UNIX:
10039 case RPC_AUTH_GSS:
10040 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
10041 &secinfo->flavor_info);
10042 break;
10043 default:
10044 flavor = RPC_AUTH_MAXFLAVOR;
10045 break;
10046 }
10047
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -040010048 if (!nfs_auth_info_match(&server->auth_info, flavor))
10049 flavor = RPC_AUTH_MAXFLAVOR;
10050
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -040010051 if (flavor != RPC_AUTH_MAXFLAVOR) {
10052 err = nfs4_lookup_root_sec(server, fhandle,
10053 info, flavor);
10054 if (!err)
10055 break;
10056 }
10057 }
10058
10059 if (flavor == RPC_AUTH_MAXFLAVOR)
10060 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -040010061
10062out_freepage:
10063 put_page(page);
10064 if (err == -EACCES)
10065 return -EPERM;
10066out:
10067 return err;
10068}
Bryan Schumaker1cab0652012-01-31 10:39:29 -050010069
Trond Myklebustab7cb0d2013-05-20 11:20:27 -040010070static int _nfs41_test_stateid(struct nfs_server *server,
10071 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +110010072 const struct cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -040010073{
10074 int status;
10075 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -050010076 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -040010077 };
10078 struct nfs41_test_stateid_res res;
10079 struct rpc_message msg = {
10080 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
10081 .rpc_argp = &args,
10082 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -040010083 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -040010084 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -040010085 struct rpc_clnt *rpc_client = server->client;
10086
10087 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
10088 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -050010089
Chuck Lever38527b12012-07-11 16:30:23 -040010090 dprintk("NFS call test_stateid %p\n", stateid);
Anna Schumakerfba83f32018-05-04 16:22:50 -040010091 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Weston Andros Adamson3787d502013-08-13 16:37:36 -040010092 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -040010093 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -040010094 if (status != NFS_OK) {
10095 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -040010096 return status;
Chuck Lever38527b12012-07-11 16:30:23 -040010097 }
10098 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -040010099 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -040010100}
10101
Trond Myklebust43912bb2016-09-22 13:38:56 -040010102static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
10103 int err, struct nfs4_exception *exception)
10104{
10105 exception->retry = 0;
10106 switch(err) {
10107 case -NFS4ERR_DELAY:
Trond Myklebust76e8a1b2016-09-22 13:39:19 -040010108 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust43912bb2016-09-22 13:38:56 -040010109 nfs4_handle_exception(server, err, exception);
10110 break;
10111 case -NFS4ERR_BADSESSION:
10112 case -NFS4ERR_BADSLOT:
10113 case -NFS4ERR_BAD_HIGH_SLOT:
10114 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
10115 case -NFS4ERR_DEADSESSION:
10116 nfs4_do_handle_exception(server, err, exception);
10117 }
10118}
10119
Chuck Lever38527b12012-07-11 16:30:23 -040010120/**
10121 * nfs41_test_stateid - perform a TEST_STATEID operation
10122 *
10123 * @server: server / transport on which to perform the operation
10124 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -040010125 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -040010126 *
10127 * Returns NFS_OK if the server recognizes that "stateid" is valid.
10128 * Otherwise a negative NFS4ERR value is returned if the operation
10129 * failed or the state ID is not currently valid.
10130 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -040010131static int nfs41_test_stateid(struct nfs_server *server,
10132 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +110010133 const struct cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -040010134{
Trond Myklebust0688e642019-04-07 13:59:09 -040010135 struct nfs4_exception exception = {
10136 .interruptible = true,
10137 };
Bryan Schumaker7d974792011-06-02 14:59:08 -040010138 int err;
10139 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -040010140 err = _nfs41_test_stateid(server, stateid, cred);
Trond Myklebust43912bb2016-09-22 13:38:56 -040010141 nfs4_handle_delay_or_session_error(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -040010142 } while (exception.retry);
10143 return err;
10144}
Bryan Schumaker9aeda352011-06-02 14:59:09 -040010145
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010146struct nfs_free_stateid_data {
10147 struct nfs_server *server;
10148 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -040010149 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010150};
10151
10152static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
10153{
10154 struct nfs_free_stateid_data *data = calldata;
Anna Schumaker7981c8a2017-01-10 11:39:53 -050010155 nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010156 &data->args.seq_args,
10157 &data->res.seq_res,
10158 task);
10159}
10160
10161static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
10162{
10163 struct nfs_free_stateid_data *data = calldata;
10164
10165 nfs41_sequence_done(task, &data->res.seq_res);
10166
10167 switch (task->tk_status) {
10168 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +100010169 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010170 rpc_restart_call_prepare(task);
10171 }
10172}
10173
10174static void nfs41_free_stateid_release(void *calldata)
10175{
Scott Mayhew576acc22021-11-03 06:24:40 -040010176 struct nfs_free_stateid_data *data = calldata;
10177 struct nfs_client *clp = data->server->nfs_client;
10178
10179 nfs_put_client(clp);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010180 kfree(calldata);
10181}
10182
Trond Myklebust17f26b12013-08-21 15:48:42 -040010183static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010184 .rpc_call_prepare = nfs41_free_stateid_prepare,
10185 .rpc_call_done = nfs41_free_stateid_done,
10186 .rpc_release = nfs41_free_stateid_release,
10187};
10188
Anna Schumaker2f261022018-05-15 13:03:39 -040010189/**
10190 * nfs41_free_stateid - perform a FREE_STATEID operation
10191 *
10192 * @server: server / transport on which to perform the operation
10193 * @stateid: state ID to release
10194 * @cred: credential
Trond Myklebust302fad72019-02-18 13:32:38 -050010195 * @privileged: set to true if this call needs to be privileged
Anna Schumaker2f261022018-05-15 13:03:39 -040010196 *
10197 * Note: this function is always asynchronous.
10198 */
10199static int nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -040010200 const nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +110010201 const struct cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010202 bool privileged)
10203{
Bryan Schumaker9aeda352011-06-02 14:59:09 -040010204 struct rpc_message msg = {
10205 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -040010206 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -040010207 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010208 struct rpc_task_setup task_setup = {
10209 .rpc_client = server->client,
10210 .rpc_message = &msg,
10211 .callback_ops = &nfs41_free_stateid_ops,
Olga Kornievskaia85e39fe2021-06-23 23:28:51 -040010212 .flags = RPC_TASK_ASYNC | RPC_TASK_MOVEABLE,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010213 };
10214 struct nfs_free_stateid_data *data;
Anna Schumaker2f261022018-05-15 13:03:39 -040010215 struct rpc_task *task;
Scott Mayhew576acc22021-11-03 06:24:40 -040010216 struct nfs_client *clp = server->nfs_client;
10217
10218 if (!refcount_inc_not_zero(&clp->cl_count))
10219 return -EIO;
Bryan Schumaker9aeda352011-06-02 14:59:09 -040010220
Weston Andros Adamson3787d502013-08-13 16:37:36 -040010221 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
10222 &task_setup.rpc_client, &msg);
10223
Chuck Lever38527b12012-07-11 16:30:23 -040010224 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010225 data = kmalloc(sizeof(*data), GFP_NOFS);
10226 if (!data)
Anna Schumaker2f261022018-05-15 13:03:39 -040010227 return -ENOMEM;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010228 data->server = server;
10229 nfs4_stateid_copy(&data->args.stateid, stateid);
10230
10231 task_setup.callback_data = data;
10232
10233 msg.rpc_argp = &data->args;
10234 msg.rpc_resp = &data->res;
Anna Schumakerfba83f32018-05-04 16:22:50 -040010235 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, privileged);
Anna Schumaker2f261022018-05-15 13:03:39 -040010236 task = rpc_run_task(&task_setup);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010237 if (IS_ERR(task))
10238 return PTR_ERR(task);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010239 rpc_put_task(task);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -040010240 return 0;
Bryan Schumaker9aeda352011-06-02 14:59:09 -040010241}
Trond Myklebust36281ca2012-03-04 18:13:56 -050010242
Jeff Laytonf1cdae82014-05-01 06:28:47 -040010243static void
10244nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -040010245{
NeilBrowna52458b2018-12-03 11:30:31 +110010246 const struct cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -040010247
Trond Myklebustf0b0bf82016-09-22 13:39:04 -040010248 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -040010249 nfs4_free_lock_state(server, lsp);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -040010250}
10251
Trond Myklebust36281ca2012-03-04 18:13:56 -050010252static bool nfs41_match_stateid(const nfs4_stateid *s1,
10253 const nfs4_stateid *s2)
10254{
Trond Myklebust93b717f2016-05-16 17:42:43 -040010255 if (s1->type != s2->type)
10256 return false;
10257
Trond Myklebust2d2f24a2012-03-04 18:13:57 -050010258 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -050010259 return false;
10260
Trond Myklebust2d2f24a2012-03-04 18:13:57 -050010261 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -050010262 return true;
Trond Myklebust36281ca2012-03-04 18:13:56 -050010263
Anna Schumaker045c5512017-01-11 16:59:48 -050010264 return s1->seqid == 0 || s2->seqid == 0;
Trond Myklebust36281ca2012-03-04 18:13:56 -050010265}
10266
Andy Adamson557134a2009-04-01 09:21:53 -040010267#endif /* CONFIG_NFS_V4_1 */
10268
Trond Myklebust36281ca2012-03-04 18:13:56 -050010269static bool nfs4_match_stateid(const nfs4_stateid *s1,
10270 const nfs4_stateid *s2)
10271{
Trond Myklebustf597c532012-03-04 18:13:56 -050010272 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -050010273}
10274
10275
Trond Myklebust17280172012-03-11 13:11:00 -040010276static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -050010277 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -050010278 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010279 .recover_open = nfs4_open_reclaim,
10280 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -040010281 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -040010282 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010283};
10284
Andy Adamson591d71c2009-04-01 09:22:47 -040010285#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -040010286static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -040010287 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
10288 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
10289 .recover_open = nfs4_open_reclaim,
10290 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -050010291 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -050010292 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -040010293 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -040010294};
10295#endif /* CONFIG_NFS_V4_1 */
10296
Trond Myklebust17280172012-03-11 13:11:00 -040010297static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -050010298 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -050010299 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +030010300 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010301 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -040010302 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010303};
10304
Andy Adamson591d71c2009-04-01 09:22:47 -040010305#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -040010306static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -040010307 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
10308 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -040010309 .recover_open = nfs41_open_expired,
10310 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -050010311 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -040010312};
10313#endif /* CONFIG_NFS_V4_1 */
10314
Trond Myklebust17280172012-03-11 13:11:00 -040010315static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -040010316 .sched_state_renewal = nfs4_proc_async_renew,
NeilBrownf15e1e82018-12-03 11:30:30 +110010317 .get_state_renewal_cred = nfs4_get_renew_cred,
Benny Halevy8e69514f2009-04-01 09:22:45 -040010318 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -040010319};
10320
10321#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -040010322static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -040010323 .sched_state_renewal = nfs41_proc_async_sequence,
NeilBrownf15e1e82018-12-03 11:30:30 +110010324 .get_state_renewal_cred = nfs4_get_machine_cred,
Benny Halevy8e69514f2009-04-01 09:22:45 -040010325 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -040010326};
10327#endif
10328
Chuck Leverec011fe2013-10-17 14:12:39 -040010329static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -040010330 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -040010331 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -040010332};
10333
10334#if defined(CONFIG_NFS_V4_1)
10335static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -040010336 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -040010337 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -040010338};
10339#endif /* CONFIG_NFS_V4_1 */
10340
Trond Myklebust97dc1352010-06-16 09:52:26 -040010341static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
10342 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -040010343 .init_caps = NFS_CAP_READDIRPLUS
10344 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -040010345 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -040010346 .init_client = nfs40_init_client,
10347 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -050010348 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -040010349 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -040010350 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust45870d62016-09-22 13:38:59 -040010351 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -050010352 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -040010353 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -040010354 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
10355 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
10356 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -040010357 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -040010358};
10359
10360#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -050010361static struct nfs_seqid *
10362nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
10363{
10364 return NULL;
10365}
10366
Trond Myklebust97dc1352010-06-16 09:52:26 -040010367static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
10368 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -040010369 .init_caps = NFS_CAP_READDIRPLUS
10370 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -040010371 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -040010372 | NFS_CAP_STATEID_NFSV41
Fred Isaman6e012602016-10-04 15:26:41 -040010373 | NFS_CAP_ATOMIC_OPEN_V1
10374 | NFS_CAP_LGOPEN,
Chuck Leverabf79bb2013-08-09 12:49:11 -040010375 .init_client = nfs41_init_client,
10376 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -050010377 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -040010378 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -040010379 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust45870d62016-09-22 13:38:59 -040010380 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -050010381 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -040010382 .session_trunk = nfs4_test_session_trunk,
Chuck Lever9915ea72013-08-09 12:48:27 -040010383 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -040010384 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
10385 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
10386 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -040010387 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -040010388};
10389#endif
10390
Steve Dickson42c2c422013-05-22 12:50:38 -040010391#if defined(CONFIG_NFS_V4_2)
10392static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
10393 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -040010394 .init_caps = NFS_CAP_READDIRPLUS
10395 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -040010396 | NFS_CAP_POSIX_LOCK
10397 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -040010398 | NFS_CAP_ATOMIC_OPEN_V1
Fred Isaman6e012602016-10-04 15:26:41 -040010399 | NFS_CAP_LGOPEN
Anna Schumakerf4ac1672014-11-25 13:18:15 -050010400 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -040010401 | NFS_CAP_COPY
Olga Kornievskaiacb95dee2018-07-09 15:13:29 -040010402 | NFS_CAP_OFFLOAD_CANCEL
Olga Kornievskaia04915672019-06-04 16:14:30 -040010403 | NFS_CAP_COPY_NOTIFY
Anna Schumaker624bd5b2014-11-25 13:18:16 -050010404 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -040010405 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +080010406 | NFS_CAP_LAYOUTSTATS
Trond Myklebust3eb86092019-02-08 10:31:05 -050010407 | NFS_CAP_CLONE
Anna Schumakerc5675522014-05-28 13:41:22 -040010408 | NFS_CAP_LAYOUTERROR
10409 | NFS_CAP_READ_PLUS,
Chuck Leverabf79bb2013-08-09 12:49:11 -040010410 .init_client = nfs41_init_client,
10411 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -040010412 .match_stateid = nfs41_match_stateid,
10413 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -040010414 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -040010415 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust45870d62016-09-22 13:38:59 -040010416 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -050010417 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -040010418 .session_trunk = nfs4_test_session_trunk,
Steve Dickson42c2c422013-05-22 12:50:38 -040010419 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
10420 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
10421 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +080010422 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -040010423};
10424#endif
10425
Trond Myklebust97dc1352010-06-16 09:52:26 -040010426const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
10427 [0] = &nfs_v4_0_minor_ops,
10428#if defined(CONFIG_NFS_V4_1)
10429 [1] = &nfs_v4_1_minor_ops,
10430#endif
Steve Dickson42c2c422013-05-22 12:50:38 -040010431#if defined(CONFIG_NFS_V4_2)
10432 [2] = &nfs_v4_2_minor_ops,
10433#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -040010434};
10435
Trond Myklebust13997822016-07-24 17:10:52 -040010436static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +010010437{
Frank van der Linden012a2112020-06-23 22:39:03 +000010438 ssize_t error, error2, error3;
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +010010439
10440 error = generic_listxattr(dentry, list, size);
10441 if (error < 0)
10442 return error;
10443 if (list) {
10444 list += error;
10445 size -= error;
10446 }
10447
10448 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
10449 if (error2 < 0)
10450 return error2;
Frank van der Linden012a2112020-06-23 22:39:03 +000010451
10452 if (list) {
10453 list += error2;
10454 size -= error2;
10455 }
10456
10457 error3 = nfs4_listxattr_nfs4_user(d_inode(dentry), list, size);
10458 if (error3 < 0)
10459 return error3;
10460
10461 return error + error2 + error3;
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +010010462}
10463
Trond Myklebust17f26b12013-08-21 15:48:42 -040010464static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -040010465 .create = nfs_create,
10466 .lookup = nfs_lookup,
10467 .atomic_open = nfs_atomic_open,
10468 .link = nfs_link,
10469 .unlink = nfs_unlink,
10470 .symlink = nfs_symlink,
10471 .mkdir = nfs_mkdir,
10472 .rmdir = nfs_rmdir,
10473 .mknod = nfs_mknod,
10474 .rename = nfs_rename,
10475 .permission = nfs_permission,
10476 .getattr = nfs_getattr,
10477 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +010010478 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -040010479};
10480
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -080010481static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +000010482 .permission = nfs_permission,
10483 .getattr = nfs_getattr,
10484 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +010010485 .listxattr = nfs4_listxattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +000010486};
10487
David Howells509de812006-08-22 20:06:11 -040010488const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010489 .version = 4, /* protocol version */
10490 .dentry_ops = &nfs4_dentry_operations,
10491 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +000010492 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -040010493 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010494 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -040010495 .submount = nfs4_submount,
David Howellsf2aedb72019-12-10 07:31:13 -050010496 .try_get_tree = nfs4_try_get_tree,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010497 .getattr = nfs4_proc_getattr,
10498 .setattr = nfs4_proc_setattr,
10499 .lookup = nfs4_proc_lookup,
Jeff Layton5b5faaf2017-06-29 06:34:52 -070010500 .lookupp = nfs4_proc_lookupp,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010501 .access = nfs4_proc_access,
10502 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010503 .create = nfs4_proc_create,
10504 .remove = nfs4_proc_remove,
10505 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -040010506 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010507 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -040010508 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -040010509 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -040010510 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010511 .link = nfs4_proc_link,
10512 .symlink = nfs4_proc_symlink,
10513 .mkdir = nfs4_proc_mkdir,
Trond Myklebust912678d2018-03-20 16:43:15 -040010514 .rmdir = nfs4_proc_rmdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010515 .readdir = nfs4_proc_readdir,
10516 .mknod = nfs4_proc_mknod,
10517 .statfs = nfs4_proc_statfs,
10518 .fsinfo = nfs4_proc_fsinfo,
10519 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -040010520 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010521 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -040010522 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010523 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -050010524 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010525 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -050010526 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010527 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -040010528 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -050010529 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010530 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +000010531 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -040010532 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -040010533 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -040010534 .have_delegation = nfs4_have_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -040010535 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +000010536 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -040010537 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -040010538 .create_server = nfs4_create_server,
10539 .clone_server = nfs_clone_server,
Olga Kornievskaia1976b2b2022-01-12 10:27:38 -050010540 .discover_trunking = nfs4_discover_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010541};
10542
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000010543static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +010010544 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000010545 .list = nfs4_xattr_list_nfs4_acl,
10546 .get = nfs4_xattr_get_nfs4_acl,
10547 .set = nfs4_xattr_set_nfs4_acl,
10548};
10549
Frank van der Linden012a2112020-06-23 22:39:03 +000010550#ifdef CONFIG_NFS_V4_2
10551static const struct xattr_handler nfs4_xattr_nfs4_user_handler = {
10552 .prefix = XATTR_USER_PREFIX,
10553 .get = nfs4_xattr_get_nfs4_user,
10554 .set = nfs4_xattr_set_nfs4_user,
10555};
10556#endif
10557
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000010558const struct xattr_handler *nfs4_xattr_handlers[] = {
10559 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -040010560#ifdef CONFIG_NFS_V4_SECURITY_LABEL
10561 &nfs4_xattr_nfs4_label_handler,
10562#endif
Frank van der Linden012a2112020-06-23 22:39:03 +000010563#ifdef CONFIG_NFS_V4_2
10564 &nfs4_xattr_nfs4_user_handler,
10565#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000010566 NULL
10567};