blob: e1214bb6b7ee5aa73e0612fa20fa2a13a15b546f [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);
Trond Myklebusta841b542018-04-07 13:50:59 -040096static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label, struct inode *inode);
NeilBrowna52458b2018-12-03 11:30:31 +110097static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
Trond Myklebust0ab64e02010-04-16 16:22:51 -040098 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +110099 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
David Quigley1775fd32013-05-22 12:50:42 -0400100 struct nfs4_label *olabel);
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
Trond Myklebust332d1a02021-03-25 18:15:36 -0400111static void nfs4_bitmask_set(__u32 bitmask[NFS4_BITMASK_SZ],
112 const __u32 *src, struct inode *inode,
113 struct nfs_server *server,
114 struct nfs4_label *label);
David Quigleyaa9c2662013-05-22 12:50:44 -0400115
116#ifdef CONFIG_NFS_V4_SECURITY_LABEL
117static inline struct nfs4_label *
118nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
119 struct iattr *sattr, struct nfs4_label *label)
120{
121 int err;
122
123 if (label == NULL)
124 return NULL;
125
126 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
127 return NULL;
128
David Quigleyaa9c2662013-05-22 12:50:44 -0400129 err = security_dentry_init_security(dentry, sattr->ia_mode,
130 &dentry->d_name, (void **)&label->label, &label->len);
131 if (err == 0)
132 return label;
133
134 return NULL;
135}
136static inline void
137nfs4_label_release_security(struct nfs4_label *label)
138{
139 if (label)
140 security_release_secctx(label->label, label->len);
141}
142static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
143{
144 if (label)
145 return server->attr_bitmask;
146
147 return server->attr_bitmask_nl;
148}
149#else
150static inline struct nfs4_label *
151nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
152 struct iattr *sattr, struct nfs4_label *l)
153{ return NULL; }
154static inline void
155nfs4_label_release_security(struct nfs4_label *label)
156{ return; }
157static inline u32 *
158nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
159{ return server->attr_bitmask; }
160#endif
161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -0500163static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{
Trond Myklebust52567b02009-10-23 14:46:42 -0400165 if (err >= -1000)
166 return err;
167 switch (err) {
168 case -NFS4ERR_RESOURCE:
Weston Andros Adamson30005122013-02-28 20:30:10 -0500169 case -NFS4ERR_LAYOUTTRYLATER:
170 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebust52567b02009-10-23 14:46:42 -0400171 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000172 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson88975382013-08-13 16:37:38 -0400173 case -NFS4ERR_WRONG_CRED:
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000174 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800175 case -NFS4ERR_BADOWNER:
176 case -NFS4ERR_BADNAME:
177 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400178 case -NFS4ERR_SHARE_DENIED:
179 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400180 case -NFS4ERR_MINOR_VERS_MISMATCH:
181 return -EPROTONOSUPPORT;
Trond Myklebust6e3cf242013-03-23 15:22:45 -0400182 case -NFS4ERR_FILE_OPEN:
183 return -EBUSY;
Trond Myklebust9fff59e2020-11-02 20:11:32 -0500184 case -NFS4ERR_NOT_SAME:
185 return -ENOTSYNC;
Trond Myklebust52567b02009-10-23 14:46:42 -0400186 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700188 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400189 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400191 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192}
193
194/*
195 * This is our standard bitmap for GETATTR requests.
196 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400197const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 FATTR4_WORD0_TYPE
199 | FATTR4_WORD0_CHANGE
200 | FATTR4_WORD0_SIZE
201 | FATTR4_WORD0_FSID
202 | FATTR4_WORD0_FILEID,
203 FATTR4_WORD1_MODE
204 | FATTR4_WORD1_NUMLINKS
205 | FATTR4_WORD1_OWNER
206 | FATTR4_WORD1_OWNER_GROUP
207 | FATTR4_WORD1_RAWDEV
208 | FATTR4_WORD1_SPACE_USED
209 | FATTR4_WORD1_TIME_ACCESS
210 | FATTR4_WORD1_TIME_METADATA
Anna Schumakerea96d1e2015-04-03 14:35:59 -0400211 | FATTR4_WORD1_TIME_MODIFY
212 | FATTR4_WORD1_MOUNTED_ON_FILEID,
David Quigleyaa9c2662013-05-22 12:50:44 -0400213#ifdef CONFIG_NFS_V4_SECURITY_LABEL
214 FATTR4_WORD2_SECURITY_LABEL
215#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216};
217
Trond Myklebust1549210f2012-06-05 09:16:47 -0400218static const u32 nfs4_pnfs_open_bitmap[3] = {
219 FATTR4_WORD0_TYPE
220 | FATTR4_WORD0_CHANGE
221 | FATTR4_WORD0_SIZE
222 | FATTR4_WORD0_FSID
223 | FATTR4_WORD0_FILEID,
224 FATTR4_WORD1_MODE
225 | FATTR4_WORD1_NUMLINKS
226 | FATTR4_WORD1_OWNER
227 | FATTR4_WORD1_OWNER_GROUP
228 | FATTR4_WORD1_RAWDEV
229 | FATTR4_WORD1_SPACE_USED
230 | FATTR4_WORD1_TIME_ACCESS
231 | FATTR4_WORD1_TIME_METADATA
232 | FATTR4_WORD1_TIME_MODIFY,
233 FATTR4_WORD2_MDSTHRESHOLD
Trond Myklebust95864c92015-12-26 15:06:03 -0500234#ifdef CONFIG_NFS_V4_SECURITY_LABEL
235 | FATTR4_WORD2_SECURITY_LABEL
236#endif
Trond Myklebust1549210f2012-06-05 09:16:47 -0400237};
238
Andy Adamsone23008e2012-10-02 21:07:32 -0400239static const u32 nfs4_open_noattr_bitmap[3] = {
240 FATTR4_WORD0_TYPE
Andy Adamsone23008e2012-10-02 21:07:32 -0400241 | FATTR4_WORD0_FILEID,
242};
243
David Quigleya09df2c2013-05-22 12:50:41 -0400244const u32 nfs4_statfs_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 FATTR4_WORD0_FILES_AVAIL
246 | FATTR4_WORD0_FILES_FREE
247 | FATTR4_WORD0_FILES_TOTAL,
248 FATTR4_WORD1_SPACE_AVAIL
249 | FATTR4_WORD1_SPACE_FREE
250 | FATTR4_WORD1_SPACE_TOTAL
251};
252
David Quigleya09df2c2013-05-22 12:50:41 -0400253const u32 nfs4_pathconf_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 FATTR4_WORD0_MAXLINK
255 | FATTR4_WORD0_MAXNAME,
256 0
257};
258
Fred Isamandae100c2011-07-30 20:52:37 -0400259const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 | FATTR4_WORD0_MAXREAD
261 | FATTR4_WORD0_MAXWRITE
262 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700263 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400264 | FATTR4_WORD1_FS_LAYOUT_TYPES,
265 FATTR4_WORD2_LAYOUT_BLKSIZE
Peng Tao2a92ee92015-09-26 02:24:37 +0800266 | FATTR4_WORD2_CLONE_BLKSIZE
Trond Myklebust7f08a332021-03-26 09:50:19 -0400267 | FATTR4_WORD2_CHANGE_ATTR_TYPE
Frank van der Lindenb78ef842020-06-23 22:38:55 +0000268 | FATTR4_WORD2_XATTR_SUPPORT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269};
270
David Quigleya09df2c2013-05-22 12:50:41 -0400271const u32 nfs4_fs_locations_bitmap[3] = {
Chuck Leverc05cefc2017-11-05 15:45:22 -0500272 FATTR4_WORD0_CHANGE
Manoj Naik830b8e32006-06-09 09:34:25 -0400273 | FATTR4_WORD0_SIZE
274 | FATTR4_WORD0_FSID
275 | FATTR4_WORD0_FILEID
276 | FATTR4_WORD0_FS_LOCATIONS,
Chuck Leverc05cefc2017-11-05 15:45:22 -0500277 FATTR4_WORD1_OWNER
Manoj Naik830b8e32006-06-09 09:34:25 -0400278 | FATTR4_WORD1_OWNER_GROUP
279 | FATTR4_WORD1_RAWDEV
280 | FATTR4_WORD1_SPACE_USED
281 | FATTR4_WORD1_TIME_ACCESS
282 | FATTR4_WORD1_TIME_METADATA
283 | FATTR4_WORD1_TIME_MODIFY
David Quigleya09df2c2013-05-22 12:50:41 -0400284 | FATTR4_WORD1_MOUNTED_ON_FILEID,
Manoj Naik830b8e32006-06-09 09:34:25 -0400285};
286
Trond Myklebust30846df2018-04-07 13:44:28 -0400287static void nfs4_bitmap_copy_adjust(__u32 *dst, const __u32 *src,
Trond Myklebusta71029b2021-04-10 00:23:03 -0400288 struct inode *inode, unsigned long flags)
Trond Myklebust30846df2018-04-07 13:44:28 -0400289{
290 unsigned long cache_validity;
291
292 memcpy(dst, src, NFS4_BITMASK_SZ*sizeof(*dst));
293 if (!inode || !nfs4_have_delegation(inode, FMODE_READ))
294 return;
295
Trond Myklebusta71029b2021-04-10 00:23:03 -0400296 cache_validity = READ_ONCE(NFS_I(inode)->cache_validity) | flags;
Trond Myklebust30846df2018-04-07 13:44:28 -0400297
Trond Myklebusta71029b2021-04-10 00:23:03 -0400298 /* Remove the attributes over which we have full control */
299 dst[1] &= ~FATTR4_WORD1_RAWDEV;
Trond Myklebust30846df2018-04-07 13:44:28 -0400300 if (!(cache_validity & NFS_INO_INVALID_SIZE))
301 dst[0] &= ~FATTR4_WORD0_SIZE;
302
303 if (!(cache_validity & NFS_INO_INVALID_CHANGE))
304 dst[0] &= ~FATTR4_WORD0_CHANGE;
Trond Myklebust30846df2018-04-07 13:44:28 -0400305
Trond Myklebust720869e2021-04-13 09:41:16 -0400306 if (!(cache_validity & NFS_INO_INVALID_MODE))
307 dst[1] &= ~FATTR4_WORD1_MODE;
Trond Myklebusta71029b2021-04-10 00:23:03 -0400308 if (!(cache_validity & NFS_INO_INVALID_OTHER))
Trond Myklebust720869e2021-04-13 09:41:16 -0400309 dst[1] &= ~(FATTR4_WORD1_OWNER | FATTR4_WORD1_OWNER_GROUP);
Trond Myklebust30846df2018-04-07 13:44:28 -0400310}
311
Al Virobc4785c2006-10-19 23:28:51 -0700312static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 struct nfs4_readdir_arg *readdir)
314{
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400315 unsigned int attrs = FATTR4_WORD0_FILEID | FATTR4_WORD0_TYPE;
Al Viro0dbb4c62006-10-19 23:28:49 -0700316 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000319 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
321 return;
322 }
323
324 readdir->cookie = 0;
325 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
326 if (cookie == 2)
327 return;
328
329 /*
330 * NFSv4 servers do not return entries for '.' and '..'
331 * Therefore, we fake these entries here. We let '.'
332 * have cookie 0 and '..' have cookie 1. Note that
333 * when talking to the server, we always send cookie 0
334 * instead of 1 or 2.
335 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800336 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
338 if (cookie == 0) {
339 *p++ = xdr_one; /* next */
340 *p++ = xdr_zero; /* cookie, first word */
341 *p++ = xdr_one; /* cookie, second word */
342 *p++ = xdr_one; /* entry len */
343 memcpy(p, ".\0\0\0", 4); /* entry */
344 p++;
345 *p++ = xdr_one; /* bitmap length */
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400346 *p++ = htonl(attrs); /* bitmap */
347 *p++ = htonl(12); /* attribute buffer length */
348 *p++ = htonl(NF4DIR);
David Howells2b0143b2015-03-17 22:25:59 +0000349 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 }
351
352 *p++ = xdr_one; /* next */
353 *p++ = xdr_zero; /* cookie, first word */
354 *p++ = xdr_two; /* cookie, second word */
355 *p++ = xdr_two; /* entry len */
356 memcpy(p, "..\0\0", 4); /* entry */
357 p++;
358 *p++ = xdr_one; /* bitmap length */
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400359 *p++ = htonl(attrs); /* bitmap */
360 *p++ = htonl(12); /* attribute buffer length */
361 *p++ = htonl(NF4DIR);
David Howells2b0143b2015-03-17 22:25:59 +0000362 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
364 readdir->pgbase = (char *)p - (char *)start;
365 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800366 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367}
368
Trond Myklebust26d36302016-09-22 13:39:05 -0400369static void nfs4_test_and_free_stateid(struct nfs_server *server,
370 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +1100371 const struct cred *cred)
Trond Myklebust26d36302016-09-22 13:39:05 -0400372{
373 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
374
375 ops->test_and_free_expired(server, stateid, cred);
376}
377
378static void __nfs4_free_revoked_stateid(struct nfs_server *server,
379 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +1100380 const struct cred *cred)
Trond Myklebust26d36302016-09-22 13:39:05 -0400381{
382 stateid->type = NFS4_REVOKED_STATEID_TYPE;
383 nfs4_test_and_free_stateid(server, stateid, cred);
384}
385
386static void nfs4_free_revoked_stateid(struct nfs_server *server,
387 const nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +1100388 const struct cred *cred)
Trond Myklebust26d36302016-09-22 13:39:05 -0400389{
390 nfs4_stateid tmp;
391
392 nfs4_stateid_copy(&tmp, stateid);
393 __nfs4_free_revoked_stateid(server, &tmp, cred);
394}
395
NeilBrown8478eaa2014-09-18 16:09:27 +1000396static long nfs4_update_delay(long *timeout)
397{
398 long ret;
399 if (!timeout)
400 return NFS4_POLL_RETRY_MAX;
401 if (*timeout <= 0)
402 *timeout = NFS4_POLL_RETRY_MIN;
403 if (*timeout > NFS4_POLL_RETRY_MAX)
404 *timeout = NFS4_POLL_RETRY_MAX;
405 ret = *timeout;
406 *timeout <<= 1;
407 return ret;
408}
409
Trond Myklebust0688e642019-04-07 13:59:09 -0400410static int nfs4_delay_killable(long *timeout)
Trond Myklebust65de8722008-12-23 15:21:44 -0500411{
Trond Myklebust65de8722008-12-23 15:21:44 -0500412 might_sleep();
413
NeilBrown8478eaa2014-09-18 16:09:27 +1000414 freezable_schedule_timeout_killable_unsafe(
415 nfs4_update_delay(timeout));
Trond Myklebust0688e642019-04-07 13:59:09 -0400416 if (!__fatal_signal_pending(current))
417 return 0;
418 return -EINTR;
419}
420
421static int nfs4_delay_interruptible(long *timeout)
422{
423 might_sleep();
424
He Zhe59679d92020-07-06 17:52:24 +0800425 freezable_schedule_timeout_interruptible_unsafe(nfs4_update_delay(timeout));
Trond Myklebust0688e642019-04-07 13:59:09 -0400426 if (!signal_pending(current))
427 return 0;
428 return __fatal_signal_pending(current) ? -EINTR :-ERESTARTSYS;
429}
430
431static int nfs4_delay(long *timeout, bool interruptible)
432{
433 if (interruptible)
434 return nfs4_delay_interruptible(timeout);
435 return nfs4_delay_killable(timeout);
Trond Myklebust65de8722008-12-23 15:21:44 -0500436}
437
Trond Myklebust50c80002019-07-11 19:02:18 -0400438static const nfs4_stateid *
439nfs4_recoverable_stateid(const nfs4_stateid *stateid)
440{
441 if (!stateid)
442 return NULL;
443 switch (stateid->type) {
444 case NFS4_OPEN_STATEID_TYPE:
445 case NFS4_LOCK_STATEID_TYPE:
446 case NFS4_DELEGATION_STATEID_TYPE:
447 return stateid;
448 default:
449 break;
450 }
451 return NULL;
452}
453
Trond Myklebust65de8722008-12-23 15:21:44 -0500454/* This is the error handling routine for processes that are allowed
455 * to sleep.
456 */
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400457static int nfs4_do_handle_exception(struct nfs_server *server,
458 int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500459{
460 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500461 struct nfs4_state *state = exception->state;
Trond Myklebust50c80002019-07-11 19:02:18 -0400462 const nfs4_stateid *stateid;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500463 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500464 int ret = errorcode;
465
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400466 exception->delay = 0;
467 exception->recovering = 0;
Trond Myklebust65de8722008-12-23 15:21:44 -0500468 exception->retry = 0;
Trond Myklebust272289a2016-09-22 13:39:15 -0400469
Trond Myklebust50c80002019-07-11 19:02:18 -0400470 stateid = nfs4_recoverable_stateid(exception->stateid);
Trond Myklebust272289a2016-09-22 13:39:15 -0400471 if (stateid == NULL && state != NULL)
Trond Myklebust50c80002019-07-11 19:02:18 -0400472 stateid = nfs4_recoverable_stateid(&state->stateid);
Trond Myklebust272289a2016-09-22 13:39:15 -0400473
Trond Myklebust65de8722008-12-23 15:21:44 -0500474 switch(errorcode) {
475 case 0:
476 return 0;
Trond Myklebustcf61eb22018-05-29 22:06:08 -0400477 case -NFS4ERR_BADHANDLE:
478 case -ESTALE:
479 if (inode != NULL && S_ISREG(inode->i_mode))
480 pnfs_destroy_layout(NFS_I(inode));
481 break;
Trond Myklebust5ba12442015-06-16 11:26:35 -0400482 case -NFS4ERR_DELEG_REVOKED:
483 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust272289a2016-09-22 13:39:15 -0400484 case -NFS4ERR_EXPIRED:
Trond Myklebust5ba12442015-06-16 11:26:35 -0400485 case -NFS4ERR_BAD_STATEID:
Olga Kornievskaiafefa1a82019-06-14 14:22:12 -0400486 case -NFS4ERR_PARTNER_NO_AUTH:
Trond Myklebust272289a2016-09-22 13:39:15 -0400487 if (inode != NULL && stateid != NULL) {
488 nfs_inode_find_state_and_recover(inode,
489 stateid);
490 goto wait_on_recovery;
491 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500492 fallthrough;
Trond Myklebust272289a2016-09-22 13:39:15 -0400493 case -NFS4ERR_OPENMODE:
Trond Myklebust8487c472016-06-26 08:44:35 -0400494 if (inode) {
495 int err;
496
497 err = nfs_async_inode_return_delegation(inode,
498 stateid);
499 if (err == 0)
500 goto wait_on_recovery;
501 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
502 exception->retry = 1;
503 break;
504 }
505 }
Trond Myklebust3114ea72012-03-07 16:39:06 -0500506 if (state == NULL)
507 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400508 ret = nfs4_schedule_stateid_recovery(server, state);
509 if (ret < 0)
510 break;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500511 goto wait_on_recovery;
Trond Myklebust65de8722008-12-23 15:21:44 -0500512 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500513 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500514 nfs4_schedule_lease_recovery(clp);
515 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -0400516 case -NFS4ERR_MOVED:
517 ret = nfs4_schedule_migration_recovery(server);
518 if (ret < 0)
519 break;
520 goto wait_on_recovery;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -0400521 case -NFS4ERR_LEASE_MOVED:
522 nfs4_schedule_lease_moved_recovery(clp);
523 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500524#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400525 case -NFS4ERR_BADSESSION:
526 case -NFS4ERR_BADSLOT:
527 case -NFS4ERR_BAD_HIGH_SLOT:
528 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
529 case -NFS4ERR_DEADSESSION:
530 case -NFS4ERR_SEQ_FALSE_RETRY:
531 case -NFS4ERR_SEQ_MISORDERED:
Trond Myklebust5c441542019-11-13 08:34:00 +0100532 /* Handled in nfs41_sequence_process() */
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400533 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500534#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500535 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500536 if (exception->timeout > HZ) {
537 /* We have retried a decent amount, time to
538 * fail
539 */
540 ret = -EBUSY;
541 break;
542 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500543 fallthrough;
Trond Myklebust65de8722008-12-23 15:21:44 -0500544 case -NFS4ERR_DELAY:
Trond Myklebust2598ed32015-09-20 16:10:18 -0400545 nfs_inc_server_stats(server, NFSIOS_DELAY);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500546 fallthrough;
Trond Myklebust2598ed32015-09-20 16:10:18 -0400547 case -NFS4ERR_GRACE:
Trond Myklebuste85d7ee2016-07-14 18:46:24 -0400548 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -0400549 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400550 exception->delay = 1;
551 return 0;
552
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400553 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500554 case -NFS4ERR_OLD_STATEID:
555 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800556 break;
557 case -NFS4ERR_BADOWNER:
558 /* The following works around a Linux server bug! */
559 case -NFS4ERR_BADNAME:
560 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
561 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
562 exception->retry = 1;
563 printk(KERN_WARNING "NFS: v4 server %s "
564 "does not accept raw "
565 "uid/gids. "
566 "Reenabling the idmapper.\n",
567 server->nfs_client->cl_hostname);
568 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500569 }
570 /* We failed to handle the error */
571 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500572wait_on_recovery:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400573 exception->recovering = 1;
574 return 0;
575}
576
577/* This is the error handling routine for processes that are allowed
578 * to sleep.
579 */
580int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
581{
582 struct nfs_client *clp = server->nfs_client;
583 int ret;
584
585 ret = nfs4_do_handle_exception(server, errorcode, exception);
586 if (exception->delay) {
Trond Myklebust0688e642019-04-07 13:59:09 -0400587 ret = nfs4_delay(&exception->timeout,
588 exception->interruptible);
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400589 goto out_retry;
590 }
591 if (exception->recovering) {
Trond Myklebustdfe1fe72021-06-01 11:10:05 -0400592 if (exception->task_is_privileged)
593 return -EDEADLOCK;
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400594 ret = nfs4_wait_clnt_recover(clp);
595 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
596 return -EIO;
597 goto out_retry;
598 }
599 return ret;
600out_retry:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500601 if (ret == 0)
602 exception->retry = 1;
603 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500604}
605
Trond Myklebust037fc982015-09-20 15:51:00 -0400606static int
607nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
608 int errorcode, struct nfs4_exception *exception)
609{
610 struct nfs_client *clp = server->nfs_client;
611 int ret;
612
613 ret = nfs4_do_handle_exception(server, errorcode, exception);
614 if (exception->delay) {
615 rpc_delay(task, nfs4_update_delay(&exception->timeout));
616 goto out_retry;
617 }
618 if (exception->recovering) {
Trond Myklebustdfe1fe72021-06-01 11:10:05 -0400619 if (exception->task_is_privileged)
620 return -EDEADLOCK;
Trond Myklebust037fc982015-09-20 15:51:00 -0400621 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
622 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
623 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
624 goto out_retry;
625 }
626 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
627 ret = -EIO;
628 return ret;
629out_retry:
Bill Baker0f90be12018-06-19 16:24:58 -0500630 if (ret == 0) {
Trond Myklebust037fc982015-09-20 15:51:00 -0400631 exception->retry = 1;
Bill Baker0f90be12018-06-19 16:24:58 -0500632 /*
633 * For NFS4ERR_MOVED, the client transport will need to
634 * be recomputed after migration recovery has completed.
635 */
636 if (errorcode == -NFS4ERR_MOVED)
637 rpc_task_release_transport(task);
638 }
Trond Myklebust037fc982015-09-20 15:51:00 -0400639 return ret;
640}
641
Olga Kornievskaia0f913a52018-07-09 15:13:33 -0400642int
Trond Myklebust037fc982015-09-20 15:51:00 -0400643nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
644 struct nfs4_state *state, long *timeout)
645{
646 struct nfs4_exception exception = {
647 .state = state,
648 };
649
650 if (task->tk_status >= 0)
651 return 0;
652 if (timeout)
653 exception.timeout = *timeout;
654 task->tk_status = nfs4_async_handle_exception(task, server,
655 task->tk_status,
656 &exception);
657 if (exception.delay && timeout)
658 *timeout = exception.timeout;
659 if (exception.retry)
660 return -EAGAIN;
661 return 0;
662}
663
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400664/*
665 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
666 * or 'false' otherwise.
667 */
668static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
669{
670 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
Anna Schumakereeea5362017-01-11 16:01:21 -0500671 return (flavor == RPC_AUTH_GSS_KRB5I) || (flavor == RPC_AUTH_GSS_KRB5P);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400672}
Trond Myklebust65de8722008-12-23 15:21:44 -0500673
Trond Myklebust452e9352010-07-31 14:29:06 -0400674static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 spin_lock(&clp->cl_lock);
677 if (time_before(clp->cl_last_renewal,timestamp))
678 clp->cl_last_renewal = timestamp;
679 spin_unlock(&clp->cl_lock);
680}
681
Trond Myklebust452e9352010-07-31 14:29:06 -0400682static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
683{
Trond Myklebustbe824162015-07-05 14:50:46 -0400684 struct nfs_client *clp = server->nfs_client;
685
686 if (!nfs4_has_session(clp))
687 do_renew_lease(clp, timestamp);
Trond Myklebust452e9352010-07-31 14:29:06 -0400688}
689
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400690struct nfs4_call_sync_data {
691 const struct nfs_server *seq_server;
692 struct nfs4_sequence_args *seq_args;
693 struct nfs4_sequence_res *seq_res;
694};
695
Trond Myklebustbe3a5d22015-06-23 19:51:55 +0800696void nfs4_init_sequence(struct nfs4_sequence_args *args,
Anna Schumakerfba83f32018-05-04 16:22:50 -0400697 struct nfs4_sequence_res *res, int cache_reply,
698 int privileged)
Chuck Levera9c92d62013-08-09 12:48:18 -0400699{
700 args->sa_slot = NULL;
701 args->sa_cache_this = cache_reply;
Anna Schumakerfba83f32018-05-04 16:22:50 -0400702 args->sa_privileged = privileged;
Chuck Levera9c92d62013-08-09 12:48:18 -0400703
704 res->sr_slot = NULL;
705}
706
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400707static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
Chuck Lever3bd23842013-08-09 12:49:19 -0400708{
709 struct nfs4_slot *slot = res->sr_slot;
710 struct nfs4_slot_table *tbl;
711
Chuck Lever3bd23842013-08-09 12:49:19 -0400712 tbl = slot->table;
713 spin_lock(&tbl->slot_tbl_lock);
714 if (!nfs41_wake_and_assign_slot(tbl, slot))
715 nfs4_free_slot(tbl, slot);
716 spin_unlock(&tbl->slot_tbl_lock);
717
718 res->sr_slot = NULL;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400719}
720
721static int nfs40_sequence_done(struct rpc_task *task,
722 struct nfs4_sequence_res *res)
723{
724 if (res->sr_slot != NULL)
725 nfs40_sequence_free_slot(res);
Chuck Lever3bd23842013-08-09 12:49:19 -0400726 return 1;
727}
728
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400729#if defined(CONFIG_NFS_V4_1)
730
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400731static void nfs41_release_slot(struct nfs4_slot *slot)
Andy Adamson13615872009-04-01 09:22:17 -0400732{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500733 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400734 struct nfs4_slot_table *tbl;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500735 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400736
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400737 if (!slot)
738 return;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500739 tbl = slot->table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500740 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500741
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400742 /* Bump the slot sequence number */
743 if (slot->seq_done)
744 slot->seq_nr++;
745 slot->seq_done = 0;
746
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500747 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500748 /* Be nice to the server: try to ensure that the last transmitted
749 * value for highest_user_slotid <= target_highest_slotid
750 */
751 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
752 send_new_highest_used_slotid = true;
753
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500754 if (nfs41_wake_and_assign_slot(tbl, slot)) {
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500755 send_new_highest_used_slotid = false;
756 goto out_unlock;
757 }
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500758 nfs4_free_slot(tbl, slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500759
760 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
761 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500762out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500763 spin_unlock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500764 if (send_new_highest_used_slotid)
Anna Schumaker3f10a6a2015-07-13 14:01:31 -0400765 nfs41_notify_server(session->clp);
Trond Myklebust045d2a62016-08-28 13:25:43 -0400766 if (waitqueue_active(&tbl->slot_waitq))
767 wake_up_all(&tbl->slot_waitq);
Andy Adamson13615872009-04-01 09:22:17 -0400768}
769
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400770static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
771{
772 nfs41_release_slot(res->sr_slot);
773 res->sr_slot = NULL;
774}
775
Trond Myklebust3453d572018-06-20 17:53:34 -0400776static void nfs4_slot_sequence_record_sent(struct nfs4_slot *slot,
777 u32 seqnr)
778{
779 if ((s32)(seqnr - slot->seq_nr_highest_sent) > 0)
780 slot->seq_nr_highest_sent = seqnr;
781}
782static void nfs4_slot_sequence_acked(struct nfs4_slot *slot,
783 u32 seqnr)
784{
785 slot->seq_nr_highest_sent = seqnr;
786 slot->seq_nr_last_acked = seqnr;
787}
788
Anna Schumaker913fadc2020-07-08 10:33:40 -0400789static void nfs4_probe_sequence(struct nfs_client *client, const struct cred *cred,
790 struct nfs4_slot *slot)
791{
792 struct rpc_task *task = _nfs41_proc_sequence(client, cred, slot, true);
793 if (!IS_ERR(task))
794 rpc_put_task_async(task);
795}
796
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400797static int nfs41_sequence_process(struct rpc_task *task,
798 struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400799{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500800 struct nfs4_session *session;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500801 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400802 struct nfs_client *clp;
Trond Myklebust5c441542019-11-13 08:34:00 +0100803 int status;
Trond Myklebust85563072012-12-11 10:31:12 -0500804 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400805
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500806 if (slot == NULL)
807 goto out_noaction;
Bryan Schumaker468f8612011-04-18 15:57:32 -0400808 /* don't increment the sequence number if the task wasn't sent */
Trond Myklebustc71c46f2019-03-01 11:40:05 -0500809 if (!RPC_WAS_SENT(task) || slot->seq_done)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400810 goto out;
811
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500812 session = slot->table->session;
Anna Schumaker913fadc2020-07-08 10:33:40 -0400813 clp = session->clp;
Trond Myklebust933602e2012-11-16 12:12:38 -0500814
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400815 trace_nfs4_sequence_done(session, res);
Trond Myklebust5c441542019-11-13 08:34:00 +0100816
817 status = res->sr_status;
818 if (task->tk_status == -NFS4ERR_DEADSESSION)
819 status = -NFS4ERR_DEADSESSION;
820
Andy Adamson691daf32009-12-04 15:55:39 -0500821 /* Check the SEQUENCE operation status */
Trond Myklebust5c441542019-11-13 08:34:00 +0100822 switch (status) {
Trond Myklebust14516c32010-07-31 14:29:06 -0400823 case 0:
Trond Myklebust3453d572018-06-20 17:53:34 -0400824 /* Mark this sequence number as having been acked */
825 nfs4_slot_sequence_acked(slot, slot->seq_nr);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400826 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400827 slot->seq_done = 1;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500828 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500829 /* Check sequence flags */
Trond Myklebust0a014a42016-09-22 13:38:51 -0400830 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
831 !!slot->privileged);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500832 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400833 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500834 case 1:
835 /*
836 * sr_status remains 1 if an RPC level error occurred.
837 * The server may or may not have processed the sequence
838 * operation..
Trond Myklebustac20d162012-12-15 15:36:07 -0500839 */
Trond Myklebust3453d572018-06-20 17:53:34 -0400840 nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
841 slot->seq_done = 1;
Trond Myklebustac20d162012-12-15 15:36:07 -0500842 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400843 case -NFS4ERR_DELAY:
844 /* The server detected a resend of the RPC call and
845 * returned NFS4ERR_DELAY as per Section 2.10.6.2
846 * of RFC5661.
847 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500848 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400849 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500850 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500851 slot->seq_nr);
Trond Myklebust3453d572018-06-20 17:53:34 -0400852 nfs4_slot_sequence_acked(slot, slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400853 goto out_retry;
Trond Myklebustf9312a52018-06-09 19:10:31 -0400854 case -NFS4ERR_RETRY_UNCACHED_REP:
855 case -NFS4ERR_SEQ_FALSE_RETRY:
856 /*
857 * The server thinks we tried to replay a request.
858 * Retry the call after bumping the sequence ID.
859 */
Trond Myklebust3453d572018-06-20 17:53:34 -0400860 nfs4_slot_sequence_acked(slot, slot->seq_nr);
Trond Myklebustf9312a52018-06-09 19:10:31 -0400861 goto retry_new_seq;
Trond Myklebust85563072012-12-11 10:31:12 -0500862 case -NFS4ERR_BADSLOT:
863 /*
864 * The slot id we used was probably retired. Try again
865 * using a different slot id.
866 */
Trond Myklebust99589102018-06-09 12:50:50 -0400867 if (slot->slot_nr < slot->table->target_highest_slotid)
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400868 goto session_recover;
Trond Myklebuste8794442012-12-15 13:56:18 -0500869 goto retry_nowait;
870 case -NFS4ERR_SEQ_MISORDERED:
Trond Myklebust3453d572018-06-20 17:53:34 -0400871 nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
Trond Myklebuste8794442012-12-15 13:56:18 -0500872 /*
Trond Myklebust3453d572018-06-20 17:53:34 -0400873 * Were one or more calls using this slot interrupted?
874 * If the server never received the request, then our
Anna Schumaker913fadc2020-07-08 10:33:40 -0400875 * transmitted slot sequence number may be too high. However,
876 * if the server did receive the request then it might
877 * accidentally give us a reply with a mismatched operation.
878 * We can sort this out by sending a lone sequence operation
879 * to the server on the same slot.
Trond Myklebustac20d162012-12-15 15:36:07 -0500880 */
Trond Myklebust3453d572018-06-20 17:53:34 -0400881 if ((s32)(slot->seq_nr - slot->seq_nr_last_acked) > 1) {
882 slot->seq_nr--;
Anna Schumaker913fadc2020-07-08 10:33:40 -0400883 if (task->tk_msg.rpc_proc != &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE]) {
884 nfs4_probe_sequence(clp, task->tk_msg.rpc_cred, slot);
885 res->sr_slot = NULL;
886 }
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500887 goto retry_nowait;
888 }
Trond Myklebust3453d572018-06-20 17:53:34 -0400889 /*
890 * RFC5661:
891 * A retry might be sent while the original request is
892 * still in progress on the replier. The replier SHOULD
893 * deal with the issue by returning NFS4ERR_DELAY as the
894 * reply to SEQUENCE or CB_SEQUENCE operation, but
895 * implementations MAY return NFS4ERR_SEQ_MISORDERED.
896 *
897 * Restart the search after a delay.
898 */
899 slot->seq_nr = slot->seq_nr_highest_sent;
900 goto out_retry;
Trond Myklebust5c441542019-11-13 08:34:00 +0100901 case -NFS4ERR_BADSESSION:
902 case -NFS4ERR_DEADSESSION:
903 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
904 goto session_recover;
Trond Myklebust14516c32010-07-31 14:29:06 -0400905 default:
906 /* Just update the slot sequence no. */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400907 slot->seq_done = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400908 }
909out:
910 /* The session may be reset by one of the error handlers. */
911 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500912out_noaction:
Trond Myklebust85563072012-12-11 10:31:12 -0500913 return ret;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400914session_recover:
Trond Myklebust5c441542019-11-13 08:34:00 +0100915 nfs4_schedule_session_recovery(session, status);
916 dprintk("%s ERROR: %d Reset session\n", __func__, status);
917 nfs41_sequence_free_slot(res);
918 goto out;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400919retry_new_seq:
920 ++slot->seq_nr;
Trond Myklebuste8794442012-12-15 13:56:18 -0500921retry_nowait:
922 if (rpc_restart_call_prepare(task)) {
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400923 nfs41_sequence_free_slot(res);
Trond Myklebuste8794442012-12-15 13:56:18 -0500924 task->tk_status = 0;
925 ret = 0;
926 }
927 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400928out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400929 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400930 goto out;
931 rpc_delay(task, NFS4_POLL_RETRY_MAX);
932 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400933}
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400934
935int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
936{
937 if (!nfs41_sequence_process(task, res))
938 return 0;
939 if (res->sr_slot != NULL)
940 nfs41_sequence_free_slot(res);
941 return 1;
942
943}
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500944EXPORT_SYMBOL_GPL(nfs41_sequence_done);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400945
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400946static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
947{
948 if (res->sr_slot == NULL)
949 return 1;
950 if (res->sr_slot->table->session != NULL)
951 return nfs41_sequence_process(task, res);
952 return nfs40_sequence_done(task, res);
953}
954
955static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
956{
957 if (res->sr_slot != NULL) {
958 if (res->sr_slot->table->session != NULL)
959 nfs41_sequence_free_slot(res);
960 else
961 nfs40_sequence_free_slot(res);
962 }
963}
964
Peng Tao2c4b1312014-06-11 05:24:16 +0800965int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400966{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500967 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400968 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400969 if (!res->sr_slot->table->session)
970 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400971 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400972}
Peng Tao2c4b1312014-06-11 05:24:16 +0800973EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Trond Myklebustdf896452010-06-16 09:52:26 -0400974
Andy Adamsonce5039c2009-04-01 09:22:13 -0400975static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
976{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400977 struct nfs4_call_sync_data *data = calldata;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400978
Trond Myklebust035168ab2010-06-16 09:52:26 -0400979 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
980
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500981 nfs4_setup_sequence(data->seq_server->nfs_client,
982 data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400983}
984
Andy Adamson69ab40c2009-04-01 09:22:19 -0400985static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
986{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400987 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400988
Trond Myklebust14516c32010-07-31 14:29:06 -0400989 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400990}
991
Trond Myklebust17280172012-03-11 13:11:00 -0400992static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400993 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400994 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400995};
996
Chuck Lever3bd23842013-08-09 12:49:19 -0400997#else /* !CONFIG_NFS_V4_1 */
998
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400999static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
1000{
1001 return nfs40_sequence_done(task, res);
1002}
1003
1004static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
1005{
1006 if (res->sr_slot != NULL)
1007 nfs40_sequence_free_slot(res);
1008}
1009
Peng Tao2c4b1312014-06-11 05:24:16 +08001010int nfs4_sequence_done(struct rpc_task *task,
1011 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -04001012{
Chuck Lever3bd23842013-08-09 12:49:19 -04001013 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -04001014}
Peng Tao2c4b1312014-06-11 05:24:16 +08001015EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Chuck Lever3bd23842013-08-09 12:49:19 -04001016
1017#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001018
Trond Myklebustc1dffe02019-03-01 12:13:34 -05001019static void nfs41_sequence_res_init(struct nfs4_sequence_res *res)
1020{
1021 res->sr_timestamp = jiffies;
1022 res->sr_status_flags = 0;
1023 res->sr_status = 1;
1024}
1025
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04001026static
1027void nfs4_sequence_attach_slot(struct nfs4_sequence_args *args,
1028 struct nfs4_sequence_res *res,
1029 struct nfs4_slot *slot)
1030{
1031 if (!slot)
1032 return;
1033 slot->privileged = args->sa_privileged ? 1 : 0;
1034 args->sa_slot = slot;
1035
1036 res->sr_slot = slot;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04001037}
1038
1039int nfs4_setup_sequence(struct nfs_client *client,
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001040 struct nfs4_sequence_args *args,
1041 struct nfs4_sequence_res *res,
1042 struct rpc_task *task)
1043{
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001044 struct nfs4_session *session = nfs4_get_session(client);
Anna Schumaker76ee0352017-01-10 16:49:31 -05001045 struct nfs4_slot_table *tbl = client->cl_slot_tbl;
Anna Schumaker3d358082017-01-11 10:54:04 -05001046 struct nfs4_slot *slot;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001047
Anna Schumaker9dd9107f2017-01-10 12:01:46 -05001048 /* slot already allocated? */
1049 if (res->sr_slot != NULL)
1050 goto out_start;
1051
Trond Myklebust6b2e6852019-04-07 13:58:49 -04001052 if (session)
Anna Schumaker76ee0352017-01-10 16:49:31 -05001053 tbl = &session->fc_slot_table;
Anna Schumaker76ee0352017-01-10 16:49:31 -05001054
Trond Myklebust3453d572018-06-20 17:53:34 -04001055 spin_lock(&tbl->slot_tbl_lock);
1056 /* The state manager will wait until the slot table is empty */
1057 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
1058 goto out_sleep;
Anna Schumaker6994cdd2017-01-10 16:13:27 -05001059
Trond Myklebust3453d572018-06-20 17:53:34 -04001060 slot = nfs4_alloc_slot(tbl);
1061 if (IS_ERR(slot)) {
Trond Myklebust3453d572018-06-20 17:53:34 -04001062 if (slot == ERR_PTR(-ENOMEM))
Trond Myklebust6b2e6852019-04-07 13:58:49 -04001063 goto out_sleep_timeout;
Trond Myklebust3453d572018-06-20 17:53:34 -04001064 goto out_sleep;
Anna Schumaker3d358082017-01-11 10:54:04 -05001065 }
Trond Myklebust3453d572018-06-20 17:53:34 -04001066 spin_unlock(&tbl->slot_tbl_lock);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001067
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04001068 nfs4_sequence_attach_slot(args, res, slot);
Anna Schumaker3d358082017-01-11 10:54:04 -05001069
Anna Schumakerad05cc02017-01-11 13:37:06 -05001070 trace_nfs4_setup_sequence(session, args);
Anna Schumaker9dd9107f2017-01-10 12:01:46 -05001071out_start:
Trond Myklebustc1dffe02019-03-01 12:13:34 -05001072 nfs41_sequence_res_init(res);
Anna Schumaker9dd9107f2017-01-10 12:01:46 -05001073 rpc_call_start(task);
1074 return 0;
Trond Myklebust6b2e6852019-04-07 13:58:49 -04001075out_sleep_timeout:
1076 /* Try again in 1/4 second */
1077 if (args->sa_privileged)
1078 rpc_sleep_on_priority_timeout(&tbl->slot_tbl_waitq, task,
1079 jiffies + (HZ >> 2), RPC_PRIORITY_PRIVILEGED);
1080 else
1081 rpc_sleep_on_timeout(&tbl->slot_tbl_waitq, task,
1082 NULL, jiffies + (HZ >> 2));
1083 spin_unlock(&tbl->slot_tbl_lock);
1084 return -EAGAIN;
Anna Schumaker0dcee8b2017-01-10 16:29:54 -05001085out_sleep:
1086 if (args->sa_privileged)
1087 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
Trond Myklebust8357a9b2019-04-07 13:58:48 -04001088 RPC_PRIORITY_PRIVILEGED);
Anna Schumaker0dcee8b2017-01-10 16:29:54 -05001089 else
1090 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
1091 spin_unlock(&tbl->slot_tbl_lock);
1092 return -EAGAIN;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001093}
1094EXPORT_SYMBOL_GPL(nfs4_setup_sequence);
1095
Chuck Lever9915ea72013-08-09 12:48:27 -04001096static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
1097{
1098 struct nfs4_call_sync_data *data = calldata;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05001099 nfs4_setup_sequence(data->seq_server->nfs_client,
Chuck Lever9915ea72013-08-09 12:48:27 -04001100 data->seq_args, data->seq_res, task);
1101}
1102
1103static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
1104{
1105 struct nfs4_call_sync_data *data = calldata;
1106 nfs4_sequence_done(task, data->seq_res);
1107}
1108
1109static const struct rpc_call_ops nfs40_call_sync_ops = {
1110 .rpc_call_prepare = nfs40_call_sync_prepare,
1111 .rpc_call_done = nfs40_call_sync_done,
1112};
1113
Anna Schumaker48c05852019-08-14 15:27:00 -04001114static int nfs4_call_sync_custom(struct rpc_task_setup *task_setup)
1115{
1116 int ret;
1117 struct rpc_task *task;
1118
1119 task = rpc_run_task(task_setup);
1120 if (IS_ERR(task))
1121 return PTR_ERR(task);
1122
1123 ret = task->tk_status;
1124 rpc_put_task(task);
1125 return ret;
1126}
1127
Trond Myklebustc74dfe92020-01-06 15:39:37 -05001128static int nfs4_do_call_sync(struct rpc_clnt *clnt,
1129 struct nfs_server *server,
1130 struct rpc_message *msg,
1131 struct nfs4_sequence_args *args,
1132 struct nfs4_sequence_res *res,
1133 unsigned short task_flags)
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001134{
Chuck Lever9915ea72013-08-09 12:48:27 -04001135 struct nfs_client *clp = server->nfs_client;
1136 struct nfs4_call_sync_data data = {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001137 .seq_server = server,
Trond Myklebustad389da2007-06-05 12:30:00 -04001138 .seq_args = args,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001139 .seq_res = res,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001140 };
1141 struct rpc_task_setup task_setup = {
Trond Myklebustad389da2007-06-05 12:30:00 -04001142 .rpc_client = clnt,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001143 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -04001144 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebustc74dfe92020-01-06 15:39:37 -05001145 .callback_data = &data,
1146 .flags = task_flags,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001147 };
1148
Anna Schumaker48c05852019-08-14 15:27:00 -04001149 return nfs4_call_sync_custom(&task_setup);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001150}
1151
Trond Myklebustc74dfe92020-01-06 15:39:37 -05001152static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
1153 struct nfs_server *server,
1154 struct rpc_message *msg,
1155 struct nfs4_sequence_args *args,
1156 struct nfs4_sequence_res *res)
1157{
Olga Kornievskaia85e39fe2021-06-23 23:28:51 -04001158 unsigned short task_flags = 0;
1159
1160 if (server->nfs_client->cl_minorversion)
1161 task_flags = RPC_TASK_MOVEABLE;
1162 return nfs4_do_call_sync(clnt, server, msg, args, res, task_flags);
Trond Myklebustc74dfe92020-01-06 15:39:37 -05001163}
1164
1165
Bryan Schumaker7c513052011-03-24 17:12:24 +00001166int nfs4_call_sync(struct rpc_clnt *clnt,
1167 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001168 struct rpc_message *msg,
1169 struct nfs4_sequence_args *args,
1170 struct nfs4_sequence_res *res,
1171 int cache_reply)
1172{
Anna Schumakerfba83f32018-05-04 16:22:50 -04001173 nfs4_init_sequence(args, res, cache_reply, 0);
Chuck Lever9915ea72013-08-09 12:48:27 -04001174 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001175}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
Trond Myklebust3c591172018-07-31 15:54:10 -04001177static void
1178nfs4_inc_nlink_locked(struct inode *inode)
1179{
Trond Myklebust82eae5a2021-04-01 14:59:59 -04001180 nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE |
1181 NFS_INO_INVALID_CTIME |
1182 NFS_INO_INVALID_NLINK);
Trond Myklebust3c591172018-07-31 15:54:10 -04001183 inc_nlink(inode);
1184}
1185
1186static void
Trond Myklebust1301e422021-04-01 14:57:56 -04001187nfs4_inc_nlink(struct inode *inode)
1188{
1189 spin_lock(&inode->i_lock);
1190 nfs4_inc_nlink_locked(inode);
1191 spin_unlock(&inode->i_lock);
1192}
1193
1194static void
Trond Myklebust3c591172018-07-31 15:54:10 -04001195nfs4_dec_nlink_locked(struct inode *inode)
1196{
Trond Myklebust82eae5a2021-04-01 14:59:59 -04001197 nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE |
1198 NFS_INO_INVALID_CTIME |
1199 NFS_INO_INVALID_NLINK);
Trond Myklebust3c591172018-07-31 15:54:10 -04001200 drop_nlink(inode);
1201}
1202
1203static void
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001204nfs4_update_changeattr_locked(struct inode *inode,
1205 struct nfs4_change_info *cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04001206 unsigned long timestamp, unsigned long cache_validity)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207{
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001208 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust6f9be832021-03-26 11:01:19 -04001209 u64 change_attr = inode_peek_iversion_raw(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
Trond Myklebustb6f80a22021-03-08 14:42:55 -05001211 cache_validity |= NFS_INO_INVALID_CTIME | NFS_INO_INVALID_MTIME;
Trond Myklebust20cf7d42021-06-25 15:49:31 -04001212 if (S_ISDIR(inode->i_mode))
1213 cache_validity |= NFS_INO_INVALID_DATA;
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001214
Trond Myklebust6f9be832021-03-26 11:01:19 -04001215 switch (NFS_SERVER(inode)->change_attr_type) {
1216 case NFS4_CHANGE_TYPE_IS_UNDEFINED:
Trond Myklebust20cf7d42021-06-25 15:49:31 -04001217 if (cinfo->after == change_attr)
Trond Myklebust6f9be832021-03-26 11:01:19 -04001218 goto out;
1219 break;
1220 default:
1221 if ((s64)(change_attr - cinfo->after) >= 0)
1222 goto out;
1223 }
1224
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001225 inode_set_iversion_raw(inode, cinfo->after);
Trond Myklebust20cf7d42021-06-25 15:49:31 -04001226 if (!cinfo->atomic || cinfo->before != change_attr) {
1227 if (S_ISDIR(inode->i_mode))
1228 nfs_force_lookup_revalidate(inode);
1229
1230 if (!NFS_PROTO(inode)->have_delegation(inode, FMODE_READ))
1231 cache_validity |=
1232 NFS_INO_INVALID_ACCESS | NFS_INO_INVALID_ACL |
1233 NFS_INO_INVALID_SIZE | NFS_INO_INVALID_OTHER |
1234 NFS_INO_INVALID_BLOCKS | NFS_INO_INVALID_NLINK |
1235 NFS_INO_INVALID_MODE | NFS_INO_INVALID_XATTR |
1236 NFS_INO_REVAL_PAGECACHE;
1237 nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
1238 }
1239 nfsi->attrtimeo_timestamp = jiffies;
Trond Myklebustd3129ef2017-01-11 22:07:28 -05001240 nfsi->read_cache_jiffies = timestamp;
Trond Myklebust3235b402015-02-26 19:52:06 -05001241 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
Trond Myklebustc8d07152018-07-31 15:54:12 -04001242 nfsi->cache_validity &= ~NFS_INO_INVALID_CHANGE;
Trond Myklebust6f9be832021-03-26 11:01:19 -04001243out:
1244 nfs_set_cache_invalid(inode, cache_validity);
Trond Myklebust3c591172018-07-31 15:54:10 -04001245}
1246
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001247void
1248nfs4_update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04001249 unsigned long timestamp, unsigned long cache_validity)
Trond Myklebust3c591172018-07-31 15:54:10 -04001250{
1251 spin_lock(&dir->i_lock);
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001252 nfs4_update_changeattr_locked(dir, cinfo, timestamp, cache_validity);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 spin_unlock(&dir->i_lock);
1254}
1255
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001256struct nfs4_open_createattrs {
1257 struct nfs4_label *label;
1258 struct iattr *sattr;
1259 const __u32 verf[2];
1260};
1261
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001262static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1263 int err, struct nfs4_exception *exception)
1264{
1265 if (err != -EINVAL)
1266 return false;
1267 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1268 return false;
1269 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1270 exception->retry = 1;
1271 return true;
1272}
1273
Trond Myklebust1bf85d82019-06-27 06:30:48 -04001274static fmode_t _nfs4_ctx_to_accessmode(const struct nfs_open_context *ctx)
1275{
1276 return ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
1277}
1278
1279static fmode_t _nfs4_ctx_to_openmode(const struct nfs_open_context *ctx)
1280{
1281 fmode_t ret = ctx->mode & (FMODE_READ|FMODE_WRITE);
1282
1283 return (ctx->mode & FMODE_EXEC) ? FMODE_READ | ret : ret;
1284}
1285
Trond Myklebust6ae37332015-01-30 14:21:14 -05001286static u32
1287nfs4_map_atomic_open_share(struct nfs_server *server,
1288 fmode_t fmode, int openflags)
1289{
1290 u32 res = 0;
1291
1292 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1293 case FMODE_READ:
1294 res = NFS4_SHARE_ACCESS_READ;
1295 break;
1296 case FMODE_WRITE:
1297 res = NFS4_SHARE_ACCESS_WRITE;
1298 break;
1299 case FMODE_READ|FMODE_WRITE:
1300 res = NFS4_SHARE_ACCESS_BOTH;
1301 }
1302 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1303 goto out;
1304 /* Want no delegation if we're using O_DIRECT */
1305 if (openflags & O_DIRECT)
1306 res |= NFS4_SHARE_WANT_NO_DELEG;
1307out:
1308 return res;
1309}
1310
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001311static enum open_claim_type4
1312nfs4_map_atomic_open_claim(struct nfs_server *server,
1313 enum open_claim_type4 claim)
1314{
1315 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1316 return claim;
1317 switch (claim) {
1318 default:
1319 return claim;
1320 case NFS4_OPEN_CLAIM_FH:
1321 return NFS4_OPEN_CLAIM_NULL;
1322 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1323 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1324 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1325 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1326 }
1327}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
1329static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001330{
1331 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001332 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001333 p->o_res.seqid = p->o_arg.seqid;
1334 p->c_res.seqid = p->c_arg.seqid;
1335 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -04001336 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001337 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd12012-01-07 13:22:46 -05001338 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001339}
1340
Al Viro82a2c1b2011-06-22 18:30:55 -04001341static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001342 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001343 const struct nfs4_open_createattrs *c,
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001344 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001345 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001346{
Al Viro82a2c1b2011-06-22 18:30:55 -04001347 struct dentry *parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001348 struct inode *dir = d_inode(parent);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001349 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust63f5f792015-01-23 19:19:25 -05001350 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001351 struct nfs4_label *label = (c != NULL) ? c->label : NULL;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001352 struct nfs4_opendata *p;
1353
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001354 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001355 if (p == NULL)
1356 goto err;
David Quigley14c43f72013-05-22 12:50:43 -04001357
1358 p->f_label = nfs4_label_alloc(server, gfp_mask);
1359 if (IS_ERR(p->f_label))
1360 goto err_free_p;
1361
Kinglong Meea49c2692015-07-27 15:31:38 +08001362 p->a_label = nfs4_label_alloc(server, gfp_mask);
1363 if (IS_ERR(p->a_label))
1364 goto err_free_f;
1365
Trond Myklebust63f5f792015-01-23 19:19:25 -05001366 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1367 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05001368 if (IS_ERR(p->o_arg.seqid))
David Quigley14c43f72013-05-22 12:50:43 -04001369 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -04001370 nfs_sb_active(dentry->d_sb);
1371 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001372 p->dir = parent;
1373 p->owner = sp;
1374 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001375 p->o_arg.open_flags = flags;
1376 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Trond Myklebust536585c2016-11-10 15:40:34 -05001377 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
Trond Myklebust6ae37332015-01-30 14:21:14 -05001378 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1379 fmode, flags);
Benjamin Coddington90910512019-06-07 06:37:30 -04001380 if (flags & O_CREAT) {
1381 p->o_arg.umask = current_umask();
1382 p->o_arg.label = nfs4_label_copy(p->a_label, label);
1383 if (c->sattr != NULL && c->sattr->ia_valid != 0) {
1384 p->o_arg.u.attrs = &p->attrs;
1385 memcpy(&p->attrs, c->sattr, sizeof(p->attrs));
1386
1387 memcpy(p->o_arg.u.verifier.data, c->verf,
1388 sizeof(p->o_arg.u.verifier.data));
1389 }
1390 }
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001391 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1392 * will return permission denied for all bits until close */
1393 if (!(flags & O_EXCL)) {
1394 /* ask server to check for all possible rights as results
1395 * are cached */
Trond Myklebust536585c2016-11-10 15:40:34 -05001396 switch (p->o_arg.claim) {
1397 default:
1398 break;
1399 case NFS4_OPEN_CLAIM_NULL:
1400 case NFS4_OPEN_CLAIM_FH:
1401 p->o_arg.access = NFS4_ACCESS_READ |
1402 NFS4_ACCESS_MODIFY |
1403 NFS4_ACCESS_EXTEND |
1404 NFS4_ACCESS_EXECUTE;
Frank van der Linden72832a22020-06-23 22:38:58 +00001405#ifdef CONFIG_NFS_V4_2
1406 if (server->caps & NFS_CAP_XATTR)
1407 p->o_arg.access |= NFS4_ACCESS_XAREAD |
1408 NFS4_ACCESS_XAWRITE |
1409 NFS4_ACCESS_XALIST;
1410#endif
Trond Myklebust536585c2016-11-10 15:40:34 -05001411 }
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001412 }
David Howells7539bba2006-08-22 20:06:09 -04001413 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001414 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1415 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001416 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001417 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001418 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001419 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001420 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001421 case NFS4_OPEN_CLAIM_NULL:
1422 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1423 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1424 p->o_arg.fh = NFS_FH(dir);
1425 break;
1426 case NFS4_OPEN_CLAIM_PREVIOUS:
1427 case NFS4_OPEN_CLAIM_FH:
1428 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1429 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
David Howells2b0143b2015-03-17 22:25:59 +00001430 p->o_arg.fh = NFS_FH(d_inode(dentry));
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001431 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001432 p->c_arg.fh = &p->o_res.fh;
1433 p->c_arg.stateid = &p->o_res.stateid;
1434 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001435 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001436 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001437 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001438
1439err_free_label:
Kinglong Meea49c2692015-07-27 15:31:38 +08001440 nfs4_label_free(p->a_label);
1441err_free_f:
David Quigley14c43f72013-05-22 12:50:43 -04001442 nfs4_label_free(p->f_label);
1443err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001444 kfree(p);
1445err:
1446 dput(parent);
1447 return NULL;
1448}
1449
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001450static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001451{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001452 struct nfs4_opendata *p = container_of(kref,
1453 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001454 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001455
Fred Isaman30ae2412016-10-18 13:39:51 -04001456 nfs4_lgopen_release(p->lgp);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001457 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001458 nfs4_sequence_free_slot(&p->o_res.seq_res);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001459 if (p->state != NULL)
1460 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001461 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001462
Kinglong Meea49c2692015-07-27 15:31:38 +08001463 nfs4_label_free(p->a_label);
David Quigley14c43f72013-05-22 12:50:43 -04001464 nfs4_label_free(p->f_label);
1465
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001466 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001467 dput(p->dentry);
1468 nfs_sb_deactive(sb);
Trond Myklebust6926afd12012-01-07 13:22:46 -05001469 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001470 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001471 kfree(p);
1472}
1473
1474static void nfs4_opendata_put(struct nfs4_opendata *p)
1475{
1476 if (p != NULL)
1477 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001478}
1479
Trond Myklebust24311f82015-09-20 10:50:17 -04001480static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1481 fmode_t fmode)
1482{
1483 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1484 case FMODE_READ|FMODE_WRITE:
1485 return state->n_rdwr != 0;
1486 case FMODE_WRITE:
1487 return state->n_wronly != 0;
1488 case FMODE_READ:
1489 return state->n_rdonly != 0;
1490 }
1491 WARN_ON_ONCE(1);
1492 return false;
1493}
1494
Trond Myklebustbe189f72018-09-27 17:12:33 -04001495static int can_open_cached(struct nfs4_state *state, fmode_t mode,
1496 int open_mode, enum open_claim_type4 claim)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001497{
1498 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001499
Trond Myklebust536e43d2012-01-17 22:04:26 -05001500 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001501 goto out;
Trond Myklebustbe189f72018-09-27 17:12:33 -04001502 switch (claim) {
1503 case NFS4_OPEN_CLAIM_NULL:
1504 case NFS4_OPEN_CLAIM_FH:
1505 goto out;
1506 default:
1507 break;
1508 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001509 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001510 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001511 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1512 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001513 break;
1514 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001515 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1516 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001517 break;
1518 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001519 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1520 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001521 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001522out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001523 return ret;
1524}
1525
Trond Myklebust2a606182015-08-19 22:30:00 -05001526static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1527 enum open_claim_type4 claim)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001528{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001529 if (delegation == NULL)
1530 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001531 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001532 return 0;
Trond Myklebust2a606182015-08-19 22:30:00 -05001533 switch (claim) {
1534 case NFS4_OPEN_CLAIM_NULL:
1535 case NFS4_OPEN_CLAIM_FH:
1536 break;
1537 case NFS4_OPEN_CLAIM_PREVIOUS:
1538 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1539 break;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001540 fallthrough;
Trond Myklebust2a606182015-08-19 22:30:00 -05001541 default:
1542 return 0;
1543 }
Trond Myklebustb7391f42008-12-23 15:21:52 -05001544 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001545 return 1;
1546}
1547
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001548static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001549{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001550 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001551 case FMODE_WRITE:
1552 state->n_wronly++;
1553 break;
1554 case FMODE_READ:
1555 state->n_rdonly++;
1556 break;
1557 case FMODE_READ|FMODE_WRITE:
1558 state->n_rdwr++;
1559 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001560 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001561}
1562
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04001563#ifdef CONFIG_NFS_V4_1
1564static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1565{
1566 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1567 return true;
1568 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1569 return true;
1570 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1571 return true;
1572 return false;
1573}
1574#endif /* CONFIG_NFS_V4_1 */
1575
Trond Myklebustc9399f22017-11-06 15:28:01 -05001576static void nfs_state_log_update_open_stateid(struct nfs4_state *state)
1577{
1578 if (test_and_clear_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1579 wake_up_all(&state->waitq);
1580}
1581
Trond Myklebust4f14c192014-02-12 19:15:06 -05001582static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001583{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001584 struct nfs_client *clp = state->owner->so_server->nfs_client;
1585 bool need_recover = false;
1586
1587 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1588 need_recover = true;
1589 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1590 need_recover = true;
1591 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1592 need_recover = true;
1593 if (need_recover)
1594 nfs4_state_mark_reclaim_nograce(clp, state);
1595}
1596
Trond Myklebustc9399f22017-11-06 15:28:01 -05001597/*
1598 * Check for whether or not the caller may update the open stateid
1599 * to the value passed in by stateid.
1600 *
1601 * Note: This function relies heavily on the server implementing
1602 * RFC7530 Section 9.1.4.2, and RFC5661 Section 8.2.2
1603 * correctly.
1604 * i.e. The stateid seqids have to be initialised to 1, and
1605 * are then incremented on every state transition.
1606 */
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04001607static bool nfs_stateid_is_sequential(struct nfs4_state *state,
Trond Myklebustc9399f22017-11-06 15:28:01 -05001608 const nfs4_stateid *stateid)
Trond Myklebuste999e802014-02-10 18:20:47 -05001609{
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04001610 if (test_bit(NFS_OPEN_STATE, &state->flags)) {
1611 /* The common case - we're updating to a new sequence number */
1612 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1613 nfs4_stateid_is_next(&state->open_stateid, stateid)) {
1614 return true;
1615 }
1616 } else {
1617 /* This is the first OPEN in this generation */
Trond Myklebustc9399f22017-11-06 15:28:01 -05001618 if (stateid->seqid == cpu_to_be32(1))
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04001619 return true;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001620 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001621 return false;
1622}
1623
Trond Myklebustf95549c2015-01-23 18:06:09 -05001624static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1625{
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001626 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1627 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001628 if (state->n_wronly)
1629 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1630 if (state->n_rdonly)
1631 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1632 if (state->n_rdwr)
1633 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001634 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustf95549c2015-01-23 18:06:09 -05001635}
1636
Trond Myklebust226056c2014-02-11 10:41:07 -05001637static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1638 nfs4_stateid *stateid, fmode_t fmode)
1639{
1640 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1641 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1642 case FMODE_WRITE:
1643 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1644 break;
1645 case FMODE_READ:
1646 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1647 break;
1648 case 0:
1649 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1650 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1651 clear_bit(NFS_OPEN_STATE, &state->flags);
1652 }
1653 if (stateid == NULL)
1654 return;
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001655 /* Handle OPEN+OPEN_DOWNGRADE races */
1656 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1657 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
Trond Myklebustf95549c2015-01-23 18:06:09 -05001658 nfs_resync_open_stateid_locked(state);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001659 goto out;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001660 }
Trond Myklebust003707c2007-07-05 18:07:55 -04001661 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001662 nfs4_stateid_copy(&state->stateid, stateid);
1663 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001664 trace_nfs4_open_stateid_update(state->inode, stateid, 0);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001665out:
1666 nfs_state_log_update_open_stateid(state);
Trond Myklebust226056c2014-02-11 10:41:07 -05001667}
1668
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001669static void nfs_clear_open_stateid(struct nfs4_state *state,
1670 nfs4_stateid *arg_stateid,
1671 nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust226056c2014-02-11 10:41:07 -05001672{
1673 write_seqlock(&state->seqlock);
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001674 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1675 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1676 nfs_clear_open_stateid_locked(state, stateid, fmode);
Trond Myklebust226056c2014-02-11 10:41:07 -05001677 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001678 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1679 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001680}
1681
Trond Myklebust1393d962016-09-22 13:39:13 -04001682static void nfs_set_open_stateid_locked(struct nfs4_state *state,
Trond Myklebustc9399f22017-11-06 15:28:01 -05001683 const nfs4_stateid *stateid, nfs4_stateid *freeme)
Trond Myklebuste9acf212019-01-22 14:01:16 -05001684 __must_hold(&state->owner->so_lock)
1685 __must_hold(&state->seqlock)
1686 __must_hold(RCU)
1687
Trond Myklebust003707c2007-07-05 18:07:55 -04001688{
Trond Myklebustc9399f22017-11-06 15:28:01 -05001689 DEFINE_WAIT(wait);
1690 int status = 0;
1691 for (;;) {
1692
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04001693 if (nfs_stateid_is_sequential(state, stateid))
Trond Myklebust003707c2007-07-05 18:07:55 -04001694 break;
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04001695
Trond Myklebustc9399f22017-11-06 15:28:01 -05001696 if (status)
Trond Myklebust003707c2007-07-05 18:07:55 -04001697 break;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001698 /* Rely on seqids for serialisation with NFSv4.0 */
1699 if (!nfs4_has_session(NFS_SERVER(state->inode)->nfs_client))
1700 break;
1701
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04001702 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001703 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
1704 /*
1705 * Ensure we process the state changes in the same order
1706 * in which the server processed them by delaying the
1707 * update of the stateid until we are in sequence.
1708 */
1709 write_sequnlock(&state->seqlock);
1710 spin_unlock(&state->owner->so_lock);
1711 rcu_read_unlock();
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001712 trace_nfs4_open_stateid_update_wait(state->inode, stateid, 0);
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04001713
zhouchuangaobb002382021-05-09 19:34:37 -07001714 if (!fatal_signal_pending(current)) {
Trond Myklebustc9399f22017-11-06 15:28:01 -05001715 if (schedule_timeout(5*HZ) == 0)
1716 status = -EAGAIN;
1717 else
1718 status = 0;
1719 } else
1720 status = -EINTR;
1721 finish_wait(&state->waitq, &wait);
1722 rcu_read_lock();
1723 spin_lock(&state->owner->so_lock);
1724 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001725 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001726
Trond Myklebuste1fff5d2017-11-07 13:10:46 -05001727 if (test_bit(NFS_OPEN_STATE, &state->flags) &&
1728 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
Trond Myklebustc9399f22017-11-06 15:28:01 -05001729 nfs4_stateid_copy(freeme, &state->open_stateid);
1730 nfs_test_and_clear_all_open_stateid(state);
1731 }
1732
Trond Myklebuste999e802014-02-10 18:20:47 -05001733 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1734 nfs4_stateid_copy(&state->stateid, stateid);
1735 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001736 trace_nfs4_open_stateid_update(state->inode, stateid, status);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001737 nfs_state_log_update_open_stateid(state);
Trond Myklebust003707c2007-07-05 18:07:55 -04001738}
1739
Trond Myklebustc9399f22017-11-06 15:28:01 -05001740static void nfs_state_set_open_stateid(struct nfs4_state *state,
Trond Myklebust1393d962016-09-22 13:39:13 -04001741 const nfs4_stateid *open_stateid,
Trond Myklebust1393d962016-09-22 13:39:13 -04001742 fmode_t fmode,
1743 nfs4_stateid *freeme)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001745 /*
1746 * Protect the call to nfs4_state_set_mode_locked and
1747 * serialise the stateid update
1748 */
1749 write_seqlock(&state->seqlock);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001750 nfs_set_open_stateid_locked(state, open_stateid, freeme);
1751 switch (fmode) {
1752 case FMODE_READ:
1753 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1754 break;
1755 case FMODE_WRITE:
1756 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1757 break;
1758 case FMODE_READ|FMODE_WRITE:
1759 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust003707c2007-07-05 18:07:55 -04001760 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001761 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001762 write_sequnlock(&state->seqlock);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001763}
1764
Trond Myklebust27a30cf2019-07-22 18:32:59 +01001765static void nfs_state_clear_open_state_flags(struct nfs4_state *state)
1766{
1767 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1768 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1769 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1770 clear_bit(NFS_OPEN_STATE, &state->flags);
1771}
1772
Trond Myklebustc9399f22017-11-06 15:28:01 -05001773static void nfs_state_set_delegation(struct nfs4_state *state,
1774 const nfs4_stateid *deleg_stateid,
1775 fmode_t fmode)
1776{
1777 /*
1778 * Protect the call to nfs4_state_set_mode_locked and
1779 * serialise the stateid update
1780 */
1781 write_seqlock(&state->seqlock);
1782 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1783 set_bit(NFS_DELEGATED_STATE, &state->flags);
1784 write_sequnlock(&state->seqlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785}
1786
Trond Myklebust9f0c5122018-09-05 14:07:15 -04001787static void nfs_state_clear_delegation(struct nfs4_state *state)
1788{
1789 write_seqlock(&state->seqlock);
1790 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1791 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1792 write_sequnlock(&state->seqlock);
1793}
1794
Olga Kornievskaiaec4b0922019-10-08 16:33:53 -04001795int update_open_stateid(struct nfs4_state *state,
Trond Myklebust1393d962016-09-22 13:39:13 -04001796 const nfs4_stateid *open_stateid,
1797 const nfs4_stateid *delegation,
1798 fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001799{
Trond Myklebust1393d962016-09-22 13:39:13 -04001800 struct nfs_server *server = NFS_SERVER(state->inode);
1801 struct nfs_client *clp = server->nfs_client;
Trond Myklebust34310432008-12-23 15:21:38 -05001802 struct nfs_inode *nfsi = NFS_I(state->inode);
1803 struct nfs_delegation *deleg_cur;
Arnd Bergmann83aa3e02016-10-18 17:21:30 +02001804 nfs4_stateid freeme = { };
Trond Myklebust34310432008-12-23 15:21:38 -05001805 int ret = 0;
1806
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001807 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001808
1809 rcu_read_lock();
Trond Myklebustc9399f22017-11-06 15:28:01 -05001810 spin_lock(&state->owner->so_lock);
1811 if (open_stateid != NULL) {
1812 nfs_state_set_open_stateid(state, open_stateid, fmode, &freeme);
1813 ret = 1;
1814 }
1815
Trond Myklebust333ac782019-10-22 12:12:17 -04001816 deleg_cur = nfs4_get_valid_delegation(state->inode);
Trond Myklebust34310432008-12-23 15:21:38 -05001817 if (deleg_cur == NULL)
1818 goto no_delegation;
1819
1820 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001821 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001822 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001823 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001824 goto no_delegation_unlock;
1825
1826 if (delegation == NULL)
1827 delegation = &deleg_cur->stateid;
Trond Myklebust333ac782019-10-22 12:12:17 -04001828 else if (!nfs4_stateid_match_other(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001829 goto no_delegation_unlock;
1830
Trond Myklebustb7391f42008-12-23 15:21:52 -05001831 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001832 nfs_state_set_delegation(state, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001833 ret = 1;
1834no_delegation_unlock:
1835 spin_unlock(&deleg_cur->lock);
1836no_delegation:
Trond Myklebustc9399f22017-11-06 15:28:01 -05001837 if (ret)
1838 update_open_stateflags(state, fmode);
1839 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001840 rcu_read_unlock();
1841
Trond Myklebust4f14c192014-02-12 19:15:06 -05001842 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
Trond Myklebust1393d962016-09-22 13:39:13 -04001843 nfs4_schedule_state_manager(clp);
1844 if (freeme.type != 0)
1845 nfs4_test_and_free_stateid(server, &freeme,
1846 state->owner->so_cred);
Trond Myklebust34310432008-12-23 15:21:38 -05001847
1848 return ret;
1849}
1850
Trond Myklebust39071e62015-01-24 15:07:56 -05001851static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1852 const nfs4_stateid *stateid)
1853{
1854 struct nfs4_state *state = lsp->ls_state;
1855 bool ret = false;
1856
1857 spin_lock(&state->state_lock);
1858 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1859 goto out_noupdate;
1860 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1861 goto out_noupdate;
1862 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1863 ret = true;
1864out_noupdate:
1865 spin_unlock(&state->state_lock);
1866 return ret;
1867}
Trond Myklebust34310432008-12-23 15:21:38 -05001868
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001869static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001870{
1871 struct nfs_delegation *delegation;
1872
Trond Myklebustf5086242018-03-20 16:43:13 -04001873 fmode &= FMODE_READ|FMODE_WRITE;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001874 rcu_read_lock();
Trond Myklebust40e6aa12019-10-27 13:38:45 -04001875 delegation = nfs4_get_valid_delegation(inode);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001876 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001877 rcu_read_unlock();
1878 return;
1879 }
1880 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001881 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001882}
1883
Trond Myklebust6ee41262007-07-08 14:11:36 -04001884static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001885{
1886 struct nfs4_state *state = opendata->state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001887 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001888 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001889 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebust2a606182015-08-19 22:30:00 -05001890 enum open_claim_type4 claim = opendata->o_arg.claim;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001891 nfs4_stateid stateid;
1892 int ret = -EAGAIN;
1893
Trond Myklebustaac00a82007-07-05 19:02:21 -04001894 for (;;) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001895 spin_lock(&state->owner->so_lock);
Trond Myklebustbe189f72018-09-27 17:12:33 -04001896 if (can_open_cached(state, fmode, open_mode, claim)) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001897 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001898 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef72014-09-03 14:15:40 -04001899 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001900 }
Anna Schumaker61beef72014-09-03 14:15:40 -04001901 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001902 rcu_read_lock();
Trond Myklebustbe3df3d2019-10-31 18:40:32 -04001903 delegation = nfs4_get_valid_delegation(state->inode);
Trond Myklebust2a606182015-08-19 22:30:00 -05001904 if (!can_open_delegated(delegation, fmode, claim)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001905 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001906 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001907 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001908 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001909 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001910 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001911 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001912 if (!opendata->is_recover) {
1913 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1914 if (ret != 0)
1915 goto out;
1916 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001917 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001918
1919 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001920 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001921 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001922 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001923out:
1924 return ERR_PTR(ret);
1925out_return_state:
Trond Myklebustace9fad2018-09-02 19:19:07 -04001926 refcount_inc(&state->count);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001927 return state;
1928}
1929
Andy Adamsone23008e2012-10-02 21:07:32 -04001930static void
1931nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1932{
1933 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1934 struct nfs_delegation *delegation;
1935 int delegation_flags = 0;
1936
1937 rcu_read_lock();
1938 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1939 if (delegation)
1940 delegation_flags = delegation->flags;
1941 rcu_read_unlock();
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001942 switch (data->o_arg.claim) {
1943 default:
1944 break;
1945 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1946 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001947 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1948 "returning a delegation for "
1949 "OPEN(CLAIM_DELEGATE_CUR)\n",
1950 clp->cl_hostname);
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001951 return;
1952 }
1953 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Andy Adamsone23008e2012-10-02 21:07:32 -04001954 nfs_inode_set_delegation(state->inode,
Trond Myklebust35156bf2018-03-20 17:03:13 -04001955 data->owner->so_cred,
1956 data->o_res.delegation_type,
1957 &data->o_res.delegation,
1958 data->o_res.pagemod_limit);
Andy Adamsone23008e2012-10-02 21:07:32 -04001959 else
1960 nfs_inode_reclaim_delegation(state->inode,
Trond Myklebust35156bf2018-03-20 17:03:13 -04001961 data->owner->so_cred,
1962 data->o_res.delegation_type,
1963 &data->o_res.delegation,
1964 data->o_res.pagemod_limit);
Jeff Layton8b199e52018-07-05 05:48:14 -04001965
1966 if (data->o_res.do_recall)
1967 nfs_async_inode_return_delegation(state->inode,
1968 &data->o_res.delegation);
Andy Adamsone23008e2012-10-02 21:07:32 -04001969}
1970
1971/*
1972 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1973 * and update the nfs4_state.
1974 */
1975static struct nfs4_state *
1976_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1977{
1978 struct inode *inode = data->state->inode;
1979 struct nfs4_state *state = data->state;
1980 int ret;
1981
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001982 if (!data->rpc_done) {
Anna Schumaker37a84842017-01-11 16:08:35 -05001983 if (data->rpc_status)
1984 return ERR_PTR(data->rpc_status);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001985 /* cached opens have already been processed */
1986 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001987 }
1988
Andy Adamsone23008e2012-10-02 21:07:32 -04001989 ret = nfs_refresh_inode(inode, &data->f_attr);
1990 if (ret)
Anna Schumaker37a84842017-01-11 16:08:35 -05001991 return ERR_PTR(ret);
Andy Adamsone23008e2012-10-02 21:07:32 -04001992
1993 if (data->o_res.delegation_type != 0)
1994 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001995update:
Trond Myklebuste3c8dc72019-07-29 18:25:00 +01001996 if (!update_open_stateid(state, &data->o_res.stateid,
1997 NULL, data->o_arg.fmode))
1998 return ERR_PTR(-EAGAIN);
Trond Myklebustace9fad2018-09-02 19:19:07 -04001999 refcount_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04002000
2001 return state;
Andy Adamsone23008e2012-10-02 21:07:32 -04002002}
2003
Trond Myklebust4e2fcac2017-08-08 09:06:18 -04002004static struct inode *
2005nfs4_opendata_get_inode(struct nfs4_opendata *data)
2006{
2007 struct inode *inode;
2008
2009 switch (data->o_arg.claim) {
2010 case NFS4_OPEN_CLAIM_NULL:
2011 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
2012 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
2013 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
2014 return ERR_PTR(-EAGAIN);
2015 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh,
2016 &data->f_attr, data->f_label);
2017 break;
2018 default:
2019 inode = d_inode(data->dentry);
2020 ihold(inode);
2021 nfs_refresh_inode(inode, &data->f_attr);
2022 }
2023 return inode;
2024}
2025
Andy Adamsone23008e2012-10-02 21:07:32 -04002026static struct nfs4_state *
Trond Myklebust75e8c482017-08-08 10:38:07 -04002027nfs4_opendata_find_nfs4_state(struct nfs4_opendata *data)
2028{
2029 struct nfs4_state *state;
2030 struct inode *inode;
2031
2032 inode = nfs4_opendata_get_inode(data);
2033 if (IS_ERR(inode))
2034 return ERR_CAST(inode);
2035 if (data->state != NULL && data->state->inode == inode) {
2036 state = data->state;
Trond Myklebustace9fad2018-09-02 19:19:07 -04002037 refcount_inc(&state->count);
Trond Myklebust75e8c482017-08-08 10:38:07 -04002038 } else
2039 state = nfs4_get_open_state(inode, data->owner);
2040 iput(inode);
2041 if (state == NULL)
2042 state = ERR_PTR(-ENOMEM);
2043 return state;
2044}
2045
Andy Adamsone23008e2012-10-02 21:07:32 -04002046static struct nfs4_state *
2047_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002048{
Trond Myklebust75e8c482017-08-08 10:38:07 -04002049 struct nfs4_state *state;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002050
Trond Myklebustaac00a82007-07-05 19:02:21 -04002051 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04002052 state = nfs4_try_open_cached(data);
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002053 trace_nfs4_cached_open(data->state);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002054 goto out;
2055 }
2056
Trond Myklebust75e8c482017-08-08 10:38:07 -04002057 state = nfs4_opendata_find_nfs4_state(data);
2058 if (IS_ERR(state))
2059 goto out;
2060
Andy Adamsone23008e2012-10-02 21:07:32 -04002061 if (data->o_res.delegation_type != 0)
2062 nfs4_opendata_check_deleg(data, state);
Trond Myklebuste3c8dc72019-07-29 18:25:00 +01002063 if (!update_open_stateid(state, &data->o_res.stateid,
2064 NULL, data->o_arg.fmode)) {
2065 nfs4_put_open_state(state);
2066 state = ERR_PTR(-EAGAIN);
2067 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002068out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08002069 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002070 return state;
2071}
2072
Andy Adamsone23008e2012-10-02 21:07:32 -04002073static struct nfs4_state *
2074nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
2075{
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002076 struct nfs4_state *ret;
2077
Andy Adamsone23008e2012-10-02 21:07:32 -04002078 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002079 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
2080 else
2081 ret = _nfs4_opendata_to_nfs4_state(data);
2082 nfs4_sequence_free_slot(&data->o_res.seq_res);
2083 return ret;
Andy Adamsone23008e2012-10-02 21:07:32 -04002084}
2085
Trond Myklebust0de43972018-09-02 15:57:01 -04002086static struct nfs_open_context *
2087nfs4_state_find_open_context_mode(struct nfs4_state *state, fmode_t mode)
Trond Myklebust864472e2006-01-03 09:55:15 +01002088{
2089 struct nfs_inode *nfsi = NFS_I(state->inode);
2090 struct nfs_open_context *ctx;
2091
Trond Myklebust0de43972018-09-02 15:57:01 -04002092 rcu_read_lock();
2093 list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
Trond Myklebust864472e2006-01-03 09:55:15 +01002094 if (ctx->state != state)
2095 continue;
Trond Myklebust0de43972018-09-02 15:57:01 -04002096 if ((ctx->mode & mode) != mode)
2097 continue;
2098 if (!get_nfs_open_context(ctx))
2099 continue;
2100 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01002101 return ctx;
2102 }
Trond Myklebust0de43972018-09-02 15:57:01 -04002103 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01002104 return ERR_PTR(-ENOENT);
2105}
2106
Trond Myklebust0de43972018-09-02 15:57:01 -04002107static struct nfs_open_context *
2108nfs4_state_find_open_context(struct nfs4_state *state)
2109{
2110 struct nfs_open_context *ctx;
2111
2112 ctx = nfs4_state_find_open_context_mode(state, FMODE_READ|FMODE_WRITE);
2113 if (!IS_ERR(ctx))
2114 return ctx;
2115 ctx = nfs4_state_find_open_context_mode(state, FMODE_WRITE);
2116 if (!IS_ERR(ctx))
2117 return ctx;
2118 return nfs4_state_find_open_context_mode(state, FMODE_READ);
2119}
2120
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002121static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
2122 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002123{
2124 struct nfs4_opendata *opendata;
2125
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002126 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002127 NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002128 if (opendata == NULL)
2129 return ERR_PTR(-ENOMEM);
2130 opendata->state = state;
Trond Myklebustace9fad2018-09-02 19:19:07 -04002131 refcount_inc(&state->count);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002132 return opendata;
2133}
2134
Trond Myklebust24311f82015-09-20 10:50:17 -04002135static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
2136 fmode_t fmode)
Trond Myklebust864472e2006-01-03 09:55:15 +01002137{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002138 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01002139 int ret;
2140
Trond Myklebust24311f82015-09-20 10:50:17 -04002141 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
NeilBrown39f897f2015-06-29 14:28:54 +10002142 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002143 opendata->o_arg.open_flags = 0;
2144 opendata->o_arg.fmode = fmode;
Trond Myklebustbe36e182015-02-27 17:04:17 -05002145 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
2146 NFS_SB(opendata->dentry->d_sb),
2147 fmode, 0);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002148 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
2149 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
2150 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002151 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002152 if (ret != 0)
2153 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002154 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002155 if (IS_ERR(newstate))
2156 return PTR_ERR(newstate);
Trond Myklebust24311f82015-09-20 10:50:17 -04002157 if (newstate != opendata->state)
2158 ret = -ESTALE;
Al Viro643168c2011-06-22 18:20:23 -04002159 nfs4_close_state(newstate, fmode);
Trond Myklebust24311f82015-09-20 10:50:17 -04002160 return ret;
Trond Myklebust864472e2006-01-03 09:55:15 +01002161}
2162
2163static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
2164{
Trond Myklebust864472e2006-01-03 09:55:15 +01002165 int ret;
2166
2167 /* memory barrier prior to reading state->n_* */
2168 smp_rmb();
Trond Myklebust24311f82015-09-20 10:50:17 -04002169 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2170 if (ret != 0)
2171 return ret;
2172 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2173 if (ret != 0)
2174 return ret;
2175 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
2176 if (ret != 0)
2177 return ret;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002178 /*
2179 * We may have performed cached opens for all three recoveries.
2180 * Check if we need to update the current stateid.
2181 */
2182 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05002183 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04002184 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002185 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05002186 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04002187 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002188 }
Trond Myklebust864472e2006-01-03 09:55:15 +01002189 return 0;
2190}
2191
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192/*
2193 * OPEN_RECLAIM:
2194 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002196static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002198 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01002199 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002200 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 int status;
2202
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002203 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2204 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002205 if (IS_ERR(opendata))
2206 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002207 rcu_read_lock();
2208 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05002209 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04002210 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002211 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01002212 opendata->o_arg.u.delegation_type = delegation_type;
2213 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002214 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 return status;
2216}
2217
Trond Myklebust539cd032007-06-05 11:46:42 -04002218static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219{
2220 struct nfs_server *server = NFS_SERVER(state->inode);
2221 struct nfs4_exception exception = { };
2222 int err;
2223 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002224 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002225 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002226 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2227 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04002228 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002229 break;
2230 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 } while (exception.retry);
2232 return err;
2233}
2234
Trond Myklebust864472e2006-01-03 09:55:15 +01002235static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
2236{
2237 struct nfs_open_context *ctx;
2238 int ret;
2239
2240 ctx = nfs4_state_find_open_context(state);
2241 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002242 return -EAGAIN;
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002243 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2244 nfs_state_clear_open_state_flags(state);
Trond Myklebust539cd032007-06-05 11:46:42 -04002245 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002246 put_nfs_open_context(ctx);
2247 return ret;
2248}
2249
NeilBrowndce26302017-12-13 09:57:09 +11002250static 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 -07002251{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002252 switch (err) {
2253 default:
2254 printk(KERN_ERR "NFS: %s: unhandled error "
2255 "%d.\n", __func__, err);
Gustavo A. R. Silvaffb81712020-11-20 12:26:46 -06002256 fallthrough;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002257 case 0:
2258 case -ENOENT:
Trond Myklebust8eee52a2015-06-04 13:51:13 -04002259 case -EAGAIN:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002260 case -ESTALE:
Trond Myklebust67e7b522019-08-07 07:31:27 -04002261 case -ETIMEDOUT:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002262 break;
2263 case -NFS4ERR_BADSESSION:
2264 case -NFS4ERR_BADSLOT:
2265 case -NFS4ERR_BAD_HIGH_SLOT:
2266 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
2267 case -NFS4ERR_DEADSESSION:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002268 return -EAGAIN;
2269 case -NFS4ERR_STALE_CLIENTID:
2270 case -NFS4ERR_STALE_STATEID:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002271 /* Don't recall a delegation if it was lost */
2272 nfs4_schedule_lease_recovery(server->nfs_client);
2273 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04002274 case -NFS4ERR_MOVED:
2275 nfs4_schedule_migration_recovery(server);
2276 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04002277 case -NFS4ERR_LEASE_MOVED:
2278 nfs4_schedule_lease_moved_recovery(server->nfs_client);
2279 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002280 case -NFS4ERR_DELEG_REVOKED:
2281 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust404ea3562016-09-22 13:39:08 -04002282 case -NFS4ERR_EXPIRED:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002283 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002284 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002285 nfs_inode_find_state_and_recover(state->inode,
2286 stateid);
2287 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05002288 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002289 case -NFS4ERR_DELAY:
2290 case -NFS4ERR_GRACE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002291 ssleep(1);
2292 return -EAGAIN;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002293 case -ENOMEM:
2294 case -NFS4ERR_DENIED:
NeilBrowndce26302017-12-13 09:57:09 +11002295 if (fl) {
2296 struct nfs4_lock_state *lsp = fl->fl_u.nfs4_fl.owner;
2297 if (lsp)
2298 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2299 }
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002300 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002301 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 return err;
2303}
2304
Trond Myklebust24311f82015-09-20 10:50:17 -04002305int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002306 struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002307{
2308 struct nfs_server *server = NFS_SERVER(state->inode);
2309 struct nfs4_opendata *opendata;
Trond Myklebust24311f82015-09-20 10:50:17 -04002310 int err = 0;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002311
2312 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2313 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2314 if (IS_ERR(opendata))
2315 return PTR_ERR(opendata);
2316 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002317 if (!test_bit(NFS_O_RDWR_STATE, &state->flags)) {
Trond Myklebust24311f82015-09-20 10:50:17 -04002318 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2319 if (err)
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002320 goto out;
2321 }
2322 if (!test_bit(NFS_O_WRONLY_STATE, &state->flags)) {
Trond Myklebust24311f82015-09-20 10:50:17 -04002323 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2324 if (err)
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002325 goto out;
Trond Myklebust24311f82015-09-20 10:50:17 -04002326 }
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002327 if (!test_bit(NFS_O_RDONLY_STATE, &state->flags)) {
2328 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2329 if (err)
2330 goto out;
2331 }
2332 nfs_state_clear_delegation(state);
2333out:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002334 nfs4_opendata_put(opendata);
NeilBrowndce26302017-12-13 09:57:09 +11002335 return nfs4_handle_delegation_recall_error(server, state, stateid, NULL, err);
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002336}
2337
Chuck Leverbe05c862013-08-09 12:49:47 -04002338static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2339{
2340 struct nfs4_opendata *data = calldata;
2341
Anna Schumaker7981c8a2017-01-10 11:39:53 -05002342 nfs4_setup_sequence(data->o_arg.server->nfs_client,
2343 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04002344}
2345
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002346static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2347{
2348 struct nfs4_opendata *data = calldata;
2349
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002350 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04002351
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002352 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002353 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05002354 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05002355 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002356 renew_lease(data->o_res.server, data->timestamp);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002357 data->rpc_done = true;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002358 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002359}
2360
2361static void nfs4_open_confirm_release(void *calldata)
2362{
2363 struct nfs4_opendata *data = calldata;
2364 struct nfs4_state *state = NULL;
2365
2366 /* If this request hasn't been cancelled, do nothing */
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002367 if (!data->cancelled)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002368 goto out_free;
2369 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002370 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002371 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002372 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002373 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002374 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002375out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002376 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002377}
2378
2379static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04002380 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002381 .rpc_call_done = nfs4_open_confirm_done,
2382 .rpc_release = nfs4_open_confirm_release,
2383};
2384
2385/*
2386 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2387 */
2388static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2389{
David Howells2b0143b2015-03-17 22:25:59 +00002390 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002391 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002392 struct rpc_message msg = {
2393 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2394 .rpc_argp = &data->c_arg,
2395 .rpc_resp = &data->c_res,
2396 .rpc_cred = data->owner->so_cred,
2397 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002398 struct rpc_task_setup task_setup_data = {
2399 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002400 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002401 .callback_ops = &nfs4_open_confirm_ops,
2402 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002403 .workqueue = nfsiod_workqueue,
Trond Myklebust61296502020-02-07 19:38:12 -05002404 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002405 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 int status;
2407
Anna Schumakerfba83f32018-05-04 16:22:50 -04002408 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1,
2409 data->is_recover);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002410 kref_get(&data->kref);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002411 data->rpc_done = false;
Trond Myklebust3e309912007-07-07 13:19:59 -04002412 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002413 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002414 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05002415 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002416 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002417 status = rpc_wait_for_completion_task(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002418 if (status != 0) {
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002419 data->cancelled = true;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002420 smp_wmb();
2421 } else
2422 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05002423 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 return status;
2425}
2426
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002427static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002429 struct nfs4_opendata *data = calldata;
2430 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04002431 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust2a606182015-08-19 22:30:00 -05002432 enum open_claim_type4 claim = data->o_arg.claim;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002433
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002434 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002435 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002436 /*
2437 * Check if we still need to send an OPEN call, or if we can use
2438 * a delegation instead.
2439 */
2440 if (data->state != NULL) {
2441 struct nfs_delegation *delegation;
2442
Trond Myklebustbe189f72018-09-27 17:12:33 -04002443 if (can_open_cached(data->state, data->o_arg.fmode,
2444 data->o_arg.open_flags, claim))
Trond Myklebust6ee41262007-07-08 14:11:36 -04002445 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002446 rcu_read_lock();
Trond Myklebustbe3df3d2019-10-31 18:40:32 -04002447 delegation = nfs4_get_valid_delegation(data->state->inode);
Trond Myklebust2a606182015-08-19 22:30:00 -05002448 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
Trond Myklebust652f89f2011-12-09 19:05:58 -05002449 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002450 rcu_read_unlock();
2451 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04002452 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04002453 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust2a606182015-08-19 22:30:00 -05002454 switch (claim) {
2455 default:
2456 break;
Trond Myklebust8188df12013-04-23 14:31:19 -04002457 case NFS4_OPEN_CLAIM_PREVIOUS:
2458 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2459 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04002460 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002461 fallthrough;
Trond Myklebust8188df12013-04-23 14:31:19 -04002462 case NFS4_OPEN_CLAIM_FH:
2463 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002464 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002465 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05002466 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
Andy Adamsond8985282009-04-01 09:22:21 -04002467 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002468 &data->o_res.seq_res,
2469 task) != 0)
2470 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04002471
2472 /* Set the create mode (note dependency on the session type) */
2473 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2474 if (data->o_arg.open_flags & O_EXCL) {
2475 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2476 if (nfs4_has_persistent_session(clp))
2477 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2478 else if (clp->cl_mvops->minor_version > 0)
2479 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2480 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04002481 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05002482unlock_no_action:
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002483 trace_nfs4_cached_open(data->state);
Trond Myklebust652f89f2011-12-09 19:05:58 -05002484 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04002485out_no_action:
2486 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002487out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002488 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002489}
2490
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002491static void nfs4_open_done(struct rpc_task *task, void *calldata)
2492{
2493 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002495 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04002496
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002497 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04002498 return;
Andy Adamsond8985282009-04-01 09:22:21 -04002499
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002500 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07002501 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2502 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07002503 case S_IFREG:
2504 break;
2505 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002506 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002507 break;
2508 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002509 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002510 break;
2511 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002512 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07002513 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07002514 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002515 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04002516 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2517 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07002518 }
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002519 data->rpc_done = true;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002520}
Trond Myklebust6f926b52005-10-18 14:20:18 -07002521
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002522static void nfs4_open_release(void *calldata)
2523{
2524 struct nfs4_opendata *data = calldata;
2525 struct nfs4_state *state = NULL;
2526
2527 /* If this request hasn't been cancelled, do nothing */
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002528 if (!data->cancelled)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002529 goto out_free;
2530 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002531 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002532 goto out_free;
2533 /* In case we need an open_confirm, no cleanup! */
2534 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2535 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002536 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002537 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002538 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002539out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002540 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002541}
2542
2543static const struct rpc_call_ops nfs4_open_ops = {
2544 .rpc_call_prepare = nfs4_open_prepare,
2545 .rpc_call_done = nfs4_open_done,
2546 .rpc_release = nfs4_open_release,
2547};
2548
Fred Isaman3b65a302016-09-19 10:06:49 -04002549static int nfs4_run_open_task(struct nfs4_opendata *data,
2550 struct nfs_open_context *ctx)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002551{
David Howells2b0143b2015-03-17 22:25:59 +00002552 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002553 struct nfs_server *server = NFS_SERVER(dir);
2554 struct nfs_openargs *o_arg = &data->o_arg;
2555 struct nfs_openres *o_res = &data->o_res;
2556 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002557 struct rpc_message msg = {
2558 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2559 .rpc_argp = o_arg,
2560 .rpc_resp = o_res,
2561 .rpc_cred = data->owner->so_cred,
2562 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002563 struct rpc_task_setup task_setup_data = {
2564 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002565 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002566 .callback_ops = &nfs4_open_ops,
2567 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002568 .workqueue = nfsiod_workqueue,
Trond Myklebust61296502020-02-07 19:38:12 -05002569 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002570 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002571 int status;
2572
Olga Kornievskaia85e39fe2021-06-23 23:28:51 -04002573 if (server->nfs_client->cl_minorversion)
2574 task_setup_data.flags |= RPC_TASK_MOVEABLE;
2575
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002576 kref_get(&data->kref);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002577 data->rpc_done = false;
Trond Myklebust3e309912007-07-07 13:19:59 -04002578 data->rpc_status = 0;
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002579 data->cancelled = false;
2580 data->is_recover = false;
Fred Isaman3b65a302016-09-19 10:06:49 -04002581 if (!ctx) {
2582 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 1);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002583 data->is_recover = true;
Trond Myklebust67e7b522019-08-07 07:31:27 -04002584 task_setup_data.flags |= RPC_TASK_TIMEOUT;
Fred Isaman2409a972016-10-06 12:11:21 -04002585 } else {
Fred Isaman3b65a302016-09-19 10:06:49 -04002586 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 0);
Fred Isaman2409a972016-10-06 12:11:21 -04002587 pnfs_lgopen_prepare(data, ctx);
2588 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04002589 task = rpc_run_task(&task_setup_data);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002590 if (IS_ERR(task))
2591 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002592 status = rpc_wait_for_completion_task(task);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002593 if (status != 0) {
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002594 data->cancelled = true;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002595 smp_wmb();
2596 } else
2597 status = data->rpc_status;
2598 rpc_put_task(task);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002599
2600 return status;
2601}
2602
2603static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2604{
David Howells2b0143b2015-03-17 22:25:59 +00002605 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002606 struct nfs_openres *o_res = &data->o_res;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002607 int status;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002608
Fred Isaman3b65a302016-09-19 10:06:49 -04002609 status = nfs4_run_open_task(data, NULL);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002610 if (status != 0 || !data->rpc_done)
2611 return status;
2612
Trond Myklebust6926afd12012-01-07 13:22:46 -05002613 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2614
Anna Schumakerd7e98252017-01-11 16:13:29 -05002615 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002616 status = _nfs4_proc_open_confirm(data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002617
2618 return status;
2619}
2620
Trond Myklebustf3792d62014-07-10 08:54:32 -04002621/*
2622 * Additional permission checks in order to distinguish between an
2623 * open for read, and an open for execute. This works around the
2624 * fact that NFSv4 OPEN treats read and execute permissions as being
2625 * the same.
2626 * Note that in the non-execute case, we want to turn off permission
2627 * checking if we just created a new file (POSIX open() semantics).
2628 */
NeilBrowna52458b2018-12-03 11:30:31 +11002629static int nfs4_opendata_access(const struct cred *cred,
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002630 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002631 struct nfs4_state *state, fmode_t fmode,
2632 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002633{
2634 struct nfs_access_entry cache;
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002635 u32 mask, flags;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002636
2637 /* access call failed or for some reason the server doesn't
2638 * support any access modes -- defer access call until later */
2639 if (opendata->o_res.access_supported == 0)
2640 return 0;
2641
2642 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002643 /*
2644 * Use openflags to check for exec, because fmode won't
2645 * always have FMODE_EXEC set when file open for exec.
2646 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002647 if (openflags & __FMODE_EXEC) {
2648 /* ONLY check for exec rights */
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002649 if (S_ISDIR(state->inode->i_mode))
2650 mask = NFS4_ACCESS_LOOKUP;
2651 else
2652 mask = NFS4_ACCESS_EXECUTE;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002653 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002654 mask = NFS4_ACCESS_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002655
2656 cache.cred = cred;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002657 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2658 nfs_access_add_cache(state->inode, &cache);
2659
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002660 flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP;
2661 if ((mask & ~cache.mask & flags) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002662 return 0;
2663
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002664 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002665}
2666
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002667/*
2668 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2669 */
Fred Isaman3b65a302016-09-19 10:06:49 -04002670static int _nfs4_proc_open(struct nfs4_opendata *data,
2671 struct nfs_open_context *ctx)
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002672{
David Howells2b0143b2015-03-17 22:25:59 +00002673 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002674 struct nfs_server *server = NFS_SERVER(dir);
2675 struct nfs_openargs *o_arg = &data->o_arg;
2676 struct nfs_openres *o_res = &data->o_res;
2677 int status;
2678
Fred Isaman3b65a302016-09-19 10:06:49 -04002679 status = nfs4_run_open_task(data, ctx);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002680 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002681 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002682 if (status != 0) {
2683 if (status == -NFS4ERR_BADNAME &&
2684 !(o_arg->open_flags & O_CREAT))
2685 return -ENOENT;
2686 return status;
2687 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002688
Trond Myklebust6926afd12012-01-07 13:22:46 -05002689 nfs_fattr_map_and_free_names(server, &data->f_attr);
2690
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002691 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002692 if (o_arg->open_flags & O_EXCL)
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002693 data->file_created = true;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002694 else if (o_res->cinfo.before != o_res->cinfo.after)
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002695 data->file_created = true;
Jeff Layton1eb5d982018-01-09 08:21:17 -05002696 if (data->file_created ||
2697 inode_peek_iversion_raw(dir) != o_res->cinfo.after)
Frank van der Linden1b523ca2020-06-23 22:38:59 +00002698 nfs4_update_changeattr(dir, &o_res->cinfo,
2699 o_res->f_attr->time_start,
2700 NFS_INO_INVALID_DATA);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002701 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002702 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2703 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002705 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002707 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 }
Trond Myklebust56e0d712017-04-15 19:20:01 -04002709 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2710 nfs4_sequence_free_slot(&o_res->seq_res);
Trond Myklebusta841b542018-04-07 13:50:59 -04002711 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr,
2712 o_res->f_label, NULL);
Trond Myklebust56e0d712017-04-15 19:20:01 -04002713 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002714 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715}
2716
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717/*
2718 * OPEN_EXPIRED:
2719 * reclaim state on the server after a network partition.
2720 * Assumes caller holds the appropriate lock
2721 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002722static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002724 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002725 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002727 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002728 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002729 if (IS_ERR(opendata))
2730 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002731 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002732 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002733 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002734 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002735 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736}
2737
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002738static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002739{
Trond Myklebust539cd032007-06-05 11:46:42 -04002740 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002741 struct nfs4_exception exception = { };
2742 int err;
2743
2744 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002745 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002746 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002747 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2748 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002749 switch (err) {
2750 default:
2751 goto out;
2752 case -NFS4ERR_GRACE:
2753 case -NFS4ERR_DELAY:
2754 nfs4_handle_exception(server, err, &exception);
2755 err = 0;
2756 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002757 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002758out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002759 return err;
2760}
2761
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2763{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002765 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766
Trond Myklebust864472e2006-01-03 09:55:15 +01002767 ctx = nfs4_state_find_open_context(state);
2768 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002769 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002770 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002771 put_nfs_open_context(ctx);
2772 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773}
2774
Trond Myklebust41020b62016-09-22 13:38:58 -04002775static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2776 const nfs4_stateid *stateid)
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002777{
Trond Myklebust41020b62016-09-22 13:38:58 -04002778 nfs_remove_bad_delegation(state->inode, stateid);
Trond Myklebust9f0c5122018-09-05 14:07:15 -04002779 nfs_state_clear_delegation(state);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002780}
2781
2782static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2783{
2784 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
Trond Myklebust41020b62016-09-22 13:38:58 -04002785 nfs_finish_clear_delegation_stateid(state, NULL);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002786}
2787
2788static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2789{
2790 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2791 nfs40_clear_delegation_stateid(state);
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002792 nfs_state_clear_open_state_flags(state);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002793 return nfs4_open_expired(sp, state);
2794}
2795
Trond Myklebust45870d62016-09-22 13:38:59 -04002796static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2797 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11002798 const struct cred *cred)
Trond Myklebust45870d62016-09-22 13:38:59 -04002799{
2800 return -NFS4ERR_BAD_STATEID;
2801}
2802
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002803#if defined(CONFIG_NFS_V4_1)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002804static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2805 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11002806 const struct cred *cred)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002807{
2808 int status;
2809
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002810 switch (stateid->type) {
2811 default:
2812 break;
2813 case NFS4_INVALID_STATEID_TYPE:
2814 case NFS4_SPECIAL_STATEID_TYPE:
2815 return -NFS4ERR_BAD_STATEID;
2816 case NFS4_REVOKED_STATEID_TYPE:
2817 goto out_free;
2818 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002819
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002820 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002821 switch (status) {
2822 case -NFS4ERR_EXPIRED:
2823 case -NFS4ERR_ADMIN_REVOKED:
2824 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002825 break;
2826 default:
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002827 return status;
2828 }
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002829out_free:
2830 /* Ack the revoked state to the server */
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04002831 nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002832 return -NFS4ERR_EXPIRED;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002833}
2834
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002835static int nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002836{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002837 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002838 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002839 struct nfs_delegation *delegation;
NeilBrowna52458b2018-12-03 11:30:31 +11002840 const struct cred *cred = NULL;
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002841 int status, ret = NFS_OK;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002842
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002843 /* Get the delegation credential for use by test/free_stateid */
2844 rcu_read_lock();
2845 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002846 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002847 rcu_read_unlock();
Trond Myklebust9f0c5122018-09-05 14:07:15 -04002848 nfs_state_clear_delegation(state);
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002849 return NFS_OK;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002850 }
2851
Trond Myklebustfc51b1c2020-04-02 15:27:09 -04002852 spin_lock(&delegation->lock);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002853 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002854
Trond Myklebust994b15b2018-09-05 14:07:14 -04002855 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2856 &delegation->flags)) {
Trond Myklebustfc51b1c2020-04-02 15:27:09 -04002857 spin_unlock(&delegation->lock);
Trond Myklebust994b15b2018-09-05 14:07:14 -04002858 rcu_read_unlock();
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002859 return NFS_OK;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002860 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002861
NeilBrowna52458b2018-12-03 11:30:31 +11002862 if (delegation->cred)
2863 cred = get_cred(delegation->cred);
Trond Myklebustfc51b1c2020-04-02 15:27:09 -04002864 spin_unlock(&delegation->lock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002865 rcu_read_unlock();
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002866 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002867 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002868 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
Trond Myklebust41020b62016-09-22 13:38:58 -04002869 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002870 else
2871 ret = status;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002872
Trond Myklebust8c39a392019-07-19 13:48:44 -04002873 put_cred(cred);
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002874 return ret;
2875}
2876
2877static void nfs41_delegation_recover_stateid(struct nfs4_state *state)
2878{
2879 nfs4_stateid tmp;
2880
2881 if (test_bit(NFS_DELEGATED_STATE, &state->flags) &&
2882 nfs4_copy_delegation_stateid(state->inode, state->state,
2883 &tmp, NULL) &&
2884 nfs4_stateid_match_other(&state->stateid, &tmp))
2885 nfs_state_set_delegation(state, &tmp, state->state);
2886 else
2887 nfs_state_clear_delegation(state);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002888}
2889
2890/**
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002891 * nfs41_check_expired_locks - possibly free a lock stateid
2892 *
2893 * @state: NFSv4 state for an inode
2894 *
2895 * Returns NFS_OK if recovery for this stateid is now finished.
2896 * Otherwise a negative NFS4ERR value is returned.
2897 */
2898static int nfs41_check_expired_locks(struct nfs4_state *state)
2899{
2900 int status, ret = NFS_OK;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002901 struct nfs4_lock_state *lsp, *prev = NULL;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002902 struct nfs_server *server = NFS_SERVER(state->inode);
2903
2904 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2905 goto out;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002906
2907 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002908 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2909 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
NeilBrowna52458b2018-12-03 11:30:31 +11002910 const struct cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002911
Elena Reshetova194bc1f2017-10-20 12:53:36 +03002912 refcount_inc(&lsp->ls_count);
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002913 spin_unlock(&state->state_lock);
2914
2915 nfs4_put_lock_state(prev);
2916 prev = lsp;
2917
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002918 status = nfs41_test_and_free_expired_stateid(server,
2919 &lsp->ls_stateid,
2920 cred);
2921 trace_nfs4_test_lock_stateid(state, lsp, status);
2922 if (status == -NFS4ERR_EXPIRED ||
2923 status == -NFS4ERR_BAD_STATEID) {
2924 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002925 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002926 if (!recover_lost_locks)
2927 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2928 } else if (status != NFS_OK) {
2929 ret = status;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002930 nfs4_put_lock_state(prev);
2931 goto out;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002932 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002933 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002934 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002935 }
2936 spin_unlock(&state->state_lock);
2937 nfs4_put_lock_state(prev);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002938out:
2939 return ret;
2940}
2941
2942/**
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002943 * nfs41_check_open_stateid - possibly free an open stateid
2944 *
2945 * @state: NFSv4 state for an inode
2946 *
2947 * Returns NFS_OK if recovery for this stateid is now finished.
2948 * Otherwise a negative NFS4ERR value is returned.
2949 */
2950static int nfs41_check_open_stateid(struct nfs4_state *state)
2951{
2952 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002953 nfs4_stateid *stateid = &state->open_stateid;
NeilBrowna52458b2018-12-03 11:30:31 +11002954 const struct cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002955 int status;
2956
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002957 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002958 return -NFS4ERR_BAD_STATEID;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002959 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002960 trace_nfs4_test_open_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002961 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002962 nfs_state_clear_open_state_flags(state);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002963 stateid->type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002964 return status;
Trond Myklebustc0ca0e52017-08-08 21:39:28 -04002965 }
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002966 if (nfs_open_stateid_recover_openmode(state))
2967 return -NFS4ERR_OPENMODE;
2968 return NFS_OK;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002969}
2970
2971static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2972{
Chuck Levereb64cf92012-07-11 16:30:05 -04002973 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002974
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002975 status = nfs41_check_delegation_stateid(state);
2976 if (status != NFS_OK)
2977 return status;
2978 nfs41_delegation_recover_stateid(state);
2979
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002980 status = nfs41_check_expired_locks(state);
2981 if (status != NFS_OK)
2982 return status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002983 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002984 if (status != NFS_OK)
2985 status = nfs4_open_expired(sp, state);
2986 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002987}
2988#endif
2989
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002991 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2992 * fields corresponding to attributes that were used to store the verifier.
2993 * Make sure we clobber those fields in the later setattr call
2994 */
Trond Myklebust609339c2018-03-28 16:18:17 -04002995static unsigned nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002996 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002997{
Trond Myklebust609339c2018-03-28 16:18:17 -04002998 const __u32 *bitmask = opendata->o_arg.server->exclcreat_bitmask;
2999 __u32 attrset[3];
3000 unsigned ret;
3001 unsigned i;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08003002
Trond Myklebust609339c2018-03-28 16:18:17 -04003003 for (i = 0; i < ARRAY_SIZE(attrset); i++) {
3004 attrset[i] = opendata->o_res.attrset[i];
3005 if (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE4_1)
3006 attrset[i] &= ~bitmask[i];
3007 }
Jeff Laytonaa53ed52007-06-05 14:49:03 -04003008
Trond Myklebust609339c2018-03-28 16:18:17 -04003009 ret = (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE) ?
3010 sattr->ia_valid : 0;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08003011
Trond Myklebust609339c2018-03-28 16:18:17 -04003012 if ((attrset[1] & (FATTR4_WORD1_TIME_ACCESS|FATTR4_WORD1_TIME_ACCESS_SET))) {
3013 if (sattr->ia_valid & ATTR_ATIME_SET)
3014 ret |= ATTR_ATIME_SET;
3015 else
3016 ret |= ATTR_ATIME;
3017 }
Kinglong Mee5334c5b2015-08-26 21:13:37 +08003018
Trond Myklebust609339c2018-03-28 16:18:17 -04003019 if ((attrset[1] & (FATTR4_WORD1_TIME_MODIFY|FATTR4_WORD1_TIME_MODIFY_SET))) {
3020 if (sattr->ia_valid & ATTR_MTIME_SET)
3021 ret |= ATTR_MTIME_SET;
3022 else
3023 ret |= ATTR_MTIME;
3024 }
3025
3026 if (!(attrset[2] & FATTR4_WORD2_SECURITY_LABEL))
Kinglong Mee5334c5b2015-08-26 21:13:37 +08003027 *label = NULL;
Trond Myklebust609339c2018-03-28 16:18:17 -04003028 return ret;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04003029}
3030
Trond Myklebustc21443c2013-02-07 14:26:21 -05003031static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003032 int flags, struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05003033{
3034 struct nfs4_state_owner *sp = opendata->owner;
3035 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04003036 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05003037 struct nfs4_state *state;
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003038 fmode_t acc_mode = _nfs4_ctx_to_accessmode(ctx);
Trond Myklebustcf5b4052020-02-05 09:01:53 -05003039 struct inode *dir = d_inode(opendata->dir);
3040 unsigned long dir_verifier;
Trond Myklebustc21443c2013-02-07 14:26:21 -05003041 unsigned int seq;
3042 int ret;
3043
3044 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
Trond Myklebustcf5b4052020-02-05 09:01:53 -05003045 dir_verifier = nfs_save_change_attribute(dir);
Trond Myklebustc21443c2013-02-07 14:26:21 -05003046
Fred Isaman3b65a302016-09-19 10:06:49 -04003047 ret = _nfs4_proc_open(opendata, ctx);
Trond Myklebustdca780012014-10-23 19:23:03 +03003048 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05003049 goto out;
3050
Trond Myklebustae55e592018-05-22 11:17:16 -04003051 state = _nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebustc21443c2013-02-07 14:26:21 -05003052 ret = PTR_ERR(state);
3053 if (IS_ERR(state))
3054 goto out;
Trond Myklebusta974dee2017-02-08 11:29:46 -05003055 ctx->state = state;
Trond Myklebustc21443c2013-02-07 14:26:21 -05003056 if (server->caps & NFS_CAP_POSIX_LOCK)
3057 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Jeff Laytona8ce3772016-09-17 18:17:35 -04003058 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
3059 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05003060
Trond Myklebust275bb302013-05-29 13:11:28 -04003061 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00003062 if (d_really_is_negative(dentry)) {
Al Viro668d0cd2016-03-08 12:44:17 -05003063 struct dentry *alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04003064 d_drop(dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05003065 alias = d_exact_alias(dentry, state->inode);
3066 if (!alias)
3067 alias = d_splice_alias(igrab(state->inode), dentry);
3068 /* d_splice_alias() can't fail here - it's a non-directory */
3069 if (alias) {
Trond Myklebust275bb302013-05-29 13:11:28 -04003070 dput(ctx->dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05003071 ctx->dentry = dentry = alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04003072 }
Trond Myklebustcf5b4052020-02-05 09:01:53 -05003073 }
3074
3075 switch(opendata->o_arg.claim) {
3076 default:
3077 break;
3078 case NFS4_OPEN_CLAIM_NULL:
3079 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
3080 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
3081 if (!opendata->rpc_done)
3082 break;
3083 if (opendata->o_res.delegation_type != 0)
3084 dir_verifier = nfs_save_change_attribute(dir);
3085 nfs_set_verifier(dentry, dir_verifier);
Trond Myklebust275bb302013-05-29 13:11:28 -04003086 }
3087
Trond Myklebustaf9b6d72018-06-29 12:45:53 -04003088 /* Parse layoutget results before we check for access */
3089 pnfs_parse_lgopen(state->inode, opendata->lgp, ctx);
3090
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003091 ret = nfs4_opendata_access(sp->so_cred, opendata, state,
3092 acc_mode, flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05003093 if (ret != 0)
3094 goto out;
3095
David Howells2b0143b2015-03-17 22:25:59 +00003096 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04003097 nfs_inode_attach_open_context(ctx);
3098 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
3099 nfs4_schedule_stateid_recovery(server, state);
3100 }
Fred Isaman2409a972016-10-06 12:11:21 -04003101
Trond Myklebustc21443c2013-02-07 14:26:21 -05003102out:
Olga Kornievskaia0cb98ab2019-03-19 12:12:13 -04003103 if (!opendata->cancelled)
3104 nfs4_sequence_free_slot(&opendata->o_res.seq_res);
Trond Myklebustc21443c2013-02-07 14:26:21 -05003105 return ret;
3106}
3107
Jeff Laytonaa53ed52007-06-05 14:49:03 -04003108/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01003109 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 */
Andy Adamson82be4172012-05-23 05:02:35 -04003111static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04003112 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04003113 int flags,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003114 const struct nfs4_open_createattrs *c,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003115 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116{
3117 struct nfs4_state_owner *sp;
3118 struct nfs4_state *state = NULL;
3119 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01003120 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04003121 struct dentry *dentry = ctx->dentry;
NeilBrowna52458b2018-12-03 11:30:31 +11003122 const struct cred *cred = ctx->cred;
Trond Myklebust4197a052013-05-29 12:37:49 -04003123 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003124 fmode_t fmode = _nfs4_ctx_to_openmode(ctx);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003125 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003126 struct iattr *sattr = c->sattr;
3127 struct nfs4_label *label = c->label;
David Quigley1775fd32013-05-22 12:50:42 -04003128 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04003129 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130
3131 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05003133 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
3134 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
3136 goto out_err;
3137 }
Anna Schumaker334f87d2017-01-11 16:17:17 -05003138 status = nfs4_client_recover_expired_lease(server->nfs_client);
Trond Myklebust58d97142006-01-03 09:55:24 +01003139 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003140 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00003141 if (d_really_is_positive(dentry))
3142 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01003143 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00003144 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003145 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003146 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
3147 c, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01003148 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05003149 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150
David Quigley14c43f72013-05-22 12:50:43 -04003151 if (label) {
3152 olabel = nfs4_label_alloc(server, GFP_KERNEL);
3153 if (IS_ERR(olabel)) {
3154 status = PTR_ERR(olabel);
3155 goto err_opendata_put;
3156 }
3157 }
3158
Trond Myklebuste911b812014-03-26 13:24:37 -07003159 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
3160 if (!opendata->f_attr.mdsthreshold) {
3161 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
3162 if (!opendata->f_attr.mdsthreshold)
3163 goto err_free_label;
3164 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04003165 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04003166 }
David Howells2b0143b2015-03-17 22:25:59 +00003167 if (d_really_is_positive(dentry))
3168 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04003169
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003170 status = _nfs4_open_and_get_state(opendata, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003171 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04003172 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04003173 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003174
NeilBrownefcbc042015-07-30 13:00:56 +10003175 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04003176 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Trond Myklebust609339c2018-03-28 16:18:17 -04003177 unsigned attrs = nfs4_exclusive_attrset(opendata, sattr, &label);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02003178 /*
3179 * send create attributes which was not set by open
3180 * with an extra setattr.
3181 */
Trond Myklebust609339c2018-03-28 16:18:17 -04003182 if (attrs || label) {
3183 unsigned ia_old = sattr->ia_valid;
3184
3185 sattr->ia_valid = attrs;
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02003186 nfs_fattr_init(opendata->o_res.f_attr);
3187 status = nfs4_do_setattr(state->inode, cred,
3188 opendata->o_res.f_attr, sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11003189 ctx, label, olabel);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02003190 if (status == 0) {
3191 nfs_setattr_update_inode(state->inode, sattr,
3192 opendata->o_res.f_attr);
3193 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
3194 }
Trond Myklebust609339c2018-03-28 16:18:17 -04003195 sattr->ia_valid = ia_old;
David Quigley1775fd32013-05-22 12:50:42 -04003196 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04003197 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08003198 if (opened && opendata->file_created)
Al Viro73a09dd2018-06-08 13:22:02 -04003199 *opened = 1;
Andy Adamson82be4172012-05-23 05:02:35 -04003200
Trond Myklebuste911b812014-03-26 13:24:37 -07003201 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04003202 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07003203 opendata->f_attr.mdsthreshold = NULL;
3204 }
Andy Adamson82be4172012-05-23 05:02:35 -04003205
David Quigley14c43f72013-05-22 12:50:43 -04003206 nfs4_label_free(olabel);
3207
Trond Myklebustc6d00e62007-06-17 16:02:44 -04003208 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04003211err_free_label:
3212 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04003213err_opendata_put:
3214 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01003215err_put_state_owner:
3216 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 return status;
3219}
3220
3221
Andy Adamson82be4172012-05-23 05:02:35 -04003222static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04003223 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04003224 int flags,
3225 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003226 struct nfs4_label *label,
3227 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003229 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust0688e642019-04-07 13:59:09 -04003230 struct nfs4_exception exception = {
3231 .interruptible = true,
3232 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233 struct nfs4_state *res;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003234 struct nfs4_open_createattrs c = {
3235 .label = label,
3236 .sattr = sattr,
3237 .verf = {
3238 [0] = (__u32)jiffies,
3239 [1] = (__u32)current->pid,
3240 },
3241 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242 int status;
3243
3244 do {
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003245 status = _nfs4_do_open(dir, ctx, flags, &c, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04003246 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04003247 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 if (status == 0)
3249 break;
3250 /* NOTE: BAD_SEQID means the server and client disagree about the
3251 * book-keeping w.r.t. state-changing operations
3252 * (OPEN/CLOSE/LOCK/LOCKU...)
3253 * It is actually a sign of a bug on the client or on the server.
3254 *
3255 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07003256 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 * have unhashed the old state_owner for us, and that we can
3258 * therefore safely retry using a new one. We should still warn
3259 * the user though...
3260 */
3261 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04003262 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04003263 " returned a bad sequence-id error!\n",
3264 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 exception.retry = 1;
3266 continue;
3267 }
Trond Myklebust550f5742005-10-18 14:20:21 -07003268 /*
3269 * BAD_STATEID on OPEN means that the server cancelled our
3270 * state before it received the OPEN_CONFIRM.
3271 * Recover by retrying the request as per the discussion
3272 * on Page 181 of RFC3530.
3273 */
3274 if (status == -NFS4ERR_BAD_STATEID) {
3275 exception.retry = 1;
3276 continue;
3277 }
Robert Milkowski924491f2020-01-28 08:37:47 +00003278 if (status == -NFS4ERR_EXPIRED) {
3279 nfs4_schedule_lease_recovery(server->nfs_client);
3280 exception.retry = 1;
3281 continue;
3282 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04003283 if (status == -EAGAIN) {
3284 /* We must have found a delegation */
3285 exception.retry = 1;
3286 continue;
3287 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003288 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
3289 continue;
3290 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291 status, &exception));
3292 } while (exception.retry);
3293 return res;
3294}
3295
Trond Myklebust8487c472016-06-26 08:44:35 -04003296static int _nfs4_do_setattr(struct inode *inode,
3297 struct nfs_setattrargs *arg,
3298 struct nfs_setattrres *res,
NeilBrowna52458b2018-12-03 11:30:31 +11003299 const struct cred *cred,
NeilBrown29b59f92016-10-13 15:26:47 +11003300 struct nfs_open_context *ctx)
Trond Myklebust8487c472016-06-26 08:44:35 -04003301{
3302 struct nfs_server *server = NFS_SERVER(inode);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003303 struct rpc_message msg = {
Trond Myklebust8487c472016-06-26 08:44:35 -04003304 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
3305 .rpc_argp = arg,
3306 .rpc_resp = res,
3307 .rpc_cred = cred,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003308 };
NeilBrowna52458b2018-12-03 11:30:31 +11003309 const struct cred *delegation_cred = NULL;
Trond Myklebust8487c472016-06-26 08:44:35 -04003310 unsigned long timestamp = jiffies;
Trond Myklebust8487c472016-06-26 08:44:35 -04003311 bool truncate;
3312 int status;
3313
3314 nfs_fattr_init(res->fattr);
3315
3316 /* Servers should only apply open mode checks for file size changes */
3317 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
Chuck Lever644c9f42020-09-04 17:39:12 -04003318 if (!truncate) {
3319 nfs4_inode_make_writeable(inode);
Trond Myklebust991eedb2018-04-09 11:15:30 -04003320 goto zero_stateid;
Chuck Lever644c9f42020-09-04 17:39:12 -04003321 }
Trond Myklebust8487c472016-06-26 08:44:35 -04003322
Trond Myklebust991eedb2018-04-09 11:15:30 -04003323 if (nfs4_copy_delegation_stateid(inode, FMODE_WRITE, &arg->stateid, &delegation_cred)) {
Trond Myklebust8487c472016-06-26 08:44:35 -04003324 /* Use that stateid */
Trond Myklebust09a54f02019-08-03 10:28:18 -04003325 } else if (ctx != NULL && ctx->state) {
NeilBrown17393472016-10-13 15:26:47 +11003326 struct nfs_lock_context *l_ctx;
NeilBrown29b59f92016-10-13 15:26:47 +11003327 if (!nfs4_valid_open_stateid(ctx->state))
Trond Myklebust8487c472016-06-26 08:44:35 -04003328 return -EBADF;
NeilBrown17393472016-10-13 15:26:47 +11003329 l_ctx = nfs_get_lock_context(ctx);
3330 if (IS_ERR(l_ctx))
3331 return PTR_ERR(l_ctx);
NeilBrown7a0566b2016-12-06 15:50:06 -05003332 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
3333 &arg->stateid, &delegation_cred);
3334 nfs_put_lock_context(l_ctx);
3335 if (status == -EIO)
Trond Myklebust8487c472016-06-26 08:44:35 -04003336 return -EBADF;
Olga Kornievskaiad826e5b2019-12-18 16:50:42 -05003337 else if (status == -EAGAIN)
3338 goto zero_stateid;
Trond Myklebust991eedb2018-04-09 11:15:30 -04003339 } else {
3340zero_stateid:
Trond Myklebust8487c472016-06-26 08:44:35 -04003341 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
Trond Myklebust991eedb2018-04-09 11:15:30 -04003342 }
Trond Myklebust8487c472016-06-26 08:44:35 -04003343 if (delegation_cred)
3344 msg.rpc_cred = delegation_cred;
3345
3346 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
3347
NeilBrowna52458b2018-12-03 11:30:31 +11003348 put_cred(delegation_cred);
NeilBrown29b59f92016-10-13 15:26:47 +11003349 if (status == 0 && ctx != NULL)
Trond Myklebust8487c472016-06-26 08:44:35 -04003350 renew_lease(server, timestamp);
3351 trace_nfs4_setattr(inode, &arg->stateid, status);
3352 return status;
3353}
3354
NeilBrowna52458b2018-12-03 11:30:31 +11003355static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
Trond Myklebust8487c472016-06-26 08:44:35 -04003356 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11003357 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
Trond Myklebust8487c472016-06-26 08:44:35 -04003358 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05003360 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebust30846df2018-04-07 13:44:28 -04003361 __u32 bitmask[NFS4_BITMASK_SZ];
NeilBrown29b59f92016-10-13 15:26:47 +11003362 struct nfs4_state *state = ctx ? ctx->state : NULL;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003363 struct nfs_setattrargs arg = {
3364 .fh = NFS_FH(inode),
3365 .iap = sattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366 .server = server,
Trond Myklebust30846df2018-04-07 13:44:28 -04003367 .bitmask = bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04003368 .label = ilabel,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003369 };
3370 struct nfs_setattrres res = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003372 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373 .server = server,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003374 };
Trond Myklebust8487c472016-06-26 08:44:35 -04003375 struct nfs4_exception exception = {
3376 .state = state,
3377 .inode = inode,
3378 .stateid = &arg.stateid,
3379 };
Trond Myklebusta71029b2021-04-10 00:23:03 -04003380 unsigned long adjust_flags = NFS_INO_INVALID_CHANGE;
Trond Myklebust8487c472016-06-26 08:44:35 -04003381 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382
Trond Myklebust720869e2021-04-13 09:41:16 -04003383 if (sattr->ia_valid & (ATTR_MODE | ATTR_KILL_SUID | ATTR_KILL_SGID))
3384 adjust_flags |= NFS_INO_INVALID_MODE;
3385 if (sattr->ia_valid & (ATTR_UID | ATTR_GID))
Trond Myklebusta71029b2021-04-10 00:23:03 -04003386 adjust_flags |= NFS_INO_INVALID_OTHER;
3387
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388 do {
Trond Myklebusta71029b2021-04-10 00:23:03 -04003389 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, olabel),
3390 inode, adjust_flags);
Trond Myklebust30846df2018-04-07 13:44:28 -04003391
NeilBrown29b59f92016-10-13 15:26:47 +11003392 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
Trond Myklebust451146b2012-04-18 16:29:11 -04003393 switch (err) {
3394 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04003395 if (!(sattr->ia_valid & ATTR_SIZE)) {
3396 pr_warn_once("NFSv4: server %s is incorrectly "
3397 "applying open mode checks to "
3398 "a SETATTR that is not "
3399 "changing file size.\n",
3400 server->nfs_client->cl_hostname);
3401 }
Trond Myklebust451146b2012-04-18 16:29:11 -04003402 if (state && !(state->state & FMODE_WRITE)) {
3403 err = -EBADF;
3404 if (sattr->ia_valid & ATTR_OPEN)
3405 err = -EACCES;
3406 goto out;
3407 }
3408 }
3409 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04003411out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412 return err;
3413}
3414
Peng Tao500d7012015-09-22 11:35:22 +08003415static bool
3416nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3417{
3418 if (inode == NULL || !nfs_have_layout(inode))
3419 return false;
3420
3421 return pnfs_wait_on_layoutreturn(inode, task);
3422}
3423
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003424/*
3425 * Update the seqid of an open stateid
3426 */
3427static void nfs4_sync_open_stateid(nfs4_stateid *dst,
3428 struct nfs4_state *state)
3429{
3430 __be32 seqid_open;
3431 u32 dst_seqid;
3432 int seq;
3433
3434 for (;;) {
3435 if (!nfs4_valid_open_stateid(state))
3436 break;
3437 seq = read_seqbegin(&state->seqlock);
3438 if (!nfs4_state_match_open_stateid_other(state, dst)) {
3439 nfs4_stateid_copy(dst, &state->open_stateid);
3440 if (read_seqretry(&state->seqlock, seq))
3441 continue;
3442 break;
3443 }
3444 seqid_open = state->open_stateid.seqid;
3445 if (read_seqretry(&state->seqlock, seq))
3446 continue;
3447
3448 dst_seqid = be32_to_cpu(dst->seqid);
3449 if ((s32)(dst_seqid - be32_to_cpu(seqid_open)) < 0)
3450 dst->seqid = seqid_open;
3451 break;
3452 }
3453}
3454
3455/*
3456 * Update the seqid of an open stateid after receiving
3457 * NFS4ERR_OLD_STATEID
3458 */
3459static bool nfs4_refresh_open_old_stateid(nfs4_stateid *dst,
3460 struct nfs4_state *state)
3461{
3462 __be32 seqid_open;
3463 u32 dst_seqid;
3464 bool ret;
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04003465 int seq, status = -EAGAIN;
3466 DEFINE_WAIT(wait);
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003467
3468 for (;;) {
3469 ret = false;
3470 if (!nfs4_valid_open_stateid(state))
3471 break;
3472 seq = read_seqbegin(&state->seqlock);
3473 if (!nfs4_state_match_open_stateid_other(state, dst)) {
3474 if (read_seqretry(&state->seqlock, seq))
3475 continue;
3476 break;
3477 }
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04003478
3479 write_seqlock(&state->seqlock);
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003480 seqid_open = state->open_stateid.seqid;
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003481
3482 dst_seqid = be32_to_cpu(dst->seqid);
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04003483
3484 /* Did another OPEN bump the state's seqid? try again: */
3485 if ((s32)(be32_to_cpu(seqid_open) - dst_seqid) > 0) {
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003486 dst->seqid = seqid_open;
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04003487 write_sequnlock(&state->seqlock);
3488 ret = true;
3489 break;
3490 }
3491
3492 /* server says we're behind but we haven't seen the update yet */
3493 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
3494 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
3495 write_sequnlock(&state->seqlock);
3496 trace_nfs4_close_stateid_update_wait(state->inode, dst, 0);
3497
zhouchuangaobb002382021-05-09 19:34:37 -07003498 if (fatal_signal_pending(current))
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04003499 status = -EINTR;
3500 else
3501 if (schedule_timeout(5*HZ) != 0)
3502 status = 0;
3503
3504 finish_wait(&state->waitq, &wait);
3505
3506 if (!status)
3507 continue;
3508 if (status == -EINTR)
3509 break;
3510
3511 /* we slept the whole 5 seconds, we must have lost a seqid */
3512 dst->seqid = cpu_to_be32(dst_seqid + 1);
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003513 ret = true;
3514 break;
3515 }
3516
3517 return ret;
3518}
3519
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520struct nfs4_closedata {
3521 struct inode *inode;
3522 struct nfs4_state *state;
3523 struct nfs_closeargs arg;
3524 struct nfs_closeres res;
Trond Myklebustcf805162016-11-15 14:56:07 -05003525 struct {
3526 struct nfs4_layoutreturn_args arg;
3527 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003528 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebustcf805162016-11-15 14:56:07 -05003529 u32 roc_barrier;
3530 bool roc;
3531 } lr;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003532 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003533 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534};
3535
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003536static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07003537{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003538 struct nfs4_closedata *calldata = data;
3539 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04003540 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07003541
Trond Myklebustcf805162016-11-15 14:56:07 -05003542 if (calldata->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003543 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3544 calldata->res.lr_ret);
Trond Myklebust95121352005-10-18 14:20:12 -07003545 nfs4_put_open_state(calldata->state);
3546 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07003547 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05003548 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003549 kfree(calldata);
3550}
3551
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003552static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003554 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003557 nfs4_stateid *res_stateid = NULL;
Trond Myklebustb8b8d222017-11-07 10:51:37 -05003558 struct nfs4_exception exception = {
3559 .state = state,
3560 .inode = calldata->inode,
3561 .stateid = &calldata->arg.stateid,
3562 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563
Chuck Levera3ca5652012-03-01 17:00:40 -05003564 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04003565 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3566 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04003567 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Trond Myklebustcf805162016-11-15 14:56:07 -05003568
3569 /* Handle Layoutreturn errors */
Trond Myklebust078000d2021-01-04 13:18:03 -05003570 if (pnfs_roc_done(task, &calldata->arg.lr_args, &calldata->res.lr_res,
3571 &calldata->res.lr_ret) == -EAGAIN)
Trond Myklebust287a9c52019-09-20 07:23:41 -04003572 goto out_restart;
Trond Myklebustcf805162016-11-15 14:56:07 -05003573
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003574 /* hmm. we are done with the inode, and in the process of freeing
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575 * the state_owner. we keep this around to process errors
3576 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577 switch (task->tk_status) {
3578 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04003579 res_stateid = &calldata->res.stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003580 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003581 break;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003582 case -NFS4ERR_ACCESS:
3583 if (calldata->arg.bitmask != NULL) {
3584 calldata->arg.bitmask = NULL;
3585 calldata->res.fattr = NULL;
Trond Myklebust91b30d22017-11-06 15:28:09 -05003586 goto out_restart;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003587
3588 }
3589 break;
Trond Myklebust12f275c2017-11-06 15:28:05 -05003590 case -NFS4ERR_OLD_STATEID:
3591 /* Did we race with OPEN? */
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003592 if (nfs4_refresh_open_old_stateid(&calldata->arg.stateid,
Trond Myklebust91b30d22017-11-06 15:28:09 -05003593 state))
3594 goto out_restart;
Trond Myklebust12f275c2017-11-06 15:28:05 -05003595 goto out_release;
Trond Myklebust69794ad2013-11-20 12:57:19 -05003596 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597 case -NFS4ERR_STALE_STATEID:
Trond Myklebust26d36302016-09-22 13:39:05 -04003598 case -NFS4ERR_EXPIRED:
3599 nfs4_free_revoked_stateid(server,
3600 &calldata->arg.stateid,
3601 task->tk_msg.rpc_cred);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05003602 fallthrough;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003603 case -NFS4ERR_BAD_STATEID:
Trond Myklebuste217e822019-09-20 07:23:46 -04003604 if (calldata->arg.fmode == 0)
3605 break;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05003606 fallthrough;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607 default:
Trond Myklebustb8b8d222017-11-07 10:51:37 -05003608 task->tk_status = nfs4_async_handle_exception(task,
3609 server, task->tk_status, &exception);
3610 if (exception.retry)
Trond Myklebust91b30d22017-11-06 15:28:09 -05003611 goto out_restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07003613 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3614 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003615out_release:
Trond Myklebust91b30d22017-11-06 15:28:09 -05003616 task->tk_status = 0;
Trond Myklebust72211db2009-12-15 14:47:36 -05003617 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustd8d84982016-12-19 12:14:44 -05003618 nfs_refresh_inode(calldata->inode, &calldata->fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05003619 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Trond Myklebust91b30d22017-11-06 15:28:09 -05003620 return;
Trond Myklebust91b30d22017-11-06 15:28:09 -05003621out_restart:
3622 task->tk_status = 0;
3623 rpc_restart_call_prepare(task);
3624 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625}
3626
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003627static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003629 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07003630 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003631 struct inode *inode = calldata->inode;
Trond Myklebust332d1a02021-03-25 18:15:36 -04003632 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustc8bf7072018-06-15 16:31:02 -04003633 struct pnfs_layout_hdr *lo;
Trond Myklebustaee7af32014-08-25 22:33:12 -04003634 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05003635 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07003636
Chuck Levera3ca5652012-03-01 17:00:40 -05003637 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003638 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003639 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04003640
Trond Myklebust88069f72009-12-08 08:33:16 -05003641 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05003642 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04003643 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3644 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3645 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust003707c2007-07-05 18:07:55 -04003646 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003647 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01003648 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003649 if (state->n_rdonly == 0)
3650 call_close |= is_rdonly;
3651 else if (is_rdonly)
3652 calldata->arg.fmode |= FMODE_READ;
3653 if (state->n_wronly == 0)
3654 call_close |= is_wronly;
3655 else if (is_wronly)
3656 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebuste547f262016-06-25 19:19:28 -04003657 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3658 call_close |= is_rdwr;
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003659 } else if (is_rdwr)
3660 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3661
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003662 nfs4_sync_open_stateid(&calldata->arg.stateid, state);
3663 if (!nfs4_valid_open_stateid(state))
Trond Myklebust5d422302013-03-14 16:57:48 -04003664 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05003665 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05003666
3667 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003668 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003669 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07003670 }
Trond Myklebust88069f72009-12-08 08:33:16 -05003671
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003672 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
Peng Tao500d7012015-09-22 11:35:22 +08003673 nfs_release_seqid(calldata->arg.seqid);
3674 goto out_wait;
3675 }
3676
Trond Myklebustc8bf7072018-06-15 16:31:02 -04003677 lo = calldata->arg.lr_args ? calldata->arg.lr_args->layout : NULL;
3678 if (lo && !pnfs_layout_is_valid(lo)) {
3679 calldata->arg.lr_args = NULL;
3680 calldata->res.lr_res = NULL;
3681 }
3682
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003683 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05003684 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003685
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003686 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003687 /* Close-to-open cache consistency revalidation */
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04003688 if (!nfs4_have_delegation(inode, FMODE_READ)) {
Trond Myklebust332d1a02021-03-25 18:15:36 -04003689 nfs4_bitmask_set(calldata->arg.bitmask_store,
3690 server->cache_consistency_bitmask,
3691 inode, server, NULL);
3692 calldata->arg.bitmask = calldata->arg.bitmask_store;
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04003693 } else
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003694 calldata->arg.bitmask = NULL;
3695 }
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003696
Trond Myklebust6ae37332015-01-30 14:21:14 -05003697 calldata->arg.share_access =
3698 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3699 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05003700
Trond Myklebustd8d84982016-12-19 12:14:44 -05003701 if (calldata->res.fattr == NULL)
3702 calldata->arg.bitmask = NULL;
3703 else if (calldata->arg.bitmask == NULL)
3704 calldata->res.fattr = NULL;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003705 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05003706 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
Trond Myklebust9d12b212012-01-17 22:04:25 -05003707 &calldata->arg.seq_args,
3708 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04003709 task) != 0)
3710 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05003711 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003712 return;
3713out_no_action:
3714 task->tk_action = NULL;
3715out_wait:
3716 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717}
3718
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003719static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003720 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003721 .rpc_call_done = nfs4_close_done,
3722 .rpc_release = nfs4_free_closedata,
3723};
3724
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725/*
3726 * It is possible for data to be read/written from a mem-mapped file
3727 * after the sys_close call (which hits the vfs layer as a flush).
3728 * This means that we can't safely call nfsv4 close on a file until
3729 * the inode is cleared. This in turn means that we are not good
3730 * NFSv4 citizens - we do not indicate to the server to update the file's
3731 * share state even when we are done with one of the three share
3732 * stateid's in the inode.
3733 *
3734 * NOTE: Caller must be holding the sp->so_owner semaphore!
3735 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04003736int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003737{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003738 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05003739 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04003741 struct nfs4_state_owner *sp = state->owner;
3742 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003743 struct rpc_message msg = {
3744 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3745 .rpc_cred = state->owner->so_cred,
3746 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003747 struct rpc_task_setup task_setup_data = {
3748 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003749 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003750 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003751 .workqueue = nfsiod_workqueue,
Trond Myklebust61296502020-02-07 19:38:12 -05003752 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003753 };
Trond Myklebust95121352005-10-18 14:20:12 -07003754 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755
Olga Kornievskaia85e39fe2021-06-23 23:28:51 -04003756 if (server->nfs_client->cl_minorversion)
3757 task_setup_data.flags |= RPC_TASK_MOVEABLE;
3758
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04003759 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3760 &task_setup_data.rpc_client, &msg);
3761
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003762 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07003764 goto out;
Anna Schumakerfba83f32018-05-04 16:22:50 -04003765 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1, 0);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003766 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003768 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebustc82bac62017-11-06 15:28:06 -05003769 if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state))
3770 goto out_free_calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05003772 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3773 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05003774 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07003775 goto out_free_calldata;
Trond Myklebustd8d84982016-12-19 12:14:44 -05003776 nfs_fattr_init(&calldata->fattr);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003777 calldata->arg.fmode = 0;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003778 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003779 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003780 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003781 calldata->res.server = server;
Trond Myklebustcf805162016-11-15 14:56:07 -05003782 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003783 calldata->lr.roc = pnfs_roc(state->inode,
3784 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3785 if (calldata->lr.roc) {
3786 calldata->arg.lr_args = &calldata->lr.arg;
3787 calldata->res.lr_res = &calldata->lr.res;
3788 }
Al Viro643168c2011-06-22 18:20:23 -04003789 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003790
Trond Myklebust1174dd12010-12-21 10:52:24 -05003791 msg.rpc_argp = &calldata->arg;
3792 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003793 task_setup_data.callback_data = calldata;
3794 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003795 if (IS_ERR(task))
3796 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003797 status = 0;
3798 if (wait)
3799 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003800 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003801 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003802out_free_calldata:
3803 kfree(calldata);
3804out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003805 nfs4_put_open_state(state);
3806 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003807 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808}
3809
Trond Myklebust2b484292010-09-17 10:56:51 -04003810static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003811nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3812 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003815 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3816
3817 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818
Trond Myklebust565277f2007-10-15 18:17:53 -04003819 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003820 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003821
3822 nfs4_label_release_security(label);
3823
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003824 if (IS_ERR(state))
3825 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003826 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827}
3828
Trond Myklebust1185a552009-12-03 15:54:02 -05003829static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003830{
3831 if (ctx->state == NULL)
3832 return;
3833 if (is_sync)
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003834 nfs4_close_sync(ctx->state, _nfs4_ctx_to_openmode(ctx));
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003835 else
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003836 nfs4_close_state(ctx->state, _nfs4_ctx_to_openmode(ctx));
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003837}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838
Trond Myklebustb944dba2013-11-04 15:20:20 -05003839#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3840#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
Frank van der Lindenb78ef842020-06-23 22:38:55 +00003841#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_XATTR_SUPPORT - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003842
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3844{
Kinglong Mee8c612822015-08-26 21:12:58 +08003845 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003846 struct nfs4_server_caps_arg args = {
3847 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003848 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003849 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850 struct nfs4_server_caps_res res = {};
3851 struct rpc_message msg = {
3852 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003853 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003854 .rpc_resp = &res,
3855 };
3856 int status;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003857 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858
Kinglong Mee8c612822015-08-26 21:12:58 +08003859 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3860 FATTR4_WORD0_FH_EXPIRE_TYPE |
3861 FATTR4_WORD0_LINK_SUPPORT |
3862 FATTR4_WORD0_SYMLINK_SUPPORT |
3863 FATTR4_WORD0_ACLSUPPORT;
3864 if (minorversion)
3865 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3866
Bryan Schumaker7c513052011-03-24 17:12:24 +00003867 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003868 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003869 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003870 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003871 case 0:
3872 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3873 res.attr_bitmask[2] = 0;
3874 break;
3875 case 1:
3876 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3877 break;
3878 case 2:
3879 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3880 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebustce62b112018-03-05 15:01:18 -05003882 server->caps &= ~(NFS_CAP_ACLS | NFS_CAP_HARDLINKS |
3883 NFS_CAP_SYMLINKS| NFS_CAP_SECURITY_LABEL);
3884 server->fattr_valid = NFS_ATTR_FATTR_V4;
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003885 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3886 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887 server->caps |= NFS_CAP_ACLS;
3888 if (res.has_links != 0)
3889 server->caps |= NFS_CAP_HARDLINKS;
3890 if (res.has_symlinks != 0)
3891 server->caps |= NFS_CAP_SYMLINKS;
Scott Mayhew0b4f1322021-06-02 13:13:11 -04003892#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3893 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3894 server->caps |= NFS_CAP_SECURITY_LABEL;
3895#endif
Trond Myklebustce62b112018-03-05 15:01:18 -05003896 if (!(res.attr_bitmask[0] & FATTR4_WORD0_FILEID))
3897 server->fattr_valid &= ~NFS_ATTR_FATTR_FILEID;
3898 if (!(res.attr_bitmask[1] & FATTR4_WORD1_MODE))
3899 server->fattr_valid &= ~NFS_ATTR_FATTR_MODE;
3900 if (!(res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS))
3901 server->fattr_valid &= ~NFS_ATTR_FATTR_NLINK;
3902 if (!(res.attr_bitmask[1] & FATTR4_WORD1_OWNER))
3903 server->fattr_valid &= ~(NFS_ATTR_FATTR_OWNER |
3904 NFS_ATTR_FATTR_OWNER_NAME);
3905 if (!(res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP))
3906 server->fattr_valid &= ~(NFS_ATTR_FATTR_GROUP |
3907 NFS_ATTR_FATTR_GROUP_NAME);
3908 if (!(res.attr_bitmask[1] & FATTR4_WORD1_SPACE_USED))
3909 server->fattr_valid &= ~NFS_ATTR_FATTR_SPACE_USED;
3910 if (!(res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS))
3911 server->fattr_valid &= ~NFS_ATTR_FATTR_ATIME;
3912 if (!(res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA))
3913 server->fattr_valid &= ~NFS_ATTR_FATTR_CTIME;
3914 if (!(res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY))
3915 server->fattr_valid &= ~NFS_ATTR_FATTR_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003916 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3917 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003918 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003919
Trond Myklebusta65318b2009-03-11 14:10:28 -04003920 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3921 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3922 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003923 server->cache_consistency_bitmask[2] = 0;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003924
3925 /* Avoid a regression due to buggy server */
3926 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3927 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
Kinglong Mee8c612822015-08-26 21:12:58 +08003928 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3929 sizeof(server->exclcreat_bitmask));
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003930
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003932 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003934
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935 return status;
3936}
3937
Trond Myklebust55a97592006-06-09 09:34:19 -04003938int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939{
Trond Myklebust0688e642019-04-07 13:59:09 -04003940 struct nfs4_exception exception = {
3941 .interruptible = true,
3942 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003943 int err;
3944 do {
3945 err = nfs4_handle_exception(server,
3946 _nfs4_server_capabilities(server, fhandle),
3947 &exception);
3948 } while (exception.retry);
3949 return err;
3950}
3951
3952static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3953 struct nfs_fsinfo *info)
3954{
David Quigleyaa9c2662013-05-22 12:50:44 -04003955 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003957 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958 };
3959 struct nfs4_lookup_res res = {
3960 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003961 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962 .fh = fhandle,
3963 };
3964 struct rpc_message msg = {
3965 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3966 .rpc_argp = &args,
3967 .rpc_resp = &res,
3968 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003969
David Quigleyaa9c2662013-05-22 12:50:44 -04003970 bitmask[0] = nfs4_fattr_bitmap[0];
3971 bitmask[1] = nfs4_fattr_bitmap[1];
3972 /*
3973 * Process the label in the upcoming getfattr
3974 */
3975 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3976
Trond Myklebust0e574af2005-10-27 22:12:38 -04003977 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003978 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979}
3980
3981static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3982 struct nfs_fsinfo *info)
3983{
Trond Myklebust0688e642019-04-07 13:59:09 -04003984 struct nfs4_exception exception = {
3985 .interruptible = true,
3986 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987 int err;
3988 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003989 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003990 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003991 switch (err) {
3992 case 0:
3993 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003994 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003995 default:
3996 err = nfs4_handle_exception(server, err, &exception);
3997 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003999out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000 return err;
4001}
4002
Bryan Schumaker8f70e952011-03-24 17:12:31 +00004003static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
4004 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
4005{
Trond Myklebustc2190662013-08-26 19:23:04 -04004006 struct rpc_auth_create_args auth_args = {
4007 .pseudoflavor = flavor,
4008 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00004009 struct rpc_auth *auth;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00004010
Trond Myklebustc2190662013-08-26 19:23:04 -04004011 auth = rpcauth_create(&auth_args, server->client);
Anna Schumaker9df13362017-01-11 16:30:08 -05004012 if (IS_ERR(auth))
4013 return -EACCES;
4014 return nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00004015}
4016
Chuck Lever9a744ba2013-03-16 15:56:02 -04004017/*
4018 * Retry pseudoroot lookup with various security flavors. We do this when:
4019 *
4020 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
4021 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
4022 *
4023 * Returns zero on success, or a negative NFS4ERR value, or a
4024 * negative errno value.
4025 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04004026static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04004027 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028{
Chuck Lever9a744ba2013-03-16 15:56:02 -04004029 /* Per 3530bis 15.33.5 */
4030 static const rpc_authflavor_t flav_array[] = {
4031 RPC_AUTH_GSS_KRB5P,
4032 RPC_AUTH_GSS_KRB5I,
4033 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04004034 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04004035 RPC_AUTH_NULL,
4036 };
4037 int status = -EPERM;
4038 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04004040 if (server->auth_info.flavor_len > 0) {
4041 /* try each flavor specified by user */
4042 for (i = 0; i < server->auth_info.flavor_len; i++) {
4043 status = nfs4_lookup_root_sec(server, fhandle, info,
4044 server->auth_info.flavors[i]);
4045 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
4046 continue;
4047 break;
4048 }
4049 } else {
4050 /* no flavors specified by user, try default list */
4051 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
4052 status = nfs4_lookup_root_sec(server, fhandle, info,
4053 flav_array[i]);
4054 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
4055 continue;
4056 break;
4057 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00004058 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04004059
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04004060 /*
Colin Ian Kingd3787af2018-10-26 19:10:31 +01004061 * -EACCES could mean that the user doesn't have correct permissions
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04004062 * to access the mount. It could also mean that we tried to mount
4063 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
4064 * existing mount programs don't handle -EACCES very well so it should
4065 * be mapped to -EPERM instead.
4066 */
4067 if (status == -EACCES)
4068 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04004069 return status;
4070}
4071
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04004072/**
4073 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
4074 * @server: initialized nfs_server handle
4075 * @fhandle: we fill in the pseudo-fs root file handle
4076 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04004077 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04004078 *
4079 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04004080 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04004081int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04004082 struct nfs_fsinfo *info,
4083 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04004084{
Andre Przywarac7757072015-04-23 17:17:40 +01004085 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04004086
Andre Przywarac7757072015-04-23 17:17:40 +01004087 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04004088 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01004089
4090 if (auth_probe || status == NFS4ERR_WRONGSEC)
Trond Myklebust698c9372016-07-25 13:31:14 -04004091 status = server->nfs_client->cl_mvops->find_root_sec(server,
4092 fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04004093
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094 if (status == 0)
4095 status = nfs4_server_capabilities(server, fhandle);
4096 if (status == 0)
4097 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04004098
Trond Myklebustc12e87f2006-03-13 21:20:47 -08004099 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100}
4101
Bryan Schumakerbae36242012-05-10 15:07:31 -04004102static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
4103 struct nfs_fsinfo *info)
4104{
4105 int error;
4106 struct nfs_fattr *fattr = info->fattr;
Scott Mayhew779df6a2020-03-03 17:58:37 -05004107 struct nfs4_label *label = fattr->label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04004108
4109 error = nfs4_server_capabilities(server, mntfh);
4110 if (error < 0) {
4111 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
4112 return error;
4113 }
4114
Trond Myklebusta841b542018-04-07 13:50:59 -04004115 error = nfs4_proc_getattr(server, mntfh, fattr, label, NULL);
Bryan Schumakerbae36242012-05-10 15:07:31 -04004116 if (error < 0) {
4117 dprintk("nfs4_get_root: getattr error = %d\n", -error);
Scott Mayhew779df6a2020-03-03 17:58:37 -05004118 goto out;
Bryan Schumakerbae36242012-05-10 15:07:31 -04004119 }
4120
4121 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
4122 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
4123 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
4124
Scott Mayhew779df6a2020-03-03 17:58:37 -05004125out:
Bryan Schumakerbae36242012-05-10 15:07:31 -04004126 return error;
4127}
4128
Manoj Naik6b97fd32006-06-09 09:34:29 -04004129/*
4130 * Get locations and (maybe) other attributes of a referral.
4131 * Note that we'll actually follow the referral later when
4132 * we detect fsid mismatch in inode revalidation
4133 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004134static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
4135 const struct qstr *name, struct nfs_fattr *fattr,
4136 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04004137{
4138 int status = -ENOMEM;
4139 struct page *page = NULL;
4140 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04004141
4142 page = alloc_page(GFP_KERNEL);
4143 if (page == NULL)
4144 goto out;
4145 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
4146 if (locations == NULL)
4147 goto out;
4148
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004149 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04004150 if (status != 0)
4151 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04004152
4153 /*
4154 * If the fsid didn't change, this is a migration event, not a
4155 * referral. Cause us to drop into the exception handler, which
4156 * will kick off migration recovery.
4157 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04004158 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04004159 dprintk("%s: server did not return a different fsid for"
4160 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04004161 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04004162 goto out;
4163 }
Andy Adamson533eb462011-06-13 18:25:56 -04004164 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
4165 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04004166
Andy Adamson533eb462011-06-13 18:25:56 -04004167 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04004168 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04004169 memset(fhandle, 0, sizeof(struct nfs_fh));
4170out:
4171 if (page)
4172 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04004173 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04004174 return status;
4175}
4176
David Quigley1775fd32013-05-22 12:50:42 -04004177static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebusta841b542018-04-07 13:50:59 -04004178 struct nfs_fattr *fattr, struct nfs4_label *label,
4179 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180{
Trond Myklebust771734f2018-04-07 13:54:23 -04004181 __u32 bitmask[NFS4_BITMASK_SZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182 struct nfs4_getattr_arg args = {
4183 .fh = fhandle,
Trond Myklebust771734f2018-04-07 13:54:23 -04004184 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185 };
4186 struct nfs4_getattr_res res = {
4187 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04004188 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189 .server = server,
4190 };
4191 struct rpc_message msg = {
4192 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
4193 .rpc_argp = &args,
4194 .rpc_resp = &res,
4195 };
Trond Myklebustc74dfe92020-01-06 15:39:37 -05004196 unsigned short task_flags = 0;
4197
Olga Kornievskaia85e39fe2021-06-23 23:28:51 -04004198 if (nfs4_has_session(server->nfs_client))
4199 task_flags = RPC_TASK_MOVEABLE;
4200
Trond Myklebustc74dfe92020-01-06 15:39:37 -05004201 /* Is this is an attribute revalidation, subject to softreval? */
4202 if (inode && (server->flags & NFS_MOUNT_SOFTREVAL))
4203 task_flags |= RPC_TASK_TIMEOUT;
David Quigleyaa9c2662013-05-22 12:50:44 -04004204
Trond Myklebusta71029b2021-04-10 00:23:03 -04004205 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, label), inode, 0);
Trond Myklebust0e574af2005-10-27 22:12:38 -04004206 nfs_fattr_init(fattr);
Trond Myklebustc74dfe92020-01-06 15:39:37 -05004207 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
4208 return nfs4_do_call_sync(server->client, server, &msg,
4209 &args.seq_args, &res.seq_res, task_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210}
4211
Olga Kornievskaiaec4b0922019-10-08 16:33:53 -04004212int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebusta841b542018-04-07 13:50:59 -04004213 struct nfs_fattr *fattr, struct nfs4_label *label,
4214 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215{
Trond Myklebust0688e642019-04-07 13:59:09 -04004216 struct nfs4_exception exception = {
4217 .interruptible = true,
4218 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219 int err;
4220 do {
Trond Myklebusta841b542018-04-07 13:50:59 -04004221 err = _nfs4_proc_getattr(server, fhandle, fattr, label, inode);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004222 trace_nfs4_getattr(server, fhandle, fattr, err);
4223 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224 &exception);
4225 } while (exception.retry);
4226 return err;
4227}
4228
4229/*
4230 * The file is not closed if it is opened due to the a request to change
4231 * the size of the file. The open call will not be needed once the
4232 * VFS layer lookup-intents are implemented.
4233 *
4234 * Close is called when the inode is destroyed.
4235 * If we haven't opened the file for O_WRONLY, we
4236 * need to in the size_change case to obtain a stateid.
4237 *
4238 * Got race?
4239 * Because OPEN is always done by name in nfsv4, it is
4240 * possible that we opened a different file by the same
4241 * name. We can recognize this race condition, but we
4242 * can't do anything about it besides returning an error.
4243 *
4244 * This will be fixed with VFS changes (lookup-intent).
4245 */
4246static int
4247nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
4248 struct iattr *sattr)
4249{
David Howells2b0143b2015-03-17 22:25:59 +00004250 struct inode *inode = d_inode(dentry);
NeilBrowna52458b2018-12-03 11:30:31 +11004251 const struct cred *cred = NULL;
NeilBrown29b59f92016-10-13 15:26:47 +11004252 struct nfs_open_context *ctx = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04004253 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254 int status;
4255
Peng Tao88ac8152014-09-12 11:04:10 +08004256 if (pnfs_ld_layoutret_on_setattr(inode) &&
4257 sattr->ia_valid & ATTR_SIZE &&
4258 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04004259 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04004260
Trond Myklebust0e574af2005-10-27 22:12:38 -04004261 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262
Andy Adamson26699402012-05-30 16:12:24 -04004263 /* Deal with open(O_TRUNC) */
4264 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03004265 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04004266
4267 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03004268 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04004269 return 0;
4270
Trond Myklebustd5308382005-11-04 15:33:38 -05004271 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04004272 if (sattr->ia_valid & ATTR_FILE) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00004273
Trond Myklebust659bfcd2008-06-10 19:39:41 -04004274 ctx = nfs_file_open_context(sattr->ia_file);
NeilBrown29b59f92016-10-13 15:26:47 +11004275 if (ctx)
Neil Brown504e5182008-10-16 14:15:16 +11004276 cred = ctx->cred;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04004277 }
4278
David Quigley14c43f72013-05-22 12:50:43 -04004279 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4280 if (IS_ERR(label))
4281 return PTR_ERR(label);
4282
Trond Myklebust199366f2018-03-20 16:43:18 -04004283 /* Return any delegations if we're going to change ACLs */
4284 if ((sattr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
Trond Myklebustc01d3642018-03-20 16:43:20 -04004285 nfs4_inode_make_writeable(inode);
Trond Myklebust199366f2018-03-20 16:43:18 -04004286
NeilBrown29b59f92016-10-13 15:26:47 +11004287 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04004288 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05004289 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04004290 nfs_setsecurity(inode, fattr, label);
4291 }
David Quigley14c43f72013-05-22 12:50:43 -04004292 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293 return status;
4294}
4295
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004296static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004297 struct dentry *dentry, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04004298 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04004299{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004300 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04004301 int status;
4302 struct nfs4_lookup_arg args = {
4303 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004304 .dir_fh = NFS_FH(dir),
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004305 .name = &dentry->d_name,
David Howells2b3de442006-08-22 20:06:09 -04004306 };
4307 struct nfs4_lookup_res res = {
4308 .server = server,
4309 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04004310 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04004311 .fh = fhandle,
4312 };
4313 struct rpc_message msg = {
4314 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
4315 .rpc_argp = &args,
4316 .rpc_resp = &res,
4317 };
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004318 unsigned short task_flags = 0;
4319
Olga Kornievskaia85e39fe2021-06-23 23:28:51 -04004320 if (server->nfs_client->cl_minorversion)
4321 task_flags = RPC_TASK_MOVEABLE;
4322
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004323 /* Is this is an attribute revalidation, subject to softreval? */
4324 if (nfs_lookup_is_soft_revalidate(dentry))
4325 task_flags |= RPC_TASK_TIMEOUT;
David Howells2b3de442006-08-22 20:06:09 -04004326
David Quigleyaa9c2662013-05-22 12:50:44 -04004327 args.bitmask = nfs4_bitmask(server, label);
4328
David Howells2b3de442006-08-22 20:06:09 -04004329 nfs_fattr_init(fattr);
4330
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004331 dprintk("NFS call lookup %pd2\n", dentry);
4332 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
4333 status = nfs4_do_call_sync(clnt, server, &msg,
4334 &args.seq_args, &res.seq_res, task_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335 dprintk("NFS reply lookup: %d\n", status);
4336 return status;
4337}
4338
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004339static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00004340{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00004341 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004342 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00004343 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4344 fattr->nlink = 2;
4345}
4346
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004347static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004348 struct dentry *dentry, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04004349 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004350{
Trond Myklebust0688e642019-04-07 13:59:09 -04004351 struct nfs4_exception exception = {
4352 .interruptible = true,
4353 };
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004354 struct rpc_clnt *client = *clnt;
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004355 const struct qstr *name = &dentry->d_name;
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004356 int err;
4357 do {
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004358 err = _nfs4_proc_lookup(client, dir, dentry, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004359 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004360 switch (err) {
4361 case -NFS4ERR_BADNAME:
4362 err = -ENOENT;
4363 goto out;
4364 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004365 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02004366 if (err == -NFS4ERR_MOVED)
4367 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004368 goto out;
4369 case -NFS4ERR_WRONGSEC:
4370 err = -EPERM;
4371 if (client != *clnt)
4372 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04004373 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004374 if (IS_ERR(client))
4375 return PTR_ERR(client);
4376
4377 exception.retry = 1;
4378 break;
4379 default:
4380 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4381 }
4382 } while (exception.retry);
4383
4384out:
4385 if (err == 0)
4386 *clnt = client;
4387 else if (client != *clnt)
4388 rpc_shutdown_client(client);
4389
4390 return err;
4391}
4392
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004393static int nfs4_proc_lookup(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004394 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4395 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004397 int status;
4398 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004399
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004400 status = nfs4_proc_lookup_common(&client, dir, dentry, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004401 if (client != NFS_CLIENT(dir)) {
4402 rpc_shutdown_client(client);
4403 nfs_fixup_secinfo_attributes(fattr);
4404 }
4405 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004406}
4407
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004408struct rpc_clnt *
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004409nfs4_proc_lookup_mountpoint(struct inode *dir, struct dentry *dentry,
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004410 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4411{
Trond Myklebustb72888c2013-08-07 20:38:07 -04004412 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004413 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004414
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004415 status = nfs4_proc_lookup_common(&client, dir, dentry, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04004416 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004417 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04004418 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004419}
4420
Jeff Layton5b5faaf2017-06-29 06:34:52 -07004421static int _nfs4_proc_lookupp(struct inode *inode,
4422 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4423 struct nfs4_label *label)
4424{
4425 struct rpc_clnt *clnt = NFS_CLIENT(inode);
4426 struct nfs_server *server = NFS_SERVER(inode);
4427 int status;
4428 struct nfs4_lookupp_arg args = {
4429 .bitmask = server->attr_bitmask,
4430 .fh = NFS_FH(inode),
4431 };
4432 struct nfs4_lookupp_res res = {
4433 .server = server,
4434 .fattr = fattr,
4435 .label = label,
4436 .fh = fhandle,
4437 };
4438 struct rpc_message msg = {
4439 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
4440 .rpc_argp = &args,
4441 .rpc_resp = &res,
4442 };
Trond Myklebust76998eb2020-10-20 14:30:35 -04004443 unsigned short task_flags = 0;
4444
4445 if (NFS_SERVER(inode)->flags & NFS_MOUNT_SOFTREVAL)
4446 task_flags |= RPC_TASK_TIMEOUT;
Jeff Layton5b5faaf2017-06-29 06:34:52 -07004447
4448 args.bitmask = nfs4_bitmask(server, label);
4449
4450 nfs_fattr_init(fattr);
4451
4452 dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino);
4453 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
Trond Myklebust76998eb2020-10-20 14:30:35 -04004454 &res.seq_res, task_flags);
Jeff Layton5b5faaf2017-06-29 06:34:52 -07004455 dprintk("NFS reply lookupp: %d\n", status);
4456 return status;
4457}
4458
4459static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
4460 struct nfs_fattr *fattr, struct nfs4_label *label)
4461{
Trond Myklebust0688e642019-04-07 13:59:09 -04004462 struct nfs4_exception exception = {
4463 .interruptible = true,
4464 };
Jeff Layton5b5faaf2017-06-29 06:34:52 -07004465 int err;
4466 do {
4467 err = _nfs4_proc_lookupp(inode, fhandle, fattr, label);
4468 trace_nfs4_lookupp(inode, err);
4469 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4470 &exception);
4471 } while (exception.retry);
4472 return err;
4473}
4474
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4476{
Trond Myklebust76b32992007-08-10 17:45:11 -04004477 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478 struct nfs4_accessargs args = {
4479 .fh = NFS_FH(inode),
Anna Schumaker1750d922017-07-26 12:00:21 -04004480 .access = entry->mask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481 };
Trond Myklebust76b32992007-08-10 17:45:11 -04004482 struct nfs4_accessres res = {
4483 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04004484 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004485 struct rpc_message msg = {
4486 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
4487 .rpc_argp = &args,
4488 .rpc_resp = &res,
4489 .rpc_cred = entry->cred,
4490 };
David Quigleyaa9c2662013-05-22 12:50:44 -04004491 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492
Trond Myklebust7c672652018-06-04 15:00:53 -04004493 if (!nfs4_have_delegation(inode, FMODE_READ)) {
Trond Myklebust8bcbe7d2018-03-20 17:03:11 -04004494 res.fattr = nfs_alloc_fattr();
4495 if (res.fattr == NULL)
4496 return -ENOMEM;
4497 args.bitmask = server->cache_consistency_bitmask;
4498 }
Bryan Schumaker7c513052011-03-24 17:12:24 +00004499 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04004501 nfs_access_set_mask(entry, res.access);
Trond Myklebust8bcbe7d2018-03-20 17:03:11 -04004502 if (res.fattr)
4503 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004504 }
Trond Myklebustc407d412010-04-16 16:22:48 -04004505 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506 return status;
4507}
4508
4509static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4510{
Trond Myklebust0688e642019-04-07 13:59:09 -04004511 struct nfs4_exception exception = {
4512 .interruptible = true,
4513 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514 int err;
4515 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004516 err = _nfs4_proc_access(inode, entry);
4517 trace_nfs4_access(inode, err);
4518 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519 &exception);
4520 } while (exception.retry);
4521 return err;
4522}
4523
4524/*
4525 * TODO: For the time being, we don't try to get any attributes
4526 * along with any of the zero-copy operations READ, READDIR,
4527 * READLINK, WRITE.
4528 *
4529 * In the case of the first three, we want to put the GETATTR
4530 * after the read-type operation -- this is because it is hard
4531 * to predict the length of a GETATTR response in v4, and thus
4532 * align the READ data correctly. This means that the GETATTR
4533 * may end up partially falling into the page cache, and we should
4534 * shift it into the 'tail' of the xdr_buf before processing.
4535 * To do this efficiently, we need to know the total length
4536 * of data received, which doesn't seem to be available outside
4537 * of the RPC layer.
4538 *
4539 * In the case of WRITE, we also want to put the GETATTR after
4540 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04004541 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542 *
4543 * Both of these changes to the XDR layer would in fact be quite
4544 * minor, but I decided to leave them for a subsequent patch.
4545 */
4546static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4547 unsigned int pgbase, unsigned int pglen)
4548{
4549 struct nfs4_readlink args = {
4550 .fh = NFS_FH(inode),
4551 .pgbase = pgbase,
4552 .pglen = pglen,
4553 .pages = &page,
4554 };
Benny Halevyf50c7002009-04-01 09:21:55 -04004555 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004556 struct rpc_message msg = {
4557 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4558 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04004559 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560 };
4561
Bryan Schumaker7c513052011-03-24 17:12:24 +00004562 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 -07004563}
4564
4565static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4566 unsigned int pgbase, unsigned int pglen)
4567{
Trond Myklebust0688e642019-04-07 13:59:09 -04004568 struct nfs4_exception exception = {
4569 .interruptible = true,
4570 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004571 int err;
4572 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004573 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4574 trace_nfs4_readlink(inode, err);
4575 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576 &exception);
4577 } while (exception.retry);
4578 return err;
4579}
4580
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004582 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584static int
4585nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004586 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004587{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004588 struct nfs_server *server = NFS_SERVER(dir);
David Quigleyaa9c2662013-05-22 12:50:44 -04004589 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004590 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592 int status = 0;
4593
NeilBrown532d4de2016-10-13 15:26:47 +11004594 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004595 if (IS_ERR(ctx))
4596 return PTR_ERR(ctx);
4597
David Quigleyaa9c2662013-05-22 12:50:44 -04004598 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4599
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004600 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4601 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08004602 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603 if (IS_ERR(state)) {
4604 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04004605 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004607out:
David Quigleyaa9c2662013-05-22 12:50:44 -04004608 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004609 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610 return status;
4611}
4612
Trond Myklebust3c591172018-07-31 15:54:10 -04004613static int
4614_nfs4_proc_remove(struct inode *dir, const struct qstr *name, u32 ftype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615{
Trond Myklebust16e42952005-10-27 22:12:44 -04004616 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004617 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07004619 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004621 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04004622 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04004623 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004625 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4626 .rpc_argp = &args,
4627 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004628 };
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004629 unsigned long timestamp = jiffies;
Trond Myklebust778d2812012-04-27 13:48:19 -04004630 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04004631
Bryan Schumaker7c513052011-03-24 17:12:24 +00004632 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust3c591172018-07-31 15:54:10 -04004633 if (status == 0) {
4634 spin_lock(&dir->i_lock);
Trond Myklebust3c591172018-07-31 15:54:10 -04004635 /* Removing a directory decrements nlink in the parent */
4636 if (ftype == NF4DIR && dir->i_nlink > 2)
4637 nfs4_dec_nlink_locked(dir);
Trond Myklebust82eae5a2021-04-01 14:59:59 -04004638 nfs4_update_changeattr_locked(dir, &res.cinfo, timestamp,
4639 NFS_INO_INVALID_DATA);
Trond Myklebust3c591172018-07-31 15:54:10 -04004640 spin_unlock(&dir->i_lock);
4641 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642 return status;
4643}
4644
Trond Myklebust912678d2018-03-20 16:43:15 -04004645static int nfs4_proc_remove(struct inode *dir, struct dentry *dentry)
4646{
Trond Myklebust0688e642019-04-07 13:59:09 -04004647 struct nfs4_exception exception = {
4648 .interruptible = true,
4649 };
Trond Myklebust912678d2018-03-20 16:43:15 -04004650 struct inode *inode = d_inode(dentry);
4651 int err;
4652
Trond Myklebustc01d3642018-03-20 16:43:20 -04004653 if (inode) {
4654 if (inode->i_nlink == 1)
4655 nfs4_inode_return_delegation(inode);
4656 else
4657 nfs4_inode_make_writeable(inode);
4658 }
Trond Myklebust912678d2018-03-20 16:43:15 -04004659 do {
Trond Myklebust3c591172018-07-31 15:54:10 -04004660 err = _nfs4_proc_remove(dir, &dentry->d_name, NF4REG);
Trond Myklebust912678d2018-03-20 16:43:15 -04004661 trace_nfs4_remove(dir, &dentry->d_name, err);
4662 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4663 &exception);
4664 } while (exception.retry);
4665 return err;
4666}
4667
4668static int nfs4_proc_rmdir(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669{
Trond Myklebust0688e642019-04-07 13:59:09 -04004670 struct nfs4_exception exception = {
4671 .interruptible = true,
4672 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673 int err;
Trond Myklebust912678d2018-03-20 16:43:15 -04004674
Linus Torvalds1da177e2005-04-16 15:20:36 -07004675 do {
Trond Myklebust3c591172018-07-31 15:54:10 -04004676 err = _nfs4_proc_remove(dir, name, NF4DIR);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004677 trace_nfs4_remove(dir, name, err);
4678 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679 &exception);
4680 } while (exception.retry);
4681 return err;
4682}
4683
Trond Myklebusted7e9ad2018-05-30 16:11:52 -04004684static void nfs4_proc_unlink_setup(struct rpc_message *msg,
4685 struct dentry *dentry,
4686 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004688 struct nfs_removeargs *args = msg->rpc_argp;
4689 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690
Trond Myklebust977fcc22018-03-20 16:43:17 -04004691 res->server = NFS_SB(dentry->d_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004692 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Anna Schumakerfba83f32018-05-04 16:22:50 -04004693 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04004694
4695 nfs_fattr_init(res->dir_attr);
Trond Myklebust977fcc22018-03-20 16:43:17 -04004696
4697 if (inode)
4698 nfs4_inode_return_delegation(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004699}
4700
Bryan Schumaker34e137c2012-03-19 14:54:41 -04004701static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4702{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004703 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004704 &data->args.seq_args,
4705 &data->res.seq_res,
4706 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707}
4708
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004709static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004711 struct nfs_unlinkdata *data = task->tk_calldata;
4712 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004713
Trond Myklebust14516c32010-07-31 14:29:06 -04004714 if (!nfs4_sequence_done(task, &res->seq_res))
4715 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004716 if (nfs4_async_handle_error(task, res->server, NULL,
4717 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004718 return 0;
Trond Myklebustc40d52f2017-01-11 12:36:11 -05004719 if (task->tk_status == 0)
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004720 nfs4_update_changeattr(dir, &res->cinfo,
4721 res->dir_attr->time_start,
4722 NFS_INO_INVALID_DATA);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004723 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004724}
4725
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004726static void nfs4_proc_rename_setup(struct rpc_message *msg,
4727 struct dentry *old_dentry,
4728 struct dentry *new_dentry)
Jeff Laytond3d41522010-09-17 17:31:57 -04004729{
Jeff Laytond3d41522010-09-17 17:31:57 -04004730 struct nfs_renameargs *arg = msg->rpc_argp;
4731 struct nfs_renameres *res = msg->rpc_resp;
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004732 struct inode *old_inode = d_inode(old_dentry);
4733 struct inode *new_inode = d_inode(new_dentry);
Jeff Laytond3d41522010-09-17 17:31:57 -04004734
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004735 if (old_inode)
Trond Myklebustc01d3642018-03-20 16:43:20 -04004736 nfs4_inode_make_writeable(old_inode);
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004737 if (new_inode)
4738 nfs4_inode_return_delegation(new_inode);
Jeff Laytond3d41522010-09-17 17:31:57 -04004739 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004740 res->server = NFS_SB(old_dentry->d_sb);
Anna Schumakerfba83f32018-05-04 16:22:50 -04004741 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1, 0);
Jeff Laytond3d41522010-09-17 17:31:57 -04004742}
4743
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04004744static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4745{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004746 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004747 &data->args.seq_args,
4748 &data->res.seq_res,
4749 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04004750}
4751
4752static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4753 struct inode *new_dir)
4754{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04004755 struct nfs_renamedata *data = task->tk_calldata;
4756 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04004757
4758 if (!nfs4_sequence_done(task, &res->seq_res))
4759 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004760 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04004761 return 0;
4762
Trond Myklebustc733c492017-01-11 12:32:26 -05004763 if (task->tk_status == 0) {
Trond Myklebust5636ec42018-07-31 15:54:11 -04004764 if (new_dir != old_dir) {
4765 /* Note: If we moved a directory, nlink will change */
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004766 nfs4_update_changeattr(old_dir, &res->old_cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04004767 res->old_fattr->time_start,
Trond Myklebustfabf2b32021-03-25 13:14:42 -04004768 NFS_INO_INVALID_NLINK |
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004769 NFS_INO_INVALID_DATA);
4770 nfs4_update_changeattr(new_dir, &res->new_cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04004771 res->new_fattr->time_start,
Trond Myklebustfabf2b32021-03-25 13:14:42 -04004772 NFS_INO_INVALID_NLINK |
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004773 NFS_INO_INVALID_DATA);
Trond Myklebust5636ec42018-07-31 15:54:11 -04004774 } else
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004775 nfs4_update_changeattr(old_dir, &res->old_cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04004776 res->old_fattr->time_start,
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004777 NFS_INO_INVALID_DATA);
Trond Myklebustc733c492017-01-11 12:32:26 -05004778 }
Jeff Laytond3d41522010-09-17 17:31:57 -04004779 return 1;
4780}
4781
Al Virobeffb8f2016-07-20 16:34:42 -04004782static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004784 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebust2f28dc32018-04-08 21:06:40 -04004785 __u32 bitmask[NFS4_BITMASK_SZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786 struct nfs4_link_arg arg = {
4787 .fh = NFS_FH(inode),
4788 .dir_fh = NFS_FH(dir),
4789 .name = name,
Trond Myklebust2f28dc32018-04-08 21:06:40 -04004790 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004792 struct nfs4_link_res res = {
4793 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04004794 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004795 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796 struct rpc_message msg = {
4797 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4798 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004799 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800 };
Trond Myklebust136f2622010-04-16 16:22:49 -04004801 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004802
Trond Myklebust136f2622010-04-16 16:22:49 -04004803 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04004804 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04004805 goto out;
4806
David Quigley14c43f72013-05-22 12:50:43 -04004807 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4808 if (IS_ERR(res.label)) {
4809 status = PTR_ERR(res.label);
4810 goto out;
4811 }
4812
Trond Myklebustc01d3642018-03-20 16:43:20 -04004813 nfs4_inode_make_writeable(inode);
Trond Myklebusta71029b2021-04-10 00:23:03 -04004814 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, res.label), inode,
4815 NFS_INO_INVALID_CHANGE);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004816 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004817 if (!status) {
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004818 nfs4_update_changeattr(dir, &res.cinfo, res.fattr->time_start,
4819 NFS_INO_INVALID_DATA);
Trond Myklebust1301e422021-04-01 14:57:56 -04004820 nfs4_inc_nlink(inode);
David Quigleyaa9c2662013-05-22 12:50:44 -04004821 status = nfs_post_op_update_inode(inode, res.fattr);
4822 if (!status)
4823 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004824 }
David Quigley14c43f72013-05-22 12:50:43 -04004825
4826
4827 nfs4_label_free(res.label);
4828
Trond Myklebust136f2622010-04-16 16:22:49 -04004829out:
Trond Myklebust136f2622010-04-16 16:22:49 -04004830 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831 return status;
4832}
4833
Al Virobeffb8f2016-07-20 16:34:42 -04004834static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004835{
Trond Myklebust0688e642019-04-07 13:59:09 -04004836 struct nfs4_exception exception = {
4837 .interruptible = true,
4838 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004839 int err;
4840 do {
4841 err = nfs4_handle_exception(NFS_SERVER(inode),
4842 _nfs4_proc_link(inode, dir, name),
4843 &exception);
4844 } while (exception.retry);
4845 return err;
4846}
4847
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004848struct nfs4_createdata {
4849 struct rpc_message msg;
4850 struct nfs4_create_arg arg;
4851 struct nfs4_create_res res;
4852 struct nfs_fh fh;
4853 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004854 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004855};
4856
4857static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004858 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004859{
4860 struct nfs4_createdata *data;
4861
4862 data = kzalloc(sizeof(*data), GFP_KERNEL);
4863 if (data != NULL) {
4864 struct nfs_server *server = NFS_SERVER(dir);
4865
David Quigley14c43f72013-05-22 12:50:43 -04004866 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4867 if (IS_ERR(data->label))
4868 goto out_free;
4869
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004870 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4871 data->msg.rpc_argp = &data->arg;
4872 data->msg.rpc_resp = &data->res;
4873 data->arg.dir_fh = NFS_FH(dir);
4874 data->arg.server = server;
4875 data->arg.name = name;
4876 data->arg.attrs = sattr;
4877 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04004878 data->arg.bitmask = nfs4_bitmask(server, data->label);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004879 data->arg.umask = current_umask();
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004880 data->res.server = server;
4881 data->res.fh = &data->fh;
4882 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004883 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004884 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004885 }
4886 return data;
David Quigley14c43f72013-05-22 12:50:43 -04004887out_free:
4888 kfree(data);
4889 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004890}
4891
4892static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4893{
Bryan Schumaker7c513052011-03-24 17:12:24 +00004894 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00004895 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004896 if (status == 0) {
Trond Myklebust3c591172018-07-31 15:54:10 -04004897 spin_lock(&dir->i_lock);
Trond Myklebust3c591172018-07-31 15:54:10 -04004898 /* Creating a directory bumps nlink in the parent */
4899 if (data->arg.ftype == NF4DIR)
4900 nfs4_inc_nlink_locked(dir);
Trond Myklebust82eae5a2021-04-01 14:59:59 -04004901 nfs4_update_changeattr_locked(dir, &data->res.dir_cinfo,
4902 data->res.fattr->time_start,
4903 NFS_INO_INVALID_DATA);
Trond Myklebust3c591172018-07-31 15:54:10 -04004904 spin_unlock(&dir->i_lock);
David Quigley1775fd32013-05-22 12:50:42 -04004905 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004906 }
4907 return status;
4908}
4909
4910static void nfs4_free_createdata(struct nfs4_createdata *data)
4911{
David Quigley14c43f72013-05-22 12:50:43 -04004912 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004913 kfree(data);
4914}
4915
Chuck Lever4f390c12006-08-22 20:06:22 -04004916static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004917 struct page *page, unsigned int len, struct iattr *sattr,
4918 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004919{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004920 struct nfs4_createdata *data;
4921 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004922
Chuck Lever94a6d752006-08-22 20:06:23 -04004923 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004924 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04004925
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004926 status = -ENOMEM;
4927 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4928 if (data == NULL)
4929 goto out;
4930
4931 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4932 data->arg.u.symlink.pages = &page;
4933 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004934 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004935
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004936 status = nfs4_do_create(dir, dentry, data);
4937
4938 nfs4_free_createdata(data);
4939out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004940 return status;
4941}
4942
Chuck Lever4f390c12006-08-22 20:06:22 -04004943static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004944 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945{
Trond Myklebust0688e642019-04-07 13:59:09 -04004946 struct nfs4_exception exception = {
4947 .interruptible = true,
4948 };
David Quigleyaa9c2662013-05-22 12:50:44 -04004949 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004950 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004951
4952 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4953
Linus Torvalds1da177e2005-04-16 15:20:36 -07004954 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004955 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4956 trace_nfs4_symlink(dir, &dentry->d_name, err);
4957 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004958 &exception);
4959 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004960
4961 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962 return err;
4963}
4964
4965static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004966 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004968 struct nfs4_createdata *data;
4969 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004970
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004971 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4972 if (data == NULL)
4973 goto out;
4974
David Quigley1775fd32013-05-22 12:50:42 -04004975 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004976 status = nfs4_do_create(dir, dentry, data);
4977
4978 nfs4_free_createdata(data);
4979out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980 return status;
4981}
4982
4983static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4984 struct iattr *sattr)
4985{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004986 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust0688e642019-04-07 13:59:09 -04004987 struct nfs4_exception exception = {
4988 .interruptible = true,
4989 };
David Quigleyaa9c2662013-05-22 12:50:44 -04004990 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004992
David Quigleyaa9c2662013-05-22 12:50:44 -04004993 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4994
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004995 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4996 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004997 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004998 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4999 trace_nfs4_mkdir(dir, &dentry->d_name, err);
5000 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005001 &exception);
5002 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04005003 nfs4_label_release_security(label);
5004
Linus Torvalds1da177e2005-04-16 15:20:36 -07005005 return err;
5006}
5007
Trond Myklebust82e22a52020-11-02 17:34:23 -05005008static int _nfs4_proc_readdir(struct nfs_readdir_arg *nr_arg,
5009 struct nfs_readdir_res *nr_res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005010{
Trond Myklebust82e22a52020-11-02 17:34:23 -05005011 struct inode *dir = d_inode(nr_arg->dentry);
Olga Kornievskaia05ad9172020-11-06 16:03:38 -05005012 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005013 struct nfs4_readdir_arg args = {
5014 .fh = NFS_FH(dir),
Trond Myklebust82e22a52020-11-02 17:34:23 -05005015 .pages = nr_arg->pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016 .pgbase = 0,
Trond Myklebust82e22a52020-11-02 17:34:23 -05005017 .count = nr_arg->page_len,
5018 .plus = nr_arg->plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005019 };
5020 struct nfs4_readdir_res res;
5021 struct rpc_message msg = {
5022 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
5023 .rpc_argp = &args,
5024 .rpc_resp = &res,
Trond Myklebust82e22a52020-11-02 17:34:23 -05005025 .rpc_cred = nr_arg->cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005026 };
5027 int status;
5028
Trond Myklebust82e22a52020-11-02 17:34:23 -05005029 dprintk("%s: dentry = %pd2, cookie = %llu\n", __func__,
5030 nr_arg->dentry, (unsigned long long)nr_arg->cookie);
Olga Kornievskaia05ad9172020-11-06 16:03:38 -05005031 if (!(server->caps & NFS_CAP_SECURITY_LABEL))
5032 args.bitmask = server->attr_bitmask_nl;
5033 else
5034 args.bitmask = server->attr_bitmask;
5035
Trond Myklebust82e22a52020-11-02 17:34:23 -05005036 nfs4_setup_readdir(nr_arg->cookie, nr_arg->verf, nr_arg->dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005037 res.pgbase = args.pgbase;
Olga Kornievskaia05ad9172020-11-06 16:03:38 -05005038 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
5039 &res.seq_res, 0);
Trond Myklebustac396122010-11-15 20:26:22 -05005040 if (status >= 0) {
Trond Myklebust82e22a52020-11-02 17:34:23 -05005041 memcpy(nr_res->verf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05005042 status += args.pgbase;
5043 }
Trond Myklebustc4812992007-09-28 17:11:45 -04005044
5045 nfs_invalidate_atime(dir);
5046
Harvey Harrison3110ff82008-05-02 13:42:44 -07005047 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005048 return status;
5049}
5050
Trond Myklebust82e22a52020-11-02 17:34:23 -05005051static int nfs4_proc_readdir(struct nfs_readdir_arg *arg,
5052 struct nfs_readdir_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005053{
Trond Myklebust0688e642019-04-07 13:59:09 -04005054 struct nfs4_exception exception = {
5055 .interruptible = true,
5056 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005057 int err;
5058 do {
Trond Myklebust82e22a52020-11-02 17:34:23 -05005059 err = _nfs4_proc_readdir(arg, res);
5060 trace_nfs4_readdir(d_inode(arg->dentry), err);
5061 err = nfs4_handle_exception(NFS_SERVER(d_inode(arg->dentry)),
5062 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005063 } while (exception.retry);
5064 return err;
5065}
5066
5067static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04005068 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04005070 struct nfs4_createdata *data;
5071 int mode = sattr->ia_mode;
5072 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005073
Trond Myklebust57dc9a52008-06-20 15:35:32 -04005074 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
5075 if (data == NULL)
5076 goto out;
5077
Linus Torvalds1da177e2005-04-16 15:20:36 -07005078 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04005079 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005080 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04005081 data->arg.ftype = NF4BLK;
5082 data->arg.u.device.specdata1 = MAJOR(rdev);
5083 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005084 }
5085 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04005086 data->arg.ftype = NF4CHR;
5087 data->arg.u.device.specdata1 = MAJOR(rdev);
5088 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04005089 } else if (!S_ISSOCK(mode)) {
5090 status = -EINVAL;
5091 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005092 }
David Quigley1775fd32013-05-22 12:50:42 -04005093
David Quigleyaa9c2662013-05-22 12:50:44 -04005094 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04005095 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04005096out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04005097 nfs4_free_createdata(data);
5098out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005099 return status;
5100}
5101
5102static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
5103 struct iattr *sattr, dev_t rdev)
5104{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05005105 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust0688e642019-04-07 13:59:09 -04005106 struct nfs4_exception exception = {
5107 .interruptible = true,
5108 };
David Quigleyaa9c2662013-05-22 12:50:44 -04005109 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005110 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00005111
David Quigleyaa9c2662013-05-22 12:50:44 -04005112 label = nfs4_label_init_security(dir, dentry, sattr, &l);
5113
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05005114 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
5115 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005116 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04005117 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
5118 trace_nfs4_mknod(dir, &dentry->d_name, err);
5119 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005120 &exception);
5121 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04005122
5123 nfs4_label_release_security(label);
5124
Linus Torvalds1da177e2005-04-16 15:20:36 -07005125 return err;
5126}
5127
5128static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
5129 struct nfs_fsstat *fsstat)
5130{
5131 struct nfs4_statfs_arg args = {
5132 .fh = fhandle,
5133 .bitmask = server->attr_bitmask,
5134 };
Benny Halevy24ad1482009-04-01 09:21:56 -04005135 struct nfs4_statfs_res res = {
5136 .fsstat = fsstat,
5137 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005138 struct rpc_message msg = {
5139 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
5140 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04005141 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005142 };
5143
Trond Myklebust0e574af2005-10-27 22:12:38 -04005144 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005145 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005146}
5147
5148static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
5149{
Trond Myklebust0688e642019-04-07 13:59:09 -04005150 struct nfs4_exception exception = {
5151 .interruptible = true,
5152 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005153 int err;
5154 do {
5155 err = nfs4_handle_exception(server,
5156 _nfs4_proc_statfs(server, fhandle, fsstat),
5157 &exception);
5158 } while (exception.retry);
5159 return err;
5160}
5161
5162static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
5163 struct nfs_fsinfo *fsinfo)
5164{
5165 struct nfs4_fsinfo_arg args = {
5166 .fh = fhandle,
5167 .bitmask = server->attr_bitmask,
5168 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04005169 struct nfs4_fsinfo_res res = {
5170 .fsinfo = fsinfo,
5171 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005172 struct rpc_message msg = {
5173 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
5174 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04005175 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005176 };
5177
Bryan Schumaker7c513052011-03-24 17:12:24 +00005178 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005179}
5180
5181static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5182{
Trond Myklebust0688e642019-04-07 13:59:09 -04005183 struct nfs4_exception exception = {
5184 .interruptible = true,
5185 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005186 int err;
5187
5188 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04005189 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04005190 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04005191 if (err == 0) {
Robert Milkowski7dc29932020-01-30 09:43:25 +00005192 nfs4_set_lease_period(server->nfs_client, fsinfo->lease_time * HZ);
Chuck Lever83ca7f52013-03-16 15:55:53 -04005193 break;
5194 }
5195 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005196 } while (exception.retry);
5197 return err;
5198}
5199
5200static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5201{
Bryan Schumakere38eb652012-06-20 15:53:40 -04005202 int error;
5203
Trond Myklebust0e574af2005-10-27 22:12:38 -04005204 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04005205 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08005206 if (error == 0) {
5207 /* block layout checks this! */
5208 server->pnfs_blksize = fsinfo->blksize;
Jeff Laytonca440c32016-09-15 14:40:49 -04005209 set_pnfs_layoutdriver(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08005210 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04005211
5212 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005213}
5214
5215static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5216 struct nfs_pathconf *pathconf)
5217{
5218 struct nfs4_pathconf_arg args = {
5219 .fh = fhandle,
5220 .bitmask = server->attr_bitmask,
5221 };
Benny Halevyd45b2982009-04-01 09:21:58 -04005222 struct nfs4_pathconf_res res = {
5223 .pathconf = pathconf,
5224 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005225 struct rpc_message msg = {
5226 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
5227 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04005228 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005229 };
5230
5231 /* None of the pathconf attributes are mandatory to implement */
5232 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
5233 memset(pathconf, 0, sizeof(*pathconf));
5234 return 0;
5235 }
5236
Trond Myklebust0e574af2005-10-27 22:12:38 -04005237 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005238 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005239}
5240
5241static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5242 struct nfs_pathconf *pathconf)
5243{
Trond Myklebust0688e642019-04-07 13:59:09 -04005244 struct nfs4_exception exception = {
5245 .interruptible = true,
5246 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005247 int err;
5248
5249 do {
5250 err = nfs4_handle_exception(server,
5251 _nfs4_proc_pathconf(server, fhandle, pathconf),
5252 &exception);
5253 } while (exception.retry);
5254 return err;
5255}
5256
Trond Myklebust5521abf2013-03-16 20:54:34 -04005257int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04005258 const struct nfs_open_context *ctx,
5259 const struct nfs_lock_context *l_ctx,
5260 fmode_t fmode)
5261{
NeilBrown17393472016-10-13 15:26:47 +11005262 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04005263}
5264EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
5265
Trond Myklebust5521abf2013-03-16 20:54:34 -04005266static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
5267 const struct nfs_open_context *ctx,
5268 const struct nfs_lock_context *l_ctx,
5269 fmode_t fmode)
5270{
Ben Dooksd49dd112019-10-16 17:28:21 +01005271 nfs4_stateid _current_stateid;
Trond Myklebust5521abf2013-03-16 20:54:34 -04005272
Trond Myklebuste1253be2014-03-05 08:44:23 -05005273 /* If the current stateid represents a lost lock, then exit */
Ben Dooksd49dd112019-10-16 17:28:21 +01005274 if (nfs4_set_rw_stateid(&_current_stateid, ctx, l_ctx, fmode) == -EIO)
Trond Myklebuste1253be2014-03-05 08:44:23 -05005275 return true;
Ben Dooksd49dd112019-10-16 17:28:21 +01005276 return nfs4_stateid_match(stateid, &_current_stateid);
Trond Myklebust5521abf2013-03-16 20:54:34 -04005277}
5278
5279static bool nfs4_error_stateid_expired(int err)
5280{
5281 switch (err) {
5282 case -NFS4ERR_DELEG_REVOKED:
5283 case -NFS4ERR_ADMIN_REVOKED:
5284 case -NFS4ERR_BAD_STATEID:
5285 case -NFS4ERR_STALE_STATEID:
5286 case -NFS4ERR_OLD_STATEID:
5287 case -NFS4ERR_OPENMODE:
5288 case -NFS4ERR_EXPIRED:
5289 return true;
5290 }
5291 return false;
5292}
5293
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005294static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005295{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005296 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005297
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005298 trace_nfs4_read(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04005299 if (task->tk_status < 0) {
5300 struct nfs4_exception exception = {
5301 .inode = hdr->inode,
5302 .state = hdr->args.context->state,
5303 .stateid = &hdr->args.stateid,
5304 };
5305 task->tk_status = nfs4_async_handle_exception(task,
5306 server, task->tk_status, &exception);
5307 if (exception.retry) {
5308 rpc_restart_call_prepare(task);
5309 return -EAGAIN;
5310 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005311 }
Trond Myklebust8850df92007-09-28 17:20:07 -04005312
Linus Torvalds1da177e2005-04-16 15:20:36 -07005313 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005314 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05005315 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005316}
5317
Trond Myklebust5521abf2013-03-16 20:54:34 -04005318static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04005319 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04005320{
5321
5322 if (!nfs4_error_stateid_expired(task->tk_status) ||
5323 nfs4_stateid_is_current(&args->stateid,
5324 args->context,
5325 args->lock_context,
5326 FMODE_READ))
5327 return false;
5328 rpc_restart_call_prepare(task);
5329 return true;
5330}
5331
Anna Schumakerc5675522014-05-28 13:41:22 -04005332static bool nfs4_read_plus_not_supported(struct rpc_task *task,
5333 struct nfs_pgio_header *hdr)
5334{
5335 struct nfs_server *server = NFS_SERVER(hdr->inode);
5336 struct rpc_message *msg = &task->tk_msg;
5337
5338 if (msg->rpc_proc == &nfs4_procedures[NFSPROC4_CLNT_READ_PLUS] &&
5339 server->caps & NFS_CAP_READ_PLUS && task->tk_status == -ENOTSUPP) {
5340 server->caps &= ~NFS_CAP_READ_PLUS;
5341 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
5342 rpc_restart_call_prepare(task);
5343 return true;
5344 }
5345 return false;
5346}
5347
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005348static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc72011-03-01 01:34:20 +00005349{
Andy Adamsoncbdabc72011-03-01 01:34:20 +00005350 dprintk("--> %s\n", __func__);
5351
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005352 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc72011-03-01 01:34:20 +00005353 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005354 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04005355 return -EAGAIN;
Anna Schumakerc5675522014-05-28 13:41:22 -04005356 if (nfs4_read_plus_not_supported(task, hdr))
5357 return -EAGAIN;
Trond Myklebustbfc505d2016-09-15 18:26:05 -04005358 if (task->tk_status > 0)
5359 nfs_invalidate_atime(hdr->inode);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005360 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5361 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00005362}
5363
Anna Schumaker21e31402020-12-03 15:18:39 -05005364#if defined CONFIG_NFS_V4_2 && defined CONFIG_NFS_V4_2_READ_PLUS
Trond Myklebust5c3485b2020-12-10 09:34:34 -05005365static void nfs42_read_plus_support(struct nfs_pgio_header *hdr,
5366 struct rpc_message *msg)
Anna Schumakerc5675522014-05-28 13:41:22 -04005367{
Trond Myklebust5c3485b2020-12-10 09:34:34 -05005368 /* Note: We don't use READ_PLUS with pNFS yet */
5369 if (nfs_server_capable(hdr->inode, NFS_CAP_READ_PLUS) && !hdr->ds_clp)
Anna Schumakerc5675522014-05-28 13:41:22 -04005370 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ_PLUS];
Anna Schumakerc5675522014-05-28 13:41:22 -04005371}
5372#else
Trond Myklebust5c3485b2020-12-10 09:34:34 -05005373static void nfs42_read_plus_support(struct nfs_pgio_header *hdr,
5374 struct rpc_message *msg)
Anna Schumakerc5675522014-05-28 13:41:22 -04005375{
Anna Schumakerc5675522014-05-28 13:41:22 -04005376}
5377#endif /* CONFIG_NFS_V4_2 */
5378
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005379static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
5380 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005381{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005382 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04005383 if (!hdr->pgio_done_cb)
5384 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebust5c3485b2020-12-10 09:34:34 -05005385 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
5386 nfs42_read_plus_support(hdr, msg);
Anna Schumakerfba83f32018-05-04 16:22:50 -04005387 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005388}
5389
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005390static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
5391 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04005392{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005393 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005394 &hdr->args.seq_args,
5395 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04005396 task))
NeilBrownef1820f2013-09-04 17:04:49 +10005397 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005398 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
5399 hdr->args.lock_context,
Benjamin Coddingtonfbe77c32017-04-19 10:11:35 -04005400 hdr->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10005401 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005402 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10005403 return -EIO;
5404 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005405}
5406
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005407static int nfs4_write_done_cb(struct rpc_task *task,
5408 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005409{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005410 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10005411
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005412 trace_nfs4_write(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04005413 if (task->tk_status < 0) {
5414 struct nfs4_exception exception = {
5415 .inode = hdr->inode,
5416 .state = hdr->args.context->state,
5417 .stateid = &hdr->args.stateid,
5418 };
5419 task->tk_status = nfs4_async_handle_exception(task,
5420 NFS_SERVER(inode), task->tk_status,
5421 &exception);
5422 if (exception.retry) {
5423 rpc_restart_call_prepare(task);
5424 return -EAGAIN;
5425 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005426 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04005427 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005428 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05005429 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04005430 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05005431 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005432}
5433
Trond Myklebust5521abf2013-03-16 20:54:34 -04005434static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04005435 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04005436{
5437
5438 if (!nfs4_error_stateid_expired(task->tk_status) ||
5439 nfs4_stateid_is_current(&args->stateid,
5440 args->context,
5441 args->lock_context,
5442 FMODE_WRITE))
5443 return false;
5444 rpc_restart_call_prepare(task);
5445 return true;
5446}
5447
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005448static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00005449{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005450 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00005451 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005452 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04005453 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005454 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5455 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00005456}
5457
Trond Myklebust5a37f852012-04-28 14:55:16 -04005458static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005459bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00005460{
Trond Myklebust5a37f852012-04-28 14:55:16 -04005461 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005462 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04005463 return false;
5464 /* Otherwise, request attributes if and only if we don't hold
5465 * a delegation
5466 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04005467 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00005468}
Fred Isamana69aef12011-03-03 15:13:47 +00005469
Trond Myklebust332d1a02021-03-25 18:15:36 -04005470static void nfs4_bitmask_set(__u32 bitmask[NFS4_BITMASK_SZ], const __u32 *src,
5471 struct inode *inode, struct nfs_server *server,
5472 struct nfs4_label *label)
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005473{
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005474 unsigned long cache_validity = READ_ONCE(NFS_I(inode)->cache_validity);
Trond Myklebust332d1a02021-03-25 18:15:36 -04005475 unsigned int i;
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005476
Trond Myklebust332d1a02021-03-25 18:15:36 -04005477 memcpy(bitmask, src, sizeof(*bitmask) * NFS4_BITMASK_SZ);
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005478
Trond Myklebust45901a22021-02-08 08:49:32 -05005479 if (cache_validity & NFS_INO_INVALID_CHANGE)
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005480 bitmask[0] |= FATTR4_WORD0_CHANGE;
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005481 if (cache_validity & NFS_INO_INVALID_ATIME)
5482 bitmask[1] |= FATTR4_WORD1_TIME_ACCESS;
Trond Myklebust720869e2021-04-13 09:41:16 -04005483 if (cache_validity & NFS_INO_INVALID_MODE)
5484 bitmask[1] |= FATTR4_WORD1_MODE;
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005485 if (cache_validity & NFS_INO_INVALID_OTHER)
Trond Myklebust720869e2021-04-13 09:41:16 -04005486 bitmask[1] |= FATTR4_WORD1_OWNER | FATTR4_WORD1_OWNER_GROUP;
Trond Myklebustfabf2b32021-03-25 13:14:42 -04005487 if (cache_validity & NFS_INO_INVALID_NLINK)
5488 bitmask[1] |= FATTR4_WORD1_NUMLINKS;
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005489 if (label && label->len && cache_validity & NFS_INO_INVALID_LABEL)
5490 bitmask[2] |= FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust45901a22021-02-08 08:49:32 -05005491 if (cache_validity & NFS_INO_INVALID_CTIME)
5492 bitmask[1] |= FATTR4_WORD1_TIME_METADATA;
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005493 if (cache_validity & NFS_INO_INVALID_MTIME)
5494 bitmask[1] |= FATTR4_WORD1_TIME_MODIFY;
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005495 if (cache_validity & NFS_INO_INVALID_BLOCKS)
5496 bitmask[1] |= FATTR4_WORD1_SPACE_USED;
Trond Myklebust332d1a02021-03-25 18:15:36 -04005497
Trond Myklebustcc7f2da2021-04-11 14:31:24 -04005498 if (cache_validity & NFS_INO_INVALID_SIZE)
Trond Myklebust332d1a02021-03-25 18:15:36 -04005499 bitmask[0] |= FATTR4_WORD0_SIZE;
5500
5501 for (i = 0; i < NFS4_BITMASK_SZ; i++)
5502 bitmask[i] &= server->attr_bitmask[i];
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005503}
5504
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005505static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
Anna Schumakerfb91fb02018-05-04 16:22:48 -04005506 struct rpc_message *msg,
5507 struct rpc_clnt **clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005508{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005509 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005510
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005511 if (!nfs4_write_need_cache_consistency_data(hdr)) {
5512 hdr->args.bitmask = NULL;
5513 hdr->res.fattr = NULL;
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005514 } else {
Trond Myklebust332d1a02021-03-25 18:15:36 -04005515 nfs4_bitmask_set(hdr->args.bitmask_store,
5516 server->cache_consistency_bitmask,
5517 hdr->inode, server, NULL);
5518 hdr->args.bitmask = hdr->args.bitmask_store;
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005519 }
Trond Myklebust5a37f852012-04-28 14:55:16 -04005520
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005521 if (!hdr->pgio_done_cb)
5522 hdr->pgio_done_cb = nfs4_write_done_cb;
5523 hdr->res.server = server;
5524 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005525
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005526 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Olga Kornievskaiacd1b6592020-02-12 17:32:12 -05005527 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
Anna Schumakerfb91fb02018-05-04 16:22:48 -04005528 nfs4_state_protect_write(server->nfs_client, clnt, msg, hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005529}
5530
Fred Isaman0b7c0152012-04-20 14:47:39 -04005531static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
5532{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005533 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005534 &data->args.seq_args,
5535 &data->res.seq_res,
5536 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005537}
5538
Fred Isaman0b7c0152012-04-20 14:47:39 -04005539static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005540{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005541 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04005542
Trond Myklebustcc668ab2013-08-14 15:31:28 -04005543 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10005544 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
5545 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005546 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05005547 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005548 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05005549 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005550}
5551
Fred Isaman0b7c0152012-04-20 14:47:39 -04005552static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00005553{
5554 if (!nfs4_sequence_done(task, &data->res.seq_res))
5555 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04005556 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00005557}
5558
Anna Schumakere9ae1ee2018-05-04 16:22:49 -04005559static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg,
5560 struct rpc_clnt **clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005561{
Trond Myklebust788e7a82006-03-20 13:44:27 -05005562 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00005563
Fred Isaman0b7c0152012-04-20 14:47:39 -04005564 if (data->commit_done_cb == NULL)
5565 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04005566 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005567 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Anna Schumakerfba83f32018-05-04 16:22:50 -04005568 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Anna Schumakere9ae1ee2018-05-04 16:22:49 -04005569 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_COMMIT, clnt, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005570}
5571
Olga Kornievskaia6b8d84e2018-07-09 15:13:36 -04005572static int _nfs4_proc_commit(struct file *dst, struct nfs_commitargs *args,
5573 struct nfs_commitres *res)
5574{
5575 struct inode *dst_inode = file_inode(dst);
5576 struct nfs_server *server = NFS_SERVER(dst_inode);
5577 struct rpc_message msg = {
5578 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
5579 .rpc_argp = args,
5580 .rpc_resp = res,
5581 };
5582
5583 args->fh = NFS_FH(dst_inode);
5584 return nfs4_call_sync(server->client, server, &msg,
5585 &args->seq_args, &res->seq_res, 1);
5586}
5587
5588int nfs4_proc_commit(struct file *dst, __u64 offset, __u32 count, struct nfs_commitres *res)
5589{
5590 struct nfs_commitargs args = {
5591 .offset = offset,
5592 .count = count,
5593 };
5594 struct nfs_server *dst_server = NFS_SERVER(file_inode(dst));
5595 struct nfs4_exception exception = { };
5596 int status;
5597
5598 do {
5599 status = _nfs4_proc_commit(dst, &args, res);
5600 status = nfs4_handle_exception(dst_server, status, &exception);
5601 } while (exception.retry);
5602
5603 return status;
5604}
5605
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005606struct nfs4_renewdata {
5607 struct nfs_client *client;
5608 unsigned long timestamp;
5609};
5610
Linus Torvalds1da177e2005-04-16 15:20:36 -07005611/*
5612 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
5613 * standalone procedure for queueing an asynchronous RENEW.
5614 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005615static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005616{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005617 struct nfs4_renewdata *data = calldata;
5618 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005619
Elena Reshetova212bf412017-10-20 12:53:38 +03005620 if (refcount_read(&clp->cl_count) > 1)
Alexandros Batsakis0851de062010-02-05 03:45:06 -08005621 nfs4_schedule_state_renewal(clp);
5622 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005623 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005624}
5625
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005626static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005627{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005628 struct nfs4_renewdata *data = calldata;
5629 struct nfs_client *clp = data->client;
5630 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005631
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005632 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04005633 switch (task->tk_status) {
5634 case 0:
5635 break;
5636 case -NFS4ERR_LEASE_MOVED:
5637 nfs4_schedule_lease_moved_recovery(clp);
5638 break;
5639 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04005640 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04005641 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
5642 return;
5643 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005644 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04005645 return;
5646 }
5647 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005648 }
Trond Myklebust452e9352010-07-31 14:29:06 -04005649 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005650}
5651
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005652static const struct rpc_call_ops nfs4_renew_ops = {
5653 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005654 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005655};
5656
NeilBrowna52458b2018-12-03 11:30:31 +11005657static int nfs4_proc_async_renew(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005658{
5659 struct rpc_message msg = {
5660 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5661 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01005662 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005663 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005664 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005665
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005666 if (renew_flags == 0)
5667 return 0;
Elena Reshetova212bf412017-10-20 12:53:38 +03005668 if (!refcount_inc_not_zero(&clp->cl_count))
Alexandros Batsakis0851de062010-02-05 03:45:06 -08005669 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04005670 data = kmalloc(sizeof(*data), GFP_NOFS);
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04005671 if (data == NULL) {
5672 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005673 return -ENOMEM;
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04005674 }
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005675 data->client = clp;
5676 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005677 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005678 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005679}
5680
NeilBrowna52458b2018-12-03 11:30:31 +11005681static int nfs4_proc_renew(struct nfs_client *clp, const struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005682{
5683 struct rpc_message msg = {
5684 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5685 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01005686 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005687 };
5688 unsigned long now = jiffies;
5689 int status;
5690
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005691 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005692 if (status < 0)
5693 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04005694 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005695 return 0;
5696}
5697
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005698static inline int nfs4_server_supports_acls(struct nfs_server *server)
5699{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06005700 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005701}
5702
Trond Myklebust21f498c2012-08-24 10:59:25 -04005703/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
5704 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005705 * the stack.
5706 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04005707#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005708
Frank van der Lindenccde1e92020-06-23 22:39:01 +00005709int nfs4_buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005710 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05005711{
5712 struct page *newpage, **spages;
5713 int rc = 0;
5714 size_t len;
5715 spages = pages;
5716
5717 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04005718 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05005719 newpage = alloc_page(GFP_KERNEL);
5720
5721 if (newpage == NULL)
5722 goto unwind;
5723 memcpy(page_address(newpage), buf, len);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005724 buf += len;
5725 buflen -= len;
Neil Hormane9e3d722011-03-04 19:26:03 -05005726 *pages++ = newpage;
5727 rc++;
5728 } while (buflen != 0);
5729
5730 return rc;
5731
5732unwind:
5733 for(; rc > 0; rc--)
5734 __free_page(spages[rc-1]);
5735 return -ENOMEM;
5736}
5737
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005738struct nfs4_cached_acl {
5739 int cached;
5740 size_t len;
Gustavo A. R. Silva5601cda2020-03-09 13:24:42 -05005741 char data[];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005742};
5743
5744static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005745{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005746 struct nfs_inode *nfsi = NFS_I(inode);
5747
5748 spin_lock(&inode->i_lock);
5749 kfree(nfsi->nfs4_acl);
5750 nfsi->nfs4_acl = acl;
5751 spin_unlock(&inode->i_lock);
5752}
5753
5754static void nfs4_zap_acl_attr(struct inode *inode)
5755{
5756 nfs4_set_cached_acl(inode, NULL);
5757}
5758
5759static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
5760{
5761 struct nfs_inode *nfsi = NFS_I(inode);
5762 struct nfs4_cached_acl *acl;
5763 int ret = -ENOENT;
5764
5765 spin_lock(&inode->i_lock);
5766 acl = nfsi->nfs4_acl;
5767 if (acl == NULL)
5768 goto out;
5769 if (buf == NULL) /* user is just asking for length */
5770 goto out_len;
5771 if (acl->cached == 0)
5772 goto out;
5773 ret = -ERANGE; /* see getxattr(2) man page */
5774 if (acl->len > buflen)
5775 goto out;
5776 memcpy(buf, acl->data, acl->len);
5777out_len:
5778 ret = acl->len;
5779out:
5780 spin_unlock(&inode->i_lock);
5781 return ret;
5782}
5783
Sachin Prabhu5794d212012-04-17 14:36:40 +01005784static 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 +00005785{
5786 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005787 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005788
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005789 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005790 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005791 if (acl == NULL)
5792 goto out;
5793 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01005794 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005795 } else {
5796 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5797 if (acl == NULL)
5798 goto out;
5799 acl->cached = 0;
5800 }
5801 acl->len = acl_len;
5802out:
5803 nfs4_set_cached_acl(inode, acl);
5804}
5805
Andy Adamsonbf118a32011-12-07 11:55:27 -05005806/*
5807 * The getxattr API returns the required buffer length when called with a
5808 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5809 * the required buf. On a NULL buf, we send a page of data to the server
5810 * guessing that the ACL request can be serviced by a page. If so, we cache
5811 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5812 * the cache. If not so, we throw away the page, and cache the required
5813 * length. The next getxattr call will then produce another round trip to
5814 * the server, this time with the input buf of the required size.
5815 */
Trond Myklebust16b42892006-08-24 12:27:15 -04005816static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005817{
Olga Kornievskaia62a15732020-01-02 17:09:54 -05005818 struct page **pages;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005819 struct nfs_getaclargs args = {
5820 .fh = NFS_FH(inode),
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005821 .acl_len = buflen,
5822 };
Benny Halevy663c79b2009-04-01 09:21:59 -04005823 struct nfs_getaclres res = {
5824 .acl_len = buflen,
5825 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005826 struct rpc_message msg = {
5827 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5828 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04005829 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005830 };
Olga Kornievskaia62a15732020-01-02 17:09:54 -05005831 unsigned int npages;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005832 int ret = -ENOMEM, i;
Olga Kornievskaia62a15732020-01-02 17:09:54 -05005833 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005834
Olga Kornievskaia62a15732020-01-02 17:09:54 -05005835 if (buflen == 0)
5836 buflen = server->rsize;
5837
5838 npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
5839 pages = kmalloc_array(npages, sizeof(struct page *), GFP_NOFS);
5840 if (!pages)
5841 return -ENOMEM;
5842
5843 args.acl_pages = pages;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005844
Andy Adamsonbf118a32011-12-07 11:55:27 -05005845 for (i = 0; i < npages; i++) {
5846 pages[i] = alloc_page(GFP_KERNEL);
5847 if (!pages[i])
5848 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005849 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01005850
5851 /* for decoding across pages */
5852 res.acl_scratch = alloc_page(GFP_KERNEL);
5853 if (!res.acl_scratch)
5854 goto out_free;
5855
Andy Adamsonbf118a32011-12-07 11:55:27 -05005856 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005857
Peng Taode040be2012-01-10 22:42:47 +08005858 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05005859 __func__, buf, buflen, npages, args.acl_len);
5860 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5861 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005862 if (ret)
5863 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05005864
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005865 /* Handle the case where the passed-in buffer is too short */
5866 if (res.acl_flags & NFS4_ACL_TRUNC) {
5867 /* Did the user only issue a request for the acl length? */
5868 if (buf == NULL)
5869 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005870 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005871 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005872 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005873 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005874 if (buf) {
5875 if (res.acl_len > buflen) {
5876 ret = -ERANGE;
5877 goto out_free;
5878 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005879 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005880 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005881out_ok:
5882 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005883out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05005884 for (i = 0; i < npages; i++)
5885 if (pages[i])
5886 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05005887 if (res.acl_scratch)
5888 __free_page(res.acl_scratch);
Olga Kornievskaia62a15732020-01-02 17:09:54 -05005889 kfree(pages);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005890 return ret;
5891}
5892
Trond Myklebust16b42892006-08-24 12:27:15 -04005893static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5894{
Trond Myklebust0688e642019-04-07 13:59:09 -04005895 struct nfs4_exception exception = {
5896 .interruptible = true,
5897 };
Trond Myklebust16b42892006-08-24 12:27:15 -04005898 ssize_t ret;
5899 do {
5900 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04005901 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b42892006-08-24 12:27:15 -04005902 if (ret >= 0)
5903 break;
5904 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5905 } while (exception.retry);
5906 return ret;
5907}
5908
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005909static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5910{
5911 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005912 int ret;
5913
5914 if (!nfs4_server_supports_acls(server))
5915 return -EOPNOTSUPP;
Trond Myklebust1f3208b2021-03-25 11:04:34 -04005916 ret = nfs_revalidate_inode(inode, NFS_INO_INVALID_CHANGE);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005917 if (ret < 0)
5918 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005919 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5920 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005921 ret = nfs4_read_cached_acl(inode, buf, buflen);
5922 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05005923 /* -ENOENT is returned if there is no ACL or if there is an ACL
5924 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005925 return ret;
5926 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005927}
5928
Trond Myklebust16b42892006-08-24 12:27:15 -04005929static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005930{
5931 struct nfs_server *server = NFS_SERVER(inode);
5932 struct page *pages[NFS4ACL_MAXPAGES];
5933 struct nfs_setaclargs arg = {
5934 .fh = NFS_FH(inode),
5935 .acl_pages = pages,
5936 .acl_len = buflen,
5937 };
Benny Halevy73c403a2009-04-01 09:22:01 -04005938 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005939 struct rpc_message msg = {
5940 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5941 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04005942 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005943 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005944 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05005945 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005946
J. Bruce Fields4f8be1f2021-01-28 17:36:38 -05005947 /* You can't remove system.nfs4_acl: */
5948 if (buflen == 0)
5949 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005950 if (!nfs4_server_supports_acls(server))
5951 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005952 if (npages > ARRAY_SIZE(pages))
5953 return -ERANGE;
Frank van der Lindenccde1e92020-06-23 22:39:01 +00005954 i = nfs4_buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05005955 if (i < 0)
5956 return i;
Trond Myklebustc01d3642018-03-20 16:43:20 -04005957 nfs4_inode_make_writeable(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005958 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05005959
5960 /*
5961 * Free each page after tx, so the only ref left is
5962 * held by the network stack
5963 */
5964 for (; i > 0; i--)
5965 put_page(pages[i-1]);
5966
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005967 /*
5968 * Acl update can result in inode attribute update.
5969 * so mark the attribute cache invalid.
5970 */
5971 spin_lock(&inode->i_lock);
Trond Myklebustb6f80a22021-03-08 14:42:55 -05005972 nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE |
5973 NFS_INO_INVALID_CTIME |
5974 NFS_INO_REVAL_FORCED);
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005975 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04005976 nfs_access_zap_cache(inode);
5977 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005978 return ret;
5979}
5980
Trond Myklebust16b42892006-08-24 12:27:15 -04005981static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5982{
5983 struct nfs4_exception exception = { };
5984 int err;
5985 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005986 err = __nfs4_proc_set_acl(inode, buf, buflen);
5987 trace_nfs4_set_acl(inode, err);
Dai Ngof8849e22021-05-19 17:15:10 -04005988 if (err == -NFS4ERR_BADOWNER || err == -NFS4ERR_BADNAME) {
5989 /*
5990 * no need to retry since the kernel
5991 * isn't involved in encoding the ACEs.
5992 */
5993 err = -EINVAL;
5994 break;
5995 }
Trond Myklebustc1578b72013-08-12 16:58:42 -04005996 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b42892006-08-24 12:27:15 -04005997 &exception);
5998 } while (exception.retry);
5999 return err;
6000}
6001
David Quigleyaa9c2662013-05-22 12:50:44 -04006002#ifdef CONFIG_NFS_V4_SECURITY_LABEL
6003static int _nfs4_get_security_label(struct inode *inode, void *buf,
6004 size_t buflen)
6005{
6006 struct nfs_server *server = NFS_SERVER(inode);
6007 struct nfs_fattr fattr;
6008 struct nfs4_label label = {0, 0, buflen, buf};
6009
6010 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04006011 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04006012 .fh = NFS_FH(inode),
6013 .bitmask = bitmask,
6014 };
6015 struct nfs4_getattr_res res = {
6016 .fattr = &fattr,
6017 .label = &label,
6018 .server = server,
6019 };
6020 struct rpc_message msg = {
6021 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04006022 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04006023 .rpc_resp = &res,
6024 };
6025 int ret;
6026
6027 nfs_fattr_init(&fattr);
6028
Trond Myklebustfcb63a92013-11-01 12:42:25 -04006029 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04006030 if (ret)
6031 return ret;
6032 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
6033 return -ENOENT;
Ondrej Mosnacek53cb2452021-01-15 18:43:56 +01006034 return label.len;
David Quigleyaa9c2662013-05-22 12:50:44 -04006035}
6036
6037static int nfs4_get_security_label(struct inode *inode, void *buf,
6038 size_t buflen)
6039{
Trond Myklebust0688e642019-04-07 13:59:09 -04006040 struct nfs4_exception exception = {
6041 .interruptible = true,
6042 };
David Quigleyaa9c2662013-05-22 12:50:44 -04006043 int err;
6044
6045 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
6046 return -EOPNOTSUPP;
6047
6048 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04006049 err = _nfs4_get_security_label(inode, buf, buflen);
6050 trace_nfs4_get_security_label(inode, err);
6051 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04006052 &exception);
6053 } while (exception.retry);
6054 return err;
6055}
6056
6057static int _nfs4_do_set_security_label(struct inode *inode,
6058 struct nfs4_label *ilabel,
6059 struct nfs_fattr *fattr,
6060 struct nfs4_label *olabel)
6061{
6062
6063 struct iattr sattr = {0};
6064 struct nfs_server *server = NFS_SERVER(inode);
6065 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04006066 struct nfs_setattrargs arg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05006067 .fh = NFS_FH(inode),
6068 .iap = &sattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04006069 .server = server,
6070 .bitmask = bitmask,
6071 .label = ilabel,
6072 };
6073 struct nfs_setattrres res = {
6074 .fattr = fattr,
6075 .label = olabel,
6076 .server = server,
6077 };
6078 struct rpc_message msg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05006079 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
6080 .rpc_argp = &arg,
6081 .rpc_resp = &res,
David Quigleyaa9c2662013-05-22 12:50:44 -04006082 };
6083 int status;
6084
Jeff Layton12207f62013-11-01 10:49:32 -04006085 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04006086
Jeff Layton12207f62013-11-01 10:49:32 -04006087 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04006088 if (status)
6089 dprintk("%s failed: %d\n", __func__, status);
6090
6091 return status;
6092}
6093
6094static int nfs4_do_set_security_label(struct inode *inode,
6095 struct nfs4_label *ilabel,
6096 struct nfs_fattr *fattr,
6097 struct nfs4_label *olabel)
6098{
6099 struct nfs4_exception exception = { };
6100 int err;
6101
6102 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04006103 err = _nfs4_do_set_security_label(inode, ilabel,
6104 fattr, olabel);
6105 trace_nfs4_set_security_label(inode, err);
6106 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04006107 &exception);
6108 } while (exception.retry);
6109 return err;
6110}
6111
6112static int
Al Viro59301222016-05-27 10:19:30 -04006113nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04006114{
6115 struct nfs4_label ilabel, *olabel = NULL;
6116 struct nfs_fattr fattr;
David Quigleyaa9c2662013-05-22 12:50:44 -04006117 int status;
6118
6119 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
6120 return -EOPNOTSUPP;
6121
6122 nfs_fattr_init(&fattr);
6123
6124 ilabel.pi = 0;
6125 ilabel.lfs = 0;
6126 ilabel.label = (char *)buf;
6127 ilabel.len = buflen;
6128
David Quigleyaa9c2662013-05-22 12:50:44 -04006129 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
6130 if (IS_ERR(olabel)) {
6131 status = -PTR_ERR(olabel);
6132 goto out;
6133 }
6134
6135 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
6136 if (status == 0)
6137 nfs_setsecurity(inode, &fattr, olabel);
6138
6139 nfs4_label_free(olabel);
6140out:
David Quigleyaa9c2662013-05-22 12:50:44 -04006141 return status;
6142}
6143#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
6144
6145
Chuck Leverf0920752012-05-21 22:45:41 -04006146static void nfs4_init_boot_verifier(const struct nfs_client *clp,
6147 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05006148{
6149 __be32 verf[2];
6150
Chuck Lever2c820d92012-05-21 22:45:33 -04006151 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
6152 /* An impossible timestamp guarantees this value
6153 * will never match a generated boot time. */
Deepa Dinamani2f86e092016-10-01 16:46:26 -07006154 verf[0] = cpu_to_be32(U32_MAX);
6155 verf[1] = cpu_to_be32(U32_MAX);
Chuck Lever2c820d92012-05-21 22:45:33 -04006156 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04006157 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Deepa Dinamani2f86e092016-10-01 16:46:26 -07006158 u64 ns = ktime_to_ns(nn->boot_time);
6159
6160 verf[0] = cpu_to_be32(ns >> 32);
6161 verf[1] = cpu_to_be32(ns);
Chuck Lever2c820d92012-05-21 22:45:33 -04006162 }
Chuck Levercd937102012-03-02 17:14:31 -05006163 memcpy(bootverf->data, verf, sizeof(bootverf->data));
6164}
6165
Trond Myklebust1aee5512020-10-07 18:24:17 -04006166static size_t
Trond Myklebust39d43d12020-10-07 18:24:18 -04006167nfs4_get_uniquifier(struct nfs_client *clp, char *buf, size_t buflen)
Trond Myklebust1aee5512020-10-07 18:24:17 -04006168{
Trond Myklebust39d43d12020-10-07 18:24:18 -04006169 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
6170 struct nfs_netns_client *nn_clp = nn->nfs_client;
6171 const char *id;
6172
Trond Myklebust1aee5512020-10-07 18:24:17 -04006173 buf[0] = '\0';
6174
Trond Myklebust39d43d12020-10-07 18:24:18 -04006175 if (nn_clp) {
6176 rcu_read_lock();
6177 id = rcu_dereference(nn_clp->identifier);
6178 if (id)
6179 strscpy(buf, id, buflen);
6180 rcu_read_unlock();
6181 }
6182
6183 if (nfs4_client_id_uniquifier[0] != '\0' && buf[0] == '\0')
Trond Myklebust1aee5512020-10-07 18:24:17 -04006184 strscpy(buf, nfs4_client_id_uniquifier, buflen);
6185
6186 return strlen(buf);
6187}
6188
Jeff Laytona3192682015-06-09 19:43:59 -04006189static int
6190nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04006191{
Trond Myklebust1aee5512020-10-07 18:24:17 -04006192 char buf[NFS4_CLIENT_ID_UNIQ_LEN];
6193 size_t buflen;
Jeff Laytona3192682015-06-09 19:43:59 -04006194 size_t len;
6195 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04006196
Trond Myklebustceb3a162015-01-03 15:16:04 -05006197 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04006198 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08006199
Jeff Laytona3192682015-06-09 19:43:59 -04006200 rcu_read_lock();
Chuck Lever848a4eb2018-06-04 10:53:29 -04006201 len = 14 +
6202 strlen(clp->cl_rpcclient->cl_nodename) +
6203 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04006204 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
Jeff Laytona3192682015-06-09 19:43:59 -04006205 1;
6206 rcu_read_unlock();
Trond Myklebust1aee5512020-10-07 18:24:17 -04006207
Trond Myklebust39d43d12020-10-07 18:24:18 -04006208 buflen = nfs4_get_uniquifier(clp, buf, sizeof(buf));
Trond Myklebust1aee5512020-10-07 18:24:17 -04006209 if (buflen)
6210 len += buflen + 1;
6211
Jeff Laytona3192682015-06-09 19:43:59 -04006212 if (len > NFS4_OPAQUE_LIMIT + 1)
6213 return -EINVAL;
6214
6215 /*
6216 * Since this string is allocated at mount time, and held until the
6217 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6218 * about a memory-reclaim deadlock.
6219 */
6220 str = kmalloc(len, GFP_KERNEL);
6221 if (!str)
6222 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05006223
Chuck Levere984a552012-09-14 17:24:21 -04006224 rcu_read_lock();
Trond Myklebust1aee5512020-10-07 18:24:17 -04006225 if (buflen)
Chuck Lever025bb9f2018-06-04 10:53:34 -04006226 scnprintf(str, len, "Linux NFSv4.0 %s/%s/%s",
Trond Myklebust1aee5512020-10-07 18:24:17 -04006227 clp->cl_rpcclient->cl_nodename, buf,
Chuck Lever848a4eb2018-06-04 10:53:29 -04006228 rpc_peeraddr2str(clp->cl_rpcclient,
Chuck Lever025bb9f2018-06-04 10:53:34 -04006229 RPC_DISPLAY_ADDR));
Chuck Lever848a4eb2018-06-04 10:53:29 -04006230 else
Chuck Lever025bb9f2018-06-04 10:53:34 -04006231 scnprintf(str, len, "Linux NFSv4.0 %s/%s",
Chuck Lever848a4eb2018-06-04 10:53:29 -04006232 clp->cl_rpcclient->cl_nodename,
6233 rpc_peeraddr2str(clp->cl_rpcclient,
Chuck Lever025bb9f2018-06-04 10:53:34 -04006234 RPC_DISPLAY_ADDR));
Chuck Levere984a552012-09-14 17:24:21 -04006235 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04006236
Jeff Laytona3192682015-06-09 19:43:59 -04006237 clp->cl_owner_id = str;
6238 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04006239}
6240
Jeff Layton873e3852015-06-09 19:44:00 -04006241static int
Jeff Layton873e3852015-06-09 19:44:00 -04006242nfs4_init_uniform_client_string(struct nfs_client *clp)
6243{
Trond Myklebust1aee5512020-10-07 18:24:17 -04006244 char buf[NFS4_CLIENT_ID_UNIQ_LEN];
6245 size_t buflen;
Jeff Layton873e3852015-06-09 19:44:00 -04006246 size_t len;
6247 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05006248
6249 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04006250 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04006251
Jeff Layton873e3852015-06-09 19:44:00 -04006252 len = 10 + 10 + 1 + 10 + 1 +
6253 strlen(clp->cl_rpcclient->cl_nodename) + 1;
6254
Trond Myklebust39d43d12020-10-07 18:24:18 -04006255 buflen = nfs4_get_uniquifier(clp, buf, sizeof(buf));
Trond Myklebust1aee5512020-10-07 18:24:17 -04006256 if (buflen)
6257 len += buflen + 1;
6258
Jeff Layton873e3852015-06-09 19:44:00 -04006259 if (len > NFS4_OPAQUE_LIMIT + 1)
6260 return -EINVAL;
6261
6262 /*
6263 * Since this string is allocated at mount time, and held until the
6264 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6265 * about a memory-reclaim deadlock.
6266 */
6267 str = kmalloc(len, GFP_KERNEL);
6268 if (!str)
6269 return -ENOMEM;
6270
Trond Myklebust1aee5512020-10-07 18:24:17 -04006271 if (buflen)
6272 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
6273 clp->rpc_ops->version, clp->cl_minorversion,
6274 buf, clp->cl_rpcclient->cl_nodename);
6275 else
6276 scnprintf(str, len, "Linux NFSv%u.%u %s",
6277 clp->rpc_ops->version, clp->cl_minorversion,
6278 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04006279 clp->cl_owner_id = str;
6280 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04006281}
6282
Chuck Lever706cb8d2014-03-12 12:51:47 -04006283/*
6284 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
6285 * services. Advertise one based on the address family of the
6286 * clientaddr.
6287 */
6288static unsigned int
6289nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
6290{
6291 if (strchr(clp->cl_ipaddr, ':') != NULL)
6292 return scnprintf(buf, len, "tcp6");
6293 else
6294 return scnprintf(buf, len, "tcp");
6295}
6296
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006297static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
6298{
6299 struct nfs4_setclientid *sc = calldata;
6300
6301 if (task->tk_status == 0)
6302 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
6303}
6304
6305static const struct rpc_call_ops nfs4_setclientid_ops = {
6306 .rpc_call_done = nfs4_setclientid_done,
6307};
6308
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006309/**
6310 * nfs4_proc_setclientid - Negotiate client ID
6311 * @clp: state data structure
6312 * @program: RPC program for NFSv4 callback service
6313 * @port: IP port number for NFS4 callback service
NeilBrowna52458b2018-12-03 11:30:31 +11006314 * @cred: credential to use for this call
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006315 * @res: where to place the result
6316 *
6317 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6318 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006319int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
NeilBrowna52458b2018-12-03 11:30:31 +11006320 unsigned short port, const struct cred *cred,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006321 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006322{
6323 nfs4_verifier sc_verifier;
6324 struct nfs4_setclientid setclientid = {
6325 .sc_verifier = &sc_verifier,
6326 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04006327 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006328 };
6329 struct rpc_message msg = {
6330 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
6331 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006332 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01006333 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006334 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006335 struct rpc_task_setup task_setup_data = {
6336 .rpc_client = clp->cl_rpcclient,
6337 .rpc_message = &msg,
6338 .callback_ops = &nfs4_setclientid_ops,
6339 .callback_data = &setclientid,
NeilBrown5a0c2572019-05-30 10:41:28 +10006340 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006341 };
Robert Milkowski7dc29932020-01-30 09:43:25 +00006342 unsigned long now = jiffies;
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006343 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006344
Chuck Leverde734832012-07-11 16:30:50 -04006345 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04006346 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04006347
6348 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
6349 status = nfs4_init_uniform_client_string(clp);
6350 else
Jeff Laytona3192682015-06-09 19:43:59 -04006351 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04006352
6353 if (status)
6354 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04006355
Chuck Leverde734832012-07-11 16:30:50 -04006356 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04006357 setclientid.sc_netid_len =
6358 nfs4_init_callback_netid(clp,
6359 setclientid.sc_netid,
6360 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04006361 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05006362 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006363 clp->cl_ipaddr, port >> 8, port & 255);
6364
Jeff Layton3a6bb732015-06-09 19:43:57 -04006365 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006366 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04006367 clp->cl_owner_id);
Anna Schumakerdae40962019-08-14 15:28:28 -04006368
6369 status = nfs4_call_sync_custom(&task_setup_data);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006370 if (setclientid.sc_cred) {
Chuck Lever1047ec82019-10-04 09:58:54 -04006371 kfree(clp->cl_acceptor);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006372 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
6373 put_rpccred(setclientid.sc_cred);
6374 }
Robert Milkowski7dc29932020-01-30 09:43:25 +00006375
6376 if (status == 0)
6377 do_renew_lease(clp, now);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006378out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006379 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006380 dprintk("NFS reply setclientid: %d\n", status);
6381 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006382}
6383
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006384/**
6385 * nfs4_proc_setclientid_confirm - Confirm client ID
6386 * @clp: state data structure
Trond Myklebust302fad72019-02-18 13:32:38 -05006387 * @arg: result of a previous SETCLIENTID
NeilBrowna52458b2018-12-03 11:30:31 +11006388 * @cred: credential to use for this call
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006389 *
6390 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6391 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04006392int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006393 struct nfs4_setclientid_res *arg,
NeilBrowna52458b2018-12-03 11:30:31 +11006394 const struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006395{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006396 struct rpc_message msg = {
6397 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006398 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01006399 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006400 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006401 int status;
6402
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006403 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
6404 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6405 clp->cl_clientid);
NeilBrown5a0c2572019-05-30 10:41:28 +10006406 status = rpc_call_sync(clp->cl_rpcclient, &msg,
6407 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006408 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006409 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006410 return status;
6411}
6412
Trond Myklebustfe650402006-01-03 09:55:18 +01006413struct nfs4_delegreturndata {
6414 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01006415 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01006416 struct nfs_fh fh;
6417 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006418 unsigned long timestamp;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006419 struct {
6420 struct nfs4_layoutreturn_args arg;
6421 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04006422 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006423 u32 roc_barrier;
6424 bool roc;
6425 } lr;
Trond Myklebustfa178f22006-01-03 09:55:38 +01006426 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01006427 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08006428 struct inode *inode;
Trond Myklebustfe650402006-01-03 09:55:18 +01006429};
6430
Trond Myklebustfe650402006-01-03 09:55:18 +01006431static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
6432{
6433 struct nfs4_delegreturndata *data = calldata;
Trond Myklebuste0dba012017-11-07 11:02:32 -05006434 struct nfs4_exception exception = {
6435 .inode = data->inode,
6436 .stateid = &data->stateid,
Trond Myklebustdfe1fe72021-06-01 11:10:05 -04006437 .task_is_privileged = data->args.seq_args.sa_privileged,
Trond Myklebuste0dba012017-11-07 11:02:32 -05006438 };
Andy Adamson938e1012009-04-01 09:22:28 -04006439
Trond Myklebust14516c32010-07-31 14:29:06 -04006440 if (!nfs4_sequence_done(task, &data->res.seq_res))
6441 return;
Andy Adamson938e1012009-04-01 09:22:28 -04006442
Trond Myklebustca8acf82013-08-13 10:36:56 -04006443 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Trond Myklebust586f1c32016-11-15 15:03:33 -05006444
6445 /* Handle Layoutreturn errors */
Trond Myklebust078000d2021-01-04 13:18:03 -05006446 if (pnfs_roc_done(task, &data->args.lr_args, &data->res.lr_res,
6447 &data->res.lr_ret) == -EAGAIN)
Trond Myklebust287a9c52019-09-20 07:23:41 -04006448 goto out_restart;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006449
Ricardo Labiaga79708862009-12-07 09:23:21 -05006450 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05006451 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01006452 renew_lease(data->res.server, data->timestamp);
Trond Myklebust23ea44c2016-11-10 16:06:28 -05006453 break;
Trond Myklebustc97cf602013-11-19 16:34:14 -05006454 case -NFS4ERR_ADMIN_REVOKED:
6455 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust26d36302016-09-22 13:39:05 -04006456 case -NFS4ERR_EXPIRED:
6457 nfs4_free_revoked_stateid(data->res.server,
6458 data->args.stateid,
6459 task->tk_msg.rpc_cred);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05006460 fallthrough;
Trond Myklebustc97cf602013-11-19 16:34:14 -05006461 case -NFS4ERR_BAD_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05006462 case -NFS4ERR_STALE_STATEID:
Trond Myklebust244fcd22019-12-20 10:43:37 -05006463 case -ETIMEDOUT:
Trond Myklebustc97cf602013-11-19 16:34:14 -05006464 task->tk_status = 0;
6465 break;
Trond Myklebust12f275c2017-11-06 15:28:05 -05006466 case -NFS4ERR_OLD_STATEID:
Trond Myklebust246afc0a2019-10-24 18:00:35 -04006467 if (!nfs4_refresh_delegation_stateid(&data->stateid, data->inode))
6468 nfs4_stateid_seqid_inc(&data->stateid);
Trond Myklebust70d136b2019-10-26 22:37:40 -04006469 if (data->args.bitmask) {
6470 data->args.bitmask = NULL;
6471 data->res.fattr = NULL;
6472 }
Trond Myklebust246afc0a2019-10-24 18:00:35 -04006473 goto out_restart;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05006474 case -NFS4ERR_ACCESS:
6475 if (data->args.bitmask) {
6476 data->args.bitmask = NULL;
6477 data->res.fattr = NULL;
Trond Myklebust140087fd2017-11-06 15:28:10 -05006478 goto out_restart;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05006479 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05006480 fallthrough;
Ricardo Labiaga79708862009-12-07 09:23:21 -05006481 default:
Trond Myklebuste0dba012017-11-07 11:02:32 -05006482 task->tk_status = nfs4_async_handle_exception(task,
6483 data->res.server, task->tk_status,
6484 &exception);
6485 if (exception.retry)
Trond Myklebust140087fd2017-11-06 15:28:10 -05006486 goto out_restart;
Ricardo Labiaga79708862009-12-07 09:23:21 -05006487 }
Trond Myklebustd51f91d2019-10-21 14:22:14 -04006488 nfs_delegation_mark_returned(data->inode, data->args.stateid);
Ricardo Labiaga79708862009-12-07 09:23:21 -05006489 data->rpc_status = task->tk_status;
Trond Myklebust140087fd2017-11-06 15:28:10 -05006490 return;
Trond Myklebust140087fd2017-11-06 15:28:10 -05006491out_restart:
6492 task->tk_status = 0;
6493 rpc_restart_call_prepare(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01006494}
6495
6496static void nfs4_delegreturn_release(void *calldata)
6497{
Peng Tao039b7562014-07-03 13:05:02 +08006498 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006499 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08006500
Trond Myklebust078000d2021-01-04 13:18:03 -05006501 if (data->lr.roc)
6502 pnfs_roc_release(&data->lr.arg, &data->lr.res,
6503 data->res.lr_ret);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006504 if (inode) {
Trond Myklebust0bc2c9b2016-12-16 19:48:09 -05006505 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006506 nfs_iput_and_deactive(inode);
6507 }
Trond Myklebustfe650402006-01-03 09:55:18 +01006508 kfree(calldata);
6509}
6510
Andy Adamson938e1012009-04-01 09:22:28 -04006511static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
6512{
6513 struct nfs4_delegreturndata *d_data;
Trond Myklebustc8bf7072018-06-15 16:31:02 -04006514 struct pnfs_layout_hdr *lo;
Andy Adamson938e1012009-04-01 09:22:28 -04006515
6516 d_data = (struct nfs4_delegreturndata *)data;
6517
Trond Myklebust5326de92019-11-13 09:39:36 +01006518 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task)) {
6519 nfs4_sequence_done(task, &d_data->res.seq_res);
Peng Tao500d7012015-09-22 11:35:22 +08006520 return;
Trond Myklebust5326de92019-11-13 09:39:36 +01006521 }
Peng Tao500d7012015-09-22 11:35:22 +08006522
Trond Myklebustc8bf7072018-06-15 16:31:02 -04006523 lo = d_data->args.lr_args ? d_data->args.lr_args->layout : NULL;
6524 if (lo && !pnfs_layout_is_valid(lo)) {
6525 d_data->args.lr_args = NULL;
6526 d_data->res.lr_res = NULL;
6527 }
6528
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006529 nfs4_setup_sequence(d_data->res.server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006530 &d_data->args.seq_args,
6531 &d_data->res.seq_res,
6532 task);
Andy Adamson938e1012009-04-01 09:22:28 -04006533}
Andy Adamson938e1012009-04-01 09:22:28 -04006534
Jesper Juhlc8d149f2006-03-20 13:44:07 -05006535static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04006536 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01006537 .rpc_call_done = nfs4_delegreturn_done,
6538 .rpc_release = nfs4_delegreturn_release,
6539};
6540
NeilBrowna52458b2018-12-03 11:30:31 +11006541static int _nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01006542{
6543 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01006544 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01006545 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006546 struct rpc_message msg = {
6547 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
6548 .rpc_cred = cred,
6549 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006550 struct rpc_task_setup task_setup_data = {
6551 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04006552 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006553 .callback_ops = &nfs4_delegreturn_ops,
Olga Kornievskaia85e39fe2021-06-23 23:28:51 -04006554 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT | RPC_TASK_MOVEABLE,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006555 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05006556 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01006557
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006558 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01006559 if (data == NULL)
6560 return -ENOMEM;
Andrew Elble99ade3c2015-12-02 09:39:51 -05006561
6562 nfs4_state_protect(server->nfs_client,
6563 NFS_SP4_MACH_CRED_CLEANUP,
6564 &task_setup_data.rpc_client, &msg);
6565
Trond Myklebustfe650402006-01-03 09:55:18 +01006566 data->args.fhandle = &data->fh;
6567 data->args.stateid = &data->stateid;
Trond Myklebust332d1a02021-03-25 18:15:36 -04006568 nfs4_bitmask_set(data->args.bitmask_store,
6569 server->cache_consistency_bitmask, inode, server,
6570 NULL);
6571 data->args.bitmask = data->args.bitmask_store;
Trond Myklebustfe650402006-01-03 09:55:18 +01006572 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05006573 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01006574 data->res.fattr = &data->fattr;
6575 data->res.server = server;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006576 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust4d796d72016-09-23 11:38:08 -04006577 data->lr.arg.ld_private = &data->lr.ld_private;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006578 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01006579 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01006580 data->rpc_status = 0;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006581 data->inode = nfs_igrab_and_active(inode);
Trond Myklebust078000d2021-01-04 13:18:03 -05006582 if (data->inode || issync) {
6583 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res,
6584 cred);
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006585 if (data->lr.roc) {
6586 data->args.lr_args = &data->lr.arg;
6587 data->res.lr_res = &data->lr.res;
6588 }
6589 }
Trond Myklebustfe650402006-01-03 09:55:18 +01006590
Trond Myklebustdfe1fe72021-06-01 11:10:05 -04006591 if (!data->inode)
6592 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1,
6593 1);
6594 else
6595 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1,
6596 0);
Trond Myklebustc970aa82007-07-14 15:39:59 -04006597 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05006598 msg.rpc_argp = &data->args;
6599 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006600 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006601 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01006602 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05006603 if (!issync)
6604 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05006605 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05006606 if (status != 0)
6607 goto out;
6608 status = data->rpc_status;
Trond Myklebuste6f81072008-01-24 18:14:34 -05006609out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006610 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01006611 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006612}
6613
NeilBrowna52458b2018-12-03 11:30:31 +11006614int nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006615{
6616 struct nfs_server *server = NFS_SERVER(inode);
6617 struct nfs4_exception exception = { };
6618 int err;
6619 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05006620 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006621 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006622 switch (err) {
6623 case -NFS4ERR_STALE_STATEID:
6624 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006625 case 0:
6626 return 0;
6627 }
6628 err = nfs4_handle_exception(server, err, &exception);
6629 } while (exception.retry);
6630 return err;
6631}
6632
Linus Torvalds1da177e2005-04-16 15:20:36 -07006633static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6634{
6635 struct inode *inode = state->inode;
6636 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04006637 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006638 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006639 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006640 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006641 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006642 struct nfs_lockt_res res = {
6643 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006644 };
6645 struct rpc_message msg = {
6646 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
Anna Schumakerd9b67e12017-01-11 15:04:25 -05006647 .rpc_argp = &arg,
6648 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006649 .rpc_cred = state->owner->so_cred,
6650 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006651 struct nfs4_lock_state *lsp;
6652 int status;
6653
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006654 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00006655 status = nfs4_set_lock_state(state, request);
6656 if (status != 0)
6657 goto out;
6658 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006659 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006660 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00006661 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006662 switch (status) {
6663 case 0:
6664 request->fl_type = F_UNLCK;
6665 break;
6666 case -NFS4ERR_DENIED:
6667 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006668 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05006669 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04006670 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00006671out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006672 return status;
6673}
6674
6675static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6676{
Trond Myklebust0688e642019-04-07 13:59:09 -04006677 struct nfs4_exception exception = {
6678 .interruptible = true,
6679 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006680 int err;
6681
6682 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006683 err = _nfs4_proc_getlk(state, cmd, request);
6684 trace_nfs4_get_lock(request, state, cmd, err);
6685 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006686 &exception);
6687 } while (exception.retry);
6688 return err;
6689}
6690
Trond Myklebust32c6e7e2019-09-20 07:23:48 -04006691/*
6692 * Update the seqid of a lock stateid after receiving
6693 * NFS4ERR_OLD_STATEID
6694 */
6695static bool nfs4_refresh_lock_old_stateid(nfs4_stateid *dst,
6696 struct nfs4_lock_state *lsp)
6697{
6698 struct nfs4_state *state = lsp->ls_state;
6699 bool ret = false;
6700
6701 spin_lock(&state->state_lock);
6702 if (!nfs4_stateid_match_other(dst, &lsp->ls_stateid))
6703 goto out;
6704 if (!nfs4_stateid_is_newer(&lsp->ls_stateid, dst))
6705 nfs4_stateid_seqid_inc(dst);
6706 else
6707 dst->seqid = lsp->ls_stateid.seqid;
6708 ret = true;
6709out:
6710 spin_unlock(&state->state_lock);
6711 return ret;
6712}
6713
6714static bool nfs4_sync_lock_stateid(nfs4_stateid *dst,
6715 struct nfs4_lock_state *lsp)
6716{
6717 struct nfs4_state *state = lsp->ls_state;
6718 bool ret;
6719
6720 spin_lock(&state->state_lock);
6721 ret = !nfs4_stateid_match_other(dst, &lsp->ls_stateid);
6722 nfs4_stateid_copy(dst, &lsp->ls_stateid);
6723 spin_unlock(&state->state_lock);
6724 return ret;
6725}
6726
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006727struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006728 struct nfs_locku_args arg;
6729 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006730 struct nfs4_lock_state *lsp;
6731 struct nfs_open_context *ctx;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006732 struct nfs_lock_context *l_ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006733 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04006734 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006735 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006736};
6737
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006738static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
6739 struct nfs_open_context *ctx,
6740 struct nfs4_lock_state *lsp,
6741 struct nfs_seqid *seqid)
6742{
6743 struct nfs4_unlockdata *p;
Trond Myklebust32c6e7e2019-09-20 07:23:48 -04006744 struct nfs4_state *state = lsp->ls_state;
6745 struct inode *inode = state->inode;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006746
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006747 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006748 if (p == NULL)
6749 return NULL;
6750 p->arg.fh = NFS_FH(inode);
6751 p->arg.fl = &p->fl;
6752 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006753 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006754 p->lsp = lsp;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006755 /* Ensure we don't close file until we're done freeing locks! */
6756 p->ctx = get_nfs_open_context(ctx);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006757 p->l_ctx = nfs_get_lock_context(ctx);
NeilBrown7b587e12018-11-30 10:04:08 +11006758 locks_init_lock(&p->fl);
6759 locks_copy_lock(&p->fl, fl);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006760 p->server = NFS_SERVER(inode);
Trond Myklebust32c6e7e2019-09-20 07:23:48 -04006761 spin_lock(&state->state_lock);
6762 nfs4_stateid_copy(&p->arg.stateid, &lsp->ls_stateid);
6763 spin_unlock(&state->state_lock);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006764 return p;
6765}
6766
Trond Myklebust06f814a2006-01-03 09:55:07 +01006767static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006768{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006769 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006770 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006771 nfs4_put_lock_state(calldata->lsp);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006772 nfs_put_lock_context(calldata->l_ctx);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006773 put_nfs_open_context(calldata->ctx);
6774 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006775}
6776
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006777static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006778{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006779 struct nfs4_unlockdata *calldata = data;
Trond Myklebust82571552017-11-07 11:14:49 -05006780 struct nfs4_exception exception = {
6781 .inode = calldata->lsp->ls_state->inode,
6782 .stateid = &calldata->arg.stateid,
6783 };
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006784
Trond Myklebust14516c32010-07-31 14:29:06 -04006785 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
6786 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006787 switch (task->tk_status) {
6788 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01006789 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton75575dd2016-09-17 18:17:32 -04006790 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006791 if (nfs4_update_lock_stateid(calldata->lsp,
6792 &calldata->res.stateid))
6793 break;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05006794 fallthrough;
Trond Myklebust26d36302016-09-22 13:39:05 -04006795 case -NFS4ERR_ADMIN_REVOKED:
6796 case -NFS4ERR_EXPIRED:
6797 nfs4_free_revoked_stateid(calldata->server,
6798 &calldata->arg.stateid,
6799 task->tk_msg.rpc_cred);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05006800 fallthrough;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05006801 case -NFS4ERR_BAD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006802 case -NFS4ERR_STALE_STATEID:
Trond Myklebust32c6e7e2019-09-20 07:23:48 -04006803 if (nfs4_sync_lock_stateid(&calldata->arg.stateid,
6804 calldata->lsp))
6805 rpc_restart_call_prepare(task);
6806 break;
6807 case -NFS4ERR_OLD_STATEID:
6808 if (nfs4_refresh_lock_old_stateid(&calldata->arg.stateid,
6809 calldata->lsp))
Trond Myklebust425c1d42015-01-24 14:57:53 -05006810 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006811 break;
6812 default:
Trond Myklebust82571552017-11-07 11:14:49 -05006813 task->tk_status = nfs4_async_handle_exception(task,
6814 calldata->server, task->tk_status,
6815 &exception);
6816 if (exception.retry)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006817 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006818 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04006819 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006820}
6821
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006822static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006823{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006824 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006825
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006826 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
6827 nfs_async_iocounter_wait(task, calldata->l_ctx))
6828 return;
6829
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006830 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006831 goto out_wait;
Trond Myklebust795a88c2012-09-10 13:26:49 -04006832 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006833 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006834 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006835 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006836 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006837 if (nfs4_setup_sequence(calldata->server->nfs_client,
Andy Adamsona8936932009-04-01 09:22:23 -04006838 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006839 &calldata->res.seq_res,
6840 task) != 0)
6841 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006842 return;
6843out_no_action:
6844 task->tk_action = NULL;
6845out_wait:
6846 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006847}
6848
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006849static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006850 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006851 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01006852 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006853};
6854
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006855static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6856 struct nfs_open_context *ctx,
6857 struct nfs4_lock_state *lsp,
6858 struct nfs_seqid *seqid)
6859{
6860 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006861 struct rpc_message msg = {
6862 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6863 .rpc_cred = ctx->cred,
6864 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006865 struct rpc_task_setup task_setup_data = {
6866 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006867 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006868 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006869 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006870 .flags = RPC_TASK_ASYNC,
6871 };
Olga Kornievskaia85e39fe2021-06-23 23:28:51 -04006872 struct nfs_client *client =
6873 NFS_SERVER(lsp->ls_state->inode)->nfs_client;
6874
6875 if (client->cl_minorversion)
6876 task_setup_data.flags |= RPC_TASK_MOVEABLE;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006877
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006878 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6879 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6880
Frank Filz137d6ac2007-07-09 15:32:29 -07006881 /* Ensure this is an unlock - when canceling a lock, the
6882 * canceled lock is passed in, and it won't be an unlock.
6883 */
6884 fl->fl_type = F_UNLCK;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006885 if (fl->fl_flags & FL_CLOSE)
6886 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
Frank Filz137d6ac2007-07-09 15:32:29 -07006887
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006888 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6889 if (data == NULL) {
6890 nfs_free_seqid(seqid);
6891 return ERR_PTR(-ENOMEM);
6892 }
6893
Anna Schumakerfba83f32018-05-04 16:22:50 -04006894 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1, 0);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006895 msg.rpc_argp = &data->arg;
6896 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006897 task_setup_data.callback_data = data;
6898 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006899}
6900
Linus Torvalds1da177e2005-04-16 15:20:36 -07006901static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6902{
Trond Myklebust65b62a22013-02-07 10:54:07 -05006903 struct inode *inode = state->inode;
6904 struct nfs4_state_owner *sp = state->owner;
6905 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006906 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006907 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01006908 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006909 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006910 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006911 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006912
Trond Myklebust9b073572006-06-29 16:38:34 -04006913 status = nfs4_set_lock_state(state, request);
6914 /* Unlock _before_ we do the RPC call */
6915 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05006916 /* Exclude nfs_delegation_claim_locks() */
6917 mutex_lock(&sp->so_delegreturn_mutex);
6918 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006919 down_read(&nfsi->rwsem);
Jeff Layton75575dd2016-09-17 18:17:32 -04006920 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05006921 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006922 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006923 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05006924 }
6925 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006926 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006927 if (status != 0)
6928 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006929 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006930 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04006931 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6932 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006933 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6934 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04006935 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006936 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04006937 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006938 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006939 status = PTR_ERR(task);
6940 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04006941 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05006942 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006943 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04006944out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006945 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006946 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006947 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006948}
6949
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006950struct nfs4_lockdata {
6951 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006952 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006953 struct nfs4_lock_state *lsp;
6954 struct nfs_open_context *ctx;
6955 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006956 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006957 int rpc_status;
6958 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04006959 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006960};
6961
6962static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006963 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6964 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006965{
6966 struct nfs4_lockdata *p;
6967 struct inode *inode = lsp->ls_state->inode;
6968 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05006969 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006970
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006971 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006972 if (p == NULL)
6973 return NULL;
6974
6975 p->arg.fh = NFS_FH(inode);
6976 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006977 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006978 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006979 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006980 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6981 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006982 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006983 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04006984 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006985 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006986 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006987 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006988 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04006989 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006990 p->ctx = get_nfs_open_context(ctx);
NeilBrown7b587e12018-11-30 10:04:08 +11006991 locks_init_lock(&p->fl);
6992 locks_copy_lock(&p->fl, fl);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006993 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006994out_free_seqid:
6995 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006996out_free:
6997 kfree(p);
6998 return NULL;
6999}
7000
7001static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
7002{
7003 struct nfs4_lockdata *data = calldata;
7004 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007005
Harvey Harrison3110ff82008-05-02 13:42:44 -07007006 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05007007 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05007008 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007009 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05007010 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007011 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04007012 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007013 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05007014 nfs4_stateid_copy(&data->arg.open_stateid,
7015 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007016 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04007017 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05007018 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05007019 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05007020 nfs4_stateid_copy(&data->arg.lock_stateid,
7021 &data->lsp->ls_stateid);
7022 }
Trond Myklebust5d422302013-03-14 16:57:48 -04007023 if (!nfs4_valid_open_stateid(state)) {
7024 data->rpc_status = -EBADF;
7025 task->tk_action = NULL;
7026 goto out_release_open_seqid;
7027 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01007028 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05007029 if (nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust035168ab2010-06-16 09:52:26 -04007030 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04007031 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007032 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04007033 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04007034out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04007035 nfs_release_seqid(data->arg.open_seqid);
7036out_release_lock_seqid:
7037 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05007038out_wait:
7039 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007040 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08007041}
7042
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007043static void nfs4_lock_done(struct rpc_task *task, void *calldata)
7044{
7045 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05007046 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007047
Harvey Harrison3110ff82008-05-02 13:42:44 -07007048 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007049
Trond Myklebust14516c32010-07-31 14:29:06 -04007050 if (!nfs4_sequence_done(task, &data->res.seq_res))
7051 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04007052
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007053 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05007054 switch (task->tk_status) {
7055 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00007056 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05007057 data->timestamp);
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04007058 if (data->arg.new_lock && !data->cancelled) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05007059 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04007060 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0)
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04007061 goto out_restart;
Trond Myklebustc69899a2015-01-24 16:03:52 -05007062 }
Trond Myklebust39071e62015-01-24 15:07:56 -05007063 if (data->arg.new_lock_owner != 0) {
7064 nfs_confirm_seqid(&lsp->ls_seqid, 0);
7065 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
7066 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04007067 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
7068 goto out_restart;
Trond Myklebust425c1d42015-01-24 14:57:53 -05007069 break;
7070 case -NFS4ERR_BAD_STATEID:
7071 case -NFS4ERR_OLD_STATEID:
7072 case -NFS4ERR_STALE_STATEID:
7073 case -NFS4ERR_EXPIRED:
7074 if (data->arg.new_lock_owner != 0) {
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04007075 if (!nfs4_stateid_match(&data->arg.open_stateid,
Trond Myklebust425c1d42015-01-24 14:57:53 -05007076 &lsp->ls_state->open_stateid))
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04007077 goto out_restart;
7078 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
Trond Myklebust425c1d42015-01-24 14:57:53 -05007079 &lsp->ls_stateid))
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04007080 goto out_restart;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007081 }
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04007082out_done:
Harvey Harrison3110ff82008-05-02 13:42:44 -07007083 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04007084 return;
7085out_restart:
7086 if (!data->cancelled)
7087 rpc_restart_call_prepare(task);
7088 goto out_done;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007089}
7090
7091static void nfs4_lock_release(void *calldata)
7092{
7093 struct nfs4_lockdata *data = calldata;
7094
Harvey Harrison3110ff82008-05-02 13:42:44 -07007095 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05007096 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04007097 if (data->cancelled && data->rpc_status == 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007098 struct rpc_task *task;
7099 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
7100 data->arg.lock_seqid);
7101 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08007102 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07007103 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007104 } else
7105 nfs_free_seqid(data->arg.lock_seqid);
7106 nfs4_put_lock_state(data->lsp);
7107 put_nfs_open_context(data->ctx);
7108 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07007109 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007110}
7111
7112static const struct rpc_call_ops nfs4_lock_ops = {
7113 .rpc_call_prepare = nfs4_lock_prepare,
7114 .rpc_call_done = nfs4_lock_done,
7115 .rpc_release = nfs4_lock_release,
7116};
7117
Trond Myklebust2bee72a2010-01-26 15:42:21 -05007118static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
7119{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05007120 switch (error) {
7121 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustd7f3e4b2016-09-22 13:39:09 -04007122 case -NFS4ERR_EXPIRED:
Trond Myklebust2bee72a2010-01-26 15:42:21 -05007123 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05007124 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05007125 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04007126 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05007127 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05007128 break;
7129 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05007130 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05007131 nfs4_schedule_lease_recovery(server->nfs_client);
zhengbin8b98a532019-12-19 18:34:47 +08007132 }
Trond Myklebust2bee72a2010-01-26 15:42:21 -05007133}
7134
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08007135static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007136{
7137 struct nfs4_lockdata *data;
7138 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04007139 struct rpc_message msg = {
7140 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
7141 .rpc_cred = state->owner->so_cred,
7142 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04007143 struct rpc_task_setup task_setup_data = {
7144 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04007145 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04007146 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05007147 .workqueue = nfsiod_workqueue,
Trond Myklebust61296502020-02-07 19:38:12 -05007148 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
Trond Myklebustc970aa82007-07-14 15:39:59 -04007149 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007150 int ret;
Olga Kornievskaia85e39fe2021-06-23 23:28:51 -04007151 struct nfs_client *client = NFS_SERVER(state->inode)->nfs_client;
7152
7153 if (client->cl_minorversion)
7154 task_setup_data.flags |= RPC_TASK_MOVEABLE;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007155
Harvey Harrison3110ff82008-05-02 13:42:44 -07007156 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04007157 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04007158 fl->fl_u.nfs4_fl.owner,
7159 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007160 if (data == NULL)
7161 return -ENOMEM;
7162 if (IS_SETLKW(cmd))
7163 data->arg.block = 1;
Anna Schumakerfba83f32018-05-04 16:22:50 -04007164 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1,
7165 recovery_type > NFS_LOCK_NEW);
Trond Myklebust1174dd12010-12-21 10:52:24 -05007166 msg.rpc_argp = &data->arg;
7167 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04007168 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007169 if (recovery_type > NFS_LOCK_NEW) {
7170 if (recovery_type == NFS_LOCK_RECLAIM)
7171 data->arg.reclaim = NFS_LOCK_RECLAIM;
Trond Myklebustc69899a2015-01-24 16:03:52 -05007172 } else
7173 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04007174 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05007175 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007176 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05007177 ret = rpc_wait_for_completion_task(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007178 if (ret == 0) {
7179 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05007180 if (ret)
7181 nfs4_handle_setlk_error(data->server, data->lsp,
7182 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007183 } else
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04007184 data->cancelled = true;
Dave Wysochanski3d1a90a2020-12-11 05:12:51 -05007185 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05007186 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07007187 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007188 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007189}
7190
7191static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
7192{
Trond Myklebust202b50d2005-06-22 17:16:29 +00007193 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04007194 struct nfs4_exception exception = {
7195 .inode = state->inode,
7196 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00007197 int err;
7198
7199 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04007200 /* Cache the lock if possible... */
7201 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
7202 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08007203 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04007204 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00007205 break;
7206 nfs4_handle_exception(server, err, &exception);
7207 } while (exception.retry);
7208 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007209}
7210
7211static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
7212{
Trond Myklebust202b50d2005-06-22 17:16:29 +00007213 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04007214 struct nfs4_exception exception = {
7215 .inode = state->inode,
7216 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00007217 int err;
7218
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05007219 err = nfs4_set_lock_state(state, request);
7220 if (err != 0)
7221 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04007222 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10007223 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
7224 return 0;
7225 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00007226 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04007227 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
7228 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08007229 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05007230 switch (err) {
7231 default:
7232 goto out;
7233 case -NFS4ERR_GRACE:
7234 case -NFS4ERR_DELAY:
7235 nfs4_handle_exception(server, err, &exception);
7236 err = 0;
7237 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00007238 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05007239out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00007240 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007241}
7242
Bryan Schumakerf062eb62011-06-02 14:59:10 -04007243#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05007244static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
7245{
Trond Myklebustc5896fc2016-09-22 13:39:03 -04007246 struct nfs4_lock_state *lsp;
7247 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05007248
Trond Myklebustc5896fc2016-09-22 13:39:03 -04007249 status = nfs4_set_lock_state(state, request);
7250 if (status != 0)
7251 return status;
7252 lsp = request->fl_u.nfs4_fl.owner;
7253 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
7254 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
7255 return 0;
Anna Schumaker81b68de2017-01-11 16:41:34 -05007256 return nfs4_lock_expired(state, request);
Bryan Schumakerf062eb62011-06-02 14:59:10 -04007257}
7258#endif
7259
Linus Torvalds1da177e2005-04-16 15:20:36 -07007260static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7261{
Trond Myklebust19e03c52008-12-23 15:21:44 -05007262 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04007263 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04007264 unsigned char fl_flags = request->fl_flags;
Jeff Layton1ea67db2016-09-17 18:17:37 -04007265 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007266
Trond Myklebust01c3b862006-06-29 16:38:39 -04007267 request->fl_flags |= FL_ACCESS;
Jeff Layton75575dd2016-09-17 18:17:32 -04007268 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04007269 if (status < 0)
7270 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04007271 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05007272 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04007273 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04007274 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04007275 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05007276 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton75575dd2016-09-17 18:17:32 -04007277 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05007278 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04007279 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05007280 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04007281 }
Trond Myklebust9a99af42013-02-04 20:17:49 -05007282 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04007283 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08007284 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04007285out:
7286 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007287 return status;
7288}
7289
7290static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7291{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05007292 struct nfs4_exception exception = {
7293 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04007294 .inode = state->inode,
Trond Myklebust0688e642019-04-07 13:59:09 -04007295 .interruptible = true,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05007296 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07007297 int err;
7298
7299 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07007300 err = _nfs4_proc_setlk(state, cmd, request);
7301 if (err == -NFS4ERR_DENIED)
7302 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007303 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07007304 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007305 } while (exception.retry);
7306 return err;
7307}
7308
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04007309#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
7310#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
7311
7312static int
Jeff Laytona1d617d82016-09-17 18:17:39 -04007313nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
7314 struct file_lock *request)
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04007315{
7316 int status = -ERESTARTSYS;
7317 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
7318
7319 while(!signalled()) {
7320 status = nfs4_proc_setlk(state, cmd, request);
7321 if ((status != -EAGAIN) || IS_SETLK(cmd))
7322 break;
7323 freezable_schedule_timeout_interruptible(timeout);
7324 timeout *= 2;
7325 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
7326 status = -ERESTARTSYS;
7327 }
7328 return status;
7329}
7330
Jeff Laytona1d617d82016-09-17 18:17:39 -04007331#ifdef CONFIG_NFS_V4_1
7332struct nfs4_lock_waiter {
Jeff Laytona1d617d82016-09-17 18:17:39 -04007333 struct inode *inode;
Trond Myklebust86438182021-02-02 14:22:51 -05007334 struct nfs_lowner owner;
7335 wait_queue_entry_t wait;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007336};
7337
7338static int
Ingo Molnarac6424b2017-06-20 12:06:13 +02007339nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
Jeff Laytona1d617d82016-09-17 18:17:39 -04007340{
Trond Myklebust86438182021-02-02 14:22:51 -05007341 struct nfs4_lock_waiter *waiter =
7342 container_of(wait, struct nfs4_lock_waiter, wait);
Jeff Laytona1d617d82016-09-17 18:17:39 -04007343
Jeff Layton57174592018-03-18 08:37:03 -04007344 /* NULL key means to wake up everyone */
7345 if (key) {
7346 struct cb_notify_lock_args *cbnl = key;
7347 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
Trond Myklebust86438182021-02-02 14:22:51 -05007348 *wowner = &waiter->owner;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007349
Jeff Layton57174592018-03-18 08:37:03 -04007350 /* Only wake if the callback was for the same owner. */
7351 if (lowner->id != wowner->id || lowner->s_dev != wowner->s_dev)
7352 return 0;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007353
Jeff Layton57174592018-03-18 08:37:03 -04007354 /* Make sure it's for the right inode */
7355 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
7356 return 0;
Jeff Layton57174592018-03-18 08:37:03 -04007357 }
Jeff Laytona1d617d82016-09-17 18:17:39 -04007358
Trond Myklebust86438182021-02-02 14:22:51 -05007359 return woken_wake_function(wait, mode, flags, key);
Jeff Laytona1d617d82016-09-17 18:17:39 -04007360}
7361
7362static int
7363nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7364{
Jeff Laytona1d617d82016-09-17 18:17:39 -04007365 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
7366 struct nfs_server *server = NFS_SERVER(state->inode);
7367 struct nfs_client *clp = server->nfs_client;
7368 wait_queue_head_t *q = &clp->cl_lock_waitq;
Trond Myklebust86438182021-02-02 14:22:51 -05007369 struct nfs4_lock_waiter waiter = {
7370 .inode = state->inode,
7371 .owner = { .clientid = clp->cl_clientid,
7372 .id = lsp->ls_seqid.owner_id,
7373 .s_dev = server->s_dev },
7374 };
7375 int status;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007376
7377 /* Don't bother with waitqueue if we don't expect a callback */
7378 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
7379 return nfs4_retry_setlk_simple(state, cmd, request);
7380
Trond Myklebust86438182021-02-02 14:22:51 -05007381 init_wait(&waiter.wait);
7382 waiter.wait.func = nfs4_wake_lock_waiter;
7383 add_wait_queue(q, &waiter.wait);
Jeff Laytona1d617d82016-09-17 18:17:39 -04007384
Trond Myklebust86438182021-02-02 14:22:51 -05007385 do {
Jeff Laytona1d617d82016-09-17 18:17:39 -04007386 status = nfs4_proc_setlk(state, cmd, request);
Trond Myklebust86438182021-02-02 14:22:51 -05007387 if (status != -EAGAIN || IS_SETLK(cmd))
Jeff Laytona1d617d82016-09-17 18:17:39 -04007388 break;
7389
7390 status = -ERESTARTSYS;
Yihao Wu52b042a2019-05-22 01:57:10 +08007391 freezer_do_not_count();
Trond Myklebust86438182021-02-02 14:22:51 -05007392 wait_woken(&waiter.wait, TASK_INTERRUPTIBLE,
7393 NFS4_LOCK_MAXTIMEOUT);
Yihao Wu52b042a2019-05-22 01:57:10 +08007394 freezer_count();
Trond Myklebust86438182021-02-02 14:22:51 -05007395 } while (!signalled());
7396
7397 remove_wait_queue(q, &waiter.wait);
Jeff Laytona1d617d82016-09-17 18:17:39 -04007398
Jeff Laytona1d617d82016-09-17 18:17:39 -04007399 return status;
7400}
7401#else /* !CONFIG_NFS_V4_1 */
7402static inline int
7403nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7404{
7405 return nfs4_retry_setlk_simple(state, cmd, request);
7406}
7407#endif
7408
Linus Torvalds1da177e2005-04-16 15:20:36 -07007409static int
7410nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
7411{
7412 struct nfs_open_context *ctx;
7413 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007414 int status;
7415
7416 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04007417 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007418 state = ctx->state;
7419
Trond Myklebustd9531262009-07-21 19:22:38 -04007420 if (IS_GETLK(cmd)) {
7421 if (state != NULL)
7422 return nfs4_proc_getlk(state, F_GETLK, request);
7423 return 0;
7424 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007425
7426 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
7427 return -EINVAL;
7428
Trond Myklebustd9531262009-07-21 19:22:38 -04007429 if (request->fl_type == F_UNLCK) {
7430 if (state != NULL)
7431 return nfs4_proc_unlck(state, cmd, request);
7432 return 0;
7433 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007434
Trond Myklebustd9531262009-07-21 19:22:38 -04007435 if (state == NULL)
7436 return -ENOLCK;
Jeff Layton1ea67db2016-09-17 18:17:37 -04007437
7438 if ((request->fl_flags & FL_POSIX) &&
7439 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
7440 return -ENOLCK;
7441
Benjamin Coddingtonfcfa4472017-11-10 06:27:49 -05007442 /*
7443 * Don't rely on the VFS having checked the file open mode,
7444 * since it won't do this for flock() locks.
7445 */
7446 switch (request->fl_type) {
7447 case F_RDLCK:
7448 if (!(filp->f_mode & FMODE_READ))
7449 return -EBADF;
7450 break;
7451 case F_WRLCK:
7452 if (!(filp->f_mode & FMODE_WRITE))
7453 return -EBADF;
7454 }
7455
Jeff Layton1ea67db2016-09-17 18:17:37 -04007456 status = nfs4_set_lock_state(state, request);
7457 if (status != 0)
7458 return status;
7459
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04007460 return nfs4_retry_setlk(state, cmd, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007461}
7462
Trond Myklebuste93a5e92021-05-07 10:06:13 -04007463static int nfs4_delete_lease(struct file *file, void **priv)
7464{
7465 return generic_setlease(file, F_UNLCK, NULL, priv);
7466}
7467
7468static int nfs4_add_lease(struct file *file, long arg, struct file_lock **lease,
7469 void **priv)
7470{
7471 struct inode *inode = file_inode(file);
7472 fmode_t type = arg == F_RDLCK ? FMODE_READ : FMODE_WRITE;
7473 int ret;
7474
7475 /* No delegation, no lease */
7476 if (!nfs4_have_delegation(inode, type))
Trond Myklebustdf2c7b92021-06-25 15:08:39 -04007477 return -EAGAIN;
Trond Myklebuste93a5e92021-05-07 10:06:13 -04007478 ret = generic_setlease(file, arg, lease, priv);
7479 if (ret || nfs4_have_delegation(inode, type))
7480 return ret;
7481 /* We raced with a delegation return */
7482 nfs4_delete_lease(file, priv);
Trond Myklebustdf2c7b92021-06-25 15:08:39 -04007483 return -EAGAIN;
Trond Myklebuste93a5e92021-05-07 10:06:13 -04007484}
7485
7486int nfs4_proc_setlease(struct file *file, long arg, struct file_lock **lease,
7487 void **priv)
7488{
7489 switch (arg) {
7490 case F_RDLCK:
7491 case F_WRLCK:
7492 return nfs4_add_lease(file, arg, lease, priv);
7493 case F_UNLCK:
7494 return nfs4_delete_lease(file, priv);
7495 default:
7496 return -EINVAL;
7497 }
7498}
7499
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04007500int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05007501{
7502 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05007503 int err;
7504
7505 err = nfs4_set_lock_state(state, fl);
7506 if (err != 0)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04007507 return err;
Olga Kornievskaia3d7a9522020-08-20 18:52:43 -04007508 do {
7509 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
7510 if (err != -NFS4ERR_DELAY)
7511 break;
7512 ssleep(1);
7513 } while (err == -NFS4ERR_DELAY);
NeilBrowndce26302017-12-13 09:57:09 +11007514 return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05007515}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007516
Trond Myklebustcf470c32012-03-07 13:49:12 -05007517struct nfs_release_lockowner_data {
7518 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04007519 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007520 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08007521 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04007522 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007523};
7524
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007525static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
7526{
7527 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08007528 struct nfs_server *server = data->server;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05007529 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
7530 &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08007531 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04007532 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007533}
7534
7535static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
7536{
7537 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04007538 struct nfs_server *server = data->server;
7539
Trond Myklebustb7e63a12014-02-26 11:19:14 -08007540 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04007541
7542 switch (task->tk_status) {
7543 case 0:
7544 renew_lease(server, data->timestamp);
7545 break;
7546 case -NFS4ERR_STALE_CLIENTID:
7547 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08007548 nfs4_schedule_lease_recovery(server->nfs_client);
7549 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04007550 case -NFS4ERR_LEASE_MOVED:
7551 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10007552 if (nfs4_async_handle_error(task, server,
7553 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04007554 rpc_restart_call_prepare(task);
7555 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007556}
7557
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007558static void nfs4_release_lockowner_release(void *calldata)
7559{
Trond Myklebustcf470c32012-03-07 13:49:12 -05007560 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04007561 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007562 kfree(calldata);
7563}
7564
Trond Myklebust17280172012-03-11 13:11:00 -04007565static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007566 .rpc_call_prepare = nfs4_release_lockowner_prepare,
7567 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007568 .rpc_release = nfs4_release_lockowner_release,
7569};
7570
Jeff Laytonf1cdae82014-05-01 06:28:47 -04007571static void
7572nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007573{
Trond Myklebustcf470c32012-03-07 13:49:12 -05007574 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007575 struct rpc_message msg = {
7576 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
7577 };
7578
7579 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04007580 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007581
Trond Myklebustcf470c32012-03-07 13:49:12 -05007582 data = kmalloc(sizeof(*data), GFP_NOFS);
7583 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04007584 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007585 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04007586 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007587 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7588 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
7589 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007590
Trond Myklebustcf470c32012-03-07 13:49:12 -05007591 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08007592 msg.rpc_resp = &data->res;
Anna Schumakerfba83f32018-05-04 16:22:50 -04007593 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05007594 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007595}
7596
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00007597#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
7598
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007599static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Christian Braunere65ce2a2021-01-21 14:19:27 +01007600 struct user_namespace *mnt_userns,
Al Viro59301222016-05-27 10:19:30 -04007601 struct dentry *unused, struct inode *inode,
7602 const char *key, const void *buf,
7603 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007604{
Al Viro59301222016-05-27 10:19:30 -04007605 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007606}
7607
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007608static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04007609 struct dentry *unused, struct inode *inode,
7610 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007611{
Al Virob2968212016-04-10 20:48:24 -04007612 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007613}
7614
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01007615static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007616{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01007617 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007618}
7619
David Quigleyc9bccef2013-05-22 12:50:45 -04007620#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04007621
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007622static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Christian Braunere65ce2a2021-01-21 14:19:27 +01007623 struct user_namespace *mnt_userns,
Al Viro59301222016-05-27 10:19:30 -04007624 struct dentry *unused, struct inode *inode,
7625 const char *key, const void *buf,
7626 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04007627{
7628 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04007629 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04007630
7631 return -EOPNOTSUPP;
7632}
7633
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007634static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04007635 struct dentry *unused, struct inode *inode,
7636 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04007637{
7638 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04007639 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04007640 return -EOPNOTSUPP;
7641}
7642
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007643static ssize_t
7644nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04007645{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007646 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04007647
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007648 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
7649 len = security_inode_listsecurity(inode, list, list_len);
Chengguang Xu82c596e2020-06-17 09:09:39 +08007650 if (len >= 0 && list_len && len > list_len)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007651 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04007652 }
7653 return len;
7654}
7655
7656static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
7657 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04007658 .get = nfs4_xattr_get_nfs4_label,
7659 .set = nfs4_xattr_set_nfs4_label,
7660};
David Quigleyc9bccef2013-05-22 12:50:45 -04007661
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007662#else
7663
7664static ssize_t
7665nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7666{
7667 return 0;
7668}
7669
7670#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04007671
Frank van der Linden012a2112020-06-23 22:39:03 +00007672#ifdef CONFIG_NFS_V4_2
7673static int nfs4_xattr_set_nfs4_user(const struct xattr_handler *handler,
Christian Braunere65ce2a2021-01-21 14:19:27 +01007674 struct user_namespace *mnt_userns,
Frank van der Linden012a2112020-06-23 22:39:03 +00007675 struct dentry *unused, struct inode *inode,
7676 const char *key, const void *buf,
7677 size_t buflen, int flags)
7678{
7679 struct nfs_access_entry cache;
Frank van der Linden95ad37f2020-06-23 22:39:04 +00007680 int ret;
Frank van der Linden012a2112020-06-23 22:39:03 +00007681
7682 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7683 return -EOPNOTSUPP;
7684
7685 /*
7686 * There is no mapping from the MAY_* flags to the NFS_ACCESS_XA*
7687 * flags right now. Handling of xattr operations use the normal
7688 * file read/write permissions.
7689 *
7690 * Just in case the server has other ideas (which RFC 8276 allows),
7691 * do a cached access check for the XA* flags to possibly avoid
7692 * doing an RPC and getting EACCES back.
7693 */
7694 if (!nfs_access_get_cached(inode, current_cred(), &cache, true)) {
7695 if (!(cache.mask & NFS_ACCESS_XAWRITE))
7696 return -EACCES;
7697 }
7698
Frank van der Linden95ad37f2020-06-23 22:39:04 +00007699 if (buf == NULL) {
7700 ret = nfs42_proc_removexattr(inode, key);
7701 if (!ret)
7702 nfs4_xattr_cache_remove(inode, key);
7703 } else {
7704 ret = nfs42_proc_setxattr(inode, key, buf, buflen, flags);
7705 if (!ret)
7706 nfs4_xattr_cache_add(inode, key, buf, NULL, buflen);
7707 }
7708
7709 return ret;
Frank van der Linden012a2112020-06-23 22:39:03 +00007710}
7711
7712static int nfs4_xattr_get_nfs4_user(const struct xattr_handler *handler,
7713 struct dentry *unused, struct inode *inode,
7714 const char *key, void *buf, size_t buflen)
7715{
7716 struct nfs_access_entry cache;
Frank van der Linden95ad37f2020-06-23 22:39:04 +00007717 ssize_t ret;
Frank van der Linden012a2112020-06-23 22:39:03 +00007718
7719 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7720 return -EOPNOTSUPP;
7721
7722 if (!nfs_access_get_cached(inode, current_cred(), &cache, true)) {
7723 if (!(cache.mask & NFS_ACCESS_XAREAD))
7724 return -EACCES;
7725 }
7726
Trond Myklebust1f3208b2021-03-25 11:04:34 -04007727 ret = nfs_revalidate_inode(inode, NFS_INO_INVALID_CHANGE);
Frank van der Linden95ad37f2020-06-23 22:39:04 +00007728 if (ret)
7729 return ret;
7730
7731 ret = nfs4_xattr_cache_get(inode, key, buf, buflen);
7732 if (ret >= 0 || (ret < 0 && ret != -ENOENT))
7733 return ret;
7734
7735 ret = nfs42_proc_getxattr(inode, key, buf, buflen);
7736
7737 return ret;
Frank van der Linden012a2112020-06-23 22:39:03 +00007738}
7739
7740static ssize_t
7741nfs4_listxattr_nfs4_user(struct inode *inode, char *list, size_t list_len)
7742{
7743 u64 cookie;
7744 bool eof;
Frank van der Linden95ad37f2020-06-23 22:39:04 +00007745 ssize_t ret, size;
Frank van der Linden012a2112020-06-23 22:39:03 +00007746 char *buf;
7747 size_t buflen;
7748 struct nfs_access_entry cache;
7749
7750 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7751 return 0;
7752
7753 if (!nfs_access_get_cached(inode, current_cred(), &cache, true)) {
7754 if (!(cache.mask & NFS_ACCESS_XALIST))
7755 return 0;
7756 }
7757
Trond Myklebust1f3208b2021-03-25 11:04:34 -04007758 ret = nfs_revalidate_inode(inode, NFS_INO_INVALID_CHANGE);
Frank van der Linden95ad37f2020-06-23 22:39:04 +00007759 if (ret)
7760 return ret;
7761
7762 ret = nfs4_xattr_cache_list(inode, list, list_len);
7763 if (ret >= 0 || (ret < 0 && ret != -ENOENT))
7764 return ret;
7765
Frank van der Linden012a2112020-06-23 22:39:03 +00007766 cookie = 0;
7767 eof = false;
7768 buflen = list_len ? list_len : XATTR_LIST_MAX;
7769 buf = list_len ? list : NULL;
7770 size = 0;
7771
7772 while (!eof) {
7773 ret = nfs42_proc_listxattrs(inode, buf, buflen,
7774 &cookie, &eof);
7775 if (ret < 0)
7776 return ret;
7777
7778 if (list_len) {
7779 buf += ret;
7780 buflen -= ret;
7781 }
7782 size += ret;
7783 }
7784
Frank van der Linden95ad37f2020-06-23 22:39:04 +00007785 if (list_len)
7786 nfs4_xattr_cache_set_list(inode, list, size);
7787
Frank van der Linden012a2112020-06-23 22:39:03 +00007788 return size;
7789}
7790
7791#else
7792
7793static ssize_t
7794nfs4_listxattr_nfs4_user(struct inode *inode, char *list, size_t list_len)
7795{
7796 return 0;
7797}
7798#endif /* CONFIG_NFS_V4_2 */
7799
Andy Adamson533eb462011-06-13 18:25:56 -04007800/*
7801 * nfs_fhget will use either the mounted_on_fileid or the fileid
7802 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007803static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
7804{
Andy Adamson533eb462011-06-13 18:25:56 -04007805 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
7806 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
7807 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05007808 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007809 return;
7810
7811 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05007812 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007813 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
7814 fattr->nlink = 2;
7815}
7816
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007817static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7818 const struct qstr *name,
7819 struct nfs4_fs_locations *fs_locations,
7820 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04007821{
7822 struct nfs_server *server = NFS_SERVER(dir);
Chuck Leverc05cefc2017-11-05 15:45:22 -05007823 u32 bitmask[3];
Trond Myklebust683b57b2006-06-09 09:34:22 -04007824 struct nfs4_fs_locations_arg args = {
7825 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05007826 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04007827 .page = page,
7828 .bitmask = bitmask,
7829 };
Benny Halevy22958462009-04-01 09:22:02 -04007830 struct nfs4_fs_locations_res res = {
7831 .fs_locations = fs_locations,
7832 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04007833 struct rpc_message msg = {
7834 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7835 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04007836 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04007837 };
7838 int status;
7839
Harvey Harrison3110ff82008-05-02 13:42:44 -07007840 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04007841
Chuck Leverc05cefc2017-11-05 15:45:22 -05007842 bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
7843 bitmask[1] = nfs4_fattr_bitmap[1];
7844
Andy Adamson533eb462011-06-13 18:25:56 -04007845 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
7846 * is not supported */
7847 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
Chuck Leverc05cefc2017-11-05 15:45:22 -05007848 bitmask[0] &= ~FATTR4_WORD0_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04007849 else
Chuck Leverc05cefc2017-11-05 15:45:22 -05007850 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04007851
Trond Myklebustc228fd32007-01-13 02:28:11 -05007852 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04007853 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04007854 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007855 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07007856 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04007857 return status;
7858}
7859
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007860int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7861 const struct qstr *name,
7862 struct nfs4_fs_locations *fs_locations,
7863 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007864{
Trond Myklebust0688e642019-04-07 13:59:09 -04007865 struct nfs4_exception exception = {
7866 .interruptible = true,
7867 };
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007868 int err;
7869 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007870 err = _nfs4_proc_fs_locations(client, dir, name,
7871 fs_locations, page);
7872 trace_nfs4_get_fs_locations(dir, name, err);
7873 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007874 &exception);
7875 } while (exception.retry);
7876 return err;
7877}
7878
Chuck Leverb03d7352013-10-17 14:12:50 -04007879/*
7880 * This operation also signals the server that this client is
7881 * performing migration recovery. The server can stop returning
7882 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
7883 * appended to this compound to identify the client ID which is
7884 * performing recovery.
7885 */
7886static int _nfs40_proc_get_locations(struct inode *inode,
7887 struct nfs4_fs_locations *locations,
NeilBrowna52458b2018-12-03 11:30:31 +11007888 struct page *page, const struct cred *cred)
Chuck Leverb03d7352013-10-17 14:12:50 -04007889{
7890 struct nfs_server *server = NFS_SERVER(inode);
7891 struct rpc_clnt *clnt = server->client;
7892 u32 bitmask[2] = {
7893 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7894 };
7895 struct nfs4_fs_locations_arg args = {
7896 .clientid = server->nfs_client->cl_clientid,
7897 .fh = NFS_FH(inode),
7898 .page = page,
7899 .bitmask = bitmask,
7900 .migration = 1, /* skip LOOKUP */
7901 .renew = 1, /* append RENEW */
7902 };
7903 struct nfs4_fs_locations_res res = {
7904 .fs_locations = locations,
7905 .migration = 1,
7906 .renew = 1,
7907 };
7908 struct rpc_message msg = {
7909 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7910 .rpc_argp = &args,
7911 .rpc_resp = &res,
7912 .rpc_cred = cred,
7913 };
7914 unsigned long now = jiffies;
7915 int status;
7916
7917 nfs_fattr_init(&locations->fattr);
7918 locations->server = server;
7919 locations->nlocations = 0;
7920
Anna Schumakerfba83f32018-05-04 16:22:50 -04007921 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Leverb03d7352013-10-17 14:12:50 -04007922 status = nfs4_call_sync_sequence(clnt, server, &msg,
7923 &args.seq_args, &res.seq_res);
7924 if (status)
7925 return status;
7926
7927 renew_lease(server, now);
7928 return 0;
7929}
7930
7931#ifdef CONFIG_NFS_V4_1
7932
7933/*
7934 * This operation also signals the server that this client is
7935 * performing migration recovery. The server can stop asserting
7936 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
7937 * performing this operation is identified in the SEQUENCE
7938 * operation in this compound.
7939 *
7940 * When the client supports GETATTR(fs_locations_info), it can
7941 * be plumbed in here.
7942 */
7943static int _nfs41_proc_get_locations(struct inode *inode,
7944 struct nfs4_fs_locations *locations,
NeilBrowna52458b2018-12-03 11:30:31 +11007945 struct page *page, const struct cred *cred)
Chuck Leverb03d7352013-10-17 14:12:50 -04007946{
7947 struct nfs_server *server = NFS_SERVER(inode);
7948 struct rpc_clnt *clnt = server->client;
7949 u32 bitmask[2] = {
7950 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7951 };
7952 struct nfs4_fs_locations_arg args = {
7953 .fh = NFS_FH(inode),
7954 .page = page,
7955 .bitmask = bitmask,
7956 .migration = 1, /* skip LOOKUP */
7957 };
7958 struct nfs4_fs_locations_res res = {
7959 .fs_locations = locations,
7960 .migration = 1,
7961 };
7962 struct rpc_message msg = {
7963 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7964 .rpc_argp = &args,
7965 .rpc_resp = &res,
7966 .rpc_cred = cred,
7967 };
7968 int status;
7969
7970 nfs_fattr_init(&locations->fattr);
7971 locations->server = server;
7972 locations->nlocations = 0;
7973
Anna Schumakerfba83f32018-05-04 16:22:50 -04007974 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Leverb03d7352013-10-17 14:12:50 -04007975 status = nfs4_call_sync_sequence(clnt, server, &msg,
7976 &args.seq_args, &res.seq_res);
7977 if (status == NFS4_OK &&
7978 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7979 status = -NFS4ERR_LEASE_MOVED;
7980 return status;
7981}
7982
7983#endif /* CONFIG_NFS_V4_1 */
7984
7985/**
7986 * nfs4_proc_get_locations - discover locations for a migrated FSID
7987 * @inode: inode on FSID that is migrating
7988 * @locations: result of query
7989 * @page: buffer
7990 * @cred: credential to use for this operation
7991 *
7992 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
7993 * operation failed, or a negative errno if a local error occurred.
7994 *
7995 * On success, "locations" is filled in, but if the server has
7996 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
7997 * asserted.
7998 *
7999 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
8000 * from this client that require migration recovery.
8001 */
8002int nfs4_proc_get_locations(struct inode *inode,
8003 struct nfs4_fs_locations *locations,
NeilBrowna52458b2018-12-03 11:30:31 +11008004 struct page *page, const struct cred *cred)
Chuck Leverb03d7352013-10-17 14:12:50 -04008005{
8006 struct nfs_server *server = NFS_SERVER(inode);
8007 struct nfs_client *clp = server->nfs_client;
8008 const struct nfs4_mig_recovery_ops *ops =
8009 clp->cl_mvops->mig_recovery_ops;
Trond Myklebust0688e642019-04-07 13:59:09 -04008010 struct nfs4_exception exception = {
8011 .interruptible = true,
8012 };
Chuck Leverb03d7352013-10-17 14:12:50 -04008013 int status;
8014
8015 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
8016 (unsigned long long)server->fsid.major,
8017 (unsigned long long)server->fsid.minor,
8018 clp->cl_hostname);
8019 nfs_display_fhandle(NFS_FH(inode), __func__);
8020
8021 do {
8022 status = ops->get_locations(inode, locations, page, cred);
8023 if (status != -NFS4ERR_DELAY)
8024 break;
8025 nfs4_handle_exception(server, status, &exception);
8026 } while (exception.retry);
8027 return status;
8028}
8029
Chuck Lever44c99932013-10-17 14:13:30 -04008030/*
8031 * This operation also signals the server that this client is
8032 * performing "lease moved" recovery. The server can stop
8033 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
8034 * is appended to this compound to identify the client ID which is
8035 * performing recovery.
8036 */
NeilBrowna52458b2018-12-03 11:30:31 +11008037static int _nfs40_proc_fsid_present(struct inode *inode, const struct cred *cred)
Chuck Lever44c99932013-10-17 14:13:30 -04008038{
8039 struct nfs_server *server = NFS_SERVER(inode);
8040 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
8041 struct rpc_clnt *clnt = server->client;
8042 struct nfs4_fsid_present_arg args = {
8043 .fh = NFS_FH(inode),
8044 .clientid = clp->cl_clientid,
8045 .renew = 1, /* append RENEW */
8046 };
8047 struct nfs4_fsid_present_res res = {
8048 .renew = 1,
8049 };
8050 struct rpc_message msg = {
8051 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
8052 .rpc_argp = &args,
8053 .rpc_resp = &res,
8054 .rpc_cred = cred,
8055 };
8056 unsigned long now = jiffies;
8057 int status;
8058
8059 res.fh = nfs_alloc_fhandle();
8060 if (res.fh == NULL)
8061 return -ENOMEM;
8062
Anna Schumakerfba83f32018-05-04 16:22:50 -04008063 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Lever44c99932013-10-17 14:13:30 -04008064 status = nfs4_call_sync_sequence(clnt, server, &msg,
8065 &args.seq_args, &res.seq_res);
8066 nfs_free_fhandle(res.fh);
8067 if (status)
8068 return status;
8069
8070 do_renew_lease(clp, now);
8071 return 0;
8072}
8073
8074#ifdef CONFIG_NFS_V4_1
8075
8076/*
8077 * This operation also signals the server that this client is
8078 * performing "lease moved" recovery. The server can stop asserting
8079 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
8080 * this operation is identified in the SEQUENCE operation in this
8081 * compound.
8082 */
NeilBrowna52458b2018-12-03 11:30:31 +11008083static int _nfs41_proc_fsid_present(struct inode *inode, const struct cred *cred)
Chuck Lever44c99932013-10-17 14:13:30 -04008084{
8085 struct nfs_server *server = NFS_SERVER(inode);
8086 struct rpc_clnt *clnt = server->client;
8087 struct nfs4_fsid_present_arg args = {
8088 .fh = NFS_FH(inode),
8089 };
8090 struct nfs4_fsid_present_res res = {
8091 };
8092 struct rpc_message msg = {
8093 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
8094 .rpc_argp = &args,
8095 .rpc_resp = &res,
8096 .rpc_cred = cred,
8097 };
8098 int status;
8099
8100 res.fh = nfs_alloc_fhandle();
8101 if (res.fh == NULL)
8102 return -ENOMEM;
8103
Anna Schumakerfba83f32018-05-04 16:22:50 -04008104 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Lever44c99932013-10-17 14:13:30 -04008105 status = nfs4_call_sync_sequence(clnt, server, &msg,
8106 &args.seq_args, &res.seq_res);
8107 nfs_free_fhandle(res.fh);
8108 if (status == NFS4_OK &&
8109 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
8110 status = -NFS4ERR_LEASE_MOVED;
8111 return status;
8112}
8113
8114#endif /* CONFIG_NFS_V4_1 */
8115
8116/**
8117 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
8118 * @inode: inode on FSID to check
8119 * @cred: credential to use for this operation
8120 *
8121 * Server indicates whether the FSID is present, moved, or not
8122 * recognized. This operation is necessary to clear a LEASE_MOVED
8123 * condition for this client ID.
8124 *
8125 * Returns NFS4_OK if the FSID is present on this server,
8126 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
8127 * NFS4ERR code if some error occurred on the server, or a
8128 * negative errno if a local failure occurred.
8129 */
NeilBrowna52458b2018-12-03 11:30:31 +11008130int nfs4_proc_fsid_present(struct inode *inode, const struct cred *cred)
Chuck Lever44c99932013-10-17 14:13:30 -04008131{
8132 struct nfs_server *server = NFS_SERVER(inode);
8133 struct nfs_client *clp = server->nfs_client;
8134 const struct nfs4_mig_recovery_ops *ops =
8135 clp->cl_mvops->mig_recovery_ops;
Trond Myklebust0688e642019-04-07 13:59:09 -04008136 struct nfs4_exception exception = {
8137 .interruptible = true,
8138 };
Chuck Lever44c99932013-10-17 14:13:30 -04008139 int status;
8140
8141 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
8142 (unsigned long long)server->fsid.major,
8143 (unsigned long long)server->fsid.minor,
8144 clp->cl_hostname);
8145 nfs_display_fhandle(NFS_FH(inode), __func__);
8146
8147 do {
8148 status = ops->fsid_present(inode, cred);
8149 if (status != -NFS4ERR_DELAY)
8150 break;
8151 nfs4_handle_exception(server, status, &exception);
8152 } while (exception.retry);
8153 return status;
8154}
8155
Trond Myklebust302fad72019-02-18 13:32:38 -05008156/*
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04008157 * If 'use_integrity' is true and the state managment nfs_client
8158 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
8159 * and the machine credential as per RFC3530bis and RFC5661 Security
8160 * Considerations sections. Otherwise, just use the user cred with the
8161 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04008162 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04008163static 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 +00008164{
8165 int status;
Anna Schumaker50493362019-08-14 15:30:16 -04008166 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
8167 struct nfs_client *clp = NFS_SERVER(dir)->nfs_client;
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00008168 struct nfs4_secinfo_arg args = {
8169 .dir_fh = NFS_FH(dir),
8170 .name = name,
8171 };
8172 struct nfs4_secinfo_res res = {
8173 .flavors = flavors,
8174 };
8175 struct rpc_message msg = {
8176 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
8177 .rpc_argp = &args,
8178 .rpc_resp = &res,
8179 };
Anna Schumaker50493362019-08-14 15:30:16 -04008180 struct nfs4_call_sync_data data = {
8181 .seq_server = NFS_SERVER(dir),
8182 .seq_args = &args.seq_args,
8183 .seq_res = &res.seq_res,
8184 };
8185 struct rpc_task_setup task_setup = {
8186 .rpc_client = clnt,
8187 .rpc_message = &msg,
8188 .callback_ops = clp->cl_mvops->call_sync_ops,
8189 .callback_data = &data,
8190 .flags = RPC_TASK_NO_ROUND_ROBIN,
8191 };
NeilBrowna52458b2018-12-03 11:30:31 +11008192 const struct cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04008193
8194 if (use_integrity) {
Anna Schumaker50493362019-08-14 15:30:16 -04008195 clnt = clp->cl_rpcclient;
8196 task_setup.rpc_client = clnt;
8197
8198 cred = nfs4_get_clid_cred(clp);
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008199 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04008200 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00008201
8202 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008203
Anna Schumaker50493362019-08-14 15:30:16 -04008204 nfs4_state_protect(clp, NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
8205 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
8206 status = nfs4_call_sync_custom(&task_setup);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008207
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00008208 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04008209
NeilBrowna52458b2018-12-03 11:30:31 +11008210 put_cred(cred);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00008211 return status;
8212}
8213
Bryan Schumaker72de53e2012-04-27 13:27:40 -04008214int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
8215 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00008216{
Trond Myklebust0688e642019-04-07 13:59:09 -04008217 struct nfs4_exception exception = {
8218 .interruptible = true,
8219 };
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00008220 int err;
8221 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04008222 err = -NFS4ERR_WRONGSEC;
8223
8224 /* try to use integrity protection with machine cred */
8225 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
8226 err = _nfs4_proc_secinfo(dir, name, flavors, true);
8227
8228 /*
8229 * if unable to use integrity protection, or SECINFO with
8230 * integrity protection returns NFS4ERR_WRONGSEC (which is
8231 * disallowed by spec, but exists in deployed servers) use
8232 * the current filesystem's rpc_client and the user cred.
8233 */
8234 if (err == -NFS4ERR_WRONGSEC)
8235 err = _nfs4_proc_secinfo(dir, name, flavors, false);
8236
Trond Myklebust078ea3d2013-08-12 16:45:55 -04008237 trace_nfs4_secinfo(dir, name, err);
8238 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00008239 &exception);
8240 } while (exception.retry);
8241 return err;
8242}
8243
Andy Adamson557134a2009-04-01 09:21:53 -04008244#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04008245/*
Andy Adamson357f54d2010-12-14 10:11:57 -05008246 * Check the exchange flags returned by the server for invalid flags, having
8247 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
8248 * DS flags set.
8249 */
Olga Kornievskaia8c390762020-10-16 09:25:45 -04008250static int nfs4_check_cl_exchange_flags(u32 flags, u32 version)
Andy Adamson357f54d2010-12-14 10:11:57 -05008251{
Olga Kornievskaia8c390762020-10-16 09:25:45 -04008252 if (version >= 2 && (flags & ~EXCHGID4_2_FLAG_MASK_R))
8253 goto out_inval;
8254 else if (version < 2 && (flags & ~EXCHGID4_FLAG_MASK_R))
Andy Adamson357f54d2010-12-14 10:11:57 -05008255 goto out_inval;
8256 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
8257 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
8258 goto out_inval;
8259 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
8260 goto out_inval;
8261 return NFS_OK;
8262out_inval:
8263 return -NFS4ERR_INVAL;
8264}
8265
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04008266static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04008267nfs41_same_server_scope(struct nfs41_server_scope *a,
8268 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04008269{
Anna Schumaker49ad0142017-01-11 16:51:59 -05008270 if (a->server_scope_sz != b->server_scope_sz)
8271 return false;
8272 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04008273}
8274
Andy Adamson02a95de2016-02-05 16:08:37 -05008275static void
8276nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
8277{
Trond Myklebust5c441542019-11-13 08:34:00 +01008278 struct nfs41_bind_conn_to_session_args *args = task->tk_msg.rpc_argp;
Olga Kornievskaiadff58532020-04-24 17:45:50 -04008279 struct nfs41_bind_conn_to_session_res *res = task->tk_msg.rpc_resp;
Trond Myklebust5c441542019-11-13 08:34:00 +01008280 struct nfs_client *clp = args->client;
8281
8282 switch (task->tk_status) {
8283 case -NFS4ERR_BADSESSION:
8284 case -NFS4ERR_DEADSESSION:
8285 nfs4_schedule_session_recovery(clp->cl_session,
8286 task->tk_status);
8287 }
Olga Kornievskaiadff58532020-04-24 17:45:50 -04008288 if (args->dir == NFS4_CDFC4_FORE_OR_BOTH &&
8289 res->dir != NFS4_CDFS4_BOTH) {
8290 rpc_task_close_connection(task);
8291 if (args->retries++ < MAX_BIND_CONN_TO_SESSION_RETRIES)
8292 rpc_restart_call(task);
8293 }
Andy Adamson02a95de2016-02-05 16:08:37 -05008294}
8295
8296static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
Olga Kornievskaia1c709b72020-04-26 11:30:00 -04008297 .rpc_call_done = nfs4_bind_one_conn_to_session_done,
Andy Adamson02a95de2016-02-05 16:08:37 -05008298};
8299
Andy Adamson357f54d2010-12-14 10:11:57 -05008300/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008301 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008302 *
8303 * The 4.1 client currently uses the same TCP connection for the
8304 * fore and backchannel.
8305 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008306static
8307int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
8308 struct rpc_xprt *xprt,
8309 struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008310 const struct cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008311{
8312 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08008313 struct nfs41_bind_conn_to_session_args args = {
8314 .client = clp,
8315 .dir = NFS4_CDFC4_FORE_OR_BOTH,
Olga Kornievskaiadff58532020-04-24 17:45:50 -04008316 .retries = 0,
Trond Myklebust71a097c2015-02-18 09:27:18 -08008317 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008318 struct nfs41_bind_conn_to_session_res res;
8319 struct rpc_message msg = {
8320 .rpc_proc =
8321 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08008322 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008323 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04008324 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008325 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008326 struct rpc_task_setup task_setup_data = {
8327 .rpc_client = clnt,
8328 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05008329 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008330 .rpc_message = &msg,
8331 .flags = RPC_TASK_TIMEOUT,
8332 };
8333 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008334
Trond Myklebust71a097c2015-02-18 09:27:18 -08008335 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
8336 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
8337 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008338
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008339 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
8340 if (xprt != rcu_access_pointer(clnt->cl_xprt))
8341 args.dir = NFS4_CDFC4_FORE;
8342
8343 task = rpc_run_task(&task_setup_data);
8344 if (!IS_ERR(task)) {
8345 status = task->tk_status;
8346 rpc_put_task(task);
8347 } else
8348 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008349 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008350 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08008351 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008352 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
8353 dprintk("NFS: %s: Session ID mismatch\n", __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04008354 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008355 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08008356 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008357 dprintk("NFS: %s: Unexpected direction from server\n",
8358 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04008359 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008360 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08008361 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008362 dprintk("NFS: %s: Server returned RDMA mode = true\n",
8363 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04008364 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008365 }
8366 }
Anna Schumakerc7ae7632017-04-07 14:15:21 -04008367
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008368 return status;
8369}
8370
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008371struct rpc_bind_conn_calldata {
8372 struct nfs_client *clp;
NeilBrowna52458b2018-12-03 11:30:31 +11008373 const struct cred *cred;
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008374};
8375
8376static int
8377nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
8378 struct rpc_xprt *xprt,
8379 void *calldata)
8380{
8381 struct rpc_bind_conn_calldata *p = calldata;
8382
8383 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
8384}
8385
NeilBrowna52458b2018-12-03 11:30:31 +11008386int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, const struct cred *cred)
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008387{
8388 struct rpc_bind_conn_calldata data = {
8389 .clp = clp,
8390 .cred = cred,
8391 };
8392 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
8393 nfs4_proc_bind_conn_to_session_callback, &data);
8394}
8395
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008396/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04008397 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
8398 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04008399 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008400static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
8401 .how = SP4_MACH_CRED,
8402 .enforce.u.words = {
8403 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
8404 1 << (OP_EXCHANGE_ID - 32) |
8405 1 << (OP_CREATE_SESSION - 32) |
8406 1 << (OP_DESTROY_SESSION - 32) |
8407 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04008408 },
8409 .allow.u.words = {
8410 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05008411 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04008412 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05008413 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04008414 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008415 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008416 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05008417 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008418 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04008419 1 << (OP_FREE_STATEID - 32) |
8420 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008421 }
8422};
8423
8424/*
8425 * Select the state protection mode for client `clp' given the server results
8426 * from exchange_id in `sp'.
8427 *
8428 * Returns 0 on success, negative errno otherwise.
8429 */
8430static int nfs4_sp4_select_mode(struct nfs_client *clp,
8431 struct nfs41_state_protection *sp)
8432{
8433 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
8434 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
8435 1 << (OP_EXCHANGE_ID - 32) |
8436 1 << (OP_CREATE_SESSION - 32) |
8437 1 << (OP_DESTROY_SESSION - 32) |
8438 1 << (OP_DESTROY_CLIENTID - 32)
8439 };
Trond Myklebust937e3132017-08-01 07:32:50 -04008440 unsigned long flags = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008441 unsigned int i;
Trond Myklebust937e3132017-08-01 07:32:50 -04008442 int ret = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008443
8444 if (sp->how == SP4_MACH_CRED) {
8445 /* Print state protect result */
8446 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
8447 for (i = 0; i <= LAST_NFS4_OP; i++) {
8448 if (test_bit(i, sp->enforce.u.longs))
8449 dfprintk(MOUNT, " enforce op %d\n", i);
8450 if (test_bit(i, sp->allow.u.longs))
8451 dfprintk(MOUNT, " allow op %d\n", i);
8452 }
8453
8454 /* make sure nothing is on enforce list that isn't supported */
8455 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
8456 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
8457 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008458 ret = -EINVAL;
8459 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008460 }
8461 }
8462
8463 /*
8464 * Minimal mode - state operations are allowed to use machine
8465 * credential. Note this already happens by default, so the
8466 * client doesn't have to do anything more than the negotiation.
8467 *
8468 * NOTE: we don't care if EXCHANGE_ID is in the list -
8469 * we're already using the machine cred for exchange_id
8470 * and will never use a different cred.
8471 */
8472 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
8473 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
8474 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
8475 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
8476 dfprintk(MOUNT, "sp4_mach_cred:\n");
8477 dfprintk(MOUNT, " minimal mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008478 __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008479 } else {
8480 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008481 ret = -EINVAL;
8482 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008483 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04008484
8485 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05008486 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
8487 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04008488 test_bit(OP_LOCKU, sp->allow.u.longs)) {
8489 dfprintk(MOUNT, " cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008490 __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04008491 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008492
Andrew Elble99ade3c2015-12-02 09:39:51 -05008493 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
8494 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008495 __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
Andrew Elble99ade3c2015-12-02 09:39:51 -05008496 }
8497
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008498 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
8499 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
8500 dfprintk(MOUNT, " secinfo mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008501 __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008502 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008503
8504 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
8505 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
8506 dfprintk(MOUNT, " stateid mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008507 __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008508 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04008509
8510 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
8511 dfprintk(MOUNT, " write mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008512 __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04008513 }
8514
8515 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
8516 dfprintk(MOUNT, " commit mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008517 __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04008518 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008519 }
Trond Myklebust937e3132017-08-01 07:32:50 -04008520out:
8521 clp->cl_sp4_flags = flags;
Wei Yongjun72bf75c2018-08-02 05:42:04 +00008522 return ret;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008523}
8524
Andy Adamson8d89bd72016-09-09 09:22:18 -04008525struct nfs41_exchange_id_data {
8526 struct nfs41_exchange_id_res res;
8527 struct nfs41_exchange_id_args args;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008528};
8529
Andy Adamson8d89bd72016-09-09 09:22:18 -04008530static void nfs4_exchange_id_release(void *data)
8531{
8532 struct nfs41_exchange_id_data *cdata =
8533 (struct nfs41_exchange_id_data *)data;
8534
Olga Kornievskaia63513232017-03-13 10:36:19 -04008535 nfs_put_client(cdata->args.client);
Andy Adamson8d89bd72016-09-09 09:22:18 -04008536 kfree(cdata->res.impl_id);
8537 kfree(cdata->res.server_scope);
8538 kfree(cdata->res.server_owner);
8539 kfree(cdata);
8540}
8541
8542static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
Andy Adamson8d89bd72016-09-09 09:22:18 -04008543 .rpc_release = nfs4_exchange_id_release,
8544};
8545
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008546/*
8547 * _nfs4_proc_exchange_id()
8548 *
8549 * Wrapper for EXCHANGE_ID operation.
8550 */
Trond Myklebust9c760d12017-07-31 18:38:50 -04008551static struct rpc_task *
NeilBrowna52458b2018-12-03 11:30:31 +11008552nfs4_run_exchange_id(struct nfs_client *clp, const struct cred *cred,
Andy Adamsonad0849a2016-09-09 09:22:28 -04008553 u32 sp4_how, struct rpc_xprt *xprt)
Benny Halevy99fe60d2009-04-01 09:22:29 -04008554{
Benny Halevy99fe60d2009-04-01 09:22:29 -04008555 struct rpc_message msg = {
8556 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
Benny Halevy99fe60d2009-04-01 09:22:29 -04008557 .rpc_cred = cred,
8558 };
Andy Adamson8d89bd72016-09-09 09:22:18 -04008559 struct rpc_task_setup task_setup_data = {
8560 .rpc_client = clp->cl_rpcclient,
8561 .callback_ops = &nfs4_exchange_id_call_ops,
8562 .rpc_message = &msg,
NeilBrown5a0c2572019-05-30 10:41:28 +10008563 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
Andy Adamson8d89bd72016-09-09 09:22:18 -04008564 };
8565 struct nfs41_exchange_id_data *calldata;
Anna Schumakere917f0d2017-04-07 14:15:22 -04008566 int status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008567
Elena Reshetova212bf412017-10-20 12:53:38 +03008568 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust9c760d12017-07-31 18:38:50 -04008569 return ERR_PTR(-EIO);
Andy Adamson8d89bd72016-09-09 09:22:18 -04008570
Trond Myklebust9c760d12017-07-31 18:38:50 -04008571 status = -ENOMEM;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008572 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust9c760d12017-07-31 18:38:50 -04008573 if (!calldata)
8574 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04008575
Trond Myklebustfd405592017-08-01 16:02:47 -04008576 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04008577
8578 status = nfs4_init_uniform_client_string(clp);
8579 if (status)
Andy Adamson8d89bd72016-09-09 09:22:18 -04008580 goto out_calldata;
Jeff Layton3a6bb732015-06-09 19:43:57 -04008581
Andy Adamson8d89bd72016-09-09 09:22:18 -04008582 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
8583 GFP_NOFS);
8584 status = -ENOMEM;
8585 if (unlikely(calldata->res.server_owner == NULL))
8586 goto out_calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04008587
Andy Adamson8d89bd72016-09-09 09:22:18 -04008588 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04008589 GFP_NOFS);
Andy Adamson8d89bd72016-09-09 09:22:18 -04008590 if (unlikely(calldata->res.server_scope == NULL))
Chuck Leveracdeb692012-05-21 22:46:16 -04008591 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04008592
Andy Adamson8d89bd72016-09-09 09:22:18 -04008593 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
8594 if (unlikely(calldata->res.impl_id == NULL))
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05008595 goto out_server_scope;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05008596
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008597 switch (sp4_how) {
8598 case SP4_NONE:
Andy Adamson8d89bd72016-09-09 09:22:18 -04008599 calldata->args.state_protect.how = SP4_NONE;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008600 break;
8601
8602 case SP4_MACH_CRED:
Andy Adamson8d89bd72016-09-09 09:22:18 -04008603 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008604 break;
8605
8606 default:
8607 /* unsupported! */
8608 WARN_ON_ONCE(1);
8609 status = -EINVAL;
Kinglong Mee6b559702015-07-01 11:54:53 +08008610 goto out_impl_id;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008611 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04008612 if (xprt) {
Andy Adamsonad0849a2016-09-09 09:22:28 -04008613 task_setup_data.rpc_xprt = xprt;
Trond Myklebustd9cb7332017-08-01 16:02:48 -04008614 task_setup_data.flags |= RPC_TASK_SOFTCONN;
Trond Myklebustfd405592017-08-01 16:02:47 -04008615 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
8616 sizeof(calldata->args.verifier.data));
Andy Adamsonad0849a2016-09-09 09:22:28 -04008617 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04008618 calldata->args.client = clp;
Trond Myklebustbfab2812017-08-01 08:17:34 -04008619 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
8620 EXCHGID4_FLAG_BIND_PRINC_STATEID;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008621#ifdef CONFIG_NFS_V4_1_MIGRATION
Trond Myklebustbfab2812017-08-01 08:17:34 -04008622 calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008623#endif
8624 msg.rpc_argp = &calldata->args;
8625 msg.rpc_resp = &calldata->res;
8626 task_setup_data.callback_data = calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04008627
Trond Myklebust9c760d12017-07-31 18:38:50 -04008628 return rpc_run_task(&task_setup_data);
Andy Adamson8d89bd72016-09-09 09:22:18 -04008629
8630out_impl_id:
8631 kfree(calldata->res.impl_id);
8632out_server_scope:
8633 kfree(calldata->res.server_scope);
8634out_server_owner:
8635 kfree(calldata->res.server_owner);
8636out_calldata:
8637 kfree(calldata);
Trond Myklebust9c760d12017-07-31 18:38:50 -04008638out:
Olga Kornievskaia63513232017-03-13 10:36:19 -04008639 nfs_put_client(clp);
Trond Myklebust9c760d12017-07-31 18:38:50 -04008640 return ERR_PTR(status);
8641}
8642
8643/*
8644 * _nfs4_proc_exchange_id()
8645 *
8646 * Wrapper for EXCHANGE_ID operation.
8647 */
NeilBrowna52458b2018-12-03 11:30:31 +11008648static int _nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred,
Trond Myklebust9c760d12017-07-31 18:38:50 -04008649 u32 sp4_how)
8650{
8651 struct rpc_task *task;
8652 struct nfs41_exchange_id_args *argp;
8653 struct nfs41_exchange_id_res *resp;
Robert Milkowski7dc29932020-01-30 09:43:25 +00008654 unsigned long now = jiffies;
Trond Myklebust9c760d12017-07-31 18:38:50 -04008655 int status;
8656
8657 task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
8658 if (IS_ERR(task))
8659 return PTR_ERR(task);
8660
8661 argp = task->tk_msg.rpc_argp;
8662 resp = task->tk_msg.rpc_resp;
8663 status = task->tk_status;
8664 if (status != 0)
8665 goto out;
8666
Olga Kornievskaia8c390762020-10-16 09:25:45 -04008667 status = nfs4_check_cl_exchange_flags(resp->flags,
8668 clp->cl_mvops->minor_version);
Trond Myklebust9c760d12017-07-31 18:38:50 -04008669 if (status != 0)
8670 goto out;
8671
8672 status = nfs4_sp4_select_mode(clp, &resp->state_protect);
8673 if (status != 0)
8674 goto out;
8675
Robert Milkowski7dc29932020-01-30 09:43:25 +00008676 do_renew_lease(clp, now);
8677
Trond Myklebust9c760d12017-07-31 18:38:50 -04008678 clp->cl_clientid = resp->clientid;
8679 clp->cl_exchange_flags = resp->flags;
8680 clp->cl_seqid = resp->seqid;
8681 /* Client ID is not confirmed */
8682 if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
8683 clear_bit(NFS4_SESSION_ESTABLISHED,
8684 &clp->cl_session->session_state);
8685
8686 if (clp->cl_serverscope != NULL &&
8687 !nfs41_same_server_scope(clp->cl_serverscope,
8688 resp->server_scope)) {
8689 dprintk("%s: server_scope mismatch detected\n",
8690 __func__);
8691 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
8692 }
8693
8694 swap(clp->cl_serverowner, resp->server_owner);
8695 swap(clp->cl_serverscope, resp->server_scope);
8696 swap(clp->cl_implid, resp->impl_id);
8697
8698 /* Save the EXCHANGE_ID verifier session trunk tests */
8699 memcpy(clp->cl_confirm.data, argp->verifier.data,
8700 sizeof(clp->cl_confirm.data));
8701out:
8702 trace_nfs4_exchange_id(clp, status);
8703 rpc_put_task(task);
8704 return status;
Benny Halevy99fe60d2009-04-01 09:22:29 -04008705}
8706
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008707/*
8708 * nfs4_proc_exchange_id()
8709 *
8710 * Returns zero, a negative errno, or a negative NFS4ERR status code.
8711 *
8712 * Since the clientid has expired, all compounds using sessions
8713 * associated with the stale clientid will be returning
8714 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
8715 * be in some phase of session reset.
8716 *
8717 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
8718 */
NeilBrowna52458b2018-12-03 11:30:31 +11008719int nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008720{
8721 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
8722 int status;
8723
8724 /* try SP4_MACH_CRED if krb5i/p */
8725 if (authflavor == RPC_AUTH_GSS_KRB5I ||
8726 authflavor == RPC_AUTH_GSS_KRB5P) {
Trond Myklebust9c760d12017-07-31 18:38:50 -04008727 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008728 if (!status)
8729 return 0;
8730 }
8731
8732 /* try SP4_NONE */
Trond Myklebust9c760d12017-07-31 18:38:50 -04008733 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008734}
8735
Andy Adamson04fa2c62016-09-09 09:22:29 -04008736/**
8737 * nfs4_test_session_trunk
8738 *
8739 * This is an add_xprt_test() test function called from
8740 * rpc_clnt_setup_test_and_add_xprt.
8741 *
8742 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
8743 * and is dereferrenced in nfs4_exchange_id_release
8744 *
8745 * Upon success, add the new transport to the rpc_clnt
8746 *
8747 * @clnt: struct rpc_clnt to get new transport
8748 * @xprt: the rpc_xprt to test
8749 * @data: call data for _nfs4_proc_exchange_id.
8750 */
Santosh kumar pradhan10e037d2018-12-19 12:29:57 +05308751void nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
Andy Adamson04fa2c62016-09-09 09:22:29 -04008752 void *data)
8753{
8754 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
Trond Myklebust9c760d12017-07-31 18:38:50 -04008755 struct rpc_task *task;
8756 int status;
8757
Andy Adamson04fa2c62016-09-09 09:22:29 -04008758 u32 sp4_how;
8759
8760 dprintk("--> %s try %s\n", __func__,
8761 xprt->address_strings[RPC_DISPLAY_ADDR]);
8762
8763 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
8764
8765 /* Test connection for session trunking. Async exchange_id call */
Trond Myklebust9c760d12017-07-31 18:38:50 -04008766 task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
8767 if (IS_ERR(task))
Santosh kumar pradhan10e037d2018-12-19 12:29:57 +05308768 return;
Trond Myklebust9c760d12017-07-31 18:38:50 -04008769
8770 status = task->tk_status;
8771 if (status == 0)
8772 status = nfs4_detect_session_trunking(adata->clp,
8773 task->tk_msg.rpc_resp, xprt);
8774
Santosh kumar pradhan10e037d2018-12-19 12:29:57 +05308775 if (status == 0)
8776 rpc_clnt_xprt_switch_add_xprt(clnt, xprt);
8777
Trond Myklebust9c760d12017-07-31 18:38:50 -04008778 rpc_put_task(task);
Andy Adamson04fa2c62016-09-09 09:22:29 -04008779}
8780EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
8781
Trond Myklebust66245532012-05-25 17:18:09 -04008782static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008783 const struct cred *cred)
Trond Myklebust66245532012-05-25 17:18:09 -04008784{
8785 struct rpc_message msg = {
8786 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
8787 .rpc_argp = clp,
8788 .rpc_cred = cred,
8789 };
8790 int status;
8791
NeilBrown5a0c2572019-05-30 10:41:28 +10008792 status = rpc_call_sync(clp->cl_rpcclient, &msg,
8793 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008794 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04008795 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04008796 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04008797 "DESTROY_CLIENTID.", status, clp->cl_hostname);
8798 return status;
8799}
8800
8801static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008802 const struct cred *cred)
Trond Myklebust66245532012-05-25 17:18:09 -04008803{
8804 unsigned int loop;
8805 int ret;
8806
8807 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
8808 ret = _nfs4_proc_destroy_clientid(clp, cred);
8809 switch (ret) {
8810 case -NFS4ERR_DELAY:
8811 case -NFS4ERR_CLIENTID_BUSY:
8812 ssleep(1);
8813 break;
8814 default:
8815 return ret;
8816 }
8817 }
8818 return 0;
8819}
8820
8821int nfs4_destroy_clientid(struct nfs_client *clp)
8822{
NeilBrowna52458b2018-12-03 11:30:31 +11008823 const struct cred *cred;
Trond Myklebust66245532012-05-25 17:18:09 -04008824 int ret = 0;
8825
8826 if (clp->cl_mvops->minor_version < 1)
8827 goto out;
8828 if (clp->cl_exchange_flags == 0)
8829 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04008830 if (clp->cl_preserve_clid)
8831 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04008832 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04008833 ret = nfs4_proc_destroy_clientid(clp, cred);
NeilBrowna52458b2018-12-03 11:30:31 +11008834 put_cred(cred);
Trond Myklebust66245532012-05-25 17:18:09 -04008835 switch (ret) {
8836 case 0:
8837 case -NFS4ERR_STALE_CLIENTID:
8838 clp->cl_exchange_flags = 0;
8839 }
8840out:
8841 return ret;
8842}
8843
Donald Buczek0efb01b2019-07-07 21:26:08 +02008844#endif /* CONFIG_NFS_V4_1 */
8845
Andy Adamson2050f0c2009-04-01 09:22:30 -04008846struct nfs4_get_lease_time_data {
8847 struct nfs4_get_lease_time_args *args;
8848 struct nfs4_get_lease_time_res *res;
8849 struct nfs_client *clp;
8850};
8851
8852static void nfs4_get_lease_time_prepare(struct rpc_task *task,
8853 void *calldata)
8854{
Andy Adamson2050f0c2009-04-01 09:22:30 -04008855 struct nfs4_get_lease_time_data *data =
8856 (struct nfs4_get_lease_time_data *)calldata;
8857
8858 dprintk("--> %s\n", __func__);
8859 /* just setup sequence, do not trigger session recovery
8860 since we're invoked within one */
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008861 nfs4_setup_sequence(data->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008862 &data->args->la_seq_args,
8863 &data->res->lr_seq_res,
8864 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008865 dprintk("<-- %s\n", __func__);
8866}
8867
8868/*
8869 * Called from nfs4_state_manager thread for session setup, so don't recover
8870 * from sequence operation or clientid errors.
8871 */
8872static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
8873{
8874 struct nfs4_get_lease_time_data *data =
8875 (struct nfs4_get_lease_time_data *)calldata;
8876
8877 dprintk("--> %s\n", __func__);
Donald Buczek0efb01b2019-07-07 21:26:08 +02008878 if (!nfs4_sequence_done(task, &data->res->lr_seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04008879 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04008880 switch (task->tk_status) {
8881 case -NFS4ERR_DELAY:
8882 case -NFS4ERR_GRACE:
8883 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
8884 rpc_delay(task, NFS4_POLL_RETRY_MIN);
8885 task->tk_status = 0;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05008886 fallthrough;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008887 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008888 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008889 return;
8890 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04008891 dprintk("<-- %s\n", __func__);
8892}
8893
Trond Myklebust17280172012-03-11 13:11:00 -04008894static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04008895 .rpc_call_prepare = nfs4_get_lease_time_prepare,
8896 .rpc_call_done = nfs4_get_lease_time_done,
8897};
8898
8899int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
8900{
Andy Adamson2050f0c2009-04-01 09:22:30 -04008901 struct nfs4_get_lease_time_args args;
8902 struct nfs4_get_lease_time_res res = {
8903 .lr_fsinfo = fsinfo,
8904 };
8905 struct nfs4_get_lease_time_data data = {
8906 .args = &args,
8907 .res = &res,
8908 .clp = clp,
8909 };
8910 struct rpc_message msg = {
8911 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
8912 .rpc_argp = &args,
8913 .rpc_resp = &res,
8914 };
8915 struct rpc_task_setup task_setup = {
8916 .rpc_client = clp->cl_rpcclient,
8917 .rpc_message = &msg,
8918 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008919 .callback_data = &data,
8920 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04008921 };
Andy Adamson2050f0c2009-04-01 09:22:30 -04008922
Anna Schumakerfba83f32018-05-04 16:22:50 -04008923 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0, 1);
Anna Schumakerf836b272019-08-19 10:18:28 -04008924 return nfs4_call_sync_custom(&task_setup);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008925}
8926
Donald Buczek0efb01b2019-07-07 21:26:08 +02008927#ifdef CONFIG_NFS_V4_1
8928
Andy Adamsonfc931582009-04-01 09:22:31 -04008929/*
8930 * Initialize the values to be used by the client in CREATE_SESSION
8931 * If nfs4_init_session set the fore channel request and response sizes,
8932 * use them.
8933 *
8934 * Set the back channel max_resp_sz_cached to zero to force the client to
8935 * always set csa_cachethis to FALSE because the current implementation
8936 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
8937 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04008938static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
8939 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04008940{
Andy Adamson18aad3d2013-06-26 12:21:49 -04008941 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04008942 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Trond Myklebust7402a4f2019-07-16 13:51:29 -04008943 unsigned int max_bc_slots = rpc_num_bc_slots(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04008944
Andy Adamson18aad3d2013-06-26 12:21:49 -04008945 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
8946 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
8947
Andy Adamsonfc931582009-04-01 09:22:31 -04008948 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04008949 args->fc_attrs.max_rqst_sz = max_rqst_sz;
8950 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04008951 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05008952 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04008953
8954 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05008955 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04008956 __func__,
8957 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05008958 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04008959
8960 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04008961 args->bc_attrs.max_rqst_sz = max_bc_payload;
8962 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04008963 args->bc_attrs.max_resp_sz_cached = 0;
8964 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust62421cd2018-08-11 11:52:39 -04008965 args->bc_attrs.max_reqs = max_t(unsigned short, max_session_cb_slots, 1);
Trond Myklebust7402a4f2019-07-16 13:51:29 -04008966 if (args->bc_attrs.max_reqs > max_bc_slots)
8967 args->bc_attrs.max_reqs = max_bc_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04008968
8969 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
8970 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
8971 __func__,
8972 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
8973 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
8974 args->bc_attrs.max_reqs);
8975}
8976
Trond Myklebust79969dd2015-02-18 11:30:18 -08008977static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
8978 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008979{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008980 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008981 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008982
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008983 if (rcvd->max_resp_sz > sent->max_resp_sz)
8984 return -EINVAL;
8985 /*
8986 * Our requested max_ops is the minimum we need; we're not
8987 * prepared to break up compounds into smaller pieces than that.
8988 * So, no point even trying to continue if the server won't
8989 * cooperate:
8990 */
8991 if (rcvd->max_ops < sent->max_ops)
8992 return -EINVAL;
8993 if (rcvd->max_reqs == 0)
8994 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04008995 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
8996 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008997 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04008998}
8999
Trond Myklebust79969dd2015-02-18 11:30:18 -08009000static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
9001 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04009002{
9003 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08009004 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04009005
Trond Myklebustb1c0df52015-02-18 11:34:58 -08009006 if (!(res->flags & SESSION4_BACK_CHAN))
9007 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04009008 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
9009 return -EINVAL;
9010 if (rcvd->max_resp_sz < sent->max_resp_sz)
9011 return -EINVAL;
9012 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
9013 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04009014 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04009015 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04009016 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04009017 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08009018out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04009019 return 0;
9020}
Andy Adamson8d353012009-04-01 09:22:32 -04009021
Andy Adamson8d353012009-04-01 09:22:32 -04009022static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08009023 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04009024{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04009025 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04009026
Trond Myklebust79969dd2015-02-18 11:30:18 -08009027 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04009028 if (ret)
9029 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08009030 return nfs4_verify_back_channel_attrs(args, res);
9031}
9032
9033static void nfs4_update_session(struct nfs4_session *session,
9034 struct nfs41_create_session_res *res)
9035{
9036 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05009037 /* Mark client id and session as being confirmed */
9038 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
9039 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08009040 session->flags = res->flags;
9041 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08009042 if (res->flags & SESSION4_BACK_CHAN)
9043 memcpy(&session->bc_attrs, &res->bc_attrs,
9044 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04009045}
9046
Trond Myklebust848f5bd2012-05-25 17:51:23 -04009047static int _nfs4_proc_create_session(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11009048 const struct cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04009049{
9050 struct nfs4_session *session = clp->cl_session;
9051 struct nfs41_create_session_args args = {
9052 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08009053 .clientid = clp->cl_clientid,
9054 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04009055 .cb_program = NFS4_CALLBACK,
9056 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08009057 struct nfs41_create_session_res res;
9058
Andy Adamsonfc931582009-04-01 09:22:31 -04009059 struct rpc_message msg = {
9060 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
9061 .rpc_argp = &args,
9062 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04009063 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04009064 };
9065 int status;
9066
Chuck Lever6b26cc82016-05-02 14:40:40 -04009067 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04009068 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04009069
NeilBrown5a0c2572019-05-30 10:41:28 +10009070 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
9071 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04009072 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04009073
Trond Myklebustb519d402016-09-11 14:50:01 -04009074 switch (status) {
9075 case -NFS4ERR_STALE_CLIENTID:
9076 case -NFS4ERR_DELAY:
9077 case -ETIMEDOUT:
9078 case -EACCES:
9079 case -EAGAIN:
9080 goto out;
zhengbin8b98a532019-12-19 18:34:47 +08009081 }
Trond Myklebustb519d402016-09-11 14:50:01 -04009082
9083 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05009084 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04009085 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08009086 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04009087 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08009088 if (status)
9089 goto out;
9090 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04009091 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08009092out:
Andy Adamsonfc931582009-04-01 09:22:31 -04009093 return status;
9094}
9095
9096/*
9097 * Issues a CREATE_SESSION operation to the server.
9098 * It is the responsibility of the caller to verify the session is
9099 * expired before calling this routine.
9100 */
NeilBrowna52458b2018-12-03 11:30:31 +11009101int nfs4_proc_create_session(struct nfs_client *clp, const struct cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04009102{
9103 int status;
9104 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04009105 struct nfs4_session *session = clp->cl_session;
9106
9107 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
9108
Trond Myklebust848f5bd2012-05-25 17:51:23 -04009109 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04009110 if (status)
9111 goto out;
9112
Andy Adamsonaacd5532011-11-09 13:58:21 -05009113 /* Init or reset the session slot tables */
9114 status = nfs4_setup_session_slot_tables(session);
9115 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04009116 if (status)
9117 goto out;
9118
9119 ptr = (unsigned *)&session->sess_id.data[0];
9120 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
9121 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04009122out:
9123 dprintk("<-- %s\n", __func__);
9124 return status;
9125}
9126
Andy Adamson0f3e66c2009-04-01 09:22:34 -04009127/*
9128 * Issue the over-the-wire RPC DESTROY_SESSION.
9129 * The caller must serialize access to this routine.
9130 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04009131int nfs4_proc_destroy_session(struct nfs4_session *session,
NeilBrowna52458b2018-12-03 11:30:31 +11009132 const struct cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04009133{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04009134 struct rpc_message msg = {
9135 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
9136 .rpc_argp = session,
9137 .rpc_cred = cred,
9138 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04009139 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04009140
9141 dprintk("--> nfs4_proc_destroy_session\n");
9142
9143 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05009144 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
9145 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04009146
NeilBrown5a0c2572019-05-30 10:41:28 +10009147 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
9148 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04009149 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04009150
9151 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04009152 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04009153 "Session has been destroyed regardless...\n", status);
9154
9155 dprintk("<-- nfs4_proc_destroy_session\n");
9156 return status;
9157}
9158
Trond Myklebust7b38c362012-05-23 13:23:31 -04009159/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009160 * Renew the cl_session lease.
9161 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04009162struct nfs4_sequence_data {
9163 struct nfs_client *clp;
9164 struct nfs4_sequence_args args;
9165 struct nfs4_sequence_res res;
9166};
9167
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08009168static void nfs41_sequence_release(void *data)
9169{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04009170 struct nfs4_sequence_data *calldata = data;
9171 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08009172
Elena Reshetova212bf412017-10-20 12:53:38 +03009173 if (refcount_read(&clp->cl_count) > 1)
Alexandros Batsakis71358402010-02-05 03:45:05 -08009174 nfs4_schedule_state_renewal(clp);
9175 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04009176 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08009177}
9178
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009179static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
9180{
9181 switch(task->tk_status) {
9182 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009183 rpc_delay(task, NFS4_POLL_RETRY_MAX);
9184 return -EAGAIN;
9185 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05009186 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009187 }
9188 return 0;
9189}
9190
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08009191static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009192{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04009193 struct nfs4_sequence_data *calldata = data;
9194 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009195
Trond Myklebust14516c32010-07-31 14:29:06 -04009196 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
9197 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009198
Trond Myklebustc6d01c62013-08-09 11:51:26 -04009199 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009200 if (task->tk_status < 0) {
9201 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Elena Reshetova212bf412017-10-20 12:53:38 +03009202 if (refcount_read(&clp->cl_count) == 1)
Alexandros Batsakis71358402010-02-05 03:45:05 -08009203 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009204
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009205 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
9206 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009207 return;
9208 }
9209 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009210 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08009211out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009212 dprintk("<-- %s\n", __func__);
9213}
9214
9215static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
9216{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04009217 struct nfs4_sequence_data *calldata = data;
9218 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009219 struct nfs4_sequence_args *args;
9220 struct nfs4_sequence_res *res;
9221
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009222 args = task->tk_msg.rpc_argp;
9223 res = task->tk_msg.rpc_resp;
9224
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009225 nfs4_setup_sequence(clp, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009226}
9227
9228static const struct rpc_call_ops nfs41_sequence_ops = {
9229 .rpc_call_done = nfs41_sequence_call_done,
9230 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08009231 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009232};
9233
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009234static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11009235 const struct cred *cred,
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009236 struct nfs4_slot *slot,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009237 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009238{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04009239 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009240 struct rpc_message msg = {
9241 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
9242 .rpc_cred = cred,
9243 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009244 struct rpc_task_setup task_setup_data = {
9245 .rpc_client = clp->cl_rpcclient,
9246 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009247 .callback_ops = &nfs41_sequence_ops,
Olga Kornievskaia85e39fe2021-06-23 23:28:51 -04009248 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT | RPC_TASK_MOVEABLE,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009249 };
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009250 struct rpc_task *ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009251
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009252 ret = ERR_PTR(-EIO);
Elena Reshetova212bf412017-10-20 12:53:38 +03009253 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009254 goto out_err;
9255
9256 ret = ERR_PTR(-ENOMEM);
Benny Halevydfb4f3092010-09-24 09:17:01 -04009257 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009258 if (calldata == NULL)
9259 goto out_put_clp;
Anna Schumakerfba83f32018-05-04 16:22:50 -04009260 nfs4_init_sequence(&calldata->args, &calldata->res, 0, is_privileged);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009261 nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04009262 msg.rpc_argp = &calldata->args;
9263 msg.rpc_resp = &calldata->res;
9264 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009265 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009266
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009267 ret = rpc_run_task(&task_setup_data);
9268 if (IS_ERR(ret))
9269 goto out_err;
9270 return ret;
9271out_put_clp:
9272 nfs_put_client(clp);
9273out_err:
9274 nfs41_release_slot(slot);
9275 return ret;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009276}
9277
NeilBrowna52458b2018-12-03 11:30:31 +11009278static int nfs41_proc_async_sequence(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009279{
9280 struct rpc_task *task;
9281 int ret = 0;
9282
Trond Myklebust2f60ea62011-08-24 15:07:37 -04009283 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04009284 return -EAGAIN;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009285 task = _nfs41_proc_sequence(clp, cred, NULL, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009286 if (IS_ERR(task))
9287 ret = PTR_ERR(task);
9288 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08009289 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009290 dprintk("<-- %s status=%d\n", __func__, ret);
9291 return ret;
9292}
9293
NeilBrowna52458b2018-12-03 11:30:31 +11009294static int nfs4_proc_sequence(struct nfs_client *clp, const struct cred *cred)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009295{
9296 struct rpc_task *task;
9297 int ret;
9298
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009299 task = _nfs41_proc_sequence(clp, cred, NULL, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009300 if (IS_ERR(task)) {
9301 ret = PTR_ERR(task);
9302 goto out;
9303 }
9304 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04009305 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009306 ret = task->tk_status;
9307 rpc_put_task(task);
9308out:
9309 dprintk("<-- %s status=%d\n", __func__, ret);
9310 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009311}
9312
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009313struct nfs4_reclaim_complete_data {
9314 struct nfs_client *clp;
9315 struct nfs41_reclaim_complete_args arg;
9316 struct nfs41_reclaim_complete_res res;
9317};
9318
9319static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
9320{
9321 struct nfs4_reclaim_complete_data *calldata = data;
9322
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009323 nfs4_setup_sequence(calldata->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04009324 &calldata->arg.seq_args,
9325 &calldata->res.seq_res,
9326 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009327}
9328
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009329static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
9330{
9331 switch(task->tk_status) {
9332 case 0:
Jeff Layton57174592018-03-18 08:37:03 -04009333 wake_up_all(&clp->cl_lock_waitq);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05009334 fallthrough;
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009335 case -NFS4ERR_COMPLETE_ALREADY:
9336 case -NFS4ERR_WRONG_CRED: /* What to do here? */
9337 break;
9338 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009339 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05009340 fallthrough;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04009341 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009342 return -EAGAIN;
Trond Myklebust0048fdd2017-05-04 13:44:04 -04009343 case -NFS4ERR_BADSESSION:
9344 case -NFS4ERR_DEADSESSION:
9345 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
Trond Myklebust0048fdd2017-05-04 13:44:04 -04009346 break;
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009347 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05009348 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009349 }
9350 return 0;
9351}
9352
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009353static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
9354{
9355 struct nfs4_reclaim_complete_data *calldata = data;
9356 struct nfs_client *clp = calldata->clp;
9357 struct nfs4_sequence_res *res = &calldata->res.seq_res;
9358
9359 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04009360 if (!nfs41_sequence_done(task, res))
9361 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009362
Trond Myklebustc6d01c62013-08-09 11:51:26 -04009363 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009364 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
9365 rpc_restart_call_prepare(task);
9366 return;
9367 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009368 dprintk("<-- %s\n", __func__);
9369}
9370
9371static void nfs4_free_reclaim_complete_data(void *data)
9372{
9373 struct nfs4_reclaim_complete_data *calldata = data;
9374
9375 kfree(calldata);
9376}
9377
9378static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
9379 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
9380 .rpc_call_done = nfs4_reclaim_complete_done,
9381 .rpc_release = nfs4_free_reclaim_complete_data,
9382};
9383
9384/*
9385 * Issue a global reclaim complete.
9386 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04009387static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11009388 const struct cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009389{
9390 struct nfs4_reclaim_complete_data *calldata;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009391 struct rpc_message msg = {
9392 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04009393 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009394 };
9395 struct rpc_task_setup task_setup_data = {
9396 .rpc_client = clp->cl_rpcclient,
9397 .rpc_message = &msg,
9398 .callback_ops = &nfs4_reclaim_complete_call_ops,
Anna Schumaker4c952e32019-08-14 15:46:48 -04009399 .flags = RPC_TASK_NO_ROUND_ROBIN,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009400 };
9401 int status = -ENOMEM;
9402
9403 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04009404 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009405 if (calldata == NULL)
9406 goto out;
9407 calldata->clp = clp;
9408 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009409
Anna Schumakerfba83f32018-05-04 16:22:50 -04009410 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0, 1);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009411 msg.rpc_argp = &calldata->arg;
9412 msg.rpc_resp = &calldata->res;
9413 task_setup_data.callback_data = calldata;
Anna Schumaker4c952e32019-08-14 15:46:48 -04009414 status = nfs4_call_sync_custom(&task_setup_data);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009415out:
9416 dprintk("<-- %s status=%d\n", __func__, status);
9417 return status;
9418}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009419
9420static void
9421nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
9422{
9423 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00009424 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009425
9426 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009427 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
Jeff Layton183d9e72016-05-17 12:28:47 -04009428 &lgp->res.seq_res, task);
9429 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009430}
9431
9432static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
9433{
9434 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04009435
9436 dprintk("--> %s\n", __func__);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04009437 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04009438 dprintk("<-- %s\n", __func__);
9439}
9440
9441static int
9442nfs4_layoutget_handle_exception(struct rpc_task *task,
9443 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
9444{
Trond Myklebustee314c22012-10-01 17:25:48 -07009445 struct inode *inode = lgp->args.inode;
9446 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4e89bc2021-07-02 17:24:22 -04009447 struct pnfs_layout_hdr *lo = lgp->lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04009448 int nfs4err = task->tk_status;
9449 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04009450 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009451
Boaz Harroshed7e5422014-01-22 20:34:54 +02009452 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009453
Trond Myklebust2dbf8df2018-06-15 15:58:45 -04009454 nfs4_sequence_free_slot(&lgp->res.seq_res);
9455
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04009456 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009457 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07009458 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08009459
9460 /*
9461 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
9462 * on the file. set tk_status to -ENODATA to tell upper layer to
9463 * retry go inband.
9464 */
9465 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04009466 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08009467 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02009468 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07009469 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
9470 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
9471 */
9472 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04009473 status = -EOVERFLOW;
9474 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07009475 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02009476 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07009477 * (or clients) writing to the same RAID stripe except when
9478 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04009479 *
9480 * Treat it like we would RECALLCONFLICT -- we retry for a little
9481 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02009482 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009483 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04009484 if (lgp->args.minlength == 0) {
9485 status = -EOVERFLOW;
9486 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02009487 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04009488 status = -EBUSY;
9489 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04009490 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04009491 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04009492 break;
Trond Myklebust26f47442016-09-22 13:39:10 -04009493 case -NFS4ERR_DELEG_REVOKED:
9494 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustee314c22012-10-01 17:25:48 -07009495 case -NFS4ERR_EXPIRED:
9496 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04009497 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07009498 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04009499 /* If the open stateid was bad, then recover it. */
9500 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
Trond Myklebuste8fa33a2017-10-04 13:49:12 -04009501 !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07009502 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04009503 exception->state = lgp->args.ctx->state;
Trond Myklebust26f47442016-09-22 13:39:10 -04009504 exception->stateid = &lgp->args.stateid;
Trond Myklebust2259f962015-09-20 13:30:30 -04009505 break;
9506 }
Trond Myklebustee314c22012-10-01 17:25:48 -07009507
Trond Myklebustf7db0b22016-07-14 15:14:02 -04009508 /*
9509 * Mark the bad layout state as invalid, then retry
9510 */
Trond Myklebust668f4552016-07-24 17:08:59 -04009511 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04009512 spin_unlock(&inode->i_lock);
Trond Myklebust1f18b822017-04-29 10:10:17 -04009513 nfs_commit_inode(inode, 0);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04009514 pnfs_free_lseg_list(&head);
9515 status = -EAGAIN;
9516 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009517 }
Jeff Layton183d9e72016-05-17 12:28:47 -04009518
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04009519 err = nfs4_handle_exception(server, nfs4err, exception);
9520 if (!status) {
9521 if (exception->retry)
9522 status = -EAGAIN;
9523 else
9524 status = err;
9525 }
Trond Myklebustee314c22012-10-01 17:25:48 -07009526out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009527 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04009528 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009529}
9530
Fred Isamandacb4522016-09-19 17:47:09 -04009531size_t max_response_pages(struct nfs_server *server)
Idan Kedar85541162012-08-02 11:47:10 +03009532{
9533 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
9534 return nfs_page_array_len(0, max_resp_sz);
9535}
9536
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009537static void nfs4_layoutget_release(void *calldata)
9538{
9539 struct nfs4_layoutget *lgp = calldata;
9540
9541 dprintk("--> %s\n", __func__);
Trond Myklebustbd171932017-06-27 17:33:38 -04009542 nfs4_sequence_free_slot(&lgp->res.seq_res);
Trond Myklebust29a8bfe2018-05-30 17:16:20 -04009543 pnfs_layoutget_free(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009544 dprintk("<-- %s\n", __func__);
9545}
9546
9547static const struct rpc_call_ops nfs4_layoutget_call_ops = {
9548 .rpc_call_prepare = nfs4_layoutget_prepare,
9549 .rpc_call_done = nfs4_layoutget_done,
9550 .rpc_release = nfs4_layoutget_release,
9551};
9552
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04009553struct pnfs_layout_segment *
Fred Isamandacb4522016-09-19 17:47:09 -04009554nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009555{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05009556 struct inode *inode = lgp->args.inode;
9557 struct nfs_server *server = NFS_SERVER(inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009558 struct rpc_task *task;
9559 struct rpc_message msg = {
9560 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
9561 .rpc_argp = &lgp->args,
9562 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04009563 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009564 };
9565 struct rpc_task_setup task_setup_data = {
9566 .rpc_client = server->client,
9567 .rpc_message = &msg,
9568 .callback_ops = &nfs4_layoutget_call_ops,
9569 .callback_data = lgp,
Olga Kornievskaia85e39fe2021-06-23 23:28:51 -04009570 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF |
9571 RPC_TASK_MOVEABLE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009572 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04009573 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04009574 struct nfs4_exception exception = {
9575 .inode = inode,
9576 .timeout = *timeout,
9577 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009578 int status = 0;
9579
9580 dprintk("--> %s\n", __func__);
9581
Anna Schumakerfba83f32018-05-04 16:22:50 -04009582 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05009583
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009584 task = rpc_run_task(&task_setup_data);
Xiyu Yang6e476662020-04-25 21:04:40 +08009585
Anna Schumaker820bf852017-01-11 15:01:43 -05009586 status = rpc_wait_for_completion_task(task);
Trond Myklebust2dbf8df2018-06-15 15:58:45 -04009587 if (status != 0)
9588 goto out;
9589
Trond Myklebust18c07782019-02-13 07:55:31 -05009590 if (task->tk_status < 0) {
Jeff Layton183d9e72016-05-17 12:28:47 -04009591 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
9592 *timeout = exception.timeout;
Trond Myklebust18c07782019-02-13 07:55:31 -05009593 } else if (lgp->res.layoutp->len == 0) {
9594 status = -EAGAIN;
9595 *timeout = nfs4_update_delay(&exception.timeout);
Trond Myklebust2dbf8df2018-06-15 15:58:45 -04009596 } else
9597 lseg = pnfs_layout_process(lgp);
9598out:
Trond Myklebust1037e6e2013-08-14 16:36:51 -04009599 trace_nfs4_layoutget(lgp->args.ctx,
9600 &lgp->args.range,
9601 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05009602 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04009603 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04009604
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009605 rpc_put_task(task);
9606 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04009607 if (status)
9608 return ERR_PTR(status);
9609 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009610}
9611
Benny Halevycbe82602011-05-22 19:52:37 +03009612static void
9613nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
9614{
9615 struct nfs4_layoutreturn *lrp = calldata;
9616
9617 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009618 nfs4_setup_sequence(lrp->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04009619 &lrp->args.seq_args,
9620 &lrp->res.seq_res,
9621 task);
Trond Myklebustc8bf7072018-06-15 16:31:02 -04009622 if (!pnfs_layout_is_valid(lrp->args.layout))
9623 rpc_exit(task, 0);
Benny Halevycbe82602011-05-22 19:52:37 +03009624}
9625
9626static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
9627{
9628 struct nfs4_layoutreturn *lrp = calldata;
9629 struct nfs_server *server;
9630
9631 dprintk("--> %s\n", __func__);
9632
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04009633 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03009634 return;
9635
Trond Myklebust6109bcf2019-09-20 07:23:43 -04009636 /*
9637 * Was there an RPC level error? Assume the call succeeded,
9638 * and that we need to release the layout
9639 */
9640 if (task->tk_rpc_status != 0 && RPC_WAS_SENT(task)) {
9641 lrp->res.lrs_present = 0;
9642 return;
9643 }
9644
Benny Halevycbe82602011-05-22 19:52:37 +03009645 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05009646 switch (task->tk_status) {
Trond Myklebustff905142017-11-06 15:28:08 -05009647 case -NFS4ERR_OLD_STATEID:
Trond Myklebust30cb3ee2019-09-20 07:23:45 -04009648 if (nfs4_layout_refresh_old_stateid(&lrp->args.stateid,
Trond Myklebustecf84022018-08-15 21:35:46 -04009649 &lrp->args.range,
Trond Myklebustff905142017-11-06 15:28:08 -05009650 lrp->args.inode))
9651 goto out_restart;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05009652 fallthrough;
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05009653 default:
9654 task->tk_status = 0;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05009655 fallthrough;
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05009656 case 0:
9657 break;
9658 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10009659 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05009660 break;
Trond Myklebustff905142017-11-06 15:28:08 -05009661 goto out_restart;
Benny Halevycbe82602011-05-22 19:52:37 +03009662 }
Benny Halevycbe82602011-05-22 19:52:37 +03009663 dprintk("<-- %s\n", __func__);
Trond Myklebustff905142017-11-06 15:28:08 -05009664 return;
9665out_restart:
9666 task->tk_status = 0;
9667 nfs4_sequence_free_slot(&lrp->res.seq_res);
9668 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03009669}
9670
9671static void nfs4_layoutreturn_release(void *calldata)
9672{
9673 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04009674 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03009675
9676 dprintk("--> %s\n", __func__);
Trond Myklebust2a974422016-11-20 13:13:54 -05009677 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
Trond Myklebust68f74472016-10-12 19:50:54 -04009678 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04009679 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebust4d796d72016-09-23 11:38:08 -04009680 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
9681 lrp->ld_private.ops->free(&lrp->ld_private);
Trond Myklebust2f065dd2016-12-07 12:29:26 -05009682 pnfs_put_layout_hdr(lrp->args.layout);
9683 nfs_iput_and_deactive(lrp->inode);
Trond Myklebust44ea8df2020-04-02 15:37:02 -04009684 put_cred(lrp->cred);
Benny Halevycbe82602011-05-22 19:52:37 +03009685 kfree(calldata);
9686 dprintk("<-- %s\n", __func__);
9687}
9688
9689static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
9690 .rpc_call_prepare = nfs4_layoutreturn_prepare,
9691 .rpc_call_done = nfs4_layoutreturn_done,
9692 .rpc_release = nfs4_layoutreturn_release,
9693};
9694
Peng Tao6c166052014-11-17 09:30:40 +08009695int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03009696{
9697 struct rpc_task *task;
9698 struct rpc_message msg = {
9699 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
9700 .rpc_argp = &lrp->args,
9701 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04009702 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03009703 };
9704 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04009705 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03009706 .rpc_message = &msg,
9707 .callback_ops = &nfs4_layoutreturn_call_ops,
9708 .callback_data = lrp,
Olga Kornievskaia85e39fe2021-06-23 23:28:51 -04009709 .flags = RPC_TASK_MOVEABLE,
Benny Halevycbe82602011-05-22 19:52:37 +03009710 };
Peng Tao6c166052014-11-17 09:30:40 +08009711 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03009712
Andrew Elble99ade3c2015-12-02 09:39:51 -05009713 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
9714 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
9715 &task_setup_data.rpc_client, &msg);
9716
Benny Halevycbe82602011-05-22 19:52:37 +03009717 dprintk("--> %s\n", __func__);
Trond Myklebustc3aba892021-06-01 11:35:56 -04009718 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05009719 if (!sync) {
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05009720 if (!lrp->inode) {
9721 nfs4_layoutreturn_release(lrp);
9722 return -EAGAIN;
9723 }
9724 task_setup_data.flags |= RPC_TASK_ASYNC;
9725 }
Trond Myklebustc3aba892021-06-01 11:35:56 -04009726 if (!lrp->inode)
9727 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1,
9728 1);
9729 else
9730 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1,
9731 0);
Benny Halevycbe82602011-05-22 19:52:37 +03009732 task = rpc_run_task(&task_setup_data);
9733 if (IS_ERR(task))
9734 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05009735 if (sync)
9736 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05009737 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03009738 dprintk("<-- %s status=%d\n", __func__, status);
9739 rpc_put_task(task);
9740 return status;
9741}
9742
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009743static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009744_nfs4_proc_getdeviceinfo(struct nfs_server *server,
9745 struct pnfs_device *pdev,
NeilBrowna52458b2018-12-03 11:30:31 +11009746 const struct cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009747{
9748 struct nfs4_getdeviceinfo_args args = {
9749 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04009750 .notify_types = NOTIFY_DEVICEID4_CHANGE |
9751 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009752 };
9753 struct nfs4_getdeviceinfo_res res = {
9754 .pdev = pdev,
9755 };
9756 struct rpc_message msg = {
9757 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
9758 .rpc_argp = &args,
9759 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009760 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009761 };
9762 int status;
9763
9764 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00009765 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04009766 if (res.notification & ~args.notify_types)
9767 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04009768 if (res.notification != args.notify_types)
9769 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04009770
Trond Myklebustcac1d3a2020-12-16 16:31:26 -05009771 trace_nfs4_getdeviceinfo(server, &pdev->dev_id, status);
9772
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009773 dprintk("<-- %s status=%d\n", __func__, status);
9774
9775 return status;
9776}
9777
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009778int nfs4_proc_getdeviceinfo(struct nfs_server *server,
9779 struct pnfs_device *pdev,
NeilBrowna52458b2018-12-03 11:30:31 +11009780 const struct cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009781{
9782 struct nfs4_exception exception = { };
9783 int err;
9784
9785 do {
9786 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009787 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009788 &exception);
9789 } while (exception.retry);
9790 return err;
9791}
9792EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
9793
Andy Adamson863a3c62011-03-23 13:27:54 +00009794static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
9795{
9796 struct nfs4_layoutcommit_data *data = calldata;
9797 struct nfs_server *server = NFS_SERVER(data->args.inode);
9798
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009799 nfs4_setup_sequence(server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04009800 &data->args.seq_args,
9801 &data->res.seq_res,
9802 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00009803}
9804
9805static void
9806nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
9807{
9808 struct nfs4_layoutcommit_data *data = calldata;
9809 struct nfs_server *server = NFS_SERVER(data->args.inode);
9810
Trond Myklebust6ba7db32012-10-22 20:07:20 -04009811 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00009812 return;
9813
9814 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009815 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
9816 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
9817 case -NFS4ERR_BADLAYOUT: /* no layout */
9818 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00009819 task->tk_status = 0;
Gustavo A. R. Silvaffb81712020-11-20 12:26:46 -06009820 break;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009821 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009822 break;
9823 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10009824 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009825 rpc_restart_call_prepare(task);
9826 return;
9827 }
9828 }
Andy Adamson863a3c62011-03-23 13:27:54 +00009829}
9830
9831static void nfs4_layoutcommit_release(void *calldata)
9832{
9833 struct nfs4_layoutcommit_data *data = calldata;
9834
Andy Adamsondb29c082011-07-30 20:52:38 -04009835 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05009836 nfs_post_op_update_inode_force_wcc(data->args.inode,
9837 data->res.fattr);
NeilBrowna52458b2018-12-03 11:30:31 +11009838 put_cred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05009839 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00009840 kfree(data);
9841}
9842
9843static const struct rpc_call_ops nfs4_layoutcommit_ops = {
9844 .rpc_call_prepare = nfs4_layoutcommit_prepare,
9845 .rpc_call_done = nfs4_layoutcommit_done,
9846 .rpc_release = nfs4_layoutcommit_release,
9847};
9848
9849int
Andy Adamsonef311532011-03-12 02:58:10 -05009850nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00009851{
9852 struct rpc_message msg = {
9853 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
9854 .rpc_argp = &data->args,
9855 .rpc_resp = &data->res,
9856 .rpc_cred = data->cred,
9857 };
9858 struct rpc_task_setup task_setup_data = {
9859 .task = &data->task,
9860 .rpc_client = NFS_CLIENT(data->args.inode),
9861 .rpc_message = &msg,
9862 .callback_ops = &nfs4_layoutcommit_ops,
9863 .callback_data = data,
Olga Kornievskaia85e39fe2021-06-23 23:28:51 -04009864 .flags = RPC_TASK_MOVEABLE,
Andy Adamson863a3c62011-03-23 13:27:54 +00009865 };
9866 struct rpc_task *task;
9867 int status = 0;
9868
Kinglong Meeb4839eb2015-07-01 12:00:13 +08009869 dprintk("NFS: initiating layoutcommit call. sync %d "
9870 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00009871 data->args.lastbytewritten,
9872 data->args.inode->i_ino);
9873
Trond Myklebust472e2592015-02-05 16:50:30 -05009874 if (!sync) {
9875 data->inode = nfs_igrab_and_active(data->args.inode);
9876 if (data->inode == NULL) {
9877 nfs4_layoutcommit_release(data);
9878 return -EAGAIN;
9879 }
9880 task_setup_data.flags = RPC_TASK_ASYNC;
9881 }
Anna Schumakerfba83f32018-05-04 16:22:50 -04009882 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Andy Adamson863a3c62011-03-23 13:27:54 +00009883 task = rpc_run_task(&task_setup_data);
9884 if (IS_ERR(task))
9885 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05009886 if (sync)
9887 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05009888 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00009889 dprintk("%s: status %d\n", __func__, status);
9890 rpc_put_task(task);
9891 return status;
9892}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009893
Trond Myklebust302fad72019-02-18 13:32:38 -05009894/*
Andy Adamson97431202013-08-08 10:57:56 -04009895 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
9896 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
9897 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009898static int
9899_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009900 struct nfs_fsinfo *info,
9901 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009902{
9903 struct nfs41_secinfo_no_name_args args = {
9904 .style = SECINFO_STYLE_CURRENT_FH,
9905 };
9906 struct nfs4_secinfo_res res = {
9907 .flavors = flavors,
9908 };
9909 struct rpc_message msg = {
9910 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
9911 .rpc_argp = &args,
9912 .rpc_resp = &res,
9913 };
Anna Schumakercc15e242019-08-14 16:22:31 -04009914 struct nfs4_call_sync_data data = {
9915 .seq_server = server,
9916 .seq_args = &args.seq_args,
9917 .seq_res = &res.seq_res,
9918 };
9919 struct rpc_task_setup task_setup = {
9920 .rpc_client = server->client,
9921 .rpc_message = &msg,
9922 .callback_ops = server->nfs_client->cl_mvops->call_sync_ops,
9923 .callback_data = &data,
9924 .flags = RPC_TASK_NO_ROUND_ROBIN,
9925 };
NeilBrowna52458b2018-12-03 11:30:31 +11009926 const struct cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009927 int status;
9928
9929 if (use_integrity) {
Colin Ian King48bb6ec2020-06-10 00:22:57 +01009930 task_setup.rpc_client = server->nfs_client->cl_rpcclient;
Anna Schumakercc15e242019-08-14 16:22:31 -04009931
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04009932 cred = nfs4_get_clid_cred(server->nfs_client);
9933 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009934 }
9935
9936 dprintk("--> %s\n", __func__);
Anna Schumakercc15e242019-08-14 16:22:31 -04009937 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
9938 status = nfs4_call_sync_custom(&task_setup);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009939 dprintk("<-- %s status=%d\n", __func__, status);
9940
NeilBrowna52458b2018-12-03 11:30:31 +11009941 put_cred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009942
9943 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009944}
9945
9946static int
9947nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9948 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
9949{
Trond Myklebust0688e642019-04-07 13:59:09 -04009950 struct nfs4_exception exception = {
9951 .interruptible = true,
9952 };
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009953 int err;
9954 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009955 /* first try using integrity protection */
9956 err = -NFS4ERR_WRONGSEC;
9957
9958 /* try to use integrity protection with machine cred */
9959 if (_nfs4_is_integrity_protected(server->nfs_client))
9960 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9961 flavors, true);
9962
9963 /*
9964 * if unable to use integrity protection, or SECINFO with
9965 * integrity protection returns NFS4ERR_WRONGSEC (which is
9966 * disallowed by spec, but exists in deployed servers) use
9967 * the current filesystem's rpc_client and the user cred.
9968 */
9969 if (err == -NFS4ERR_WRONGSEC)
9970 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9971 flavors, false);
9972
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009973 switch (err) {
9974 case 0:
9975 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05009976 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009977 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009978 default:
9979 err = nfs4_handle_exception(server, err, &exception);
9980 }
9981 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009982out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009983 return err;
9984}
9985
9986static int
9987nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
9988 struct nfs_fsinfo *info)
9989{
9990 int err;
9991 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04009992 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009993 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009994 struct nfs4_secinfo4 *secinfo;
9995 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009996
9997 page = alloc_page(GFP_KERNEL);
9998 if (!page) {
9999 err = -ENOMEM;
10000 goto out;
10001 }
10002
10003 flavors = page_address(page);
10004 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
10005
10006 /*
10007 * Fall back on "guess and check" method if
10008 * the server doesn't support SECINFO_NO_NAME
10009 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -050010010 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -040010011 err = nfs4_find_root_sec(server, fhandle, info);
10012 goto out_freepage;
10013 }
10014 if (err)
10015 goto out_freepage;
10016
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -040010017 for (i = 0; i < flavors->num_flavors; i++) {
10018 secinfo = &flavors->flavors[i];
10019
10020 switch (secinfo->flavor) {
10021 case RPC_AUTH_NULL:
10022 case RPC_AUTH_UNIX:
10023 case RPC_AUTH_GSS:
10024 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
10025 &secinfo->flavor_info);
10026 break;
10027 default:
10028 flavor = RPC_AUTH_MAXFLAVOR;
10029 break;
10030 }
10031
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -040010032 if (!nfs_auth_info_match(&server->auth_info, flavor))
10033 flavor = RPC_AUTH_MAXFLAVOR;
10034
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -040010035 if (flavor != RPC_AUTH_MAXFLAVOR) {
10036 err = nfs4_lookup_root_sec(server, fhandle,
10037 info, flavor);
10038 if (!err)
10039 break;
10040 }
10041 }
10042
10043 if (flavor == RPC_AUTH_MAXFLAVOR)
10044 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -040010045
10046out_freepage:
10047 put_page(page);
10048 if (err == -EACCES)
10049 return -EPERM;
10050out:
10051 return err;
10052}
Bryan Schumaker1cab0652012-01-31 10:39:29 -050010053
Trond Myklebustab7cb0d2013-05-20 11:20:27 -040010054static int _nfs41_test_stateid(struct nfs_server *server,
10055 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +110010056 const struct cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -040010057{
10058 int status;
10059 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -050010060 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -040010061 };
10062 struct nfs41_test_stateid_res res;
10063 struct rpc_message msg = {
10064 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
10065 .rpc_argp = &args,
10066 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -040010067 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -040010068 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -040010069 struct rpc_clnt *rpc_client = server->client;
10070
10071 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
10072 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -050010073
Chuck Lever38527b12012-07-11 16:30:23 -040010074 dprintk("NFS call test_stateid %p\n", stateid);
Anna Schumakerfba83f32018-05-04 16:22:50 -040010075 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Weston Andros Adamson3787d502013-08-13 16:37:36 -040010076 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -040010077 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -040010078 if (status != NFS_OK) {
10079 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -040010080 return status;
Chuck Lever38527b12012-07-11 16:30:23 -040010081 }
10082 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -040010083 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -040010084}
10085
Trond Myklebust43912bb2016-09-22 13:38:56 -040010086static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
10087 int err, struct nfs4_exception *exception)
10088{
10089 exception->retry = 0;
10090 switch(err) {
10091 case -NFS4ERR_DELAY:
Trond Myklebust76e8a1b2016-09-22 13:39:19 -040010092 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust43912bb2016-09-22 13:38:56 -040010093 nfs4_handle_exception(server, err, exception);
10094 break;
10095 case -NFS4ERR_BADSESSION:
10096 case -NFS4ERR_BADSLOT:
10097 case -NFS4ERR_BAD_HIGH_SLOT:
10098 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
10099 case -NFS4ERR_DEADSESSION:
10100 nfs4_do_handle_exception(server, err, exception);
10101 }
10102}
10103
Chuck Lever38527b12012-07-11 16:30:23 -040010104/**
10105 * nfs41_test_stateid - perform a TEST_STATEID operation
10106 *
10107 * @server: server / transport on which to perform the operation
10108 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -040010109 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -040010110 *
10111 * Returns NFS_OK if the server recognizes that "stateid" is valid.
10112 * Otherwise a negative NFS4ERR value is returned if the operation
10113 * failed or the state ID is not currently valid.
10114 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -040010115static int nfs41_test_stateid(struct nfs_server *server,
10116 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +110010117 const struct cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -040010118{
Trond Myklebust0688e642019-04-07 13:59:09 -040010119 struct nfs4_exception exception = {
10120 .interruptible = true,
10121 };
Bryan Schumaker7d974792011-06-02 14:59:08 -040010122 int err;
10123 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -040010124 err = _nfs41_test_stateid(server, stateid, cred);
Trond Myklebust43912bb2016-09-22 13:38:56 -040010125 nfs4_handle_delay_or_session_error(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -040010126 } while (exception.retry);
10127 return err;
10128}
Bryan Schumaker9aeda352011-06-02 14:59:09 -040010129
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010130struct nfs_free_stateid_data {
10131 struct nfs_server *server;
10132 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -040010133 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010134};
10135
10136static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
10137{
10138 struct nfs_free_stateid_data *data = calldata;
Anna Schumaker7981c8a2017-01-10 11:39:53 -050010139 nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010140 &data->args.seq_args,
10141 &data->res.seq_res,
10142 task);
10143}
10144
10145static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
10146{
10147 struct nfs_free_stateid_data *data = calldata;
10148
10149 nfs41_sequence_done(task, &data->res.seq_res);
10150
10151 switch (task->tk_status) {
10152 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +100010153 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010154 rpc_restart_call_prepare(task);
10155 }
10156}
10157
10158static void nfs41_free_stateid_release(void *calldata)
10159{
10160 kfree(calldata);
10161}
10162
Trond Myklebust17f26b12013-08-21 15:48:42 -040010163static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010164 .rpc_call_prepare = nfs41_free_stateid_prepare,
10165 .rpc_call_done = nfs41_free_stateid_done,
10166 .rpc_release = nfs41_free_stateid_release,
10167};
10168
Anna Schumaker2f261022018-05-15 13:03:39 -040010169/**
10170 * nfs41_free_stateid - perform a FREE_STATEID operation
10171 *
10172 * @server: server / transport on which to perform the operation
10173 * @stateid: state ID to release
10174 * @cred: credential
Trond Myklebust302fad72019-02-18 13:32:38 -050010175 * @privileged: set to true if this call needs to be privileged
Anna Schumaker2f261022018-05-15 13:03:39 -040010176 *
10177 * Note: this function is always asynchronous.
10178 */
10179static int nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -040010180 const nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +110010181 const struct cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010182 bool privileged)
10183{
Bryan Schumaker9aeda352011-06-02 14:59:09 -040010184 struct rpc_message msg = {
10185 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -040010186 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -040010187 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010188 struct rpc_task_setup task_setup = {
10189 .rpc_client = server->client,
10190 .rpc_message = &msg,
10191 .callback_ops = &nfs41_free_stateid_ops,
Olga Kornievskaia85e39fe2021-06-23 23:28:51 -040010192 .flags = RPC_TASK_ASYNC | RPC_TASK_MOVEABLE,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010193 };
10194 struct nfs_free_stateid_data *data;
Anna Schumaker2f261022018-05-15 13:03:39 -040010195 struct rpc_task *task;
Bryan Schumaker9aeda352011-06-02 14:59:09 -040010196
Weston Andros Adamson3787d502013-08-13 16:37:36 -040010197 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
10198 &task_setup.rpc_client, &msg);
10199
Chuck Lever38527b12012-07-11 16:30:23 -040010200 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010201 data = kmalloc(sizeof(*data), GFP_NOFS);
10202 if (!data)
Anna Schumaker2f261022018-05-15 13:03:39 -040010203 return -ENOMEM;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010204 data->server = server;
10205 nfs4_stateid_copy(&data->args.stateid, stateid);
10206
10207 task_setup.callback_data = data;
10208
10209 msg.rpc_argp = &data->args;
10210 msg.rpc_resp = &data->res;
Anna Schumakerfba83f32018-05-04 16:22:50 -040010211 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, privileged);
Anna Schumaker2f261022018-05-15 13:03:39 -040010212 task = rpc_run_task(&task_setup);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010213 if (IS_ERR(task))
10214 return PTR_ERR(task);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010215 rpc_put_task(task);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -040010216 return 0;
Bryan Schumaker9aeda352011-06-02 14:59:09 -040010217}
Trond Myklebust36281ca2012-03-04 18:13:56 -050010218
Jeff Laytonf1cdae82014-05-01 06:28:47 -040010219static void
10220nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -040010221{
NeilBrowna52458b2018-12-03 11:30:31 +110010222 const struct cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -040010223
Trond Myklebustf0b0bf82016-09-22 13:39:04 -040010224 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -040010225 nfs4_free_lock_state(server, lsp);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -040010226}
10227
Trond Myklebust36281ca2012-03-04 18:13:56 -050010228static bool nfs41_match_stateid(const nfs4_stateid *s1,
10229 const nfs4_stateid *s2)
10230{
Trond Myklebust93b717f2016-05-16 17:42:43 -040010231 if (s1->type != s2->type)
10232 return false;
10233
Trond Myklebust2d2f24a2012-03-04 18:13:57 -050010234 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -050010235 return false;
10236
Trond Myklebust2d2f24a2012-03-04 18:13:57 -050010237 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -050010238 return true;
Trond Myklebust36281ca2012-03-04 18:13:56 -050010239
Anna Schumaker045c5512017-01-11 16:59:48 -050010240 return s1->seqid == 0 || s2->seqid == 0;
Trond Myklebust36281ca2012-03-04 18:13:56 -050010241}
10242
Andy Adamson557134a2009-04-01 09:21:53 -040010243#endif /* CONFIG_NFS_V4_1 */
10244
Trond Myklebust36281ca2012-03-04 18:13:56 -050010245static bool nfs4_match_stateid(const nfs4_stateid *s1,
10246 const nfs4_stateid *s2)
10247{
Trond Myklebustf597c532012-03-04 18:13:56 -050010248 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -050010249}
10250
10251
Trond Myklebust17280172012-03-11 13:11:00 -040010252static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -050010253 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -050010254 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010255 .recover_open = nfs4_open_reclaim,
10256 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -040010257 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -040010258 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010259};
10260
Andy Adamson591d71c2009-04-01 09:22:47 -040010261#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -040010262static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -040010263 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
10264 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
10265 .recover_open = nfs4_open_reclaim,
10266 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -050010267 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -050010268 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -040010269 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -040010270};
10271#endif /* CONFIG_NFS_V4_1 */
10272
Trond Myklebust17280172012-03-11 13:11:00 -040010273static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -050010274 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -050010275 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +030010276 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010277 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -040010278 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010279};
10280
Andy Adamson591d71c2009-04-01 09:22:47 -040010281#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -040010282static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -040010283 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
10284 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -040010285 .recover_open = nfs41_open_expired,
10286 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -050010287 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -040010288};
10289#endif /* CONFIG_NFS_V4_1 */
10290
Trond Myklebust17280172012-03-11 13:11:00 -040010291static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -040010292 .sched_state_renewal = nfs4_proc_async_renew,
NeilBrownf15e1e82018-12-03 11:30:30 +110010293 .get_state_renewal_cred = nfs4_get_renew_cred,
Benny Halevy8e69514f2009-04-01 09:22:45 -040010294 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -040010295};
10296
10297#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -040010298static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -040010299 .sched_state_renewal = nfs41_proc_async_sequence,
NeilBrownf15e1e82018-12-03 11:30:30 +110010300 .get_state_renewal_cred = nfs4_get_machine_cred,
Benny Halevy8e69514f2009-04-01 09:22:45 -040010301 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -040010302};
10303#endif
10304
Chuck Leverec011fe2013-10-17 14:12:39 -040010305static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -040010306 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -040010307 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -040010308};
10309
10310#if defined(CONFIG_NFS_V4_1)
10311static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -040010312 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -040010313 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -040010314};
10315#endif /* CONFIG_NFS_V4_1 */
10316
Trond Myklebust97dc1352010-06-16 09:52:26 -040010317static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
10318 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -040010319 .init_caps = NFS_CAP_READDIRPLUS
10320 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -040010321 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -040010322 .init_client = nfs40_init_client,
10323 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -050010324 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -040010325 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -040010326 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust45870d62016-09-22 13:38:59 -040010327 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -050010328 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -040010329 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -040010330 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
10331 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
10332 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -040010333 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -040010334};
10335
10336#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -050010337static struct nfs_seqid *
10338nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
10339{
10340 return NULL;
10341}
10342
Trond Myklebust97dc1352010-06-16 09:52:26 -040010343static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
10344 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -040010345 .init_caps = NFS_CAP_READDIRPLUS
10346 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -040010347 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -040010348 | NFS_CAP_STATEID_NFSV41
Fred Isaman6e012602016-10-04 15:26:41 -040010349 | NFS_CAP_ATOMIC_OPEN_V1
10350 | NFS_CAP_LGOPEN,
Chuck Leverabf79bb2013-08-09 12:49:11 -040010351 .init_client = nfs41_init_client,
10352 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -050010353 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -040010354 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -040010355 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust45870d62016-09-22 13:38:59 -040010356 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -050010357 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -040010358 .session_trunk = nfs4_test_session_trunk,
Chuck Lever9915ea72013-08-09 12:48:27 -040010359 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -040010360 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
10361 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
10362 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -040010363 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -040010364};
10365#endif
10366
Steve Dickson42c2c422013-05-22 12:50:38 -040010367#if defined(CONFIG_NFS_V4_2)
10368static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
10369 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -040010370 .init_caps = NFS_CAP_READDIRPLUS
10371 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -040010372 | NFS_CAP_POSIX_LOCK
10373 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -040010374 | NFS_CAP_ATOMIC_OPEN_V1
Fred Isaman6e012602016-10-04 15:26:41 -040010375 | NFS_CAP_LGOPEN
Anna Schumakerf4ac1672014-11-25 13:18:15 -050010376 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -040010377 | NFS_CAP_COPY
Olga Kornievskaiacb95dee2018-07-09 15:13:29 -040010378 | NFS_CAP_OFFLOAD_CANCEL
Olga Kornievskaia04915672019-06-04 16:14:30 -040010379 | NFS_CAP_COPY_NOTIFY
Anna Schumaker624bd5b2014-11-25 13:18:16 -050010380 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -040010381 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +080010382 | NFS_CAP_LAYOUTSTATS
Trond Myklebust3eb86092019-02-08 10:31:05 -050010383 | NFS_CAP_CLONE
Anna Schumakerc5675522014-05-28 13:41:22 -040010384 | NFS_CAP_LAYOUTERROR
10385 | NFS_CAP_READ_PLUS,
Chuck Leverabf79bb2013-08-09 12:49:11 -040010386 .init_client = nfs41_init_client,
10387 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -040010388 .match_stateid = nfs41_match_stateid,
10389 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -040010390 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -040010391 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust45870d62016-09-22 13:38:59 -040010392 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -050010393 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -040010394 .session_trunk = nfs4_test_session_trunk,
Steve Dickson42c2c422013-05-22 12:50:38 -040010395 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
10396 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
10397 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +080010398 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -040010399};
10400#endif
10401
Trond Myklebust97dc1352010-06-16 09:52:26 -040010402const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
10403 [0] = &nfs_v4_0_minor_ops,
10404#if defined(CONFIG_NFS_V4_1)
10405 [1] = &nfs_v4_1_minor_ops,
10406#endif
Steve Dickson42c2c422013-05-22 12:50:38 -040010407#if defined(CONFIG_NFS_V4_2)
10408 [2] = &nfs_v4_2_minor_ops,
10409#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -040010410};
10411
Trond Myklebust13997822016-07-24 17:10:52 -040010412static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +010010413{
Frank van der Linden012a2112020-06-23 22:39:03 +000010414 ssize_t error, error2, error3;
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +010010415
10416 error = generic_listxattr(dentry, list, size);
10417 if (error < 0)
10418 return error;
10419 if (list) {
10420 list += error;
10421 size -= error;
10422 }
10423
10424 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
10425 if (error2 < 0)
10426 return error2;
Frank van der Linden012a2112020-06-23 22:39:03 +000010427
10428 if (list) {
10429 list += error2;
10430 size -= error2;
10431 }
10432
10433 error3 = nfs4_listxattr_nfs4_user(d_inode(dentry), list, size);
10434 if (error3 < 0)
10435 return error3;
10436
10437 return error + error2 + error3;
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +010010438}
10439
Trond Myklebust17f26b12013-08-21 15:48:42 -040010440static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -040010441 .create = nfs_create,
10442 .lookup = nfs_lookup,
10443 .atomic_open = nfs_atomic_open,
10444 .link = nfs_link,
10445 .unlink = nfs_unlink,
10446 .symlink = nfs_symlink,
10447 .mkdir = nfs_mkdir,
10448 .rmdir = nfs_rmdir,
10449 .mknod = nfs_mknod,
10450 .rename = nfs_rename,
10451 .permission = nfs_permission,
10452 .getattr = nfs_getattr,
10453 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +010010454 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -040010455};
10456
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -080010457static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +000010458 .permission = nfs_permission,
10459 .getattr = nfs_getattr,
10460 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +010010461 .listxattr = nfs4_listxattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +000010462};
10463
David Howells509de812006-08-22 20:06:11 -040010464const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010465 .version = 4, /* protocol version */
10466 .dentry_ops = &nfs4_dentry_operations,
10467 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +000010468 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -040010469 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010470 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -040010471 .submount = nfs4_submount,
David Howellsf2aedb72019-12-10 07:31:13 -050010472 .try_get_tree = nfs4_try_get_tree,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010473 .getattr = nfs4_proc_getattr,
10474 .setattr = nfs4_proc_setattr,
10475 .lookup = nfs4_proc_lookup,
Jeff Layton5b5faaf2017-06-29 06:34:52 -070010476 .lookupp = nfs4_proc_lookupp,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010477 .access = nfs4_proc_access,
10478 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010479 .create = nfs4_proc_create,
10480 .remove = nfs4_proc_remove,
10481 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -040010482 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010483 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -040010484 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -040010485 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -040010486 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010487 .link = nfs4_proc_link,
10488 .symlink = nfs4_proc_symlink,
10489 .mkdir = nfs4_proc_mkdir,
Trond Myklebust912678d2018-03-20 16:43:15 -040010490 .rmdir = nfs4_proc_rmdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010491 .readdir = nfs4_proc_readdir,
10492 .mknod = nfs4_proc_mknod,
10493 .statfs = nfs4_proc_statfs,
10494 .fsinfo = nfs4_proc_fsinfo,
10495 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -040010496 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010497 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -040010498 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010499 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -050010500 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010501 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -050010502 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010503 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -040010504 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -050010505 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010506 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +000010507 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -040010508 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -040010509 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -040010510 .have_delegation = nfs4_have_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -040010511 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +000010512 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -040010513 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -040010514 .create_server = nfs4_create_server,
10515 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010516};
10517
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000010518static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +010010519 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000010520 .list = nfs4_xattr_list_nfs4_acl,
10521 .get = nfs4_xattr_get_nfs4_acl,
10522 .set = nfs4_xattr_set_nfs4_acl,
10523};
10524
Frank van der Linden012a2112020-06-23 22:39:03 +000010525#ifdef CONFIG_NFS_V4_2
10526static const struct xattr_handler nfs4_xattr_nfs4_user_handler = {
10527 .prefix = XATTR_USER_PREFIX,
10528 .get = nfs4_xattr_get_nfs4_user,
10529 .set = nfs4_xattr_set_nfs4_user,
10530};
10531#endif
10532
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000010533const struct xattr_handler *nfs4_xattr_handlers[] = {
10534 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -040010535#ifdef CONFIG_NFS_V4_SECURITY_LABEL
10536 &nfs4_xattr_nfs4_label_handler,
10537#endif
Frank van der Linden012a2112020-06-23 22:39:03 +000010538#ifdef CONFIG_NFS_V4_2
10539 &nfs4_xattr_nfs4_user_handler,
10540#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000010541 NULL
10542};