blob: f0262397faeca1c23c28aa327cde27371f504104 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/nfs/nfs4proc.c
3 *
4 * Client-side procedure declarations for NFSv4.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/delay.h>
40#include <linux/errno.h>
41#include <linux/string.h>
Trond Myklebust652f89f2011-12-09 19:05:58 -050042#include <linux/ratelimit.h>
43#include <linux/printk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/sunrpc/clnt.h>
46#include <linux/nfs.h>
47#include <linux/nfs4.h>
48#include <linux/nfs_fs.h>
49#include <linux/nfs_page.h>
Bryan Schumaker9b7160c2011-04-13 14:31:30 -040050#include <linux/nfs_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070052#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040053#include <linux/module.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000054#include <linux/xattr.h>
Andy Adamsonc7a360b2011-01-25 19:15:32 -050055#include <linux/utsname.h>
Jeff Laytond3103102011-12-01 22:44:39 +010056#include <linux/freezer.h>
Jeff Layton1eb5d982018-01-09 08:21:17 -050057#include <linux/iversion.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Trond Myklebust4ce79712005-06-22 17:16:21 +000059#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050061#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050062#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040063#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040064#include "pnfs.h"
Chuck Leverf0920752012-05-21 22:45:41 -040065#include "netns.h"
Trond Myklebust39d43d12020-10-07 18:24:18 -040066#include "sysfs.h"
Anna Schumaker40c64c22015-04-15 13:00:05 -040067#include "nfs4idmap.h"
Trond Myklebust73e39aa2012-11-26 12:49:34 -050068#include "nfs4session.h"
David Howellsde242c02012-12-20 21:52:38 +000069#include "fscache.h"
Frank van der Linden012a2112020-06-23 22:39:03 +000070#include "nfs42.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Trond Myklebustc6d01c62013-08-09 11:51:26 -040072#include "nfs4trace.h"
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#define NFSDBG_FACILITY NFSDBG_PROC
75
Trond Myklebust30846df2018-04-07 13:44:28 -040076#define NFS4_BITMASK_SZ 3
77
Trond Myklebust2066fe82006-09-15 08:30:46 -040078#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079#define NFS4_POLL_RETRY_MAX (15*HZ)
80
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +020081/* file attributes which can be mapped to nfs attributes */
82#define NFS4_VALID_ATTRS (ATTR_MODE \
83 | ATTR_UID \
84 | ATTR_GID \
85 | ATTR_SIZE \
86 | ATTR_ATIME \
87 | ATTR_MTIME \
88 | ATTR_CTIME \
89 | ATTR_ATIME_SET \
90 | ATTR_MTIME_SET)
91
Trond Myklebustcdd4e682006-01-03 09:55:12 +010092struct nfs4_opendata;
Alexandros Batsakisb2579572009-12-14 21:27:57 -080093static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Chuck Lever81934dd2012-03-01 17:01:57 -050095static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
Anna Schumaker2ef61e02021-10-22 13:11:07 -040096static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
97 struct nfs_fattr *fattr, struct inode *inode);
NeilBrowna52458b2018-12-03 11:30:31 +110098static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
Trond Myklebust0ab64e02010-04-16 16:22:51 -040099 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +1100100 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
David Quigley1775fd32013-05-22 12:50:42 -0400101 struct nfs4_label *olabel);
Bryan Schumakerf062eb62011-06-02 14:59:10 -0400102#ifdef CONFIG_NFS_V4_1
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400103static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +1100104 const struct cred *cred,
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400105 struct nfs4_slot *slot,
106 bool is_privileged);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -0400107static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
NeilBrowna52458b2018-12-03 11:30:31 +1100108 const struct cred *);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -0400109static int nfs41_free_stateid(struct nfs_server *, const nfs4_stateid *,
NeilBrowna52458b2018-12-03 11:30:31 +1100110 const struct cred *, bool);
Bryan Schumakerf062eb62011-06-02 14:59:10 -0400111#endif
Trond Myklebust332d1a02021-03-25 18:15:36 -0400112static void nfs4_bitmask_set(__u32 bitmask[NFS4_BITMASK_SZ],
113 const __u32 *src, struct inode *inode,
114 struct nfs_server *server,
115 struct nfs4_label *label);
David Quigleyaa9c2662013-05-22 12:50:44 -0400116
117#ifdef CONFIG_NFS_V4_SECURITY_LABEL
118static inline struct nfs4_label *
119nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
120 struct iattr *sattr, struct nfs4_label *label)
121{
122 int err;
123
124 if (label == NULL)
125 return NULL;
126
127 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
128 return NULL;
129
David Quigleyaa9c2662013-05-22 12:50:44 -0400130 err = security_dentry_init_security(dentry, sattr->ia_mode,
131 &dentry->d_name, (void **)&label->label, &label->len);
132 if (err == 0)
133 return label;
134
135 return NULL;
136}
137static inline void
138nfs4_label_release_security(struct nfs4_label *label)
139{
140 if (label)
141 security_release_secctx(label->label, label->len);
142}
143static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
144{
145 if (label)
146 return server->attr_bitmask;
147
148 return server->attr_bitmask_nl;
149}
150#else
151static inline struct nfs4_label *
152nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
153 struct iattr *sattr, struct nfs4_label *l)
154{ return NULL; }
155static inline void
156nfs4_label_release_security(struct nfs4_label *label)
157{ return; }
158static inline u32 *
159nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
160{ return server->attr_bitmask; }
161#endif
162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -0500164static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165{
Trond Myklebust52567b02009-10-23 14:46:42 -0400166 if (err >= -1000)
167 return err;
168 switch (err) {
169 case -NFS4ERR_RESOURCE:
Weston Andros Adamson30005122013-02-28 20:30:10 -0500170 case -NFS4ERR_LAYOUTTRYLATER:
171 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebust52567b02009-10-23 14:46:42 -0400172 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000173 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson88975382013-08-13 16:37:38 -0400174 case -NFS4ERR_WRONG_CRED:
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000175 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800176 case -NFS4ERR_BADOWNER:
177 case -NFS4ERR_BADNAME:
178 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400179 case -NFS4ERR_SHARE_DENIED:
180 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400181 case -NFS4ERR_MINOR_VERS_MISMATCH:
182 return -EPROTONOSUPPORT;
Trond Myklebust6e3cf242013-03-23 15:22:45 -0400183 case -NFS4ERR_FILE_OPEN:
184 return -EBUSY;
Trond Myklebust9fff59e2020-11-02 20:11:32 -0500185 case -NFS4ERR_NOT_SAME:
186 return -ENOTSYNC;
Trond Myklebust52567b02009-10-23 14:46:42 -0400187 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700189 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400190 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400192 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193}
194
195/*
196 * This is our standard bitmap for GETATTR requests.
197 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400198const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 FATTR4_WORD0_TYPE
200 | FATTR4_WORD0_CHANGE
201 | FATTR4_WORD0_SIZE
202 | FATTR4_WORD0_FSID
203 | FATTR4_WORD0_FILEID,
204 FATTR4_WORD1_MODE
205 | FATTR4_WORD1_NUMLINKS
206 | FATTR4_WORD1_OWNER
207 | FATTR4_WORD1_OWNER_GROUP
208 | FATTR4_WORD1_RAWDEV
209 | FATTR4_WORD1_SPACE_USED
210 | FATTR4_WORD1_TIME_ACCESS
211 | FATTR4_WORD1_TIME_METADATA
Anna Schumakerea96d1e2015-04-03 14:35:59 -0400212 | FATTR4_WORD1_TIME_MODIFY
213 | FATTR4_WORD1_MOUNTED_ON_FILEID,
David Quigleyaa9c2662013-05-22 12:50:44 -0400214#ifdef CONFIG_NFS_V4_SECURITY_LABEL
215 FATTR4_WORD2_SECURITY_LABEL
216#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217};
218
Trond Myklebust1549210f2012-06-05 09:16:47 -0400219static const u32 nfs4_pnfs_open_bitmap[3] = {
220 FATTR4_WORD0_TYPE
221 | FATTR4_WORD0_CHANGE
222 | FATTR4_WORD0_SIZE
223 | FATTR4_WORD0_FSID
224 | FATTR4_WORD0_FILEID,
225 FATTR4_WORD1_MODE
226 | FATTR4_WORD1_NUMLINKS
227 | FATTR4_WORD1_OWNER
228 | FATTR4_WORD1_OWNER_GROUP
229 | FATTR4_WORD1_RAWDEV
230 | FATTR4_WORD1_SPACE_USED
231 | FATTR4_WORD1_TIME_ACCESS
232 | FATTR4_WORD1_TIME_METADATA
233 | FATTR4_WORD1_TIME_MODIFY,
234 FATTR4_WORD2_MDSTHRESHOLD
Trond Myklebust95864c92015-12-26 15:06:03 -0500235#ifdef CONFIG_NFS_V4_SECURITY_LABEL
236 | FATTR4_WORD2_SECURITY_LABEL
237#endif
Trond Myklebust1549210f2012-06-05 09:16:47 -0400238};
239
Andy Adamsone23008e2012-10-02 21:07:32 -0400240static const u32 nfs4_open_noattr_bitmap[3] = {
241 FATTR4_WORD0_TYPE
Andy Adamsone23008e2012-10-02 21:07:32 -0400242 | FATTR4_WORD0_FILEID,
243};
244
David Quigleya09df2c2013-05-22 12:50:41 -0400245const u32 nfs4_statfs_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 FATTR4_WORD0_FILES_AVAIL
247 | FATTR4_WORD0_FILES_FREE
248 | FATTR4_WORD0_FILES_TOTAL,
249 FATTR4_WORD1_SPACE_AVAIL
250 | FATTR4_WORD1_SPACE_FREE
251 | FATTR4_WORD1_SPACE_TOTAL
252};
253
David Quigleya09df2c2013-05-22 12:50:41 -0400254const u32 nfs4_pathconf_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 FATTR4_WORD0_MAXLINK
256 | FATTR4_WORD0_MAXNAME,
257 0
258};
259
Fred Isamandae100c2011-07-30 20:52:37 -0400260const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 | FATTR4_WORD0_MAXREAD
262 | FATTR4_WORD0_MAXWRITE
263 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700264 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400265 | FATTR4_WORD1_FS_LAYOUT_TYPES,
266 FATTR4_WORD2_LAYOUT_BLKSIZE
Peng Tao2a92ee92015-09-26 02:24:37 +0800267 | FATTR4_WORD2_CLONE_BLKSIZE
Trond Myklebust7f08a332021-03-26 09:50:19 -0400268 | FATTR4_WORD2_CHANGE_ATTR_TYPE
Frank van der Lindenb78ef842020-06-23 22:38:55 +0000269 | FATTR4_WORD2_XATTR_SUPPORT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270};
271
David Quigleya09df2c2013-05-22 12:50:41 -0400272const u32 nfs4_fs_locations_bitmap[3] = {
Chuck Leverc05cefc2017-11-05 15:45:22 -0500273 FATTR4_WORD0_CHANGE
Manoj Naik830b8e32006-06-09 09:34:25 -0400274 | FATTR4_WORD0_SIZE
275 | FATTR4_WORD0_FSID
276 | FATTR4_WORD0_FILEID
277 | FATTR4_WORD0_FS_LOCATIONS,
Chuck Leverc05cefc2017-11-05 15:45:22 -0500278 FATTR4_WORD1_OWNER
Manoj Naik830b8e32006-06-09 09:34:25 -0400279 | FATTR4_WORD1_OWNER_GROUP
280 | FATTR4_WORD1_RAWDEV
281 | FATTR4_WORD1_SPACE_USED
282 | FATTR4_WORD1_TIME_ACCESS
283 | FATTR4_WORD1_TIME_METADATA
284 | FATTR4_WORD1_TIME_MODIFY
David Quigleya09df2c2013-05-22 12:50:41 -0400285 | FATTR4_WORD1_MOUNTED_ON_FILEID,
Manoj Naik830b8e32006-06-09 09:34:25 -0400286};
287
Trond Myklebust30846df2018-04-07 13:44:28 -0400288static void nfs4_bitmap_copy_adjust(__u32 *dst, const __u32 *src,
Trond Myklebusta71029b2021-04-10 00:23:03 -0400289 struct inode *inode, unsigned long flags)
Trond Myklebust30846df2018-04-07 13:44:28 -0400290{
291 unsigned long cache_validity;
292
293 memcpy(dst, src, NFS4_BITMASK_SZ*sizeof(*dst));
294 if (!inode || !nfs4_have_delegation(inode, FMODE_READ))
295 return;
296
Trond Myklebusta71029b2021-04-10 00:23:03 -0400297 cache_validity = READ_ONCE(NFS_I(inode)->cache_validity) | flags;
Trond Myklebust30846df2018-04-07 13:44:28 -0400298
Trond Myklebusta71029b2021-04-10 00:23:03 -0400299 /* Remove the attributes over which we have full control */
300 dst[1] &= ~FATTR4_WORD1_RAWDEV;
Trond Myklebust30846df2018-04-07 13:44:28 -0400301 if (!(cache_validity & NFS_INO_INVALID_SIZE))
302 dst[0] &= ~FATTR4_WORD0_SIZE;
303
304 if (!(cache_validity & NFS_INO_INVALID_CHANGE))
305 dst[0] &= ~FATTR4_WORD0_CHANGE;
Trond Myklebust30846df2018-04-07 13:44:28 -0400306
Trond Myklebust720869e2021-04-13 09:41:16 -0400307 if (!(cache_validity & NFS_INO_INVALID_MODE))
308 dst[1] &= ~FATTR4_WORD1_MODE;
Trond Myklebusta71029b2021-04-10 00:23:03 -0400309 if (!(cache_validity & NFS_INO_INVALID_OTHER))
Trond Myklebust720869e2021-04-13 09:41:16 -0400310 dst[1] &= ~(FATTR4_WORD1_OWNER | FATTR4_WORD1_OWNER_GROUP);
Trond Myklebust30846df2018-04-07 13:44:28 -0400311}
312
Al Virobc4785c2006-10-19 23:28:51 -0700313static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 struct nfs4_readdir_arg *readdir)
315{
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400316 unsigned int attrs = FATTR4_WORD0_FILEID | FATTR4_WORD0_TYPE;
Al Viro0dbb4c62006-10-19 23:28:49 -0700317 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000320 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
322 return;
323 }
324
325 readdir->cookie = 0;
326 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
327 if (cookie == 2)
328 return;
329
330 /*
331 * NFSv4 servers do not return entries for '.' and '..'
332 * Therefore, we fake these entries here. We let '.'
333 * have cookie 0 and '..' have cookie 1. Note that
334 * when talking to the server, we always send cookie 0
335 * instead of 1 or 2.
336 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800337 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339 if (cookie == 0) {
340 *p++ = xdr_one; /* next */
341 *p++ = xdr_zero; /* cookie, first word */
342 *p++ = xdr_one; /* cookie, second word */
343 *p++ = xdr_one; /* entry len */
344 memcpy(p, ".\0\0\0", 4); /* entry */
345 p++;
346 *p++ = xdr_one; /* bitmap length */
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400347 *p++ = htonl(attrs); /* bitmap */
348 *p++ = htonl(12); /* attribute buffer length */
349 *p++ = htonl(NF4DIR);
David Howells2b0143b2015-03-17 22:25:59 +0000350 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 }
352
353 *p++ = xdr_one; /* next */
354 *p++ = xdr_zero; /* cookie, first word */
355 *p++ = xdr_two; /* cookie, second word */
356 *p++ = xdr_two; /* entry len */
357 memcpy(p, "..\0\0", 4); /* entry */
358 p++;
359 *p++ = xdr_one; /* bitmap length */
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400360 *p++ = htonl(attrs); /* bitmap */
361 *p++ = htonl(12); /* attribute buffer length */
362 *p++ = htonl(NF4DIR);
David Howells2b0143b2015-03-17 22:25:59 +0000363 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
365 readdir->pgbase = (char *)p - (char *)start;
366 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800367 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368}
369
Trond Myklebust26d36302016-09-22 13:39:05 -0400370static void nfs4_test_and_free_stateid(struct nfs_server *server,
371 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +1100372 const struct cred *cred)
Trond Myklebust26d36302016-09-22 13:39:05 -0400373{
374 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
375
376 ops->test_and_free_expired(server, stateid, cred);
377}
378
379static void __nfs4_free_revoked_stateid(struct nfs_server *server,
380 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +1100381 const struct cred *cred)
Trond Myklebust26d36302016-09-22 13:39:05 -0400382{
383 stateid->type = NFS4_REVOKED_STATEID_TYPE;
384 nfs4_test_and_free_stateid(server, stateid, cred);
385}
386
387static void nfs4_free_revoked_stateid(struct nfs_server *server,
388 const nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +1100389 const struct cred *cred)
Trond Myklebust26d36302016-09-22 13:39:05 -0400390{
391 nfs4_stateid tmp;
392
393 nfs4_stateid_copy(&tmp, stateid);
394 __nfs4_free_revoked_stateid(server, &tmp, cred);
395}
396
NeilBrown8478eaa2014-09-18 16:09:27 +1000397static long nfs4_update_delay(long *timeout)
398{
399 long ret;
400 if (!timeout)
401 return NFS4_POLL_RETRY_MAX;
402 if (*timeout <= 0)
403 *timeout = NFS4_POLL_RETRY_MIN;
404 if (*timeout > NFS4_POLL_RETRY_MAX)
405 *timeout = NFS4_POLL_RETRY_MAX;
406 ret = *timeout;
407 *timeout <<= 1;
408 return ret;
409}
410
Trond Myklebust0688e642019-04-07 13:59:09 -0400411static int nfs4_delay_killable(long *timeout)
Trond Myklebust65de8722008-12-23 15:21:44 -0500412{
Trond Myklebust65de8722008-12-23 15:21:44 -0500413 might_sleep();
414
NeilBrown8478eaa2014-09-18 16:09:27 +1000415 freezable_schedule_timeout_killable_unsafe(
416 nfs4_update_delay(timeout));
Trond Myklebust0688e642019-04-07 13:59:09 -0400417 if (!__fatal_signal_pending(current))
418 return 0;
419 return -EINTR;
420}
421
422static int nfs4_delay_interruptible(long *timeout)
423{
424 might_sleep();
425
He Zhe59679d92020-07-06 17:52:24 +0800426 freezable_schedule_timeout_interruptible_unsafe(nfs4_update_delay(timeout));
Trond Myklebust0688e642019-04-07 13:59:09 -0400427 if (!signal_pending(current))
428 return 0;
429 return __fatal_signal_pending(current) ? -EINTR :-ERESTARTSYS;
430}
431
432static int nfs4_delay(long *timeout, bool interruptible)
433{
434 if (interruptible)
435 return nfs4_delay_interruptible(timeout);
436 return nfs4_delay_killable(timeout);
Trond Myklebust65de8722008-12-23 15:21:44 -0500437}
438
Trond Myklebust50c80002019-07-11 19:02:18 -0400439static const nfs4_stateid *
440nfs4_recoverable_stateid(const nfs4_stateid *stateid)
441{
442 if (!stateid)
443 return NULL;
444 switch (stateid->type) {
445 case NFS4_OPEN_STATEID_TYPE:
446 case NFS4_LOCK_STATEID_TYPE:
447 case NFS4_DELEGATION_STATEID_TYPE:
448 return stateid;
449 default:
450 break;
451 }
452 return NULL;
453}
454
Trond Myklebust65de8722008-12-23 15:21:44 -0500455/* This is the error handling routine for processes that are allowed
456 * to sleep.
457 */
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400458static int nfs4_do_handle_exception(struct nfs_server *server,
459 int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500460{
461 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500462 struct nfs4_state *state = exception->state;
Trond Myklebust50c80002019-07-11 19:02:18 -0400463 const nfs4_stateid *stateid;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500464 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500465 int ret = errorcode;
466
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400467 exception->delay = 0;
468 exception->recovering = 0;
Trond Myklebust65de8722008-12-23 15:21:44 -0500469 exception->retry = 0;
Trond Myklebust272289a2016-09-22 13:39:15 -0400470
Trond Myklebust50c80002019-07-11 19:02:18 -0400471 stateid = nfs4_recoverable_stateid(exception->stateid);
Trond Myklebust272289a2016-09-22 13:39:15 -0400472 if (stateid == NULL && state != NULL)
Trond Myklebust50c80002019-07-11 19:02:18 -0400473 stateid = nfs4_recoverable_stateid(&state->stateid);
Trond Myklebust272289a2016-09-22 13:39:15 -0400474
Trond Myklebust65de8722008-12-23 15:21:44 -0500475 switch(errorcode) {
476 case 0:
477 return 0;
Trond Myklebustcf61eb22018-05-29 22:06:08 -0400478 case -NFS4ERR_BADHANDLE:
479 case -ESTALE:
480 if (inode != NULL && S_ISREG(inode->i_mode))
481 pnfs_destroy_layout(NFS_I(inode));
482 break;
Trond Myklebust5ba12442015-06-16 11:26:35 -0400483 case -NFS4ERR_DELEG_REVOKED:
484 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust272289a2016-09-22 13:39:15 -0400485 case -NFS4ERR_EXPIRED:
Trond Myklebust5ba12442015-06-16 11:26:35 -0400486 case -NFS4ERR_BAD_STATEID:
Olga Kornievskaiafefa1a82019-06-14 14:22:12 -0400487 case -NFS4ERR_PARTNER_NO_AUTH:
Trond Myklebust272289a2016-09-22 13:39:15 -0400488 if (inode != NULL && stateid != NULL) {
489 nfs_inode_find_state_and_recover(inode,
490 stateid);
491 goto wait_on_recovery;
492 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500493 fallthrough;
Trond Myklebust272289a2016-09-22 13:39:15 -0400494 case -NFS4ERR_OPENMODE:
Trond Myklebust8487c472016-06-26 08:44:35 -0400495 if (inode) {
496 int err;
497
498 err = nfs_async_inode_return_delegation(inode,
499 stateid);
500 if (err == 0)
501 goto wait_on_recovery;
502 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
503 exception->retry = 1;
504 break;
505 }
506 }
Trond Myklebust3114ea72012-03-07 16:39:06 -0500507 if (state == NULL)
508 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400509 ret = nfs4_schedule_stateid_recovery(server, state);
510 if (ret < 0)
511 break;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500512 goto wait_on_recovery;
Trond Myklebust65de8722008-12-23 15:21:44 -0500513 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500514 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500515 nfs4_schedule_lease_recovery(clp);
516 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -0400517 case -NFS4ERR_MOVED:
518 ret = nfs4_schedule_migration_recovery(server);
519 if (ret < 0)
520 break;
521 goto wait_on_recovery;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -0400522 case -NFS4ERR_LEASE_MOVED:
523 nfs4_schedule_lease_moved_recovery(clp);
524 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500525#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400526 case -NFS4ERR_BADSESSION:
527 case -NFS4ERR_BADSLOT:
528 case -NFS4ERR_BAD_HIGH_SLOT:
529 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
530 case -NFS4ERR_DEADSESSION:
531 case -NFS4ERR_SEQ_FALSE_RETRY:
532 case -NFS4ERR_SEQ_MISORDERED:
Trond Myklebust5c441542019-11-13 08:34:00 +0100533 /* Handled in nfs41_sequence_process() */
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400534 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500535#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500536 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500537 if (exception->timeout > HZ) {
538 /* We have retried a decent amount, time to
539 * fail
540 */
541 ret = -EBUSY;
542 break;
543 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500544 fallthrough;
Trond Myklebust65de8722008-12-23 15:21:44 -0500545 case -NFS4ERR_DELAY:
Trond Myklebust2598ed32015-09-20 16:10:18 -0400546 nfs_inc_server_stats(server, NFSIOS_DELAY);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500547 fallthrough;
Trond Myklebust2598ed32015-09-20 16:10:18 -0400548 case -NFS4ERR_GRACE:
Trond Myklebuste85d7ee2016-07-14 18:46:24 -0400549 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -0400550 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400551 exception->delay = 1;
552 return 0;
553
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400554 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500555 case -NFS4ERR_OLD_STATEID:
556 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800557 break;
558 case -NFS4ERR_BADOWNER:
559 /* The following works around a Linux server bug! */
560 case -NFS4ERR_BADNAME:
561 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
562 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
563 exception->retry = 1;
564 printk(KERN_WARNING "NFS: v4 server %s "
565 "does not accept raw "
566 "uid/gids. "
567 "Reenabling the idmapper.\n",
568 server->nfs_client->cl_hostname);
569 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500570 }
571 /* We failed to handle the error */
572 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500573wait_on_recovery:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400574 exception->recovering = 1;
575 return 0;
576}
577
578/* This is the error handling routine for processes that are allowed
579 * to sleep.
580 */
581int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
582{
583 struct nfs_client *clp = server->nfs_client;
584 int ret;
585
586 ret = nfs4_do_handle_exception(server, errorcode, exception);
587 if (exception->delay) {
Trond Myklebust0688e642019-04-07 13:59:09 -0400588 ret = nfs4_delay(&exception->timeout,
589 exception->interruptible);
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400590 goto out_retry;
591 }
592 if (exception->recovering) {
Trond Myklebustdfe1fe72021-06-01 11:10:05 -0400593 if (exception->task_is_privileged)
594 return -EDEADLOCK;
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400595 ret = nfs4_wait_clnt_recover(clp);
596 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
597 return -EIO;
598 goto out_retry;
599 }
600 return ret;
601out_retry:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500602 if (ret == 0)
603 exception->retry = 1;
604 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500605}
606
Trond Myklebust037fc982015-09-20 15:51:00 -0400607static int
608nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
609 int errorcode, struct nfs4_exception *exception)
610{
611 struct nfs_client *clp = server->nfs_client;
612 int ret;
613
614 ret = nfs4_do_handle_exception(server, errorcode, exception);
615 if (exception->delay) {
616 rpc_delay(task, nfs4_update_delay(&exception->timeout));
617 goto out_retry;
618 }
619 if (exception->recovering) {
Trond Myklebustdfe1fe72021-06-01 11:10:05 -0400620 if (exception->task_is_privileged)
621 return -EDEADLOCK;
Trond Myklebust037fc982015-09-20 15:51:00 -0400622 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
623 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
624 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
625 goto out_retry;
626 }
627 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
628 ret = -EIO;
629 return ret;
630out_retry:
Bill Baker0f90be12018-06-19 16:24:58 -0500631 if (ret == 0) {
Trond Myklebust037fc982015-09-20 15:51:00 -0400632 exception->retry = 1;
Bill Baker0f90be12018-06-19 16:24:58 -0500633 /*
634 * For NFS4ERR_MOVED, the client transport will need to
635 * be recomputed after migration recovery has completed.
636 */
637 if (errorcode == -NFS4ERR_MOVED)
638 rpc_task_release_transport(task);
639 }
Trond Myklebust037fc982015-09-20 15:51:00 -0400640 return ret;
641}
642
Olga Kornievskaia0f913a52018-07-09 15:13:33 -0400643int
Trond Myklebust037fc982015-09-20 15:51:00 -0400644nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
645 struct nfs4_state *state, long *timeout)
646{
647 struct nfs4_exception exception = {
648 .state = state,
649 };
650
651 if (task->tk_status >= 0)
652 return 0;
653 if (timeout)
654 exception.timeout = *timeout;
655 task->tk_status = nfs4_async_handle_exception(task, server,
656 task->tk_status,
657 &exception);
658 if (exception.delay && timeout)
659 *timeout = exception.timeout;
660 if (exception.retry)
661 return -EAGAIN;
662 return 0;
663}
664
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400665/*
666 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
667 * or 'false' otherwise.
668 */
669static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
670{
671 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
Anna Schumakereeea5362017-01-11 16:01:21 -0500672 return (flavor == RPC_AUTH_GSS_KRB5I) || (flavor == RPC_AUTH_GSS_KRB5P);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400673}
Trond Myklebust65de8722008-12-23 15:21:44 -0500674
Trond Myklebust452e9352010-07-31 14:29:06 -0400675static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 spin_lock(&clp->cl_lock);
678 if (time_before(clp->cl_last_renewal,timestamp))
679 clp->cl_last_renewal = timestamp;
680 spin_unlock(&clp->cl_lock);
681}
682
Trond Myklebust452e9352010-07-31 14:29:06 -0400683static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
684{
Trond Myklebustbe824162015-07-05 14:50:46 -0400685 struct nfs_client *clp = server->nfs_client;
686
687 if (!nfs4_has_session(clp))
688 do_renew_lease(clp, timestamp);
Trond Myklebust452e9352010-07-31 14:29:06 -0400689}
690
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400691struct nfs4_call_sync_data {
692 const struct nfs_server *seq_server;
693 struct nfs4_sequence_args *seq_args;
694 struct nfs4_sequence_res *seq_res;
695};
696
Trond Myklebustbe3a5d22015-06-23 19:51:55 +0800697void nfs4_init_sequence(struct nfs4_sequence_args *args,
Anna Schumakerfba83f32018-05-04 16:22:50 -0400698 struct nfs4_sequence_res *res, int cache_reply,
699 int privileged)
Chuck Levera9c92d62013-08-09 12:48:18 -0400700{
701 args->sa_slot = NULL;
702 args->sa_cache_this = cache_reply;
Anna Schumakerfba83f32018-05-04 16:22:50 -0400703 args->sa_privileged = privileged;
Chuck Levera9c92d62013-08-09 12:48:18 -0400704
705 res->sr_slot = NULL;
706}
707
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400708static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
Chuck Lever3bd23842013-08-09 12:49:19 -0400709{
710 struct nfs4_slot *slot = res->sr_slot;
711 struct nfs4_slot_table *tbl;
712
Chuck Lever3bd23842013-08-09 12:49:19 -0400713 tbl = slot->table;
714 spin_lock(&tbl->slot_tbl_lock);
715 if (!nfs41_wake_and_assign_slot(tbl, slot))
716 nfs4_free_slot(tbl, slot);
717 spin_unlock(&tbl->slot_tbl_lock);
718
719 res->sr_slot = NULL;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400720}
721
722static int nfs40_sequence_done(struct rpc_task *task,
723 struct nfs4_sequence_res *res)
724{
725 if (res->sr_slot != NULL)
726 nfs40_sequence_free_slot(res);
Chuck Lever3bd23842013-08-09 12:49:19 -0400727 return 1;
728}
729
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400730#if defined(CONFIG_NFS_V4_1)
731
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400732static void nfs41_release_slot(struct nfs4_slot *slot)
Andy Adamson13615872009-04-01 09:22:17 -0400733{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500734 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400735 struct nfs4_slot_table *tbl;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500736 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400737
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400738 if (!slot)
739 return;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500740 tbl = slot->table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500741 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500742
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400743 /* Bump the slot sequence number */
744 if (slot->seq_done)
745 slot->seq_nr++;
746 slot->seq_done = 0;
747
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500748 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500749 /* Be nice to the server: try to ensure that the last transmitted
750 * value for highest_user_slotid <= target_highest_slotid
751 */
752 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
753 send_new_highest_used_slotid = true;
754
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500755 if (nfs41_wake_and_assign_slot(tbl, slot)) {
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500756 send_new_highest_used_slotid = false;
757 goto out_unlock;
758 }
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500759 nfs4_free_slot(tbl, slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500760
761 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
762 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500763out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500764 spin_unlock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500765 if (send_new_highest_used_slotid)
Anna Schumaker3f10a6a2015-07-13 14:01:31 -0400766 nfs41_notify_server(session->clp);
Trond Myklebust045d2a62016-08-28 13:25:43 -0400767 if (waitqueue_active(&tbl->slot_waitq))
768 wake_up_all(&tbl->slot_waitq);
Andy Adamson13615872009-04-01 09:22:17 -0400769}
770
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400771static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
772{
773 nfs41_release_slot(res->sr_slot);
774 res->sr_slot = NULL;
775}
776
Trond Myklebust3453d572018-06-20 17:53:34 -0400777static void nfs4_slot_sequence_record_sent(struct nfs4_slot *slot,
778 u32 seqnr)
779{
780 if ((s32)(seqnr - slot->seq_nr_highest_sent) > 0)
781 slot->seq_nr_highest_sent = seqnr;
782}
783static void nfs4_slot_sequence_acked(struct nfs4_slot *slot,
784 u32 seqnr)
785{
786 slot->seq_nr_highest_sent = seqnr;
787 slot->seq_nr_last_acked = seqnr;
788}
789
Anna Schumaker913fadc2020-07-08 10:33:40 -0400790static void nfs4_probe_sequence(struct nfs_client *client, const struct cred *cred,
791 struct nfs4_slot *slot)
792{
793 struct rpc_task *task = _nfs41_proc_sequence(client, cred, slot, true);
794 if (!IS_ERR(task))
795 rpc_put_task_async(task);
796}
797
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400798static int nfs41_sequence_process(struct rpc_task *task,
799 struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400800{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500801 struct nfs4_session *session;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500802 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400803 struct nfs_client *clp;
Trond Myklebust5c441542019-11-13 08:34:00 +0100804 int status;
Trond Myklebust85563072012-12-11 10:31:12 -0500805 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400806
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500807 if (slot == NULL)
808 goto out_noaction;
Bryan Schumaker468f8612011-04-18 15:57:32 -0400809 /* don't increment the sequence number if the task wasn't sent */
Trond Myklebustc71c46f2019-03-01 11:40:05 -0500810 if (!RPC_WAS_SENT(task) || slot->seq_done)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400811 goto out;
812
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500813 session = slot->table->session;
Anna Schumaker913fadc2020-07-08 10:33:40 -0400814 clp = session->clp;
Trond Myklebust933602e2012-11-16 12:12:38 -0500815
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400816 trace_nfs4_sequence_done(session, res);
Trond Myklebust5c441542019-11-13 08:34:00 +0100817
818 status = res->sr_status;
819 if (task->tk_status == -NFS4ERR_DEADSESSION)
820 status = -NFS4ERR_DEADSESSION;
821
Andy Adamson691daf32009-12-04 15:55:39 -0500822 /* Check the SEQUENCE operation status */
Trond Myklebust5c441542019-11-13 08:34:00 +0100823 switch (status) {
Trond Myklebust14516c32010-07-31 14:29:06 -0400824 case 0:
Trond Myklebust3453d572018-06-20 17:53:34 -0400825 /* Mark this sequence number as having been acked */
826 nfs4_slot_sequence_acked(slot, slot->seq_nr);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400827 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400828 slot->seq_done = 1;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500829 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500830 /* Check sequence flags */
Trond Myklebust0a014a42016-09-22 13:38:51 -0400831 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
832 !!slot->privileged);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500833 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400834 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500835 case 1:
836 /*
837 * sr_status remains 1 if an RPC level error occurred.
838 * The server may or may not have processed the sequence
839 * operation..
Trond Myklebustac20d162012-12-15 15:36:07 -0500840 */
Trond Myklebust3453d572018-06-20 17:53:34 -0400841 nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
842 slot->seq_done = 1;
Trond Myklebustac20d162012-12-15 15:36:07 -0500843 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400844 case -NFS4ERR_DELAY:
845 /* The server detected a resend of the RPC call and
846 * returned NFS4ERR_DELAY as per Section 2.10.6.2
847 * of RFC5661.
848 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500849 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400850 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500851 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500852 slot->seq_nr);
Trond Myklebust3453d572018-06-20 17:53:34 -0400853 nfs4_slot_sequence_acked(slot, slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400854 goto out_retry;
Trond Myklebustf9312a52018-06-09 19:10:31 -0400855 case -NFS4ERR_RETRY_UNCACHED_REP:
856 case -NFS4ERR_SEQ_FALSE_RETRY:
857 /*
858 * The server thinks we tried to replay a request.
859 * Retry the call after bumping the sequence ID.
860 */
Trond Myklebust3453d572018-06-20 17:53:34 -0400861 nfs4_slot_sequence_acked(slot, slot->seq_nr);
Trond Myklebustf9312a52018-06-09 19:10:31 -0400862 goto retry_new_seq;
Trond Myklebust85563072012-12-11 10:31:12 -0500863 case -NFS4ERR_BADSLOT:
864 /*
865 * The slot id we used was probably retired. Try again
866 * using a different slot id.
867 */
Trond Myklebust99589102018-06-09 12:50:50 -0400868 if (slot->slot_nr < slot->table->target_highest_slotid)
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400869 goto session_recover;
Trond Myklebuste8794442012-12-15 13:56:18 -0500870 goto retry_nowait;
871 case -NFS4ERR_SEQ_MISORDERED:
Trond Myklebust3453d572018-06-20 17:53:34 -0400872 nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
Trond Myklebuste8794442012-12-15 13:56:18 -0500873 /*
Trond Myklebust3453d572018-06-20 17:53:34 -0400874 * Were one or more calls using this slot interrupted?
875 * If the server never received the request, then our
Anna Schumaker913fadc2020-07-08 10:33:40 -0400876 * transmitted slot sequence number may be too high. However,
877 * if the server did receive the request then it might
878 * accidentally give us a reply with a mismatched operation.
879 * We can sort this out by sending a lone sequence operation
880 * to the server on the same slot.
Trond Myklebustac20d162012-12-15 15:36:07 -0500881 */
Trond Myklebust3453d572018-06-20 17:53:34 -0400882 if ((s32)(slot->seq_nr - slot->seq_nr_last_acked) > 1) {
883 slot->seq_nr--;
Anna Schumaker913fadc2020-07-08 10:33:40 -0400884 if (task->tk_msg.rpc_proc != &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE]) {
885 nfs4_probe_sequence(clp, task->tk_msg.rpc_cred, slot);
886 res->sr_slot = NULL;
887 }
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500888 goto retry_nowait;
889 }
Trond Myklebust3453d572018-06-20 17:53:34 -0400890 /*
891 * RFC5661:
892 * A retry might be sent while the original request is
893 * still in progress on the replier. The replier SHOULD
894 * deal with the issue by returning NFS4ERR_DELAY as the
895 * reply to SEQUENCE or CB_SEQUENCE operation, but
896 * implementations MAY return NFS4ERR_SEQ_MISORDERED.
897 *
898 * Restart the search after a delay.
899 */
900 slot->seq_nr = slot->seq_nr_highest_sent;
901 goto out_retry;
Trond Myklebust5c441542019-11-13 08:34:00 +0100902 case -NFS4ERR_BADSESSION:
903 case -NFS4ERR_DEADSESSION:
904 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
905 goto session_recover;
Trond Myklebust14516c32010-07-31 14:29:06 -0400906 default:
907 /* Just update the slot sequence no. */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400908 slot->seq_done = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400909 }
910out:
911 /* The session may be reset by one of the error handlers. */
912 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500913out_noaction:
Trond Myklebust85563072012-12-11 10:31:12 -0500914 return ret;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400915session_recover:
Trond Myklebust5c441542019-11-13 08:34:00 +0100916 nfs4_schedule_session_recovery(session, status);
917 dprintk("%s ERROR: %d Reset session\n", __func__, status);
918 nfs41_sequence_free_slot(res);
919 goto out;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400920retry_new_seq:
921 ++slot->seq_nr;
Trond Myklebuste8794442012-12-15 13:56:18 -0500922retry_nowait:
923 if (rpc_restart_call_prepare(task)) {
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400924 nfs41_sequence_free_slot(res);
Trond Myklebuste8794442012-12-15 13:56:18 -0500925 task->tk_status = 0;
926 ret = 0;
927 }
928 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400929out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400930 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400931 goto out;
932 rpc_delay(task, NFS4_POLL_RETRY_MAX);
933 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400934}
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400935
936int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
937{
938 if (!nfs41_sequence_process(task, res))
939 return 0;
940 if (res->sr_slot != NULL)
941 nfs41_sequence_free_slot(res);
942 return 1;
943
944}
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500945EXPORT_SYMBOL_GPL(nfs41_sequence_done);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400946
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400947static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
948{
949 if (res->sr_slot == NULL)
950 return 1;
951 if (res->sr_slot->table->session != NULL)
952 return nfs41_sequence_process(task, res);
953 return nfs40_sequence_done(task, res);
954}
955
956static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
957{
958 if (res->sr_slot != NULL) {
959 if (res->sr_slot->table->session != NULL)
960 nfs41_sequence_free_slot(res);
961 else
962 nfs40_sequence_free_slot(res);
963 }
964}
965
Peng Tao2c4b1312014-06-11 05:24:16 +0800966int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400967{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500968 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400969 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400970 if (!res->sr_slot->table->session)
971 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400972 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400973}
Peng Tao2c4b1312014-06-11 05:24:16 +0800974EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Trond Myklebustdf896452010-06-16 09:52:26 -0400975
Andy Adamsonce5039c2009-04-01 09:22:13 -0400976static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
977{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400978 struct nfs4_call_sync_data *data = calldata;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400979
Trond Myklebust035168ab2010-06-16 09:52:26 -0400980 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
981
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500982 nfs4_setup_sequence(data->seq_server->nfs_client,
983 data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400984}
985
Andy Adamson69ab40c2009-04-01 09:22:19 -0400986static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
987{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400988 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400989
Trond Myklebust14516c32010-07-31 14:29:06 -0400990 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400991}
992
Trond Myklebust17280172012-03-11 13:11:00 -0400993static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400994 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400995 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400996};
997
Chuck Lever3bd23842013-08-09 12:49:19 -0400998#else /* !CONFIG_NFS_V4_1 */
999
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001000static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
1001{
1002 return nfs40_sequence_done(task, res);
1003}
1004
1005static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
1006{
1007 if (res->sr_slot != NULL)
1008 nfs40_sequence_free_slot(res);
1009}
1010
Peng Tao2c4b1312014-06-11 05:24:16 +08001011int nfs4_sequence_done(struct rpc_task *task,
1012 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -04001013{
Chuck Lever3bd23842013-08-09 12:49:19 -04001014 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -04001015}
Peng Tao2c4b1312014-06-11 05:24:16 +08001016EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Chuck Lever3bd23842013-08-09 12:49:19 -04001017
1018#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001019
Trond Myklebustc1dffe02019-03-01 12:13:34 -05001020static void nfs41_sequence_res_init(struct nfs4_sequence_res *res)
1021{
1022 res->sr_timestamp = jiffies;
1023 res->sr_status_flags = 0;
1024 res->sr_status = 1;
1025}
1026
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04001027static
1028void nfs4_sequence_attach_slot(struct nfs4_sequence_args *args,
1029 struct nfs4_sequence_res *res,
1030 struct nfs4_slot *slot)
1031{
1032 if (!slot)
1033 return;
1034 slot->privileged = args->sa_privileged ? 1 : 0;
1035 args->sa_slot = slot;
1036
1037 res->sr_slot = slot;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04001038}
1039
1040int nfs4_setup_sequence(struct nfs_client *client,
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001041 struct nfs4_sequence_args *args,
1042 struct nfs4_sequence_res *res,
1043 struct rpc_task *task)
1044{
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001045 struct nfs4_session *session = nfs4_get_session(client);
Anna Schumaker76ee0352017-01-10 16:49:31 -05001046 struct nfs4_slot_table *tbl = client->cl_slot_tbl;
Anna Schumaker3d358082017-01-11 10:54:04 -05001047 struct nfs4_slot *slot;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001048
Anna Schumaker9dd9107f2017-01-10 12:01:46 -05001049 /* slot already allocated? */
1050 if (res->sr_slot != NULL)
1051 goto out_start;
1052
Trond Myklebust6b2e6852019-04-07 13:58:49 -04001053 if (session)
Anna Schumaker76ee0352017-01-10 16:49:31 -05001054 tbl = &session->fc_slot_table;
Anna Schumaker76ee0352017-01-10 16:49:31 -05001055
Trond Myklebust3453d572018-06-20 17:53:34 -04001056 spin_lock(&tbl->slot_tbl_lock);
1057 /* The state manager will wait until the slot table is empty */
1058 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
1059 goto out_sleep;
Anna Schumaker6994cdd2017-01-10 16:13:27 -05001060
Trond Myklebust3453d572018-06-20 17:53:34 -04001061 slot = nfs4_alloc_slot(tbl);
1062 if (IS_ERR(slot)) {
Trond Myklebust3453d572018-06-20 17:53:34 -04001063 if (slot == ERR_PTR(-ENOMEM))
Trond Myklebust6b2e6852019-04-07 13:58:49 -04001064 goto out_sleep_timeout;
Trond Myklebust3453d572018-06-20 17:53:34 -04001065 goto out_sleep;
Anna Schumaker3d358082017-01-11 10:54:04 -05001066 }
Trond Myklebust3453d572018-06-20 17:53:34 -04001067 spin_unlock(&tbl->slot_tbl_lock);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001068
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04001069 nfs4_sequence_attach_slot(args, res, slot);
Anna Schumaker3d358082017-01-11 10:54:04 -05001070
Anna Schumakerad05cc02017-01-11 13:37:06 -05001071 trace_nfs4_setup_sequence(session, args);
Anna Schumaker9dd9107f2017-01-10 12:01:46 -05001072out_start:
Trond Myklebustc1dffe02019-03-01 12:13:34 -05001073 nfs41_sequence_res_init(res);
Anna Schumaker9dd9107f2017-01-10 12:01:46 -05001074 rpc_call_start(task);
1075 return 0;
Trond Myklebust6b2e6852019-04-07 13:58:49 -04001076out_sleep_timeout:
1077 /* Try again in 1/4 second */
1078 if (args->sa_privileged)
1079 rpc_sleep_on_priority_timeout(&tbl->slot_tbl_waitq, task,
1080 jiffies + (HZ >> 2), RPC_PRIORITY_PRIVILEGED);
1081 else
1082 rpc_sleep_on_timeout(&tbl->slot_tbl_waitq, task,
1083 NULL, jiffies + (HZ >> 2));
1084 spin_unlock(&tbl->slot_tbl_lock);
1085 return -EAGAIN;
Anna Schumaker0dcee8b2017-01-10 16:29:54 -05001086out_sleep:
1087 if (args->sa_privileged)
1088 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
Trond Myklebust8357a9b2019-04-07 13:58:48 -04001089 RPC_PRIORITY_PRIVILEGED);
Anna Schumaker0dcee8b2017-01-10 16:29:54 -05001090 else
1091 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
1092 spin_unlock(&tbl->slot_tbl_lock);
1093 return -EAGAIN;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001094}
1095EXPORT_SYMBOL_GPL(nfs4_setup_sequence);
1096
Chuck Lever9915ea72013-08-09 12:48:27 -04001097static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
1098{
1099 struct nfs4_call_sync_data *data = calldata;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05001100 nfs4_setup_sequence(data->seq_server->nfs_client,
Chuck Lever9915ea72013-08-09 12:48:27 -04001101 data->seq_args, data->seq_res, task);
1102}
1103
1104static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
1105{
1106 struct nfs4_call_sync_data *data = calldata;
1107 nfs4_sequence_done(task, data->seq_res);
1108}
1109
1110static const struct rpc_call_ops nfs40_call_sync_ops = {
1111 .rpc_call_prepare = nfs40_call_sync_prepare,
1112 .rpc_call_done = nfs40_call_sync_done,
1113};
1114
Anna Schumaker48c05852019-08-14 15:27:00 -04001115static int nfs4_call_sync_custom(struct rpc_task_setup *task_setup)
1116{
1117 int ret;
1118 struct rpc_task *task;
1119
1120 task = rpc_run_task(task_setup);
1121 if (IS_ERR(task))
1122 return PTR_ERR(task);
1123
1124 ret = task->tk_status;
1125 rpc_put_task(task);
1126 return ret;
1127}
1128
Trond Myklebustc74dfe92020-01-06 15:39:37 -05001129static int nfs4_do_call_sync(struct rpc_clnt *clnt,
1130 struct nfs_server *server,
1131 struct rpc_message *msg,
1132 struct nfs4_sequence_args *args,
1133 struct nfs4_sequence_res *res,
1134 unsigned short task_flags)
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001135{
Chuck Lever9915ea72013-08-09 12:48:27 -04001136 struct nfs_client *clp = server->nfs_client;
1137 struct nfs4_call_sync_data data = {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001138 .seq_server = server,
Trond Myklebustad389da2007-06-05 12:30:00 -04001139 .seq_args = args,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001140 .seq_res = res,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001141 };
1142 struct rpc_task_setup task_setup = {
Trond Myklebustad389da2007-06-05 12:30:00 -04001143 .rpc_client = clnt,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001144 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -04001145 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebustc74dfe92020-01-06 15:39:37 -05001146 .callback_data = &data,
1147 .flags = task_flags,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001148 };
1149
Anna Schumaker48c05852019-08-14 15:27:00 -04001150 return nfs4_call_sync_custom(&task_setup);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001151}
1152
Trond Myklebustc74dfe92020-01-06 15:39:37 -05001153static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
1154 struct nfs_server *server,
1155 struct rpc_message *msg,
1156 struct nfs4_sequence_args *args,
1157 struct nfs4_sequence_res *res)
1158{
Olga Kornievskaia85e39fe2021-06-23 23:28:51 -04001159 unsigned short task_flags = 0;
1160
1161 if (server->nfs_client->cl_minorversion)
1162 task_flags = RPC_TASK_MOVEABLE;
1163 return nfs4_do_call_sync(clnt, server, msg, args, res, task_flags);
Trond Myklebustc74dfe92020-01-06 15:39:37 -05001164}
1165
1166
Bryan Schumaker7c513052011-03-24 17:12:24 +00001167int nfs4_call_sync(struct rpc_clnt *clnt,
1168 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001169 struct rpc_message *msg,
1170 struct nfs4_sequence_args *args,
1171 struct nfs4_sequence_res *res,
1172 int cache_reply)
1173{
Anna Schumakerfba83f32018-05-04 16:22:50 -04001174 nfs4_init_sequence(args, res, cache_reply, 0);
Chuck Lever9915ea72013-08-09 12:48:27 -04001175 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001176}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
Trond Myklebust3c591172018-07-31 15:54:10 -04001178static void
1179nfs4_inc_nlink_locked(struct inode *inode)
1180{
Trond Myklebust82eae5a2021-04-01 14:59:59 -04001181 nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE |
1182 NFS_INO_INVALID_CTIME |
1183 NFS_INO_INVALID_NLINK);
Trond Myklebust3c591172018-07-31 15:54:10 -04001184 inc_nlink(inode);
1185}
1186
1187static void
Trond Myklebust1301e422021-04-01 14:57:56 -04001188nfs4_inc_nlink(struct inode *inode)
1189{
1190 spin_lock(&inode->i_lock);
1191 nfs4_inc_nlink_locked(inode);
1192 spin_unlock(&inode->i_lock);
1193}
1194
1195static void
Trond Myklebust3c591172018-07-31 15:54:10 -04001196nfs4_dec_nlink_locked(struct inode *inode)
1197{
Trond Myklebust82eae5a2021-04-01 14:59:59 -04001198 nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE |
1199 NFS_INO_INVALID_CTIME |
1200 NFS_INO_INVALID_NLINK);
Trond Myklebust3c591172018-07-31 15:54:10 -04001201 drop_nlink(inode);
1202}
1203
1204static void
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001205nfs4_update_changeattr_locked(struct inode *inode,
1206 struct nfs4_change_info *cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04001207 unsigned long timestamp, unsigned long cache_validity)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208{
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001209 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust6f9be832021-03-26 11:01:19 -04001210 u64 change_attr = inode_peek_iversion_raw(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
Trond Myklebustb6f80a22021-03-08 14:42:55 -05001212 cache_validity |= NFS_INO_INVALID_CTIME | NFS_INO_INVALID_MTIME;
Trond Myklebust20cf7d42021-06-25 15:49:31 -04001213 if (S_ISDIR(inode->i_mode))
1214 cache_validity |= NFS_INO_INVALID_DATA;
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001215
Trond Myklebust6f9be832021-03-26 11:01:19 -04001216 switch (NFS_SERVER(inode)->change_attr_type) {
1217 case NFS4_CHANGE_TYPE_IS_UNDEFINED:
Trond Myklebust20cf7d42021-06-25 15:49:31 -04001218 if (cinfo->after == change_attr)
Trond Myklebust6f9be832021-03-26 11:01:19 -04001219 goto out;
1220 break;
1221 default:
1222 if ((s64)(change_attr - cinfo->after) >= 0)
1223 goto out;
1224 }
1225
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001226 inode_set_iversion_raw(inode, cinfo->after);
Trond Myklebust20cf7d42021-06-25 15:49:31 -04001227 if (!cinfo->atomic || cinfo->before != change_attr) {
1228 if (S_ISDIR(inode->i_mode))
1229 nfs_force_lookup_revalidate(inode);
1230
1231 if (!NFS_PROTO(inode)->have_delegation(inode, FMODE_READ))
1232 cache_validity |=
1233 NFS_INO_INVALID_ACCESS | NFS_INO_INVALID_ACL |
1234 NFS_INO_INVALID_SIZE | NFS_INO_INVALID_OTHER |
1235 NFS_INO_INVALID_BLOCKS | NFS_INO_INVALID_NLINK |
1236 NFS_INO_INVALID_MODE | NFS_INO_INVALID_XATTR |
1237 NFS_INO_REVAL_PAGECACHE;
1238 nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
1239 }
1240 nfsi->attrtimeo_timestamp = jiffies;
Trond Myklebustd3129ef2017-01-11 22:07:28 -05001241 nfsi->read_cache_jiffies = timestamp;
Trond Myklebust3235b402015-02-26 19:52:06 -05001242 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
Trond Myklebustc8d07152018-07-31 15:54:12 -04001243 nfsi->cache_validity &= ~NFS_INO_INVALID_CHANGE;
Trond Myklebust6f9be832021-03-26 11:01:19 -04001244out:
1245 nfs_set_cache_invalid(inode, cache_validity);
Trond Myklebust3c591172018-07-31 15:54:10 -04001246}
1247
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001248void
1249nfs4_update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04001250 unsigned long timestamp, unsigned long cache_validity)
Trond Myklebust3c591172018-07-31 15:54:10 -04001251{
1252 spin_lock(&dir->i_lock);
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001253 nfs4_update_changeattr_locked(dir, cinfo, timestamp, cache_validity);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 spin_unlock(&dir->i_lock);
1255}
1256
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001257struct nfs4_open_createattrs {
1258 struct nfs4_label *label;
1259 struct iattr *sattr;
1260 const __u32 verf[2];
1261};
1262
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001263static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1264 int err, struct nfs4_exception *exception)
1265{
1266 if (err != -EINVAL)
1267 return false;
1268 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1269 return false;
1270 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1271 exception->retry = 1;
1272 return true;
1273}
1274
Trond Myklebust1bf85d82019-06-27 06:30:48 -04001275static fmode_t _nfs4_ctx_to_accessmode(const struct nfs_open_context *ctx)
1276{
1277 return ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
1278}
1279
1280static fmode_t _nfs4_ctx_to_openmode(const struct nfs_open_context *ctx)
1281{
1282 fmode_t ret = ctx->mode & (FMODE_READ|FMODE_WRITE);
1283
1284 return (ctx->mode & FMODE_EXEC) ? FMODE_READ | ret : ret;
1285}
1286
Trond Myklebust6ae37332015-01-30 14:21:14 -05001287static u32
1288nfs4_map_atomic_open_share(struct nfs_server *server,
1289 fmode_t fmode, int openflags)
1290{
1291 u32 res = 0;
1292
1293 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1294 case FMODE_READ:
1295 res = NFS4_SHARE_ACCESS_READ;
1296 break;
1297 case FMODE_WRITE:
1298 res = NFS4_SHARE_ACCESS_WRITE;
1299 break;
1300 case FMODE_READ|FMODE_WRITE:
1301 res = NFS4_SHARE_ACCESS_BOTH;
1302 }
1303 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1304 goto out;
1305 /* Want no delegation if we're using O_DIRECT */
1306 if (openflags & O_DIRECT)
1307 res |= NFS4_SHARE_WANT_NO_DELEG;
1308out:
1309 return res;
1310}
1311
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001312static enum open_claim_type4
1313nfs4_map_atomic_open_claim(struct nfs_server *server,
1314 enum open_claim_type4 claim)
1315{
1316 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1317 return claim;
1318 switch (claim) {
1319 default:
1320 return claim;
1321 case NFS4_OPEN_CLAIM_FH:
1322 return NFS4_OPEN_CLAIM_NULL;
1323 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1324 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1325 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1326 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1327 }
1328}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
1330static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001331{
1332 p->o_res.f_attr = &p->f_attr;
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
Anna Schumaker76baa2b2021-10-22 13:11:06 -04001358 p->f_attr.label = nfs4_label_alloc(server, gfp_mask);
1359 if (IS_ERR(p->f_attr.label))
David Quigley14c43f72013-05-22 12:50:43 -04001360 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 }
Trond Myklebusta1e7f302021-07-14 13:00:58 -04001391 /* ask server to check for all possible rights as results
1392 * are cached */
1393 switch (p->o_arg.claim) {
1394 default:
1395 break;
1396 case NFS4_OPEN_CLAIM_NULL:
1397 case NFS4_OPEN_CLAIM_FH:
1398 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
1399 NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE |
1400 NFS4_ACCESS_EXECUTE;
Frank van der Linden72832a22020-06-23 22:38:58 +00001401#ifdef CONFIG_NFS_V4_2
Trond Myklebusta1e7f302021-07-14 13:00:58 -04001402 if (!(server->caps & NFS_CAP_XATTR))
1403 break;
1404 p->o_arg.access |= NFS4_ACCESS_XAREAD | NFS4_ACCESS_XAWRITE |
1405 NFS4_ACCESS_XALIST;
Frank van der Linden72832a22020-06-23 22:38:58 +00001406#endif
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001407 }
David Howells7539bba2006-08-22 20:06:09 -04001408 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001409 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1410 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001411 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001412 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001413 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001414 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001415 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001416 case NFS4_OPEN_CLAIM_NULL:
1417 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1418 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1419 p->o_arg.fh = NFS_FH(dir);
1420 break;
1421 case NFS4_OPEN_CLAIM_PREVIOUS:
1422 case NFS4_OPEN_CLAIM_FH:
1423 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1424 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
David Howells2b0143b2015-03-17 22:25:59 +00001425 p->o_arg.fh = NFS_FH(d_inode(dentry));
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001426 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001427 p->c_arg.fh = &p->o_res.fh;
1428 p->c_arg.stateid = &p->o_res.stateid;
1429 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001430 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001431 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001432 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001433
1434err_free_label:
Kinglong Meea49c2692015-07-27 15:31:38 +08001435 nfs4_label_free(p->a_label);
1436err_free_f:
Anna Schumaker76baa2b2021-10-22 13:11:06 -04001437 nfs4_label_free(p->f_attr.label);
David Quigley14c43f72013-05-22 12:50:43 -04001438err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001439 kfree(p);
1440err:
1441 dput(parent);
1442 return NULL;
1443}
1444
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001445static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001446{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001447 struct nfs4_opendata *p = container_of(kref,
1448 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001449 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001450
Fred Isaman30ae2412016-10-18 13:39:51 -04001451 nfs4_lgopen_release(p->lgp);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001452 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001453 nfs4_sequence_free_slot(&p->o_res.seq_res);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001454 if (p->state != NULL)
1455 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001456 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001457
Kinglong Meea49c2692015-07-27 15:31:38 +08001458 nfs4_label_free(p->a_label);
Anna Schumaker76baa2b2021-10-22 13:11:06 -04001459 nfs4_label_free(p->f_attr.label);
David Quigley14c43f72013-05-22 12:50:43 -04001460
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001461 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001462 dput(p->dentry);
1463 nfs_sb_deactive(sb);
Trond Myklebust6926afd12012-01-07 13:22:46 -05001464 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001465 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001466 kfree(p);
1467}
1468
1469static void nfs4_opendata_put(struct nfs4_opendata *p)
1470{
1471 if (p != NULL)
1472 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001473}
1474
Trond Myklebust24311f82015-09-20 10:50:17 -04001475static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1476 fmode_t fmode)
1477{
1478 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1479 case FMODE_READ|FMODE_WRITE:
1480 return state->n_rdwr != 0;
1481 case FMODE_WRITE:
1482 return state->n_wronly != 0;
1483 case FMODE_READ:
1484 return state->n_rdonly != 0;
1485 }
1486 WARN_ON_ONCE(1);
1487 return false;
1488}
1489
Trond Myklebustbe189f72018-09-27 17:12:33 -04001490static int can_open_cached(struct nfs4_state *state, fmode_t mode,
1491 int open_mode, enum open_claim_type4 claim)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001492{
1493 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001494
Trond Myklebust536e43d2012-01-17 22:04:26 -05001495 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001496 goto out;
Trond Myklebustbe189f72018-09-27 17:12:33 -04001497 switch (claim) {
1498 case NFS4_OPEN_CLAIM_NULL:
1499 case NFS4_OPEN_CLAIM_FH:
1500 goto out;
1501 default:
1502 break;
1503 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001504 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001505 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001506 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1507 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001508 break;
1509 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001510 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1511 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001512 break;
1513 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001514 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1515 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001516 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001517out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001518 return ret;
1519}
1520
Trond Myklebust2a606182015-08-19 22:30:00 -05001521static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1522 enum open_claim_type4 claim)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001523{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001524 if (delegation == NULL)
1525 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001526 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001527 return 0;
Trond Myklebust2a606182015-08-19 22:30:00 -05001528 switch (claim) {
1529 case NFS4_OPEN_CLAIM_NULL:
1530 case NFS4_OPEN_CLAIM_FH:
1531 break;
1532 case NFS4_OPEN_CLAIM_PREVIOUS:
1533 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1534 break;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001535 fallthrough;
Trond Myklebust2a606182015-08-19 22:30:00 -05001536 default:
1537 return 0;
1538 }
Trond Myklebustb7391f42008-12-23 15:21:52 -05001539 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001540 return 1;
1541}
1542
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001543static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001544{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001545 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001546 case FMODE_WRITE:
1547 state->n_wronly++;
1548 break;
1549 case FMODE_READ:
1550 state->n_rdonly++;
1551 break;
1552 case FMODE_READ|FMODE_WRITE:
1553 state->n_rdwr++;
1554 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001555 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001556}
1557
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04001558#ifdef CONFIG_NFS_V4_1
1559static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1560{
1561 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1562 return true;
1563 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1564 return true;
1565 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1566 return true;
1567 return false;
1568}
1569#endif /* CONFIG_NFS_V4_1 */
1570
Trond Myklebustc9399f22017-11-06 15:28:01 -05001571static void nfs_state_log_update_open_stateid(struct nfs4_state *state)
1572{
1573 if (test_and_clear_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1574 wake_up_all(&state->waitq);
1575}
1576
Trond Myklebust4f14c192014-02-12 19:15:06 -05001577static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001578{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001579 struct nfs_client *clp = state->owner->so_server->nfs_client;
1580 bool need_recover = false;
1581
1582 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1583 need_recover = true;
1584 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1585 need_recover = true;
1586 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1587 need_recover = true;
1588 if (need_recover)
1589 nfs4_state_mark_reclaim_nograce(clp, state);
1590}
1591
Trond Myklebustc9399f22017-11-06 15:28:01 -05001592/*
1593 * Check for whether or not the caller may update the open stateid
1594 * to the value passed in by stateid.
1595 *
1596 * Note: This function relies heavily on the server implementing
1597 * RFC7530 Section 9.1.4.2, and RFC5661 Section 8.2.2
1598 * correctly.
1599 * i.e. The stateid seqids have to be initialised to 1, and
1600 * are then incremented on every state transition.
1601 */
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04001602static bool nfs_stateid_is_sequential(struct nfs4_state *state,
Trond Myklebustc9399f22017-11-06 15:28:01 -05001603 const nfs4_stateid *stateid)
Trond Myklebuste999e802014-02-10 18:20:47 -05001604{
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04001605 if (test_bit(NFS_OPEN_STATE, &state->flags)) {
1606 /* The common case - we're updating to a new sequence number */
Trond Myklebust01d29f82021-10-26 21:56:40 -04001607 if (nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1608 if (nfs4_stateid_is_next(&state->open_stateid, stateid))
1609 return true;
1610 return false;
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04001611 }
Trond Myklebust01d29f82021-10-26 21:56:40 -04001612 /* The server returned a new stateid */
Trond Myklebustc9399f22017-11-06 15:28:01 -05001613 }
Trond Myklebust01d29f82021-10-26 21:56:40 -04001614 /* This is the first OPEN in this generation */
1615 if (stateid->seqid == cpu_to_be32(1))
1616 return true;
Trond Myklebuste999e802014-02-10 18:20:47 -05001617 return false;
1618}
1619
Trond Myklebustf95549c2015-01-23 18:06:09 -05001620static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1621{
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001622 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1623 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001624 if (state->n_wronly)
1625 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1626 if (state->n_rdonly)
1627 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1628 if (state->n_rdwr)
1629 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001630 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustf95549c2015-01-23 18:06:09 -05001631}
1632
Trond Myklebust226056c2014-02-11 10:41:07 -05001633static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1634 nfs4_stateid *stateid, fmode_t fmode)
1635{
1636 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1637 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1638 case FMODE_WRITE:
1639 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1640 break;
1641 case FMODE_READ:
1642 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1643 break;
1644 case 0:
1645 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1646 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1647 clear_bit(NFS_OPEN_STATE, &state->flags);
1648 }
1649 if (stateid == NULL)
1650 return;
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001651 /* Handle OPEN+OPEN_DOWNGRADE races */
1652 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1653 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
Trond Myklebustf95549c2015-01-23 18:06:09 -05001654 nfs_resync_open_stateid_locked(state);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001655 goto out;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001656 }
Trond Myklebust003707c2007-07-05 18:07:55 -04001657 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001658 nfs4_stateid_copy(&state->stateid, stateid);
1659 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001660 trace_nfs4_open_stateid_update(state->inode, stateid, 0);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001661out:
1662 nfs_state_log_update_open_stateid(state);
Trond Myklebust226056c2014-02-11 10:41:07 -05001663}
1664
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001665static void nfs_clear_open_stateid(struct nfs4_state *state,
1666 nfs4_stateid *arg_stateid,
1667 nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust226056c2014-02-11 10:41:07 -05001668{
1669 write_seqlock(&state->seqlock);
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001670 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1671 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1672 nfs_clear_open_stateid_locked(state, stateid, fmode);
Trond Myklebust226056c2014-02-11 10:41:07 -05001673 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001674 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1675 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001676}
1677
Trond Myklebust1393d962016-09-22 13:39:13 -04001678static void nfs_set_open_stateid_locked(struct nfs4_state *state,
Trond Myklebustc9399f22017-11-06 15:28:01 -05001679 const nfs4_stateid *stateid, nfs4_stateid *freeme)
Trond Myklebuste9acf212019-01-22 14:01:16 -05001680 __must_hold(&state->owner->so_lock)
1681 __must_hold(&state->seqlock)
1682 __must_hold(RCU)
1683
Trond Myklebust003707c2007-07-05 18:07:55 -04001684{
Trond Myklebustc9399f22017-11-06 15:28:01 -05001685 DEFINE_WAIT(wait);
1686 int status = 0;
1687 for (;;) {
1688
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04001689 if (nfs_stateid_is_sequential(state, stateid))
Trond Myklebust003707c2007-07-05 18:07:55 -04001690 break;
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04001691
Trond Myklebustc9399f22017-11-06 15:28:01 -05001692 if (status)
Trond Myklebust003707c2007-07-05 18:07:55 -04001693 break;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001694 /* Rely on seqids for serialisation with NFSv4.0 */
1695 if (!nfs4_has_session(NFS_SERVER(state->inode)->nfs_client))
1696 break;
1697
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04001698 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001699 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
1700 /*
1701 * Ensure we process the state changes in the same order
1702 * in which the server processed them by delaying the
1703 * update of the stateid until we are in sequence.
1704 */
1705 write_sequnlock(&state->seqlock);
1706 spin_unlock(&state->owner->so_lock);
1707 rcu_read_unlock();
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001708 trace_nfs4_open_stateid_update_wait(state->inode, stateid, 0);
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04001709
zhouchuangaobb002382021-05-09 19:34:37 -07001710 if (!fatal_signal_pending(current)) {
Trond Myklebustc9399f22017-11-06 15:28:01 -05001711 if (schedule_timeout(5*HZ) == 0)
1712 status = -EAGAIN;
1713 else
1714 status = 0;
1715 } else
1716 status = -EINTR;
1717 finish_wait(&state->waitq, &wait);
1718 rcu_read_lock();
1719 spin_lock(&state->owner->so_lock);
1720 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001721 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001722
Trond Myklebuste1fff5d2017-11-07 13:10:46 -05001723 if (test_bit(NFS_OPEN_STATE, &state->flags) &&
1724 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
Trond Myklebustc9399f22017-11-06 15:28:01 -05001725 nfs4_stateid_copy(freeme, &state->open_stateid);
1726 nfs_test_and_clear_all_open_stateid(state);
1727 }
1728
Trond Myklebuste999e802014-02-10 18:20:47 -05001729 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1730 nfs4_stateid_copy(&state->stateid, stateid);
1731 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001732 trace_nfs4_open_stateid_update(state->inode, stateid, status);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001733 nfs_state_log_update_open_stateid(state);
Trond Myklebust003707c2007-07-05 18:07:55 -04001734}
1735
Trond Myklebustc9399f22017-11-06 15:28:01 -05001736static void nfs_state_set_open_stateid(struct nfs4_state *state,
Trond Myklebust1393d962016-09-22 13:39:13 -04001737 const nfs4_stateid *open_stateid,
Trond Myklebust1393d962016-09-22 13:39:13 -04001738 fmode_t fmode,
1739 nfs4_stateid *freeme)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001741 /*
1742 * Protect the call to nfs4_state_set_mode_locked and
1743 * serialise the stateid update
1744 */
1745 write_seqlock(&state->seqlock);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001746 nfs_set_open_stateid_locked(state, open_stateid, freeme);
1747 switch (fmode) {
1748 case FMODE_READ:
1749 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1750 break;
1751 case FMODE_WRITE:
1752 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1753 break;
1754 case FMODE_READ|FMODE_WRITE:
1755 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust003707c2007-07-05 18:07:55 -04001756 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001757 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001758 write_sequnlock(&state->seqlock);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001759}
1760
Trond Myklebust27a30cf2019-07-22 18:32:59 +01001761static void nfs_state_clear_open_state_flags(struct nfs4_state *state)
1762{
1763 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1764 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1765 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1766 clear_bit(NFS_OPEN_STATE, &state->flags);
1767}
1768
Trond Myklebustc9399f22017-11-06 15:28:01 -05001769static void nfs_state_set_delegation(struct nfs4_state *state,
1770 const nfs4_stateid *deleg_stateid,
1771 fmode_t fmode)
1772{
1773 /*
1774 * Protect the call to nfs4_state_set_mode_locked and
1775 * serialise the stateid update
1776 */
1777 write_seqlock(&state->seqlock);
1778 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1779 set_bit(NFS_DELEGATED_STATE, &state->flags);
1780 write_sequnlock(&state->seqlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781}
1782
Trond Myklebust9f0c5122018-09-05 14:07:15 -04001783static void nfs_state_clear_delegation(struct nfs4_state *state)
1784{
1785 write_seqlock(&state->seqlock);
1786 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1787 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1788 write_sequnlock(&state->seqlock);
1789}
1790
Olga Kornievskaiaec4b0922019-10-08 16:33:53 -04001791int update_open_stateid(struct nfs4_state *state,
Trond Myklebust1393d962016-09-22 13:39:13 -04001792 const nfs4_stateid *open_stateid,
1793 const nfs4_stateid *delegation,
1794 fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001795{
Trond Myklebust1393d962016-09-22 13:39:13 -04001796 struct nfs_server *server = NFS_SERVER(state->inode);
1797 struct nfs_client *clp = server->nfs_client;
Trond Myklebust34310432008-12-23 15:21:38 -05001798 struct nfs_inode *nfsi = NFS_I(state->inode);
1799 struct nfs_delegation *deleg_cur;
Arnd Bergmann83aa3e02016-10-18 17:21:30 +02001800 nfs4_stateid freeme = { };
Trond Myklebust34310432008-12-23 15:21:38 -05001801 int ret = 0;
1802
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001803 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001804
1805 rcu_read_lock();
Trond Myklebustc9399f22017-11-06 15:28:01 -05001806 spin_lock(&state->owner->so_lock);
1807 if (open_stateid != NULL) {
1808 nfs_state_set_open_stateid(state, open_stateid, fmode, &freeme);
1809 ret = 1;
1810 }
1811
Trond Myklebust333ac782019-10-22 12:12:17 -04001812 deleg_cur = nfs4_get_valid_delegation(state->inode);
Trond Myklebust34310432008-12-23 15:21:38 -05001813 if (deleg_cur == NULL)
1814 goto no_delegation;
1815
1816 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001817 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001818 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001819 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001820 goto no_delegation_unlock;
1821
1822 if (delegation == NULL)
1823 delegation = &deleg_cur->stateid;
Trond Myklebust333ac782019-10-22 12:12:17 -04001824 else if (!nfs4_stateid_match_other(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001825 goto no_delegation_unlock;
1826
Trond Myklebustb7391f42008-12-23 15:21:52 -05001827 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001828 nfs_state_set_delegation(state, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001829 ret = 1;
1830no_delegation_unlock:
1831 spin_unlock(&deleg_cur->lock);
1832no_delegation:
Trond Myklebustc9399f22017-11-06 15:28:01 -05001833 if (ret)
1834 update_open_stateflags(state, fmode);
1835 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001836 rcu_read_unlock();
1837
Trond Myklebust4f14c192014-02-12 19:15:06 -05001838 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
Trond Myklebust1393d962016-09-22 13:39:13 -04001839 nfs4_schedule_state_manager(clp);
1840 if (freeme.type != 0)
1841 nfs4_test_and_free_stateid(server, &freeme,
1842 state->owner->so_cred);
Trond Myklebust34310432008-12-23 15:21:38 -05001843
1844 return ret;
1845}
1846
Trond Myklebust39071e62015-01-24 15:07:56 -05001847static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1848 const nfs4_stateid *stateid)
1849{
1850 struct nfs4_state *state = lsp->ls_state;
1851 bool ret = false;
1852
1853 spin_lock(&state->state_lock);
1854 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1855 goto out_noupdate;
1856 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1857 goto out_noupdate;
1858 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1859 ret = true;
1860out_noupdate:
1861 spin_unlock(&state->state_lock);
1862 return ret;
1863}
Trond Myklebust34310432008-12-23 15:21:38 -05001864
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001865static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001866{
1867 struct nfs_delegation *delegation;
1868
Trond Myklebustf5086242018-03-20 16:43:13 -04001869 fmode &= FMODE_READ|FMODE_WRITE;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001870 rcu_read_lock();
Trond Myklebust40e6aa12019-10-27 13:38:45 -04001871 delegation = nfs4_get_valid_delegation(inode);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001872 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001873 rcu_read_unlock();
1874 return;
1875 }
1876 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001877 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001878}
1879
Trond Myklebust6ee41262007-07-08 14:11:36 -04001880static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001881{
1882 struct nfs4_state *state = opendata->state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001883 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001884 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001885 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebust2a606182015-08-19 22:30:00 -05001886 enum open_claim_type4 claim = opendata->o_arg.claim;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001887 nfs4_stateid stateid;
1888 int ret = -EAGAIN;
1889
Trond Myklebustaac00a82007-07-05 19:02:21 -04001890 for (;;) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001891 spin_lock(&state->owner->so_lock);
Trond Myklebustbe189f72018-09-27 17:12:33 -04001892 if (can_open_cached(state, fmode, open_mode, claim)) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001893 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001894 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef72014-09-03 14:15:40 -04001895 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001896 }
Anna Schumaker61beef72014-09-03 14:15:40 -04001897 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001898 rcu_read_lock();
Trond Myklebustbe3df3d2019-10-31 18:40:32 -04001899 delegation = nfs4_get_valid_delegation(state->inode);
Trond Myklebust2a606182015-08-19 22:30:00 -05001900 if (!can_open_delegated(delegation, fmode, claim)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001901 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001902 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001903 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001904 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001905 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001906 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001907 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001908 if (!opendata->is_recover) {
1909 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1910 if (ret != 0)
1911 goto out;
1912 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001913 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001914
1915 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001916 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001917 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001918 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001919out:
1920 return ERR_PTR(ret);
1921out_return_state:
Trond Myklebustace9fad2018-09-02 19:19:07 -04001922 refcount_inc(&state->count);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001923 return state;
1924}
1925
Andy Adamsone23008e2012-10-02 21:07:32 -04001926static void
1927nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1928{
1929 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1930 struct nfs_delegation *delegation;
1931 int delegation_flags = 0;
1932
1933 rcu_read_lock();
1934 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1935 if (delegation)
1936 delegation_flags = delegation->flags;
1937 rcu_read_unlock();
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001938 switch (data->o_arg.claim) {
1939 default:
1940 break;
1941 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1942 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001943 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1944 "returning a delegation for "
1945 "OPEN(CLAIM_DELEGATE_CUR)\n",
1946 clp->cl_hostname);
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001947 return;
1948 }
1949 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Andy Adamsone23008e2012-10-02 21:07:32 -04001950 nfs_inode_set_delegation(state->inode,
Trond Myklebust35156bf2018-03-20 17:03:13 -04001951 data->owner->so_cred,
1952 data->o_res.delegation_type,
1953 &data->o_res.delegation,
1954 data->o_res.pagemod_limit);
Andy Adamsone23008e2012-10-02 21:07:32 -04001955 else
1956 nfs_inode_reclaim_delegation(state->inode,
Trond Myklebust35156bf2018-03-20 17:03:13 -04001957 data->owner->so_cred,
1958 data->o_res.delegation_type,
1959 &data->o_res.delegation,
1960 data->o_res.pagemod_limit);
Jeff Layton8b199e52018-07-05 05:48:14 -04001961
1962 if (data->o_res.do_recall)
1963 nfs_async_inode_return_delegation(state->inode,
1964 &data->o_res.delegation);
Andy Adamsone23008e2012-10-02 21:07:32 -04001965}
1966
1967/*
1968 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1969 * and update the nfs4_state.
1970 */
1971static struct nfs4_state *
1972_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1973{
1974 struct inode *inode = data->state->inode;
1975 struct nfs4_state *state = data->state;
1976 int ret;
1977
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001978 if (!data->rpc_done) {
Anna Schumaker37a84842017-01-11 16:08:35 -05001979 if (data->rpc_status)
1980 return ERR_PTR(data->rpc_status);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001981 /* cached opens have already been processed */
1982 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001983 }
1984
Andy Adamsone23008e2012-10-02 21:07:32 -04001985 ret = nfs_refresh_inode(inode, &data->f_attr);
1986 if (ret)
Anna Schumaker37a84842017-01-11 16:08:35 -05001987 return ERR_PTR(ret);
Andy Adamsone23008e2012-10-02 21:07:32 -04001988
1989 if (data->o_res.delegation_type != 0)
1990 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001991update:
Trond Myklebuste3c8dc72019-07-29 18:25:00 +01001992 if (!update_open_stateid(state, &data->o_res.stateid,
1993 NULL, data->o_arg.fmode))
1994 return ERR_PTR(-EAGAIN);
Trond Myklebustace9fad2018-09-02 19:19:07 -04001995 refcount_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001996
1997 return state;
Andy Adamsone23008e2012-10-02 21:07:32 -04001998}
1999
Trond Myklebust4e2fcac2017-08-08 09:06:18 -04002000static struct inode *
2001nfs4_opendata_get_inode(struct nfs4_opendata *data)
2002{
2003 struct inode *inode;
2004
2005 switch (data->o_arg.claim) {
2006 case NFS4_OPEN_CLAIM_NULL:
2007 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
2008 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
2009 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
2010 return ERR_PTR(-EAGAIN);
2011 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh,
Anna Schumaker76baa2b2021-10-22 13:11:06 -04002012 &data->f_attr, data->f_attr.label);
Trond Myklebust4e2fcac2017-08-08 09:06:18 -04002013 break;
2014 default:
2015 inode = d_inode(data->dentry);
2016 ihold(inode);
2017 nfs_refresh_inode(inode, &data->f_attr);
2018 }
2019 return inode;
2020}
2021
Andy Adamsone23008e2012-10-02 21:07:32 -04002022static struct nfs4_state *
Trond Myklebust75e8c482017-08-08 10:38:07 -04002023nfs4_opendata_find_nfs4_state(struct nfs4_opendata *data)
2024{
2025 struct nfs4_state *state;
2026 struct inode *inode;
2027
2028 inode = nfs4_opendata_get_inode(data);
2029 if (IS_ERR(inode))
2030 return ERR_CAST(inode);
2031 if (data->state != NULL && data->state->inode == inode) {
2032 state = data->state;
Trond Myklebustace9fad2018-09-02 19:19:07 -04002033 refcount_inc(&state->count);
Trond Myklebust75e8c482017-08-08 10:38:07 -04002034 } else
2035 state = nfs4_get_open_state(inode, data->owner);
2036 iput(inode);
2037 if (state == NULL)
2038 state = ERR_PTR(-ENOMEM);
2039 return state;
2040}
2041
Andy Adamsone23008e2012-10-02 21:07:32 -04002042static struct nfs4_state *
2043_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002044{
Trond Myklebust75e8c482017-08-08 10:38:07 -04002045 struct nfs4_state *state;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002046
Trond Myklebustaac00a82007-07-05 19:02:21 -04002047 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04002048 state = nfs4_try_open_cached(data);
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002049 trace_nfs4_cached_open(data->state);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002050 goto out;
2051 }
2052
Trond Myklebust75e8c482017-08-08 10:38:07 -04002053 state = nfs4_opendata_find_nfs4_state(data);
2054 if (IS_ERR(state))
2055 goto out;
2056
Andy Adamsone23008e2012-10-02 21:07:32 -04002057 if (data->o_res.delegation_type != 0)
2058 nfs4_opendata_check_deleg(data, state);
Trond Myklebuste3c8dc72019-07-29 18:25:00 +01002059 if (!update_open_stateid(state, &data->o_res.stateid,
2060 NULL, data->o_arg.fmode)) {
2061 nfs4_put_open_state(state);
2062 state = ERR_PTR(-EAGAIN);
2063 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002064out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08002065 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002066 return state;
2067}
2068
Andy Adamsone23008e2012-10-02 21:07:32 -04002069static struct nfs4_state *
2070nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
2071{
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002072 struct nfs4_state *ret;
2073
Andy Adamsone23008e2012-10-02 21:07:32 -04002074 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002075 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
2076 else
2077 ret = _nfs4_opendata_to_nfs4_state(data);
2078 nfs4_sequence_free_slot(&data->o_res.seq_res);
2079 return ret;
Andy Adamsone23008e2012-10-02 21:07:32 -04002080}
2081
Trond Myklebust0de43972018-09-02 15:57:01 -04002082static struct nfs_open_context *
2083nfs4_state_find_open_context_mode(struct nfs4_state *state, fmode_t mode)
Trond Myklebust864472e2006-01-03 09:55:15 +01002084{
2085 struct nfs_inode *nfsi = NFS_I(state->inode);
2086 struct nfs_open_context *ctx;
2087
Trond Myklebust0de43972018-09-02 15:57:01 -04002088 rcu_read_lock();
2089 list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
Trond Myklebust864472e2006-01-03 09:55:15 +01002090 if (ctx->state != state)
2091 continue;
Trond Myklebust0de43972018-09-02 15:57:01 -04002092 if ((ctx->mode & mode) != mode)
2093 continue;
2094 if (!get_nfs_open_context(ctx))
2095 continue;
2096 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01002097 return ctx;
2098 }
Trond Myklebust0de43972018-09-02 15:57:01 -04002099 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01002100 return ERR_PTR(-ENOENT);
2101}
2102
Trond Myklebust0de43972018-09-02 15:57:01 -04002103static struct nfs_open_context *
2104nfs4_state_find_open_context(struct nfs4_state *state)
2105{
2106 struct nfs_open_context *ctx;
2107
2108 ctx = nfs4_state_find_open_context_mode(state, FMODE_READ|FMODE_WRITE);
2109 if (!IS_ERR(ctx))
2110 return ctx;
2111 ctx = nfs4_state_find_open_context_mode(state, FMODE_WRITE);
2112 if (!IS_ERR(ctx))
2113 return ctx;
2114 return nfs4_state_find_open_context_mode(state, FMODE_READ);
2115}
2116
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002117static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
2118 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002119{
2120 struct nfs4_opendata *opendata;
2121
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002122 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002123 NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002124 if (opendata == NULL)
2125 return ERR_PTR(-ENOMEM);
2126 opendata->state = state;
Trond Myklebustace9fad2018-09-02 19:19:07 -04002127 refcount_inc(&state->count);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002128 return opendata;
2129}
2130
Trond Myklebust24311f82015-09-20 10:50:17 -04002131static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
2132 fmode_t fmode)
Trond Myklebust864472e2006-01-03 09:55:15 +01002133{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002134 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01002135 int ret;
2136
Trond Myklebust24311f82015-09-20 10:50:17 -04002137 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
NeilBrown39f897f2015-06-29 14:28:54 +10002138 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002139 opendata->o_arg.open_flags = 0;
2140 opendata->o_arg.fmode = fmode;
Trond Myklebustbe36e182015-02-27 17:04:17 -05002141 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
2142 NFS_SB(opendata->dentry->d_sb),
2143 fmode, 0);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002144 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
2145 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
2146 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002147 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002148 if (ret != 0)
2149 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002150 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002151 if (IS_ERR(newstate))
2152 return PTR_ERR(newstate);
Trond Myklebust24311f82015-09-20 10:50:17 -04002153 if (newstate != opendata->state)
2154 ret = -ESTALE;
Al Viro643168c2011-06-22 18:20:23 -04002155 nfs4_close_state(newstate, fmode);
Trond Myklebust24311f82015-09-20 10:50:17 -04002156 return ret;
Trond Myklebust864472e2006-01-03 09:55:15 +01002157}
2158
2159static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
2160{
Trond Myklebust864472e2006-01-03 09:55:15 +01002161 int ret;
2162
2163 /* memory barrier prior to reading state->n_* */
2164 smp_rmb();
Trond Myklebust24311f82015-09-20 10:50:17 -04002165 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2166 if (ret != 0)
2167 return ret;
2168 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2169 if (ret != 0)
2170 return ret;
2171 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
2172 if (ret != 0)
2173 return ret;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002174 /*
2175 * We may have performed cached opens for all three recoveries.
2176 * Check if we need to update the current stateid.
2177 */
2178 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05002179 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04002180 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002181 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05002182 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04002183 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002184 }
Trond Myklebust864472e2006-01-03 09:55:15 +01002185 return 0;
2186}
2187
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188/*
2189 * OPEN_RECLAIM:
2190 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002192static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002194 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01002195 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002196 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 int status;
2198
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002199 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2200 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002201 if (IS_ERR(opendata))
2202 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002203 rcu_read_lock();
2204 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05002205 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04002206 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002207 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01002208 opendata->o_arg.u.delegation_type = delegation_type;
2209 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002210 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 return status;
2212}
2213
Trond Myklebust539cd032007-06-05 11:46:42 -04002214static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215{
2216 struct nfs_server *server = NFS_SERVER(state->inode);
2217 struct nfs4_exception exception = { };
2218 int err;
2219 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002220 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002221 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002222 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2223 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04002224 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002225 break;
2226 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 } while (exception.retry);
2228 return err;
2229}
2230
Trond Myklebust864472e2006-01-03 09:55:15 +01002231static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
2232{
2233 struct nfs_open_context *ctx;
2234 int ret;
2235
2236 ctx = nfs4_state_find_open_context(state);
2237 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002238 return -EAGAIN;
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002239 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2240 nfs_state_clear_open_state_flags(state);
Trond Myklebust539cd032007-06-05 11:46:42 -04002241 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002242 put_nfs_open_context(ctx);
2243 return ret;
2244}
2245
NeilBrowndce26302017-12-13 09:57:09 +11002246static 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 -07002247{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002248 switch (err) {
2249 default:
2250 printk(KERN_ERR "NFS: %s: unhandled error "
2251 "%d.\n", __func__, err);
Gustavo A. R. Silvaffb81712020-11-20 12:26:46 -06002252 fallthrough;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002253 case 0:
2254 case -ENOENT:
Trond Myklebust8eee52a2015-06-04 13:51:13 -04002255 case -EAGAIN:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002256 case -ESTALE:
Trond Myklebust67e7b522019-08-07 07:31:27 -04002257 case -ETIMEDOUT:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002258 break;
2259 case -NFS4ERR_BADSESSION:
2260 case -NFS4ERR_BADSLOT:
2261 case -NFS4ERR_BAD_HIGH_SLOT:
2262 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
2263 case -NFS4ERR_DEADSESSION:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002264 return -EAGAIN;
2265 case -NFS4ERR_STALE_CLIENTID:
2266 case -NFS4ERR_STALE_STATEID:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002267 /* Don't recall a delegation if it was lost */
2268 nfs4_schedule_lease_recovery(server->nfs_client);
2269 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04002270 case -NFS4ERR_MOVED:
2271 nfs4_schedule_migration_recovery(server);
2272 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04002273 case -NFS4ERR_LEASE_MOVED:
2274 nfs4_schedule_lease_moved_recovery(server->nfs_client);
2275 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002276 case -NFS4ERR_DELEG_REVOKED:
2277 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust404ea3562016-09-22 13:39:08 -04002278 case -NFS4ERR_EXPIRED:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002279 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002280 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002281 nfs_inode_find_state_and_recover(state->inode,
2282 stateid);
2283 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05002284 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002285 case -NFS4ERR_DELAY:
2286 case -NFS4ERR_GRACE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002287 ssleep(1);
2288 return -EAGAIN;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002289 case -ENOMEM:
2290 case -NFS4ERR_DENIED:
NeilBrowndce26302017-12-13 09:57:09 +11002291 if (fl) {
2292 struct nfs4_lock_state *lsp = fl->fl_u.nfs4_fl.owner;
2293 if (lsp)
2294 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2295 }
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002296 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002297 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 return err;
2299}
2300
Trond Myklebust24311f82015-09-20 10:50:17 -04002301int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002302 struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002303{
2304 struct nfs_server *server = NFS_SERVER(state->inode);
2305 struct nfs4_opendata *opendata;
Trond Myklebust24311f82015-09-20 10:50:17 -04002306 int err = 0;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002307
2308 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2309 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2310 if (IS_ERR(opendata))
2311 return PTR_ERR(opendata);
2312 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002313 if (!test_bit(NFS_O_RDWR_STATE, &state->flags)) {
Trond Myklebust24311f82015-09-20 10:50:17 -04002314 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2315 if (err)
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002316 goto out;
2317 }
2318 if (!test_bit(NFS_O_WRONLY_STATE, &state->flags)) {
Trond Myklebust24311f82015-09-20 10:50:17 -04002319 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2320 if (err)
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002321 goto out;
Trond Myklebust24311f82015-09-20 10:50:17 -04002322 }
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002323 if (!test_bit(NFS_O_RDONLY_STATE, &state->flags)) {
2324 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2325 if (err)
2326 goto out;
2327 }
2328 nfs_state_clear_delegation(state);
2329out:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002330 nfs4_opendata_put(opendata);
NeilBrowndce26302017-12-13 09:57:09 +11002331 return nfs4_handle_delegation_recall_error(server, state, stateid, NULL, err);
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002332}
2333
Chuck Leverbe05c862013-08-09 12:49:47 -04002334static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2335{
2336 struct nfs4_opendata *data = calldata;
2337
Anna Schumaker7981c8a2017-01-10 11:39:53 -05002338 nfs4_setup_sequence(data->o_arg.server->nfs_client,
2339 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04002340}
2341
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002342static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2343{
2344 struct nfs4_opendata *data = calldata;
2345
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002346 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04002347
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002348 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002349 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05002350 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05002351 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002352 renew_lease(data->o_res.server, data->timestamp);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002353 data->rpc_done = true;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002354 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002355}
2356
2357static void nfs4_open_confirm_release(void *calldata)
2358{
2359 struct nfs4_opendata *data = calldata;
2360 struct nfs4_state *state = NULL;
2361
2362 /* If this request hasn't been cancelled, do nothing */
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002363 if (!data->cancelled)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002364 goto out_free;
2365 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002366 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002367 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002368 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002369 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002370 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002371out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002372 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002373}
2374
2375static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04002376 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002377 .rpc_call_done = nfs4_open_confirm_done,
2378 .rpc_release = nfs4_open_confirm_release,
2379};
2380
2381/*
2382 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2383 */
2384static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2385{
David Howells2b0143b2015-03-17 22:25:59 +00002386 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002387 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002388 struct rpc_message msg = {
2389 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2390 .rpc_argp = &data->c_arg,
2391 .rpc_resp = &data->c_res,
2392 .rpc_cred = data->owner->so_cred,
2393 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002394 struct rpc_task_setup task_setup_data = {
2395 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002396 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002397 .callback_ops = &nfs4_open_confirm_ops,
2398 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002399 .workqueue = nfsiod_workqueue,
Trond Myklebust61296502020-02-07 19:38:12 -05002400 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002401 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 int status;
2403
Anna Schumakerfba83f32018-05-04 16:22:50 -04002404 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1,
2405 data->is_recover);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002406 kref_get(&data->kref);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002407 data->rpc_done = false;
Trond Myklebust3e309912007-07-07 13:19:59 -04002408 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002409 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002410 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05002411 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002412 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002413 status = rpc_wait_for_completion_task(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002414 if (status != 0) {
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002415 data->cancelled = true;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002416 smp_wmb();
2417 } else
2418 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05002419 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 return status;
2421}
2422
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002423static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002425 struct nfs4_opendata *data = calldata;
2426 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04002427 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust2a606182015-08-19 22:30:00 -05002428 enum open_claim_type4 claim = data->o_arg.claim;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002429
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002430 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002431 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002432 /*
2433 * Check if we still need to send an OPEN call, or if we can use
2434 * a delegation instead.
2435 */
2436 if (data->state != NULL) {
2437 struct nfs_delegation *delegation;
2438
Trond Myklebustbe189f72018-09-27 17:12:33 -04002439 if (can_open_cached(data->state, data->o_arg.fmode,
2440 data->o_arg.open_flags, claim))
Trond Myklebust6ee41262007-07-08 14:11:36 -04002441 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002442 rcu_read_lock();
Trond Myklebustbe3df3d2019-10-31 18:40:32 -04002443 delegation = nfs4_get_valid_delegation(data->state->inode);
Trond Myklebust2a606182015-08-19 22:30:00 -05002444 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
Trond Myklebust652f89f2011-12-09 19:05:58 -05002445 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002446 rcu_read_unlock();
2447 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04002448 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04002449 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust2a606182015-08-19 22:30:00 -05002450 switch (claim) {
2451 default:
2452 break;
Trond Myklebust8188df12013-04-23 14:31:19 -04002453 case NFS4_OPEN_CLAIM_PREVIOUS:
2454 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2455 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04002456 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002457 fallthrough;
Trond Myklebust8188df12013-04-23 14:31:19 -04002458 case NFS4_OPEN_CLAIM_FH:
2459 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002460 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002461 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05002462 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
Andy Adamsond8985282009-04-01 09:22:21 -04002463 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002464 &data->o_res.seq_res,
2465 task) != 0)
2466 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04002467
2468 /* Set the create mode (note dependency on the session type) */
2469 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2470 if (data->o_arg.open_flags & O_EXCL) {
Trond Myklebusta1e7f302021-07-14 13:00:58 -04002471 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2472 if (clp->cl_mvops->minor_version == 0) {
2473 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2474 /* don't put an ACCESS op in OPEN compound if O_EXCL,
2475 * because ACCESS will return permission denied for
2476 * all bits until close */
2477 data->o_res.access_request = data->o_arg.access = 0;
2478 } else if (nfs4_has_persistent_session(clp))
Trond Myklebust549b19c2013-04-16 18:42:34 -04002479 data->o_arg.createmode = NFS4_CREATE_GUARDED;
Trond Myklebust549b19c2013-04-16 18:42:34 -04002480 }
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);
Anna Schumaker2ef61e02021-10-22 13:11:07 -04002711 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, NULL);
Trond Myklebust56e0d712017-04-15 19:20:01 -04002712 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002713 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714}
2715
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716/*
2717 * OPEN_EXPIRED:
2718 * reclaim state on the server after a network partition.
2719 * Assumes caller holds the appropriate lock
2720 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002721static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002723 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002724 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002726 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002727 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002728 if (IS_ERR(opendata))
2729 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002730 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002731 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002732 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002733 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002734 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735}
2736
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002737static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002738{
Trond Myklebust539cd032007-06-05 11:46:42 -04002739 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002740 struct nfs4_exception exception = { };
2741 int err;
2742
2743 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002744 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002745 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002746 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2747 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002748 switch (err) {
2749 default:
2750 goto out;
2751 case -NFS4ERR_GRACE:
2752 case -NFS4ERR_DELAY:
2753 nfs4_handle_exception(server, err, &exception);
2754 err = 0;
2755 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002756 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002757out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002758 return err;
2759}
2760
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2762{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002764 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765
Trond Myklebust864472e2006-01-03 09:55:15 +01002766 ctx = nfs4_state_find_open_context(state);
2767 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002768 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002769 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002770 put_nfs_open_context(ctx);
2771 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772}
2773
Trond Myklebust41020b62016-09-22 13:38:58 -04002774static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2775 const nfs4_stateid *stateid)
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002776{
Trond Myklebust41020b62016-09-22 13:38:58 -04002777 nfs_remove_bad_delegation(state->inode, stateid);
Trond Myklebust9f0c5122018-09-05 14:07:15 -04002778 nfs_state_clear_delegation(state);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002779}
2780
2781static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2782{
2783 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
Trond Myklebust41020b62016-09-22 13:38:58 -04002784 nfs_finish_clear_delegation_stateid(state, NULL);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002785}
2786
2787static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2788{
2789 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2790 nfs40_clear_delegation_stateid(state);
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002791 nfs_state_clear_open_state_flags(state);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002792 return nfs4_open_expired(sp, state);
2793}
2794
Trond Myklebust45870d62016-09-22 13:38:59 -04002795static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2796 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11002797 const struct cred *cred)
Trond Myklebust45870d62016-09-22 13:38:59 -04002798{
2799 return -NFS4ERR_BAD_STATEID;
2800}
2801
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002802#if defined(CONFIG_NFS_V4_1)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002803static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2804 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11002805 const struct cred *cred)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002806{
2807 int status;
2808
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002809 switch (stateid->type) {
2810 default:
2811 break;
2812 case NFS4_INVALID_STATEID_TYPE:
2813 case NFS4_SPECIAL_STATEID_TYPE:
2814 return -NFS4ERR_BAD_STATEID;
2815 case NFS4_REVOKED_STATEID_TYPE:
2816 goto out_free;
2817 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002818
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002819 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002820 switch (status) {
2821 case -NFS4ERR_EXPIRED:
2822 case -NFS4ERR_ADMIN_REVOKED:
2823 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002824 break;
2825 default:
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002826 return status;
2827 }
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002828out_free:
2829 /* Ack the revoked state to the server */
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04002830 nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002831 return -NFS4ERR_EXPIRED;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002832}
2833
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002834static int nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002835{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002836 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002837 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002838 struct nfs_delegation *delegation;
NeilBrowna52458b2018-12-03 11:30:31 +11002839 const struct cred *cred = NULL;
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002840 int status, ret = NFS_OK;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002841
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002842 /* Get the delegation credential for use by test/free_stateid */
2843 rcu_read_lock();
2844 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002845 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002846 rcu_read_unlock();
Trond Myklebust9f0c5122018-09-05 14:07:15 -04002847 nfs_state_clear_delegation(state);
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002848 return NFS_OK;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002849 }
2850
Trond Myklebustfc51b1c2020-04-02 15:27:09 -04002851 spin_lock(&delegation->lock);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002852 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002853
Trond Myklebust994b15b2018-09-05 14:07:14 -04002854 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2855 &delegation->flags)) {
Trond Myklebustfc51b1c2020-04-02 15:27:09 -04002856 spin_unlock(&delegation->lock);
Trond Myklebust994b15b2018-09-05 14:07:14 -04002857 rcu_read_unlock();
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002858 return NFS_OK;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002859 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002860
NeilBrowna52458b2018-12-03 11:30:31 +11002861 if (delegation->cred)
2862 cred = get_cred(delegation->cred);
Trond Myklebustfc51b1c2020-04-02 15:27:09 -04002863 spin_unlock(&delegation->lock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002864 rcu_read_unlock();
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002865 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002866 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002867 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
Trond Myklebust41020b62016-09-22 13:38:58 -04002868 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002869 else
2870 ret = status;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002871
Trond Myklebust8c39a392019-07-19 13:48:44 -04002872 put_cred(cred);
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002873 return ret;
2874}
2875
2876static void nfs41_delegation_recover_stateid(struct nfs4_state *state)
2877{
2878 nfs4_stateid tmp;
2879
2880 if (test_bit(NFS_DELEGATED_STATE, &state->flags) &&
2881 nfs4_copy_delegation_stateid(state->inode, state->state,
2882 &tmp, NULL) &&
2883 nfs4_stateid_match_other(&state->stateid, &tmp))
2884 nfs_state_set_delegation(state, &tmp, state->state);
2885 else
2886 nfs_state_clear_delegation(state);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002887}
2888
2889/**
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002890 * nfs41_check_expired_locks - possibly free a lock stateid
2891 *
2892 * @state: NFSv4 state for an inode
2893 *
2894 * Returns NFS_OK if recovery for this stateid is now finished.
2895 * Otherwise a negative NFS4ERR value is returned.
2896 */
2897static int nfs41_check_expired_locks(struct nfs4_state *state)
2898{
2899 int status, ret = NFS_OK;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002900 struct nfs4_lock_state *lsp, *prev = NULL;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002901 struct nfs_server *server = NFS_SERVER(state->inode);
2902
2903 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2904 goto out;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002905
2906 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002907 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2908 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
NeilBrowna52458b2018-12-03 11:30:31 +11002909 const struct cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002910
Elena Reshetova194bc1f2017-10-20 12:53:36 +03002911 refcount_inc(&lsp->ls_count);
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002912 spin_unlock(&state->state_lock);
2913
2914 nfs4_put_lock_state(prev);
2915 prev = lsp;
2916
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002917 status = nfs41_test_and_free_expired_stateid(server,
2918 &lsp->ls_stateid,
2919 cred);
2920 trace_nfs4_test_lock_stateid(state, lsp, status);
2921 if (status == -NFS4ERR_EXPIRED ||
2922 status == -NFS4ERR_BAD_STATEID) {
2923 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002924 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002925 if (!recover_lost_locks)
2926 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2927 } else if (status != NFS_OK) {
2928 ret = status;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002929 nfs4_put_lock_state(prev);
2930 goto out;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002931 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002932 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002933 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002934 }
2935 spin_unlock(&state->state_lock);
2936 nfs4_put_lock_state(prev);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002937out:
2938 return ret;
2939}
2940
2941/**
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002942 * nfs41_check_open_stateid - possibly free an open stateid
2943 *
2944 * @state: NFSv4 state for an inode
2945 *
2946 * Returns NFS_OK if recovery for this stateid is now finished.
2947 * Otherwise a negative NFS4ERR value is returned.
2948 */
2949static int nfs41_check_open_stateid(struct nfs4_state *state)
2950{
2951 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002952 nfs4_stateid *stateid = &state->open_stateid;
NeilBrowna52458b2018-12-03 11:30:31 +11002953 const struct cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002954 int status;
2955
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002956 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002957 return -NFS4ERR_BAD_STATEID;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002958 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002959 trace_nfs4_test_open_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002960 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002961 nfs_state_clear_open_state_flags(state);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002962 stateid->type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002963 return status;
Trond Myklebustc0ca0e52017-08-08 21:39:28 -04002964 }
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002965 if (nfs_open_stateid_recover_openmode(state))
2966 return -NFS4ERR_OPENMODE;
2967 return NFS_OK;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002968}
2969
2970static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2971{
Chuck Levereb64cf92012-07-11 16:30:05 -04002972 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002973
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002974 status = nfs41_check_delegation_stateid(state);
2975 if (status != NFS_OK)
2976 return status;
2977 nfs41_delegation_recover_stateid(state);
2978
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002979 status = nfs41_check_expired_locks(state);
2980 if (status != NFS_OK)
2981 return status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002982 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002983 if (status != NFS_OK)
2984 status = nfs4_open_expired(sp, state);
2985 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002986}
2987#endif
2988
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002990 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2991 * fields corresponding to attributes that were used to store the verifier.
2992 * Make sure we clobber those fields in the later setattr call
2993 */
Trond Myklebust609339c2018-03-28 16:18:17 -04002994static unsigned nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002995 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002996{
Trond Myklebust609339c2018-03-28 16:18:17 -04002997 const __u32 *bitmask = opendata->o_arg.server->exclcreat_bitmask;
2998 __u32 attrset[3];
2999 unsigned ret;
3000 unsigned i;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08003001
Trond Myklebust609339c2018-03-28 16:18:17 -04003002 for (i = 0; i < ARRAY_SIZE(attrset); i++) {
3003 attrset[i] = opendata->o_res.attrset[i];
3004 if (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE4_1)
3005 attrset[i] &= ~bitmask[i];
3006 }
Jeff Laytonaa53ed52007-06-05 14:49:03 -04003007
Trond Myklebust609339c2018-03-28 16:18:17 -04003008 ret = (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE) ?
3009 sattr->ia_valid : 0;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08003010
Trond Myklebust609339c2018-03-28 16:18:17 -04003011 if ((attrset[1] & (FATTR4_WORD1_TIME_ACCESS|FATTR4_WORD1_TIME_ACCESS_SET))) {
3012 if (sattr->ia_valid & ATTR_ATIME_SET)
3013 ret |= ATTR_ATIME_SET;
3014 else
3015 ret |= ATTR_ATIME;
3016 }
Kinglong Mee5334c5b2015-08-26 21:13:37 +08003017
Trond Myklebust609339c2018-03-28 16:18:17 -04003018 if ((attrset[1] & (FATTR4_WORD1_TIME_MODIFY|FATTR4_WORD1_TIME_MODIFY_SET))) {
3019 if (sattr->ia_valid & ATTR_MTIME_SET)
3020 ret |= ATTR_MTIME_SET;
3021 else
3022 ret |= ATTR_MTIME;
3023 }
3024
3025 if (!(attrset[2] & FATTR4_WORD2_SECURITY_LABEL))
Kinglong Mee5334c5b2015-08-26 21:13:37 +08003026 *label = NULL;
Trond Myklebust609339c2018-03-28 16:18:17 -04003027 return ret;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04003028}
3029
Trond Myklebustc21443c2013-02-07 14:26:21 -05003030static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003031 int flags, struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05003032{
3033 struct nfs4_state_owner *sp = opendata->owner;
3034 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04003035 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05003036 struct nfs4_state *state;
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003037 fmode_t acc_mode = _nfs4_ctx_to_accessmode(ctx);
Trond Myklebustcf5b4052020-02-05 09:01:53 -05003038 struct inode *dir = d_inode(opendata->dir);
3039 unsigned long dir_verifier;
Trond Myklebustc21443c2013-02-07 14:26:21 -05003040 unsigned int seq;
3041 int ret;
3042
3043 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
Trond Myklebustcf5b4052020-02-05 09:01:53 -05003044 dir_verifier = nfs_save_change_attribute(dir);
Trond Myklebustc21443c2013-02-07 14:26:21 -05003045
Fred Isaman3b65a302016-09-19 10:06:49 -04003046 ret = _nfs4_proc_open(opendata, ctx);
Trond Myklebustdca780012014-10-23 19:23:03 +03003047 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05003048 goto out;
3049
Trond Myklebustae55e592018-05-22 11:17:16 -04003050 state = _nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebustc21443c2013-02-07 14:26:21 -05003051 ret = PTR_ERR(state);
3052 if (IS_ERR(state))
3053 goto out;
Trond Myklebusta974dee2017-02-08 11:29:46 -05003054 ctx->state = state;
Trond Myklebustc21443c2013-02-07 14:26:21 -05003055 if (server->caps & NFS_CAP_POSIX_LOCK)
3056 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Jeff Laytona8ce3772016-09-17 18:17:35 -04003057 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
3058 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05003059
Trond Myklebust275bb302013-05-29 13:11:28 -04003060 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00003061 if (d_really_is_negative(dentry)) {
Al Viro668d0cd2016-03-08 12:44:17 -05003062 struct dentry *alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04003063 d_drop(dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05003064 alias = d_exact_alias(dentry, state->inode);
3065 if (!alias)
3066 alias = d_splice_alias(igrab(state->inode), dentry);
3067 /* d_splice_alias() can't fail here - it's a non-directory */
3068 if (alias) {
Trond Myklebust275bb302013-05-29 13:11:28 -04003069 dput(ctx->dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05003070 ctx->dentry = dentry = alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04003071 }
Trond Myklebustcf5b4052020-02-05 09:01:53 -05003072 }
3073
3074 switch(opendata->o_arg.claim) {
3075 default:
3076 break;
3077 case NFS4_OPEN_CLAIM_NULL:
3078 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
3079 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
3080 if (!opendata->rpc_done)
3081 break;
3082 if (opendata->o_res.delegation_type != 0)
3083 dir_verifier = nfs_save_change_attribute(dir);
3084 nfs_set_verifier(dentry, dir_verifier);
Trond Myklebust275bb302013-05-29 13:11:28 -04003085 }
3086
Trond Myklebustaf9b6d72018-06-29 12:45:53 -04003087 /* Parse layoutget results before we check for access */
3088 pnfs_parse_lgopen(state->inode, opendata->lgp, ctx);
3089
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003090 ret = nfs4_opendata_access(sp->so_cred, opendata, state,
3091 acc_mode, flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05003092 if (ret != 0)
3093 goto out;
3094
David Howells2b0143b2015-03-17 22:25:59 +00003095 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04003096 nfs_inode_attach_open_context(ctx);
3097 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
3098 nfs4_schedule_stateid_recovery(server, state);
3099 }
Fred Isaman2409a972016-10-06 12:11:21 -04003100
Trond Myklebustc21443c2013-02-07 14:26:21 -05003101out:
Olga Kornievskaia0cb98ab2019-03-19 12:12:13 -04003102 if (!opendata->cancelled)
3103 nfs4_sequence_free_slot(&opendata->o_res.seq_res);
Trond Myklebustc21443c2013-02-07 14:26:21 -05003104 return ret;
3105}
3106
Jeff Laytonaa53ed52007-06-05 14:49:03 -04003107/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01003108 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 */
Andy Adamson82be4172012-05-23 05:02:35 -04003110static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04003111 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04003112 int flags,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003113 const struct nfs4_open_createattrs *c,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003114 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115{
3116 struct nfs4_state_owner *sp;
3117 struct nfs4_state *state = NULL;
3118 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01003119 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04003120 struct dentry *dentry = ctx->dentry;
NeilBrowna52458b2018-12-03 11:30:31 +11003121 const struct cred *cred = ctx->cred;
Trond Myklebust4197a052013-05-29 12:37:49 -04003122 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003123 fmode_t fmode = _nfs4_ctx_to_openmode(ctx);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003124 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003125 struct iattr *sattr = c->sattr;
3126 struct nfs4_label *label = c->label;
Trond Myklebustaac00a82007-07-05 19:02:21 -04003127 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128
3129 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05003131 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
3132 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
3134 goto out_err;
3135 }
Anna Schumaker334f87d2017-01-11 16:17:17 -05003136 status = nfs4_client_recover_expired_lease(server->nfs_client);
Trond Myklebust58d97142006-01-03 09:55:24 +01003137 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003138 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00003139 if (d_really_is_positive(dentry))
3140 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01003141 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00003142 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003143 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003144 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
3145 c, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01003146 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05003147 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148
Trond Myklebuste911b812014-03-26 13:24:37 -07003149 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
3150 if (!opendata->f_attr.mdsthreshold) {
3151 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
3152 if (!opendata->f_attr.mdsthreshold)
Anna Schumaker76baa2b2021-10-22 13:11:06 -04003153 goto err_opendata_put;
Trond Myklebuste911b812014-03-26 13:24:37 -07003154 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04003155 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04003156 }
David Howells2b0143b2015-03-17 22:25:59 +00003157 if (d_really_is_positive(dentry))
3158 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04003159
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003160 status = _nfs4_open_and_get_state(opendata, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003161 if (status != 0)
Anna Schumaker76baa2b2021-10-22 13:11:06 -04003162 goto err_opendata_put;
Trond Myklebust3efb9722013-05-29 13:17:04 -04003163 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003164
NeilBrownefcbc042015-07-30 13:00:56 +10003165 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04003166 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Trond Myklebust609339c2018-03-28 16:18:17 -04003167 unsigned attrs = nfs4_exclusive_attrset(opendata, sattr, &label);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02003168 /*
3169 * send create attributes which was not set by open
3170 * with an extra setattr.
3171 */
Trond Myklebust609339c2018-03-28 16:18:17 -04003172 if (attrs || label) {
3173 unsigned ia_old = sattr->ia_valid;
3174
3175 sattr->ia_valid = attrs;
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02003176 nfs_fattr_init(opendata->o_res.f_attr);
3177 status = nfs4_do_setattr(state->inode, cred,
3178 opendata->o_res.f_attr, sattr,
Anna Schumaker76baa2b2021-10-22 13:11:06 -04003179 ctx, label, opendata->o_res.f_attr->label);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02003180 if (status == 0) {
3181 nfs_setattr_update_inode(state->inode, sattr,
3182 opendata->o_res.f_attr);
Anna Schumaker76baa2b2021-10-22 13:11:06 -04003183 nfs_setsecurity(state->inode, opendata->o_res.f_attr,
3184 opendata->o_res.f_attr->label);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02003185 }
Trond Myklebust609339c2018-03-28 16:18:17 -04003186 sattr->ia_valid = ia_old;
David Quigley1775fd32013-05-22 12:50:42 -04003187 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04003188 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08003189 if (opened && opendata->file_created)
Al Viro73a09dd2018-06-08 13:22:02 -04003190 *opened = 1;
Andy Adamson82be4172012-05-23 05:02:35 -04003191
Trond Myklebuste911b812014-03-26 13:24:37 -07003192 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04003193 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07003194 opendata->f_attr.mdsthreshold = NULL;
3195 }
Andy Adamson82be4172012-05-23 05:02:35 -04003196
Trond Myklebustc6d00e62007-06-17 16:02:44 -04003197 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04003200err_opendata_put:
3201 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01003202err_put_state_owner:
3203 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 return status;
3206}
3207
3208
Andy Adamson82be4172012-05-23 05:02:35 -04003209static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04003210 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04003211 int flags,
3212 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003213 struct nfs4_label *label,
3214 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003216 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust0688e642019-04-07 13:59:09 -04003217 struct nfs4_exception exception = {
3218 .interruptible = true,
3219 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 struct nfs4_state *res;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003221 struct nfs4_open_createattrs c = {
3222 .label = label,
3223 .sattr = sattr,
3224 .verf = {
3225 [0] = (__u32)jiffies,
3226 [1] = (__u32)current->pid,
3227 },
3228 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 int status;
3230
3231 do {
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003232 status = _nfs4_do_open(dir, ctx, flags, &c, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04003233 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04003234 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235 if (status == 0)
3236 break;
3237 /* NOTE: BAD_SEQID means the server and client disagree about the
3238 * book-keeping w.r.t. state-changing operations
3239 * (OPEN/CLOSE/LOCK/LOCKU...)
3240 * It is actually a sign of a bug on the client or on the server.
3241 *
3242 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07003243 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244 * have unhashed the old state_owner for us, and that we can
3245 * therefore safely retry using a new one. We should still warn
3246 * the user though...
3247 */
3248 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04003249 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04003250 " returned a bad sequence-id error!\n",
3251 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 exception.retry = 1;
3253 continue;
3254 }
Trond Myklebust550f5742005-10-18 14:20:21 -07003255 /*
3256 * BAD_STATEID on OPEN means that the server cancelled our
3257 * state before it received the OPEN_CONFIRM.
3258 * Recover by retrying the request as per the discussion
3259 * on Page 181 of RFC3530.
3260 */
3261 if (status == -NFS4ERR_BAD_STATEID) {
3262 exception.retry = 1;
3263 continue;
3264 }
Robert Milkowski924491f2020-01-28 08:37:47 +00003265 if (status == -NFS4ERR_EXPIRED) {
3266 nfs4_schedule_lease_recovery(server->nfs_client);
3267 exception.retry = 1;
3268 continue;
3269 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04003270 if (status == -EAGAIN) {
3271 /* We must have found a delegation */
3272 exception.retry = 1;
3273 continue;
3274 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003275 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
3276 continue;
3277 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 status, &exception));
3279 } while (exception.retry);
3280 return res;
3281}
3282
Trond Myklebust8487c472016-06-26 08:44:35 -04003283static int _nfs4_do_setattr(struct inode *inode,
3284 struct nfs_setattrargs *arg,
3285 struct nfs_setattrres *res,
NeilBrowna52458b2018-12-03 11:30:31 +11003286 const struct cred *cred,
NeilBrown29b59f92016-10-13 15:26:47 +11003287 struct nfs_open_context *ctx)
Trond Myklebust8487c472016-06-26 08:44:35 -04003288{
3289 struct nfs_server *server = NFS_SERVER(inode);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003290 struct rpc_message msg = {
Trond Myklebust8487c472016-06-26 08:44:35 -04003291 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
3292 .rpc_argp = arg,
3293 .rpc_resp = res,
3294 .rpc_cred = cred,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003295 };
NeilBrowna52458b2018-12-03 11:30:31 +11003296 const struct cred *delegation_cred = NULL;
Trond Myklebust8487c472016-06-26 08:44:35 -04003297 unsigned long timestamp = jiffies;
Trond Myklebust8487c472016-06-26 08:44:35 -04003298 bool truncate;
3299 int status;
3300
3301 nfs_fattr_init(res->fattr);
3302
3303 /* Servers should only apply open mode checks for file size changes */
3304 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
Chuck Lever644c9f42020-09-04 17:39:12 -04003305 if (!truncate) {
3306 nfs4_inode_make_writeable(inode);
Trond Myklebust991eedb2018-04-09 11:15:30 -04003307 goto zero_stateid;
Chuck Lever644c9f42020-09-04 17:39:12 -04003308 }
Trond Myklebust8487c472016-06-26 08:44:35 -04003309
Trond Myklebust991eedb2018-04-09 11:15:30 -04003310 if (nfs4_copy_delegation_stateid(inode, FMODE_WRITE, &arg->stateid, &delegation_cred)) {
Trond Myklebust8487c472016-06-26 08:44:35 -04003311 /* Use that stateid */
Trond Myklebust09a54f02019-08-03 10:28:18 -04003312 } else if (ctx != NULL && ctx->state) {
NeilBrown17393472016-10-13 15:26:47 +11003313 struct nfs_lock_context *l_ctx;
NeilBrown29b59f92016-10-13 15:26:47 +11003314 if (!nfs4_valid_open_stateid(ctx->state))
Trond Myklebust8487c472016-06-26 08:44:35 -04003315 return -EBADF;
NeilBrown17393472016-10-13 15:26:47 +11003316 l_ctx = nfs_get_lock_context(ctx);
3317 if (IS_ERR(l_ctx))
3318 return PTR_ERR(l_ctx);
NeilBrown7a0566b2016-12-06 15:50:06 -05003319 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
3320 &arg->stateid, &delegation_cred);
3321 nfs_put_lock_context(l_ctx);
3322 if (status == -EIO)
Trond Myklebust8487c472016-06-26 08:44:35 -04003323 return -EBADF;
Olga Kornievskaiad826e5b2019-12-18 16:50:42 -05003324 else if (status == -EAGAIN)
3325 goto zero_stateid;
Trond Myklebust991eedb2018-04-09 11:15:30 -04003326 } else {
3327zero_stateid:
Trond Myklebust8487c472016-06-26 08:44:35 -04003328 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
Trond Myklebust991eedb2018-04-09 11:15:30 -04003329 }
Trond Myklebust8487c472016-06-26 08:44:35 -04003330 if (delegation_cred)
3331 msg.rpc_cred = delegation_cred;
3332
3333 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
3334
NeilBrowna52458b2018-12-03 11:30:31 +11003335 put_cred(delegation_cred);
NeilBrown29b59f92016-10-13 15:26:47 +11003336 if (status == 0 && ctx != NULL)
Trond Myklebust8487c472016-06-26 08:44:35 -04003337 renew_lease(server, timestamp);
3338 trace_nfs4_setattr(inode, &arg->stateid, status);
3339 return status;
3340}
3341
NeilBrowna52458b2018-12-03 11:30:31 +11003342static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
Trond Myklebust8487c472016-06-26 08:44:35 -04003343 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11003344 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
Trond Myklebust8487c472016-06-26 08:44:35 -04003345 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05003347 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebust30846df2018-04-07 13:44:28 -04003348 __u32 bitmask[NFS4_BITMASK_SZ];
NeilBrown29b59f92016-10-13 15:26:47 +11003349 struct nfs4_state *state = ctx ? ctx->state : NULL;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003350 struct nfs_setattrargs arg = {
3351 .fh = NFS_FH(inode),
3352 .iap = sattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353 .server = server,
Trond Myklebust30846df2018-04-07 13:44:28 -04003354 .bitmask = bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04003355 .label = ilabel,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003356 };
3357 struct nfs_setattrres res = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003359 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 .server = server,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003361 };
Trond Myklebust8487c472016-06-26 08:44:35 -04003362 struct nfs4_exception exception = {
3363 .state = state,
3364 .inode = inode,
3365 .stateid = &arg.stateid,
3366 };
Trond Myklebusta71029b2021-04-10 00:23:03 -04003367 unsigned long adjust_flags = NFS_INO_INVALID_CHANGE;
Trond Myklebust8487c472016-06-26 08:44:35 -04003368 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369
Trond Myklebust720869e2021-04-13 09:41:16 -04003370 if (sattr->ia_valid & (ATTR_MODE | ATTR_KILL_SUID | ATTR_KILL_SGID))
3371 adjust_flags |= NFS_INO_INVALID_MODE;
3372 if (sattr->ia_valid & (ATTR_UID | ATTR_GID))
Trond Myklebusta71029b2021-04-10 00:23:03 -04003373 adjust_flags |= NFS_INO_INVALID_OTHER;
3374
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 do {
Trond Myklebusta71029b2021-04-10 00:23:03 -04003376 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, olabel),
3377 inode, adjust_flags);
Trond Myklebust30846df2018-04-07 13:44:28 -04003378
NeilBrown29b59f92016-10-13 15:26:47 +11003379 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
Trond Myklebust451146b2012-04-18 16:29:11 -04003380 switch (err) {
3381 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04003382 if (!(sattr->ia_valid & ATTR_SIZE)) {
3383 pr_warn_once("NFSv4: server %s is incorrectly "
3384 "applying open mode checks to "
3385 "a SETATTR that is not "
3386 "changing file size.\n",
3387 server->nfs_client->cl_hostname);
3388 }
Trond Myklebust451146b2012-04-18 16:29:11 -04003389 if (state && !(state->state & FMODE_WRITE)) {
3390 err = -EBADF;
3391 if (sattr->ia_valid & ATTR_OPEN)
3392 err = -EACCES;
3393 goto out;
3394 }
3395 }
3396 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04003398out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399 return err;
3400}
3401
Peng Tao500d7012015-09-22 11:35:22 +08003402static bool
3403nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3404{
3405 if (inode == NULL || !nfs_have_layout(inode))
3406 return false;
3407
3408 return pnfs_wait_on_layoutreturn(inode, task);
3409}
3410
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003411/*
3412 * Update the seqid of an open stateid
3413 */
3414static void nfs4_sync_open_stateid(nfs4_stateid *dst,
3415 struct nfs4_state *state)
3416{
3417 __be32 seqid_open;
3418 u32 dst_seqid;
3419 int seq;
3420
3421 for (;;) {
3422 if (!nfs4_valid_open_stateid(state))
3423 break;
3424 seq = read_seqbegin(&state->seqlock);
3425 if (!nfs4_state_match_open_stateid_other(state, dst)) {
3426 nfs4_stateid_copy(dst, &state->open_stateid);
3427 if (read_seqretry(&state->seqlock, seq))
3428 continue;
3429 break;
3430 }
3431 seqid_open = state->open_stateid.seqid;
3432 if (read_seqretry(&state->seqlock, seq))
3433 continue;
3434
3435 dst_seqid = be32_to_cpu(dst->seqid);
3436 if ((s32)(dst_seqid - be32_to_cpu(seqid_open)) < 0)
3437 dst->seqid = seqid_open;
3438 break;
3439 }
3440}
3441
3442/*
3443 * Update the seqid of an open stateid after receiving
3444 * NFS4ERR_OLD_STATEID
3445 */
3446static bool nfs4_refresh_open_old_stateid(nfs4_stateid *dst,
3447 struct nfs4_state *state)
3448{
3449 __be32 seqid_open;
3450 u32 dst_seqid;
3451 bool ret;
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04003452 int seq, status = -EAGAIN;
3453 DEFINE_WAIT(wait);
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003454
3455 for (;;) {
3456 ret = false;
3457 if (!nfs4_valid_open_stateid(state))
3458 break;
3459 seq = read_seqbegin(&state->seqlock);
3460 if (!nfs4_state_match_open_stateid_other(state, dst)) {
3461 if (read_seqretry(&state->seqlock, seq))
3462 continue;
3463 break;
3464 }
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04003465
3466 write_seqlock(&state->seqlock);
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003467 seqid_open = state->open_stateid.seqid;
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003468
3469 dst_seqid = be32_to_cpu(dst->seqid);
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04003470
3471 /* Did another OPEN bump the state's seqid? try again: */
3472 if ((s32)(be32_to_cpu(seqid_open) - dst_seqid) > 0) {
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003473 dst->seqid = seqid_open;
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04003474 write_sequnlock(&state->seqlock);
3475 ret = true;
3476 break;
3477 }
3478
3479 /* server says we're behind but we haven't seen the update yet */
3480 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
3481 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
3482 write_sequnlock(&state->seqlock);
3483 trace_nfs4_close_stateid_update_wait(state->inode, dst, 0);
3484
zhouchuangaobb002382021-05-09 19:34:37 -07003485 if (fatal_signal_pending(current))
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04003486 status = -EINTR;
3487 else
3488 if (schedule_timeout(5*HZ) != 0)
3489 status = 0;
3490
3491 finish_wait(&state->waitq, &wait);
3492
3493 if (!status)
3494 continue;
3495 if (status == -EINTR)
3496 break;
3497
3498 /* we slept the whole 5 seconds, we must have lost a seqid */
3499 dst->seqid = cpu_to_be32(dst_seqid + 1);
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003500 ret = true;
3501 break;
3502 }
3503
3504 return ret;
3505}
3506
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507struct nfs4_closedata {
3508 struct inode *inode;
3509 struct nfs4_state *state;
3510 struct nfs_closeargs arg;
3511 struct nfs_closeres res;
Trond Myklebustcf805162016-11-15 14:56:07 -05003512 struct {
3513 struct nfs4_layoutreturn_args arg;
3514 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003515 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebustcf805162016-11-15 14:56:07 -05003516 u32 roc_barrier;
3517 bool roc;
3518 } lr;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003519 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003520 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521};
3522
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003523static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07003524{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003525 struct nfs4_closedata *calldata = data;
3526 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04003527 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07003528
Trond Myklebustcf805162016-11-15 14:56:07 -05003529 if (calldata->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003530 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3531 calldata->res.lr_ret);
Trond Myklebust95121352005-10-18 14:20:12 -07003532 nfs4_put_open_state(calldata->state);
3533 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07003534 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05003535 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003536 kfree(calldata);
3537}
3538
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003539static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003541 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003544 nfs4_stateid *res_stateid = NULL;
Trond Myklebustb8b8d222017-11-07 10:51:37 -05003545 struct nfs4_exception exception = {
3546 .state = state,
3547 .inode = calldata->inode,
3548 .stateid = &calldata->arg.stateid,
3549 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550
Trond Myklebust14516c32010-07-31 14:29:06 -04003551 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3552 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04003553 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Trond Myklebustcf805162016-11-15 14:56:07 -05003554
3555 /* Handle Layoutreturn errors */
Trond Myklebust078000d2021-01-04 13:18:03 -05003556 if (pnfs_roc_done(task, &calldata->arg.lr_args, &calldata->res.lr_res,
3557 &calldata->res.lr_ret) == -EAGAIN)
Trond Myklebust287a9c52019-09-20 07:23:41 -04003558 goto out_restart;
Trond Myklebustcf805162016-11-15 14:56:07 -05003559
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003560 /* hmm. we are done with the inode, and in the process of freeing
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561 * the state_owner. we keep this around to process errors
3562 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563 switch (task->tk_status) {
3564 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04003565 res_stateid = &calldata->res.stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003566 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003567 break;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003568 case -NFS4ERR_ACCESS:
3569 if (calldata->arg.bitmask != NULL) {
3570 calldata->arg.bitmask = NULL;
3571 calldata->res.fattr = NULL;
Trond Myklebust91b30d22017-11-06 15:28:09 -05003572 goto out_restart;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003573
3574 }
3575 break;
Trond Myklebust12f275c2017-11-06 15:28:05 -05003576 case -NFS4ERR_OLD_STATEID:
3577 /* Did we race with OPEN? */
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003578 if (nfs4_refresh_open_old_stateid(&calldata->arg.stateid,
Trond Myklebust91b30d22017-11-06 15:28:09 -05003579 state))
3580 goto out_restart;
Trond Myklebust12f275c2017-11-06 15:28:05 -05003581 goto out_release;
Trond Myklebust69794ad2013-11-20 12:57:19 -05003582 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583 case -NFS4ERR_STALE_STATEID:
Trond Myklebust26d36302016-09-22 13:39:05 -04003584 case -NFS4ERR_EXPIRED:
3585 nfs4_free_revoked_stateid(server,
3586 &calldata->arg.stateid,
3587 task->tk_msg.rpc_cred);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05003588 fallthrough;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003589 case -NFS4ERR_BAD_STATEID:
Trond Myklebuste217e822019-09-20 07:23:46 -04003590 if (calldata->arg.fmode == 0)
3591 break;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05003592 fallthrough;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593 default:
Trond Myklebustb8b8d222017-11-07 10:51:37 -05003594 task->tk_status = nfs4_async_handle_exception(task,
3595 server, task->tk_status, &exception);
3596 if (exception.retry)
Trond Myklebust91b30d22017-11-06 15:28:09 -05003597 goto out_restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07003599 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3600 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003601out_release:
Trond Myklebust91b30d22017-11-06 15:28:09 -05003602 task->tk_status = 0;
Trond Myklebust72211db2009-12-15 14:47:36 -05003603 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustd8d84982016-12-19 12:14:44 -05003604 nfs_refresh_inode(calldata->inode, &calldata->fattr);
Chuck Lever86882c72021-10-16 18:03:04 -04003605 dprintk("%s: ret = %d\n", __func__, task->tk_status);
Trond Myklebust91b30d22017-11-06 15:28:09 -05003606 return;
Trond Myklebust91b30d22017-11-06 15:28:09 -05003607out_restart:
3608 task->tk_status = 0;
3609 rpc_restart_call_prepare(task);
3610 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611}
3612
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003613static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003615 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07003616 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003617 struct inode *inode = calldata->inode;
Trond Myklebust332d1a02021-03-25 18:15:36 -04003618 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustc8bf7072018-06-15 16:31:02 -04003619 struct pnfs_layout_hdr *lo;
Trond Myklebustaee7af32014-08-25 22:33:12 -04003620 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05003621 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07003622
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003623 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003624 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04003625
Trond Myklebust88069f72009-12-08 08:33:16 -05003626 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05003627 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04003628 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3629 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3630 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust003707c2007-07-05 18:07:55 -04003631 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003632 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01003633 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003634 if (state->n_rdonly == 0)
3635 call_close |= is_rdonly;
3636 else if (is_rdonly)
3637 calldata->arg.fmode |= FMODE_READ;
3638 if (state->n_wronly == 0)
3639 call_close |= is_wronly;
3640 else if (is_wronly)
3641 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebuste547f262016-06-25 19:19:28 -04003642 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3643 call_close |= is_rdwr;
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003644 } else if (is_rdwr)
3645 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3646
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003647 nfs4_sync_open_stateid(&calldata->arg.stateid, state);
3648 if (!nfs4_valid_open_stateid(state))
Trond Myklebust5d422302013-03-14 16:57:48 -04003649 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05003650 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05003651
3652 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003653 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003654 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07003655 }
Trond Myklebust88069f72009-12-08 08:33:16 -05003656
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003657 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
Peng Tao500d7012015-09-22 11:35:22 +08003658 nfs_release_seqid(calldata->arg.seqid);
3659 goto out_wait;
3660 }
3661
Trond Myklebustc8bf7072018-06-15 16:31:02 -04003662 lo = calldata->arg.lr_args ? calldata->arg.lr_args->layout : NULL;
3663 if (lo && !pnfs_layout_is_valid(lo)) {
3664 calldata->arg.lr_args = NULL;
3665 calldata->res.lr_res = NULL;
3666 }
3667
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003668 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05003669 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003670
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003671 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003672 /* Close-to-open cache consistency revalidation */
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04003673 if (!nfs4_have_delegation(inode, FMODE_READ)) {
Trond Myklebust332d1a02021-03-25 18:15:36 -04003674 nfs4_bitmask_set(calldata->arg.bitmask_store,
3675 server->cache_consistency_bitmask,
3676 inode, server, NULL);
3677 calldata->arg.bitmask = calldata->arg.bitmask_store;
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04003678 } else
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003679 calldata->arg.bitmask = NULL;
3680 }
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003681
Trond Myklebust6ae37332015-01-30 14:21:14 -05003682 calldata->arg.share_access =
3683 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3684 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05003685
Trond Myklebustd8d84982016-12-19 12:14:44 -05003686 if (calldata->res.fattr == NULL)
3687 calldata->arg.bitmask = NULL;
3688 else if (calldata->arg.bitmask == NULL)
3689 calldata->res.fattr = NULL;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003690 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05003691 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
Trond Myklebust9d12b212012-01-17 22:04:25 -05003692 &calldata->arg.seq_args,
3693 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04003694 task) != 0)
3695 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003696 return;
3697out_no_action:
3698 task->tk_action = NULL;
3699out_wait:
3700 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701}
3702
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003703static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003704 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003705 .rpc_call_done = nfs4_close_done,
3706 .rpc_release = nfs4_free_closedata,
3707};
3708
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709/*
3710 * It is possible for data to be read/written from a mem-mapped file
3711 * after the sys_close call (which hits the vfs layer as a flush).
3712 * This means that we can't safely call nfsv4 close on a file until
3713 * the inode is cleared. This in turn means that we are not good
3714 * NFSv4 citizens - we do not indicate to the server to update the file's
3715 * share state even when we are done with one of the three share
3716 * stateid's in the inode.
3717 *
3718 * NOTE: Caller must be holding the sp->so_owner semaphore!
3719 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04003720int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003722 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05003723 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04003725 struct nfs4_state_owner *sp = state->owner;
3726 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003727 struct rpc_message msg = {
3728 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3729 .rpc_cred = state->owner->so_cred,
3730 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003731 struct rpc_task_setup task_setup_data = {
3732 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003733 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003734 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003735 .workqueue = nfsiod_workqueue,
Trond Myklebust61296502020-02-07 19:38:12 -05003736 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003737 };
Trond Myklebust95121352005-10-18 14:20:12 -07003738 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739
Olga Kornievskaia85e39fe2021-06-23 23:28:51 -04003740 if (server->nfs_client->cl_minorversion)
3741 task_setup_data.flags |= RPC_TASK_MOVEABLE;
3742
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04003743 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3744 &task_setup_data.rpc_client, &msg);
3745
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003746 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07003748 goto out;
Anna Schumakerfba83f32018-05-04 16:22:50 -04003749 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1, 0);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003750 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003752 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebustc82bac62017-11-06 15:28:06 -05003753 if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state))
3754 goto out_free_calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05003756 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3757 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05003758 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07003759 goto out_free_calldata;
Trond Myklebustd8d84982016-12-19 12:14:44 -05003760 nfs_fattr_init(&calldata->fattr);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003761 calldata->arg.fmode = 0;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003762 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003763 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003764 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003765 calldata->res.server = server;
Trond Myklebustcf805162016-11-15 14:56:07 -05003766 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003767 calldata->lr.roc = pnfs_roc(state->inode,
3768 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3769 if (calldata->lr.roc) {
3770 calldata->arg.lr_args = &calldata->lr.arg;
3771 calldata->res.lr_res = &calldata->lr.res;
3772 }
Al Viro643168c2011-06-22 18:20:23 -04003773 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003774
Trond Myklebust1174dd12010-12-21 10:52:24 -05003775 msg.rpc_argp = &calldata->arg;
3776 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003777 task_setup_data.callback_data = calldata;
3778 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003779 if (IS_ERR(task))
3780 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003781 status = 0;
3782 if (wait)
3783 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003784 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003785 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003786out_free_calldata:
3787 kfree(calldata);
3788out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003789 nfs4_put_open_state(state);
3790 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003791 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792}
3793
Trond Myklebust2b484292010-09-17 10:56:51 -04003794static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003795nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3796 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003799 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3800
3801 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802
Trond Myklebust565277f2007-10-15 18:17:53 -04003803 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003804 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003805
3806 nfs4_label_release_security(label);
3807
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003808 if (IS_ERR(state))
3809 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003810 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811}
3812
Trond Myklebust1185a552009-12-03 15:54:02 -05003813static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003814{
3815 if (ctx->state == NULL)
3816 return;
3817 if (is_sync)
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003818 nfs4_close_sync(ctx->state, _nfs4_ctx_to_openmode(ctx));
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003819 else
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003820 nfs4_close_state(ctx->state, _nfs4_ctx_to_openmode(ctx));
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003821}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822
Trond Myklebustb944dba2013-11-04 15:20:20 -05003823#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3824#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
Frank van der Lindenb78ef842020-06-23 22:38:55 +00003825#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_XATTR_SUPPORT - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003826
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3828{
Kinglong Mee8c612822015-08-26 21:12:58 +08003829 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003830 struct nfs4_server_caps_arg args = {
3831 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003832 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003833 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834 struct nfs4_server_caps_res res = {};
3835 struct rpc_message msg = {
3836 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003837 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838 .rpc_resp = &res,
3839 };
3840 int status;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003841 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842
Kinglong Mee8c612822015-08-26 21:12:58 +08003843 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3844 FATTR4_WORD0_FH_EXPIRE_TYPE |
3845 FATTR4_WORD0_LINK_SUPPORT |
3846 FATTR4_WORD0_SYMLINK_SUPPORT |
3847 FATTR4_WORD0_ACLSUPPORT;
3848 if (minorversion)
3849 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3850
Bryan Schumaker7c513052011-03-24 17:12:24 +00003851 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003853 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003854 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003855 case 0:
3856 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3857 res.attr_bitmask[2] = 0;
3858 break;
3859 case 1:
3860 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3861 break;
3862 case 2:
3863 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3864 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebustce62b112018-03-05 15:01:18 -05003866 server->caps &= ~(NFS_CAP_ACLS | NFS_CAP_HARDLINKS |
3867 NFS_CAP_SYMLINKS| NFS_CAP_SECURITY_LABEL);
3868 server->fattr_valid = NFS_ATTR_FATTR_V4;
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003869 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3870 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871 server->caps |= NFS_CAP_ACLS;
3872 if (res.has_links != 0)
3873 server->caps |= NFS_CAP_HARDLINKS;
3874 if (res.has_symlinks != 0)
3875 server->caps |= NFS_CAP_SYMLINKS;
Scott Mayhew0b4f1322021-06-02 13:13:11 -04003876#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3877 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3878 server->caps |= NFS_CAP_SECURITY_LABEL;
3879#endif
Trond Myklebustce62b112018-03-05 15:01:18 -05003880 if (!(res.attr_bitmask[0] & FATTR4_WORD0_FILEID))
3881 server->fattr_valid &= ~NFS_ATTR_FATTR_FILEID;
3882 if (!(res.attr_bitmask[1] & FATTR4_WORD1_MODE))
3883 server->fattr_valid &= ~NFS_ATTR_FATTR_MODE;
3884 if (!(res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS))
3885 server->fattr_valid &= ~NFS_ATTR_FATTR_NLINK;
3886 if (!(res.attr_bitmask[1] & FATTR4_WORD1_OWNER))
3887 server->fattr_valid &= ~(NFS_ATTR_FATTR_OWNER |
3888 NFS_ATTR_FATTR_OWNER_NAME);
3889 if (!(res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP))
3890 server->fattr_valid &= ~(NFS_ATTR_FATTR_GROUP |
3891 NFS_ATTR_FATTR_GROUP_NAME);
3892 if (!(res.attr_bitmask[1] & FATTR4_WORD1_SPACE_USED))
3893 server->fattr_valid &= ~NFS_ATTR_FATTR_SPACE_USED;
3894 if (!(res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS))
3895 server->fattr_valid &= ~NFS_ATTR_FATTR_ATIME;
3896 if (!(res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA))
3897 server->fattr_valid &= ~NFS_ATTR_FATTR_CTIME;
3898 if (!(res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY))
3899 server->fattr_valid &= ~NFS_ATTR_FATTR_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003900 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3901 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003902 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003903
Trond Myklebusta65318b2009-03-11 14:10:28 -04003904 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3905 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3906 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003907 server->cache_consistency_bitmask[2] = 0;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003908
3909 /* Avoid a regression due to buggy server */
3910 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3911 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
Kinglong Mee8c612822015-08-26 21:12:58 +08003912 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3913 sizeof(server->exclcreat_bitmask));
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003914
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003916 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003918
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919 return status;
3920}
3921
Trond Myklebust55a97592006-06-09 09:34:19 -04003922int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923{
Trond Myklebust0688e642019-04-07 13:59:09 -04003924 struct nfs4_exception exception = {
3925 .interruptible = true,
3926 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927 int err;
Anna Schumaker01dde762021-10-14 13:55:04 -04003928
3929 nfs4_server_set_init_caps(server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930 do {
3931 err = nfs4_handle_exception(server,
3932 _nfs4_server_capabilities(server, fhandle),
3933 &exception);
3934 } while (exception.retry);
3935 return err;
3936}
3937
3938static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3939 struct nfs_fsinfo *info)
3940{
David Quigleyaa9c2662013-05-22 12:50:44 -04003941 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003943 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944 };
3945 struct nfs4_lookup_res res = {
3946 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003947 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948 .fh = fhandle,
3949 };
3950 struct rpc_message msg = {
3951 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3952 .rpc_argp = &args,
3953 .rpc_resp = &res,
3954 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003955
David Quigleyaa9c2662013-05-22 12:50:44 -04003956 bitmask[0] = nfs4_fattr_bitmap[0];
3957 bitmask[1] = nfs4_fattr_bitmap[1];
3958 /*
3959 * Process the label in the upcoming getfattr
3960 */
3961 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3962
Trond Myklebust0e574af2005-10-27 22:12:38 -04003963 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003964 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965}
3966
3967static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3968 struct nfs_fsinfo *info)
3969{
Trond Myklebust0688e642019-04-07 13:59:09 -04003970 struct nfs4_exception exception = {
3971 .interruptible = true,
3972 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973 int err;
3974 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003975 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003976 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003977 switch (err) {
3978 case 0:
3979 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003980 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003981 default:
3982 err = nfs4_handle_exception(server, err, &exception);
3983 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003985out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986 return err;
3987}
3988
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003989static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3990 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3991{
Trond Myklebustc2190662013-08-26 19:23:04 -04003992 struct rpc_auth_create_args auth_args = {
3993 .pseudoflavor = flavor,
3994 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003995 struct rpc_auth *auth;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003996
Trond Myklebustc2190662013-08-26 19:23:04 -04003997 auth = rpcauth_create(&auth_args, server->client);
Anna Schumaker9df13362017-01-11 16:30:08 -05003998 if (IS_ERR(auth))
3999 return -EACCES;
4000 return nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00004001}
4002
Chuck Lever9a744ba2013-03-16 15:56:02 -04004003/*
4004 * Retry pseudoroot lookup with various security flavors. We do this when:
4005 *
4006 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
4007 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
4008 *
4009 * Returns zero on success, or a negative NFS4ERR value, or a
4010 * negative errno value.
4011 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04004012static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04004013 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014{
Chuck Lever9a744ba2013-03-16 15:56:02 -04004015 /* Per 3530bis 15.33.5 */
4016 static const rpc_authflavor_t flav_array[] = {
4017 RPC_AUTH_GSS_KRB5P,
4018 RPC_AUTH_GSS_KRB5I,
4019 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04004020 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04004021 RPC_AUTH_NULL,
4022 };
4023 int status = -EPERM;
4024 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04004026 if (server->auth_info.flavor_len > 0) {
4027 /* try each flavor specified by user */
4028 for (i = 0; i < server->auth_info.flavor_len; i++) {
4029 status = nfs4_lookup_root_sec(server, fhandle, info,
4030 server->auth_info.flavors[i]);
4031 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
4032 continue;
4033 break;
4034 }
4035 } else {
4036 /* no flavors specified by user, try default list */
4037 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
4038 status = nfs4_lookup_root_sec(server, fhandle, info,
4039 flav_array[i]);
4040 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
4041 continue;
4042 break;
4043 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00004044 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04004045
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04004046 /*
Colin Ian Kingd3787af2018-10-26 19:10:31 +01004047 * -EACCES could mean that the user doesn't have correct permissions
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04004048 * to access the mount. It could also mean that we tried to mount
4049 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
4050 * existing mount programs don't handle -EACCES very well so it should
4051 * be mapped to -EPERM instead.
4052 */
4053 if (status == -EACCES)
4054 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04004055 return status;
4056}
4057
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04004058/**
4059 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
4060 * @server: initialized nfs_server handle
4061 * @fhandle: we fill in the pseudo-fs root file handle
4062 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04004063 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04004064 *
4065 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04004066 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04004067int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04004068 struct nfs_fsinfo *info,
4069 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04004070{
Andre Przywarac7757072015-04-23 17:17:40 +01004071 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04004072
Andre Przywarac7757072015-04-23 17:17:40 +01004073 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04004074 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01004075
4076 if (auth_probe || status == NFS4ERR_WRONGSEC)
Trond Myklebust698c9372016-07-25 13:31:14 -04004077 status = server->nfs_client->cl_mvops->find_root_sec(server,
4078 fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04004079
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080 if (status == 0)
4081 status = nfs4_server_capabilities(server, fhandle);
4082 if (status == 0)
4083 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04004084
Trond Myklebustc12e87f2006-03-13 21:20:47 -08004085 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086}
4087
Bryan Schumakerbae36242012-05-10 15:07:31 -04004088static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
4089 struct nfs_fsinfo *info)
4090{
4091 int error;
4092 struct nfs_fattr *fattr = info->fattr;
4093
4094 error = nfs4_server_capabilities(server, mntfh);
4095 if (error < 0) {
4096 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
4097 return error;
4098 }
4099
Anna Schumaker2ef61e02021-10-22 13:11:07 -04004100 error = nfs4_proc_getattr(server, mntfh, fattr, NULL);
Bryan Schumakerbae36242012-05-10 15:07:31 -04004101 if (error < 0) {
4102 dprintk("nfs4_get_root: getattr error = %d\n", -error);
Scott Mayhew779df6a2020-03-03 17:58:37 -05004103 goto out;
Bryan Schumakerbae36242012-05-10 15:07:31 -04004104 }
4105
4106 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
4107 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
4108 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
4109
Scott Mayhew779df6a2020-03-03 17:58:37 -05004110out:
Bryan Schumakerbae36242012-05-10 15:07:31 -04004111 return error;
4112}
4113
Manoj Naik6b97fd32006-06-09 09:34:29 -04004114/*
4115 * Get locations and (maybe) other attributes of a referral.
4116 * Note that we'll actually follow the referral later when
4117 * we detect fsid mismatch in inode revalidation
4118 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004119static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
4120 const struct qstr *name, struct nfs_fattr *fattr,
4121 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04004122{
4123 int status = -ENOMEM;
4124 struct page *page = NULL;
4125 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04004126
4127 page = alloc_page(GFP_KERNEL);
4128 if (page == NULL)
4129 goto out;
4130 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
4131 if (locations == NULL)
4132 goto out;
4133
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004134 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04004135 if (status != 0)
4136 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04004137
4138 /*
4139 * If the fsid didn't change, this is a migration event, not a
4140 * referral. Cause us to drop into the exception handler, which
4141 * will kick off migration recovery.
4142 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04004143 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04004144 dprintk("%s: server did not return a different fsid for"
4145 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04004146 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04004147 goto out;
4148 }
Andy Adamson533eb462011-06-13 18:25:56 -04004149 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
4150 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04004151
Andy Adamson533eb462011-06-13 18:25:56 -04004152 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04004153 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04004154 memset(fhandle, 0, sizeof(struct nfs_fh));
4155out:
4156 if (page)
4157 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04004158 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04004159 return status;
4160}
4161
David Quigley1775fd32013-05-22 12:50:42 -04004162static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
Anna Schumaker2ef61e02021-10-22 13:11:07 -04004163 struct nfs_fattr *fattr, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164{
Trond Myklebust771734f2018-04-07 13:54:23 -04004165 __u32 bitmask[NFS4_BITMASK_SZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166 struct nfs4_getattr_arg args = {
4167 .fh = fhandle,
Trond Myklebust771734f2018-04-07 13:54:23 -04004168 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169 };
4170 struct nfs4_getattr_res res = {
4171 .fattr = fattr,
4172 .server = server,
4173 };
4174 struct rpc_message msg = {
4175 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
4176 .rpc_argp = &args,
4177 .rpc_resp = &res,
4178 };
Trond Myklebustc74dfe92020-01-06 15:39:37 -05004179 unsigned short task_flags = 0;
4180
Olga Kornievskaia85e39fe2021-06-23 23:28:51 -04004181 if (nfs4_has_session(server->nfs_client))
4182 task_flags = RPC_TASK_MOVEABLE;
4183
Trond Myklebustc74dfe92020-01-06 15:39:37 -05004184 /* Is this is an attribute revalidation, subject to softreval? */
4185 if (inode && (server->flags & NFS_MOUNT_SOFTREVAL))
4186 task_flags |= RPC_TASK_TIMEOUT;
David Quigleyaa9c2662013-05-22 12:50:44 -04004187
Anna Schumaker2ef61e02021-10-22 13:11:07 -04004188 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, fattr->label), inode, 0);
Trond Myklebust0e574af2005-10-27 22:12:38 -04004189 nfs_fattr_init(fattr);
Trond Myklebustc74dfe92020-01-06 15:39:37 -05004190 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
4191 return nfs4_do_call_sync(server->client, server, &msg,
4192 &args.seq_args, &res.seq_res, task_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193}
4194
Olga Kornievskaiaec4b0922019-10-08 16:33:53 -04004195int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
Anna Schumaker2ef61e02021-10-22 13:11:07 -04004196 struct nfs_fattr *fattr, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197{
Trond Myklebust0688e642019-04-07 13:59:09 -04004198 struct nfs4_exception exception = {
4199 .interruptible = true,
4200 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004201 int err;
4202 do {
Anna Schumaker2ef61e02021-10-22 13:11:07 -04004203 err = _nfs4_proc_getattr(server, fhandle, fattr, inode);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004204 trace_nfs4_getattr(server, fhandle, fattr, err);
4205 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206 &exception);
4207 } while (exception.retry);
4208 return err;
4209}
4210
4211/*
4212 * The file is not closed if it is opened due to the a request to change
4213 * the size of the file. The open call will not be needed once the
4214 * VFS layer lookup-intents are implemented.
4215 *
4216 * Close is called when the inode is destroyed.
4217 * If we haven't opened the file for O_WRONLY, we
4218 * need to in the size_change case to obtain a stateid.
4219 *
4220 * Got race?
4221 * Because OPEN is always done by name in nfsv4, it is
4222 * possible that we opened a different file by the same
4223 * name. We can recognize this race condition, but we
4224 * can't do anything about it besides returning an error.
4225 *
4226 * This will be fixed with VFS changes (lookup-intent).
4227 */
4228static int
4229nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
4230 struct iattr *sattr)
4231{
David Howells2b0143b2015-03-17 22:25:59 +00004232 struct inode *inode = d_inode(dentry);
NeilBrowna52458b2018-12-03 11:30:31 +11004233 const struct cred *cred = NULL;
NeilBrown29b59f92016-10-13 15:26:47 +11004234 struct nfs_open_context *ctx = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04004235 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236 int status;
4237
Peng Tao88ac8152014-09-12 11:04:10 +08004238 if (pnfs_ld_layoutret_on_setattr(inode) &&
4239 sattr->ia_valid & ATTR_SIZE &&
4240 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04004241 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04004242
Trond Myklebust0e574af2005-10-27 22:12:38 -04004243 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244
Andy Adamson26699402012-05-30 16:12:24 -04004245 /* Deal with open(O_TRUNC) */
4246 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03004247 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04004248
4249 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03004250 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04004251 return 0;
4252
Trond Myklebustd5308382005-11-04 15:33:38 -05004253 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04004254 if (sattr->ia_valid & ATTR_FILE) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00004255
Trond Myklebust659bfcd2008-06-10 19:39:41 -04004256 ctx = nfs_file_open_context(sattr->ia_file);
NeilBrown29b59f92016-10-13 15:26:47 +11004257 if (ctx)
Neil Brown504e5182008-10-16 14:15:16 +11004258 cred = ctx->cred;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04004259 }
4260
David Quigley14c43f72013-05-22 12:50:43 -04004261 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4262 if (IS_ERR(label))
4263 return PTR_ERR(label);
4264
Trond Myklebust199366f2018-03-20 16:43:18 -04004265 /* Return any delegations if we're going to change ACLs */
4266 if ((sattr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
Trond Myklebustc01d3642018-03-20 16:43:20 -04004267 nfs4_inode_make_writeable(inode);
Trond Myklebust199366f2018-03-20 16:43:18 -04004268
NeilBrown29b59f92016-10-13 15:26:47 +11004269 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04004270 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05004271 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04004272 nfs_setsecurity(inode, fattr, label);
4273 }
David Quigley14c43f72013-05-22 12:50:43 -04004274 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275 return status;
4276}
4277
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004278static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004279 struct dentry *dentry, struct nfs_fh *fhandle,
Anna Schumaker9558a002021-10-22 13:11:04 -04004280 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04004281{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004282 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04004283 int status;
4284 struct nfs4_lookup_arg args = {
4285 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004286 .dir_fh = NFS_FH(dir),
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004287 .name = &dentry->d_name,
David Howells2b3de442006-08-22 20:06:09 -04004288 };
4289 struct nfs4_lookup_res res = {
4290 .server = server,
4291 .fattr = fattr,
4292 .fh = fhandle,
4293 };
4294 struct rpc_message msg = {
4295 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
4296 .rpc_argp = &args,
4297 .rpc_resp = &res,
4298 };
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004299 unsigned short task_flags = 0;
4300
Olga Kornievskaia85e39fe2021-06-23 23:28:51 -04004301 if (server->nfs_client->cl_minorversion)
4302 task_flags = RPC_TASK_MOVEABLE;
4303
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004304 /* Is this is an attribute revalidation, subject to softreval? */
4305 if (nfs_lookup_is_soft_revalidate(dentry))
4306 task_flags |= RPC_TASK_TIMEOUT;
David Howells2b3de442006-08-22 20:06:09 -04004307
Anna Schumaker9558a002021-10-22 13:11:04 -04004308 args.bitmask = nfs4_bitmask(server, fattr->label);
David Quigleyaa9c2662013-05-22 12:50:44 -04004309
David Howells2b3de442006-08-22 20:06:09 -04004310 nfs_fattr_init(fattr);
4311
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004312 dprintk("NFS call lookup %pd2\n", dentry);
4313 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
4314 status = nfs4_do_call_sync(clnt, server, &msg,
4315 &args.seq_args, &res.seq_res, task_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316 dprintk("NFS reply lookup: %d\n", status);
4317 return status;
4318}
4319
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004320static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00004321{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00004322 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004323 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00004324 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4325 fattr->nlink = 2;
4326}
4327
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004328static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004329 struct dentry *dentry, struct nfs_fh *fhandle,
Anna Schumaker9558a002021-10-22 13:11:04 -04004330 struct nfs_fattr *fattr)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004331{
Trond Myklebust0688e642019-04-07 13:59:09 -04004332 struct nfs4_exception exception = {
4333 .interruptible = true,
4334 };
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004335 struct rpc_clnt *client = *clnt;
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004336 const struct qstr *name = &dentry->d_name;
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004337 int err;
4338 do {
Anna Schumaker9558a002021-10-22 13:11:04 -04004339 err = _nfs4_proc_lookup(client, dir, dentry, fhandle, fattr);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004340 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004341 switch (err) {
4342 case -NFS4ERR_BADNAME:
4343 err = -ENOENT;
4344 goto out;
4345 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004346 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02004347 if (err == -NFS4ERR_MOVED)
4348 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004349 goto out;
4350 case -NFS4ERR_WRONGSEC:
4351 err = -EPERM;
4352 if (client != *clnt)
4353 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04004354 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004355 if (IS_ERR(client))
4356 return PTR_ERR(client);
4357
4358 exception.retry = 1;
4359 break;
4360 default:
4361 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4362 }
4363 } while (exception.retry);
4364
4365out:
4366 if (err == 0)
4367 *clnt = client;
4368 else if (client != *clnt)
4369 rpc_shutdown_client(client);
4370
4371 return err;
4372}
4373
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004374static int nfs4_proc_lookup(struct inode *dir, struct dentry *dentry,
Anna Schumaker9558a002021-10-22 13:11:04 -04004375 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004377 int status;
4378 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004379
Anna Schumaker9558a002021-10-22 13:11:04 -04004380 status = nfs4_proc_lookup_common(&client, dir, dentry, fhandle, fattr);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004381 if (client != NFS_CLIENT(dir)) {
4382 rpc_shutdown_client(client);
4383 nfs_fixup_secinfo_attributes(fattr);
4384 }
4385 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004386}
4387
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004388struct rpc_clnt *
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004389nfs4_proc_lookup_mountpoint(struct inode *dir, struct dentry *dentry,
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004390 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4391{
Trond Myklebustb72888c2013-08-07 20:38:07 -04004392 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004393 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004394
Anna Schumaker9558a002021-10-22 13:11:04 -04004395 status = nfs4_proc_lookup_common(&client, dir, dentry, fhandle, fattr);
Trond Myklebustb72888c2013-08-07 20:38:07 -04004396 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004397 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04004398 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004399}
4400
Jeff Layton5b5faaf2017-06-29 06:34:52 -07004401static int _nfs4_proc_lookupp(struct inode *inode,
Anna Schumakerba4bc8d2021-10-22 13:11:05 -04004402 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Jeff Layton5b5faaf2017-06-29 06:34:52 -07004403{
4404 struct rpc_clnt *clnt = NFS_CLIENT(inode);
4405 struct nfs_server *server = NFS_SERVER(inode);
4406 int status;
4407 struct nfs4_lookupp_arg args = {
4408 .bitmask = server->attr_bitmask,
4409 .fh = NFS_FH(inode),
4410 };
4411 struct nfs4_lookupp_res res = {
4412 .server = server,
4413 .fattr = fattr,
Jeff Layton5b5faaf2017-06-29 06:34:52 -07004414 .fh = fhandle,
4415 };
4416 struct rpc_message msg = {
4417 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
4418 .rpc_argp = &args,
4419 .rpc_resp = &res,
4420 };
Trond Myklebust76998eb2020-10-20 14:30:35 -04004421 unsigned short task_flags = 0;
4422
4423 if (NFS_SERVER(inode)->flags & NFS_MOUNT_SOFTREVAL)
4424 task_flags |= RPC_TASK_TIMEOUT;
Jeff Layton5b5faaf2017-06-29 06:34:52 -07004425
Anna Schumakerba4bc8d2021-10-22 13:11:05 -04004426 args.bitmask = nfs4_bitmask(server, fattr->label);
Jeff Layton5b5faaf2017-06-29 06:34:52 -07004427
4428 nfs_fattr_init(fattr);
4429
4430 dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino);
4431 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
Trond Myklebust76998eb2020-10-20 14:30:35 -04004432 &res.seq_res, task_flags);
Jeff Layton5b5faaf2017-06-29 06:34:52 -07004433 dprintk("NFS reply lookupp: %d\n", status);
4434 return status;
4435}
4436
4437static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
Anna Schumakerba4bc8d2021-10-22 13:11:05 -04004438 struct nfs_fattr *fattr)
Jeff Layton5b5faaf2017-06-29 06:34:52 -07004439{
Trond Myklebust0688e642019-04-07 13:59:09 -04004440 struct nfs4_exception exception = {
4441 .interruptible = true,
4442 };
Jeff Layton5b5faaf2017-06-29 06:34:52 -07004443 int err;
4444 do {
Anna Schumakerba4bc8d2021-10-22 13:11:05 -04004445 err = _nfs4_proc_lookupp(inode, fhandle, fattr);
Jeff Layton5b5faaf2017-06-29 06:34:52 -07004446 trace_nfs4_lookupp(inode, err);
4447 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4448 &exception);
4449 } while (exception.retry);
4450 return err;
4451}
4452
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4454{
Trond Myklebust76b32992007-08-10 17:45:11 -04004455 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456 struct nfs4_accessargs args = {
4457 .fh = NFS_FH(inode),
Anna Schumaker1750d922017-07-26 12:00:21 -04004458 .access = entry->mask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459 };
Trond Myklebust76b32992007-08-10 17:45:11 -04004460 struct nfs4_accessres res = {
4461 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04004462 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463 struct rpc_message msg = {
4464 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
4465 .rpc_argp = &args,
4466 .rpc_resp = &res,
4467 .rpc_cred = entry->cred,
4468 };
David Quigleyaa9c2662013-05-22 12:50:44 -04004469 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470
Trond Myklebust7c672652018-06-04 15:00:53 -04004471 if (!nfs4_have_delegation(inode, FMODE_READ)) {
Trond Myklebust8bcbe7d2018-03-20 17:03:11 -04004472 res.fattr = nfs_alloc_fattr();
4473 if (res.fattr == NULL)
4474 return -ENOMEM;
4475 args.bitmask = server->cache_consistency_bitmask;
4476 }
Bryan Schumaker7c513052011-03-24 17:12:24 +00004477 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04004479 nfs_access_set_mask(entry, res.access);
Trond Myklebust8bcbe7d2018-03-20 17:03:11 -04004480 if (res.fattr)
4481 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482 }
Trond Myklebustc407d412010-04-16 16:22:48 -04004483 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484 return status;
4485}
4486
4487static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4488{
Trond Myklebust0688e642019-04-07 13:59:09 -04004489 struct nfs4_exception exception = {
4490 .interruptible = true,
4491 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492 int err;
4493 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004494 err = _nfs4_proc_access(inode, entry);
4495 trace_nfs4_access(inode, err);
4496 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004497 &exception);
4498 } while (exception.retry);
4499 return err;
4500}
4501
4502/*
4503 * TODO: For the time being, we don't try to get any attributes
4504 * along with any of the zero-copy operations READ, READDIR,
4505 * READLINK, WRITE.
4506 *
4507 * In the case of the first three, we want to put the GETATTR
4508 * after the read-type operation -- this is because it is hard
4509 * to predict the length of a GETATTR response in v4, and thus
4510 * align the READ data correctly. This means that the GETATTR
4511 * may end up partially falling into the page cache, and we should
4512 * shift it into the 'tail' of the xdr_buf before processing.
4513 * To do this efficiently, we need to know the total length
4514 * of data received, which doesn't seem to be available outside
4515 * of the RPC layer.
4516 *
4517 * In the case of WRITE, we also want to put the GETATTR after
4518 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04004519 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520 *
4521 * Both of these changes to the XDR layer would in fact be quite
4522 * minor, but I decided to leave them for a subsequent patch.
4523 */
4524static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4525 unsigned int pgbase, unsigned int pglen)
4526{
4527 struct nfs4_readlink args = {
4528 .fh = NFS_FH(inode),
4529 .pgbase = pgbase,
4530 .pglen = pglen,
4531 .pages = &page,
4532 };
Benny Halevyf50c7002009-04-01 09:21:55 -04004533 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534 struct rpc_message msg = {
4535 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4536 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04004537 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538 };
4539
Bryan Schumaker7c513052011-03-24 17:12:24 +00004540 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 -07004541}
4542
4543static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4544 unsigned int pgbase, unsigned int pglen)
4545{
Trond Myklebust0688e642019-04-07 13:59:09 -04004546 struct nfs4_exception exception = {
4547 .interruptible = true,
4548 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549 int err;
4550 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004551 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4552 trace_nfs4_readlink(inode, err);
4553 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004554 &exception);
4555 } while (exception.retry);
4556 return err;
4557}
4558
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004560 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562static int
4563nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004564 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004566 struct nfs_server *server = NFS_SERVER(dir);
David Quigleyaa9c2662013-05-22 12:50:44 -04004567 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004568 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570 int status = 0;
4571
NeilBrown532d4de2016-10-13 15:26:47 +11004572 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004573 if (IS_ERR(ctx))
4574 return PTR_ERR(ctx);
4575
David Quigleyaa9c2662013-05-22 12:50:44 -04004576 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4577
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004578 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4579 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08004580 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581 if (IS_ERR(state)) {
4582 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04004583 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585out:
David Quigleyaa9c2662013-05-22 12:50:44 -04004586 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004587 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004588 return status;
4589}
4590
Trond Myklebust3c591172018-07-31 15:54:10 -04004591static int
4592_nfs4_proc_remove(struct inode *dir, const struct qstr *name, u32 ftype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593{
Trond Myklebust16e42952005-10-27 22:12:44 -04004594 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004595 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07004597 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004599 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04004600 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04004601 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004602 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004603 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4604 .rpc_argp = &args,
4605 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606 };
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004607 unsigned long timestamp = jiffies;
Trond Myklebust778d2812012-04-27 13:48:19 -04004608 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04004609
Bryan Schumaker7c513052011-03-24 17:12:24 +00004610 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust3c591172018-07-31 15:54:10 -04004611 if (status == 0) {
4612 spin_lock(&dir->i_lock);
Trond Myklebust3c591172018-07-31 15:54:10 -04004613 /* Removing a directory decrements nlink in the parent */
4614 if (ftype == NF4DIR && dir->i_nlink > 2)
4615 nfs4_dec_nlink_locked(dir);
Trond Myklebust82eae5a2021-04-01 14:59:59 -04004616 nfs4_update_changeattr_locked(dir, &res.cinfo, timestamp,
4617 NFS_INO_INVALID_DATA);
Trond Myklebust3c591172018-07-31 15:54:10 -04004618 spin_unlock(&dir->i_lock);
4619 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620 return status;
4621}
4622
Trond Myklebust912678d2018-03-20 16:43:15 -04004623static int nfs4_proc_remove(struct inode *dir, struct dentry *dentry)
4624{
Trond Myklebust0688e642019-04-07 13:59:09 -04004625 struct nfs4_exception exception = {
4626 .interruptible = true,
4627 };
Trond Myklebust912678d2018-03-20 16:43:15 -04004628 struct inode *inode = d_inode(dentry);
4629 int err;
4630
Trond Myklebustc01d3642018-03-20 16:43:20 -04004631 if (inode) {
4632 if (inode->i_nlink == 1)
4633 nfs4_inode_return_delegation(inode);
4634 else
4635 nfs4_inode_make_writeable(inode);
4636 }
Trond Myklebust912678d2018-03-20 16:43:15 -04004637 do {
Trond Myklebust3c591172018-07-31 15:54:10 -04004638 err = _nfs4_proc_remove(dir, &dentry->d_name, NF4REG);
Trond Myklebust912678d2018-03-20 16:43:15 -04004639 trace_nfs4_remove(dir, &dentry->d_name, err);
4640 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4641 &exception);
4642 } while (exception.retry);
4643 return err;
4644}
4645
4646static int nfs4_proc_rmdir(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647{
Trond Myklebust0688e642019-04-07 13:59:09 -04004648 struct nfs4_exception exception = {
4649 .interruptible = true,
4650 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651 int err;
Trond Myklebust912678d2018-03-20 16:43:15 -04004652
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653 do {
Trond Myklebust3c591172018-07-31 15:54:10 -04004654 err = _nfs4_proc_remove(dir, name, NF4DIR);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004655 trace_nfs4_remove(dir, name, err);
4656 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657 &exception);
4658 } while (exception.retry);
4659 return err;
4660}
4661
Trond Myklebusted7e9ad2018-05-30 16:11:52 -04004662static void nfs4_proc_unlink_setup(struct rpc_message *msg,
4663 struct dentry *dentry,
4664 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004666 struct nfs_removeargs *args = msg->rpc_argp;
4667 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668
Trond Myklebust977fcc22018-03-20 16:43:17 -04004669 res->server = NFS_SB(dentry->d_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Anna Schumakerfba83f32018-05-04 16:22:50 -04004671 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04004672
4673 nfs_fattr_init(res->dir_attr);
Trond Myklebust977fcc22018-03-20 16:43:17 -04004674
4675 if (inode)
4676 nfs4_inode_return_delegation(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677}
4678
Bryan Schumaker34e137c2012-03-19 14:54:41 -04004679static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4680{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004681 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004682 &data->args.seq_args,
4683 &data->res.seq_res,
4684 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685}
4686
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004687static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004689 struct nfs_unlinkdata *data = task->tk_calldata;
4690 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004691
Trond Myklebust14516c32010-07-31 14:29:06 -04004692 if (!nfs4_sequence_done(task, &res->seq_res))
4693 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004694 if (nfs4_async_handle_error(task, res->server, NULL,
4695 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004696 return 0;
Trond Myklebustc40d52f2017-01-11 12:36:11 -05004697 if (task->tk_status == 0)
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004698 nfs4_update_changeattr(dir, &res->cinfo,
4699 res->dir_attr->time_start,
4700 NFS_INO_INVALID_DATA);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004701 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702}
4703
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004704static void nfs4_proc_rename_setup(struct rpc_message *msg,
4705 struct dentry *old_dentry,
4706 struct dentry *new_dentry)
Jeff Laytond3d41522010-09-17 17:31:57 -04004707{
Jeff Laytond3d41522010-09-17 17:31:57 -04004708 struct nfs_renameargs *arg = msg->rpc_argp;
4709 struct nfs_renameres *res = msg->rpc_resp;
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004710 struct inode *old_inode = d_inode(old_dentry);
4711 struct inode *new_inode = d_inode(new_dentry);
Jeff Laytond3d41522010-09-17 17:31:57 -04004712
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004713 if (old_inode)
Trond Myklebustc01d3642018-03-20 16:43:20 -04004714 nfs4_inode_make_writeable(old_inode);
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004715 if (new_inode)
4716 nfs4_inode_return_delegation(new_inode);
Jeff Laytond3d41522010-09-17 17:31:57 -04004717 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004718 res->server = NFS_SB(old_dentry->d_sb);
Anna Schumakerfba83f32018-05-04 16:22:50 -04004719 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1, 0);
Jeff Laytond3d41522010-09-17 17:31:57 -04004720}
4721
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04004722static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4723{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004724 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004725 &data->args.seq_args,
4726 &data->res.seq_res,
4727 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04004728}
4729
4730static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4731 struct inode *new_dir)
4732{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04004733 struct nfs_renamedata *data = task->tk_calldata;
4734 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04004735
4736 if (!nfs4_sequence_done(task, &res->seq_res))
4737 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004738 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04004739 return 0;
4740
Trond Myklebustc733c492017-01-11 12:32:26 -05004741 if (task->tk_status == 0) {
Trond Myklebust5636ec42018-07-31 15:54:11 -04004742 if (new_dir != old_dir) {
4743 /* Note: If we moved a directory, nlink will change */
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004744 nfs4_update_changeattr(old_dir, &res->old_cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04004745 res->old_fattr->time_start,
Trond Myklebustfabf2b32021-03-25 13:14:42 -04004746 NFS_INO_INVALID_NLINK |
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004747 NFS_INO_INVALID_DATA);
4748 nfs4_update_changeattr(new_dir, &res->new_cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04004749 res->new_fattr->time_start,
Trond Myklebustfabf2b32021-03-25 13:14:42 -04004750 NFS_INO_INVALID_NLINK |
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004751 NFS_INO_INVALID_DATA);
Trond Myklebust5636ec42018-07-31 15:54:11 -04004752 } else
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004753 nfs4_update_changeattr(old_dir, &res->old_cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04004754 res->old_fattr->time_start,
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004755 NFS_INO_INVALID_DATA);
Trond Myklebustc733c492017-01-11 12:32:26 -05004756 }
Jeff Laytond3d41522010-09-17 17:31:57 -04004757 return 1;
4758}
4759
Al Virobeffb8f2016-07-20 16:34:42 -04004760static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004761{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004762 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebust2f28dc32018-04-08 21:06:40 -04004763 __u32 bitmask[NFS4_BITMASK_SZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764 struct nfs4_link_arg arg = {
4765 .fh = NFS_FH(inode),
4766 .dir_fh = NFS_FH(dir),
4767 .name = name,
Trond Myklebust2f28dc32018-04-08 21:06:40 -04004768 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004770 struct nfs4_link_res res = {
4771 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004772 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004773 struct rpc_message msg = {
4774 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4775 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004776 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777 };
Trond Myklebust136f2622010-04-16 16:22:49 -04004778 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779
Anna Schumakeraa7ca3b2021-10-22 13:11:03 -04004780 res.fattr = nfs_alloc_fattr_with_label(server);
Trond Myklebust778d2812012-04-27 13:48:19 -04004781 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04004782 goto out;
4783
Trond Myklebustc01d3642018-03-20 16:43:20 -04004784 nfs4_inode_make_writeable(inode);
Anna Schumakeraa7ca3b2021-10-22 13:11:03 -04004785 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, res.fattr->label), inode,
Trond Myklebusta71029b2021-04-10 00:23:03 -04004786 NFS_INO_INVALID_CHANGE);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004787 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004788 if (!status) {
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004789 nfs4_update_changeattr(dir, &res.cinfo, res.fattr->time_start,
4790 NFS_INO_INVALID_DATA);
Trond Myklebust1301e422021-04-01 14:57:56 -04004791 nfs4_inc_nlink(inode);
David Quigleyaa9c2662013-05-22 12:50:44 -04004792 status = nfs_post_op_update_inode(inode, res.fattr);
4793 if (!status)
Anna Schumakeraa7ca3b2021-10-22 13:11:03 -04004794 nfs_setsecurity(inode, res.fattr, res.fattr->label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004795 }
David Quigley14c43f72013-05-22 12:50:43 -04004796
Trond Myklebust136f2622010-04-16 16:22:49 -04004797out:
Trond Myklebust136f2622010-04-16 16:22:49 -04004798 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004799 return status;
4800}
4801
Al Virobeffb8f2016-07-20 16:34:42 -04004802static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803{
Trond Myklebust0688e642019-04-07 13:59:09 -04004804 struct nfs4_exception exception = {
4805 .interruptible = true,
4806 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807 int err;
4808 do {
4809 err = nfs4_handle_exception(NFS_SERVER(inode),
4810 _nfs4_proc_link(inode, dir, name),
4811 &exception);
4812 } while (exception.retry);
4813 return err;
4814}
4815
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004816struct nfs4_createdata {
4817 struct rpc_message msg;
4818 struct nfs4_create_arg arg;
4819 struct nfs4_create_res res;
4820 struct nfs_fh fh;
4821 struct nfs_fattr fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004822};
4823
4824static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004825 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004826{
4827 struct nfs4_createdata *data;
4828
4829 data = kzalloc(sizeof(*data), GFP_KERNEL);
4830 if (data != NULL) {
4831 struct nfs_server *server = NFS_SERVER(dir);
4832
Anna Schumaker68be1742021-10-22 13:11:02 -04004833 data->fattr.label = nfs4_label_alloc(server, GFP_KERNEL);
4834 if (IS_ERR(data->fattr.label))
David Quigley14c43f72013-05-22 12:50:43 -04004835 goto out_free;
4836
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004837 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4838 data->msg.rpc_argp = &data->arg;
4839 data->msg.rpc_resp = &data->res;
4840 data->arg.dir_fh = NFS_FH(dir);
4841 data->arg.server = server;
4842 data->arg.name = name;
4843 data->arg.attrs = sattr;
4844 data->arg.ftype = ftype;
Anna Schumaker68be1742021-10-22 13:11:02 -04004845 data->arg.bitmask = nfs4_bitmask(server, data->fattr.label);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004846 data->arg.umask = current_umask();
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004847 data->res.server = server;
4848 data->res.fh = &data->fh;
4849 data->res.fattr = &data->fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004850 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004851 }
4852 return data;
David Quigley14c43f72013-05-22 12:50:43 -04004853out_free:
4854 kfree(data);
4855 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004856}
4857
4858static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4859{
Bryan Schumaker7c513052011-03-24 17:12:24 +00004860 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00004861 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004862 if (status == 0) {
Trond Myklebust3c591172018-07-31 15:54:10 -04004863 spin_lock(&dir->i_lock);
Trond Myklebust3c591172018-07-31 15:54:10 -04004864 /* Creating a directory bumps nlink in the parent */
4865 if (data->arg.ftype == NF4DIR)
4866 nfs4_inc_nlink_locked(dir);
Trond Myklebust82eae5a2021-04-01 14:59:59 -04004867 nfs4_update_changeattr_locked(dir, &data->res.dir_cinfo,
4868 data->res.fattr->time_start,
4869 NFS_INO_INVALID_DATA);
Trond Myklebust3c591172018-07-31 15:54:10 -04004870 spin_unlock(&dir->i_lock);
Anna Schumaker68be1742021-10-22 13:11:02 -04004871 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.fattr->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004872 }
4873 return status;
4874}
4875
4876static void nfs4_free_createdata(struct nfs4_createdata *data)
4877{
Anna Schumaker68be1742021-10-22 13:11:02 -04004878 nfs4_label_free(data->fattr.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004879 kfree(data);
4880}
4881
Chuck Lever4f390c12006-08-22 20:06:22 -04004882static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004883 struct page *page, unsigned int len, struct iattr *sattr,
4884 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004885{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004886 struct nfs4_createdata *data;
4887 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004888
Chuck Lever94a6d752006-08-22 20:06:23 -04004889 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004890 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04004891
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004892 status = -ENOMEM;
4893 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4894 if (data == NULL)
4895 goto out;
4896
4897 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4898 data->arg.u.symlink.pages = &page;
4899 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004900 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004901
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004902 status = nfs4_do_create(dir, dentry, data);
4903
4904 nfs4_free_createdata(data);
4905out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004906 return status;
4907}
4908
Chuck Lever4f390c12006-08-22 20:06:22 -04004909static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004910 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004911{
Trond Myklebust0688e642019-04-07 13:59:09 -04004912 struct nfs4_exception exception = {
4913 .interruptible = true,
4914 };
David Quigleyaa9c2662013-05-22 12:50:44 -04004915 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004917
4918 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4919
Linus Torvalds1da177e2005-04-16 15:20:36 -07004920 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004921 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4922 trace_nfs4_symlink(dir, &dentry->d_name, err);
4923 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924 &exception);
4925 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004926
4927 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004928 return err;
4929}
4930
4931static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004932 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004934 struct nfs4_createdata *data;
4935 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004936
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004937 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4938 if (data == NULL)
4939 goto out;
4940
David Quigley1775fd32013-05-22 12:50:42 -04004941 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004942 status = nfs4_do_create(dir, dentry, data);
4943
4944 nfs4_free_createdata(data);
4945out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946 return status;
4947}
4948
4949static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4950 struct iattr *sattr)
4951{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004952 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust0688e642019-04-07 13:59:09 -04004953 struct nfs4_exception exception = {
4954 .interruptible = true,
4955 };
David Quigleyaa9c2662013-05-22 12:50:44 -04004956 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004957 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004958
David Quigleyaa9c2662013-05-22 12:50:44 -04004959 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4960
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004961 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4962 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004963 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004964 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4965 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4966 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967 &exception);
4968 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004969 nfs4_label_release_security(label);
4970
Linus Torvalds1da177e2005-04-16 15:20:36 -07004971 return err;
4972}
4973
Trond Myklebust82e22a52020-11-02 17:34:23 -05004974static int _nfs4_proc_readdir(struct nfs_readdir_arg *nr_arg,
4975 struct nfs_readdir_res *nr_res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004976{
Trond Myklebust82e22a52020-11-02 17:34:23 -05004977 struct inode *dir = d_inode(nr_arg->dentry);
Olga Kornievskaia05ad9172020-11-06 16:03:38 -05004978 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979 struct nfs4_readdir_arg args = {
4980 .fh = NFS_FH(dir),
Trond Myklebust82e22a52020-11-02 17:34:23 -05004981 .pages = nr_arg->pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004982 .pgbase = 0,
Trond Myklebust82e22a52020-11-02 17:34:23 -05004983 .count = nr_arg->page_len,
4984 .plus = nr_arg->plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985 };
4986 struct nfs4_readdir_res res;
4987 struct rpc_message msg = {
4988 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4989 .rpc_argp = &args,
4990 .rpc_resp = &res,
Trond Myklebust82e22a52020-11-02 17:34:23 -05004991 .rpc_cred = nr_arg->cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004992 };
4993 int status;
4994
Trond Myklebust82e22a52020-11-02 17:34:23 -05004995 dprintk("%s: dentry = %pd2, cookie = %llu\n", __func__,
4996 nr_arg->dentry, (unsigned long long)nr_arg->cookie);
Olga Kornievskaia05ad9172020-11-06 16:03:38 -05004997 if (!(server->caps & NFS_CAP_SECURITY_LABEL))
4998 args.bitmask = server->attr_bitmask_nl;
4999 else
5000 args.bitmask = server->attr_bitmask;
5001
Trond Myklebust82e22a52020-11-02 17:34:23 -05005002 nfs4_setup_readdir(nr_arg->cookie, nr_arg->verf, nr_arg->dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005003 res.pgbase = args.pgbase;
Olga Kornievskaia05ad9172020-11-06 16:03:38 -05005004 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
5005 &res.seq_res, 0);
Trond Myklebustac396122010-11-15 20:26:22 -05005006 if (status >= 0) {
Trond Myklebust82e22a52020-11-02 17:34:23 -05005007 memcpy(nr_res->verf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05005008 status += args.pgbase;
5009 }
Trond Myklebustc4812992007-09-28 17:11:45 -04005010
5011 nfs_invalidate_atime(dir);
5012
Harvey Harrison3110ff82008-05-02 13:42:44 -07005013 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005014 return status;
5015}
5016
Trond Myklebust82e22a52020-11-02 17:34:23 -05005017static int nfs4_proc_readdir(struct nfs_readdir_arg *arg,
5018 struct nfs_readdir_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005019{
Trond Myklebust0688e642019-04-07 13:59:09 -04005020 struct nfs4_exception exception = {
5021 .interruptible = true,
5022 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023 int err;
5024 do {
Trond Myklebust82e22a52020-11-02 17:34:23 -05005025 err = _nfs4_proc_readdir(arg, res);
5026 trace_nfs4_readdir(d_inode(arg->dentry), err);
5027 err = nfs4_handle_exception(NFS_SERVER(d_inode(arg->dentry)),
5028 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005029 } while (exception.retry);
5030 return err;
5031}
5032
5033static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04005034 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005035{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04005036 struct nfs4_createdata *data;
5037 int mode = sattr->ia_mode;
5038 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005039
Trond Myklebust57dc9a52008-06-20 15:35:32 -04005040 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
5041 if (data == NULL)
5042 goto out;
5043
Linus Torvalds1da177e2005-04-16 15:20:36 -07005044 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04005045 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005046 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04005047 data->arg.ftype = NF4BLK;
5048 data->arg.u.device.specdata1 = MAJOR(rdev);
5049 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005050 }
5051 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04005052 data->arg.ftype = NF4CHR;
5053 data->arg.u.device.specdata1 = MAJOR(rdev);
5054 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04005055 } else if (!S_ISSOCK(mode)) {
5056 status = -EINVAL;
5057 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005058 }
David Quigley1775fd32013-05-22 12:50:42 -04005059
David Quigleyaa9c2662013-05-22 12:50:44 -04005060 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04005061 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04005062out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04005063 nfs4_free_createdata(data);
5064out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005065 return status;
5066}
5067
5068static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
5069 struct iattr *sattr, dev_t rdev)
5070{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05005071 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust0688e642019-04-07 13:59:09 -04005072 struct nfs4_exception exception = {
5073 .interruptible = true,
5074 };
David Quigleyaa9c2662013-05-22 12:50:44 -04005075 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005076 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00005077
David Quigleyaa9c2662013-05-22 12:50:44 -04005078 label = nfs4_label_init_security(dir, dentry, sattr, &l);
5079
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05005080 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
5081 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005082 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04005083 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
5084 trace_nfs4_mknod(dir, &dentry->d_name, err);
5085 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005086 &exception);
5087 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04005088
5089 nfs4_label_release_security(label);
5090
Linus Torvalds1da177e2005-04-16 15:20:36 -07005091 return err;
5092}
5093
5094static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
5095 struct nfs_fsstat *fsstat)
5096{
5097 struct nfs4_statfs_arg args = {
5098 .fh = fhandle,
5099 .bitmask = server->attr_bitmask,
5100 };
Benny Halevy24ad1482009-04-01 09:21:56 -04005101 struct nfs4_statfs_res res = {
5102 .fsstat = fsstat,
5103 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005104 struct rpc_message msg = {
5105 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
5106 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04005107 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005108 };
5109
Trond Myklebust0e574af2005-10-27 22:12:38 -04005110 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005111 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005112}
5113
5114static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
5115{
Trond Myklebust0688e642019-04-07 13:59:09 -04005116 struct nfs4_exception exception = {
5117 .interruptible = true,
5118 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005119 int err;
5120 do {
5121 err = nfs4_handle_exception(server,
5122 _nfs4_proc_statfs(server, fhandle, fsstat),
5123 &exception);
5124 } while (exception.retry);
5125 return err;
5126}
5127
5128static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
5129 struct nfs_fsinfo *fsinfo)
5130{
5131 struct nfs4_fsinfo_arg args = {
5132 .fh = fhandle,
5133 .bitmask = server->attr_bitmask,
5134 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04005135 struct nfs4_fsinfo_res res = {
5136 .fsinfo = fsinfo,
5137 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005138 struct rpc_message msg = {
5139 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
5140 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04005141 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005142 };
5143
Bryan Schumaker7c513052011-03-24 17:12:24 +00005144 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005145}
5146
5147static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5148{
Trond Myklebust0688e642019-04-07 13:59:09 -04005149 struct nfs4_exception exception = {
5150 .interruptible = true,
5151 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005152 int err;
5153
5154 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04005155 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04005156 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04005157 if (err == 0) {
Robert Milkowski7dc29932020-01-30 09:43:25 +00005158 nfs4_set_lease_period(server->nfs_client, fsinfo->lease_time * HZ);
Chuck Lever83ca7f52013-03-16 15:55:53 -04005159 break;
5160 }
5161 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005162 } while (exception.retry);
5163 return err;
5164}
5165
5166static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5167{
Bryan Schumakere38eb652012-06-20 15:53:40 -04005168 int error;
5169
Trond Myklebust0e574af2005-10-27 22:12:38 -04005170 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04005171 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08005172 if (error == 0) {
5173 /* block layout checks this! */
5174 server->pnfs_blksize = fsinfo->blksize;
Jeff Laytonca440c32016-09-15 14:40:49 -04005175 set_pnfs_layoutdriver(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08005176 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04005177
5178 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005179}
5180
5181static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5182 struct nfs_pathconf *pathconf)
5183{
5184 struct nfs4_pathconf_arg args = {
5185 .fh = fhandle,
5186 .bitmask = server->attr_bitmask,
5187 };
Benny Halevyd45b2982009-04-01 09:21:58 -04005188 struct nfs4_pathconf_res res = {
5189 .pathconf = pathconf,
5190 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005191 struct rpc_message msg = {
5192 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
5193 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04005194 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005195 };
5196
5197 /* None of the pathconf attributes are mandatory to implement */
5198 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
5199 memset(pathconf, 0, sizeof(*pathconf));
5200 return 0;
5201 }
5202
Trond Myklebust0e574af2005-10-27 22:12:38 -04005203 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005204 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005205}
5206
5207static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5208 struct nfs_pathconf *pathconf)
5209{
Trond Myklebust0688e642019-04-07 13:59:09 -04005210 struct nfs4_exception exception = {
5211 .interruptible = true,
5212 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005213 int err;
5214
5215 do {
5216 err = nfs4_handle_exception(server,
5217 _nfs4_proc_pathconf(server, fhandle, pathconf),
5218 &exception);
5219 } while (exception.retry);
5220 return err;
5221}
5222
Trond Myklebust5521abf2013-03-16 20:54:34 -04005223int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04005224 const struct nfs_open_context *ctx,
5225 const struct nfs_lock_context *l_ctx,
5226 fmode_t fmode)
5227{
NeilBrown17393472016-10-13 15:26:47 +11005228 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04005229}
5230EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
5231
Trond Myklebust5521abf2013-03-16 20:54:34 -04005232static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
5233 const struct nfs_open_context *ctx,
5234 const struct nfs_lock_context *l_ctx,
5235 fmode_t fmode)
5236{
Ben Dooksd49dd112019-10-16 17:28:21 +01005237 nfs4_stateid _current_stateid;
Trond Myklebust5521abf2013-03-16 20:54:34 -04005238
Trond Myklebuste1253be2014-03-05 08:44:23 -05005239 /* If the current stateid represents a lost lock, then exit */
Ben Dooksd49dd112019-10-16 17:28:21 +01005240 if (nfs4_set_rw_stateid(&_current_stateid, ctx, l_ctx, fmode) == -EIO)
Trond Myklebuste1253be2014-03-05 08:44:23 -05005241 return true;
Ben Dooksd49dd112019-10-16 17:28:21 +01005242 return nfs4_stateid_match(stateid, &_current_stateid);
Trond Myklebust5521abf2013-03-16 20:54:34 -04005243}
5244
5245static bool nfs4_error_stateid_expired(int err)
5246{
5247 switch (err) {
5248 case -NFS4ERR_DELEG_REVOKED:
5249 case -NFS4ERR_ADMIN_REVOKED:
5250 case -NFS4ERR_BAD_STATEID:
5251 case -NFS4ERR_STALE_STATEID:
5252 case -NFS4ERR_OLD_STATEID:
5253 case -NFS4ERR_OPENMODE:
5254 case -NFS4ERR_EXPIRED:
5255 return true;
5256 }
5257 return false;
5258}
5259
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005260static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005261{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005262 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005263
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005264 trace_nfs4_read(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04005265 if (task->tk_status < 0) {
5266 struct nfs4_exception exception = {
5267 .inode = hdr->inode,
5268 .state = hdr->args.context->state,
5269 .stateid = &hdr->args.stateid,
5270 };
5271 task->tk_status = nfs4_async_handle_exception(task,
5272 server, task->tk_status, &exception);
5273 if (exception.retry) {
5274 rpc_restart_call_prepare(task);
5275 return -EAGAIN;
5276 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005277 }
Trond Myklebust8850df92007-09-28 17:20:07 -04005278
Linus Torvalds1da177e2005-04-16 15:20:36 -07005279 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005280 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05005281 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005282}
5283
Trond Myklebust5521abf2013-03-16 20:54:34 -04005284static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04005285 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04005286{
5287
5288 if (!nfs4_error_stateid_expired(task->tk_status) ||
5289 nfs4_stateid_is_current(&args->stateid,
5290 args->context,
5291 args->lock_context,
5292 FMODE_READ))
5293 return false;
5294 rpc_restart_call_prepare(task);
5295 return true;
5296}
5297
Anna Schumakerc5675522014-05-28 13:41:22 -04005298static bool nfs4_read_plus_not_supported(struct rpc_task *task,
5299 struct nfs_pgio_header *hdr)
5300{
5301 struct nfs_server *server = NFS_SERVER(hdr->inode);
5302 struct rpc_message *msg = &task->tk_msg;
5303
5304 if (msg->rpc_proc == &nfs4_procedures[NFSPROC4_CLNT_READ_PLUS] &&
5305 server->caps & NFS_CAP_READ_PLUS && task->tk_status == -ENOTSUPP) {
5306 server->caps &= ~NFS_CAP_READ_PLUS;
5307 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
5308 rpc_restart_call_prepare(task);
5309 return true;
5310 }
5311 return false;
5312}
5313
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005314static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc72011-03-01 01:34:20 +00005315{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005316 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc72011-03-01 01:34:20 +00005317 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005318 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04005319 return -EAGAIN;
Anna Schumakerc5675522014-05-28 13:41:22 -04005320 if (nfs4_read_plus_not_supported(task, hdr))
5321 return -EAGAIN;
Trond Myklebustbfc505d2016-09-15 18:26:05 -04005322 if (task->tk_status > 0)
5323 nfs_invalidate_atime(hdr->inode);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005324 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5325 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00005326}
5327
Anna Schumaker21e31402020-12-03 15:18:39 -05005328#if defined CONFIG_NFS_V4_2 && defined CONFIG_NFS_V4_2_READ_PLUS
Trond Myklebust5c3485b2020-12-10 09:34:34 -05005329static void nfs42_read_plus_support(struct nfs_pgio_header *hdr,
5330 struct rpc_message *msg)
Anna Schumakerc5675522014-05-28 13:41:22 -04005331{
Trond Myklebust5c3485b2020-12-10 09:34:34 -05005332 /* Note: We don't use READ_PLUS with pNFS yet */
5333 if (nfs_server_capable(hdr->inode, NFS_CAP_READ_PLUS) && !hdr->ds_clp)
Anna Schumakerc5675522014-05-28 13:41:22 -04005334 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ_PLUS];
Anna Schumakerc5675522014-05-28 13:41:22 -04005335}
5336#else
Trond Myklebust5c3485b2020-12-10 09:34:34 -05005337static void nfs42_read_plus_support(struct nfs_pgio_header *hdr,
5338 struct rpc_message *msg)
Anna Schumakerc5675522014-05-28 13:41:22 -04005339{
Anna Schumakerc5675522014-05-28 13:41:22 -04005340}
5341#endif /* CONFIG_NFS_V4_2 */
5342
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005343static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
5344 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005345{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005346 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04005347 if (!hdr->pgio_done_cb)
5348 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebust5c3485b2020-12-10 09:34:34 -05005349 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
5350 nfs42_read_plus_support(hdr, msg);
Anna Schumakerfba83f32018-05-04 16:22:50 -04005351 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005352}
5353
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005354static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
5355 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04005356{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005357 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005358 &hdr->args.seq_args,
5359 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04005360 task))
NeilBrownef1820f2013-09-04 17:04:49 +10005361 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005362 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
5363 hdr->args.lock_context,
Benjamin Coddingtonfbe77c32017-04-19 10:11:35 -04005364 hdr->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10005365 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005366 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10005367 return -EIO;
5368 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005369}
5370
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005371static int nfs4_write_done_cb(struct rpc_task *task,
5372 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005373{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005374 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10005375
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005376 trace_nfs4_write(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04005377 if (task->tk_status < 0) {
5378 struct nfs4_exception exception = {
5379 .inode = hdr->inode,
5380 .state = hdr->args.context->state,
5381 .stateid = &hdr->args.stateid,
5382 };
5383 task->tk_status = nfs4_async_handle_exception(task,
5384 NFS_SERVER(inode), task->tk_status,
5385 &exception);
5386 if (exception.retry) {
5387 rpc_restart_call_prepare(task);
5388 return -EAGAIN;
5389 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005390 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04005391 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005392 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05005393 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04005394 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05005395 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005396}
5397
Trond Myklebust5521abf2013-03-16 20:54:34 -04005398static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04005399 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04005400{
5401
5402 if (!nfs4_error_stateid_expired(task->tk_status) ||
5403 nfs4_stateid_is_current(&args->stateid,
5404 args->context,
5405 args->lock_context,
5406 FMODE_WRITE))
5407 return false;
5408 rpc_restart_call_prepare(task);
5409 return true;
5410}
5411
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005412static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00005413{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005414 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00005415 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005416 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04005417 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005418 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5419 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00005420}
5421
Trond Myklebust5a37f852012-04-28 14:55:16 -04005422static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005423bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00005424{
Trond Myklebust5a37f852012-04-28 14:55:16 -04005425 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005426 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04005427 return false;
5428 /* Otherwise, request attributes if and only if we don't hold
5429 * a delegation
5430 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04005431 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00005432}
Fred Isamana69aef12011-03-03 15:13:47 +00005433
Trond Myklebust332d1a02021-03-25 18:15:36 -04005434static void nfs4_bitmask_set(__u32 bitmask[NFS4_BITMASK_SZ], const __u32 *src,
5435 struct inode *inode, struct nfs_server *server,
5436 struct nfs4_label *label)
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005437{
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005438 unsigned long cache_validity = READ_ONCE(NFS_I(inode)->cache_validity);
Trond Myklebust332d1a02021-03-25 18:15:36 -04005439 unsigned int i;
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005440
Trond Myklebust332d1a02021-03-25 18:15:36 -04005441 memcpy(bitmask, src, sizeof(*bitmask) * NFS4_BITMASK_SZ);
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005442
Trond Myklebust45901a22021-02-08 08:49:32 -05005443 if (cache_validity & NFS_INO_INVALID_CHANGE)
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005444 bitmask[0] |= FATTR4_WORD0_CHANGE;
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005445 if (cache_validity & NFS_INO_INVALID_ATIME)
5446 bitmask[1] |= FATTR4_WORD1_TIME_ACCESS;
Trond Myklebust720869e2021-04-13 09:41:16 -04005447 if (cache_validity & NFS_INO_INVALID_MODE)
5448 bitmask[1] |= FATTR4_WORD1_MODE;
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005449 if (cache_validity & NFS_INO_INVALID_OTHER)
Trond Myklebust720869e2021-04-13 09:41:16 -04005450 bitmask[1] |= FATTR4_WORD1_OWNER | FATTR4_WORD1_OWNER_GROUP;
Trond Myklebustfabf2b32021-03-25 13:14:42 -04005451 if (cache_validity & NFS_INO_INVALID_NLINK)
5452 bitmask[1] |= FATTR4_WORD1_NUMLINKS;
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005453 if (label && label->len && cache_validity & NFS_INO_INVALID_LABEL)
5454 bitmask[2] |= FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust45901a22021-02-08 08:49:32 -05005455 if (cache_validity & NFS_INO_INVALID_CTIME)
5456 bitmask[1] |= FATTR4_WORD1_TIME_METADATA;
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005457 if (cache_validity & NFS_INO_INVALID_MTIME)
5458 bitmask[1] |= FATTR4_WORD1_TIME_MODIFY;
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005459 if (cache_validity & NFS_INO_INVALID_BLOCKS)
5460 bitmask[1] |= FATTR4_WORD1_SPACE_USED;
Trond Myklebust332d1a02021-03-25 18:15:36 -04005461
Trond Myklebustcc7f2da2021-04-11 14:31:24 -04005462 if (cache_validity & NFS_INO_INVALID_SIZE)
Trond Myklebust332d1a02021-03-25 18:15:36 -04005463 bitmask[0] |= FATTR4_WORD0_SIZE;
5464
5465 for (i = 0; i < NFS4_BITMASK_SZ; i++)
5466 bitmask[i] &= server->attr_bitmask[i];
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005467}
5468
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005469static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
Anna Schumakerfb91fb02018-05-04 16:22:48 -04005470 struct rpc_message *msg,
5471 struct rpc_clnt **clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005472{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005473 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005474
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005475 if (!nfs4_write_need_cache_consistency_data(hdr)) {
5476 hdr->args.bitmask = NULL;
5477 hdr->res.fattr = NULL;
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005478 } else {
Trond Myklebust332d1a02021-03-25 18:15:36 -04005479 nfs4_bitmask_set(hdr->args.bitmask_store,
5480 server->cache_consistency_bitmask,
5481 hdr->inode, server, NULL);
5482 hdr->args.bitmask = hdr->args.bitmask_store;
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005483 }
Trond Myklebust5a37f852012-04-28 14:55:16 -04005484
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005485 if (!hdr->pgio_done_cb)
5486 hdr->pgio_done_cb = nfs4_write_done_cb;
5487 hdr->res.server = server;
5488 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005489
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005490 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Olga Kornievskaiacd1b6592020-02-12 17:32:12 -05005491 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
Anna Schumakerfb91fb02018-05-04 16:22:48 -04005492 nfs4_state_protect_write(server->nfs_client, clnt, msg, hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005493}
5494
Fred Isaman0b7c0152012-04-20 14:47:39 -04005495static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
5496{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005497 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005498 &data->args.seq_args,
5499 &data->res.seq_res,
5500 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005501}
5502
Fred Isaman0b7c0152012-04-20 14:47:39 -04005503static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005504{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005505 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04005506
Trond Myklebustcc668ab2013-08-14 15:31:28 -04005507 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10005508 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
5509 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005510 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05005511 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005512 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05005513 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005514}
5515
Fred Isaman0b7c0152012-04-20 14:47:39 -04005516static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00005517{
5518 if (!nfs4_sequence_done(task, &data->res.seq_res))
5519 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04005520 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00005521}
5522
Anna Schumakere9ae1ee2018-05-04 16:22:49 -04005523static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg,
5524 struct rpc_clnt **clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005525{
Trond Myklebust788e7a82006-03-20 13:44:27 -05005526 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00005527
Fred Isaman0b7c0152012-04-20 14:47:39 -04005528 if (data->commit_done_cb == NULL)
5529 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04005530 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005531 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Anna Schumakerfba83f32018-05-04 16:22:50 -04005532 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Anna Schumakere9ae1ee2018-05-04 16:22:49 -04005533 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_COMMIT, clnt, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005534}
5535
Olga Kornievskaia6b8d84e2018-07-09 15:13:36 -04005536static int _nfs4_proc_commit(struct file *dst, struct nfs_commitargs *args,
5537 struct nfs_commitres *res)
5538{
5539 struct inode *dst_inode = file_inode(dst);
5540 struct nfs_server *server = NFS_SERVER(dst_inode);
5541 struct rpc_message msg = {
5542 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
5543 .rpc_argp = args,
5544 .rpc_resp = res,
5545 };
5546
5547 args->fh = NFS_FH(dst_inode);
5548 return nfs4_call_sync(server->client, server, &msg,
5549 &args->seq_args, &res->seq_res, 1);
5550}
5551
5552int nfs4_proc_commit(struct file *dst, __u64 offset, __u32 count, struct nfs_commitres *res)
5553{
5554 struct nfs_commitargs args = {
5555 .offset = offset,
5556 .count = count,
5557 };
5558 struct nfs_server *dst_server = NFS_SERVER(file_inode(dst));
5559 struct nfs4_exception exception = { };
5560 int status;
5561
5562 do {
5563 status = _nfs4_proc_commit(dst, &args, res);
5564 status = nfs4_handle_exception(dst_server, status, &exception);
5565 } while (exception.retry);
5566
5567 return status;
5568}
5569
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005570struct nfs4_renewdata {
5571 struct nfs_client *client;
5572 unsigned long timestamp;
5573};
5574
Linus Torvalds1da177e2005-04-16 15:20:36 -07005575/*
5576 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
5577 * standalone procedure for queueing an asynchronous RENEW.
5578 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005579static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005580{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005581 struct nfs4_renewdata *data = calldata;
5582 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005583
Elena Reshetova212bf412017-10-20 12:53:38 +03005584 if (refcount_read(&clp->cl_count) > 1)
Alexandros Batsakis0851de062010-02-05 03:45:06 -08005585 nfs4_schedule_state_renewal(clp);
5586 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005587 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005588}
5589
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005590static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005591{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005592 struct nfs4_renewdata *data = calldata;
5593 struct nfs_client *clp = data->client;
5594 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005595
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005596 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04005597 switch (task->tk_status) {
5598 case 0:
5599 break;
5600 case -NFS4ERR_LEASE_MOVED:
5601 nfs4_schedule_lease_moved_recovery(clp);
5602 break;
5603 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04005604 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04005605 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
5606 return;
5607 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005608 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04005609 return;
5610 }
5611 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005612 }
Trond Myklebust452e9352010-07-31 14:29:06 -04005613 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005614}
5615
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005616static const struct rpc_call_ops nfs4_renew_ops = {
5617 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005618 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005619};
5620
NeilBrowna52458b2018-12-03 11:30:31 +11005621static int nfs4_proc_async_renew(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005622{
5623 struct rpc_message msg = {
5624 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5625 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01005626 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005627 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005628 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005629
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005630 if (renew_flags == 0)
5631 return 0;
Elena Reshetova212bf412017-10-20 12:53:38 +03005632 if (!refcount_inc_not_zero(&clp->cl_count))
Alexandros Batsakis0851de062010-02-05 03:45:06 -08005633 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04005634 data = kmalloc(sizeof(*data), GFP_NOFS);
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04005635 if (data == NULL) {
5636 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005637 return -ENOMEM;
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04005638 }
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005639 data->client = clp;
5640 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005641 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005642 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005643}
5644
NeilBrowna52458b2018-12-03 11:30:31 +11005645static int nfs4_proc_renew(struct nfs_client *clp, const struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005646{
5647 struct rpc_message msg = {
5648 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5649 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01005650 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005651 };
5652 unsigned long now = jiffies;
5653 int status;
5654
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005655 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005656 if (status < 0)
5657 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04005658 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005659 return 0;
5660}
5661
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005662static inline int nfs4_server_supports_acls(struct nfs_server *server)
5663{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06005664 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005665}
5666
Trond Myklebust21f498c2012-08-24 10:59:25 -04005667/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
5668 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005669 * the stack.
5670 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04005671#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005672
Frank van der Lindenccde1e92020-06-23 22:39:01 +00005673int nfs4_buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005674 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05005675{
5676 struct page *newpage, **spages;
5677 int rc = 0;
5678 size_t len;
5679 spages = pages;
5680
5681 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04005682 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05005683 newpage = alloc_page(GFP_KERNEL);
5684
5685 if (newpage == NULL)
5686 goto unwind;
5687 memcpy(page_address(newpage), buf, len);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005688 buf += len;
5689 buflen -= len;
Neil Hormane9e3d722011-03-04 19:26:03 -05005690 *pages++ = newpage;
5691 rc++;
5692 } while (buflen != 0);
5693
5694 return rc;
5695
5696unwind:
5697 for(; rc > 0; rc--)
5698 __free_page(spages[rc-1]);
5699 return -ENOMEM;
5700}
5701
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005702struct nfs4_cached_acl {
5703 int cached;
5704 size_t len;
Gustavo A. R. Silva5601cda2020-03-09 13:24:42 -05005705 char data[];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005706};
5707
5708static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005709{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005710 struct nfs_inode *nfsi = NFS_I(inode);
5711
5712 spin_lock(&inode->i_lock);
5713 kfree(nfsi->nfs4_acl);
5714 nfsi->nfs4_acl = acl;
5715 spin_unlock(&inode->i_lock);
5716}
5717
5718static void nfs4_zap_acl_attr(struct inode *inode)
5719{
5720 nfs4_set_cached_acl(inode, NULL);
5721}
5722
5723static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
5724{
5725 struct nfs_inode *nfsi = NFS_I(inode);
5726 struct nfs4_cached_acl *acl;
5727 int ret = -ENOENT;
5728
5729 spin_lock(&inode->i_lock);
5730 acl = nfsi->nfs4_acl;
5731 if (acl == NULL)
5732 goto out;
5733 if (buf == NULL) /* user is just asking for length */
5734 goto out_len;
5735 if (acl->cached == 0)
5736 goto out;
5737 ret = -ERANGE; /* see getxattr(2) man page */
5738 if (acl->len > buflen)
5739 goto out;
5740 memcpy(buf, acl->data, acl->len);
5741out_len:
5742 ret = acl->len;
5743out:
5744 spin_unlock(&inode->i_lock);
5745 return ret;
5746}
5747
Sachin Prabhu5794d212012-04-17 14:36:40 +01005748static 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 +00005749{
5750 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005751 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005752
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005753 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005754 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005755 if (acl == NULL)
5756 goto out;
5757 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01005758 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005759 } else {
5760 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5761 if (acl == NULL)
5762 goto out;
5763 acl->cached = 0;
5764 }
5765 acl->len = acl_len;
5766out:
5767 nfs4_set_cached_acl(inode, acl);
5768}
5769
Andy Adamsonbf118a32011-12-07 11:55:27 -05005770/*
5771 * The getxattr API returns the required buffer length when called with a
5772 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5773 * the required buf. On a NULL buf, we send a page of data to the server
5774 * guessing that the ACL request can be serviced by a page. If so, we cache
5775 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5776 * the cache. If not so, we throw away the page, and cache the required
5777 * length. The next getxattr call will then produce another round trip to
5778 * the server, this time with the input buf of the required size.
5779 */
Trond Myklebust16b42892006-08-24 12:27:15 -04005780static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005781{
Olga Kornievskaia62a15732020-01-02 17:09:54 -05005782 struct page **pages;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005783 struct nfs_getaclargs args = {
5784 .fh = NFS_FH(inode),
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005785 .acl_len = buflen,
5786 };
Benny Halevy663c79b2009-04-01 09:21:59 -04005787 struct nfs_getaclres res = {
5788 .acl_len = buflen,
5789 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005790 struct rpc_message msg = {
5791 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5792 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04005793 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005794 };
Olga Kornievskaia62a15732020-01-02 17:09:54 -05005795 unsigned int npages;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005796 int ret = -ENOMEM, i;
Olga Kornievskaia62a15732020-01-02 17:09:54 -05005797 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005798
Olga Kornievskaia62a15732020-01-02 17:09:54 -05005799 if (buflen == 0)
5800 buflen = server->rsize;
5801
5802 npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
5803 pages = kmalloc_array(npages, sizeof(struct page *), GFP_NOFS);
5804 if (!pages)
5805 return -ENOMEM;
5806
5807 args.acl_pages = pages;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005808
Andy Adamsonbf118a32011-12-07 11:55:27 -05005809 for (i = 0; i < npages; i++) {
5810 pages[i] = alloc_page(GFP_KERNEL);
5811 if (!pages[i])
5812 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005813 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01005814
5815 /* for decoding across pages */
5816 res.acl_scratch = alloc_page(GFP_KERNEL);
5817 if (!res.acl_scratch)
5818 goto out_free;
5819
Andy Adamsonbf118a32011-12-07 11:55:27 -05005820 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005821
Peng Taode040be2012-01-10 22:42:47 +08005822 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05005823 __func__, buf, buflen, npages, args.acl_len);
5824 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5825 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005826 if (ret)
5827 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05005828
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005829 /* Handle the case where the passed-in buffer is too short */
5830 if (res.acl_flags & NFS4_ACL_TRUNC) {
5831 /* Did the user only issue a request for the acl length? */
5832 if (buf == NULL)
5833 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005834 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005835 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005836 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005837 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005838 if (buf) {
5839 if (res.acl_len > buflen) {
5840 ret = -ERANGE;
5841 goto out_free;
5842 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005843 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005844 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005845out_ok:
5846 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005847out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05005848 for (i = 0; i < npages; i++)
5849 if (pages[i])
5850 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05005851 if (res.acl_scratch)
5852 __free_page(res.acl_scratch);
Olga Kornievskaia62a15732020-01-02 17:09:54 -05005853 kfree(pages);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005854 return ret;
5855}
5856
Trond Myklebust16b42892006-08-24 12:27:15 -04005857static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5858{
Trond Myklebust0688e642019-04-07 13:59:09 -04005859 struct nfs4_exception exception = {
5860 .interruptible = true,
5861 };
Trond Myklebust16b42892006-08-24 12:27:15 -04005862 ssize_t ret;
5863 do {
5864 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04005865 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b42892006-08-24 12:27:15 -04005866 if (ret >= 0)
5867 break;
5868 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5869 } while (exception.retry);
5870 return ret;
5871}
5872
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005873static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5874{
5875 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005876 int ret;
5877
5878 if (!nfs4_server_supports_acls(server))
5879 return -EOPNOTSUPP;
Trond Myklebust1f3208b2021-03-25 11:04:34 -04005880 ret = nfs_revalidate_inode(inode, NFS_INO_INVALID_CHANGE);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005881 if (ret < 0)
5882 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005883 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5884 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005885 ret = nfs4_read_cached_acl(inode, buf, buflen);
5886 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05005887 /* -ENOENT is returned if there is no ACL or if there is an ACL
5888 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005889 return ret;
5890 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005891}
5892
Trond Myklebust16b42892006-08-24 12:27:15 -04005893static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005894{
5895 struct nfs_server *server = NFS_SERVER(inode);
5896 struct page *pages[NFS4ACL_MAXPAGES];
5897 struct nfs_setaclargs arg = {
5898 .fh = NFS_FH(inode),
5899 .acl_pages = pages,
5900 .acl_len = buflen,
5901 };
Benny Halevy73c403a2009-04-01 09:22:01 -04005902 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005903 struct rpc_message msg = {
5904 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5905 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04005906 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005907 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005908 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05005909 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005910
J. Bruce Fields4f8be1f2021-01-28 17:36:38 -05005911 /* You can't remove system.nfs4_acl: */
5912 if (buflen == 0)
5913 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005914 if (!nfs4_server_supports_acls(server))
5915 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005916 if (npages > ARRAY_SIZE(pages))
5917 return -ERANGE;
Frank van der Lindenccde1e92020-06-23 22:39:01 +00005918 i = nfs4_buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05005919 if (i < 0)
5920 return i;
Trond Myklebustc01d3642018-03-20 16:43:20 -04005921 nfs4_inode_make_writeable(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005922 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05005923
5924 /*
5925 * Free each page after tx, so the only ref left is
5926 * held by the network stack
5927 */
5928 for (; i > 0; i--)
5929 put_page(pages[i-1]);
5930
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005931 /*
5932 * Acl update can result in inode attribute update.
5933 * so mark the attribute cache invalid.
5934 */
5935 spin_lock(&inode->i_lock);
Trond Myklebustb6f80a22021-03-08 14:42:55 -05005936 nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE |
5937 NFS_INO_INVALID_CTIME |
5938 NFS_INO_REVAL_FORCED);
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005939 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04005940 nfs_access_zap_cache(inode);
5941 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005942 return ret;
5943}
5944
Trond Myklebust16b42892006-08-24 12:27:15 -04005945static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5946{
5947 struct nfs4_exception exception = { };
5948 int err;
5949 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005950 err = __nfs4_proc_set_acl(inode, buf, buflen);
5951 trace_nfs4_set_acl(inode, err);
Dai Ngof8849e22021-05-19 17:15:10 -04005952 if (err == -NFS4ERR_BADOWNER || err == -NFS4ERR_BADNAME) {
5953 /*
5954 * no need to retry since the kernel
5955 * isn't involved in encoding the ACEs.
5956 */
5957 err = -EINVAL;
5958 break;
5959 }
Trond Myklebustc1578b72013-08-12 16:58:42 -04005960 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b42892006-08-24 12:27:15 -04005961 &exception);
5962 } while (exception.retry);
5963 return err;
5964}
5965
David Quigleyaa9c2662013-05-22 12:50:44 -04005966#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5967static int _nfs4_get_security_label(struct inode *inode, void *buf,
5968 size_t buflen)
5969{
5970 struct nfs_server *server = NFS_SERVER(inode);
David Quigleyaa9c2662013-05-22 12:50:44 -04005971 struct nfs4_label label = {0, 0, buflen, buf};
5972
5973 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Anna Schumaker2ef61e02021-10-22 13:11:07 -04005974 struct nfs_fattr fattr = {
5975 .label = &label,
5976 };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005977 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005978 .fh = NFS_FH(inode),
5979 .bitmask = bitmask,
5980 };
5981 struct nfs4_getattr_res res = {
5982 .fattr = &fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04005983 .server = server,
5984 };
5985 struct rpc_message msg = {
5986 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005987 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005988 .rpc_resp = &res,
5989 };
5990 int ret;
5991
5992 nfs_fattr_init(&fattr);
5993
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005994 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04005995 if (ret)
5996 return ret;
5997 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5998 return -ENOENT;
Ondrej Mosnacek53cb2452021-01-15 18:43:56 +01005999 return label.len;
David Quigleyaa9c2662013-05-22 12:50:44 -04006000}
6001
6002static int nfs4_get_security_label(struct inode *inode, void *buf,
6003 size_t buflen)
6004{
Trond Myklebust0688e642019-04-07 13:59:09 -04006005 struct nfs4_exception exception = {
6006 .interruptible = true,
6007 };
David Quigleyaa9c2662013-05-22 12:50:44 -04006008 int err;
6009
6010 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
6011 return -EOPNOTSUPP;
6012
6013 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04006014 err = _nfs4_get_security_label(inode, buf, buflen);
6015 trace_nfs4_get_security_label(inode, err);
6016 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04006017 &exception);
6018 } while (exception.retry);
6019 return err;
6020}
6021
6022static int _nfs4_do_set_security_label(struct inode *inode,
6023 struct nfs4_label *ilabel,
6024 struct nfs_fattr *fattr,
6025 struct nfs4_label *olabel)
6026{
6027
6028 struct iattr sattr = {0};
6029 struct nfs_server *server = NFS_SERVER(inode);
6030 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04006031 struct nfs_setattrargs arg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05006032 .fh = NFS_FH(inode),
6033 .iap = &sattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04006034 .server = server,
6035 .bitmask = bitmask,
6036 .label = ilabel,
6037 };
6038 struct nfs_setattrres res = {
6039 .fattr = fattr,
6040 .label = olabel,
6041 .server = server,
6042 };
6043 struct rpc_message msg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05006044 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
6045 .rpc_argp = &arg,
6046 .rpc_resp = &res,
David Quigleyaa9c2662013-05-22 12:50:44 -04006047 };
6048 int status;
6049
Jeff Layton12207f62013-11-01 10:49:32 -04006050 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04006051
Jeff Layton12207f62013-11-01 10:49:32 -04006052 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04006053 if (status)
6054 dprintk("%s failed: %d\n", __func__, status);
6055
6056 return status;
6057}
6058
6059static int nfs4_do_set_security_label(struct inode *inode,
6060 struct nfs4_label *ilabel,
6061 struct nfs_fattr *fattr,
6062 struct nfs4_label *olabel)
6063{
6064 struct nfs4_exception exception = { };
6065 int err;
6066
6067 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04006068 err = _nfs4_do_set_security_label(inode, ilabel,
6069 fattr, olabel);
6070 trace_nfs4_set_security_label(inode, err);
6071 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04006072 &exception);
6073 } while (exception.retry);
6074 return err;
6075}
6076
6077static int
Al Viro59301222016-05-27 10:19:30 -04006078nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04006079{
6080 struct nfs4_label ilabel, *olabel = NULL;
6081 struct nfs_fattr fattr;
David Quigleyaa9c2662013-05-22 12:50:44 -04006082 int status;
6083
6084 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
6085 return -EOPNOTSUPP;
6086
6087 nfs_fattr_init(&fattr);
6088
6089 ilabel.pi = 0;
6090 ilabel.lfs = 0;
6091 ilabel.label = (char *)buf;
6092 ilabel.len = buflen;
6093
David Quigleyaa9c2662013-05-22 12:50:44 -04006094 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
6095 if (IS_ERR(olabel)) {
6096 status = -PTR_ERR(olabel);
6097 goto out;
6098 }
6099
6100 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
6101 if (status == 0)
6102 nfs_setsecurity(inode, &fattr, olabel);
6103
6104 nfs4_label_free(olabel);
6105out:
David Quigleyaa9c2662013-05-22 12:50:44 -04006106 return status;
6107}
6108#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
6109
6110
Chuck Leverf0920752012-05-21 22:45:41 -04006111static void nfs4_init_boot_verifier(const struct nfs_client *clp,
6112 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05006113{
6114 __be32 verf[2];
6115
Chuck Lever2c820d92012-05-21 22:45:33 -04006116 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
6117 /* An impossible timestamp guarantees this value
6118 * will never match a generated boot time. */
Deepa Dinamani2f86e092016-10-01 16:46:26 -07006119 verf[0] = cpu_to_be32(U32_MAX);
6120 verf[1] = cpu_to_be32(U32_MAX);
Chuck Lever2c820d92012-05-21 22:45:33 -04006121 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04006122 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Deepa Dinamani2f86e092016-10-01 16:46:26 -07006123 u64 ns = ktime_to_ns(nn->boot_time);
6124
6125 verf[0] = cpu_to_be32(ns >> 32);
6126 verf[1] = cpu_to_be32(ns);
Chuck Lever2c820d92012-05-21 22:45:33 -04006127 }
Chuck Levercd937102012-03-02 17:14:31 -05006128 memcpy(bootverf->data, verf, sizeof(bootverf->data));
6129}
6130
Trond Myklebust1aee5512020-10-07 18:24:17 -04006131static size_t
Trond Myklebust39d43d12020-10-07 18:24:18 -04006132nfs4_get_uniquifier(struct nfs_client *clp, char *buf, size_t buflen)
Trond Myklebust1aee5512020-10-07 18:24:17 -04006133{
Trond Myklebust39d43d12020-10-07 18:24:18 -04006134 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
6135 struct nfs_netns_client *nn_clp = nn->nfs_client;
6136 const char *id;
6137
Trond Myklebust1aee5512020-10-07 18:24:17 -04006138 buf[0] = '\0';
6139
Trond Myklebust39d43d12020-10-07 18:24:18 -04006140 if (nn_clp) {
6141 rcu_read_lock();
6142 id = rcu_dereference(nn_clp->identifier);
6143 if (id)
6144 strscpy(buf, id, buflen);
6145 rcu_read_unlock();
6146 }
6147
6148 if (nfs4_client_id_uniquifier[0] != '\0' && buf[0] == '\0')
Trond Myklebust1aee5512020-10-07 18:24:17 -04006149 strscpy(buf, nfs4_client_id_uniquifier, buflen);
6150
6151 return strlen(buf);
6152}
6153
Jeff Laytona3192682015-06-09 19:43:59 -04006154static int
6155nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04006156{
Trond Myklebust1aee5512020-10-07 18:24:17 -04006157 char buf[NFS4_CLIENT_ID_UNIQ_LEN];
6158 size_t buflen;
Jeff Laytona3192682015-06-09 19:43:59 -04006159 size_t len;
6160 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04006161
Trond Myklebustceb3a162015-01-03 15:16:04 -05006162 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04006163 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08006164
Jeff Laytona3192682015-06-09 19:43:59 -04006165 rcu_read_lock();
Chuck Lever848a4eb2018-06-04 10:53:29 -04006166 len = 14 +
6167 strlen(clp->cl_rpcclient->cl_nodename) +
6168 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04006169 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
Jeff Laytona3192682015-06-09 19:43:59 -04006170 1;
6171 rcu_read_unlock();
Trond Myklebust1aee5512020-10-07 18:24:17 -04006172
Trond Myklebust39d43d12020-10-07 18:24:18 -04006173 buflen = nfs4_get_uniquifier(clp, buf, sizeof(buf));
Trond Myklebust1aee5512020-10-07 18:24:17 -04006174 if (buflen)
6175 len += buflen + 1;
6176
Jeff Laytona3192682015-06-09 19:43:59 -04006177 if (len > NFS4_OPAQUE_LIMIT + 1)
6178 return -EINVAL;
6179
6180 /*
6181 * Since this string is allocated at mount time, and held until the
6182 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6183 * about a memory-reclaim deadlock.
6184 */
6185 str = kmalloc(len, GFP_KERNEL);
6186 if (!str)
6187 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05006188
Chuck Levere984a552012-09-14 17:24:21 -04006189 rcu_read_lock();
Trond Myklebust1aee5512020-10-07 18:24:17 -04006190 if (buflen)
Chuck Lever025bb9f2018-06-04 10:53:34 -04006191 scnprintf(str, len, "Linux NFSv4.0 %s/%s/%s",
Trond Myklebust1aee5512020-10-07 18:24:17 -04006192 clp->cl_rpcclient->cl_nodename, buf,
Chuck Lever848a4eb2018-06-04 10:53:29 -04006193 rpc_peeraddr2str(clp->cl_rpcclient,
Chuck Lever025bb9f2018-06-04 10:53:34 -04006194 RPC_DISPLAY_ADDR));
Chuck Lever848a4eb2018-06-04 10:53:29 -04006195 else
Chuck Lever025bb9f2018-06-04 10:53:34 -04006196 scnprintf(str, len, "Linux NFSv4.0 %s/%s",
Chuck Lever848a4eb2018-06-04 10:53:29 -04006197 clp->cl_rpcclient->cl_nodename,
6198 rpc_peeraddr2str(clp->cl_rpcclient,
Chuck Lever025bb9f2018-06-04 10:53:34 -04006199 RPC_DISPLAY_ADDR));
Chuck Levere984a552012-09-14 17:24:21 -04006200 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04006201
Jeff Laytona3192682015-06-09 19:43:59 -04006202 clp->cl_owner_id = str;
6203 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04006204}
6205
Jeff Layton873e3852015-06-09 19:44:00 -04006206static int
Jeff Layton873e3852015-06-09 19:44:00 -04006207nfs4_init_uniform_client_string(struct nfs_client *clp)
6208{
Trond Myklebust1aee5512020-10-07 18:24:17 -04006209 char buf[NFS4_CLIENT_ID_UNIQ_LEN];
6210 size_t buflen;
Jeff Layton873e3852015-06-09 19:44:00 -04006211 size_t len;
6212 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05006213
6214 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04006215 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04006216
Jeff Layton873e3852015-06-09 19:44:00 -04006217 len = 10 + 10 + 1 + 10 + 1 +
6218 strlen(clp->cl_rpcclient->cl_nodename) + 1;
6219
Trond Myklebust39d43d12020-10-07 18:24:18 -04006220 buflen = nfs4_get_uniquifier(clp, buf, sizeof(buf));
Trond Myklebust1aee5512020-10-07 18:24:17 -04006221 if (buflen)
6222 len += buflen + 1;
6223
Jeff Layton873e3852015-06-09 19:44:00 -04006224 if (len > NFS4_OPAQUE_LIMIT + 1)
6225 return -EINVAL;
6226
6227 /*
6228 * Since this string is allocated at mount time, and held until the
6229 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6230 * about a memory-reclaim deadlock.
6231 */
6232 str = kmalloc(len, GFP_KERNEL);
6233 if (!str)
6234 return -ENOMEM;
6235
Trond Myklebust1aee5512020-10-07 18:24:17 -04006236 if (buflen)
6237 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
6238 clp->rpc_ops->version, clp->cl_minorversion,
6239 buf, clp->cl_rpcclient->cl_nodename);
6240 else
6241 scnprintf(str, len, "Linux NFSv%u.%u %s",
6242 clp->rpc_ops->version, clp->cl_minorversion,
6243 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04006244 clp->cl_owner_id = str;
6245 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04006246}
6247
Chuck Lever706cb8d2014-03-12 12:51:47 -04006248/*
6249 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
6250 * services. Advertise one based on the address family of the
6251 * clientaddr.
6252 */
6253static unsigned int
6254nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
6255{
6256 if (strchr(clp->cl_ipaddr, ':') != NULL)
6257 return scnprintf(buf, len, "tcp6");
6258 else
6259 return scnprintf(buf, len, "tcp");
6260}
6261
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006262static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
6263{
6264 struct nfs4_setclientid *sc = calldata;
6265
6266 if (task->tk_status == 0)
6267 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
6268}
6269
6270static const struct rpc_call_ops nfs4_setclientid_ops = {
6271 .rpc_call_done = nfs4_setclientid_done,
6272};
6273
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006274/**
6275 * nfs4_proc_setclientid - Negotiate client ID
6276 * @clp: state data structure
6277 * @program: RPC program for NFSv4 callback service
6278 * @port: IP port number for NFS4 callback service
NeilBrowna52458b2018-12-03 11:30:31 +11006279 * @cred: credential to use for this call
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006280 * @res: where to place the result
6281 *
6282 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6283 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006284int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
NeilBrowna52458b2018-12-03 11:30:31 +11006285 unsigned short port, const struct cred *cred,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006286 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006287{
6288 nfs4_verifier sc_verifier;
6289 struct nfs4_setclientid setclientid = {
6290 .sc_verifier = &sc_verifier,
6291 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04006292 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006293 };
6294 struct rpc_message msg = {
6295 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
6296 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006297 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01006298 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006299 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006300 struct rpc_task_setup task_setup_data = {
6301 .rpc_client = clp->cl_rpcclient,
6302 .rpc_message = &msg,
6303 .callback_ops = &nfs4_setclientid_ops,
6304 .callback_data = &setclientid,
NeilBrown5a0c2572019-05-30 10:41:28 +10006305 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006306 };
Robert Milkowski7dc29932020-01-30 09:43:25 +00006307 unsigned long now = jiffies;
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006308 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006309
Chuck Leverde734832012-07-11 16:30:50 -04006310 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04006311 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04006312
6313 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
6314 status = nfs4_init_uniform_client_string(clp);
6315 else
Jeff Laytona3192682015-06-09 19:43:59 -04006316 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04006317
6318 if (status)
6319 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04006320
Chuck Leverde734832012-07-11 16:30:50 -04006321 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04006322 setclientid.sc_netid_len =
6323 nfs4_init_callback_netid(clp,
6324 setclientid.sc_netid,
6325 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04006326 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05006327 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006328 clp->cl_ipaddr, port >> 8, port & 255);
6329
Jeff Layton3a6bb732015-06-09 19:43:57 -04006330 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006331 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04006332 clp->cl_owner_id);
Anna Schumakerdae40962019-08-14 15:28:28 -04006333
6334 status = nfs4_call_sync_custom(&task_setup_data);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006335 if (setclientid.sc_cred) {
Chuck Lever1047ec82019-10-04 09:58:54 -04006336 kfree(clp->cl_acceptor);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006337 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
6338 put_rpccred(setclientid.sc_cred);
6339 }
Robert Milkowski7dc29932020-01-30 09:43:25 +00006340
6341 if (status == 0)
6342 do_renew_lease(clp, now);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006343out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006344 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006345 dprintk("NFS reply setclientid: %d\n", status);
6346 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006347}
6348
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006349/**
6350 * nfs4_proc_setclientid_confirm - Confirm client ID
6351 * @clp: state data structure
Trond Myklebust302fad72019-02-18 13:32:38 -05006352 * @arg: result of a previous SETCLIENTID
NeilBrowna52458b2018-12-03 11:30:31 +11006353 * @cred: credential to use for this call
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006354 *
6355 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6356 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04006357int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006358 struct nfs4_setclientid_res *arg,
NeilBrowna52458b2018-12-03 11:30:31 +11006359 const struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006360{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006361 struct rpc_message msg = {
6362 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006363 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01006364 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006365 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006366 int status;
6367
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006368 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
6369 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6370 clp->cl_clientid);
NeilBrown5a0c2572019-05-30 10:41:28 +10006371 status = rpc_call_sync(clp->cl_rpcclient, &msg,
6372 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006373 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006374 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006375 return status;
6376}
6377
Trond Myklebustfe650402006-01-03 09:55:18 +01006378struct nfs4_delegreturndata {
6379 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01006380 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01006381 struct nfs_fh fh;
6382 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006383 unsigned long timestamp;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006384 struct {
6385 struct nfs4_layoutreturn_args arg;
6386 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04006387 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006388 u32 roc_barrier;
6389 bool roc;
6390 } lr;
Trond Myklebustfa178f22006-01-03 09:55:38 +01006391 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01006392 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08006393 struct inode *inode;
Trond Myklebustfe650402006-01-03 09:55:18 +01006394};
6395
Trond Myklebustfe650402006-01-03 09:55:18 +01006396static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
6397{
6398 struct nfs4_delegreturndata *data = calldata;
Trond Myklebuste0dba012017-11-07 11:02:32 -05006399 struct nfs4_exception exception = {
6400 .inode = data->inode,
6401 .stateid = &data->stateid,
Trond Myklebustdfe1fe72021-06-01 11:10:05 -04006402 .task_is_privileged = data->args.seq_args.sa_privileged,
Trond Myklebuste0dba012017-11-07 11:02:32 -05006403 };
Andy Adamson938e1012009-04-01 09:22:28 -04006404
Trond Myklebust14516c32010-07-31 14:29:06 -04006405 if (!nfs4_sequence_done(task, &data->res.seq_res))
6406 return;
Andy Adamson938e1012009-04-01 09:22:28 -04006407
Trond Myklebustca8acf82013-08-13 10:36:56 -04006408 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Trond Myklebust586f1c32016-11-15 15:03:33 -05006409
6410 /* Handle Layoutreturn errors */
Trond Myklebust078000d2021-01-04 13:18:03 -05006411 if (pnfs_roc_done(task, &data->args.lr_args, &data->res.lr_res,
6412 &data->res.lr_ret) == -EAGAIN)
Trond Myklebust287a9c52019-09-20 07:23:41 -04006413 goto out_restart;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006414
Ricardo Labiaga79708862009-12-07 09:23:21 -05006415 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05006416 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01006417 renew_lease(data->res.server, data->timestamp);
Trond Myklebust23ea44c2016-11-10 16:06:28 -05006418 break;
Trond Myklebustc97cf602013-11-19 16:34:14 -05006419 case -NFS4ERR_ADMIN_REVOKED:
6420 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust26d36302016-09-22 13:39:05 -04006421 case -NFS4ERR_EXPIRED:
6422 nfs4_free_revoked_stateid(data->res.server,
6423 data->args.stateid,
6424 task->tk_msg.rpc_cred);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05006425 fallthrough;
Trond Myklebustc97cf602013-11-19 16:34:14 -05006426 case -NFS4ERR_BAD_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05006427 case -NFS4ERR_STALE_STATEID:
Trond Myklebust244fcd22019-12-20 10:43:37 -05006428 case -ETIMEDOUT:
Trond Myklebustc97cf602013-11-19 16:34:14 -05006429 task->tk_status = 0;
6430 break;
Trond Myklebust12f275c2017-11-06 15:28:05 -05006431 case -NFS4ERR_OLD_STATEID:
Trond Myklebust246afc0a2019-10-24 18:00:35 -04006432 if (!nfs4_refresh_delegation_stateid(&data->stateid, data->inode))
6433 nfs4_stateid_seqid_inc(&data->stateid);
Trond Myklebust70d136b2019-10-26 22:37:40 -04006434 if (data->args.bitmask) {
6435 data->args.bitmask = NULL;
6436 data->res.fattr = NULL;
6437 }
Trond Myklebust246afc0a2019-10-24 18:00:35 -04006438 goto out_restart;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05006439 case -NFS4ERR_ACCESS:
6440 if (data->args.bitmask) {
6441 data->args.bitmask = NULL;
6442 data->res.fattr = NULL;
Trond Myklebust140087fd2017-11-06 15:28:10 -05006443 goto out_restart;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05006444 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05006445 fallthrough;
Ricardo Labiaga79708862009-12-07 09:23:21 -05006446 default:
Trond Myklebuste0dba012017-11-07 11:02:32 -05006447 task->tk_status = nfs4_async_handle_exception(task,
6448 data->res.server, task->tk_status,
6449 &exception);
6450 if (exception.retry)
Trond Myklebust140087fd2017-11-06 15:28:10 -05006451 goto out_restart;
Ricardo Labiaga79708862009-12-07 09:23:21 -05006452 }
Trond Myklebustd51f91d2019-10-21 14:22:14 -04006453 nfs_delegation_mark_returned(data->inode, data->args.stateid);
Ricardo Labiaga79708862009-12-07 09:23:21 -05006454 data->rpc_status = task->tk_status;
Trond Myklebust140087fd2017-11-06 15:28:10 -05006455 return;
Trond Myklebust140087fd2017-11-06 15:28:10 -05006456out_restart:
6457 task->tk_status = 0;
6458 rpc_restart_call_prepare(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01006459}
6460
6461static void nfs4_delegreturn_release(void *calldata)
6462{
Peng Tao039b7562014-07-03 13:05:02 +08006463 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006464 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08006465
Trond Myklebust078000d2021-01-04 13:18:03 -05006466 if (data->lr.roc)
6467 pnfs_roc_release(&data->lr.arg, &data->lr.res,
6468 data->res.lr_ret);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006469 if (inode) {
Trond Myklebust0bc2c9b2016-12-16 19:48:09 -05006470 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006471 nfs_iput_and_deactive(inode);
6472 }
Trond Myklebustfe650402006-01-03 09:55:18 +01006473 kfree(calldata);
6474}
6475
Andy Adamson938e1012009-04-01 09:22:28 -04006476static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
6477{
6478 struct nfs4_delegreturndata *d_data;
Trond Myklebustc8bf7072018-06-15 16:31:02 -04006479 struct pnfs_layout_hdr *lo;
Andy Adamson938e1012009-04-01 09:22:28 -04006480
6481 d_data = (struct nfs4_delegreturndata *)data;
6482
Trond Myklebust5326de92019-11-13 09:39:36 +01006483 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task)) {
6484 nfs4_sequence_done(task, &d_data->res.seq_res);
Peng Tao500d7012015-09-22 11:35:22 +08006485 return;
Trond Myklebust5326de92019-11-13 09:39:36 +01006486 }
Peng Tao500d7012015-09-22 11:35:22 +08006487
Trond Myklebustc8bf7072018-06-15 16:31:02 -04006488 lo = d_data->args.lr_args ? d_data->args.lr_args->layout : NULL;
6489 if (lo && !pnfs_layout_is_valid(lo)) {
6490 d_data->args.lr_args = NULL;
6491 d_data->res.lr_res = NULL;
6492 }
6493
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006494 nfs4_setup_sequence(d_data->res.server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006495 &d_data->args.seq_args,
6496 &d_data->res.seq_res,
6497 task);
Andy Adamson938e1012009-04-01 09:22:28 -04006498}
Andy Adamson938e1012009-04-01 09:22:28 -04006499
Jesper Juhlc8d149f2006-03-20 13:44:07 -05006500static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04006501 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01006502 .rpc_call_done = nfs4_delegreturn_done,
6503 .rpc_release = nfs4_delegreturn_release,
6504};
6505
NeilBrowna52458b2018-12-03 11:30:31 +11006506static int _nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01006507{
6508 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01006509 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01006510 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006511 struct rpc_message msg = {
6512 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
6513 .rpc_cred = cred,
6514 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006515 struct rpc_task_setup task_setup_data = {
6516 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04006517 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006518 .callback_ops = &nfs4_delegreturn_ops,
Olga Kornievskaia85e39fe2021-06-23 23:28:51 -04006519 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT | RPC_TASK_MOVEABLE,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006520 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05006521 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01006522
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006523 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01006524 if (data == NULL)
6525 return -ENOMEM;
Andrew Elble99ade3c2015-12-02 09:39:51 -05006526
6527 nfs4_state_protect(server->nfs_client,
6528 NFS_SP4_MACH_CRED_CLEANUP,
6529 &task_setup_data.rpc_client, &msg);
6530
Trond Myklebustfe650402006-01-03 09:55:18 +01006531 data->args.fhandle = &data->fh;
6532 data->args.stateid = &data->stateid;
Trond Myklebust332d1a02021-03-25 18:15:36 -04006533 nfs4_bitmask_set(data->args.bitmask_store,
6534 server->cache_consistency_bitmask, inode, server,
6535 NULL);
6536 data->args.bitmask = data->args.bitmask_store;
Trond Myklebustfe650402006-01-03 09:55:18 +01006537 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05006538 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01006539 data->res.fattr = &data->fattr;
6540 data->res.server = server;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006541 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust4d796d72016-09-23 11:38:08 -04006542 data->lr.arg.ld_private = &data->lr.ld_private;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006543 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01006544 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01006545 data->rpc_status = 0;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006546 data->inode = nfs_igrab_and_active(inode);
Trond Myklebust078000d2021-01-04 13:18:03 -05006547 if (data->inode || issync) {
6548 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res,
6549 cred);
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006550 if (data->lr.roc) {
6551 data->args.lr_args = &data->lr.arg;
6552 data->res.lr_res = &data->lr.res;
6553 }
6554 }
Trond Myklebustfe650402006-01-03 09:55:18 +01006555
Trond Myklebustdfe1fe72021-06-01 11:10:05 -04006556 if (!data->inode)
6557 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1,
6558 1);
6559 else
6560 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1,
6561 0);
Trond Myklebustc970aa82007-07-14 15:39:59 -04006562 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05006563 msg.rpc_argp = &data->args;
6564 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006565 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006566 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01006567 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05006568 if (!issync)
6569 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05006570 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05006571 if (status != 0)
6572 goto out;
6573 status = data->rpc_status;
Trond Myklebuste6f81072008-01-24 18:14:34 -05006574out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006575 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01006576 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006577}
6578
NeilBrowna52458b2018-12-03 11:30:31 +11006579int nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006580{
6581 struct nfs_server *server = NFS_SERVER(inode);
6582 struct nfs4_exception exception = { };
6583 int err;
6584 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05006585 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006586 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006587 switch (err) {
6588 case -NFS4ERR_STALE_STATEID:
6589 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006590 case 0:
6591 return 0;
6592 }
6593 err = nfs4_handle_exception(server, err, &exception);
6594 } while (exception.retry);
6595 return err;
6596}
6597
Linus Torvalds1da177e2005-04-16 15:20:36 -07006598static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6599{
6600 struct inode *inode = state->inode;
6601 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04006602 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006603 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006604 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006605 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006606 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006607 struct nfs_lockt_res res = {
6608 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006609 };
6610 struct rpc_message msg = {
6611 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
Anna Schumakerd9b67e12017-01-11 15:04:25 -05006612 .rpc_argp = &arg,
6613 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006614 .rpc_cred = state->owner->so_cred,
6615 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006616 struct nfs4_lock_state *lsp;
6617 int status;
6618
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006619 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00006620 status = nfs4_set_lock_state(state, request);
6621 if (status != 0)
6622 goto out;
6623 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006624 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006625 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00006626 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006627 switch (status) {
6628 case 0:
6629 request->fl_type = F_UNLCK;
6630 break;
6631 case -NFS4ERR_DENIED:
6632 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006633 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05006634 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04006635 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00006636out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006637 return status;
6638}
6639
6640static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6641{
Trond Myklebust0688e642019-04-07 13:59:09 -04006642 struct nfs4_exception exception = {
6643 .interruptible = true,
6644 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006645 int err;
6646
6647 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006648 err = _nfs4_proc_getlk(state, cmd, request);
6649 trace_nfs4_get_lock(request, state, cmd, err);
6650 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006651 &exception);
6652 } while (exception.retry);
6653 return err;
6654}
6655
Trond Myklebust32c6e7e2019-09-20 07:23:48 -04006656/*
6657 * Update the seqid of a lock stateid after receiving
6658 * NFS4ERR_OLD_STATEID
6659 */
6660static bool nfs4_refresh_lock_old_stateid(nfs4_stateid *dst,
6661 struct nfs4_lock_state *lsp)
6662{
6663 struct nfs4_state *state = lsp->ls_state;
6664 bool ret = false;
6665
6666 spin_lock(&state->state_lock);
6667 if (!nfs4_stateid_match_other(dst, &lsp->ls_stateid))
6668 goto out;
6669 if (!nfs4_stateid_is_newer(&lsp->ls_stateid, dst))
6670 nfs4_stateid_seqid_inc(dst);
6671 else
6672 dst->seqid = lsp->ls_stateid.seqid;
6673 ret = true;
6674out:
6675 spin_unlock(&state->state_lock);
6676 return ret;
6677}
6678
6679static bool nfs4_sync_lock_stateid(nfs4_stateid *dst,
6680 struct nfs4_lock_state *lsp)
6681{
6682 struct nfs4_state *state = lsp->ls_state;
6683 bool ret;
6684
6685 spin_lock(&state->state_lock);
6686 ret = !nfs4_stateid_match_other(dst, &lsp->ls_stateid);
6687 nfs4_stateid_copy(dst, &lsp->ls_stateid);
6688 spin_unlock(&state->state_lock);
6689 return ret;
6690}
6691
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006692struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006693 struct nfs_locku_args arg;
6694 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006695 struct nfs4_lock_state *lsp;
6696 struct nfs_open_context *ctx;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006697 struct nfs_lock_context *l_ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006698 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04006699 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006700 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006701};
6702
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006703static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
6704 struct nfs_open_context *ctx,
6705 struct nfs4_lock_state *lsp,
6706 struct nfs_seqid *seqid)
6707{
6708 struct nfs4_unlockdata *p;
Trond Myklebust32c6e7e2019-09-20 07:23:48 -04006709 struct nfs4_state *state = lsp->ls_state;
6710 struct inode *inode = state->inode;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006711
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006712 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006713 if (p == NULL)
6714 return NULL;
6715 p->arg.fh = NFS_FH(inode);
6716 p->arg.fl = &p->fl;
6717 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006718 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006719 p->lsp = lsp;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006720 /* Ensure we don't close file until we're done freeing locks! */
6721 p->ctx = get_nfs_open_context(ctx);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006722 p->l_ctx = nfs_get_lock_context(ctx);
NeilBrown7b587e12018-11-30 10:04:08 +11006723 locks_init_lock(&p->fl);
6724 locks_copy_lock(&p->fl, fl);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006725 p->server = NFS_SERVER(inode);
Trond Myklebust32c6e7e2019-09-20 07:23:48 -04006726 spin_lock(&state->state_lock);
6727 nfs4_stateid_copy(&p->arg.stateid, &lsp->ls_stateid);
6728 spin_unlock(&state->state_lock);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006729 return p;
6730}
6731
Trond Myklebust06f814a2006-01-03 09:55:07 +01006732static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006733{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006734 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006735 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006736 nfs4_put_lock_state(calldata->lsp);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006737 nfs_put_lock_context(calldata->l_ctx);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006738 put_nfs_open_context(calldata->ctx);
6739 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006740}
6741
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006742static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006743{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006744 struct nfs4_unlockdata *calldata = data;
Trond Myklebust82571552017-11-07 11:14:49 -05006745 struct nfs4_exception exception = {
6746 .inode = calldata->lsp->ls_state->inode,
6747 .stateid = &calldata->arg.stateid,
6748 };
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006749
Trond Myklebust14516c32010-07-31 14:29:06 -04006750 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
6751 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006752 switch (task->tk_status) {
6753 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01006754 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton75575dd2016-09-17 18:17:32 -04006755 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006756 if (nfs4_update_lock_stateid(calldata->lsp,
6757 &calldata->res.stateid))
6758 break;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05006759 fallthrough;
Trond Myklebust26d36302016-09-22 13:39:05 -04006760 case -NFS4ERR_ADMIN_REVOKED:
6761 case -NFS4ERR_EXPIRED:
6762 nfs4_free_revoked_stateid(calldata->server,
6763 &calldata->arg.stateid,
6764 task->tk_msg.rpc_cred);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05006765 fallthrough;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05006766 case -NFS4ERR_BAD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006767 case -NFS4ERR_STALE_STATEID:
Trond Myklebust32c6e7e2019-09-20 07:23:48 -04006768 if (nfs4_sync_lock_stateid(&calldata->arg.stateid,
6769 calldata->lsp))
6770 rpc_restart_call_prepare(task);
6771 break;
6772 case -NFS4ERR_OLD_STATEID:
6773 if (nfs4_refresh_lock_old_stateid(&calldata->arg.stateid,
6774 calldata->lsp))
Trond Myklebust425c1d42015-01-24 14:57:53 -05006775 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006776 break;
6777 default:
Trond Myklebust82571552017-11-07 11:14:49 -05006778 task->tk_status = nfs4_async_handle_exception(task,
6779 calldata->server, task->tk_status,
6780 &exception);
6781 if (exception.retry)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006782 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006783 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04006784 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006785}
6786
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006787static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006788{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006789 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006790
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006791 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
6792 nfs_async_iocounter_wait(task, calldata->l_ctx))
6793 return;
6794
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006795 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006796 goto out_wait;
Trond Myklebust795a88c2012-09-10 13:26:49 -04006797 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006798 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006799 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006800 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006801 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006802 if (nfs4_setup_sequence(calldata->server->nfs_client,
Andy Adamsona8936932009-04-01 09:22:23 -04006803 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006804 &calldata->res.seq_res,
6805 task) != 0)
6806 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006807 return;
6808out_no_action:
6809 task->tk_action = NULL;
6810out_wait:
6811 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006812}
6813
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006814static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006815 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006816 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01006817 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006818};
6819
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006820static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6821 struct nfs_open_context *ctx,
6822 struct nfs4_lock_state *lsp,
6823 struct nfs_seqid *seqid)
6824{
6825 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006826 struct rpc_message msg = {
6827 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6828 .rpc_cred = ctx->cred,
6829 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006830 struct rpc_task_setup task_setup_data = {
6831 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006832 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006833 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006834 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006835 .flags = RPC_TASK_ASYNC,
6836 };
Olga Kornievskaia85e39fe2021-06-23 23:28:51 -04006837 struct nfs_client *client =
6838 NFS_SERVER(lsp->ls_state->inode)->nfs_client;
6839
6840 if (client->cl_minorversion)
6841 task_setup_data.flags |= RPC_TASK_MOVEABLE;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006842
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006843 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6844 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6845
Frank Filz137d6ac2007-07-09 15:32:29 -07006846 /* Ensure this is an unlock - when canceling a lock, the
6847 * canceled lock is passed in, and it won't be an unlock.
6848 */
6849 fl->fl_type = F_UNLCK;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006850 if (fl->fl_flags & FL_CLOSE)
6851 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
Frank Filz137d6ac2007-07-09 15:32:29 -07006852
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006853 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6854 if (data == NULL) {
6855 nfs_free_seqid(seqid);
6856 return ERR_PTR(-ENOMEM);
6857 }
6858
Anna Schumakerfba83f32018-05-04 16:22:50 -04006859 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1, 0);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006860 msg.rpc_argp = &data->arg;
6861 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006862 task_setup_data.callback_data = data;
6863 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006864}
6865
Linus Torvalds1da177e2005-04-16 15:20:36 -07006866static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6867{
Trond Myklebust65b62a22013-02-07 10:54:07 -05006868 struct inode *inode = state->inode;
6869 struct nfs4_state_owner *sp = state->owner;
6870 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006871 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006872 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01006873 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006874 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006875 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006876 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006877
Trond Myklebust9b073572006-06-29 16:38:34 -04006878 status = nfs4_set_lock_state(state, request);
6879 /* Unlock _before_ we do the RPC call */
6880 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05006881 /* Exclude nfs_delegation_claim_locks() */
6882 mutex_lock(&sp->so_delegreturn_mutex);
6883 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006884 down_read(&nfsi->rwsem);
Jeff Layton75575dd2016-09-17 18:17:32 -04006885 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05006886 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006887 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006888 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05006889 }
6890 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006891 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006892 if (status != 0)
6893 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006894 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006895 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04006896 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6897 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006898 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6899 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04006900 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006901 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04006902 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006903 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006904 status = PTR_ERR(task);
6905 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04006906 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05006907 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006908 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04006909out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006910 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006911 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006912 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006913}
6914
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006915struct nfs4_lockdata {
6916 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006917 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006918 struct nfs4_lock_state *lsp;
6919 struct nfs_open_context *ctx;
6920 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006921 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006922 int rpc_status;
6923 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04006924 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006925};
6926
6927static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006928 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6929 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006930{
6931 struct nfs4_lockdata *p;
6932 struct inode *inode = lsp->ls_state->inode;
6933 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05006934 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006935
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006936 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006937 if (p == NULL)
6938 return NULL;
6939
6940 p->arg.fh = NFS_FH(inode);
6941 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006942 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006943 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006944 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006945 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6946 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006947 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006948 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04006949 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006950 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006951 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006952 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006953 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04006954 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006955 p->ctx = get_nfs_open_context(ctx);
NeilBrown7b587e12018-11-30 10:04:08 +11006956 locks_init_lock(&p->fl);
6957 locks_copy_lock(&p->fl, fl);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006958 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006959out_free_seqid:
6960 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006961out_free:
6962 kfree(p);
6963 return NULL;
6964}
6965
6966static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6967{
6968 struct nfs4_lockdata *data = calldata;
6969 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006970
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006971 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006972 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006973 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05006974 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006975 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006976 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006977 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05006978 nfs4_stateid_copy(&data->arg.open_stateid,
6979 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006980 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006981 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006982 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006983 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006984 nfs4_stateid_copy(&data->arg.lock_stateid,
6985 &data->lsp->ls_stateid);
6986 }
Trond Myklebust5d422302013-03-14 16:57:48 -04006987 if (!nfs4_valid_open_stateid(state)) {
6988 data->rpc_status = -EBADF;
6989 task->tk_action = NULL;
6990 goto out_release_open_seqid;
6991 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006992 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006993 if (nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust035168ab2010-06-16 09:52:26 -04006994 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006995 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006996 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04006997 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04006998out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006999 nfs_release_seqid(data->arg.open_seqid);
7000out_release_lock_seqid:
7001 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05007002out_wait:
7003 nfs4_sequence_done(task, &data->res.seq_res);
Chuck Lever86882c72021-10-16 18:03:04 -04007004 dprintk("%s: ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08007005}
7006
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007007static void nfs4_lock_done(struct rpc_task *task, void *calldata)
7008{
7009 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05007010 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007011
Trond Myklebust14516c32010-07-31 14:29:06 -04007012 if (!nfs4_sequence_done(task, &data->res.seq_res))
7013 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04007014
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007015 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05007016 switch (task->tk_status) {
7017 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00007018 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05007019 data->timestamp);
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04007020 if (data->arg.new_lock && !data->cancelled) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05007021 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04007022 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0)
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04007023 goto out_restart;
Trond Myklebustc69899a2015-01-24 16:03:52 -05007024 }
Trond Myklebust39071e62015-01-24 15:07:56 -05007025 if (data->arg.new_lock_owner != 0) {
7026 nfs_confirm_seqid(&lsp->ls_seqid, 0);
7027 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
7028 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04007029 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
7030 goto out_restart;
Trond Myklebust425c1d42015-01-24 14:57:53 -05007031 break;
7032 case -NFS4ERR_BAD_STATEID:
7033 case -NFS4ERR_OLD_STATEID:
7034 case -NFS4ERR_STALE_STATEID:
7035 case -NFS4ERR_EXPIRED:
7036 if (data->arg.new_lock_owner != 0) {
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04007037 if (!nfs4_stateid_match(&data->arg.open_stateid,
Trond Myklebust425c1d42015-01-24 14:57:53 -05007038 &lsp->ls_state->open_stateid))
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04007039 goto out_restart;
7040 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
Trond Myklebust425c1d42015-01-24 14:57:53 -05007041 &lsp->ls_stateid))
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04007042 goto out_restart;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007043 }
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04007044out_done:
Chuck Lever86882c72021-10-16 18:03:04 -04007045 dprintk("%s: ret = %d!\n", __func__, data->rpc_status);
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04007046 return;
7047out_restart:
7048 if (!data->cancelled)
7049 rpc_restart_call_prepare(task);
7050 goto out_done;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007051}
7052
7053static void nfs4_lock_release(void *calldata)
7054{
7055 struct nfs4_lockdata *data = calldata;
7056
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05007057 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04007058 if (data->cancelled && data->rpc_status == 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007059 struct rpc_task *task;
7060 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
7061 data->arg.lock_seqid);
7062 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08007063 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07007064 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007065 } else
7066 nfs_free_seqid(data->arg.lock_seqid);
7067 nfs4_put_lock_state(data->lsp);
7068 put_nfs_open_context(data->ctx);
7069 kfree(data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007070}
7071
7072static const struct rpc_call_ops nfs4_lock_ops = {
7073 .rpc_call_prepare = nfs4_lock_prepare,
7074 .rpc_call_done = nfs4_lock_done,
7075 .rpc_release = nfs4_lock_release,
7076};
7077
Trond Myklebust2bee72a2010-01-26 15:42:21 -05007078static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
7079{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05007080 switch (error) {
7081 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustd7f3e4b2016-09-22 13:39:09 -04007082 case -NFS4ERR_EXPIRED:
Trond Myklebust2bee72a2010-01-26 15:42:21 -05007083 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05007084 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05007085 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04007086 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05007087 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05007088 break;
7089 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05007090 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05007091 nfs4_schedule_lease_recovery(server->nfs_client);
zhengbin8b98a532019-12-19 18:34:47 +08007092 }
Trond Myklebust2bee72a2010-01-26 15:42:21 -05007093}
7094
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08007095static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007096{
7097 struct nfs4_lockdata *data;
7098 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04007099 struct rpc_message msg = {
7100 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
7101 .rpc_cred = state->owner->so_cred,
7102 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04007103 struct rpc_task_setup task_setup_data = {
7104 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04007105 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04007106 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05007107 .workqueue = nfsiod_workqueue,
Trond Myklebust61296502020-02-07 19:38:12 -05007108 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
Trond Myklebustc970aa82007-07-14 15:39:59 -04007109 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007110 int ret;
Olga Kornievskaia85e39fe2021-06-23 23:28:51 -04007111 struct nfs_client *client = NFS_SERVER(state->inode)->nfs_client;
7112
7113 if (client->cl_minorversion)
7114 task_setup_data.flags |= RPC_TASK_MOVEABLE;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007115
Trond Myklebustcd3758e2007-08-10 17:44:32 -04007116 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04007117 fl->fl_u.nfs4_fl.owner,
7118 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007119 if (data == NULL)
7120 return -ENOMEM;
7121 if (IS_SETLKW(cmd))
7122 data->arg.block = 1;
Anna Schumakerfba83f32018-05-04 16:22:50 -04007123 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1,
7124 recovery_type > NFS_LOCK_NEW);
Trond Myklebust1174dd12010-12-21 10:52:24 -05007125 msg.rpc_argp = &data->arg;
7126 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04007127 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007128 if (recovery_type > NFS_LOCK_NEW) {
7129 if (recovery_type == NFS_LOCK_RECLAIM)
7130 data->arg.reclaim = NFS_LOCK_RECLAIM;
Trond Myklebustc69899a2015-01-24 16:03:52 -05007131 } else
7132 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04007133 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05007134 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007135 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05007136 ret = rpc_wait_for_completion_task(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007137 if (ret == 0) {
7138 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05007139 if (ret)
7140 nfs4_handle_setlk_error(data->server, data->lsp,
7141 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007142 } else
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04007143 data->cancelled = true;
Dave Wysochanski3d1a90a2020-12-11 05:12:51 -05007144 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05007145 rpc_put_task(task);
Chuck Lever86882c72021-10-16 18:03:04 -04007146 dprintk("%s: ret = %d\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007147 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007148}
7149
7150static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
7151{
Trond Myklebust202b50d2005-06-22 17:16:29 +00007152 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04007153 struct nfs4_exception exception = {
7154 .inode = state->inode,
7155 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00007156 int err;
7157
7158 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04007159 /* Cache the lock if possible... */
7160 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
7161 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08007162 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04007163 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00007164 break;
7165 nfs4_handle_exception(server, err, &exception);
7166 } while (exception.retry);
7167 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007168}
7169
7170static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
7171{
Trond Myklebust202b50d2005-06-22 17:16:29 +00007172 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04007173 struct nfs4_exception exception = {
7174 .inode = state->inode,
7175 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00007176 int err;
7177
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05007178 err = nfs4_set_lock_state(state, request);
7179 if (err != 0)
7180 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04007181 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10007182 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
7183 return 0;
7184 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00007185 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04007186 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
7187 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08007188 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05007189 switch (err) {
7190 default:
7191 goto out;
7192 case -NFS4ERR_GRACE:
7193 case -NFS4ERR_DELAY:
7194 nfs4_handle_exception(server, err, &exception);
7195 err = 0;
7196 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00007197 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05007198out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00007199 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007200}
7201
Bryan Schumakerf062eb62011-06-02 14:59:10 -04007202#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05007203static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
7204{
Trond Myklebustc5896fc2016-09-22 13:39:03 -04007205 struct nfs4_lock_state *lsp;
7206 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05007207
Trond Myklebustc5896fc2016-09-22 13:39:03 -04007208 status = nfs4_set_lock_state(state, request);
7209 if (status != 0)
7210 return status;
7211 lsp = request->fl_u.nfs4_fl.owner;
7212 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
7213 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
7214 return 0;
Anna Schumaker81b68de2017-01-11 16:41:34 -05007215 return nfs4_lock_expired(state, request);
Bryan Schumakerf062eb62011-06-02 14:59:10 -04007216}
7217#endif
7218
Linus Torvalds1da177e2005-04-16 15:20:36 -07007219static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7220{
Trond Myklebust19e03c52008-12-23 15:21:44 -05007221 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04007222 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04007223 unsigned char fl_flags = request->fl_flags;
Jeff Layton1ea67db2016-09-17 18:17:37 -04007224 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007225
Trond Myklebust01c3b862006-06-29 16:38:39 -04007226 request->fl_flags |= FL_ACCESS;
Jeff Layton75575dd2016-09-17 18:17:32 -04007227 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04007228 if (status < 0)
7229 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04007230 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05007231 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04007232 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04007233 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04007234 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05007235 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton75575dd2016-09-17 18:17:32 -04007236 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05007237 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04007238 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05007239 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04007240 }
Trond Myklebust9a99af42013-02-04 20:17:49 -05007241 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04007242 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08007243 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04007244out:
7245 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007246 return status;
7247}
7248
7249static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7250{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05007251 struct nfs4_exception exception = {
7252 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04007253 .inode = state->inode,
Trond Myklebust0688e642019-04-07 13:59:09 -04007254 .interruptible = true,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05007255 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07007256 int err;
7257
7258 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07007259 err = _nfs4_proc_setlk(state, cmd, request);
7260 if (err == -NFS4ERR_DENIED)
7261 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007262 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07007263 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007264 } while (exception.retry);
7265 return err;
7266}
7267
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04007268#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
7269#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
7270
7271static int
Jeff Laytona1d617d82016-09-17 18:17:39 -04007272nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
7273 struct file_lock *request)
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04007274{
7275 int status = -ERESTARTSYS;
7276 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
7277
7278 while(!signalled()) {
7279 status = nfs4_proc_setlk(state, cmd, request);
7280 if ((status != -EAGAIN) || IS_SETLK(cmd))
7281 break;
7282 freezable_schedule_timeout_interruptible(timeout);
7283 timeout *= 2;
7284 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
7285 status = -ERESTARTSYS;
7286 }
7287 return status;
7288}
7289
Jeff Laytona1d617d82016-09-17 18:17:39 -04007290#ifdef CONFIG_NFS_V4_1
7291struct nfs4_lock_waiter {
Jeff Laytona1d617d82016-09-17 18:17:39 -04007292 struct inode *inode;
Trond Myklebust86438182021-02-02 14:22:51 -05007293 struct nfs_lowner owner;
7294 wait_queue_entry_t wait;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007295};
7296
7297static int
Ingo Molnarac6424b2017-06-20 12:06:13 +02007298nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
Jeff Laytona1d617d82016-09-17 18:17:39 -04007299{
Trond Myklebust86438182021-02-02 14:22:51 -05007300 struct nfs4_lock_waiter *waiter =
7301 container_of(wait, struct nfs4_lock_waiter, wait);
Jeff Laytona1d617d82016-09-17 18:17:39 -04007302
Jeff Layton57174592018-03-18 08:37:03 -04007303 /* NULL key means to wake up everyone */
7304 if (key) {
7305 struct cb_notify_lock_args *cbnl = key;
7306 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
Trond Myklebust86438182021-02-02 14:22:51 -05007307 *wowner = &waiter->owner;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007308
Jeff Layton57174592018-03-18 08:37:03 -04007309 /* Only wake if the callback was for the same owner. */
7310 if (lowner->id != wowner->id || lowner->s_dev != wowner->s_dev)
7311 return 0;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007312
Jeff Layton57174592018-03-18 08:37:03 -04007313 /* Make sure it's for the right inode */
7314 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
7315 return 0;
Jeff Layton57174592018-03-18 08:37:03 -04007316 }
Jeff Laytona1d617d82016-09-17 18:17:39 -04007317
Trond Myklebust86438182021-02-02 14:22:51 -05007318 return woken_wake_function(wait, mode, flags, key);
Jeff Laytona1d617d82016-09-17 18:17:39 -04007319}
7320
7321static int
7322nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7323{
Jeff Laytona1d617d82016-09-17 18:17:39 -04007324 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
7325 struct nfs_server *server = NFS_SERVER(state->inode);
7326 struct nfs_client *clp = server->nfs_client;
7327 wait_queue_head_t *q = &clp->cl_lock_waitq;
Trond Myklebust86438182021-02-02 14:22:51 -05007328 struct nfs4_lock_waiter waiter = {
7329 .inode = state->inode,
7330 .owner = { .clientid = clp->cl_clientid,
7331 .id = lsp->ls_seqid.owner_id,
7332 .s_dev = server->s_dev },
7333 };
7334 int status;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007335
7336 /* Don't bother with waitqueue if we don't expect a callback */
7337 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
7338 return nfs4_retry_setlk_simple(state, cmd, request);
7339
Trond Myklebust86438182021-02-02 14:22:51 -05007340 init_wait(&waiter.wait);
7341 waiter.wait.func = nfs4_wake_lock_waiter;
7342 add_wait_queue(q, &waiter.wait);
Jeff Laytona1d617d82016-09-17 18:17:39 -04007343
Trond Myklebust86438182021-02-02 14:22:51 -05007344 do {
Jeff Laytona1d617d82016-09-17 18:17:39 -04007345 status = nfs4_proc_setlk(state, cmd, request);
Trond Myklebust86438182021-02-02 14:22:51 -05007346 if (status != -EAGAIN || IS_SETLK(cmd))
Jeff Laytona1d617d82016-09-17 18:17:39 -04007347 break;
7348
7349 status = -ERESTARTSYS;
Yihao Wu52b042a2019-05-22 01:57:10 +08007350 freezer_do_not_count();
Trond Myklebust86438182021-02-02 14:22:51 -05007351 wait_woken(&waiter.wait, TASK_INTERRUPTIBLE,
7352 NFS4_LOCK_MAXTIMEOUT);
Yihao Wu52b042a2019-05-22 01:57:10 +08007353 freezer_count();
Trond Myklebust86438182021-02-02 14:22:51 -05007354 } while (!signalled());
7355
7356 remove_wait_queue(q, &waiter.wait);
Jeff Laytona1d617d82016-09-17 18:17:39 -04007357
Jeff Laytona1d617d82016-09-17 18:17:39 -04007358 return status;
7359}
7360#else /* !CONFIG_NFS_V4_1 */
7361static inline int
7362nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7363{
7364 return nfs4_retry_setlk_simple(state, cmd, request);
7365}
7366#endif
7367
Linus Torvalds1da177e2005-04-16 15:20:36 -07007368static int
7369nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
7370{
7371 struct nfs_open_context *ctx;
7372 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007373 int status;
7374
7375 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04007376 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007377 state = ctx->state;
7378
Trond Myklebustd9531262009-07-21 19:22:38 -04007379 if (IS_GETLK(cmd)) {
7380 if (state != NULL)
7381 return nfs4_proc_getlk(state, F_GETLK, request);
7382 return 0;
7383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007384
7385 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
7386 return -EINVAL;
7387
Trond Myklebustd9531262009-07-21 19:22:38 -04007388 if (request->fl_type == F_UNLCK) {
7389 if (state != NULL)
7390 return nfs4_proc_unlck(state, cmd, request);
7391 return 0;
7392 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007393
Trond Myklebustd9531262009-07-21 19:22:38 -04007394 if (state == NULL)
7395 return -ENOLCK;
Jeff Layton1ea67db2016-09-17 18:17:37 -04007396
7397 if ((request->fl_flags & FL_POSIX) &&
7398 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
7399 return -ENOLCK;
7400
Benjamin Coddingtonfcfa4472017-11-10 06:27:49 -05007401 /*
7402 * Don't rely on the VFS having checked the file open mode,
7403 * since it won't do this for flock() locks.
7404 */
7405 switch (request->fl_type) {
7406 case F_RDLCK:
7407 if (!(filp->f_mode & FMODE_READ))
7408 return -EBADF;
7409 break;
7410 case F_WRLCK:
7411 if (!(filp->f_mode & FMODE_WRITE))
7412 return -EBADF;
7413 }
7414
Jeff Layton1ea67db2016-09-17 18:17:37 -04007415 status = nfs4_set_lock_state(state, request);
7416 if (status != 0)
7417 return status;
7418
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04007419 return nfs4_retry_setlk(state, cmd, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007420}
7421
Trond Myklebuste93a5e92021-05-07 10:06:13 -04007422static int nfs4_delete_lease(struct file *file, void **priv)
7423{
7424 return generic_setlease(file, F_UNLCK, NULL, priv);
7425}
7426
7427static int nfs4_add_lease(struct file *file, long arg, struct file_lock **lease,
7428 void **priv)
7429{
7430 struct inode *inode = file_inode(file);
7431 fmode_t type = arg == F_RDLCK ? FMODE_READ : FMODE_WRITE;
7432 int ret;
7433
7434 /* No delegation, no lease */
7435 if (!nfs4_have_delegation(inode, type))
Trond Myklebustdf2c7b92021-06-25 15:08:39 -04007436 return -EAGAIN;
Trond Myklebuste93a5e92021-05-07 10:06:13 -04007437 ret = generic_setlease(file, arg, lease, priv);
7438 if (ret || nfs4_have_delegation(inode, type))
7439 return ret;
7440 /* We raced with a delegation return */
7441 nfs4_delete_lease(file, priv);
Trond Myklebustdf2c7b92021-06-25 15:08:39 -04007442 return -EAGAIN;
Trond Myklebuste93a5e92021-05-07 10:06:13 -04007443}
7444
7445int nfs4_proc_setlease(struct file *file, long arg, struct file_lock **lease,
7446 void **priv)
7447{
7448 switch (arg) {
7449 case F_RDLCK:
7450 case F_WRLCK:
7451 return nfs4_add_lease(file, arg, lease, priv);
7452 case F_UNLCK:
7453 return nfs4_delete_lease(file, priv);
7454 default:
7455 return -EINVAL;
7456 }
7457}
7458
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04007459int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05007460{
7461 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05007462 int err;
7463
7464 err = nfs4_set_lock_state(state, fl);
7465 if (err != 0)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04007466 return err;
Olga Kornievskaia3d7a9522020-08-20 18:52:43 -04007467 do {
7468 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
7469 if (err != -NFS4ERR_DELAY)
7470 break;
7471 ssleep(1);
7472 } while (err == -NFS4ERR_DELAY);
NeilBrowndce26302017-12-13 09:57:09 +11007473 return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05007474}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007475
Trond Myklebustcf470c32012-03-07 13:49:12 -05007476struct nfs_release_lockowner_data {
7477 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04007478 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007479 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08007480 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04007481 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007482};
7483
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007484static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
7485{
7486 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08007487 struct nfs_server *server = data->server;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05007488 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
7489 &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08007490 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04007491 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007492}
7493
7494static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
7495{
7496 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04007497 struct nfs_server *server = data->server;
7498
Trond Myklebustb7e63a12014-02-26 11:19:14 -08007499 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04007500
7501 switch (task->tk_status) {
7502 case 0:
7503 renew_lease(server, data->timestamp);
7504 break;
7505 case -NFS4ERR_STALE_CLIENTID:
7506 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08007507 nfs4_schedule_lease_recovery(server->nfs_client);
7508 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04007509 case -NFS4ERR_LEASE_MOVED:
7510 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10007511 if (nfs4_async_handle_error(task, server,
7512 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04007513 rpc_restart_call_prepare(task);
7514 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007515}
7516
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007517static void nfs4_release_lockowner_release(void *calldata)
7518{
Trond Myklebustcf470c32012-03-07 13:49:12 -05007519 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04007520 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007521 kfree(calldata);
7522}
7523
Trond Myklebust17280172012-03-11 13:11:00 -04007524static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007525 .rpc_call_prepare = nfs4_release_lockowner_prepare,
7526 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007527 .rpc_release = nfs4_release_lockowner_release,
7528};
7529
Jeff Laytonf1cdae82014-05-01 06:28:47 -04007530static void
7531nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007532{
Trond Myklebustcf470c32012-03-07 13:49:12 -05007533 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007534 struct rpc_message msg = {
7535 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
7536 };
7537
7538 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04007539 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007540
Trond Myklebustcf470c32012-03-07 13:49:12 -05007541 data = kmalloc(sizeof(*data), GFP_NOFS);
7542 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04007543 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007544 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04007545 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007546 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7547 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
7548 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007549
Trond Myklebustcf470c32012-03-07 13:49:12 -05007550 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08007551 msg.rpc_resp = &data->res;
Anna Schumakerfba83f32018-05-04 16:22:50 -04007552 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05007553 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007554}
7555
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00007556#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
7557
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007558static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Christian Braunere65ce2a2021-01-21 14:19:27 +01007559 struct user_namespace *mnt_userns,
Al Viro59301222016-05-27 10:19:30 -04007560 struct dentry *unused, struct inode *inode,
7561 const char *key, const void *buf,
7562 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007563{
Al Viro59301222016-05-27 10:19:30 -04007564 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007565}
7566
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007567static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04007568 struct dentry *unused, struct inode *inode,
7569 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007570{
Al Virob2968212016-04-10 20:48:24 -04007571 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007572}
7573
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01007574static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007575{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01007576 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007577}
7578
David Quigleyc9bccef2013-05-22 12:50:45 -04007579#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04007580
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007581static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Christian Braunere65ce2a2021-01-21 14:19:27 +01007582 struct user_namespace *mnt_userns,
Al Viro59301222016-05-27 10:19:30 -04007583 struct dentry *unused, struct inode *inode,
7584 const char *key, const void *buf,
7585 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04007586{
7587 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04007588 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04007589
7590 return -EOPNOTSUPP;
7591}
7592
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007593static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04007594 struct dentry *unused, struct inode *inode,
7595 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04007596{
7597 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04007598 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04007599 return -EOPNOTSUPP;
7600}
7601
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007602static ssize_t
7603nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04007604{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007605 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04007606
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007607 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
7608 len = security_inode_listsecurity(inode, list, list_len);
Chengguang Xu82c596e2020-06-17 09:09:39 +08007609 if (len >= 0 && list_len && len > list_len)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007610 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04007611 }
7612 return len;
7613}
7614
7615static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
7616 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04007617 .get = nfs4_xattr_get_nfs4_label,
7618 .set = nfs4_xattr_set_nfs4_label,
7619};
David Quigleyc9bccef2013-05-22 12:50:45 -04007620
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007621#else
7622
7623static ssize_t
7624nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7625{
7626 return 0;
7627}
7628
7629#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04007630
Frank van der Linden012a2112020-06-23 22:39:03 +00007631#ifdef CONFIG_NFS_V4_2
7632static int nfs4_xattr_set_nfs4_user(const struct xattr_handler *handler,
Christian Braunere65ce2a2021-01-21 14:19:27 +01007633 struct user_namespace *mnt_userns,
Frank van der Linden012a2112020-06-23 22:39:03 +00007634 struct dentry *unused, struct inode *inode,
7635 const char *key, const void *buf,
7636 size_t buflen, int flags)
7637{
7638 struct nfs_access_entry cache;
Frank van der Linden95ad37f2020-06-23 22:39:04 +00007639 int ret;
Frank van der Linden012a2112020-06-23 22:39:03 +00007640
7641 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7642 return -EOPNOTSUPP;
7643
7644 /*
7645 * There is no mapping from the MAY_* flags to the NFS_ACCESS_XA*
7646 * flags right now. Handling of xattr operations use the normal
7647 * file read/write permissions.
7648 *
7649 * Just in case the server has other ideas (which RFC 8276 allows),
7650 * do a cached access check for the XA* flags to possibly avoid
7651 * doing an RPC and getting EACCES back.
7652 */
7653 if (!nfs_access_get_cached(inode, current_cred(), &cache, true)) {
7654 if (!(cache.mask & NFS_ACCESS_XAWRITE))
7655 return -EACCES;
7656 }
7657
Frank van der Linden95ad37f2020-06-23 22:39:04 +00007658 if (buf == NULL) {
7659 ret = nfs42_proc_removexattr(inode, key);
7660 if (!ret)
7661 nfs4_xattr_cache_remove(inode, key);
7662 } else {
7663 ret = nfs42_proc_setxattr(inode, key, buf, buflen, flags);
7664 if (!ret)
7665 nfs4_xattr_cache_add(inode, key, buf, NULL, buflen);
7666 }
7667
7668 return ret;
Frank van der Linden012a2112020-06-23 22:39:03 +00007669}
7670
7671static int nfs4_xattr_get_nfs4_user(const struct xattr_handler *handler,
7672 struct dentry *unused, struct inode *inode,
7673 const char *key, void *buf, size_t buflen)
7674{
7675 struct nfs_access_entry cache;
Frank van der Linden95ad37f2020-06-23 22:39:04 +00007676 ssize_t ret;
Frank van der Linden012a2112020-06-23 22:39:03 +00007677
7678 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7679 return -EOPNOTSUPP;
7680
7681 if (!nfs_access_get_cached(inode, current_cred(), &cache, true)) {
7682 if (!(cache.mask & NFS_ACCESS_XAREAD))
7683 return -EACCES;
7684 }
7685
Trond Myklebust1f3208b2021-03-25 11:04:34 -04007686 ret = nfs_revalidate_inode(inode, NFS_INO_INVALID_CHANGE);
Frank van der Linden95ad37f2020-06-23 22:39:04 +00007687 if (ret)
7688 return ret;
7689
7690 ret = nfs4_xattr_cache_get(inode, key, buf, buflen);
7691 if (ret >= 0 || (ret < 0 && ret != -ENOENT))
7692 return ret;
7693
7694 ret = nfs42_proc_getxattr(inode, key, buf, buflen);
7695
7696 return ret;
Frank van der Linden012a2112020-06-23 22:39:03 +00007697}
7698
7699static ssize_t
7700nfs4_listxattr_nfs4_user(struct inode *inode, char *list, size_t list_len)
7701{
7702 u64 cookie;
7703 bool eof;
Frank van der Linden95ad37f2020-06-23 22:39:04 +00007704 ssize_t ret, size;
Frank van der Linden012a2112020-06-23 22:39:03 +00007705 char *buf;
7706 size_t buflen;
7707 struct nfs_access_entry cache;
7708
7709 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7710 return 0;
7711
7712 if (!nfs_access_get_cached(inode, current_cred(), &cache, true)) {
7713 if (!(cache.mask & NFS_ACCESS_XALIST))
7714 return 0;
7715 }
7716
Trond Myklebust1f3208b2021-03-25 11:04:34 -04007717 ret = nfs_revalidate_inode(inode, NFS_INO_INVALID_CHANGE);
Frank van der Linden95ad37f2020-06-23 22:39:04 +00007718 if (ret)
7719 return ret;
7720
7721 ret = nfs4_xattr_cache_list(inode, list, list_len);
7722 if (ret >= 0 || (ret < 0 && ret != -ENOENT))
7723 return ret;
7724
Frank van der Linden012a2112020-06-23 22:39:03 +00007725 cookie = 0;
7726 eof = false;
7727 buflen = list_len ? list_len : XATTR_LIST_MAX;
7728 buf = list_len ? list : NULL;
7729 size = 0;
7730
7731 while (!eof) {
7732 ret = nfs42_proc_listxattrs(inode, buf, buflen,
7733 &cookie, &eof);
7734 if (ret < 0)
7735 return ret;
7736
7737 if (list_len) {
7738 buf += ret;
7739 buflen -= ret;
7740 }
7741 size += ret;
7742 }
7743
Frank van der Linden95ad37f2020-06-23 22:39:04 +00007744 if (list_len)
7745 nfs4_xattr_cache_set_list(inode, list, size);
7746
Frank van der Linden012a2112020-06-23 22:39:03 +00007747 return size;
7748}
7749
7750#else
7751
7752static ssize_t
7753nfs4_listxattr_nfs4_user(struct inode *inode, char *list, size_t list_len)
7754{
7755 return 0;
7756}
7757#endif /* CONFIG_NFS_V4_2 */
7758
Andy Adamson533eb462011-06-13 18:25:56 -04007759/*
7760 * nfs_fhget will use either the mounted_on_fileid or the fileid
7761 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007762static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
7763{
Andy Adamson533eb462011-06-13 18:25:56 -04007764 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
7765 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
7766 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05007767 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007768 return;
7769
7770 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05007771 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007772 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
7773 fattr->nlink = 2;
7774}
7775
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007776static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7777 const struct qstr *name,
7778 struct nfs4_fs_locations *fs_locations,
7779 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04007780{
7781 struct nfs_server *server = NFS_SERVER(dir);
Chuck Leverc05cefc2017-11-05 15:45:22 -05007782 u32 bitmask[3];
Trond Myklebust683b57b2006-06-09 09:34:22 -04007783 struct nfs4_fs_locations_arg args = {
7784 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05007785 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04007786 .page = page,
7787 .bitmask = bitmask,
7788 };
Benny Halevy22958462009-04-01 09:22:02 -04007789 struct nfs4_fs_locations_res res = {
7790 .fs_locations = fs_locations,
7791 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04007792 struct rpc_message msg = {
7793 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7794 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04007795 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04007796 };
7797 int status;
7798
Harvey Harrison3110ff82008-05-02 13:42:44 -07007799 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04007800
Chuck Leverc05cefc2017-11-05 15:45:22 -05007801 bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
7802 bitmask[1] = nfs4_fattr_bitmap[1];
7803
Andy Adamson533eb462011-06-13 18:25:56 -04007804 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
7805 * is not supported */
7806 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
Chuck Leverc05cefc2017-11-05 15:45:22 -05007807 bitmask[0] &= ~FATTR4_WORD0_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04007808 else
Chuck Leverc05cefc2017-11-05 15:45:22 -05007809 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04007810
Trond Myklebustc228fd32007-01-13 02:28:11 -05007811 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04007812 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04007813 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007814 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07007815 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04007816 return status;
7817}
7818
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007819int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7820 const struct qstr *name,
7821 struct nfs4_fs_locations *fs_locations,
7822 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007823{
Trond Myklebust0688e642019-04-07 13:59:09 -04007824 struct nfs4_exception exception = {
7825 .interruptible = true,
7826 };
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007827 int err;
7828 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007829 err = _nfs4_proc_fs_locations(client, dir, name,
7830 fs_locations, page);
7831 trace_nfs4_get_fs_locations(dir, name, err);
7832 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007833 &exception);
7834 } while (exception.retry);
7835 return err;
7836}
7837
Chuck Leverb03d7352013-10-17 14:12:50 -04007838/*
7839 * This operation also signals the server that this client is
7840 * performing migration recovery. The server can stop returning
7841 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
7842 * appended to this compound to identify the client ID which is
7843 * performing recovery.
7844 */
7845static int _nfs40_proc_get_locations(struct inode *inode,
7846 struct nfs4_fs_locations *locations,
NeilBrowna52458b2018-12-03 11:30:31 +11007847 struct page *page, const struct cred *cred)
Chuck Leverb03d7352013-10-17 14:12:50 -04007848{
7849 struct nfs_server *server = NFS_SERVER(inode);
7850 struct rpc_clnt *clnt = server->client;
7851 u32 bitmask[2] = {
7852 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7853 };
7854 struct nfs4_fs_locations_arg args = {
7855 .clientid = server->nfs_client->cl_clientid,
7856 .fh = NFS_FH(inode),
7857 .page = page,
7858 .bitmask = bitmask,
7859 .migration = 1, /* skip LOOKUP */
7860 .renew = 1, /* append RENEW */
7861 };
7862 struct nfs4_fs_locations_res res = {
7863 .fs_locations = locations,
7864 .migration = 1,
7865 .renew = 1,
7866 };
7867 struct rpc_message msg = {
7868 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7869 .rpc_argp = &args,
7870 .rpc_resp = &res,
7871 .rpc_cred = cred,
7872 };
7873 unsigned long now = jiffies;
7874 int status;
7875
7876 nfs_fattr_init(&locations->fattr);
7877 locations->server = server;
7878 locations->nlocations = 0;
7879
Anna Schumakerfba83f32018-05-04 16:22:50 -04007880 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Leverb03d7352013-10-17 14:12:50 -04007881 status = nfs4_call_sync_sequence(clnt, server, &msg,
7882 &args.seq_args, &res.seq_res);
7883 if (status)
7884 return status;
7885
7886 renew_lease(server, now);
7887 return 0;
7888}
7889
7890#ifdef CONFIG_NFS_V4_1
7891
7892/*
7893 * This operation also signals the server that this client is
7894 * performing migration recovery. The server can stop asserting
7895 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
7896 * performing this operation is identified in the SEQUENCE
7897 * operation in this compound.
7898 *
7899 * When the client supports GETATTR(fs_locations_info), it can
7900 * be plumbed in here.
7901 */
7902static int _nfs41_proc_get_locations(struct inode *inode,
7903 struct nfs4_fs_locations *locations,
NeilBrowna52458b2018-12-03 11:30:31 +11007904 struct page *page, const struct cred *cred)
Chuck Leverb03d7352013-10-17 14:12:50 -04007905{
7906 struct nfs_server *server = NFS_SERVER(inode);
7907 struct rpc_clnt *clnt = server->client;
7908 u32 bitmask[2] = {
7909 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7910 };
7911 struct nfs4_fs_locations_arg args = {
7912 .fh = NFS_FH(inode),
7913 .page = page,
7914 .bitmask = bitmask,
7915 .migration = 1, /* skip LOOKUP */
7916 };
7917 struct nfs4_fs_locations_res res = {
7918 .fs_locations = locations,
7919 .migration = 1,
7920 };
7921 struct rpc_message msg = {
7922 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7923 .rpc_argp = &args,
7924 .rpc_resp = &res,
7925 .rpc_cred = cred,
7926 };
7927 int status;
7928
7929 nfs_fattr_init(&locations->fattr);
7930 locations->server = server;
7931 locations->nlocations = 0;
7932
Anna Schumakerfba83f32018-05-04 16:22:50 -04007933 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Leverb03d7352013-10-17 14:12:50 -04007934 status = nfs4_call_sync_sequence(clnt, server, &msg,
7935 &args.seq_args, &res.seq_res);
7936 if (status == NFS4_OK &&
7937 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7938 status = -NFS4ERR_LEASE_MOVED;
7939 return status;
7940}
7941
7942#endif /* CONFIG_NFS_V4_1 */
7943
7944/**
7945 * nfs4_proc_get_locations - discover locations for a migrated FSID
7946 * @inode: inode on FSID that is migrating
7947 * @locations: result of query
7948 * @page: buffer
7949 * @cred: credential to use for this operation
7950 *
7951 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
7952 * operation failed, or a negative errno if a local error occurred.
7953 *
7954 * On success, "locations" is filled in, but if the server has
7955 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
7956 * asserted.
7957 *
7958 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
7959 * from this client that require migration recovery.
7960 */
7961int nfs4_proc_get_locations(struct inode *inode,
7962 struct nfs4_fs_locations *locations,
NeilBrowna52458b2018-12-03 11:30:31 +11007963 struct page *page, const struct cred *cred)
Chuck Leverb03d7352013-10-17 14:12:50 -04007964{
7965 struct nfs_server *server = NFS_SERVER(inode);
7966 struct nfs_client *clp = server->nfs_client;
7967 const struct nfs4_mig_recovery_ops *ops =
7968 clp->cl_mvops->mig_recovery_ops;
Trond Myklebust0688e642019-04-07 13:59:09 -04007969 struct nfs4_exception exception = {
7970 .interruptible = true,
7971 };
Chuck Leverb03d7352013-10-17 14:12:50 -04007972 int status;
7973
7974 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7975 (unsigned long long)server->fsid.major,
7976 (unsigned long long)server->fsid.minor,
7977 clp->cl_hostname);
7978 nfs_display_fhandle(NFS_FH(inode), __func__);
7979
7980 do {
7981 status = ops->get_locations(inode, locations, page, cred);
7982 if (status != -NFS4ERR_DELAY)
7983 break;
7984 nfs4_handle_exception(server, status, &exception);
7985 } while (exception.retry);
7986 return status;
7987}
7988
Chuck Lever44c99932013-10-17 14:13:30 -04007989/*
7990 * This operation also signals the server that this client is
7991 * performing "lease moved" recovery. The server can stop
7992 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
7993 * is appended to this compound to identify the client ID which is
7994 * performing recovery.
7995 */
NeilBrowna52458b2018-12-03 11:30:31 +11007996static int _nfs40_proc_fsid_present(struct inode *inode, const struct cred *cred)
Chuck Lever44c99932013-10-17 14:13:30 -04007997{
7998 struct nfs_server *server = NFS_SERVER(inode);
7999 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
8000 struct rpc_clnt *clnt = server->client;
8001 struct nfs4_fsid_present_arg args = {
8002 .fh = NFS_FH(inode),
8003 .clientid = clp->cl_clientid,
8004 .renew = 1, /* append RENEW */
8005 };
8006 struct nfs4_fsid_present_res res = {
8007 .renew = 1,
8008 };
8009 struct rpc_message msg = {
8010 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
8011 .rpc_argp = &args,
8012 .rpc_resp = &res,
8013 .rpc_cred = cred,
8014 };
8015 unsigned long now = jiffies;
8016 int status;
8017
8018 res.fh = nfs_alloc_fhandle();
8019 if (res.fh == NULL)
8020 return -ENOMEM;
8021
Anna Schumakerfba83f32018-05-04 16:22:50 -04008022 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Lever44c99932013-10-17 14:13:30 -04008023 status = nfs4_call_sync_sequence(clnt, server, &msg,
8024 &args.seq_args, &res.seq_res);
8025 nfs_free_fhandle(res.fh);
8026 if (status)
8027 return status;
8028
8029 do_renew_lease(clp, now);
8030 return 0;
8031}
8032
8033#ifdef CONFIG_NFS_V4_1
8034
8035/*
8036 * This operation also signals the server that this client is
8037 * performing "lease moved" recovery. The server can stop asserting
8038 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
8039 * this operation is identified in the SEQUENCE operation in this
8040 * compound.
8041 */
NeilBrowna52458b2018-12-03 11:30:31 +11008042static int _nfs41_proc_fsid_present(struct inode *inode, const struct cred *cred)
Chuck Lever44c99932013-10-17 14:13:30 -04008043{
8044 struct nfs_server *server = NFS_SERVER(inode);
8045 struct rpc_clnt *clnt = server->client;
8046 struct nfs4_fsid_present_arg args = {
8047 .fh = NFS_FH(inode),
8048 };
8049 struct nfs4_fsid_present_res res = {
8050 };
8051 struct rpc_message msg = {
8052 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
8053 .rpc_argp = &args,
8054 .rpc_resp = &res,
8055 .rpc_cred = cred,
8056 };
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 == NFS4_OK &&
8068 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
8069 status = -NFS4ERR_LEASE_MOVED;
8070 return status;
8071}
8072
8073#endif /* CONFIG_NFS_V4_1 */
8074
8075/**
8076 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
8077 * @inode: inode on FSID to check
8078 * @cred: credential to use for this operation
8079 *
8080 * Server indicates whether the FSID is present, moved, or not
8081 * recognized. This operation is necessary to clear a LEASE_MOVED
8082 * condition for this client ID.
8083 *
8084 * Returns NFS4_OK if the FSID is present on this server,
8085 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
8086 * NFS4ERR code if some error occurred on the server, or a
8087 * negative errno if a local failure occurred.
8088 */
NeilBrowna52458b2018-12-03 11:30:31 +11008089int nfs4_proc_fsid_present(struct inode *inode, const struct cred *cred)
Chuck Lever44c99932013-10-17 14:13:30 -04008090{
8091 struct nfs_server *server = NFS_SERVER(inode);
8092 struct nfs_client *clp = server->nfs_client;
8093 const struct nfs4_mig_recovery_ops *ops =
8094 clp->cl_mvops->mig_recovery_ops;
Trond Myklebust0688e642019-04-07 13:59:09 -04008095 struct nfs4_exception exception = {
8096 .interruptible = true,
8097 };
Chuck Lever44c99932013-10-17 14:13:30 -04008098 int status;
8099
8100 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
8101 (unsigned long long)server->fsid.major,
8102 (unsigned long long)server->fsid.minor,
8103 clp->cl_hostname);
8104 nfs_display_fhandle(NFS_FH(inode), __func__);
8105
8106 do {
8107 status = ops->fsid_present(inode, cred);
8108 if (status != -NFS4ERR_DELAY)
8109 break;
8110 nfs4_handle_exception(server, status, &exception);
8111 } while (exception.retry);
8112 return status;
8113}
8114
Trond Myklebust302fad72019-02-18 13:32:38 -05008115/*
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04008116 * If 'use_integrity' is true and the state managment nfs_client
8117 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
8118 * and the machine credential as per RFC3530bis and RFC5661 Security
8119 * Considerations sections. Otherwise, just use the user cred with the
8120 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04008121 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04008122static 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 +00008123{
8124 int status;
Anna Schumaker50493362019-08-14 15:30:16 -04008125 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
8126 struct nfs_client *clp = NFS_SERVER(dir)->nfs_client;
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00008127 struct nfs4_secinfo_arg args = {
8128 .dir_fh = NFS_FH(dir),
8129 .name = name,
8130 };
8131 struct nfs4_secinfo_res res = {
8132 .flavors = flavors,
8133 };
8134 struct rpc_message msg = {
8135 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
8136 .rpc_argp = &args,
8137 .rpc_resp = &res,
8138 };
Anna Schumaker50493362019-08-14 15:30:16 -04008139 struct nfs4_call_sync_data data = {
8140 .seq_server = NFS_SERVER(dir),
8141 .seq_args = &args.seq_args,
8142 .seq_res = &res.seq_res,
8143 };
8144 struct rpc_task_setup task_setup = {
8145 .rpc_client = clnt,
8146 .rpc_message = &msg,
8147 .callback_ops = clp->cl_mvops->call_sync_ops,
8148 .callback_data = &data,
8149 .flags = RPC_TASK_NO_ROUND_ROBIN,
8150 };
NeilBrowna52458b2018-12-03 11:30:31 +11008151 const struct cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04008152
8153 if (use_integrity) {
Anna Schumaker50493362019-08-14 15:30:16 -04008154 clnt = clp->cl_rpcclient;
8155 task_setup.rpc_client = clnt;
8156
8157 cred = nfs4_get_clid_cred(clp);
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008158 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04008159 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00008160
8161 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008162
Anna Schumaker50493362019-08-14 15:30:16 -04008163 nfs4_state_protect(clp, NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
8164 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
8165 status = nfs4_call_sync_custom(&task_setup);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008166
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00008167 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04008168
NeilBrowna52458b2018-12-03 11:30:31 +11008169 put_cred(cred);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00008170 return status;
8171}
8172
Bryan Schumaker72de53e2012-04-27 13:27:40 -04008173int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
8174 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00008175{
Trond Myklebust0688e642019-04-07 13:59:09 -04008176 struct nfs4_exception exception = {
8177 .interruptible = true,
8178 };
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00008179 int err;
8180 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04008181 err = -NFS4ERR_WRONGSEC;
8182
8183 /* try to use integrity protection with machine cred */
8184 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
8185 err = _nfs4_proc_secinfo(dir, name, flavors, true);
8186
8187 /*
8188 * if unable to use integrity protection, or SECINFO with
8189 * integrity protection returns NFS4ERR_WRONGSEC (which is
8190 * disallowed by spec, but exists in deployed servers) use
8191 * the current filesystem's rpc_client and the user cred.
8192 */
8193 if (err == -NFS4ERR_WRONGSEC)
8194 err = _nfs4_proc_secinfo(dir, name, flavors, false);
8195
Trond Myklebust078ea3d2013-08-12 16:45:55 -04008196 trace_nfs4_secinfo(dir, name, err);
8197 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00008198 &exception);
8199 } while (exception.retry);
8200 return err;
8201}
8202
Andy Adamson557134a2009-04-01 09:21:53 -04008203#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04008204/*
Andy Adamson357f54d2010-12-14 10:11:57 -05008205 * Check the exchange flags returned by the server for invalid flags, having
8206 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
8207 * DS flags set.
8208 */
Olga Kornievskaia8c390762020-10-16 09:25:45 -04008209static int nfs4_check_cl_exchange_flags(u32 flags, u32 version)
Andy Adamson357f54d2010-12-14 10:11:57 -05008210{
Olga Kornievskaia8c390762020-10-16 09:25:45 -04008211 if (version >= 2 && (flags & ~EXCHGID4_2_FLAG_MASK_R))
8212 goto out_inval;
8213 else if (version < 2 && (flags & ~EXCHGID4_FLAG_MASK_R))
Andy Adamson357f54d2010-12-14 10:11:57 -05008214 goto out_inval;
8215 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
8216 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
8217 goto out_inval;
8218 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
8219 goto out_inval;
8220 return NFS_OK;
8221out_inval:
8222 return -NFS4ERR_INVAL;
8223}
8224
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04008225static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04008226nfs41_same_server_scope(struct nfs41_server_scope *a,
8227 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04008228{
Anna Schumaker49ad0142017-01-11 16:51:59 -05008229 if (a->server_scope_sz != b->server_scope_sz)
8230 return false;
8231 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04008232}
8233
Andy Adamson02a95de2016-02-05 16:08:37 -05008234static void
8235nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
8236{
Trond Myklebust5c441542019-11-13 08:34:00 +01008237 struct nfs41_bind_conn_to_session_args *args = task->tk_msg.rpc_argp;
Olga Kornievskaiadff58532020-04-24 17:45:50 -04008238 struct nfs41_bind_conn_to_session_res *res = task->tk_msg.rpc_resp;
Trond Myklebust5c441542019-11-13 08:34:00 +01008239 struct nfs_client *clp = args->client;
8240
8241 switch (task->tk_status) {
8242 case -NFS4ERR_BADSESSION:
8243 case -NFS4ERR_DEADSESSION:
8244 nfs4_schedule_session_recovery(clp->cl_session,
8245 task->tk_status);
8246 }
Olga Kornievskaiadff58532020-04-24 17:45:50 -04008247 if (args->dir == NFS4_CDFC4_FORE_OR_BOTH &&
8248 res->dir != NFS4_CDFS4_BOTH) {
8249 rpc_task_close_connection(task);
8250 if (args->retries++ < MAX_BIND_CONN_TO_SESSION_RETRIES)
8251 rpc_restart_call(task);
8252 }
Andy Adamson02a95de2016-02-05 16:08:37 -05008253}
8254
8255static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
Olga Kornievskaia1c709b72020-04-26 11:30:00 -04008256 .rpc_call_done = nfs4_bind_one_conn_to_session_done,
Andy Adamson02a95de2016-02-05 16:08:37 -05008257};
8258
Andy Adamson357f54d2010-12-14 10:11:57 -05008259/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008260 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008261 *
8262 * The 4.1 client currently uses the same TCP connection for the
8263 * fore and backchannel.
8264 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008265static
8266int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
8267 struct rpc_xprt *xprt,
8268 struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008269 const struct cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008270{
8271 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08008272 struct nfs41_bind_conn_to_session_args args = {
8273 .client = clp,
8274 .dir = NFS4_CDFC4_FORE_OR_BOTH,
Olga Kornievskaiadff58532020-04-24 17:45:50 -04008275 .retries = 0,
Trond Myklebust71a097c2015-02-18 09:27:18 -08008276 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008277 struct nfs41_bind_conn_to_session_res res;
8278 struct rpc_message msg = {
8279 .rpc_proc =
8280 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08008281 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008282 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04008283 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008284 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008285 struct rpc_task_setup task_setup_data = {
8286 .rpc_client = clnt,
8287 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05008288 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008289 .rpc_message = &msg,
8290 .flags = RPC_TASK_TIMEOUT,
8291 };
8292 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008293
Trond Myklebust71a097c2015-02-18 09:27:18 -08008294 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
8295 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
8296 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008297
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008298 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
8299 if (xprt != rcu_access_pointer(clnt->cl_xprt))
8300 args.dir = NFS4_CDFC4_FORE;
8301
8302 task = rpc_run_task(&task_setup_data);
8303 if (!IS_ERR(task)) {
8304 status = task->tk_status;
8305 rpc_put_task(task);
8306 } else
8307 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008308 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008309 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08008310 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008311 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
8312 dprintk("NFS: %s: Session ID mismatch\n", __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04008313 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008314 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08008315 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008316 dprintk("NFS: %s: Unexpected direction from server\n",
8317 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04008318 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008319 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08008320 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008321 dprintk("NFS: %s: Server returned RDMA mode = true\n",
8322 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04008323 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008324 }
8325 }
Anna Schumakerc7ae7632017-04-07 14:15:21 -04008326
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008327 return status;
8328}
8329
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008330struct rpc_bind_conn_calldata {
8331 struct nfs_client *clp;
NeilBrowna52458b2018-12-03 11:30:31 +11008332 const struct cred *cred;
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008333};
8334
8335static int
8336nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
8337 struct rpc_xprt *xprt,
8338 void *calldata)
8339{
8340 struct rpc_bind_conn_calldata *p = calldata;
8341
8342 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
8343}
8344
NeilBrowna52458b2018-12-03 11:30:31 +11008345int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, const struct cred *cred)
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008346{
8347 struct rpc_bind_conn_calldata data = {
8348 .clp = clp,
8349 .cred = cred,
8350 };
8351 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
8352 nfs4_proc_bind_conn_to_session_callback, &data);
8353}
8354
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008355/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04008356 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
8357 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04008358 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008359static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
8360 .how = SP4_MACH_CRED,
8361 .enforce.u.words = {
8362 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
8363 1 << (OP_EXCHANGE_ID - 32) |
8364 1 << (OP_CREATE_SESSION - 32) |
8365 1 << (OP_DESTROY_SESSION - 32) |
8366 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04008367 },
8368 .allow.u.words = {
8369 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05008370 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04008371 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05008372 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04008373 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008374 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008375 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05008376 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008377 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04008378 1 << (OP_FREE_STATEID - 32) |
8379 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008380 }
8381};
8382
8383/*
8384 * Select the state protection mode for client `clp' given the server results
8385 * from exchange_id in `sp'.
8386 *
8387 * Returns 0 on success, negative errno otherwise.
8388 */
8389static int nfs4_sp4_select_mode(struct nfs_client *clp,
8390 struct nfs41_state_protection *sp)
8391{
8392 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
8393 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
8394 1 << (OP_EXCHANGE_ID - 32) |
8395 1 << (OP_CREATE_SESSION - 32) |
8396 1 << (OP_DESTROY_SESSION - 32) |
8397 1 << (OP_DESTROY_CLIENTID - 32)
8398 };
Trond Myklebust937e3132017-08-01 07:32:50 -04008399 unsigned long flags = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008400 unsigned int i;
Trond Myklebust937e3132017-08-01 07:32:50 -04008401 int ret = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008402
8403 if (sp->how == SP4_MACH_CRED) {
8404 /* Print state protect result */
8405 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
8406 for (i = 0; i <= LAST_NFS4_OP; i++) {
8407 if (test_bit(i, sp->enforce.u.longs))
8408 dfprintk(MOUNT, " enforce op %d\n", i);
8409 if (test_bit(i, sp->allow.u.longs))
8410 dfprintk(MOUNT, " allow op %d\n", i);
8411 }
8412
8413 /* make sure nothing is on enforce list that isn't supported */
8414 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
8415 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
8416 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008417 ret = -EINVAL;
8418 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008419 }
8420 }
8421
8422 /*
8423 * Minimal mode - state operations are allowed to use machine
8424 * credential. Note this already happens by default, so the
8425 * client doesn't have to do anything more than the negotiation.
8426 *
8427 * NOTE: we don't care if EXCHANGE_ID is in the list -
8428 * we're already using the machine cred for exchange_id
8429 * and will never use a different cred.
8430 */
8431 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
8432 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
8433 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
8434 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
8435 dfprintk(MOUNT, "sp4_mach_cred:\n");
8436 dfprintk(MOUNT, " minimal mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008437 __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008438 } else {
8439 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008440 ret = -EINVAL;
8441 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008442 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04008443
8444 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05008445 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
8446 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04008447 test_bit(OP_LOCKU, sp->allow.u.longs)) {
8448 dfprintk(MOUNT, " cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008449 __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04008450 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008451
Andrew Elble99ade3c2015-12-02 09:39:51 -05008452 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
8453 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008454 __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
Andrew Elble99ade3c2015-12-02 09:39:51 -05008455 }
8456
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008457 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
8458 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
8459 dfprintk(MOUNT, " secinfo mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008460 __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008461 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008462
8463 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
8464 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
8465 dfprintk(MOUNT, " stateid mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008466 __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008467 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04008468
8469 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
8470 dfprintk(MOUNT, " write mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008471 __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04008472 }
8473
8474 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
8475 dfprintk(MOUNT, " commit mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008476 __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04008477 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008478 }
Trond Myklebust937e3132017-08-01 07:32:50 -04008479out:
8480 clp->cl_sp4_flags = flags;
Wei Yongjun72bf75c2018-08-02 05:42:04 +00008481 return ret;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008482}
8483
Andy Adamson8d89bd72016-09-09 09:22:18 -04008484struct nfs41_exchange_id_data {
8485 struct nfs41_exchange_id_res res;
8486 struct nfs41_exchange_id_args args;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008487};
8488
Andy Adamson8d89bd72016-09-09 09:22:18 -04008489static void nfs4_exchange_id_release(void *data)
8490{
8491 struct nfs41_exchange_id_data *cdata =
8492 (struct nfs41_exchange_id_data *)data;
8493
Olga Kornievskaia63513232017-03-13 10:36:19 -04008494 nfs_put_client(cdata->args.client);
Andy Adamson8d89bd72016-09-09 09:22:18 -04008495 kfree(cdata->res.impl_id);
8496 kfree(cdata->res.server_scope);
8497 kfree(cdata->res.server_owner);
8498 kfree(cdata);
8499}
8500
8501static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
Andy Adamson8d89bd72016-09-09 09:22:18 -04008502 .rpc_release = nfs4_exchange_id_release,
8503};
8504
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008505/*
8506 * _nfs4_proc_exchange_id()
8507 *
8508 * Wrapper for EXCHANGE_ID operation.
8509 */
Trond Myklebust9c760d12017-07-31 18:38:50 -04008510static struct rpc_task *
NeilBrowna52458b2018-12-03 11:30:31 +11008511nfs4_run_exchange_id(struct nfs_client *clp, const struct cred *cred,
Andy Adamsonad0849a2016-09-09 09:22:28 -04008512 u32 sp4_how, struct rpc_xprt *xprt)
Benny Halevy99fe60d2009-04-01 09:22:29 -04008513{
Benny Halevy99fe60d2009-04-01 09:22:29 -04008514 struct rpc_message msg = {
8515 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
Benny Halevy99fe60d2009-04-01 09:22:29 -04008516 .rpc_cred = cred,
8517 };
Andy Adamson8d89bd72016-09-09 09:22:18 -04008518 struct rpc_task_setup task_setup_data = {
8519 .rpc_client = clp->cl_rpcclient,
8520 .callback_ops = &nfs4_exchange_id_call_ops,
8521 .rpc_message = &msg,
NeilBrown5a0c2572019-05-30 10:41:28 +10008522 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
Andy Adamson8d89bd72016-09-09 09:22:18 -04008523 };
8524 struct nfs41_exchange_id_data *calldata;
Anna Schumakere917f0d2017-04-07 14:15:22 -04008525 int status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008526
Elena Reshetova212bf412017-10-20 12:53:38 +03008527 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust9c760d12017-07-31 18:38:50 -04008528 return ERR_PTR(-EIO);
Andy Adamson8d89bd72016-09-09 09:22:18 -04008529
Trond Myklebust9c760d12017-07-31 18:38:50 -04008530 status = -ENOMEM;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008531 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust9c760d12017-07-31 18:38:50 -04008532 if (!calldata)
8533 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04008534
Trond Myklebustfd405592017-08-01 16:02:47 -04008535 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04008536
8537 status = nfs4_init_uniform_client_string(clp);
8538 if (status)
Andy Adamson8d89bd72016-09-09 09:22:18 -04008539 goto out_calldata;
Jeff Layton3a6bb732015-06-09 19:43:57 -04008540
Andy Adamson8d89bd72016-09-09 09:22:18 -04008541 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
8542 GFP_NOFS);
8543 status = -ENOMEM;
8544 if (unlikely(calldata->res.server_owner == NULL))
8545 goto out_calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04008546
Andy Adamson8d89bd72016-09-09 09:22:18 -04008547 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04008548 GFP_NOFS);
Andy Adamson8d89bd72016-09-09 09:22:18 -04008549 if (unlikely(calldata->res.server_scope == NULL))
Chuck Leveracdeb692012-05-21 22:46:16 -04008550 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04008551
Andy Adamson8d89bd72016-09-09 09:22:18 -04008552 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
8553 if (unlikely(calldata->res.impl_id == NULL))
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05008554 goto out_server_scope;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05008555
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008556 switch (sp4_how) {
8557 case SP4_NONE:
Andy Adamson8d89bd72016-09-09 09:22:18 -04008558 calldata->args.state_protect.how = SP4_NONE;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008559 break;
8560
8561 case SP4_MACH_CRED:
Andy Adamson8d89bd72016-09-09 09:22:18 -04008562 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008563 break;
8564
8565 default:
8566 /* unsupported! */
8567 WARN_ON_ONCE(1);
8568 status = -EINVAL;
Kinglong Mee6b559702015-07-01 11:54:53 +08008569 goto out_impl_id;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008570 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04008571 if (xprt) {
Andy Adamsonad0849a2016-09-09 09:22:28 -04008572 task_setup_data.rpc_xprt = xprt;
Trond Myklebustd9cb7332017-08-01 16:02:48 -04008573 task_setup_data.flags |= RPC_TASK_SOFTCONN;
Trond Myklebustfd405592017-08-01 16:02:47 -04008574 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
8575 sizeof(calldata->args.verifier.data));
Andy Adamsonad0849a2016-09-09 09:22:28 -04008576 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04008577 calldata->args.client = clp;
Trond Myklebustbfab2812017-08-01 08:17:34 -04008578 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
8579 EXCHGID4_FLAG_BIND_PRINC_STATEID;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008580#ifdef CONFIG_NFS_V4_1_MIGRATION
Trond Myklebustbfab2812017-08-01 08:17:34 -04008581 calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008582#endif
8583 msg.rpc_argp = &calldata->args;
8584 msg.rpc_resp = &calldata->res;
8585 task_setup_data.callback_data = calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04008586
Trond Myklebust9c760d12017-07-31 18:38:50 -04008587 return rpc_run_task(&task_setup_data);
Andy Adamson8d89bd72016-09-09 09:22:18 -04008588
8589out_impl_id:
8590 kfree(calldata->res.impl_id);
8591out_server_scope:
8592 kfree(calldata->res.server_scope);
8593out_server_owner:
8594 kfree(calldata->res.server_owner);
8595out_calldata:
8596 kfree(calldata);
Trond Myklebust9c760d12017-07-31 18:38:50 -04008597out:
Olga Kornievskaia63513232017-03-13 10:36:19 -04008598 nfs_put_client(clp);
Trond Myklebust9c760d12017-07-31 18:38:50 -04008599 return ERR_PTR(status);
8600}
8601
8602/*
8603 * _nfs4_proc_exchange_id()
8604 *
8605 * Wrapper for EXCHANGE_ID operation.
8606 */
NeilBrowna52458b2018-12-03 11:30:31 +11008607static int _nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred,
Trond Myklebust9c760d12017-07-31 18:38:50 -04008608 u32 sp4_how)
8609{
8610 struct rpc_task *task;
8611 struct nfs41_exchange_id_args *argp;
8612 struct nfs41_exchange_id_res *resp;
Robert Milkowski7dc29932020-01-30 09:43:25 +00008613 unsigned long now = jiffies;
Trond Myklebust9c760d12017-07-31 18:38:50 -04008614 int status;
8615
8616 task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
8617 if (IS_ERR(task))
8618 return PTR_ERR(task);
8619
8620 argp = task->tk_msg.rpc_argp;
8621 resp = task->tk_msg.rpc_resp;
8622 status = task->tk_status;
8623 if (status != 0)
8624 goto out;
8625
Olga Kornievskaia8c390762020-10-16 09:25:45 -04008626 status = nfs4_check_cl_exchange_flags(resp->flags,
8627 clp->cl_mvops->minor_version);
Trond Myklebust9c760d12017-07-31 18:38:50 -04008628 if (status != 0)
8629 goto out;
8630
8631 status = nfs4_sp4_select_mode(clp, &resp->state_protect);
8632 if (status != 0)
8633 goto out;
8634
Robert Milkowski7dc29932020-01-30 09:43:25 +00008635 do_renew_lease(clp, now);
8636
Trond Myklebust9c760d12017-07-31 18:38:50 -04008637 clp->cl_clientid = resp->clientid;
8638 clp->cl_exchange_flags = resp->flags;
8639 clp->cl_seqid = resp->seqid;
8640 /* Client ID is not confirmed */
8641 if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
8642 clear_bit(NFS4_SESSION_ESTABLISHED,
8643 &clp->cl_session->session_state);
8644
8645 if (clp->cl_serverscope != NULL &&
8646 !nfs41_same_server_scope(clp->cl_serverscope,
8647 resp->server_scope)) {
8648 dprintk("%s: server_scope mismatch detected\n",
8649 __func__);
8650 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
8651 }
8652
8653 swap(clp->cl_serverowner, resp->server_owner);
8654 swap(clp->cl_serverscope, resp->server_scope);
8655 swap(clp->cl_implid, resp->impl_id);
8656
8657 /* Save the EXCHANGE_ID verifier session trunk tests */
8658 memcpy(clp->cl_confirm.data, argp->verifier.data,
8659 sizeof(clp->cl_confirm.data));
8660out:
8661 trace_nfs4_exchange_id(clp, status);
8662 rpc_put_task(task);
8663 return status;
Benny Halevy99fe60d2009-04-01 09:22:29 -04008664}
8665
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008666/*
8667 * nfs4_proc_exchange_id()
8668 *
8669 * Returns zero, a negative errno, or a negative NFS4ERR status code.
8670 *
8671 * Since the clientid has expired, all compounds using sessions
8672 * associated with the stale clientid will be returning
8673 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
8674 * be in some phase of session reset.
8675 *
8676 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
8677 */
NeilBrowna52458b2018-12-03 11:30:31 +11008678int nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008679{
8680 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
8681 int status;
8682
8683 /* try SP4_MACH_CRED if krb5i/p */
8684 if (authflavor == RPC_AUTH_GSS_KRB5I ||
8685 authflavor == RPC_AUTH_GSS_KRB5P) {
Trond Myklebust9c760d12017-07-31 18:38:50 -04008686 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008687 if (!status)
8688 return 0;
8689 }
8690
8691 /* try SP4_NONE */
Trond Myklebust9c760d12017-07-31 18:38:50 -04008692 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008693}
8694
Andy Adamson04fa2c62016-09-09 09:22:29 -04008695/**
8696 * nfs4_test_session_trunk
8697 *
8698 * This is an add_xprt_test() test function called from
8699 * rpc_clnt_setup_test_and_add_xprt.
8700 *
8701 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
8702 * and is dereferrenced in nfs4_exchange_id_release
8703 *
8704 * Upon success, add the new transport to the rpc_clnt
8705 *
8706 * @clnt: struct rpc_clnt to get new transport
8707 * @xprt: the rpc_xprt to test
8708 * @data: call data for _nfs4_proc_exchange_id.
8709 */
Santosh kumar pradhan10e037d2018-12-19 12:29:57 +05308710void nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
Andy Adamson04fa2c62016-09-09 09:22:29 -04008711 void *data)
8712{
8713 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
Trond Myklebust9c760d12017-07-31 18:38:50 -04008714 struct rpc_task *task;
8715 int status;
8716
Andy Adamson04fa2c62016-09-09 09:22:29 -04008717 u32 sp4_how;
8718
8719 dprintk("--> %s try %s\n", __func__,
8720 xprt->address_strings[RPC_DISPLAY_ADDR]);
8721
8722 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
8723
8724 /* Test connection for session trunking. Async exchange_id call */
Trond Myklebust9c760d12017-07-31 18:38:50 -04008725 task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
8726 if (IS_ERR(task))
Santosh kumar pradhan10e037d2018-12-19 12:29:57 +05308727 return;
Trond Myklebust9c760d12017-07-31 18:38:50 -04008728
8729 status = task->tk_status;
8730 if (status == 0)
8731 status = nfs4_detect_session_trunking(adata->clp,
8732 task->tk_msg.rpc_resp, xprt);
8733
Santosh kumar pradhan10e037d2018-12-19 12:29:57 +05308734 if (status == 0)
8735 rpc_clnt_xprt_switch_add_xprt(clnt, xprt);
8736
Trond Myklebust9c760d12017-07-31 18:38:50 -04008737 rpc_put_task(task);
Andy Adamson04fa2c62016-09-09 09:22:29 -04008738}
8739EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
8740
Trond Myklebust66245532012-05-25 17:18:09 -04008741static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008742 const struct cred *cred)
Trond Myklebust66245532012-05-25 17:18:09 -04008743{
8744 struct rpc_message msg = {
8745 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
8746 .rpc_argp = clp,
8747 .rpc_cred = cred,
8748 };
8749 int status;
8750
NeilBrown5a0c2572019-05-30 10:41:28 +10008751 status = rpc_call_sync(clp->cl_rpcclient, &msg,
8752 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008753 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04008754 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04008755 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04008756 "DESTROY_CLIENTID.", status, clp->cl_hostname);
8757 return status;
8758}
8759
8760static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008761 const struct cred *cred)
Trond Myklebust66245532012-05-25 17:18:09 -04008762{
8763 unsigned int loop;
8764 int ret;
8765
8766 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
8767 ret = _nfs4_proc_destroy_clientid(clp, cred);
8768 switch (ret) {
8769 case -NFS4ERR_DELAY:
8770 case -NFS4ERR_CLIENTID_BUSY:
8771 ssleep(1);
8772 break;
8773 default:
8774 return ret;
8775 }
8776 }
8777 return 0;
8778}
8779
8780int nfs4_destroy_clientid(struct nfs_client *clp)
8781{
NeilBrowna52458b2018-12-03 11:30:31 +11008782 const struct cred *cred;
Trond Myklebust66245532012-05-25 17:18:09 -04008783 int ret = 0;
8784
8785 if (clp->cl_mvops->minor_version < 1)
8786 goto out;
8787 if (clp->cl_exchange_flags == 0)
8788 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04008789 if (clp->cl_preserve_clid)
8790 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04008791 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04008792 ret = nfs4_proc_destroy_clientid(clp, cred);
NeilBrowna52458b2018-12-03 11:30:31 +11008793 put_cred(cred);
Trond Myklebust66245532012-05-25 17:18:09 -04008794 switch (ret) {
8795 case 0:
8796 case -NFS4ERR_STALE_CLIENTID:
8797 clp->cl_exchange_flags = 0;
8798 }
8799out:
8800 return ret;
8801}
8802
Donald Buczek0efb01b2019-07-07 21:26:08 +02008803#endif /* CONFIG_NFS_V4_1 */
8804
Andy Adamson2050f0c2009-04-01 09:22:30 -04008805struct nfs4_get_lease_time_data {
8806 struct nfs4_get_lease_time_args *args;
8807 struct nfs4_get_lease_time_res *res;
8808 struct nfs_client *clp;
8809};
8810
8811static void nfs4_get_lease_time_prepare(struct rpc_task *task,
8812 void *calldata)
8813{
Andy Adamson2050f0c2009-04-01 09:22:30 -04008814 struct nfs4_get_lease_time_data *data =
8815 (struct nfs4_get_lease_time_data *)calldata;
8816
Andy Adamson2050f0c2009-04-01 09:22:30 -04008817 /* just setup sequence, do not trigger session recovery
8818 since we're invoked within one */
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008819 nfs4_setup_sequence(data->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008820 &data->args->la_seq_args,
8821 &data->res->lr_seq_res,
8822 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008823}
8824
8825/*
8826 * Called from nfs4_state_manager thread for session setup, so don't recover
8827 * from sequence operation or clientid errors.
8828 */
8829static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
8830{
8831 struct nfs4_get_lease_time_data *data =
8832 (struct nfs4_get_lease_time_data *)calldata;
8833
Donald Buczek0efb01b2019-07-07 21:26:08 +02008834 if (!nfs4_sequence_done(task, &data->res->lr_seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04008835 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04008836 switch (task->tk_status) {
8837 case -NFS4ERR_DELAY:
8838 case -NFS4ERR_GRACE:
Andy Adamson2050f0c2009-04-01 09:22:30 -04008839 rpc_delay(task, NFS4_POLL_RETRY_MIN);
8840 task->tk_status = 0;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05008841 fallthrough;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008842 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008843 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008844 return;
8845 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04008846}
8847
Trond Myklebust17280172012-03-11 13:11:00 -04008848static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04008849 .rpc_call_prepare = nfs4_get_lease_time_prepare,
8850 .rpc_call_done = nfs4_get_lease_time_done,
8851};
8852
8853int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
8854{
Andy Adamson2050f0c2009-04-01 09:22:30 -04008855 struct nfs4_get_lease_time_args args;
8856 struct nfs4_get_lease_time_res res = {
8857 .lr_fsinfo = fsinfo,
8858 };
8859 struct nfs4_get_lease_time_data data = {
8860 .args = &args,
8861 .res = &res,
8862 .clp = clp,
8863 };
8864 struct rpc_message msg = {
8865 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
8866 .rpc_argp = &args,
8867 .rpc_resp = &res,
8868 };
8869 struct rpc_task_setup task_setup = {
8870 .rpc_client = clp->cl_rpcclient,
8871 .rpc_message = &msg,
8872 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008873 .callback_data = &data,
8874 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04008875 };
Andy Adamson2050f0c2009-04-01 09:22:30 -04008876
Anna Schumakerfba83f32018-05-04 16:22:50 -04008877 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0, 1);
Anna Schumakerf836b272019-08-19 10:18:28 -04008878 return nfs4_call_sync_custom(&task_setup);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008879}
8880
Donald Buczek0efb01b2019-07-07 21:26:08 +02008881#ifdef CONFIG_NFS_V4_1
8882
Andy Adamsonfc931582009-04-01 09:22:31 -04008883/*
8884 * Initialize the values to be used by the client in CREATE_SESSION
8885 * If nfs4_init_session set the fore channel request and response sizes,
8886 * use them.
8887 *
8888 * Set the back channel max_resp_sz_cached to zero to force the client to
8889 * always set csa_cachethis to FALSE because the current implementation
8890 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
8891 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04008892static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
8893 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04008894{
Andy Adamson18aad3d2013-06-26 12:21:49 -04008895 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04008896 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Trond Myklebust7402a4f2019-07-16 13:51:29 -04008897 unsigned int max_bc_slots = rpc_num_bc_slots(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04008898
Andy Adamson18aad3d2013-06-26 12:21:49 -04008899 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
8900 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
8901
Andy Adamsonfc931582009-04-01 09:22:31 -04008902 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04008903 args->fc_attrs.max_rqst_sz = max_rqst_sz;
8904 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04008905 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05008906 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04008907
8908 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05008909 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04008910 __func__,
8911 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05008912 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04008913
8914 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04008915 args->bc_attrs.max_rqst_sz = max_bc_payload;
8916 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04008917 args->bc_attrs.max_resp_sz_cached = 0;
8918 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust62421cd2018-08-11 11:52:39 -04008919 args->bc_attrs.max_reqs = max_t(unsigned short, max_session_cb_slots, 1);
Trond Myklebust7402a4f2019-07-16 13:51:29 -04008920 if (args->bc_attrs.max_reqs > max_bc_slots)
8921 args->bc_attrs.max_reqs = max_bc_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04008922
8923 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
8924 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
8925 __func__,
8926 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
8927 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
8928 args->bc_attrs.max_reqs);
8929}
8930
Trond Myklebust79969dd2015-02-18 11:30:18 -08008931static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
8932 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008933{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008934 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008935 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008936
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008937 if (rcvd->max_resp_sz > sent->max_resp_sz)
8938 return -EINVAL;
8939 /*
8940 * Our requested max_ops is the minimum we need; we're not
8941 * prepared to break up compounds into smaller pieces than that.
8942 * So, no point even trying to continue if the server won't
8943 * cooperate:
8944 */
8945 if (rcvd->max_ops < sent->max_ops)
8946 return -EINVAL;
8947 if (rcvd->max_reqs == 0)
8948 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04008949 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
8950 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008951 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04008952}
8953
Trond Myklebust79969dd2015-02-18 11:30:18 -08008954static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
8955 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008956{
8957 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008958 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04008959
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008960 if (!(res->flags & SESSION4_BACK_CHAN))
8961 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008962 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
8963 return -EINVAL;
8964 if (rcvd->max_resp_sz < sent->max_resp_sz)
8965 return -EINVAL;
8966 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
8967 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008968 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008969 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008970 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008971 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008972out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008973 return 0;
8974}
Andy Adamson8d353012009-04-01 09:22:32 -04008975
Andy Adamson8d353012009-04-01 09:22:32 -04008976static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08008977 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008978{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008979 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04008980
Trond Myklebust79969dd2015-02-18 11:30:18 -08008981 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008982 if (ret)
8983 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008984 return nfs4_verify_back_channel_attrs(args, res);
8985}
8986
8987static void nfs4_update_session(struct nfs4_session *session,
8988 struct nfs41_create_session_res *res)
8989{
8990 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05008991 /* Mark client id and session as being confirmed */
8992 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
8993 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08008994 session->flags = res->flags;
8995 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008996 if (res->flags & SESSION4_BACK_CHAN)
8997 memcpy(&session->bc_attrs, &res->bc_attrs,
8998 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04008999}
9000
Trond Myklebust848f5bd2012-05-25 17:51:23 -04009001static int _nfs4_proc_create_session(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11009002 const struct cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04009003{
9004 struct nfs4_session *session = clp->cl_session;
9005 struct nfs41_create_session_args args = {
9006 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08009007 .clientid = clp->cl_clientid,
9008 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04009009 .cb_program = NFS4_CALLBACK,
9010 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08009011 struct nfs41_create_session_res res;
9012
Andy Adamsonfc931582009-04-01 09:22:31 -04009013 struct rpc_message msg = {
9014 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
9015 .rpc_argp = &args,
9016 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04009017 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04009018 };
9019 int status;
9020
Chuck Lever6b26cc82016-05-02 14:40:40 -04009021 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04009022 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04009023
NeilBrown5a0c2572019-05-30 10:41:28 +10009024 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
9025 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04009026 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04009027
Trond Myklebustb519d402016-09-11 14:50:01 -04009028 switch (status) {
9029 case -NFS4ERR_STALE_CLIENTID:
9030 case -NFS4ERR_DELAY:
9031 case -ETIMEDOUT:
9032 case -EACCES:
9033 case -EAGAIN:
9034 goto out;
zhengbin8b98a532019-12-19 18:34:47 +08009035 }
Trond Myklebustb519d402016-09-11 14:50:01 -04009036
9037 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05009038 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04009039 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08009040 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04009041 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08009042 if (status)
9043 goto out;
9044 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04009045 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08009046out:
Andy Adamsonfc931582009-04-01 09:22:31 -04009047 return status;
9048}
9049
9050/*
9051 * Issues a CREATE_SESSION operation to the server.
9052 * It is the responsibility of the caller to verify the session is
9053 * expired before calling this routine.
9054 */
NeilBrowna52458b2018-12-03 11:30:31 +11009055int nfs4_proc_create_session(struct nfs_client *clp, const struct cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04009056{
9057 int status;
9058 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04009059 struct nfs4_session *session = clp->cl_session;
9060
9061 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
9062
Trond Myklebust848f5bd2012-05-25 17:51:23 -04009063 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04009064 if (status)
9065 goto out;
9066
Andy Adamsonaacd5532011-11-09 13:58:21 -05009067 /* Init or reset the session slot tables */
9068 status = nfs4_setup_session_slot_tables(session);
9069 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04009070 if (status)
9071 goto out;
9072
9073 ptr = (unsigned *)&session->sess_id.data[0];
9074 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
9075 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04009076out:
Andy Adamsonfc931582009-04-01 09:22:31 -04009077 return status;
9078}
9079
Andy Adamson0f3e66c2009-04-01 09:22:34 -04009080/*
9081 * Issue the over-the-wire RPC DESTROY_SESSION.
9082 * The caller must serialize access to this routine.
9083 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04009084int nfs4_proc_destroy_session(struct nfs4_session *session,
NeilBrowna52458b2018-12-03 11:30:31 +11009085 const struct cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04009086{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04009087 struct rpc_message msg = {
9088 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
9089 .rpc_argp = session,
9090 .rpc_cred = cred,
9091 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04009092 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04009093
Andy Adamson0f3e66c2009-04-01 09:22:34 -04009094 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05009095 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
9096 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04009097
NeilBrown5a0c2572019-05-30 10:41:28 +10009098 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
9099 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04009100 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04009101
9102 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04009103 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04009104 "Session has been destroyed regardless...\n", status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04009105 return status;
9106}
9107
Trond Myklebust7b38c362012-05-23 13:23:31 -04009108/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009109 * Renew the cl_session lease.
9110 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04009111struct nfs4_sequence_data {
9112 struct nfs_client *clp;
9113 struct nfs4_sequence_args args;
9114 struct nfs4_sequence_res res;
9115};
9116
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08009117static void nfs41_sequence_release(void *data)
9118{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04009119 struct nfs4_sequence_data *calldata = data;
9120 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08009121
Elena Reshetova212bf412017-10-20 12:53:38 +03009122 if (refcount_read(&clp->cl_count) > 1)
Alexandros Batsakis71358402010-02-05 03:45:05 -08009123 nfs4_schedule_state_renewal(clp);
9124 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04009125 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08009126}
9127
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009128static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
9129{
9130 switch(task->tk_status) {
9131 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009132 rpc_delay(task, NFS4_POLL_RETRY_MAX);
9133 return -EAGAIN;
9134 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05009135 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009136 }
9137 return 0;
9138}
9139
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08009140static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009141{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04009142 struct nfs4_sequence_data *calldata = data;
9143 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009144
Trond Myklebust14516c32010-07-31 14:29:06 -04009145 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
9146 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009147
Trond Myklebustc6d01c62013-08-09 11:51:26 -04009148 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009149 if (task->tk_status < 0) {
9150 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Elena Reshetova212bf412017-10-20 12:53:38 +03009151 if (refcount_read(&clp->cl_count) == 1)
Chuck Lever86882c72021-10-16 18:03:04 -04009152 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009153
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009154 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
9155 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009156 return;
9157 }
9158 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009159 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009160}
9161
9162static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
9163{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04009164 struct nfs4_sequence_data *calldata = data;
9165 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009166 struct nfs4_sequence_args *args;
9167 struct nfs4_sequence_res *res;
9168
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009169 args = task->tk_msg.rpc_argp;
9170 res = task->tk_msg.rpc_resp;
9171
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009172 nfs4_setup_sequence(clp, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009173}
9174
9175static const struct rpc_call_ops nfs41_sequence_ops = {
9176 .rpc_call_done = nfs41_sequence_call_done,
9177 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08009178 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009179};
9180
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009181static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11009182 const struct cred *cred,
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009183 struct nfs4_slot *slot,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009184 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009185{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04009186 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009187 struct rpc_message msg = {
9188 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
9189 .rpc_cred = cred,
9190 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009191 struct rpc_task_setup task_setup_data = {
9192 .rpc_client = clp->cl_rpcclient,
9193 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009194 .callback_ops = &nfs41_sequence_ops,
Olga Kornievskaia85e39fe2021-06-23 23:28:51 -04009195 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT | RPC_TASK_MOVEABLE,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009196 };
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009197 struct rpc_task *ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009198
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009199 ret = ERR_PTR(-EIO);
Elena Reshetova212bf412017-10-20 12:53:38 +03009200 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009201 goto out_err;
9202
9203 ret = ERR_PTR(-ENOMEM);
Benny Halevydfb4f3092010-09-24 09:17:01 -04009204 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009205 if (calldata == NULL)
9206 goto out_put_clp;
Anna Schumakerfba83f32018-05-04 16:22:50 -04009207 nfs4_init_sequence(&calldata->args, &calldata->res, 0, is_privileged);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009208 nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04009209 msg.rpc_argp = &calldata->args;
9210 msg.rpc_resp = &calldata->res;
9211 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009212 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009213
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009214 ret = rpc_run_task(&task_setup_data);
9215 if (IS_ERR(ret))
9216 goto out_err;
9217 return ret;
9218out_put_clp:
9219 nfs_put_client(clp);
9220out_err:
9221 nfs41_release_slot(slot);
9222 return ret;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009223}
9224
NeilBrowna52458b2018-12-03 11:30:31 +11009225static int nfs41_proc_async_sequence(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009226{
9227 struct rpc_task *task;
9228 int ret = 0;
9229
Trond Myklebust2f60ea62011-08-24 15:07:37 -04009230 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04009231 return -EAGAIN;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009232 task = _nfs41_proc_sequence(clp, cred, NULL, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009233 if (IS_ERR(task))
9234 ret = PTR_ERR(task);
9235 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08009236 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009237 dprintk("<-- %s status=%d\n", __func__, ret);
9238 return ret;
9239}
9240
NeilBrowna52458b2018-12-03 11:30:31 +11009241static int nfs4_proc_sequence(struct nfs_client *clp, const struct cred *cred)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009242{
9243 struct rpc_task *task;
9244 int ret;
9245
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009246 task = _nfs41_proc_sequence(clp, cred, NULL, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009247 if (IS_ERR(task)) {
9248 ret = PTR_ERR(task);
9249 goto out;
9250 }
9251 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04009252 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009253 ret = task->tk_status;
9254 rpc_put_task(task);
9255out:
9256 dprintk("<-- %s status=%d\n", __func__, ret);
9257 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009258}
9259
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009260struct nfs4_reclaim_complete_data {
9261 struct nfs_client *clp;
9262 struct nfs41_reclaim_complete_args arg;
9263 struct nfs41_reclaim_complete_res res;
9264};
9265
9266static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
9267{
9268 struct nfs4_reclaim_complete_data *calldata = data;
9269
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009270 nfs4_setup_sequence(calldata->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04009271 &calldata->arg.seq_args,
9272 &calldata->res.seq_res,
9273 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009274}
9275
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009276static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
9277{
9278 switch(task->tk_status) {
9279 case 0:
Jeff Layton57174592018-03-18 08:37:03 -04009280 wake_up_all(&clp->cl_lock_waitq);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05009281 fallthrough;
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009282 case -NFS4ERR_COMPLETE_ALREADY:
9283 case -NFS4ERR_WRONG_CRED: /* What to do here? */
9284 break;
9285 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009286 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05009287 fallthrough;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04009288 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009289 return -EAGAIN;
Trond Myklebust0048fdd2017-05-04 13:44:04 -04009290 case -NFS4ERR_BADSESSION:
9291 case -NFS4ERR_DEADSESSION:
9292 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
Trond Myklebust0048fdd2017-05-04 13:44:04 -04009293 break;
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009294 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05009295 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009296 }
9297 return 0;
9298}
9299
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009300static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
9301{
9302 struct nfs4_reclaim_complete_data *calldata = data;
9303 struct nfs_client *clp = calldata->clp;
9304 struct nfs4_sequence_res *res = &calldata->res.seq_res;
9305
Trond Myklebust14516c32010-07-31 14:29:06 -04009306 if (!nfs41_sequence_done(task, res))
9307 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009308
Trond Myklebustc6d01c62013-08-09 11:51:26 -04009309 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009310 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
9311 rpc_restart_call_prepare(task);
9312 return;
9313 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009314}
9315
9316static void nfs4_free_reclaim_complete_data(void *data)
9317{
9318 struct nfs4_reclaim_complete_data *calldata = data;
9319
9320 kfree(calldata);
9321}
9322
9323static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
9324 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
9325 .rpc_call_done = nfs4_reclaim_complete_done,
9326 .rpc_release = nfs4_free_reclaim_complete_data,
9327};
9328
9329/*
9330 * Issue a global reclaim complete.
9331 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04009332static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11009333 const struct cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009334{
9335 struct nfs4_reclaim_complete_data *calldata;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009336 struct rpc_message msg = {
9337 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04009338 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009339 };
9340 struct rpc_task_setup task_setup_data = {
9341 .rpc_client = clp->cl_rpcclient,
9342 .rpc_message = &msg,
9343 .callback_ops = &nfs4_reclaim_complete_call_ops,
Anna Schumaker4c952e32019-08-14 15:46:48 -04009344 .flags = RPC_TASK_NO_ROUND_ROBIN,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009345 };
9346 int status = -ENOMEM;
9347
Trond Myklebust8535b2b2010-05-13 12:51:01 -04009348 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009349 if (calldata == NULL)
9350 goto out;
9351 calldata->clp = clp;
9352 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009353
Anna Schumakerfba83f32018-05-04 16:22:50 -04009354 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0, 1);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009355 msg.rpc_argp = &calldata->arg;
9356 msg.rpc_resp = &calldata->res;
9357 task_setup_data.callback_data = calldata;
Anna Schumaker4c952e32019-08-14 15:46:48 -04009358 status = nfs4_call_sync_custom(&task_setup_data);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009359out:
9360 dprintk("<-- %s status=%d\n", __func__, status);
9361 return status;
9362}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009363
9364static void
9365nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
9366{
9367 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00009368 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009369
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009370 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
Jeff Layton183d9e72016-05-17 12:28:47 -04009371 &lgp->res.seq_res, task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009372}
9373
9374static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
9375{
9376 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04009377
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04009378 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04009379}
9380
9381static int
9382nfs4_layoutget_handle_exception(struct rpc_task *task,
9383 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
9384{
Trond Myklebustee314c22012-10-01 17:25:48 -07009385 struct inode *inode = lgp->args.inode;
9386 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4e89bc2021-07-02 17:24:22 -04009387 struct pnfs_layout_hdr *lo = lgp->lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04009388 int nfs4err = task->tk_status;
9389 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04009390 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009391
Boaz Harroshed7e5422014-01-22 20:34:54 +02009392 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009393
Trond Myklebust2dbf8df2018-06-15 15:58:45 -04009394 nfs4_sequence_free_slot(&lgp->res.seq_res);
9395
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04009396 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009397 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07009398 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08009399
9400 /*
9401 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
9402 * on the file. set tk_status to -ENODATA to tell upper layer to
9403 * retry go inband.
9404 */
9405 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04009406 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08009407 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02009408 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07009409 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
9410 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
9411 */
9412 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04009413 status = -EOVERFLOW;
9414 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07009415 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02009416 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07009417 * (or clients) writing to the same RAID stripe except when
9418 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04009419 *
9420 * Treat it like we would RECALLCONFLICT -- we retry for a little
9421 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02009422 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009423 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04009424 if (lgp->args.minlength == 0) {
9425 status = -EOVERFLOW;
9426 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02009427 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04009428 status = -EBUSY;
9429 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04009430 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04009431 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04009432 break;
Trond Myklebust26f47442016-09-22 13:39:10 -04009433 case -NFS4ERR_DELEG_REVOKED:
9434 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustee314c22012-10-01 17:25:48 -07009435 case -NFS4ERR_EXPIRED:
9436 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04009437 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07009438 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04009439 /* If the open stateid was bad, then recover it. */
9440 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
Trond Myklebuste8fa33a2017-10-04 13:49:12 -04009441 !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07009442 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04009443 exception->state = lgp->args.ctx->state;
Trond Myklebust26f47442016-09-22 13:39:10 -04009444 exception->stateid = &lgp->args.stateid;
Trond Myklebust2259f962015-09-20 13:30:30 -04009445 break;
9446 }
Trond Myklebustee314c22012-10-01 17:25:48 -07009447
Trond Myklebustf7db0b22016-07-14 15:14:02 -04009448 /*
9449 * Mark the bad layout state as invalid, then retry
9450 */
Trond Myklebust668f4552016-07-24 17:08:59 -04009451 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04009452 spin_unlock(&inode->i_lock);
Trond Myklebust1f18b822017-04-29 10:10:17 -04009453 nfs_commit_inode(inode, 0);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04009454 pnfs_free_lseg_list(&head);
9455 status = -EAGAIN;
9456 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009457 }
Jeff Layton183d9e72016-05-17 12:28:47 -04009458
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04009459 err = nfs4_handle_exception(server, nfs4err, exception);
9460 if (!status) {
9461 if (exception->retry)
9462 status = -EAGAIN;
9463 else
9464 status = err;
9465 }
Trond Myklebustee314c22012-10-01 17:25:48 -07009466out:
Jeff Layton183d9e72016-05-17 12:28:47 -04009467 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009468}
9469
Fred Isamandacb4522016-09-19 17:47:09 -04009470size_t max_response_pages(struct nfs_server *server)
Idan Kedar85541162012-08-02 11:47:10 +03009471{
9472 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
9473 return nfs_page_array_len(0, max_resp_sz);
9474}
9475
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009476static void nfs4_layoutget_release(void *calldata)
9477{
9478 struct nfs4_layoutget *lgp = calldata;
9479
Trond Myklebustbd171932017-06-27 17:33:38 -04009480 nfs4_sequence_free_slot(&lgp->res.seq_res);
Trond Myklebust29a8bfe2018-05-30 17:16:20 -04009481 pnfs_layoutget_free(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009482}
9483
9484static const struct rpc_call_ops nfs4_layoutget_call_ops = {
9485 .rpc_call_prepare = nfs4_layoutget_prepare,
9486 .rpc_call_done = nfs4_layoutget_done,
9487 .rpc_release = nfs4_layoutget_release,
9488};
9489
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04009490struct pnfs_layout_segment *
Fred Isamandacb4522016-09-19 17:47:09 -04009491nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009492{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05009493 struct inode *inode = lgp->args.inode;
9494 struct nfs_server *server = NFS_SERVER(inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009495 struct rpc_task *task;
9496 struct rpc_message msg = {
9497 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
9498 .rpc_argp = &lgp->args,
9499 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04009500 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009501 };
9502 struct rpc_task_setup task_setup_data = {
9503 .rpc_client = server->client,
9504 .rpc_message = &msg,
9505 .callback_ops = &nfs4_layoutget_call_ops,
9506 .callback_data = lgp,
Olga Kornievskaia85e39fe2021-06-23 23:28:51 -04009507 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF |
9508 RPC_TASK_MOVEABLE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009509 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04009510 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04009511 struct nfs4_exception exception = {
9512 .inode = inode,
9513 .timeout = *timeout,
9514 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009515 int status = 0;
9516
Anna Schumakerfba83f32018-05-04 16:22:50 -04009517 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05009518
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009519 task = rpc_run_task(&task_setup_data);
Xiyu Yang6e476662020-04-25 21:04:40 +08009520
Anna Schumaker820bf852017-01-11 15:01:43 -05009521 status = rpc_wait_for_completion_task(task);
Trond Myklebust2dbf8df2018-06-15 15:58:45 -04009522 if (status != 0)
9523 goto out;
9524
Trond Myklebust18c07782019-02-13 07:55:31 -05009525 if (task->tk_status < 0) {
Jeff Layton183d9e72016-05-17 12:28:47 -04009526 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
9527 *timeout = exception.timeout;
Trond Myklebust18c07782019-02-13 07:55:31 -05009528 } else if (lgp->res.layoutp->len == 0) {
9529 status = -EAGAIN;
9530 *timeout = nfs4_update_delay(&exception.timeout);
Trond Myklebust2dbf8df2018-06-15 15:58:45 -04009531 } else
9532 lseg = pnfs_layout_process(lgp);
9533out:
Trond Myklebust1037e6e2013-08-14 16:36:51 -04009534 trace_nfs4_layoutget(lgp->args.ctx,
9535 &lgp->args.range,
9536 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05009537 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04009538 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04009539
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009540 rpc_put_task(task);
9541 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04009542 if (status)
9543 return ERR_PTR(status);
9544 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009545}
9546
Benny Halevycbe82602011-05-22 19:52:37 +03009547static void
9548nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
9549{
9550 struct nfs4_layoutreturn *lrp = calldata;
9551
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009552 nfs4_setup_sequence(lrp->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04009553 &lrp->args.seq_args,
9554 &lrp->res.seq_res,
9555 task);
Trond Myklebustc8bf7072018-06-15 16:31:02 -04009556 if (!pnfs_layout_is_valid(lrp->args.layout))
9557 rpc_exit(task, 0);
Benny Halevycbe82602011-05-22 19:52:37 +03009558}
9559
9560static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
9561{
9562 struct nfs4_layoutreturn *lrp = calldata;
9563 struct nfs_server *server;
9564
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04009565 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03009566 return;
9567
Trond Myklebust6109bcf2019-09-20 07:23:43 -04009568 /*
9569 * Was there an RPC level error? Assume the call succeeded,
9570 * and that we need to release the layout
9571 */
9572 if (task->tk_rpc_status != 0 && RPC_WAS_SENT(task)) {
9573 lrp->res.lrs_present = 0;
9574 return;
9575 }
9576
Benny Halevycbe82602011-05-22 19:52:37 +03009577 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05009578 switch (task->tk_status) {
Trond Myklebustff905142017-11-06 15:28:08 -05009579 case -NFS4ERR_OLD_STATEID:
Trond Myklebust30cb3ee2019-09-20 07:23:45 -04009580 if (nfs4_layout_refresh_old_stateid(&lrp->args.stateid,
Trond Myklebustecf84022018-08-15 21:35:46 -04009581 &lrp->args.range,
Trond Myklebustff905142017-11-06 15:28:08 -05009582 lrp->args.inode))
9583 goto out_restart;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05009584 fallthrough;
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05009585 default:
9586 task->tk_status = 0;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05009587 fallthrough;
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05009588 case 0:
9589 break;
9590 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10009591 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05009592 break;
Trond Myklebustff905142017-11-06 15:28:08 -05009593 goto out_restart;
Benny Halevycbe82602011-05-22 19:52:37 +03009594 }
Trond Myklebustff905142017-11-06 15:28:08 -05009595 return;
9596out_restart:
9597 task->tk_status = 0;
9598 nfs4_sequence_free_slot(&lrp->res.seq_res);
9599 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03009600}
9601
9602static void nfs4_layoutreturn_release(void *calldata)
9603{
9604 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04009605 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03009606
Trond Myklebust2a974422016-11-20 13:13:54 -05009607 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
Trond Myklebust68f74472016-10-12 19:50:54 -04009608 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04009609 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebust4d796d72016-09-23 11:38:08 -04009610 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
9611 lrp->ld_private.ops->free(&lrp->ld_private);
Trond Myklebust2f065dd2016-12-07 12:29:26 -05009612 pnfs_put_layout_hdr(lrp->args.layout);
9613 nfs_iput_and_deactive(lrp->inode);
Trond Myklebust44ea8df2020-04-02 15:37:02 -04009614 put_cred(lrp->cred);
Benny Halevycbe82602011-05-22 19:52:37 +03009615 kfree(calldata);
Benny Halevycbe82602011-05-22 19:52:37 +03009616}
9617
9618static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
9619 .rpc_call_prepare = nfs4_layoutreturn_prepare,
9620 .rpc_call_done = nfs4_layoutreturn_done,
9621 .rpc_release = nfs4_layoutreturn_release,
9622};
9623
Peng Tao6c166052014-11-17 09:30:40 +08009624int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03009625{
9626 struct rpc_task *task;
9627 struct rpc_message msg = {
9628 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
9629 .rpc_argp = &lrp->args,
9630 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04009631 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03009632 };
9633 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04009634 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03009635 .rpc_message = &msg,
9636 .callback_ops = &nfs4_layoutreturn_call_ops,
9637 .callback_data = lrp,
Olga Kornievskaia85e39fe2021-06-23 23:28:51 -04009638 .flags = RPC_TASK_MOVEABLE,
Benny Halevycbe82602011-05-22 19:52:37 +03009639 };
Peng Tao6c166052014-11-17 09:30:40 +08009640 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03009641
Andrew Elble99ade3c2015-12-02 09:39:51 -05009642 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
9643 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
9644 &task_setup_data.rpc_client, &msg);
9645
Trond Myklebustc3aba892021-06-01 11:35:56 -04009646 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05009647 if (!sync) {
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05009648 if (!lrp->inode) {
9649 nfs4_layoutreturn_release(lrp);
9650 return -EAGAIN;
9651 }
9652 task_setup_data.flags |= RPC_TASK_ASYNC;
9653 }
Trond Myklebustc3aba892021-06-01 11:35:56 -04009654 if (!lrp->inode)
9655 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1,
9656 1);
9657 else
9658 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1,
9659 0);
Benny Halevycbe82602011-05-22 19:52:37 +03009660 task = rpc_run_task(&task_setup_data);
9661 if (IS_ERR(task))
9662 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05009663 if (sync)
9664 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05009665 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03009666 dprintk("<-- %s status=%d\n", __func__, status);
9667 rpc_put_task(task);
9668 return status;
9669}
9670
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009671static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009672_nfs4_proc_getdeviceinfo(struct nfs_server *server,
9673 struct pnfs_device *pdev,
NeilBrowna52458b2018-12-03 11:30:31 +11009674 const struct cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009675{
9676 struct nfs4_getdeviceinfo_args args = {
9677 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04009678 .notify_types = NOTIFY_DEVICEID4_CHANGE |
9679 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009680 };
9681 struct nfs4_getdeviceinfo_res res = {
9682 .pdev = pdev,
9683 };
9684 struct rpc_message msg = {
9685 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
9686 .rpc_argp = &args,
9687 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009688 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009689 };
9690 int status;
9691
Bryan Schumaker7c513052011-03-24 17:12:24 +00009692 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04009693 if (res.notification & ~args.notify_types)
9694 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04009695 if (res.notification != args.notify_types)
9696 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04009697
Trond Myklebustcac1d3a2020-12-16 16:31:26 -05009698 trace_nfs4_getdeviceinfo(server, &pdev->dev_id, status);
9699
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009700 dprintk("<-- %s status=%d\n", __func__, status);
9701
9702 return status;
9703}
9704
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009705int nfs4_proc_getdeviceinfo(struct nfs_server *server,
9706 struct pnfs_device *pdev,
NeilBrowna52458b2018-12-03 11:30:31 +11009707 const struct cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009708{
9709 struct nfs4_exception exception = { };
9710 int err;
9711
9712 do {
9713 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009714 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009715 &exception);
9716 } while (exception.retry);
9717 return err;
9718}
9719EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
9720
Andy Adamson863a3c62011-03-23 13:27:54 +00009721static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
9722{
9723 struct nfs4_layoutcommit_data *data = calldata;
9724 struct nfs_server *server = NFS_SERVER(data->args.inode);
9725
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009726 nfs4_setup_sequence(server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04009727 &data->args.seq_args,
9728 &data->res.seq_res,
9729 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00009730}
9731
9732static void
9733nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
9734{
9735 struct nfs4_layoutcommit_data *data = calldata;
9736 struct nfs_server *server = NFS_SERVER(data->args.inode);
9737
Trond Myklebust6ba7db32012-10-22 20:07:20 -04009738 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00009739 return;
9740
9741 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009742 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
9743 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
9744 case -NFS4ERR_BADLAYOUT: /* no layout */
9745 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00009746 task->tk_status = 0;
Gustavo A. R. Silvaffb81712020-11-20 12:26:46 -06009747 break;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009748 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009749 break;
9750 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10009751 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009752 rpc_restart_call_prepare(task);
9753 return;
9754 }
9755 }
Andy Adamson863a3c62011-03-23 13:27:54 +00009756}
9757
9758static void nfs4_layoutcommit_release(void *calldata)
9759{
9760 struct nfs4_layoutcommit_data *data = calldata;
9761
Andy Adamsondb29c082011-07-30 20:52:38 -04009762 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05009763 nfs_post_op_update_inode_force_wcc(data->args.inode,
9764 data->res.fattr);
NeilBrowna52458b2018-12-03 11:30:31 +11009765 put_cred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05009766 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00009767 kfree(data);
9768}
9769
9770static const struct rpc_call_ops nfs4_layoutcommit_ops = {
9771 .rpc_call_prepare = nfs4_layoutcommit_prepare,
9772 .rpc_call_done = nfs4_layoutcommit_done,
9773 .rpc_release = nfs4_layoutcommit_release,
9774};
9775
9776int
Andy Adamsonef311532011-03-12 02:58:10 -05009777nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00009778{
9779 struct rpc_message msg = {
9780 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
9781 .rpc_argp = &data->args,
9782 .rpc_resp = &data->res,
9783 .rpc_cred = data->cred,
9784 };
9785 struct rpc_task_setup task_setup_data = {
9786 .task = &data->task,
9787 .rpc_client = NFS_CLIENT(data->args.inode),
9788 .rpc_message = &msg,
9789 .callback_ops = &nfs4_layoutcommit_ops,
9790 .callback_data = data,
Olga Kornievskaia85e39fe2021-06-23 23:28:51 -04009791 .flags = RPC_TASK_MOVEABLE,
Andy Adamson863a3c62011-03-23 13:27:54 +00009792 };
9793 struct rpc_task *task;
9794 int status = 0;
9795
Kinglong Meeb4839eb2015-07-01 12:00:13 +08009796 dprintk("NFS: initiating layoutcommit call. sync %d "
9797 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00009798 data->args.lastbytewritten,
9799 data->args.inode->i_ino);
9800
Trond Myklebust472e2592015-02-05 16:50:30 -05009801 if (!sync) {
9802 data->inode = nfs_igrab_and_active(data->args.inode);
9803 if (data->inode == NULL) {
9804 nfs4_layoutcommit_release(data);
9805 return -EAGAIN;
9806 }
9807 task_setup_data.flags = RPC_TASK_ASYNC;
9808 }
Anna Schumakerfba83f32018-05-04 16:22:50 -04009809 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Andy Adamson863a3c62011-03-23 13:27:54 +00009810 task = rpc_run_task(&task_setup_data);
9811 if (IS_ERR(task))
9812 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05009813 if (sync)
9814 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05009815 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00009816 dprintk("%s: status %d\n", __func__, status);
9817 rpc_put_task(task);
9818 return status;
9819}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009820
Trond Myklebust302fad72019-02-18 13:32:38 -05009821/*
Andy Adamson97431202013-08-08 10:57:56 -04009822 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
9823 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
9824 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009825static int
9826_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009827 struct nfs_fsinfo *info,
9828 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009829{
9830 struct nfs41_secinfo_no_name_args args = {
9831 .style = SECINFO_STYLE_CURRENT_FH,
9832 };
9833 struct nfs4_secinfo_res res = {
9834 .flavors = flavors,
9835 };
9836 struct rpc_message msg = {
9837 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
9838 .rpc_argp = &args,
9839 .rpc_resp = &res,
9840 };
Anna Schumakercc15e242019-08-14 16:22:31 -04009841 struct nfs4_call_sync_data data = {
9842 .seq_server = server,
9843 .seq_args = &args.seq_args,
9844 .seq_res = &res.seq_res,
9845 };
9846 struct rpc_task_setup task_setup = {
9847 .rpc_client = server->client,
9848 .rpc_message = &msg,
9849 .callback_ops = server->nfs_client->cl_mvops->call_sync_ops,
9850 .callback_data = &data,
9851 .flags = RPC_TASK_NO_ROUND_ROBIN,
9852 };
NeilBrowna52458b2018-12-03 11:30:31 +11009853 const struct cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009854 int status;
9855
9856 if (use_integrity) {
Colin Ian King48bb6ec2020-06-10 00:22:57 +01009857 task_setup.rpc_client = server->nfs_client->cl_rpcclient;
Anna Schumakercc15e242019-08-14 16:22:31 -04009858
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04009859 cred = nfs4_get_clid_cred(server->nfs_client);
9860 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009861 }
9862
Anna Schumakercc15e242019-08-14 16:22:31 -04009863 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
9864 status = nfs4_call_sync_custom(&task_setup);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009865 dprintk("<-- %s status=%d\n", __func__, status);
9866
NeilBrowna52458b2018-12-03 11:30:31 +11009867 put_cred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009868
9869 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009870}
9871
9872static int
9873nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9874 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
9875{
Trond Myklebust0688e642019-04-07 13:59:09 -04009876 struct nfs4_exception exception = {
9877 .interruptible = true,
9878 };
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009879 int err;
9880 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009881 /* first try using integrity protection */
9882 err = -NFS4ERR_WRONGSEC;
9883
9884 /* try to use integrity protection with machine cred */
9885 if (_nfs4_is_integrity_protected(server->nfs_client))
9886 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9887 flavors, true);
9888
9889 /*
9890 * if unable to use integrity protection, or SECINFO with
9891 * integrity protection returns NFS4ERR_WRONGSEC (which is
9892 * disallowed by spec, but exists in deployed servers) use
9893 * the current filesystem's rpc_client and the user cred.
9894 */
9895 if (err == -NFS4ERR_WRONGSEC)
9896 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9897 flavors, false);
9898
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009899 switch (err) {
9900 case 0:
9901 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05009902 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009903 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009904 default:
9905 err = nfs4_handle_exception(server, err, &exception);
9906 }
9907 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009908out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009909 return err;
9910}
9911
9912static int
9913nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
9914 struct nfs_fsinfo *info)
9915{
9916 int err;
9917 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04009918 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009919 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009920 struct nfs4_secinfo4 *secinfo;
9921 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009922
9923 page = alloc_page(GFP_KERNEL);
9924 if (!page) {
9925 err = -ENOMEM;
9926 goto out;
9927 }
9928
9929 flavors = page_address(page);
9930 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
9931
9932 /*
9933 * Fall back on "guess and check" method if
9934 * the server doesn't support SECINFO_NO_NAME
9935 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05009936 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009937 err = nfs4_find_root_sec(server, fhandle, info);
9938 goto out_freepage;
9939 }
9940 if (err)
9941 goto out_freepage;
9942
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009943 for (i = 0; i < flavors->num_flavors; i++) {
9944 secinfo = &flavors->flavors[i];
9945
9946 switch (secinfo->flavor) {
9947 case RPC_AUTH_NULL:
9948 case RPC_AUTH_UNIX:
9949 case RPC_AUTH_GSS:
9950 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
9951 &secinfo->flavor_info);
9952 break;
9953 default:
9954 flavor = RPC_AUTH_MAXFLAVOR;
9955 break;
9956 }
9957
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04009958 if (!nfs_auth_info_match(&server->auth_info, flavor))
9959 flavor = RPC_AUTH_MAXFLAVOR;
9960
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009961 if (flavor != RPC_AUTH_MAXFLAVOR) {
9962 err = nfs4_lookup_root_sec(server, fhandle,
9963 info, flavor);
9964 if (!err)
9965 break;
9966 }
9967 }
9968
9969 if (flavor == RPC_AUTH_MAXFLAVOR)
9970 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009971
9972out_freepage:
9973 put_page(page);
9974 if (err == -EACCES)
9975 return -EPERM;
9976out:
9977 return err;
9978}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009979
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009980static int _nfs41_test_stateid(struct nfs_server *server,
9981 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11009982 const struct cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009983{
9984 int status;
9985 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009986 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009987 };
9988 struct nfs41_test_stateid_res res;
9989 struct rpc_message msg = {
9990 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
9991 .rpc_argp = &args,
9992 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009993 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009994 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009995 struct rpc_clnt *rpc_client = server->client;
9996
9997 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9998 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009999
Chuck Lever38527b12012-07-11 16:30:23 -040010000 dprintk("NFS call test_stateid %p\n", stateid);
Anna Schumakerfba83f32018-05-04 16:22:50 -040010001 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Weston Andros Adamson3787d502013-08-13 16:37:36 -040010002 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -040010003 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -040010004 if (status != NFS_OK) {
10005 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -040010006 return status;
Chuck Lever38527b12012-07-11 16:30:23 -040010007 }
10008 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -040010009 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -040010010}
10011
Trond Myklebust43912bb2016-09-22 13:38:56 -040010012static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
10013 int err, struct nfs4_exception *exception)
10014{
10015 exception->retry = 0;
10016 switch(err) {
10017 case -NFS4ERR_DELAY:
Trond Myklebust76e8a1b2016-09-22 13:39:19 -040010018 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust43912bb2016-09-22 13:38:56 -040010019 nfs4_handle_exception(server, err, exception);
10020 break;
10021 case -NFS4ERR_BADSESSION:
10022 case -NFS4ERR_BADSLOT:
10023 case -NFS4ERR_BAD_HIGH_SLOT:
10024 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
10025 case -NFS4ERR_DEADSESSION:
10026 nfs4_do_handle_exception(server, err, exception);
10027 }
10028}
10029
Chuck Lever38527b12012-07-11 16:30:23 -040010030/**
10031 * nfs41_test_stateid - perform a TEST_STATEID operation
10032 *
10033 * @server: server / transport on which to perform the operation
10034 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -040010035 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -040010036 *
10037 * Returns NFS_OK if the server recognizes that "stateid" is valid.
10038 * Otherwise a negative NFS4ERR value is returned if the operation
10039 * failed or the state ID is not currently valid.
10040 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -040010041static int nfs41_test_stateid(struct nfs_server *server,
10042 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +110010043 const struct cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -040010044{
Trond Myklebust0688e642019-04-07 13:59:09 -040010045 struct nfs4_exception exception = {
10046 .interruptible = true,
10047 };
Bryan Schumaker7d974792011-06-02 14:59:08 -040010048 int err;
10049 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -040010050 err = _nfs41_test_stateid(server, stateid, cred);
Trond Myklebust43912bb2016-09-22 13:38:56 -040010051 nfs4_handle_delay_or_session_error(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -040010052 } while (exception.retry);
10053 return err;
10054}
Bryan Schumaker9aeda352011-06-02 14:59:09 -040010055
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010056struct nfs_free_stateid_data {
10057 struct nfs_server *server;
10058 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -040010059 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010060};
10061
10062static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
10063{
10064 struct nfs_free_stateid_data *data = calldata;
Anna Schumaker7981c8a2017-01-10 11:39:53 -050010065 nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010066 &data->args.seq_args,
10067 &data->res.seq_res,
10068 task);
10069}
10070
10071static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
10072{
10073 struct nfs_free_stateid_data *data = calldata;
10074
10075 nfs41_sequence_done(task, &data->res.seq_res);
10076
10077 switch (task->tk_status) {
10078 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +100010079 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010080 rpc_restart_call_prepare(task);
10081 }
10082}
10083
10084static void nfs41_free_stateid_release(void *calldata)
10085{
Scott Mayhew576acc22021-11-03 06:24:40 -040010086 struct nfs_free_stateid_data *data = calldata;
10087 struct nfs_client *clp = data->server->nfs_client;
10088
10089 nfs_put_client(clp);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010090 kfree(calldata);
10091}
10092
Trond Myklebust17f26b12013-08-21 15:48:42 -040010093static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010094 .rpc_call_prepare = nfs41_free_stateid_prepare,
10095 .rpc_call_done = nfs41_free_stateid_done,
10096 .rpc_release = nfs41_free_stateid_release,
10097};
10098
Anna Schumaker2f261022018-05-15 13:03:39 -040010099/**
10100 * nfs41_free_stateid - perform a FREE_STATEID operation
10101 *
10102 * @server: server / transport on which to perform the operation
10103 * @stateid: state ID to release
10104 * @cred: credential
Trond Myklebust302fad72019-02-18 13:32:38 -050010105 * @privileged: set to true if this call needs to be privileged
Anna Schumaker2f261022018-05-15 13:03:39 -040010106 *
10107 * Note: this function is always asynchronous.
10108 */
10109static int nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -040010110 const nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +110010111 const struct cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010112 bool privileged)
10113{
Bryan Schumaker9aeda352011-06-02 14:59:09 -040010114 struct rpc_message msg = {
10115 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -040010116 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -040010117 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010118 struct rpc_task_setup task_setup = {
10119 .rpc_client = server->client,
10120 .rpc_message = &msg,
10121 .callback_ops = &nfs41_free_stateid_ops,
Olga Kornievskaia85e39fe2021-06-23 23:28:51 -040010122 .flags = RPC_TASK_ASYNC | RPC_TASK_MOVEABLE,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010123 };
10124 struct nfs_free_stateid_data *data;
Anna Schumaker2f261022018-05-15 13:03:39 -040010125 struct rpc_task *task;
Scott Mayhew576acc22021-11-03 06:24:40 -040010126 struct nfs_client *clp = server->nfs_client;
10127
10128 if (!refcount_inc_not_zero(&clp->cl_count))
10129 return -EIO;
Bryan Schumaker9aeda352011-06-02 14:59:09 -040010130
Weston Andros Adamson3787d502013-08-13 16:37:36 -040010131 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
10132 &task_setup.rpc_client, &msg);
10133
Chuck Lever38527b12012-07-11 16:30:23 -040010134 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010135 data = kmalloc(sizeof(*data), GFP_NOFS);
10136 if (!data)
Anna Schumaker2f261022018-05-15 13:03:39 -040010137 return -ENOMEM;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010138 data->server = server;
10139 nfs4_stateid_copy(&data->args.stateid, stateid);
10140
10141 task_setup.callback_data = data;
10142
10143 msg.rpc_argp = &data->args;
10144 msg.rpc_resp = &data->res;
Anna Schumakerfba83f32018-05-04 16:22:50 -040010145 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, privileged);
Anna Schumaker2f261022018-05-15 13:03:39 -040010146 task = rpc_run_task(&task_setup);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010147 if (IS_ERR(task))
10148 return PTR_ERR(task);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010149 rpc_put_task(task);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -040010150 return 0;
Bryan Schumaker9aeda352011-06-02 14:59:09 -040010151}
Trond Myklebust36281ca2012-03-04 18:13:56 -050010152
Jeff Laytonf1cdae82014-05-01 06:28:47 -040010153static void
10154nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -040010155{
NeilBrowna52458b2018-12-03 11:30:31 +110010156 const struct cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -040010157
Trond Myklebustf0b0bf82016-09-22 13:39:04 -040010158 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -040010159 nfs4_free_lock_state(server, lsp);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -040010160}
10161
Trond Myklebust36281ca2012-03-04 18:13:56 -050010162static bool nfs41_match_stateid(const nfs4_stateid *s1,
10163 const nfs4_stateid *s2)
10164{
Trond Myklebust93b717f2016-05-16 17:42:43 -040010165 if (s1->type != s2->type)
10166 return false;
10167
Trond Myklebust2d2f24a2012-03-04 18:13:57 -050010168 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -050010169 return false;
10170
Trond Myklebust2d2f24a2012-03-04 18:13:57 -050010171 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -050010172 return true;
Trond Myklebust36281ca2012-03-04 18:13:56 -050010173
Anna Schumaker045c5512017-01-11 16:59:48 -050010174 return s1->seqid == 0 || s2->seqid == 0;
Trond Myklebust36281ca2012-03-04 18:13:56 -050010175}
10176
Andy Adamson557134a2009-04-01 09:21:53 -040010177#endif /* CONFIG_NFS_V4_1 */
10178
Trond Myklebust36281ca2012-03-04 18:13:56 -050010179static bool nfs4_match_stateid(const nfs4_stateid *s1,
10180 const nfs4_stateid *s2)
10181{
Trond Myklebustf597c532012-03-04 18:13:56 -050010182 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -050010183}
10184
10185
Trond Myklebust17280172012-03-11 13:11:00 -040010186static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -050010187 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -050010188 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010189 .recover_open = nfs4_open_reclaim,
10190 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -040010191 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -040010192 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010193};
10194
Andy Adamson591d71c2009-04-01 09:22:47 -040010195#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -040010196static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -040010197 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
10198 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
10199 .recover_open = nfs4_open_reclaim,
10200 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -050010201 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -050010202 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -040010203 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -040010204};
10205#endif /* CONFIG_NFS_V4_1 */
10206
Trond Myklebust17280172012-03-11 13:11:00 -040010207static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -050010208 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -050010209 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +030010210 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010211 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -040010212 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010213};
10214
Andy Adamson591d71c2009-04-01 09:22:47 -040010215#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -040010216static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -040010217 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
10218 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -040010219 .recover_open = nfs41_open_expired,
10220 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -050010221 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -040010222};
10223#endif /* CONFIG_NFS_V4_1 */
10224
Trond Myklebust17280172012-03-11 13:11:00 -040010225static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -040010226 .sched_state_renewal = nfs4_proc_async_renew,
NeilBrownf15e1e82018-12-03 11:30:30 +110010227 .get_state_renewal_cred = nfs4_get_renew_cred,
Benny Halevy8e69514f2009-04-01 09:22:45 -040010228 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -040010229};
10230
10231#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -040010232static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -040010233 .sched_state_renewal = nfs41_proc_async_sequence,
NeilBrownf15e1e82018-12-03 11:30:30 +110010234 .get_state_renewal_cred = nfs4_get_machine_cred,
Benny Halevy8e69514f2009-04-01 09:22:45 -040010235 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -040010236};
10237#endif
10238
Chuck Leverec011fe2013-10-17 14:12:39 -040010239static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -040010240 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -040010241 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -040010242};
10243
10244#if defined(CONFIG_NFS_V4_1)
10245static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -040010246 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -040010247 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -040010248};
10249#endif /* CONFIG_NFS_V4_1 */
10250
Trond Myklebust97dc1352010-06-16 09:52:26 -040010251static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
10252 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -040010253 .init_caps = NFS_CAP_READDIRPLUS
10254 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -040010255 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -040010256 .init_client = nfs40_init_client,
10257 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -050010258 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -040010259 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -040010260 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust45870d62016-09-22 13:38:59 -040010261 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -050010262 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -040010263 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -040010264 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
10265 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
10266 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -040010267 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -040010268};
10269
10270#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -050010271static struct nfs_seqid *
10272nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
10273{
10274 return NULL;
10275}
10276
Trond Myklebust97dc1352010-06-16 09:52:26 -040010277static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
10278 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -040010279 .init_caps = NFS_CAP_READDIRPLUS
10280 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -040010281 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -040010282 | NFS_CAP_STATEID_NFSV41
Fred Isaman6e012602016-10-04 15:26:41 -040010283 | NFS_CAP_ATOMIC_OPEN_V1
10284 | NFS_CAP_LGOPEN,
Chuck Leverabf79bb2013-08-09 12:49:11 -040010285 .init_client = nfs41_init_client,
10286 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -050010287 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -040010288 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -040010289 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust45870d62016-09-22 13:38:59 -040010290 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -050010291 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -040010292 .session_trunk = nfs4_test_session_trunk,
Chuck Lever9915ea72013-08-09 12:48:27 -040010293 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -040010294 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
10295 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
10296 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -040010297 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -040010298};
10299#endif
10300
Steve Dickson42c2c422013-05-22 12:50:38 -040010301#if defined(CONFIG_NFS_V4_2)
10302static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
10303 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -040010304 .init_caps = NFS_CAP_READDIRPLUS
10305 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -040010306 | NFS_CAP_POSIX_LOCK
10307 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -040010308 | NFS_CAP_ATOMIC_OPEN_V1
Fred Isaman6e012602016-10-04 15:26:41 -040010309 | NFS_CAP_LGOPEN
Anna Schumakerf4ac1672014-11-25 13:18:15 -050010310 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -040010311 | NFS_CAP_COPY
Olga Kornievskaiacb95dee2018-07-09 15:13:29 -040010312 | NFS_CAP_OFFLOAD_CANCEL
Olga Kornievskaia04915672019-06-04 16:14:30 -040010313 | NFS_CAP_COPY_NOTIFY
Anna Schumaker624bd5b2014-11-25 13:18:16 -050010314 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -040010315 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +080010316 | NFS_CAP_LAYOUTSTATS
Trond Myklebust3eb86092019-02-08 10:31:05 -050010317 | NFS_CAP_CLONE
Anna Schumakerc5675522014-05-28 13:41:22 -040010318 | NFS_CAP_LAYOUTERROR
10319 | NFS_CAP_READ_PLUS,
Chuck Leverabf79bb2013-08-09 12:49:11 -040010320 .init_client = nfs41_init_client,
10321 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -040010322 .match_stateid = nfs41_match_stateid,
10323 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -040010324 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -040010325 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust45870d62016-09-22 13:38:59 -040010326 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -050010327 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -040010328 .session_trunk = nfs4_test_session_trunk,
Steve Dickson42c2c422013-05-22 12:50:38 -040010329 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
10330 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
10331 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +080010332 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -040010333};
10334#endif
10335
Trond Myklebust97dc1352010-06-16 09:52:26 -040010336const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
10337 [0] = &nfs_v4_0_minor_ops,
10338#if defined(CONFIG_NFS_V4_1)
10339 [1] = &nfs_v4_1_minor_ops,
10340#endif
Steve Dickson42c2c422013-05-22 12:50:38 -040010341#if defined(CONFIG_NFS_V4_2)
10342 [2] = &nfs_v4_2_minor_ops,
10343#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -040010344};
10345
Trond Myklebust13997822016-07-24 17:10:52 -040010346static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +010010347{
Frank van der Linden012a2112020-06-23 22:39:03 +000010348 ssize_t error, error2, error3;
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +010010349
10350 error = generic_listxattr(dentry, list, size);
10351 if (error < 0)
10352 return error;
10353 if (list) {
10354 list += error;
10355 size -= error;
10356 }
10357
10358 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
10359 if (error2 < 0)
10360 return error2;
Frank van der Linden012a2112020-06-23 22:39:03 +000010361
10362 if (list) {
10363 list += error2;
10364 size -= error2;
10365 }
10366
10367 error3 = nfs4_listxattr_nfs4_user(d_inode(dentry), list, size);
10368 if (error3 < 0)
10369 return error3;
10370
10371 return error + error2 + error3;
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +010010372}
10373
Trond Myklebust17f26b12013-08-21 15:48:42 -040010374static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -040010375 .create = nfs_create,
10376 .lookup = nfs_lookup,
10377 .atomic_open = nfs_atomic_open,
10378 .link = nfs_link,
10379 .unlink = nfs_unlink,
10380 .symlink = nfs_symlink,
10381 .mkdir = nfs_mkdir,
10382 .rmdir = nfs_rmdir,
10383 .mknod = nfs_mknod,
10384 .rename = nfs_rename,
10385 .permission = nfs_permission,
10386 .getattr = nfs_getattr,
10387 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +010010388 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -040010389};
10390
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -080010391static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +000010392 .permission = nfs_permission,
10393 .getattr = nfs_getattr,
10394 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +010010395 .listxattr = nfs4_listxattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +000010396};
10397
David Howells509de812006-08-22 20:06:11 -040010398const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010399 .version = 4, /* protocol version */
10400 .dentry_ops = &nfs4_dentry_operations,
10401 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +000010402 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -040010403 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010404 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -040010405 .submount = nfs4_submount,
David Howellsf2aedb72019-12-10 07:31:13 -050010406 .try_get_tree = nfs4_try_get_tree,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010407 .getattr = nfs4_proc_getattr,
10408 .setattr = nfs4_proc_setattr,
10409 .lookup = nfs4_proc_lookup,
Jeff Layton5b5faaf2017-06-29 06:34:52 -070010410 .lookupp = nfs4_proc_lookupp,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010411 .access = nfs4_proc_access,
10412 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010413 .create = nfs4_proc_create,
10414 .remove = nfs4_proc_remove,
10415 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -040010416 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010417 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -040010418 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -040010419 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -040010420 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010421 .link = nfs4_proc_link,
10422 .symlink = nfs4_proc_symlink,
10423 .mkdir = nfs4_proc_mkdir,
Trond Myklebust912678d2018-03-20 16:43:15 -040010424 .rmdir = nfs4_proc_rmdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010425 .readdir = nfs4_proc_readdir,
10426 .mknod = nfs4_proc_mknod,
10427 .statfs = nfs4_proc_statfs,
10428 .fsinfo = nfs4_proc_fsinfo,
10429 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -040010430 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010431 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -040010432 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010433 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -050010434 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010435 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -050010436 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010437 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -040010438 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -050010439 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010440 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +000010441 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -040010442 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -040010443 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -040010444 .have_delegation = nfs4_have_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -040010445 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +000010446 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -040010447 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -040010448 .create_server = nfs4_create_server,
10449 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010450};
10451
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000010452static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +010010453 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000010454 .list = nfs4_xattr_list_nfs4_acl,
10455 .get = nfs4_xattr_get_nfs4_acl,
10456 .set = nfs4_xattr_set_nfs4_acl,
10457};
10458
Frank van der Linden012a2112020-06-23 22:39:03 +000010459#ifdef CONFIG_NFS_V4_2
10460static const struct xattr_handler nfs4_xattr_nfs4_user_handler = {
10461 .prefix = XATTR_USER_PREFIX,
10462 .get = nfs4_xattr_get_nfs4_user,
10463 .set = nfs4_xattr_set_nfs4_user,
10464};
10465#endif
10466
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000010467const struct xattr_handler *nfs4_xattr_handlers[] = {
10468 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -040010469#ifdef CONFIG_NFS_V4_SECURITY_LABEL
10470 &nfs4_xattr_nfs4_label_handler,
10471#endif
Frank van der Linden012a2112020-06-23 22:39:03 +000010472#ifdef CONFIG_NFS_V4_2
10473 &nfs4_xattr_nfs4_user_handler,
10474#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000010475 NULL
10476};