blob: bc90f2a12d5dc72891e1a63d7dcbe4bc97361884 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/nfs/nfs4proc.c
3 *
4 * Client-side procedure declarations for NFSv4.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/delay.h>
40#include <linux/errno.h>
41#include <linux/string.h>
Trond Myklebust652f89f2011-12-09 19:05:58 -050042#include <linux/ratelimit.h>
43#include <linux/printk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/sunrpc/clnt.h>
46#include <linux/nfs.h>
47#include <linux/nfs4.h>
48#include <linux/nfs_fs.h>
49#include <linux/nfs_page.h>
Bryan Schumaker9b7160c2011-04-13 14:31:30 -040050#include <linux/nfs_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070052#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040053#include <linux/module.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000054#include <linux/xattr.h>
Andy Adamsonc7a360b2011-01-25 19:15:32 -050055#include <linux/utsname.h>
Jeff Laytond3103102011-12-01 22:44:39 +010056#include <linux/freezer.h>
Jeff Layton1eb5d982018-01-09 08:21:17 -050057#include <linux/iversion.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Trond Myklebust4ce79712005-06-22 17:16:21 +000059#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050061#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050062#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040063#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040064#include "pnfs.h"
Chuck Leverf0920752012-05-21 22:45:41 -040065#include "netns.h"
Trond Myklebust39d43d12020-10-07 18:24:18 -040066#include "sysfs.h"
Anna Schumaker40c64c22015-04-15 13:00:05 -040067#include "nfs4idmap.h"
Trond Myklebust73e39aa2012-11-26 12:49:34 -050068#include "nfs4session.h"
David Howellsde242c02012-12-20 21:52:38 +000069#include "fscache.h"
Frank van der Linden012a2112020-06-23 22:39:03 +000070#include "nfs42.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Trond Myklebustc6d01c62013-08-09 11:51:26 -040072#include "nfs4trace.h"
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#define NFSDBG_FACILITY NFSDBG_PROC
75
Trond Myklebust30846df2018-04-07 13:44:28 -040076#define NFS4_BITMASK_SZ 3
77
Trond Myklebust2066fe82006-09-15 08:30:46 -040078#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079#define NFS4_POLL_RETRY_MAX (15*HZ)
80
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +020081/* file attributes which can be mapped to nfs attributes */
82#define NFS4_VALID_ATTRS (ATTR_MODE \
83 | ATTR_UID \
84 | ATTR_GID \
85 | ATTR_SIZE \
86 | ATTR_ATIME \
87 | ATTR_MTIME \
88 | ATTR_CTIME \
89 | ATTR_ATIME_SET \
90 | ATTR_MTIME_SET)
91
Trond Myklebustcdd4e682006-01-03 09:55:12 +010092struct nfs4_opendata;
Alexandros Batsakisb2579572009-12-14 21:27:57 -080093static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Chuck Lever81934dd2012-03-01 17:01:57 -050095static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
Trond Myklebusta841b542018-04-07 13:50:59 -040096static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label, struct inode *inode);
NeilBrowna52458b2018-12-03 11:30:31 +110097static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
Trond Myklebust0ab64e02010-04-16 16:22:51 -040098 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +110099 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
David Quigley1775fd32013-05-22 12:50:42 -0400100 struct nfs4_label *olabel);
Bryan Schumakerf062eb62011-06-02 14:59:10 -0400101#ifdef CONFIG_NFS_V4_1
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400102static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +1100103 const struct cred *cred,
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400104 struct nfs4_slot *slot,
105 bool is_privileged);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -0400106static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
NeilBrowna52458b2018-12-03 11:30:31 +1100107 const struct cred *);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -0400108static int nfs41_free_stateid(struct nfs_server *, const nfs4_stateid *,
NeilBrowna52458b2018-12-03 11:30:31 +1100109 const struct cred *, bool);
Bryan Schumakerf062eb62011-06-02 14:59:10 -0400110#endif
Trond Myklebust332d1a02021-03-25 18:15:36 -0400111static void nfs4_bitmask_set(__u32 bitmask[NFS4_BITMASK_SZ],
112 const __u32 *src, struct inode *inode,
113 struct nfs_server *server,
114 struct nfs4_label *label);
David Quigleyaa9c2662013-05-22 12:50:44 -0400115
116#ifdef CONFIG_NFS_V4_SECURITY_LABEL
117static inline struct nfs4_label *
118nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
119 struct iattr *sattr, struct nfs4_label *label)
120{
121 int err;
122
123 if (label == NULL)
124 return NULL;
125
126 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
127 return NULL;
128
David Quigleyaa9c2662013-05-22 12:50:44 -0400129 err = security_dentry_init_security(dentry, sattr->ia_mode,
130 &dentry->d_name, (void **)&label->label, &label->len);
131 if (err == 0)
132 return label;
133
134 return NULL;
135}
136static inline void
137nfs4_label_release_security(struct nfs4_label *label)
138{
139 if (label)
140 security_release_secctx(label->label, label->len);
141}
142static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
143{
144 if (label)
145 return server->attr_bitmask;
146
147 return server->attr_bitmask_nl;
148}
149#else
150static inline struct nfs4_label *
151nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
152 struct iattr *sattr, struct nfs4_label *l)
153{ return NULL; }
154static inline void
155nfs4_label_release_security(struct nfs4_label *label)
156{ return; }
157static inline u32 *
158nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
159{ return server->attr_bitmask; }
160#endif
161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -0500163static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{
Trond Myklebust52567b02009-10-23 14:46:42 -0400165 if (err >= -1000)
166 return err;
167 switch (err) {
168 case -NFS4ERR_RESOURCE:
Weston Andros Adamson30005122013-02-28 20:30:10 -0500169 case -NFS4ERR_LAYOUTTRYLATER:
170 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebust52567b02009-10-23 14:46:42 -0400171 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000172 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson88975382013-08-13 16:37:38 -0400173 case -NFS4ERR_WRONG_CRED:
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000174 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800175 case -NFS4ERR_BADOWNER:
176 case -NFS4ERR_BADNAME:
177 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400178 case -NFS4ERR_SHARE_DENIED:
179 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400180 case -NFS4ERR_MINOR_VERS_MISMATCH:
181 return -EPROTONOSUPPORT;
Trond Myklebust6e3cf242013-03-23 15:22:45 -0400182 case -NFS4ERR_FILE_OPEN:
183 return -EBUSY;
Trond Myklebust9fff59e2020-11-02 20:11:32 -0500184 case -NFS4ERR_NOT_SAME:
185 return -ENOTSYNC;
Trond Myklebust52567b02009-10-23 14:46:42 -0400186 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700188 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400189 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400191 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192}
193
194/*
195 * This is our standard bitmap for GETATTR requests.
196 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400197const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 FATTR4_WORD0_TYPE
199 | FATTR4_WORD0_CHANGE
200 | FATTR4_WORD0_SIZE
201 | FATTR4_WORD0_FSID
202 | FATTR4_WORD0_FILEID,
203 FATTR4_WORD1_MODE
204 | FATTR4_WORD1_NUMLINKS
205 | FATTR4_WORD1_OWNER
206 | FATTR4_WORD1_OWNER_GROUP
207 | FATTR4_WORD1_RAWDEV
208 | FATTR4_WORD1_SPACE_USED
209 | FATTR4_WORD1_TIME_ACCESS
210 | FATTR4_WORD1_TIME_METADATA
Anna Schumakerea96d1e2015-04-03 14:35:59 -0400211 | FATTR4_WORD1_TIME_MODIFY
212 | FATTR4_WORD1_MOUNTED_ON_FILEID,
David Quigleyaa9c2662013-05-22 12:50:44 -0400213#ifdef CONFIG_NFS_V4_SECURITY_LABEL
214 FATTR4_WORD2_SECURITY_LABEL
215#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216};
217
Trond Myklebust1549210f2012-06-05 09:16:47 -0400218static const u32 nfs4_pnfs_open_bitmap[3] = {
219 FATTR4_WORD0_TYPE
220 | FATTR4_WORD0_CHANGE
221 | FATTR4_WORD0_SIZE
222 | FATTR4_WORD0_FSID
223 | FATTR4_WORD0_FILEID,
224 FATTR4_WORD1_MODE
225 | FATTR4_WORD1_NUMLINKS
226 | FATTR4_WORD1_OWNER
227 | FATTR4_WORD1_OWNER_GROUP
228 | FATTR4_WORD1_RAWDEV
229 | FATTR4_WORD1_SPACE_USED
230 | FATTR4_WORD1_TIME_ACCESS
231 | FATTR4_WORD1_TIME_METADATA
232 | FATTR4_WORD1_TIME_MODIFY,
233 FATTR4_WORD2_MDSTHRESHOLD
Trond Myklebust95864c92015-12-26 15:06:03 -0500234#ifdef CONFIG_NFS_V4_SECURITY_LABEL
235 | FATTR4_WORD2_SECURITY_LABEL
236#endif
Trond Myklebust1549210f2012-06-05 09:16:47 -0400237};
238
Andy Adamsone23008e2012-10-02 21:07:32 -0400239static const u32 nfs4_open_noattr_bitmap[3] = {
240 FATTR4_WORD0_TYPE
Andy Adamsone23008e2012-10-02 21:07:32 -0400241 | FATTR4_WORD0_FILEID,
242};
243
David Quigleya09df2c2013-05-22 12:50:41 -0400244const u32 nfs4_statfs_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 FATTR4_WORD0_FILES_AVAIL
246 | FATTR4_WORD0_FILES_FREE
247 | FATTR4_WORD0_FILES_TOTAL,
248 FATTR4_WORD1_SPACE_AVAIL
249 | FATTR4_WORD1_SPACE_FREE
250 | FATTR4_WORD1_SPACE_TOTAL
251};
252
David Quigleya09df2c2013-05-22 12:50:41 -0400253const u32 nfs4_pathconf_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 FATTR4_WORD0_MAXLINK
255 | FATTR4_WORD0_MAXNAME,
256 0
257};
258
Fred Isamandae100c2011-07-30 20:52:37 -0400259const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 | FATTR4_WORD0_MAXREAD
261 | FATTR4_WORD0_MAXWRITE
262 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700263 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400264 | FATTR4_WORD1_FS_LAYOUT_TYPES,
265 FATTR4_WORD2_LAYOUT_BLKSIZE
Peng Tao2a92ee92015-09-26 02:24:37 +0800266 | FATTR4_WORD2_CLONE_BLKSIZE
Frank van der Lindenb78ef842020-06-23 22:38:55 +0000267 | FATTR4_WORD2_XATTR_SUPPORT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268};
269
David Quigleya09df2c2013-05-22 12:50:41 -0400270const u32 nfs4_fs_locations_bitmap[3] = {
Chuck Leverc05cefc2017-11-05 15:45:22 -0500271 FATTR4_WORD0_CHANGE
Manoj Naik830b8e32006-06-09 09:34:25 -0400272 | FATTR4_WORD0_SIZE
273 | FATTR4_WORD0_FSID
274 | FATTR4_WORD0_FILEID
275 | FATTR4_WORD0_FS_LOCATIONS,
Chuck Leverc05cefc2017-11-05 15:45:22 -0500276 FATTR4_WORD1_OWNER
Manoj Naik830b8e32006-06-09 09:34:25 -0400277 | FATTR4_WORD1_OWNER_GROUP
278 | FATTR4_WORD1_RAWDEV
279 | FATTR4_WORD1_SPACE_USED
280 | FATTR4_WORD1_TIME_ACCESS
281 | FATTR4_WORD1_TIME_METADATA
282 | FATTR4_WORD1_TIME_MODIFY
David Quigleya09df2c2013-05-22 12:50:41 -0400283 | FATTR4_WORD1_MOUNTED_ON_FILEID,
Manoj Naik830b8e32006-06-09 09:34:25 -0400284};
285
Trond Myklebust30846df2018-04-07 13:44:28 -0400286static void nfs4_bitmap_copy_adjust(__u32 *dst, const __u32 *src,
Trond Myklebusta71029b2021-04-10 00:23:03 -0400287 struct inode *inode, unsigned long flags)
Trond Myklebust30846df2018-04-07 13:44:28 -0400288{
289 unsigned long cache_validity;
290
291 memcpy(dst, src, NFS4_BITMASK_SZ*sizeof(*dst));
292 if (!inode || !nfs4_have_delegation(inode, FMODE_READ))
293 return;
294
Trond Myklebusta71029b2021-04-10 00:23:03 -0400295 cache_validity = READ_ONCE(NFS_I(inode)->cache_validity) | flags;
Trond Myklebust30846df2018-04-07 13:44:28 -0400296
Trond Myklebusta71029b2021-04-10 00:23:03 -0400297 /* Remove the attributes over which we have full control */
298 dst[1] &= ~FATTR4_WORD1_RAWDEV;
Trond Myklebust30846df2018-04-07 13:44:28 -0400299 if (!(cache_validity & NFS_INO_INVALID_SIZE))
300 dst[0] &= ~FATTR4_WORD0_SIZE;
301
302 if (!(cache_validity & NFS_INO_INVALID_CHANGE))
303 dst[0] &= ~FATTR4_WORD0_CHANGE;
Trond Myklebust30846df2018-04-07 13:44:28 -0400304
Trond Myklebust720869e2021-04-13 09:41:16 -0400305 if (!(cache_validity & NFS_INO_INVALID_MODE))
306 dst[1] &= ~FATTR4_WORD1_MODE;
Trond Myklebusta71029b2021-04-10 00:23:03 -0400307 if (!(cache_validity & NFS_INO_INVALID_OTHER))
Trond Myklebust720869e2021-04-13 09:41:16 -0400308 dst[1] &= ~(FATTR4_WORD1_OWNER | FATTR4_WORD1_OWNER_GROUP);
Trond Myklebust30846df2018-04-07 13:44:28 -0400309}
310
Al Virobc4785c2006-10-19 23:28:51 -0700311static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 struct nfs4_readdir_arg *readdir)
313{
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400314 unsigned int attrs = FATTR4_WORD0_FILEID | FATTR4_WORD0_TYPE;
Al Viro0dbb4c62006-10-19 23:28:49 -0700315 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000318 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
320 return;
321 }
322
323 readdir->cookie = 0;
324 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
325 if (cookie == 2)
326 return;
327
328 /*
329 * NFSv4 servers do not return entries for '.' and '..'
330 * Therefore, we fake these entries here. We let '.'
331 * have cookie 0 and '..' have cookie 1. Note that
332 * when talking to the server, we always send cookie 0
333 * instead of 1 or 2.
334 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800335 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
337 if (cookie == 0) {
338 *p++ = xdr_one; /* next */
339 *p++ = xdr_zero; /* cookie, first word */
340 *p++ = xdr_one; /* cookie, second word */
341 *p++ = xdr_one; /* entry len */
342 memcpy(p, ".\0\0\0", 4); /* entry */
343 p++;
344 *p++ = xdr_one; /* bitmap length */
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400345 *p++ = htonl(attrs); /* bitmap */
346 *p++ = htonl(12); /* attribute buffer length */
347 *p++ = htonl(NF4DIR);
David Howells2b0143b2015-03-17 22:25:59 +0000348 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 }
350
351 *p++ = xdr_one; /* next */
352 *p++ = xdr_zero; /* cookie, first word */
353 *p++ = xdr_two; /* cookie, second word */
354 *p++ = xdr_two; /* entry len */
355 memcpy(p, "..\0\0", 4); /* entry */
356 p++;
357 *p++ = xdr_one; /* bitmap length */
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400358 *p++ = htonl(attrs); /* bitmap */
359 *p++ = htonl(12); /* attribute buffer length */
360 *p++ = htonl(NF4DIR);
David Howells2b0143b2015-03-17 22:25:59 +0000361 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
363 readdir->pgbase = (char *)p - (char *)start;
364 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800365 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366}
367
Trond Myklebust26d36302016-09-22 13:39:05 -0400368static void nfs4_test_and_free_stateid(struct nfs_server *server,
369 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +1100370 const struct cred *cred)
Trond Myklebust26d36302016-09-22 13:39:05 -0400371{
372 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
373
374 ops->test_and_free_expired(server, stateid, cred);
375}
376
377static void __nfs4_free_revoked_stateid(struct nfs_server *server,
378 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +1100379 const struct cred *cred)
Trond Myklebust26d36302016-09-22 13:39:05 -0400380{
381 stateid->type = NFS4_REVOKED_STATEID_TYPE;
382 nfs4_test_and_free_stateid(server, stateid, cred);
383}
384
385static void nfs4_free_revoked_stateid(struct nfs_server *server,
386 const nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +1100387 const struct cred *cred)
Trond Myklebust26d36302016-09-22 13:39:05 -0400388{
389 nfs4_stateid tmp;
390
391 nfs4_stateid_copy(&tmp, stateid);
392 __nfs4_free_revoked_stateid(server, &tmp, cred);
393}
394
NeilBrown8478eaa2014-09-18 16:09:27 +1000395static long nfs4_update_delay(long *timeout)
396{
397 long ret;
398 if (!timeout)
399 return NFS4_POLL_RETRY_MAX;
400 if (*timeout <= 0)
401 *timeout = NFS4_POLL_RETRY_MIN;
402 if (*timeout > NFS4_POLL_RETRY_MAX)
403 *timeout = NFS4_POLL_RETRY_MAX;
404 ret = *timeout;
405 *timeout <<= 1;
406 return ret;
407}
408
Trond Myklebust0688e642019-04-07 13:59:09 -0400409static int nfs4_delay_killable(long *timeout)
Trond Myklebust65de8722008-12-23 15:21:44 -0500410{
Trond Myklebust65de8722008-12-23 15:21:44 -0500411 might_sleep();
412
NeilBrown8478eaa2014-09-18 16:09:27 +1000413 freezable_schedule_timeout_killable_unsafe(
414 nfs4_update_delay(timeout));
Trond Myklebust0688e642019-04-07 13:59:09 -0400415 if (!__fatal_signal_pending(current))
416 return 0;
417 return -EINTR;
418}
419
420static int nfs4_delay_interruptible(long *timeout)
421{
422 might_sleep();
423
He Zhe59679d92020-07-06 17:52:24 +0800424 freezable_schedule_timeout_interruptible_unsafe(nfs4_update_delay(timeout));
Trond Myklebust0688e642019-04-07 13:59:09 -0400425 if (!signal_pending(current))
426 return 0;
427 return __fatal_signal_pending(current) ? -EINTR :-ERESTARTSYS;
428}
429
430static int nfs4_delay(long *timeout, bool interruptible)
431{
432 if (interruptible)
433 return nfs4_delay_interruptible(timeout);
434 return nfs4_delay_killable(timeout);
Trond Myklebust65de8722008-12-23 15:21:44 -0500435}
436
Trond Myklebust50c80002019-07-11 19:02:18 -0400437static const nfs4_stateid *
438nfs4_recoverable_stateid(const nfs4_stateid *stateid)
439{
440 if (!stateid)
441 return NULL;
442 switch (stateid->type) {
443 case NFS4_OPEN_STATEID_TYPE:
444 case NFS4_LOCK_STATEID_TYPE:
445 case NFS4_DELEGATION_STATEID_TYPE:
446 return stateid;
447 default:
448 break;
449 }
450 return NULL;
451}
452
Trond Myklebust65de8722008-12-23 15:21:44 -0500453/* This is the error handling routine for processes that are allowed
454 * to sleep.
455 */
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400456static int nfs4_do_handle_exception(struct nfs_server *server,
457 int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500458{
459 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500460 struct nfs4_state *state = exception->state;
Trond Myklebust50c80002019-07-11 19:02:18 -0400461 const nfs4_stateid *stateid;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500462 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500463 int ret = errorcode;
464
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400465 exception->delay = 0;
466 exception->recovering = 0;
Trond Myklebust65de8722008-12-23 15:21:44 -0500467 exception->retry = 0;
Trond Myklebust272289a2016-09-22 13:39:15 -0400468
Trond Myklebust50c80002019-07-11 19:02:18 -0400469 stateid = nfs4_recoverable_stateid(exception->stateid);
Trond Myklebust272289a2016-09-22 13:39:15 -0400470 if (stateid == NULL && state != NULL)
Trond Myklebust50c80002019-07-11 19:02:18 -0400471 stateid = nfs4_recoverable_stateid(&state->stateid);
Trond Myklebust272289a2016-09-22 13:39:15 -0400472
Trond Myklebust65de8722008-12-23 15:21:44 -0500473 switch(errorcode) {
474 case 0:
475 return 0;
Trond Myklebustcf61eb22018-05-29 22:06:08 -0400476 case -NFS4ERR_BADHANDLE:
477 case -ESTALE:
478 if (inode != NULL && S_ISREG(inode->i_mode))
479 pnfs_destroy_layout(NFS_I(inode));
480 break;
Trond Myklebust5ba12442015-06-16 11:26:35 -0400481 case -NFS4ERR_DELEG_REVOKED:
482 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust272289a2016-09-22 13:39:15 -0400483 case -NFS4ERR_EXPIRED:
Trond Myklebust5ba12442015-06-16 11:26:35 -0400484 case -NFS4ERR_BAD_STATEID:
Olga Kornievskaiafefa1a82019-06-14 14:22:12 -0400485 case -NFS4ERR_PARTNER_NO_AUTH:
Trond Myklebust272289a2016-09-22 13:39:15 -0400486 if (inode != NULL && stateid != NULL) {
487 nfs_inode_find_state_and_recover(inode,
488 stateid);
489 goto wait_on_recovery;
490 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500491 fallthrough;
Trond Myklebust272289a2016-09-22 13:39:15 -0400492 case -NFS4ERR_OPENMODE:
Trond Myklebust8487c472016-06-26 08:44:35 -0400493 if (inode) {
494 int err;
495
496 err = nfs_async_inode_return_delegation(inode,
497 stateid);
498 if (err == 0)
499 goto wait_on_recovery;
500 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
501 exception->retry = 1;
502 break;
503 }
504 }
Trond Myklebust3114ea72012-03-07 16:39:06 -0500505 if (state == NULL)
506 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400507 ret = nfs4_schedule_stateid_recovery(server, state);
508 if (ret < 0)
509 break;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500510 goto wait_on_recovery;
Trond Myklebust65de8722008-12-23 15:21:44 -0500511 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500512 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500513 nfs4_schedule_lease_recovery(clp);
514 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -0400515 case -NFS4ERR_MOVED:
516 ret = nfs4_schedule_migration_recovery(server);
517 if (ret < 0)
518 break;
519 goto wait_on_recovery;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -0400520 case -NFS4ERR_LEASE_MOVED:
521 nfs4_schedule_lease_moved_recovery(clp);
522 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500523#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400524 case -NFS4ERR_BADSESSION:
525 case -NFS4ERR_BADSLOT:
526 case -NFS4ERR_BAD_HIGH_SLOT:
527 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
528 case -NFS4ERR_DEADSESSION:
529 case -NFS4ERR_SEQ_FALSE_RETRY:
530 case -NFS4ERR_SEQ_MISORDERED:
Trond Myklebust5c441542019-11-13 08:34:00 +0100531 /* Handled in nfs41_sequence_process() */
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400532 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500533#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500534 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500535 if (exception->timeout > HZ) {
536 /* We have retried a decent amount, time to
537 * fail
538 */
539 ret = -EBUSY;
540 break;
541 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500542 fallthrough;
Trond Myklebust65de8722008-12-23 15:21:44 -0500543 case -NFS4ERR_DELAY:
Trond Myklebust2598ed32015-09-20 16:10:18 -0400544 nfs_inc_server_stats(server, NFSIOS_DELAY);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500545 fallthrough;
Trond Myklebust2598ed32015-09-20 16:10:18 -0400546 case -NFS4ERR_GRACE:
Trond Myklebuste85d7ee2016-07-14 18:46:24 -0400547 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -0400548 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400549 exception->delay = 1;
550 return 0;
551
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400552 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500553 case -NFS4ERR_OLD_STATEID:
554 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800555 break;
556 case -NFS4ERR_BADOWNER:
557 /* The following works around a Linux server bug! */
558 case -NFS4ERR_BADNAME:
559 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
560 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
561 exception->retry = 1;
562 printk(KERN_WARNING "NFS: v4 server %s "
563 "does not accept raw "
564 "uid/gids. "
565 "Reenabling the idmapper.\n",
566 server->nfs_client->cl_hostname);
567 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500568 }
569 /* We failed to handle the error */
570 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500571wait_on_recovery:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400572 exception->recovering = 1;
573 return 0;
574}
575
576/* This is the error handling routine for processes that are allowed
577 * to sleep.
578 */
579int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
580{
581 struct nfs_client *clp = server->nfs_client;
582 int ret;
583
584 ret = nfs4_do_handle_exception(server, errorcode, exception);
585 if (exception->delay) {
Trond Myklebust0688e642019-04-07 13:59:09 -0400586 ret = nfs4_delay(&exception->timeout,
587 exception->interruptible);
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400588 goto out_retry;
589 }
590 if (exception->recovering) {
591 ret = nfs4_wait_clnt_recover(clp);
592 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
593 return -EIO;
594 goto out_retry;
595 }
596 return ret;
597out_retry:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500598 if (ret == 0)
599 exception->retry = 1;
600 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500601}
602
Trond Myklebust037fc982015-09-20 15:51:00 -0400603static int
604nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
605 int errorcode, struct nfs4_exception *exception)
606{
607 struct nfs_client *clp = server->nfs_client;
608 int ret;
609
610 ret = nfs4_do_handle_exception(server, errorcode, exception);
611 if (exception->delay) {
612 rpc_delay(task, nfs4_update_delay(&exception->timeout));
613 goto out_retry;
614 }
615 if (exception->recovering) {
616 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
617 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
618 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
619 goto out_retry;
620 }
621 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
622 ret = -EIO;
623 return ret;
624out_retry:
Bill Baker0f90be12018-06-19 16:24:58 -0500625 if (ret == 0) {
Trond Myklebust037fc982015-09-20 15:51:00 -0400626 exception->retry = 1;
Bill Baker0f90be12018-06-19 16:24:58 -0500627 /*
628 * For NFS4ERR_MOVED, the client transport will need to
629 * be recomputed after migration recovery has completed.
630 */
631 if (errorcode == -NFS4ERR_MOVED)
632 rpc_task_release_transport(task);
633 }
Trond Myklebust037fc982015-09-20 15:51:00 -0400634 return ret;
635}
636
Olga Kornievskaia0f913a52018-07-09 15:13:33 -0400637int
Trond Myklebust037fc982015-09-20 15:51:00 -0400638nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
639 struct nfs4_state *state, long *timeout)
640{
641 struct nfs4_exception exception = {
642 .state = state,
643 };
644
645 if (task->tk_status >= 0)
646 return 0;
647 if (timeout)
648 exception.timeout = *timeout;
649 task->tk_status = nfs4_async_handle_exception(task, server,
650 task->tk_status,
651 &exception);
652 if (exception.delay && timeout)
653 *timeout = exception.timeout;
654 if (exception.retry)
655 return -EAGAIN;
656 return 0;
657}
658
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400659/*
660 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
661 * or 'false' otherwise.
662 */
663static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
664{
665 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
Anna Schumakereeea5362017-01-11 16:01:21 -0500666 return (flavor == RPC_AUTH_GSS_KRB5I) || (flavor == RPC_AUTH_GSS_KRB5P);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400667}
Trond Myklebust65de8722008-12-23 15:21:44 -0500668
Trond Myklebust452e9352010-07-31 14:29:06 -0400669static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 spin_lock(&clp->cl_lock);
672 if (time_before(clp->cl_last_renewal,timestamp))
673 clp->cl_last_renewal = timestamp;
674 spin_unlock(&clp->cl_lock);
675}
676
Trond Myklebust452e9352010-07-31 14:29:06 -0400677static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
678{
Trond Myklebustbe824162015-07-05 14:50:46 -0400679 struct nfs_client *clp = server->nfs_client;
680
681 if (!nfs4_has_session(clp))
682 do_renew_lease(clp, timestamp);
Trond Myklebust452e9352010-07-31 14:29:06 -0400683}
684
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400685struct nfs4_call_sync_data {
686 const struct nfs_server *seq_server;
687 struct nfs4_sequence_args *seq_args;
688 struct nfs4_sequence_res *seq_res;
689};
690
Trond Myklebustbe3a5d22015-06-23 19:51:55 +0800691void nfs4_init_sequence(struct nfs4_sequence_args *args,
Anna Schumakerfba83f32018-05-04 16:22:50 -0400692 struct nfs4_sequence_res *res, int cache_reply,
693 int privileged)
Chuck Levera9c92d62013-08-09 12:48:18 -0400694{
695 args->sa_slot = NULL;
696 args->sa_cache_this = cache_reply;
Anna Schumakerfba83f32018-05-04 16:22:50 -0400697 args->sa_privileged = privileged;
Chuck Levera9c92d62013-08-09 12:48:18 -0400698
699 res->sr_slot = NULL;
700}
701
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400702static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
Chuck Lever3bd23842013-08-09 12:49:19 -0400703{
704 struct nfs4_slot *slot = res->sr_slot;
705 struct nfs4_slot_table *tbl;
706
Chuck Lever3bd23842013-08-09 12:49:19 -0400707 tbl = slot->table;
708 spin_lock(&tbl->slot_tbl_lock);
709 if (!nfs41_wake_and_assign_slot(tbl, slot))
710 nfs4_free_slot(tbl, slot);
711 spin_unlock(&tbl->slot_tbl_lock);
712
713 res->sr_slot = NULL;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400714}
715
716static int nfs40_sequence_done(struct rpc_task *task,
717 struct nfs4_sequence_res *res)
718{
719 if (res->sr_slot != NULL)
720 nfs40_sequence_free_slot(res);
Chuck Lever3bd23842013-08-09 12:49:19 -0400721 return 1;
722}
723
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400724#if defined(CONFIG_NFS_V4_1)
725
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400726static void nfs41_release_slot(struct nfs4_slot *slot)
Andy Adamson13615872009-04-01 09:22:17 -0400727{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500728 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400729 struct nfs4_slot_table *tbl;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500730 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400731
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400732 if (!slot)
733 return;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500734 tbl = slot->table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500735 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500736
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400737 /* Bump the slot sequence number */
738 if (slot->seq_done)
739 slot->seq_nr++;
740 slot->seq_done = 0;
741
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500742 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500743 /* Be nice to the server: try to ensure that the last transmitted
744 * value for highest_user_slotid <= target_highest_slotid
745 */
746 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
747 send_new_highest_used_slotid = true;
748
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500749 if (nfs41_wake_and_assign_slot(tbl, slot)) {
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500750 send_new_highest_used_slotid = false;
751 goto out_unlock;
752 }
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500753 nfs4_free_slot(tbl, slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500754
755 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
756 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500757out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500758 spin_unlock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500759 if (send_new_highest_used_slotid)
Anna Schumaker3f10a6a2015-07-13 14:01:31 -0400760 nfs41_notify_server(session->clp);
Trond Myklebust045d2a62016-08-28 13:25:43 -0400761 if (waitqueue_active(&tbl->slot_waitq))
762 wake_up_all(&tbl->slot_waitq);
Andy Adamson13615872009-04-01 09:22:17 -0400763}
764
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400765static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
766{
767 nfs41_release_slot(res->sr_slot);
768 res->sr_slot = NULL;
769}
770
Trond Myklebust3453d572018-06-20 17:53:34 -0400771static void nfs4_slot_sequence_record_sent(struct nfs4_slot *slot,
772 u32 seqnr)
773{
774 if ((s32)(seqnr - slot->seq_nr_highest_sent) > 0)
775 slot->seq_nr_highest_sent = seqnr;
776}
777static void nfs4_slot_sequence_acked(struct nfs4_slot *slot,
778 u32 seqnr)
779{
780 slot->seq_nr_highest_sent = seqnr;
781 slot->seq_nr_last_acked = seqnr;
782}
783
Anna Schumaker913fadc2020-07-08 10:33:40 -0400784static void nfs4_probe_sequence(struct nfs_client *client, const struct cred *cred,
785 struct nfs4_slot *slot)
786{
787 struct rpc_task *task = _nfs41_proc_sequence(client, cred, slot, true);
788 if (!IS_ERR(task))
789 rpc_put_task_async(task);
790}
791
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400792static int nfs41_sequence_process(struct rpc_task *task,
793 struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400794{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500795 struct nfs4_session *session;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500796 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400797 struct nfs_client *clp;
Trond Myklebust5c441542019-11-13 08:34:00 +0100798 int status;
Trond Myklebust85563072012-12-11 10:31:12 -0500799 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400800
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500801 if (slot == NULL)
802 goto out_noaction;
Bryan Schumaker468f8612011-04-18 15:57:32 -0400803 /* don't increment the sequence number if the task wasn't sent */
Trond Myklebustc71c46f2019-03-01 11:40:05 -0500804 if (!RPC_WAS_SENT(task) || slot->seq_done)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400805 goto out;
806
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500807 session = slot->table->session;
Anna Schumaker913fadc2020-07-08 10:33:40 -0400808 clp = session->clp;
Trond Myklebust933602e2012-11-16 12:12:38 -0500809
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400810 trace_nfs4_sequence_done(session, res);
Trond Myklebust5c441542019-11-13 08:34:00 +0100811
812 status = res->sr_status;
813 if (task->tk_status == -NFS4ERR_DEADSESSION)
814 status = -NFS4ERR_DEADSESSION;
815
Andy Adamson691daf32009-12-04 15:55:39 -0500816 /* Check the SEQUENCE operation status */
Trond Myklebust5c441542019-11-13 08:34:00 +0100817 switch (status) {
Trond Myklebust14516c32010-07-31 14:29:06 -0400818 case 0:
Trond Myklebust3453d572018-06-20 17:53:34 -0400819 /* Mark this sequence number as having been acked */
820 nfs4_slot_sequence_acked(slot, slot->seq_nr);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400821 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400822 slot->seq_done = 1;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500823 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500824 /* Check sequence flags */
Trond Myklebust0a014a42016-09-22 13:38:51 -0400825 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
826 !!slot->privileged);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500827 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400828 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500829 case 1:
830 /*
831 * sr_status remains 1 if an RPC level error occurred.
832 * The server may or may not have processed the sequence
833 * operation..
Trond Myklebustac20d162012-12-15 15:36:07 -0500834 */
Trond Myklebust3453d572018-06-20 17:53:34 -0400835 nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
836 slot->seq_done = 1;
Trond Myklebustac20d162012-12-15 15:36:07 -0500837 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400838 case -NFS4ERR_DELAY:
839 /* The server detected a resend of the RPC call and
840 * returned NFS4ERR_DELAY as per Section 2.10.6.2
841 * of RFC5661.
842 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500843 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400844 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500845 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500846 slot->seq_nr);
Trond Myklebust3453d572018-06-20 17:53:34 -0400847 nfs4_slot_sequence_acked(slot, slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400848 goto out_retry;
Trond Myklebustf9312a52018-06-09 19:10:31 -0400849 case -NFS4ERR_RETRY_UNCACHED_REP:
850 case -NFS4ERR_SEQ_FALSE_RETRY:
851 /*
852 * The server thinks we tried to replay a request.
853 * Retry the call after bumping the sequence ID.
854 */
Trond Myklebust3453d572018-06-20 17:53:34 -0400855 nfs4_slot_sequence_acked(slot, slot->seq_nr);
Trond Myklebustf9312a52018-06-09 19:10:31 -0400856 goto retry_new_seq;
Trond Myklebust85563072012-12-11 10:31:12 -0500857 case -NFS4ERR_BADSLOT:
858 /*
859 * The slot id we used was probably retired. Try again
860 * using a different slot id.
861 */
Trond Myklebust99589102018-06-09 12:50:50 -0400862 if (slot->slot_nr < slot->table->target_highest_slotid)
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400863 goto session_recover;
Trond Myklebuste8794442012-12-15 13:56:18 -0500864 goto retry_nowait;
865 case -NFS4ERR_SEQ_MISORDERED:
Trond Myklebust3453d572018-06-20 17:53:34 -0400866 nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
Trond Myklebuste8794442012-12-15 13:56:18 -0500867 /*
Trond Myklebust3453d572018-06-20 17:53:34 -0400868 * Were one or more calls using this slot interrupted?
869 * If the server never received the request, then our
Anna Schumaker913fadc2020-07-08 10:33:40 -0400870 * transmitted slot sequence number may be too high. However,
871 * if the server did receive the request then it might
872 * accidentally give us a reply with a mismatched operation.
873 * We can sort this out by sending a lone sequence operation
874 * to the server on the same slot.
Trond Myklebustac20d162012-12-15 15:36:07 -0500875 */
Trond Myklebust3453d572018-06-20 17:53:34 -0400876 if ((s32)(slot->seq_nr - slot->seq_nr_last_acked) > 1) {
877 slot->seq_nr--;
Anna Schumaker913fadc2020-07-08 10:33:40 -0400878 if (task->tk_msg.rpc_proc != &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE]) {
879 nfs4_probe_sequence(clp, task->tk_msg.rpc_cred, slot);
880 res->sr_slot = NULL;
881 }
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500882 goto retry_nowait;
883 }
Trond Myklebust3453d572018-06-20 17:53:34 -0400884 /*
885 * RFC5661:
886 * A retry might be sent while the original request is
887 * still in progress on the replier. The replier SHOULD
888 * deal with the issue by returning NFS4ERR_DELAY as the
889 * reply to SEQUENCE or CB_SEQUENCE operation, but
890 * implementations MAY return NFS4ERR_SEQ_MISORDERED.
891 *
892 * Restart the search after a delay.
893 */
894 slot->seq_nr = slot->seq_nr_highest_sent;
895 goto out_retry;
Trond Myklebust5c441542019-11-13 08:34:00 +0100896 case -NFS4ERR_BADSESSION:
897 case -NFS4ERR_DEADSESSION:
898 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
899 goto session_recover;
Trond Myklebust14516c32010-07-31 14:29:06 -0400900 default:
901 /* Just update the slot sequence no. */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400902 slot->seq_done = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400903 }
904out:
905 /* The session may be reset by one of the error handlers. */
906 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500907out_noaction:
Trond Myklebust85563072012-12-11 10:31:12 -0500908 return ret;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400909session_recover:
Trond Myklebust5c441542019-11-13 08:34:00 +0100910 nfs4_schedule_session_recovery(session, status);
911 dprintk("%s ERROR: %d Reset session\n", __func__, status);
912 nfs41_sequence_free_slot(res);
913 goto out;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400914retry_new_seq:
915 ++slot->seq_nr;
Trond Myklebuste8794442012-12-15 13:56:18 -0500916retry_nowait:
917 if (rpc_restart_call_prepare(task)) {
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400918 nfs41_sequence_free_slot(res);
Trond Myklebuste8794442012-12-15 13:56:18 -0500919 task->tk_status = 0;
920 ret = 0;
921 }
922 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400923out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400924 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400925 goto out;
926 rpc_delay(task, NFS4_POLL_RETRY_MAX);
927 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400928}
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400929
930int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
931{
932 if (!nfs41_sequence_process(task, res))
933 return 0;
934 if (res->sr_slot != NULL)
935 nfs41_sequence_free_slot(res);
936 return 1;
937
938}
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500939EXPORT_SYMBOL_GPL(nfs41_sequence_done);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400940
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400941static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
942{
943 if (res->sr_slot == NULL)
944 return 1;
945 if (res->sr_slot->table->session != NULL)
946 return nfs41_sequence_process(task, res);
947 return nfs40_sequence_done(task, res);
948}
949
950static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
951{
952 if (res->sr_slot != NULL) {
953 if (res->sr_slot->table->session != NULL)
954 nfs41_sequence_free_slot(res);
955 else
956 nfs40_sequence_free_slot(res);
957 }
958}
959
Peng Tao2c4b1312014-06-11 05:24:16 +0800960int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400961{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500962 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400963 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400964 if (!res->sr_slot->table->session)
965 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400966 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400967}
Peng Tao2c4b1312014-06-11 05:24:16 +0800968EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Trond Myklebustdf896452010-06-16 09:52:26 -0400969
Andy Adamsonce5039c2009-04-01 09:22:13 -0400970static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
971{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400972 struct nfs4_call_sync_data *data = calldata;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400973
Trond Myklebust035168ab2010-06-16 09:52:26 -0400974 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
975
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500976 nfs4_setup_sequence(data->seq_server->nfs_client,
977 data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400978}
979
Andy Adamson69ab40c2009-04-01 09:22:19 -0400980static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
981{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400982 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400983
Trond Myklebust14516c32010-07-31 14:29:06 -0400984 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400985}
986
Trond Myklebust17280172012-03-11 13:11:00 -0400987static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400988 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400989 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400990};
991
Chuck Lever3bd23842013-08-09 12:49:19 -0400992#else /* !CONFIG_NFS_V4_1 */
993
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400994static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
995{
996 return nfs40_sequence_done(task, res);
997}
998
999static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
1000{
1001 if (res->sr_slot != NULL)
1002 nfs40_sequence_free_slot(res);
1003}
1004
Peng Tao2c4b1312014-06-11 05:24:16 +08001005int nfs4_sequence_done(struct rpc_task *task,
1006 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -04001007{
Chuck Lever3bd23842013-08-09 12:49:19 -04001008 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -04001009}
Peng Tao2c4b1312014-06-11 05:24:16 +08001010EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Chuck Lever3bd23842013-08-09 12:49:19 -04001011
1012#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001013
Trond Myklebustc1dffe02019-03-01 12:13:34 -05001014static void nfs41_sequence_res_init(struct nfs4_sequence_res *res)
1015{
1016 res->sr_timestamp = jiffies;
1017 res->sr_status_flags = 0;
1018 res->sr_status = 1;
1019}
1020
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04001021static
1022void nfs4_sequence_attach_slot(struct nfs4_sequence_args *args,
1023 struct nfs4_sequence_res *res,
1024 struct nfs4_slot *slot)
1025{
1026 if (!slot)
1027 return;
1028 slot->privileged = args->sa_privileged ? 1 : 0;
1029 args->sa_slot = slot;
1030
1031 res->sr_slot = slot;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04001032}
1033
1034int nfs4_setup_sequence(struct nfs_client *client,
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001035 struct nfs4_sequence_args *args,
1036 struct nfs4_sequence_res *res,
1037 struct rpc_task *task)
1038{
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001039 struct nfs4_session *session = nfs4_get_session(client);
Anna Schumaker76ee0352017-01-10 16:49:31 -05001040 struct nfs4_slot_table *tbl = client->cl_slot_tbl;
Anna Schumaker3d358082017-01-11 10:54:04 -05001041 struct nfs4_slot *slot;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001042
Anna Schumaker9dd9107f2017-01-10 12:01:46 -05001043 /* slot already allocated? */
1044 if (res->sr_slot != NULL)
1045 goto out_start;
1046
Trond Myklebust6b2e6852019-04-07 13:58:49 -04001047 if (session)
Anna Schumaker76ee0352017-01-10 16:49:31 -05001048 tbl = &session->fc_slot_table;
Anna Schumaker76ee0352017-01-10 16:49:31 -05001049
Trond Myklebust3453d572018-06-20 17:53:34 -04001050 spin_lock(&tbl->slot_tbl_lock);
1051 /* The state manager will wait until the slot table is empty */
1052 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
1053 goto out_sleep;
Anna Schumaker6994cdd2017-01-10 16:13:27 -05001054
Trond Myklebust3453d572018-06-20 17:53:34 -04001055 slot = nfs4_alloc_slot(tbl);
1056 if (IS_ERR(slot)) {
Trond Myklebust3453d572018-06-20 17:53:34 -04001057 if (slot == ERR_PTR(-ENOMEM))
Trond Myklebust6b2e6852019-04-07 13:58:49 -04001058 goto out_sleep_timeout;
Trond Myklebust3453d572018-06-20 17:53:34 -04001059 goto out_sleep;
Anna Schumaker3d358082017-01-11 10:54:04 -05001060 }
Trond Myklebust3453d572018-06-20 17:53:34 -04001061 spin_unlock(&tbl->slot_tbl_lock);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001062
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04001063 nfs4_sequence_attach_slot(args, res, slot);
Anna Schumaker3d358082017-01-11 10:54:04 -05001064
Anna Schumakerad05cc02017-01-11 13:37:06 -05001065 trace_nfs4_setup_sequence(session, args);
Anna Schumaker9dd9107f2017-01-10 12:01:46 -05001066out_start:
Trond Myklebustc1dffe02019-03-01 12:13:34 -05001067 nfs41_sequence_res_init(res);
Anna Schumaker9dd9107f2017-01-10 12:01:46 -05001068 rpc_call_start(task);
1069 return 0;
Trond Myklebust6b2e6852019-04-07 13:58:49 -04001070out_sleep_timeout:
1071 /* Try again in 1/4 second */
1072 if (args->sa_privileged)
1073 rpc_sleep_on_priority_timeout(&tbl->slot_tbl_waitq, task,
1074 jiffies + (HZ >> 2), RPC_PRIORITY_PRIVILEGED);
1075 else
1076 rpc_sleep_on_timeout(&tbl->slot_tbl_waitq, task,
1077 NULL, jiffies + (HZ >> 2));
1078 spin_unlock(&tbl->slot_tbl_lock);
1079 return -EAGAIN;
Anna Schumaker0dcee8b2017-01-10 16:29:54 -05001080out_sleep:
1081 if (args->sa_privileged)
1082 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
Trond Myklebust8357a9b2019-04-07 13:58:48 -04001083 RPC_PRIORITY_PRIVILEGED);
Anna Schumaker0dcee8b2017-01-10 16:29:54 -05001084 else
1085 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
1086 spin_unlock(&tbl->slot_tbl_lock);
1087 return -EAGAIN;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001088}
1089EXPORT_SYMBOL_GPL(nfs4_setup_sequence);
1090
Chuck Lever9915ea72013-08-09 12:48:27 -04001091static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
1092{
1093 struct nfs4_call_sync_data *data = calldata;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05001094 nfs4_setup_sequence(data->seq_server->nfs_client,
Chuck Lever9915ea72013-08-09 12:48:27 -04001095 data->seq_args, data->seq_res, task);
1096}
1097
1098static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
1099{
1100 struct nfs4_call_sync_data *data = calldata;
1101 nfs4_sequence_done(task, data->seq_res);
1102}
1103
1104static const struct rpc_call_ops nfs40_call_sync_ops = {
1105 .rpc_call_prepare = nfs40_call_sync_prepare,
1106 .rpc_call_done = nfs40_call_sync_done,
1107};
1108
Anna Schumaker48c05852019-08-14 15:27:00 -04001109static int nfs4_call_sync_custom(struct rpc_task_setup *task_setup)
1110{
1111 int ret;
1112 struct rpc_task *task;
1113
1114 task = rpc_run_task(task_setup);
1115 if (IS_ERR(task))
1116 return PTR_ERR(task);
1117
1118 ret = task->tk_status;
1119 rpc_put_task(task);
1120 return ret;
1121}
1122
Trond Myklebustc74dfe92020-01-06 15:39:37 -05001123static int nfs4_do_call_sync(struct rpc_clnt *clnt,
1124 struct nfs_server *server,
1125 struct rpc_message *msg,
1126 struct nfs4_sequence_args *args,
1127 struct nfs4_sequence_res *res,
1128 unsigned short task_flags)
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001129{
Chuck Lever9915ea72013-08-09 12:48:27 -04001130 struct nfs_client *clp = server->nfs_client;
1131 struct nfs4_call_sync_data data = {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001132 .seq_server = server,
Trond Myklebustad389da2007-06-05 12:30:00 -04001133 .seq_args = args,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001134 .seq_res = res,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001135 };
1136 struct rpc_task_setup task_setup = {
Trond Myklebustad389da2007-06-05 12:30:00 -04001137 .rpc_client = clnt,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001138 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -04001139 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebustc74dfe92020-01-06 15:39:37 -05001140 .callback_data = &data,
1141 .flags = task_flags,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001142 };
1143
Anna Schumaker48c05852019-08-14 15:27:00 -04001144 return nfs4_call_sync_custom(&task_setup);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001145}
1146
Trond Myklebustc74dfe92020-01-06 15:39:37 -05001147static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
1148 struct nfs_server *server,
1149 struct rpc_message *msg,
1150 struct nfs4_sequence_args *args,
1151 struct nfs4_sequence_res *res)
1152{
1153 return nfs4_do_call_sync(clnt, server, msg, args, res, 0);
1154}
1155
1156
Bryan Schumaker7c513052011-03-24 17:12:24 +00001157int nfs4_call_sync(struct rpc_clnt *clnt,
1158 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001159 struct rpc_message *msg,
1160 struct nfs4_sequence_args *args,
1161 struct nfs4_sequence_res *res,
1162 int cache_reply)
1163{
Anna Schumakerfba83f32018-05-04 16:22:50 -04001164 nfs4_init_sequence(args, res, cache_reply, 0);
Chuck Lever9915ea72013-08-09 12:48:27 -04001165 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001166}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
Trond Myklebust3c591172018-07-31 15:54:10 -04001168static void
1169nfs4_inc_nlink_locked(struct inode *inode)
1170{
Trond Myklebustfabf2b32021-03-25 13:14:42 -04001171 nfs_set_cache_invalid(inode, NFS_INO_INVALID_NLINK);
Trond Myklebust3c591172018-07-31 15:54:10 -04001172 inc_nlink(inode);
1173}
1174
1175static void
1176nfs4_dec_nlink_locked(struct inode *inode)
1177{
Trond Myklebustfabf2b32021-03-25 13:14:42 -04001178 nfs_set_cache_invalid(inode, NFS_INO_INVALID_NLINK);
Trond Myklebust3c591172018-07-31 15:54:10 -04001179 drop_nlink(inode);
1180}
1181
1182static void
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001183nfs4_update_changeattr_locked(struct inode *inode,
1184 struct nfs4_change_info *cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04001185 unsigned long timestamp, unsigned long cache_validity)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186{
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001187 struct nfs_inode *nfsi = NFS_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
Trond Myklebustb6f80a22021-03-08 14:42:55 -05001189 cache_validity |= NFS_INO_INVALID_CTIME | NFS_INO_INVALID_MTIME;
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001190
1191 if (cinfo->atomic && cinfo->before == inode_peek_iversion_raw(inode)) {
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001192 nfsi->attrtimeo_timestamp = jiffies;
1193 } else {
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001194 if (S_ISDIR(inode->i_mode)) {
Trond Myklebustb6f80a22021-03-08 14:42:55 -05001195 cache_validity |= NFS_INO_INVALID_DATA;
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001196 nfs_force_lookup_revalidate(inode);
1197 } else {
1198 if (!NFS_PROTO(inode)->have_delegation(inode,
1199 FMODE_READ))
Trond Myklebustb6f80a22021-03-08 14:42:55 -05001200 cache_validity |= NFS_INO_REVAL_PAGECACHE;
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001201 }
1202
1203 if (cinfo->before != inode_peek_iversion_raw(inode))
Trond Myklebustb6f80a22021-03-08 14:42:55 -05001204 cache_validity |= NFS_INO_INVALID_ACCESS |
1205 NFS_INO_INVALID_ACL |
1206 NFS_INO_INVALID_XATTR;
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001207 }
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001208 inode_set_iversion_raw(inode, cinfo->after);
Trond Myklebustd3129ef2017-01-11 22:07:28 -05001209 nfsi->read_cache_jiffies = timestamp;
Trond Myklebust3235b402015-02-26 19:52:06 -05001210 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
Trond Myklebustb6f80a22021-03-08 14:42:55 -05001211 nfs_set_cache_invalid(inode, cache_validity);
Trond Myklebustc8d07152018-07-31 15:54:12 -04001212 nfsi->cache_validity &= ~NFS_INO_INVALID_CHANGE;
Trond Myklebust3c591172018-07-31 15:54:10 -04001213}
1214
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001215void
1216nfs4_update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04001217 unsigned long timestamp, unsigned long cache_validity)
Trond Myklebust3c591172018-07-31 15:54:10 -04001218{
1219 spin_lock(&dir->i_lock);
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001220 nfs4_update_changeattr_locked(dir, cinfo, timestamp, cache_validity);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 spin_unlock(&dir->i_lock);
1222}
1223
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001224struct nfs4_open_createattrs {
1225 struct nfs4_label *label;
1226 struct iattr *sattr;
1227 const __u32 verf[2];
1228};
1229
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001230static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1231 int err, struct nfs4_exception *exception)
1232{
1233 if (err != -EINVAL)
1234 return false;
1235 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1236 return false;
1237 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1238 exception->retry = 1;
1239 return true;
1240}
1241
Trond Myklebust1bf85d82019-06-27 06:30:48 -04001242static fmode_t _nfs4_ctx_to_accessmode(const struct nfs_open_context *ctx)
1243{
1244 return ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
1245}
1246
1247static fmode_t _nfs4_ctx_to_openmode(const struct nfs_open_context *ctx)
1248{
1249 fmode_t ret = ctx->mode & (FMODE_READ|FMODE_WRITE);
1250
1251 return (ctx->mode & FMODE_EXEC) ? FMODE_READ | ret : ret;
1252}
1253
Trond Myklebust6ae37332015-01-30 14:21:14 -05001254static u32
1255nfs4_map_atomic_open_share(struct nfs_server *server,
1256 fmode_t fmode, int openflags)
1257{
1258 u32 res = 0;
1259
1260 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1261 case FMODE_READ:
1262 res = NFS4_SHARE_ACCESS_READ;
1263 break;
1264 case FMODE_WRITE:
1265 res = NFS4_SHARE_ACCESS_WRITE;
1266 break;
1267 case FMODE_READ|FMODE_WRITE:
1268 res = NFS4_SHARE_ACCESS_BOTH;
1269 }
1270 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1271 goto out;
1272 /* Want no delegation if we're using O_DIRECT */
1273 if (openflags & O_DIRECT)
1274 res |= NFS4_SHARE_WANT_NO_DELEG;
1275out:
1276 return res;
1277}
1278
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001279static enum open_claim_type4
1280nfs4_map_atomic_open_claim(struct nfs_server *server,
1281 enum open_claim_type4 claim)
1282{
1283 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1284 return claim;
1285 switch (claim) {
1286 default:
1287 return claim;
1288 case NFS4_OPEN_CLAIM_FH:
1289 return NFS4_OPEN_CLAIM_NULL;
1290 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1291 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1292 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1293 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1294 }
1295}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
1297static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001298{
1299 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001300 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001301 p->o_res.seqid = p->o_arg.seqid;
1302 p->c_res.seqid = p->c_arg.seqid;
1303 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -04001304 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001305 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd12012-01-07 13:22:46 -05001306 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001307}
1308
Al Viro82a2c1b2011-06-22 18:30:55 -04001309static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001310 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001311 const struct nfs4_open_createattrs *c,
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001312 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001313 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001314{
Al Viro82a2c1b2011-06-22 18:30:55 -04001315 struct dentry *parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001316 struct inode *dir = d_inode(parent);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001317 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust63f5f792015-01-23 19:19:25 -05001318 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001319 struct nfs4_label *label = (c != NULL) ? c->label : NULL;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001320 struct nfs4_opendata *p;
1321
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001322 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001323 if (p == NULL)
1324 goto err;
David Quigley14c43f72013-05-22 12:50:43 -04001325
1326 p->f_label = nfs4_label_alloc(server, gfp_mask);
1327 if (IS_ERR(p->f_label))
1328 goto err_free_p;
1329
Kinglong Meea49c2692015-07-27 15:31:38 +08001330 p->a_label = nfs4_label_alloc(server, gfp_mask);
1331 if (IS_ERR(p->a_label))
1332 goto err_free_f;
1333
Trond Myklebust63f5f792015-01-23 19:19:25 -05001334 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1335 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05001336 if (IS_ERR(p->o_arg.seqid))
David Quigley14c43f72013-05-22 12:50:43 -04001337 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -04001338 nfs_sb_active(dentry->d_sb);
1339 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001340 p->dir = parent;
1341 p->owner = sp;
1342 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001343 p->o_arg.open_flags = flags;
1344 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Trond Myklebust536585c2016-11-10 15:40:34 -05001345 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
Trond Myklebust6ae37332015-01-30 14:21:14 -05001346 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1347 fmode, flags);
Benjamin Coddington90910512019-06-07 06:37:30 -04001348 if (flags & O_CREAT) {
1349 p->o_arg.umask = current_umask();
1350 p->o_arg.label = nfs4_label_copy(p->a_label, label);
1351 if (c->sattr != NULL && c->sattr->ia_valid != 0) {
1352 p->o_arg.u.attrs = &p->attrs;
1353 memcpy(&p->attrs, c->sattr, sizeof(p->attrs));
1354
1355 memcpy(p->o_arg.u.verifier.data, c->verf,
1356 sizeof(p->o_arg.u.verifier.data));
1357 }
1358 }
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001359 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1360 * will return permission denied for all bits until close */
1361 if (!(flags & O_EXCL)) {
1362 /* ask server to check for all possible rights as results
1363 * are cached */
Trond Myklebust536585c2016-11-10 15:40:34 -05001364 switch (p->o_arg.claim) {
1365 default:
1366 break;
1367 case NFS4_OPEN_CLAIM_NULL:
1368 case NFS4_OPEN_CLAIM_FH:
1369 p->o_arg.access = NFS4_ACCESS_READ |
1370 NFS4_ACCESS_MODIFY |
1371 NFS4_ACCESS_EXTEND |
1372 NFS4_ACCESS_EXECUTE;
Frank van der Linden72832a22020-06-23 22:38:58 +00001373#ifdef CONFIG_NFS_V4_2
1374 if (server->caps & NFS_CAP_XATTR)
1375 p->o_arg.access |= NFS4_ACCESS_XAREAD |
1376 NFS4_ACCESS_XAWRITE |
1377 NFS4_ACCESS_XALIST;
1378#endif
Trond Myklebust536585c2016-11-10 15:40:34 -05001379 }
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001380 }
David Howells7539bba2006-08-22 20:06:09 -04001381 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001382 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1383 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001384 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001385 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001386 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001387 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001388 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001389 case NFS4_OPEN_CLAIM_NULL:
1390 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1391 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1392 p->o_arg.fh = NFS_FH(dir);
1393 break;
1394 case NFS4_OPEN_CLAIM_PREVIOUS:
1395 case NFS4_OPEN_CLAIM_FH:
1396 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1397 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
David Howells2b0143b2015-03-17 22:25:59 +00001398 p->o_arg.fh = NFS_FH(d_inode(dentry));
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001399 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001400 p->c_arg.fh = &p->o_res.fh;
1401 p->c_arg.stateid = &p->o_res.stateid;
1402 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001403 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001404 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001405 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001406
1407err_free_label:
Kinglong Meea49c2692015-07-27 15:31:38 +08001408 nfs4_label_free(p->a_label);
1409err_free_f:
David Quigley14c43f72013-05-22 12:50:43 -04001410 nfs4_label_free(p->f_label);
1411err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001412 kfree(p);
1413err:
1414 dput(parent);
1415 return NULL;
1416}
1417
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001418static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001419{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001420 struct nfs4_opendata *p = container_of(kref,
1421 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001422 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001423
Fred Isaman30ae2412016-10-18 13:39:51 -04001424 nfs4_lgopen_release(p->lgp);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001425 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001426 nfs4_sequence_free_slot(&p->o_res.seq_res);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001427 if (p->state != NULL)
1428 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001429 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001430
Kinglong Meea49c2692015-07-27 15:31:38 +08001431 nfs4_label_free(p->a_label);
David Quigley14c43f72013-05-22 12:50:43 -04001432 nfs4_label_free(p->f_label);
1433
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001434 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001435 dput(p->dentry);
1436 nfs_sb_deactive(sb);
Trond Myklebust6926afd12012-01-07 13:22:46 -05001437 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001438 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001439 kfree(p);
1440}
1441
1442static void nfs4_opendata_put(struct nfs4_opendata *p)
1443{
1444 if (p != NULL)
1445 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001446}
1447
Trond Myklebust24311f82015-09-20 10:50:17 -04001448static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1449 fmode_t fmode)
1450{
1451 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1452 case FMODE_READ|FMODE_WRITE:
1453 return state->n_rdwr != 0;
1454 case FMODE_WRITE:
1455 return state->n_wronly != 0;
1456 case FMODE_READ:
1457 return state->n_rdonly != 0;
1458 }
1459 WARN_ON_ONCE(1);
1460 return false;
1461}
1462
Trond Myklebustbe189f72018-09-27 17:12:33 -04001463static int can_open_cached(struct nfs4_state *state, fmode_t mode,
1464 int open_mode, enum open_claim_type4 claim)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001465{
1466 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001467
Trond Myklebust536e43d2012-01-17 22:04:26 -05001468 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001469 goto out;
Trond Myklebustbe189f72018-09-27 17:12:33 -04001470 switch (claim) {
1471 case NFS4_OPEN_CLAIM_NULL:
1472 case NFS4_OPEN_CLAIM_FH:
1473 goto out;
1474 default:
1475 break;
1476 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001477 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001478 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001479 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1480 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001481 break;
1482 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001483 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1484 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001485 break;
1486 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001487 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1488 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001489 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001490out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001491 return ret;
1492}
1493
Trond Myklebust2a606182015-08-19 22:30:00 -05001494static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1495 enum open_claim_type4 claim)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001496{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001497 if (delegation == NULL)
1498 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001499 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001500 return 0;
Trond Myklebust2a606182015-08-19 22:30:00 -05001501 switch (claim) {
1502 case NFS4_OPEN_CLAIM_NULL:
1503 case NFS4_OPEN_CLAIM_FH:
1504 break;
1505 case NFS4_OPEN_CLAIM_PREVIOUS:
1506 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1507 break;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001508 fallthrough;
Trond Myklebust2a606182015-08-19 22:30:00 -05001509 default:
1510 return 0;
1511 }
Trond Myklebustb7391f42008-12-23 15:21:52 -05001512 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001513 return 1;
1514}
1515
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001516static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001517{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001518 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001519 case FMODE_WRITE:
1520 state->n_wronly++;
1521 break;
1522 case FMODE_READ:
1523 state->n_rdonly++;
1524 break;
1525 case FMODE_READ|FMODE_WRITE:
1526 state->n_rdwr++;
1527 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001528 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001529}
1530
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04001531#ifdef CONFIG_NFS_V4_1
1532static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1533{
1534 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1535 return true;
1536 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1537 return true;
1538 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1539 return true;
1540 return false;
1541}
1542#endif /* CONFIG_NFS_V4_1 */
1543
Trond Myklebustc9399f22017-11-06 15:28:01 -05001544static void nfs_state_log_update_open_stateid(struct nfs4_state *state)
1545{
1546 if (test_and_clear_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1547 wake_up_all(&state->waitq);
1548}
1549
Trond Myklebust4f14c192014-02-12 19:15:06 -05001550static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001551{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001552 struct nfs_client *clp = state->owner->so_server->nfs_client;
1553 bool need_recover = false;
1554
1555 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1556 need_recover = true;
1557 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1558 need_recover = true;
1559 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1560 need_recover = true;
1561 if (need_recover)
1562 nfs4_state_mark_reclaim_nograce(clp, state);
1563}
1564
Trond Myklebustc9399f22017-11-06 15:28:01 -05001565/*
1566 * Check for whether or not the caller may update the open stateid
1567 * to the value passed in by stateid.
1568 *
1569 * Note: This function relies heavily on the server implementing
1570 * RFC7530 Section 9.1.4.2, and RFC5661 Section 8.2.2
1571 * correctly.
1572 * i.e. The stateid seqids have to be initialised to 1, and
1573 * are then incremented on every state transition.
1574 */
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04001575static bool nfs_stateid_is_sequential(struct nfs4_state *state,
Trond Myklebustc9399f22017-11-06 15:28:01 -05001576 const nfs4_stateid *stateid)
Trond Myklebuste999e802014-02-10 18:20:47 -05001577{
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04001578 if (test_bit(NFS_OPEN_STATE, &state->flags)) {
1579 /* The common case - we're updating to a new sequence number */
1580 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1581 nfs4_stateid_is_next(&state->open_stateid, stateid)) {
1582 return true;
1583 }
1584 } else {
1585 /* This is the first OPEN in this generation */
Trond Myklebustc9399f22017-11-06 15:28:01 -05001586 if (stateid->seqid == cpu_to_be32(1))
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04001587 return true;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001588 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001589 return false;
1590}
1591
Trond Myklebustf95549c2015-01-23 18:06:09 -05001592static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1593{
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001594 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1595 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001596 if (state->n_wronly)
1597 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1598 if (state->n_rdonly)
1599 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1600 if (state->n_rdwr)
1601 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001602 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustf95549c2015-01-23 18:06:09 -05001603}
1604
Trond Myklebust226056c2014-02-11 10:41:07 -05001605static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1606 nfs4_stateid *stateid, fmode_t fmode)
1607{
1608 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1609 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1610 case FMODE_WRITE:
1611 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1612 break;
1613 case FMODE_READ:
1614 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1615 break;
1616 case 0:
1617 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1618 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1619 clear_bit(NFS_OPEN_STATE, &state->flags);
1620 }
1621 if (stateid == NULL)
1622 return;
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001623 /* Handle OPEN+OPEN_DOWNGRADE races */
1624 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1625 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
Trond Myklebustf95549c2015-01-23 18:06:09 -05001626 nfs_resync_open_stateid_locked(state);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001627 goto out;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001628 }
Trond Myklebust003707c2007-07-05 18:07:55 -04001629 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001630 nfs4_stateid_copy(&state->stateid, stateid);
1631 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001632 trace_nfs4_open_stateid_update(state->inode, stateid, 0);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001633out:
1634 nfs_state_log_update_open_stateid(state);
Trond Myklebust226056c2014-02-11 10:41:07 -05001635}
1636
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001637static void nfs_clear_open_stateid(struct nfs4_state *state,
1638 nfs4_stateid *arg_stateid,
1639 nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust226056c2014-02-11 10:41:07 -05001640{
1641 write_seqlock(&state->seqlock);
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001642 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1643 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1644 nfs_clear_open_stateid_locked(state, stateid, fmode);
Trond Myklebust226056c2014-02-11 10:41:07 -05001645 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001646 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1647 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001648}
1649
Trond Myklebust1393d962016-09-22 13:39:13 -04001650static void nfs_set_open_stateid_locked(struct nfs4_state *state,
Trond Myklebustc9399f22017-11-06 15:28:01 -05001651 const nfs4_stateid *stateid, nfs4_stateid *freeme)
Trond Myklebuste9acf212019-01-22 14:01:16 -05001652 __must_hold(&state->owner->so_lock)
1653 __must_hold(&state->seqlock)
1654 __must_hold(RCU)
1655
Trond Myklebust003707c2007-07-05 18:07:55 -04001656{
Trond Myklebustc9399f22017-11-06 15:28:01 -05001657 DEFINE_WAIT(wait);
1658 int status = 0;
1659 for (;;) {
1660
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04001661 if (nfs_stateid_is_sequential(state, stateid))
Trond Myklebust003707c2007-07-05 18:07:55 -04001662 break;
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04001663
Trond Myklebustc9399f22017-11-06 15:28:01 -05001664 if (status)
Trond Myklebust003707c2007-07-05 18:07:55 -04001665 break;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001666 /* Rely on seqids for serialisation with NFSv4.0 */
1667 if (!nfs4_has_session(NFS_SERVER(state->inode)->nfs_client))
1668 break;
1669
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04001670 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001671 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
1672 /*
1673 * Ensure we process the state changes in the same order
1674 * in which the server processed them by delaying the
1675 * update of the stateid until we are in sequence.
1676 */
1677 write_sequnlock(&state->seqlock);
1678 spin_unlock(&state->owner->so_lock);
1679 rcu_read_unlock();
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001680 trace_nfs4_open_stateid_update_wait(state->inode, stateid, 0);
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04001681
Trond Myklebustc9399f22017-11-06 15:28:01 -05001682 if (!signal_pending(current)) {
1683 if (schedule_timeout(5*HZ) == 0)
1684 status = -EAGAIN;
1685 else
1686 status = 0;
1687 } else
1688 status = -EINTR;
1689 finish_wait(&state->waitq, &wait);
1690 rcu_read_lock();
1691 spin_lock(&state->owner->so_lock);
1692 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001693 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001694
Trond Myklebuste1fff5d2017-11-07 13:10:46 -05001695 if (test_bit(NFS_OPEN_STATE, &state->flags) &&
1696 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
Trond Myklebustc9399f22017-11-06 15:28:01 -05001697 nfs4_stateid_copy(freeme, &state->open_stateid);
1698 nfs_test_and_clear_all_open_stateid(state);
1699 }
1700
Trond Myklebuste999e802014-02-10 18:20:47 -05001701 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1702 nfs4_stateid_copy(&state->stateid, stateid);
1703 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001704 trace_nfs4_open_stateid_update(state->inode, stateid, status);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001705 nfs_state_log_update_open_stateid(state);
Trond Myklebust003707c2007-07-05 18:07:55 -04001706}
1707
Trond Myklebustc9399f22017-11-06 15:28:01 -05001708static void nfs_state_set_open_stateid(struct nfs4_state *state,
Trond Myklebust1393d962016-09-22 13:39:13 -04001709 const nfs4_stateid *open_stateid,
Trond Myklebust1393d962016-09-22 13:39:13 -04001710 fmode_t fmode,
1711 nfs4_stateid *freeme)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001713 /*
1714 * Protect the call to nfs4_state_set_mode_locked and
1715 * serialise the stateid update
1716 */
1717 write_seqlock(&state->seqlock);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001718 nfs_set_open_stateid_locked(state, open_stateid, freeme);
1719 switch (fmode) {
1720 case FMODE_READ:
1721 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1722 break;
1723 case FMODE_WRITE:
1724 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1725 break;
1726 case FMODE_READ|FMODE_WRITE:
1727 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust003707c2007-07-05 18:07:55 -04001728 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001729 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001730 write_sequnlock(&state->seqlock);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001731}
1732
Trond Myklebust27a30cf2019-07-22 18:32:59 +01001733static void nfs_state_clear_open_state_flags(struct nfs4_state *state)
1734{
1735 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1736 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1737 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1738 clear_bit(NFS_OPEN_STATE, &state->flags);
1739}
1740
Trond Myklebustc9399f22017-11-06 15:28:01 -05001741static void nfs_state_set_delegation(struct nfs4_state *state,
1742 const nfs4_stateid *deleg_stateid,
1743 fmode_t fmode)
1744{
1745 /*
1746 * Protect the call to nfs4_state_set_mode_locked and
1747 * serialise the stateid update
1748 */
1749 write_seqlock(&state->seqlock);
1750 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1751 set_bit(NFS_DELEGATED_STATE, &state->flags);
1752 write_sequnlock(&state->seqlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753}
1754
Trond Myklebust9f0c5122018-09-05 14:07:15 -04001755static void nfs_state_clear_delegation(struct nfs4_state *state)
1756{
1757 write_seqlock(&state->seqlock);
1758 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1759 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1760 write_sequnlock(&state->seqlock);
1761}
1762
Olga Kornievskaiaec4b0922019-10-08 16:33:53 -04001763int update_open_stateid(struct nfs4_state *state,
Trond Myklebust1393d962016-09-22 13:39:13 -04001764 const nfs4_stateid *open_stateid,
1765 const nfs4_stateid *delegation,
1766 fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001767{
Trond Myklebust1393d962016-09-22 13:39:13 -04001768 struct nfs_server *server = NFS_SERVER(state->inode);
1769 struct nfs_client *clp = server->nfs_client;
Trond Myklebust34310432008-12-23 15:21:38 -05001770 struct nfs_inode *nfsi = NFS_I(state->inode);
1771 struct nfs_delegation *deleg_cur;
Arnd Bergmann83aa3e02016-10-18 17:21:30 +02001772 nfs4_stateid freeme = { };
Trond Myklebust34310432008-12-23 15:21:38 -05001773 int ret = 0;
1774
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001775 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001776
1777 rcu_read_lock();
Trond Myklebustc9399f22017-11-06 15:28:01 -05001778 spin_lock(&state->owner->so_lock);
1779 if (open_stateid != NULL) {
1780 nfs_state_set_open_stateid(state, open_stateid, fmode, &freeme);
1781 ret = 1;
1782 }
1783
Trond Myklebust333ac782019-10-22 12:12:17 -04001784 deleg_cur = nfs4_get_valid_delegation(state->inode);
Trond Myklebust34310432008-12-23 15:21:38 -05001785 if (deleg_cur == NULL)
1786 goto no_delegation;
1787
1788 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001789 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001790 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001791 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001792 goto no_delegation_unlock;
1793
1794 if (delegation == NULL)
1795 delegation = &deleg_cur->stateid;
Trond Myklebust333ac782019-10-22 12:12:17 -04001796 else if (!nfs4_stateid_match_other(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001797 goto no_delegation_unlock;
1798
Trond Myklebustb7391f42008-12-23 15:21:52 -05001799 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001800 nfs_state_set_delegation(state, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001801 ret = 1;
1802no_delegation_unlock:
1803 spin_unlock(&deleg_cur->lock);
1804no_delegation:
Trond Myklebustc9399f22017-11-06 15:28:01 -05001805 if (ret)
1806 update_open_stateflags(state, fmode);
1807 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001808 rcu_read_unlock();
1809
Trond Myklebust4f14c192014-02-12 19:15:06 -05001810 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
Trond Myklebust1393d962016-09-22 13:39:13 -04001811 nfs4_schedule_state_manager(clp);
1812 if (freeme.type != 0)
1813 nfs4_test_and_free_stateid(server, &freeme,
1814 state->owner->so_cred);
Trond Myklebust34310432008-12-23 15:21:38 -05001815
1816 return ret;
1817}
1818
Trond Myklebust39071e62015-01-24 15:07:56 -05001819static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1820 const nfs4_stateid *stateid)
1821{
1822 struct nfs4_state *state = lsp->ls_state;
1823 bool ret = false;
1824
1825 spin_lock(&state->state_lock);
1826 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1827 goto out_noupdate;
1828 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1829 goto out_noupdate;
1830 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1831 ret = true;
1832out_noupdate:
1833 spin_unlock(&state->state_lock);
1834 return ret;
1835}
Trond Myklebust34310432008-12-23 15:21:38 -05001836
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001837static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001838{
1839 struct nfs_delegation *delegation;
1840
Trond Myklebustf5086242018-03-20 16:43:13 -04001841 fmode &= FMODE_READ|FMODE_WRITE;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001842 rcu_read_lock();
Trond Myklebust40e6aa12019-10-27 13:38:45 -04001843 delegation = nfs4_get_valid_delegation(inode);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001844 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001845 rcu_read_unlock();
1846 return;
1847 }
1848 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001849 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001850}
1851
Trond Myklebust6ee41262007-07-08 14:11:36 -04001852static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001853{
1854 struct nfs4_state *state = opendata->state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001855 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001856 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001857 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebust2a606182015-08-19 22:30:00 -05001858 enum open_claim_type4 claim = opendata->o_arg.claim;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001859 nfs4_stateid stateid;
1860 int ret = -EAGAIN;
1861
Trond Myklebustaac00a82007-07-05 19:02:21 -04001862 for (;;) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001863 spin_lock(&state->owner->so_lock);
Trond Myklebustbe189f72018-09-27 17:12:33 -04001864 if (can_open_cached(state, fmode, open_mode, claim)) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001865 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001866 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef72014-09-03 14:15:40 -04001867 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001868 }
Anna Schumaker61beef72014-09-03 14:15:40 -04001869 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001870 rcu_read_lock();
Trond Myklebustbe3df3d2019-10-31 18:40:32 -04001871 delegation = nfs4_get_valid_delegation(state->inode);
Trond Myklebust2a606182015-08-19 22:30:00 -05001872 if (!can_open_delegated(delegation, fmode, claim)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001873 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001874 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001875 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001876 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001877 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001878 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001879 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001880 if (!opendata->is_recover) {
1881 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1882 if (ret != 0)
1883 goto out;
1884 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001885 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001886
1887 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001888 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001889 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001890 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001891out:
1892 return ERR_PTR(ret);
1893out_return_state:
Trond Myklebustace9fad2018-09-02 19:19:07 -04001894 refcount_inc(&state->count);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001895 return state;
1896}
1897
Andy Adamsone23008e2012-10-02 21:07:32 -04001898static void
1899nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1900{
1901 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1902 struct nfs_delegation *delegation;
1903 int delegation_flags = 0;
1904
1905 rcu_read_lock();
1906 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1907 if (delegation)
1908 delegation_flags = delegation->flags;
1909 rcu_read_unlock();
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001910 switch (data->o_arg.claim) {
1911 default:
1912 break;
1913 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1914 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001915 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1916 "returning a delegation for "
1917 "OPEN(CLAIM_DELEGATE_CUR)\n",
1918 clp->cl_hostname);
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001919 return;
1920 }
1921 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Andy Adamsone23008e2012-10-02 21:07:32 -04001922 nfs_inode_set_delegation(state->inode,
Trond Myklebust35156bf2018-03-20 17:03:13 -04001923 data->owner->so_cred,
1924 data->o_res.delegation_type,
1925 &data->o_res.delegation,
1926 data->o_res.pagemod_limit);
Andy Adamsone23008e2012-10-02 21:07:32 -04001927 else
1928 nfs_inode_reclaim_delegation(state->inode,
Trond Myklebust35156bf2018-03-20 17:03:13 -04001929 data->owner->so_cred,
1930 data->o_res.delegation_type,
1931 &data->o_res.delegation,
1932 data->o_res.pagemod_limit);
Jeff Layton8b199e52018-07-05 05:48:14 -04001933
1934 if (data->o_res.do_recall)
1935 nfs_async_inode_return_delegation(state->inode,
1936 &data->o_res.delegation);
Andy Adamsone23008e2012-10-02 21:07:32 -04001937}
1938
1939/*
1940 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1941 * and update the nfs4_state.
1942 */
1943static struct nfs4_state *
1944_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1945{
1946 struct inode *inode = data->state->inode;
1947 struct nfs4_state *state = data->state;
1948 int ret;
1949
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001950 if (!data->rpc_done) {
Anna Schumaker37a84842017-01-11 16:08:35 -05001951 if (data->rpc_status)
1952 return ERR_PTR(data->rpc_status);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001953 /* cached opens have already been processed */
1954 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001955 }
1956
Andy Adamsone23008e2012-10-02 21:07:32 -04001957 ret = nfs_refresh_inode(inode, &data->f_attr);
1958 if (ret)
Anna Schumaker37a84842017-01-11 16:08:35 -05001959 return ERR_PTR(ret);
Andy Adamsone23008e2012-10-02 21:07:32 -04001960
1961 if (data->o_res.delegation_type != 0)
1962 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001963update:
Trond Myklebuste3c8dc72019-07-29 18:25:00 +01001964 if (!update_open_stateid(state, &data->o_res.stateid,
1965 NULL, data->o_arg.fmode))
1966 return ERR_PTR(-EAGAIN);
Trond Myklebustace9fad2018-09-02 19:19:07 -04001967 refcount_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001968
1969 return state;
Andy Adamsone23008e2012-10-02 21:07:32 -04001970}
1971
Trond Myklebust4e2fcac2017-08-08 09:06:18 -04001972static struct inode *
1973nfs4_opendata_get_inode(struct nfs4_opendata *data)
1974{
1975 struct inode *inode;
1976
1977 switch (data->o_arg.claim) {
1978 case NFS4_OPEN_CLAIM_NULL:
1979 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1980 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1981 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1982 return ERR_PTR(-EAGAIN);
1983 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh,
1984 &data->f_attr, data->f_label);
1985 break;
1986 default:
1987 inode = d_inode(data->dentry);
1988 ihold(inode);
1989 nfs_refresh_inode(inode, &data->f_attr);
1990 }
1991 return inode;
1992}
1993
Andy Adamsone23008e2012-10-02 21:07:32 -04001994static struct nfs4_state *
Trond Myklebust75e8c482017-08-08 10:38:07 -04001995nfs4_opendata_find_nfs4_state(struct nfs4_opendata *data)
1996{
1997 struct nfs4_state *state;
1998 struct inode *inode;
1999
2000 inode = nfs4_opendata_get_inode(data);
2001 if (IS_ERR(inode))
2002 return ERR_CAST(inode);
2003 if (data->state != NULL && data->state->inode == inode) {
2004 state = data->state;
Trond Myklebustace9fad2018-09-02 19:19:07 -04002005 refcount_inc(&state->count);
Trond Myklebust75e8c482017-08-08 10:38:07 -04002006 } else
2007 state = nfs4_get_open_state(inode, data->owner);
2008 iput(inode);
2009 if (state == NULL)
2010 state = ERR_PTR(-ENOMEM);
2011 return state;
2012}
2013
Andy Adamsone23008e2012-10-02 21:07:32 -04002014static struct nfs4_state *
2015_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002016{
Trond Myklebust75e8c482017-08-08 10:38:07 -04002017 struct nfs4_state *state;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002018
Trond Myklebustaac00a82007-07-05 19:02:21 -04002019 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04002020 state = nfs4_try_open_cached(data);
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002021 trace_nfs4_cached_open(data->state);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002022 goto out;
2023 }
2024
Trond Myklebust75e8c482017-08-08 10:38:07 -04002025 state = nfs4_opendata_find_nfs4_state(data);
2026 if (IS_ERR(state))
2027 goto out;
2028
Andy Adamsone23008e2012-10-02 21:07:32 -04002029 if (data->o_res.delegation_type != 0)
2030 nfs4_opendata_check_deleg(data, state);
Trond Myklebuste3c8dc72019-07-29 18:25:00 +01002031 if (!update_open_stateid(state, &data->o_res.stateid,
2032 NULL, data->o_arg.fmode)) {
2033 nfs4_put_open_state(state);
2034 state = ERR_PTR(-EAGAIN);
2035 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002036out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08002037 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002038 return state;
2039}
2040
Andy Adamsone23008e2012-10-02 21:07:32 -04002041static struct nfs4_state *
2042nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
2043{
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002044 struct nfs4_state *ret;
2045
Andy Adamsone23008e2012-10-02 21:07:32 -04002046 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002047 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
2048 else
2049 ret = _nfs4_opendata_to_nfs4_state(data);
2050 nfs4_sequence_free_slot(&data->o_res.seq_res);
2051 return ret;
Andy Adamsone23008e2012-10-02 21:07:32 -04002052}
2053
Trond Myklebust0de43972018-09-02 15:57:01 -04002054static struct nfs_open_context *
2055nfs4_state_find_open_context_mode(struct nfs4_state *state, fmode_t mode)
Trond Myklebust864472e2006-01-03 09:55:15 +01002056{
2057 struct nfs_inode *nfsi = NFS_I(state->inode);
2058 struct nfs_open_context *ctx;
2059
Trond Myklebust0de43972018-09-02 15:57:01 -04002060 rcu_read_lock();
2061 list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
Trond Myklebust864472e2006-01-03 09:55:15 +01002062 if (ctx->state != state)
2063 continue;
Trond Myklebust0de43972018-09-02 15:57:01 -04002064 if ((ctx->mode & mode) != mode)
2065 continue;
2066 if (!get_nfs_open_context(ctx))
2067 continue;
2068 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01002069 return ctx;
2070 }
Trond Myklebust0de43972018-09-02 15:57:01 -04002071 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01002072 return ERR_PTR(-ENOENT);
2073}
2074
Trond Myklebust0de43972018-09-02 15:57:01 -04002075static struct nfs_open_context *
2076nfs4_state_find_open_context(struct nfs4_state *state)
2077{
2078 struct nfs_open_context *ctx;
2079
2080 ctx = nfs4_state_find_open_context_mode(state, FMODE_READ|FMODE_WRITE);
2081 if (!IS_ERR(ctx))
2082 return ctx;
2083 ctx = nfs4_state_find_open_context_mode(state, FMODE_WRITE);
2084 if (!IS_ERR(ctx))
2085 return ctx;
2086 return nfs4_state_find_open_context_mode(state, FMODE_READ);
2087}
2088
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002089static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
2090 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002091{
2092 struct nfs4_opendata *opendata;
2093
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002094 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002095 NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002096 if (opendata == NULL)
2097 return ERR_PTR(-ENOMEM);
2098 opendata->state = state;
Trond Myklebustace9fad2018-09-02 19:19:07 -04002099 refcount_inc(&state->count);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002100 return opendata;
2101}
2102
Trond Myklebust24311f82015-09-20 10:50:17 -04002103static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
2104 fmode_t fmode)
Trond Myklebust864472e2006-01-03 09:55:15 +01002105{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002106 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01002107 int ret;
2108
Trond Myklebust24311f82015-09-20 10:50:17 -04002109 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
NeilBrown39f897f2015-06-29 14:28:54 +10002110 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002111 opendata->o_arg.open_flags = 0;
2112 opendata->o_arg.fmode = fmode;
Trond Myklebustbe36e182015-02-27 17:04:17 -05002113 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
2114 NFS_SB(opendata->dentry->d_sb),
2115 fmode, 0);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002116 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
2117 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
2118 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002119 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002120 if (ret != 0)
2121 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002122 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002123 if (IS_ERR(newstate))
2124 return PTR_ERR(newstate);
Trond Myklebust24311f82015-09-20 10:50:17 -04002125 if (newstate != opendata->state)
2126 ret = -ESTALE;
Al Viro643168c2011-06-22 18:20:23 -04002127 nfs4_close_state(newstate, fmode);
Trond Myklebust24311f82015-09-20 10:50:17 -04002128 return ret;
Trond Myklebust864472e2006-01-03 09:55:15 +01002129}
2130
2131static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
2132{
Trond Myklebust864472e2006-01-03 09:55:15 +01002133 int ret;
2134
2135 /* memory barrier prior to reading state->n_* */
2136 smp_rmb();
Trond Myklebust24311f82015-09-20 10:50:17 -04002137 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2138 if (ret != 0)
2139 return ret;
2140 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2141 if (ret != 0)
2142 return ret;
2143 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
2144 if (ret != 0)
2145 return ret;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002146 /*
2147 * We may have performed cached opens for all three recoveries.
2148 * Check if we need to update the current stateid.
2149 */
2150 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05002151 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04002152 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002153 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05002154 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04002155 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002156 }
Trond Myklebust864472e2006-01-03 09:55:15 +01002157 return 0;
2158}
2159
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160/*
2161 * OPEN_RECLAIM:
2162 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002164static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002166 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01002167 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002168 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 int status;
2170
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002171 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2172 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002173 if (IS_ERR(opendata))
2174 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002175 rcu_read_lock();
2176 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05002177 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04002178 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002179 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01002180 opendata->o_arg.u.delegation_type = delegation_type;
2181 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002182 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 return status;
2184}
2185
Trond Myklebust539cd032007-06-05 11:46:42 -04002186static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187{
2188 struct nfs_server *server = NFS_SERVER(state->inode);
2189 struct nfs4_exception exception = { };
2190 int err;
2191 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002192 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002193 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002194 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2195 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04002196 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002197 break;
2198 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 } while (exception.retry);
2200 return err;
2201}
2202
Trond Myklebust864472e2006-01-03 09:55:15 +01002203static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
2204{
2205 struct nfs_open_context *ctx;
2206 int ret;
2207
2208 ctx = nfs4_state_find_open_context(state);
2209 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002210 return -EAGAIN;
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002211 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2212 nfs_state_clear_open_state_flags(state);
Trond Myklebust539cd032007-06-05 11:46:42 -04002213 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002214 put_nfs_open_context(ctx);
2215 return ret;
2216}
2217
NeilBrowndce26302017-12-13 09:57:09 +11002218static 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 -07002219{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002220 switch (err) {
2221 default:
2222 printk(KERN_ERR "NFS: %s: unhandled error "
2223 "%d.\n", __func__, err);
Gustavo A. R. Silvaffb81712020-11-20 12:26:46 -06002224 fallthrough;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002225 case 0:
2226 case -ENOENT:
Trond Myklebust8eee52a2015-06-04 13:51:13 -04002227 case -EAGAIN:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002228 case -ESTALE:
Trond Myklebust67e7b522019-08-07 07:31:27 -04002229 case -ETIMEDOUT:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002230 break;
2231 case -NFS4ERR_BADSESSION:
2232 case -NFS4ERR_BADSLOT:
2233 case -NFS4ERR_BAD_HIGH_SLOT:
2234 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
2235 case -NFS4ERR_DEADSESSION:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002236 return -EAGAIN;
2237 case -NFS4ERR_STALE_CLIENTID:
2238 case -NFS4ERR_STALE_STATEID:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002239 /* Don't recall a delegation if it was lost */
2240 nfs4_schedule_lease_recovery(server->nfs_client);
2241 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04002242 case -NFS4ERR_MOVED:
2243 nfs4_schedule_migration_recovery(server);
2244 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04002245 case -NFS4ERR_LEASE_MOVED:
2246 nfs4_schedule_lease_moved_recovery(server->nfs_client);
2247 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002248 case -NFS4ERR_DELEG_REVOKED:
2249 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust404ea3562016-09-22 13:39:08 -04002250 case -NFS4ERR_EXPIRED:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002251 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002252 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002253 nfs_inode_find_state_and_recover(state->inode,
2254 stateid);
2255 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05002256 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002257 case -NFS4ERR_DELAY:
2258 case -NFS4ERR_GRACE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002259 ssleep(1);
2260 return -EAGAIN;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002261 case -ENOMEM:
2262 case -NFS4ERR_DENIED:
NeilBrowndce26302017-12-13 09:57:09 +11002263 if (fl) {
2264 struct nfs4_lock_state *lsp = fl->fl_u.nfs4_fl.owner;
2265 if (lsp)
2266 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2267 }
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002268 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002269 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 return err;
2271}
2272
Trond Myklebust24311f82015-09-20 10:50:17 -04002273int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002274 struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002275{
2276 struct nfs_server *server = NFS_SERVER(state->inode);
2277 struct nfs4_opendata *opendata;
Trond Myklebust24311f82015-09-20 10:50:17 -04002278 int err = 0;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002279
2280 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2281 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2282 if (IS_ERR(opendata))
2283 return PTR_ERR(opendata);
2284 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002285 if (!test_bit(NFS_O_RDWR_STATE, &state->flags)) {
Trond Myklebust24311f82015-09-20 10:50:17 -04002286 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2287 if (err)
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002288 goto out;
2289 }
2290 if (!test_bit(NFS_O_WRONLY_STATE, &state->flags)) {
Trond Myklebust24311f82015-09-20 10:50:17 -04002291 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2292 if (err)
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002293 goto out;
Trond Myklebust24311f82015-09-20 10:50:17 -04002294 }
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002295 if (!test_bit(NFS_O_RDONLY_STATE, &state->flags)) {
2296 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2297 if (err)
2298 goto out;
2299 }
2300 nfs_state_clear_delegation(state);
2301out:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002302 nfs4_opendata_put(opendata);
NeilBrowndce26302017-12-13 09:57:09 +11002303 return nfs4_handle_delegation_recall_error(server, state, stateid, NULL, err);
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002304}
2305
Chuck Leverbe05c862013-08-09 12:49:47 -04002306static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2307{
2308 struct nfs4_opendata *data = calldata;
2309
Anna Schumaker7981c8a2017-01-10 11:39:53 -05002310 nfs4_setup_sequence(data->o_arg.server->nfs_client,
2311 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04002312}
2313
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002314static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2315{
2316 struct nfs4_opendata *data = calldata;
2317
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002318 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04002319
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002320 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002321 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05002322 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05002323 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002324 renew_lease(data->o_res.server, data->timestamp);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002325 data->rpc_done = true;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002326 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002327}
2328
2329static void nfs4_open_confirm_release(void *calldata)
2330{
2331 struct nfs4_opendata *data = calldata;
2332 struct nfs4_state *state = NULL;
2333
2334 /* If this request hasn't been cancelled, do nothing */
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002335 if (!data->cancelled)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002336 goto out_free;
2337 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002338 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002339 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002340 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002341 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002342 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002343out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002344 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002345}
2346
2347static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04002348 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002349 .rpc_call_done = nfs4_open_confirm_done,
2350 .rpc_release = nfs4_open_confirm_release,
2351};
2352
2353/*
2354 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2355 */
2356static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2357{
David Howells2b0143b2015-03-17 22:25:59 +00002358 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002359 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002360 struct rpc_message msg = {
2361 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2362 .rpc_argp = &data->c_arg,
2363 .rpc_resp = &data->c_res,
2364 .rpc_cred = data->owner->so_cred,
2365 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002366 struct rpc_task_setup task_setup_data = {
2367 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002368 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002369 .callback_ops = &nfs4_open_confirm_ops,
2370 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002371 .workqueue = nfsiod_workqueue,
Trond Myklebust61296502020-02-07 19:38:12 -05002372 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002373 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 int status;
2375
Anna Schumakerfba83f32018-05-04 16:22:50 -04002376 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1,
2377 data->is_recover);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002378 kref_get(&data->kref);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002379 data->rpc_done = false;
Trond Myklebust3e309912007-07-07 13:19:59 -04002380 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002381 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002382 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05002383 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002384 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002385 status = rpc_wait_for_completion_task(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002386 if (status != 0) {
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002387 data->cancelled = true;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002388 smp_wmb();
2389 } else
2390 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05002391 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 return status;
2393}
2394
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002395static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002397 struct nfs4_opendata *data = calldata;
2398 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04002399 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust2a606182015-08-19 22:30:00 -05002400 enum open_claim_type4 claim = data->o_arg.claim;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002401
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002402 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002403 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002404 /*
2405 * Check if we still need to send an OPEN call, or if we can use
2406 * a delegation instead.
2407 */
2408 if (data->state != NULL) {
2409 struct nfs_delegation *delegation;
2410
Trond Myklebustbe189f72018-09-27 17:12:33 -04002411 if (can_open_cached(data->state, data->o_arg.fmode,
2412 data->o_arg.open_flags, claim))
Trond Myklebust6ee41262007-07-08 14:11:36 -04002413 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002414 rcu_read_lock();
Trond Myklebustbe3df3d2019-10-31 18:40:32 -04002415 delegation = nfs4_get_valid_delegation(data->state->inode);
Trond Myklebust2a606182015-08-19 22:30:00 -05002416 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
Trond Myklebust652f89f2011-12-09 19:05:58 -05002417 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002418 rcu_read_unlock();
2419 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04002420 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04002421 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust2a606182015-08-19 22:30:00 -05002422 switch (claim) {
2423 default:
2424 break;
Trond Myklebust8188df12013-04-23 14:31:19 -04002425 case NFS4_OPEN_CLAIM_PREVIOUS:
2426 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2427 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04002428 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002429 fallthrough;
Trond Myklebust8188df12013-04-23 14:31:19 -04002430 case NFS4_OPEN_CLAIM_FH:
2431 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002432 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002433 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05002434 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
Andy Adamsond8985282009-04-01 09:22:21 -04002435 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002436 &data->o_res.seq_res,
2437 task) != 0)
2438 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04002439
2440 /* Set the create mode (note dependency on the session type) */
2441 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2442 if (data->o_arg.open_flags & O_EXCL) {
2443 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2444 if (nfs4_has_persistent_session(clp))
2445 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2446 else if (clp->cl_mvops->minor_version > 0)
2447 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2448 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04002449 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05002450unlock_no_action:
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002451 trace_nfs4_cached_open(data->state);
Trond Myklebust652f89f2011-12-09 19:05:58 -05002452 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04002453out_no_action:
2454 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002455out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002456 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002457}
2458
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002459static void nfs4_open_done(struct rpc_task *task, void *calldata)
2460{
2461 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002463 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04002464
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002465 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04002466 return;
Andy Adamsond8985282009-04-01 09:22:21 -04002467
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002468 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07002469 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2470 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07002471 case S_IFREG:
2472 break;
2473 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002474 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002475 break;
2476 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002477 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002478 break;
2479 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002480 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07002481 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07002482 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002483 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04002484 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2485 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07002486 }
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002487 data->rpc_done = true;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002488}
Trond Myklebust6f926b52005-10-18 14:20:18 -07002489
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002490static void nfs4_open_release(void *calldata)
2491{
2492 struct nfs4_opendata *data = calldata;
2493 struct nfs4_state *state = NULL;
2494
2495 /* If this request hasn't been cancelled, do nothing */
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002496 if (!data->cancelled)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002497 goto out_free;
2498 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002499 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002500 goto out_free;
2501 /* In case we need an open_confirm, no cleanup! */
2502 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2503 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002504 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002505 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002506 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002507out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002508 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002509}
2510
2511static const struct rpc_call_ops nfs4_open_ops = {
2512 .rpc_call_prepare = nfs4_open_prepare,
2513 .rpc_call_done = nfs4_open_done,
2514 .rpc_release = nfs4_open_release,
2515};
2516
Fred Isaman3b65a302016-09-19 10:06:49 -04002517static int nfs4_run_open_task(struct nfs4_opendata *data,
2518 struct nfs_open_context *ctx)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002519{
David Howells2b0143b2015-03-17 22:25:59 +00002520 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002521 struct nfs_server *server = NFS_SERVER(dir);
2522 struct nfs_openargs *o_arg = &data->o_arg;
2523 struct nfs_openres *o_res = &data->o_res;
2524 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002525 struct rpc_message msg = {
2526 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2527 .rpc_argp = o_arg,
2528 .rpc_resp = o_res,
2529 .rpc_cred = data->owner->so_cred,
2530 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002531 struct rpc_task_setup task_setup_data = {
2532 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002533 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002534 .callback_ops = &nfs4_open_ops,
2535 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002536 .workqueue = nfsiod_workqueue,
Trond Myklebust61296502020-02-07 19:38:12 -05002537 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002538 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002539 int status;
2540
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002541 kref_get(&data->kref);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002542 data->rpc_done = false;
Trond Myklebust3e309912007-07-07 13:19:59 -04002543 data->rpc_status = 0;
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002544 data->cancelled = false;
2545 data->is_recover = false;
Fred Isaman3b65a302016-09-19 10:06:49 -04002546 if (!ctx) {
2547 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 1);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002548 data->is_recover = true;
Trond Myklebust67e7b522019-08-07 07:31:27 -04002549 task_setup_data.flags |= RPC_TASK_TIMEOUT;
Fred Isaman2409a972016-10-06 12:11:21 -04002550 } else {
Fred Isaman3b65a302016-09-19 10:06:49 -04002551 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 0);
Fred Isaman2409a972016-10-06 12:11:21 -04002552 pnfs_lgopen_prepare(data, ctx);
2553 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04002554 task = rpc_run_task(&task_setup_data);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002555 if (IS_ERR(task))
2556 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002557 status = rpc_wait_for_completion_task(task);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002558 if (status != 0) {
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002559 data->cancelled = true;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002560 smp_wmb();
2561 } else
2562 status = data->rpc_status;
2563 rpc_put_task(task);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002564
2565 return status;
2566}
2567
2568static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2569{
David Howells2b0143b2015-03-17 22:25:59 +00002570 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002571 struct nfs_openres *o_res = &data->o_res;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002572 int status;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002573
Fred Isaman3b65a302016-09-19 10:06:49 -04002574 status = nfs4_run_open_task(data, NULL);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002575 if (status != 0 || !data->rpc_done)
2576 return status;
2577
Trond Myklebust6926afd12012-01-07 13:22:46 -05002578 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2579
Anna Schumakerd7e98252017-01-11 16:13:29 -05002580 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002581 status = _nfs4_proc_open_confirm(data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002582
2583 return status;
2584}
2585
Trond Myklebustf3792d62014-07-10 08:54:32 -04002586/*
2587 * Additional permission checks in order to distinguish between an
2588 * open for read, and an open for execute. This works around the
2589 * fact that NFSv4 OPEN treats read and execute permissions as being
2590 * the same.
2591 * Note that in the non-execute case, we want to turn off permission
2592 * checking if we just created a new file (POSIX open() semantics).
2593 */
NeilBrowna52458b2018-12-03 11:30:31 +11002594static int nfs4_opendata_access(const struct cred *cred,
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002595 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002596 struct nfs4_state *state, fmode_t fmode,
2597 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002598{
2599 struct nfs_access_entry cache;
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002600 u32 mask, flags;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002601
2602 /* access call failed or for some reason the server doesn't
2603 * support any access modes -- defer access call until later */
2604 if (opendata->o_res.access_supported == 0)
2605 return 0;
2606
2607 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002608 /*
2609 * Use openflags to check for exec, because fmode won't
2610 * always have FMODE_EXEC set when file open for exec.
2611 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002612 if (openflags & __FMODE_EXEC) {
2613 /* ONLY check for exec rights */
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002614 if (S_ISDIR(state->inode->i_mode))
2615 mask = NFS4_ACCESS_LOOKUP;
2616 else
2617 mask = NFS4_ACCESS_EXECUTE;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002618 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002619 mask = NFS4_ACCESS_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002620
2621 cache.cred = cred;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002622 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2623 nfs_access_add_cache(state->inode, &cache);
2624
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002625 flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP;
2626 if ((mask & ~cache.mask & flags) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002627 return 0;
2628
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002629 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002630}
2631
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002632/*
2633 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2634 */
Fred Isaman3b65a302016-09-19 10:06:49 -04002635static int _nfs4_proc_open(struct nfs4_opendata *data,
2636 struct nfs_open_context *ctx)
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002637{
David Howells2b0143b2015-03-17 22:25:59 +00002638 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002639 struct nfs_server *server = NFS_SERVER(dir);
2640 struct nfs_openargs *o_arg = &data->o_arg;
2641 struct nfs_openres *o_res = &data->o_res;
2642 int status;
2643
Fred Isaman3b65a302016-09-19 10:06:49 -04002644 status = nfs4_run_open_task(data, ctx);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002645 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002646 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002647 if (status != 0) {
2648 if (status == -NFS4ERR_BADNAME &&
2649 !(o_arg->open_flags & O_CREAT))
2650 return -ENOENT;
2651 return status;
2652 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002653
Trond Myklebust6926afd12012-01-07 13:22:46 -05002654 nfs_fattr_map_and_free_names(server, &data->f_attr);
2655
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002656 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002657 if (o_arg->open_flags & O_EXCL)
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002658 data->file_created = true;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002659 else if (o_res->cinfo.before != o_res->cinfo.after)
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002660 data->file_created = true;
Jeff Layton1eb5d982018-01-09 08:21:17 -05002661 if (data->file_created ||
2662 inode_peek_iversion_raw(dir) != o_res->cinfo.after)
Frank van der Linden1b523ca2020-06-23 22:38:59 +00002663 nfs4_update_changeattr(dir, &o_res->cinfo,
2664 o_res->f_attr->time_start,
2665 NFS_INO_INVALID_DATA);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002666 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002667 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2668 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002670 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002672 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 }
Trond Myklebust56e0d712017-04-15 19:20:01 -04002674 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2675 nfs4_sequence_free_slot(&o_res->seq_res);
Trond Myklebusta841b542018-04-07 13:50:59 -04002676 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr,
2677 o_res->f_label, NULL);
Trond Myklebust56e0d712017-04-15 19:20:01 -04002678 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002679 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680}
2681
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682/*
2683 * OPEN_EXPIRED:
2684 * reclaim state on the server after a network partition.
2685 * Assumes caller holds the appropriate lock
2686 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002687static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002689 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002690 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002692 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002693 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002694 if (IS_ERR(opendata))
2695 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002696 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002697 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002698 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002699 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002700 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701}
2702
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002703static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002704{
Trond Myklebust539cd032007-06-05 11:46:42 -04002705 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002706 struct nfs4_exception exception = { };
2707 int err;
2708
2709 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002710 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002711 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002712 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2713 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002714 switch (err) {
2715 default:
2716 goto out;
2717 case -NFS4ERR_GRACE:
2718 case -NFS4ERR_DELAY:
2719 nfs4_handle_exception(server, err, &exception);
2720 err = 0;
2721 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002722 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002723out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002724 return err;
2725}
2726
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2728{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002730 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731
Trond Myklebust864472e2006-01-03 09:55:15 +01002732 ctx = nfs4_state_find_open_context(state);
2733 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002734 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002735 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002736 put_nfs_open_context(ctx);
2737 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738}
2739
Trond Myklebust41020b62016-09-22 13:38:58 -04002740static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2741 const nfs4_stateid *stateid)
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002742{
Trond Myklebust41020b62016-09-22 13:38:58 -04002743 nfs_remove_bad_delegation(state->inode, stateid);
Trond Myklebust9f0c5122018-09-05 14:07:15 -04002744 nfs_state_clear_delegation(state);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002745}
2746
2747static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2748{
2749 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
Trond Myklebust41020b62016-09-22 13:38:58 -04002750 nfs_finish_clear_delegation_stateid(state, NULL);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002751}
2752
2753static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2754{
2755 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2756 nfs40_clear_delegation_stateid(state);
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002757 nfs_state_clear_open_state_flags(state);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002758 return nfs4_open_expired(sp, state);
2759}
2760
Trond Myklebust45870d62016-09-22 13:38:59 -04002761static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2762 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11002763 const struct cred *cred)
Trond Myklebust45870d62016-09-22 13:38:59 -04002764{
2765 return -NFS4ERR_BAD_STATEID;
2766}
2767
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002768#if defined(CONFIG_NFS_V4_1)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002769static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2770 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11002771 const struct cred *cred)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002772{
2773 int status;
2774
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002775 switch (stateid->type) {
2776 default:
2777 break;
2778 case NFS4_INVALID_STATEID_TYPE:
2779 case NFS4_SPECIAL_STATEID_TYPE:
2780 return -NFS4ERR_BAD_STATEID;
2781 case NFS4_REVOKED_STATEID_TYPE:
2782 goto out_free;
2783 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002784
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002785 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002786 switch (status) {
2787 case -NFS4ERR_EXPIRED:
2788 case -NFS4ERR_ADMIN_REVOKED:
2789 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002790 break;
2791 default:
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002792 return status;
2793 }
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002794out_free:
2795 /* Ack the revoked state to the server */
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04002796 nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002797 return -NFS4ERR_EXPIRED;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002798}
2799
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002800static int nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002801{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002802 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002803 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002804 struct nfs_delegation *delegation;
NeilBrowna52458b2018-12-03 11:30:31 +11002805 const struct cred *cred = NULL;
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002806 int status, ret = NFS_OK;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002807
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002808 /* Get the delegation credential for use by test/free_stateid */
2809 rcu_read_lock();
2810 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002811 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002812 rcu_read_unlock();
Trond Myklebust9f0c5122018-09-05 14:07:15 -04002813 nfs_state_clear_delegation(state);
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002814 return NFS_OK;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002815 }
2816
Trond Myklebustfc51b1c2020-04-02 15:27:09 -04002817 spin_lock(&delegation->lock);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002818 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002819
Trond Myklebust994b15b2018-09-05 14:07:14 -04002820 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2821 &delegation->flags)) {
Trond Myklebustfc51b1c2020-04-02 15:27:09 -04002822 spin_unlock(&delegation->lock);
Trond Myklebust994b15b2018-09-05 14:07:14 -04002823 rcu_read_unlock();
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002824 return NFS_OK;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002825 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002826
NeilBrowna52458b2018-12-03 11:30:31 +11002827 if (delegation->cred)
2828 cred = get_cred(delegation->cred);
Trond Myklebustfc51b1c2020-04-02 15:27:09 -04002829 spin_unlock(&delegation->lock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002830 rcu_read_unlock();
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002831 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002832 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002833 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
Trond Myklebust41020b62016-09-22 13:38:58 -04002834 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002835 else
2836 ret = status;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002837
Trond Myklebust8c39a392019-07-19 13:48:44 -04002838 put_cred(cred);
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002839 return ret;
2840}
2841
2842static void nfs41_delegation_recover_stateid(struct nfs4_state *state)
2843{
2844 nfs4_stateid tmp;
2845
2846 if (test_bit(NFS_DELEGATED_STATE, &state->flags) &&
2847 nfs4_copy_delegation_stateid(state->inode, state->state,
2848 &tmp, NULL) &&
2849 nfs4_stateid_match_other(&state->stateid, &tmp))
2850 nfs_state_set_delegation(state, &tmp, state->state);
2851 else
2852 nfs_state_clear_delegation(state);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002853}
2854
2855/**
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002856 * nfs41_check_expired_locks - possibly free a lock stateid
2857 *
2858 * @state: NFSv4 state for an inode
2859 *
2860 * Returns NFS_OK if recovery for this stateid is now finished.
2861 * Otherwise a negative NFS4ERR value is returned.
2862 */
2863static int nfs41_check_expired_locks(struct nfs4_state *state)
2864{
2865 int status, ret = NFS_OK;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002866 struct nfs4_lock_state *lsp, *prev = NULL;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002867 struct nfs_server *server = NFS_SERVER(state->inode);
2868
2869 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2870 goto out;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002871
2872 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002873 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2874 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
NeilBrowna52458b2018-12-03 11:30:31 +11002875 const struct cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002876
Elena Reshetova194bc1f2017-10-20 12:53:36 +03002877 refcount_inc(&lsp->ls_count);
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002878 spin_unlock(&state->state_lock);
2879
2880 nfs4_put_lock_state(prev);
2881 prev = lsp;
2882
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002883 status = nfs41_test_and_free_expired_stateid(server,
2884 &lsp->ls_stateid,
2885 cred);
2886 trace_nfs4_test_lock_stateid(state, lsp, status);
2887 if (status == -NFS4ERR_EXPIRED ||
2888 status == -NFS4ERR_BAD_STATEID) {
2889 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002890 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002891 if (!recover_lost_locks)
2892 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2893 } else if (status != NFS_OK) {
2894 ret = status;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002895 nfs4_put_lock_state(prev);
2896 goto out;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002897 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002898 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002899 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002900 }
2901 spin_unlock(&state->state_lock);
2902 nfs4_put_lock_state(prev);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002903out:
2904 return ret;
2905}
2906
2907/**
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002908 * nfs41_check_open_stateid - possibly free an open stateid
2909 *
2910 * @state: NFSv4 state for an inode
2911 *
2912 * Returns NFS_OK if recovery for this stateid is now finished.
2913 * Otherwise a negative NFS4ERR value is returned.
2914 */
2915static int nfs41_check_open_stateid(struct nfs4_state *state)
2916{
2917 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002918 nfs4_stateid *stateid = &state->open_stateid;
NeilBrowna52458b2018-12-03 11:30:31 +11002919 const struct cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002920 int status;
2921
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002922 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002923 return -NFS4ERR_BAD_STATEID;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002924 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002925 trace_nfs4_test_open_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002926 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002927 nfs_state_clear_open_state_flags(state);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002928 stateid->type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002929 return status;
Trond Myklebustc0ca0e52017-08-08 21:39:28 -04002930 }
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002931 if (nfs_open_stateid_recover_openmode(state))
2932 return -NFS4ERR_OPENMODE;
2933 return NFS_OK;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002934}
2935
2936static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2937{
Chuck Levereb64cf92012-07-11 16:30:05 -04002938 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002939
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002940 status = nfs41_check_delegation_stateid(state);
2941 if (status != NFS_OK)
2942 return status;
2943 nfs41_delegation_recover_stateid(state);
2944
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002945 status = nfs41_check_expired_locks(state);
2946 if (status != NFS_OK)
2947 return status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002948 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002949 if (status != NFS_OK)
2950 status = nfs4_open_expired(sp, state);
2951 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002952}
2953#endif
2954
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002956 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2957 * fields corresponding to attributes that were used to store the verifier.
2958 * Make sure we clobber those fields in the later setattr call
2959 */
Trond Myklebust609339c2018-03-28 16:18:17 -04002960static unsigned nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002961 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002962{
Trond Myklebust609339c2018-03-28 16:18:17 -04002963 const __u32 *bitmask = opendata->o_arg.server->exclcreat_bitmask;
2964 __u32 attrset[3];
2965 unsigned ret;
2966 unsigned i;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002967
Trond Myklebust609339c2018-03-28 16:18:17 -04002968 for (i = 0; i < ARRAY_SIZE(attrset); i++) {
2969 attrset[i] = opendata->o_res.attrset[i];
2970 if (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE4_1)
2971 attrset[i] &= ~bitmask[i];
2972 }
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002973
Trond Myklebust609339c2018-03-28 16:18:17 -04002974 ret = (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE) ?
2975 sattr->ia_valid : 0;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002976
Trond Myklebust609339c2018-03-28 16:18:17 -04002977 if ((attrset[1] & (FATTR4_WORD1_TIME_ACCESS|FATTR4_WORD1_TIME_ACCESS_SET))) {
2978 if (sattr->ia_valid & ATTR_ATIME_SET)
2979 ret |= ATTR_ATIME_SET;
2980 else
2981 ret |= ATTR_ATIME;
2982 }
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002983
Trond Myklebust609339c2018-03-28 16:18:17 -04002984 if ((attrset[1] & (FATTR4_WORD1_TIME_MODIFY|FATTR4_WORD1_TIME_MODIFY_SET))) {
2985 if (sattr->ia_valid & ATTR_MTIME_SET)
2986 ret |= ATTR_MTIME_SET;
2987 else
2988 ret |= ATTR_MTIME;
2989 }
2990
2991 if (!(attrset[2] & FATTR4_WORD2_SECURITY_LABEL))
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002992 *label = NULL;
Trond Myklebust609339c2018-03-28 16:18:17 -04002993 return ret;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002994}
2995
Trond Myklebustc21443c2013-02-07 14:26:21 -05002996static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
Trond Myklebust1bf85d82019-06-27 06:30:48 -04002997 int flags, struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002998{
2999 struct nfs4_state_owner *sp = opendata->owner;
3000 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04003001 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05003002 struct nfs4_state *state;
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003003 fmode_t acc_mode = _nfs4_ctx_to_accessmode(ctx);
Trond Myklebustcf5b4052020-02-05 09:01:53 -05003004 struct inode *dir = d_inode(opendata->dir);
3005 unsigned long dir_verifier;
Trond Myklebustc21443c2013-02-07 14:26:21 -05003006 unsigned int seq;
3007 int ret;
3008
3009 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
Trond Myklebustcf5b4052020-02-05 09:01:53 -05003010 dir_verifier = nfs_save_change_attribute(dir);
Trond Myklebustc21443c2013-02-07 14:26:21 -05003011
Fred Isaman3b65a302016-09-19 10:06:49 -04003012 ret = _nfs4_proc_open(opendata, ctx);
Trond Myklebustdca780012014-10-23 19:23:03 +03003013 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05003014 goto out;
3015
Trond Myklebustae55e592018-05-22 11:17:16 -04003016 state = _nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebustc21443c2013-02-07 14:26:21 -05003017 ret = PTR_ERR(state);
3018 if (IS_ERR(state))
3019 goto out;
Trond Myklebusta974dee2017-02-08 11:29:46 -05003020 ctx->state = state;
Trond Myklebustc21443c2013-02-07 14:26:21 -05003021 if (server->caps & NFS_CAP_POSIX_LOCK)
3022 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Jeff Laytona8ce3772016-09-17 18:17:35 -04003023 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
3024 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05003025
Trond Myklebust275bb302013-05-29 13:11:28 -04003026 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00003027 if (d_really_is_negative(dentry)) {
Al Viro668d0cd2016-03-08 12:44:17 -05003028 struct dentry *alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04003029 d_drop(dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05003030 alias = d_exact_alias(dentry, state->inode);
3031 if (!alias)
3032 alias = d_splice_alias(igrab(state->inode), dentry);
3033 /* d_splice_alias() can't fail here - it's a non-directory */
3034 if (alias) {
Trond Myklebust275bb302013-05-29 13:11:28 -04003035 dput(ctx->dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05003036 ctx->dentry = dentry = alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04003037 }
Trond Myklebustcf5b4052020-02-05 09:01:53 -05003038 }
3039
3040 switch(opendata->o_arg.claim) {
3041 default:
3042 break;
3043 case NFS4_OPEN_CLAIM_NULL:
3044 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
3045 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
3046 if (!opendata->rpc_done)
3047 break;
3048 if (opendata->o_res.delegation_type != 0)
3049 dir_verifier = nfs_save_change_attribute(dir);
3050 nfs_set_verifier(dentry, dir_verifier);
Trond Myklebust275bb302013-05-29 13:11:28 -04003051 }
3052
Trond Myklebustaf9b6d72018-06-29 12:45:53 -04003053 /* Parse layoutget results before we check for access */
3054 pnfs_parse_lgopen(state->inode, opendata->lgp, ctx);
3055
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003056 ret = nfs4_opendata_access(sp->so_cred, opendata, state,
3057 acc_mode, flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05003058 if (ret != 0)
3059 goto out;
3060
David Howells2b0143b2015-03-17 22:25:59 +00003061 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04003062 nfs_inode_attach_open_context(ctx);
3063 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
3064 nfs4_schedule_stateid_recovery(server, state);
3065 }
Fred Isaman2409a972016-10-06 12:11:21 -04003066
Trond Myklebustc21443c2013-02-07 14:26:21 -05003067out:
Olga Kornievskaia0cb98ab2019-03-19 12:12:13 -04003068 if (!opendata->cancelled)
3069 nfs4_sequence_free_slot(&opendata->o_res.seq_res);
Trond Myklebustc21443c2013-02-07 14:26:21 -05003070 return ret;
3071}
3072
Jeff Laytonaa53ed52007-06-05 14:49:03 -04003073/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01003074 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 */
Andy Adamson82be4172012-05-23 05:02:35 -04003076static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04003077 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04003078 int flags,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003079 const struct nfs4_open_createattrs *c,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003080 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081{
3082 struct nfs4_state_owner *sp;
3083 struct nfs4_state *state = NULL;
3084 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01003085 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04003086 struct dentry *dentry = ctx->dentry;
NeilBrowna52458b2018-12-03 11:30:31 +11003087 const struct cred *cred = ctx->cred;
Trond Myklebust4197a052013-05-29 12:37:49 -04003088 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003089 fmode_t fmode = _nfs4_ctx_to_openmode(ctx);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003090 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003091 struct iattr *sattr = c->sattr;
3092 struct nfs4_label *label = c->label;
David Quigley1775fd32013-05-22 12:50:42 -04003093 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04003094 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095
3096 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05003098 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
3099 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
3101 goto out_err;
3102 }
Anna Schumaker334f87d2017-01-11 16:17:17 -05003103 status = nfs4_client_recover_expired_lease(server->nfs_client);
Trond Myklebust58d97142006-01-03 09:55:24 +01003104 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003105 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00003106 if (d_really_is_positive(dentry))
3107 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01003108 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00003109 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003110 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003111 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
3112 c, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01003113 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05003114 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115
David Quigley14c43f72013-05-22 12:50:43 -04003116 if (label) {
3117 olabel = nfs4_label_alloc(server, GFP_KERNEL);
3118 if (IS_ERR(olabel)) {
3119 status = PTR_ERR(olabel);
3120 goto err_opendata_put;
3121 }
3122 }
3123
Trond Myklebuste911b812014-03-26 13:24:37 -07003124 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
3125 if (!opendata->f_attr.mdsthreshold) {
3126 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
3127 if (!opendata->f_attr.mdsthreshold)
3128 goto err_free_label;
3129 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04003130 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04003131 }
David Howells2b0143b2015-03-17 22:25:59 +00003132 if (d_really_is_positive(dentry))
3133 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04003134
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003135 status = _nfs4_open_and_get_state(opendata, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003136 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04003137 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04003138 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003139
NeilBrownefcbc042015-07-30 13:00:56 +10003140 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04003141 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Trond Myklebust609339c2018-03-28 16:18:17 -04003142 unsigned attrs = nfs4_exclusive_attrset(opendata, sattr, &label);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02003143 /*
3144 * send create attributes which was not set by open
3145 * with an extra setattr.
3146 */
Trond Myklebust609339c2018-03-28 16:18:17 -04003147 if (attrs || label) {
3148 unsigned ia_old = sattr->ia_valid;
3149
3150 sattr->ia_valid = attrs;
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02003151 nfs_fattr_init(opendata->o_res.f_attr);
3152 status = nfs4_do_setattr(state->inode, cred,
3153 opendata->o_res.f_attr, sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11003154 ctx, label, olabel);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02003155 if (status == 0) {
3156 nfs_setattr_update_inode(state->inode, sattr,
3157 opendata->o_res.f_attr);
3158 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
3159 }
Trond Myklebust609339c2018-03-28 16:18:17 -04003160 sattr->ia_valid = ia_old;
David Quigley1775fd32013-05-22 12:50:42 -04003161 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04003162 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08003163 if (opened && opendata->file_created)
Al Viro73a09dd2018-06-08 13:22:02 -04003164 *opened = 1;
Andy Adamson82be4172012-05-23 05:02:35 -04003165
Trond Myklebuste911b812014-03-26 13:24:37 -07003166 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04003167 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07003168 opendata->f_attr.mdsthreshold = NULL;
3169 }
Andy Adamson82be4172012-05-23 05:02:35 -04003170
David Quigley14c43f72013-05-22 12:50:43 -04003171 nfs4_label_free(olabel);
3172
Trond Myklebustc6d00e62007-06-17 16:02:44 -04003173 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04003176err_free_label:
3177 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04003178err_opendata_put:
3179 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01003180err_put_state_owner:
3181 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 return status;
3184}
3185
3186
Andy Adamson82be4172012-05-23 05:02:35 -04003187static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04003188 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04003189 int flags,
3190 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003191 struct nfs4_label *label,
3192 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003194 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust0688e642019-04-07 13:59:09 -04003195 struct nfs4_exception exception = {
3196 .interruptible = true,
3197 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 struct nfs4_state *res;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003199 struct nfs4_open_createattrs c = {
3200 .label = label,
3201 .sattr = sattr,
3202 .verf = {
3203 [0] = (__u32)jiffies,
3204 [1] = (__u32)current->pid,
3205 },
3206 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207 int status;
3208
3209 do {
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003210 status = _nfs4_do_open(dir, ctx, flags, &c, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04003211 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04003212 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213 if (status == 0)
3214 break;
3215 /* NOTE: BAD_SEQID means the server and client disagree about the
3216 * book-keeping w.r.t. state-changing operations
3217 * (OPEN/CLOSE/LOCK/LOCKU...)
3218 * It is actually a sign of a bug on the client or on the server.
3219 *
3220 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07003221 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222 * have unhashed the old state_owner for us, and that we can
3223 * therefore safely retry using a new one. We should still warn
3224 * the user though...
3225 */
3226 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04003227 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04003228 " returned a bad sequence-id error!\n",
3229 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230 exception.retry = 1;
3231 continue;
3232 }
Trond Myklebust550f5742005-10-18 14:20:21 -07003233 /*
3234 * BAD_STATEID on OPEN means that the server cancelled our
3235 * state before it received the OPEN_CONFIRM.
3236 * Recover by retrying the request as per the discussion
3237 * on Page 181 of RFC3530.
3238 */
3239 if (status == -NFS4ERR_BAD_STATEID) {
3240 exception.retry = 1;
3241 continue;
3242 }
Robert Milkowski924491f2020-01-28 08:37:47 +00003243 if (status == -NFS4ERR_EXPIRED) {
3244 nfs4_schedule_lease_recovery(server->nfs_client);
3245 exception.retry = 1;
3246 continue;
3247 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04003248 if (status == -EAGAIN) {
3249 /* We must have found a delegation */
3250 exception.retry = 1;
3251 continue;
3252 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003253 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
3254 continue;
3255 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 status, &exception));
3257 } while (exception.retry);
3258 return res;
3259}
3260
Trond Myklebust8487c472016-06-26 08:44:35 -04003261static int _nfs4_do_setattr(struct inode *inode,
3262 struct nfs_setattrargs *arg,
3263 struct nfs_setattrres *res,
NeilBrowna52458b2018-12-03 11:30:31 +11003264 const struct cred *cred,
NeilBrown29b59f92016-10-13 15:26:47 +11003265 struct nfs_open_context *ctx)
Trond Myklebust8487c472016-06-26 08:44:35 -04003266{
3267 struct nfs_server *server = NFS_SERVER(inode);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003268 struct rpc_message msg = {
Trond Myklebust8487c472016-06-26 08:44:35 -04003269 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
3270 .rpc_argp = arg,
3271 .rpc_resp = res,
3272 .rpc_cred = cred,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003273 };
NeilBrowna52458b2018-12-03 11:30:31 +11003274 const struct cred *delegation_cred = NULL;
Trond Myklebust8487c472016-06-26 08:44:35 -04003275 unsigned long timestamp = jiffies;
Trond Myklebust8487c472016-06-26 08:44:35 -04003276 bool truncate;
3277 int status;
3278
3279 nfs_fattr_init(res->fattr);
3280
3281 /* Servers should only apply open mode checks for file size changes */
3282 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
Chuck Lever644c9f42020-09-04 17:39:12 -04003283 if (!truncate) {
3284 nfs4_inode_make_writeable(inode);
Trond Myklebust991eedb2018-04-09 11:15:30 -04003285 goto zero_stateid;
Chuck Lever644c9f42020-09-04 17:39:12 -04003286 }
Trond Myklebust8487c472016-06-26 08:44:35 -04003287
Trond Myklebust991eedb2018-04-09 11:15:30 -04003288 if (nfs4_copy_delegation_stateid(inode, FMODE_WRITE, &arg->stateid, &delegation_cred)) {
Trond Myklebust8487c472016-06-26 08:44:35 -04003289 /* Use that stateid */
Trond Myklebust09a54f02019-08-03 10:28:18 -04003290 } else if (ctx != NULL && ctx->state) {
NeilBrown17393472016-10-13 15:26:47 +11003291 struct nfs_lock_context *l_ctx;
NeilBrown29b59f92016-10-13 15:26:47 +11003292 if (!nfs4_valid_open_stateid(ctx->state))
Trond Myklebust8487c472016-06-26 08:44:35 -04003293 return -EBADF;
NeilBrown17393472016-10-13 15:26:47 +11003294 l_ctx = nfs_get_lock_context(ctx);
3295 if (IS_ERR(l_ctx))
3296 return PTR_ERR(l_ctx);
NeilBrown7a0566b2016-12-06 15:50:06 -05003297 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
3298 &arg->stateid, &delegation_cred);
3299 nfs_put_lock_context(l_ctx);
3300 if (status == -EIO)
Trond Myklebust8487c472016-06-26 08:44:35 -04003301 return -EBADF;
Olga Kornievskaiad826e5b2019-12-18 16:50:42 -05003302 else if (status == -EAGAIN)
3303 goto zero_stateid;
Trond Myklebust991eedb2018-04-09 11:15:30 -04003304 } else {
3305zero_stateid:
Trond Myklebust8487c472016-06-26 08:44:35 -04003306 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
Trond Myklebust991eedb2018-04-09 11:15:30 -04003307 }
Trond Myklebust8487c472016-06-26 08:44:35 -04003308 if (delegation_cred)
3309 msg.rpc_cred = delegation_cred;
3310
3311 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
3312
NeilBrowna52458b2018-12-03 11:30:31 +11003313 put_cred(delegation_cred);
NeilBrown29b59f92016-10-13 15:26:47 +11003314 if (status == 0 && ctx != NULL)
Trond Myklebust8487c472016-06-26 08:44:35 -04003315 renew_lease(server, timestamp);
3316 trace_nfs4_setattr(inode, &arg->stateid, status);
3317 return status;
3318}
3319
NeilBrowna52458b2018-12-03 11:30:31 +11003320static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
Trond Myklebust8487c472016-06-26 08:44:35 -04003321 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11003322 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
Trond Myklebust8487c472016-06-26 08:44:35 -04003323 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05003325 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebust30846df2018-04-07 13:44:28 -04003326 __u32 bitmask[NFS4_BITMASK_SZ];
NeilBrown29b59f92016-10-13 15:26:47 +11003327 struct nfs4_state *state = ctx ? ctx->state : NULL;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003328 struct nfs_setattrargs arg = {
3329 .fh = NFS_FH(inode),
3330 .iap = sattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331 .server = server,
Trond Myklebust30846df2018-04-07 13:44:28 -04003332 .bitmask = bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04003333 .label = ilabel,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003334 };
3335 struct nfs_setattrres res = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003337 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338 .server = server,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003339 };
Trond Myklebust8487c472016-06-26 08:44:35 -04003340 struct nfs4_exception exception = {
3341 .state = state,
3342 .inode = inode,
3343 .stateid = &arg.stateid,
3344 };
Trond Myklebusta71029b2021-04-10 00:23:03 -04003345 unsigned long adjust_flags = NFS_INO_INVALID_CHANGE;
Trond Myklebust8487c472016-06-26 08:44:35 -04003346 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347
Trond Myklebust720869e2021-04-13 09:41:16 -04003348 if (sattr->ia_valid & (ATTR_MODE | ATTR_KILL_SUID | ATTR_KILL_SGID))
3349 adjust_flags |= NFS_INO_INVALID_MODE;
3350 if (sattr->ia_valid & (ATTR_UID | ATTR_GID))
Trond Myklebusta71029b2021-04-10 00:23:03 -04003351 adjust_flags |= NFS_INO_INVALID_OTHER;
3352
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353 do {
Trond Myklebusta71029b2021-04-10 00:23:03 -04003354 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, olabel),
3355 inode, adjust_flags);
Trond Myklebust30846df2018-04-07 13:44:28 -04003356
NeilBrown29b59f92016-10-13 15:26:47 +11003357 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
Trond Myklebust451146b2012-04-18 16:29:11 -04003358 switch (err) {
3359 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04003360 if (!(sattr->ia_valid & ATTR_SIZE)) {
3361 pr_warn_once("NFSv4: server %s is incorrectly "
3362 "applying open mode checks to "
3363 "a SETATTR that is not "
3364 "changing file size.\n",
3365 server->nfs_client->cl_hostname);
3366 }
Trond Myklebust451146b2012-04-18 16:29:11 -04003367 if (state && !(state->state & FMODE_WRITE)) {
3368 err = -EBADF;
3369 if (sattr->ia_valid & ATTR_OPEN)
3370 err = -EACCES;
3371 goto out;
3372 }
3373 }
3374 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04003376out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 return err;
3378}
3379
Peng Tao500d7012015-09-22 11:35:22 +08003380static bool
3381nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3382{
3383 if (inode == NULL || !nfs_have_layout(inode))
3384 return false;
3385
3386 return pnfs_wait_on_layoutreturn(inode, task);
3387}
3388
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003389/*
3390 * Update the seqid of an open stateid
3391 */
3392static void nfs4_sync_open_stateid(nfs4_stateid *dst,
3393 struct nfs4_state *state)
3394{
3395 __be32 seqid_open;
3396 u32 dst_seqid;
3397 int seq;
3398
3399 for (;;) {
3400 if (!nfs4_valid_open_stateid(state))
3401 break;
3402 seq = read_seqbegin(&state->seqlock);
3403 if (!nfs4_state_match_open_stateid_other(state, dst)) {
3404 nfs4_stateid_copy(dst, &state->open_stateid);
3405 if (read_seqretry(&state->seqlock, seq))
3406 continue;
3407 break;
3408 }
3409 seqid_open = state->open_stateid.seqid;
3410 if (read_seqretry(&state->seqlock, seq))
3411 continue;
3412
3413 dst_seqid = be32_to_cpu(dst->seqid);
3414 if ((s32)(dst_seqid - be32_to_cpu(seqid_open)) < 0)
3415 dst->seqid = seqid_open;
3416 break;
3417 }
3418}
3419
3420/*
3421 * Update the seqid of an open stateid after receiving
3422 * NFS4ERR_OLD_STATEID
3423 */
3424static bool nfs4_refresh_open_old_stateid(nfs4_stateid *dst,
3425 struct nfs4_state *state)
3426{
3427 __be32 seqid_open;
3428 u32 dst_seqid;
3429 bool ret;
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04003430 int seq, status = -EAGAIN;
3431 DEFINE_WAIT(wait);
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003432
3433 for (;;) {
3434 ret = false;
3435 if (!nfs4_valid_open_stateid(state))
3436 break;
3437 seq = read_seqbegin(&state->seqlock);
3438 if (!nfs4_state_match_open_stateid_other(state, dst)) {
3439 if (read_seqretry(&state->seqlock, seq))
3440 continue;
3441 break;
3442 }
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04003443
3444 write_seqlock(&state->seqlock);
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003445 seqid_open = state->open_stateid.seqid;
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003446
3447 dst_seqid = be32_to_cpu(dst->seqid);
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04003448
3449 /* Did another OPEN bump the state's seqid? try again: */
3450 if ((s32)(be32_to_cpu(seqid_open) - dst_seqid) > 0) {
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003451 dst->seqid = seqid_open;
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04003452 write_sequnlock(&state->seqlock);
3453 ret = true;
3454 break;
3455 }
3456
3457 /* server says we're behind but we haven't seen the update yet */
3458 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
3459 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
3460 write_sequnlock(&state->seqlock);
3461 trace_nfs4_close_stateid_update_wait(state->inode, dst, 0);
3462
3463 if (signal_pending(current))
3464 status = -EINTR;
3465 else
3466 if (schedule_timeout(5*HZ) != 0)
3467 status = 0;
3468
3469 finish_wait(&state->waitq, &wait);
3470
3471 if (!status)
3472 continue;
3473 if (status == -EINTR)
3474 break;
3475
3476 /* we slept the whole 5 seconds, we must have lost a seqid */
3477 dst->seqid = cpu_to_be32(dst_seqid + 1);
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003478 ret = true;
3479 break;
3480 }
3481
3482 return ret;
3483}
3484
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485struct nfs4_closedata {
3486 struct inode *inode;
3487 struct nfs4_state *state;
3488 struct nfs_closeargs arg;
3489 struct nfs_closeres res;
Trond Myklebustcf805162016-11-15 14:56:07 -05003490 struct {
3491 struct nfs4_layoutreturn_args arg;
3492 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003493 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebustcf805162016-11-15 14:56:07 -05003494 u32 roc_barrier;
3495 bool roc;
3496 } lr;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003497 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003498 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499};
3500
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003501static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07003502{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003503 struct nfs4_closedata *calldata = data;
3504 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04003505 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07003506
Trond Myklebustcf805162016-11-15 14:56:07 -05003507 if (calldata->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003508 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3509 calldata->res.lr_ret);
Trond Myklebust95121352005-10-18 14:20:12 -07003510 nfs4_put_open_state(calldata->state);
3511 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07003512 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05003513 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003514 kfree(calldata);
3515}
3516
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003517static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003519 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003522 nfs4_stateid *res_stateid = NULL;
Trond Myklebustb8b8d222017-11-07 10:51:37 -05003523 struct nfs4_exception exception = {
3524 .state = state,
3525 .inode = calldata->inode,
3526 .stateid = &calldata->arg.stateid,
3527 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528
Chuck Levera3ca5652012-03-01 17:00:40 -05003529 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04003530 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3531 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04003532 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Trond Myklebustcf805162016-11-15 14:56:07 -05003533
3534 /* Handle Layoutreturn errors */
Trond Myklebust078000d2021-01-04 13:18:03 -05003535 if (pnfs_roc_done(task, &calldata->arg.lr_args, &calldata->res.lr_res,
3536 &calldata->res.lr_ret) == -EAGAIN)
Trond Myklebust287a9c52019-09-20 07:23:41 -04003537 goto out_restart;
Trond Myklebustcf805162016-11-15 14:56:07 -05003538
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003539 /* hmm. we are done with the inode, and in the process of freeing
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540 * the state_owner. we keep this around to process errors
3541 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542 switch (task->tk_status) {
3543 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04003544 res_stateid = &calldata->res.stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003545 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003546 break;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003547 case -NFS4ERR_ACCESS:
3548 if (calldata->arg.bitmask != NULL) {
3549 calldata->arg.bitmask = NULL;
3550 calldata->res.fattr = NULL;
Trond Myklebust91b30d22017-11-06 15:28:09 -05003551 goto out_restart;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003552
3553 }
3554 break;
Trond Myklebust12f275c2017-11-06 15:28:05 -05003555 case -NFS4ERR_OLD_STATEID:
3556 /* Did we race with OPEN? */
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003557 if (nfs4_refresh_open_old_stateid(&calldata->arg.stateid,
Trond Myklebust91b30d22017-11-06 15:28:09 -05003558 state))
3559 goto out_restart;
Trond Myklebust12f275c2017-11-06 15:28:05 -05003560 goto out_release;
Trond Myklebust69794ad2013-11-20 12:57:19 -05003561 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562 case -NFS4ERR_STALE_STATEID:
Trond Myklebust26d36302016-09-22 13:39:05 -04003563 case -NFS4ERR_EXPIRED:
3564 nfs4_free_revoked_stateid(server,
3565 &calldata->arg.stateid,
3566 task->tk_msg.rpc_cred);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05003567 fallthrough;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003568 case -NFS4ERR_BAD_STATEID:
Trond Myklebuste217e822019-09-20 07:23:46 -04003569 if (calldata->arg.fmode == 0)
3570 break;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05003571 fallthrough;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572 default:
Trond Myklebustb8b8d222017-11-07 10:51:37 -05003573 task->tk_status = nfs4_async_handle_exception(task,
3574 server, task->tk_status, &exception);
3575 if (exception.retry)
Trond Myklebust91b30d22017-11-06 15:28:09 -05003576 goto out_restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07003578 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3579 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003580out_release:
Trond Myklebust91b30d22017-11-06 15:28:09 -05003581 task->tk_status = 0;
Trond Myklebust72211db2009-12-15 14:47:36 -05003582 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustd8d84982016-12-19 12:14:44 -05003583 nfs_refresh_inode(calldata->inode, &calldata->fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05003584 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Trond Myklebust91b30d22017-11-06 15:28:09 -05003585 return;
Trond Myklebust91b30d22017-11-06 15:28:09 -05003586out_restart:
3587 task->tk_status = 0;
3588 rpc_restart_call_prepare(task);
3589 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590}
3591
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003592static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003594 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07003595 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003596 struct inode *inode = calldata->inode;
Trond Myklebust332d1a02021-03-25 18:15:36 -04003597 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustc8bf7072018-06-15 16:31:02 -04003598 struct pnfs_layout_hdr *lo;
Trond Myklebustaee7af32014-08-25 22:33:12 -04003599 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05003600 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07003601
Chuck Levera3ca5652012-03-01 17:00:40 -05003602 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003603 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003604 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04003605
Trond Myklebust88069f72009-12-08 08:33:16 -05003606 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05003607 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04003608 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3609 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3610 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust003707c2007-07-05 18:07:55 -04003611 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003612 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01003613 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003614 if (state->n_rdonly == 0)
3615 call_close |= is_rdonly;
3616 else if (is_rdonly)
3617 calldata->arg.fmode |= FMODE_READ;
3618 if (state->n_wronly == 0)
3619 call_close |= is_wronly;
3620 else if (is_wronly)
3621 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebuste547f262016-06-25 19:19:28 -04003622 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3623 call_close |= is_rdwr;
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003624 } else if (is_rdwr)
3625 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3626
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003627 nfs4_sync_open_stateid(&calldata->arg.stateid, state);
3628 if (!nfs4_valid_open_stateid(state))
Trond Myklebust5d422302013-03-14 16:57:48 -04003629 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05003630 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05003631
3632 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003633 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003634 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07003635 }
Trond Myklebust88069f72009-12-08 08:33:16 -05003636
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003637 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
Peng Tao500d7012015-09-22 11:35:22 +08003638 nfs_release_seqid(calldata->arg.seqid);
3639 goto out_wait;
3640 }
3641
Trond Myklebustc8bf7072018-06-15 16:31:02 -04003642 lo = calldata->arg.lr_args ? calldata->arg.lr_args->layout : NULL;
3643 if (lo && !pnfs_layout_is_valid(lo)) {
3644 calldata->arg.lr_args = NULL;
3645 calldata->res.lr_res = NULL;
3646 }
3647
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003648 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05003649 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003650
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003651 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003652 /* Close-to-open cache consistency revalidation */
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04003653 if (!nfs4_have_delegation(inode, FMODE_READ)) {
Trond Myklebust332d1a02021-03-25 18:15:36 -04003654 nfs4_bitmask_set(calldata->arg.bitmask_store,
3655 server->cache_consistency_bitmask,
3656 inode, server, NULL);
3657 calldata->arg.bitmask = calldata->arg.bitmask_store;
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04003658 } else
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003659 calldata->arg.bitmask = NULL;
3660 }
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003661
Trond Myklebust6ae37332015-01-30 14:21:14 -05003662 calldata->arg.share_access =
3663 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3664 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05003665
Trond Myklebustd8d84982016-12-19 12:14:44 -05003666 if (calldata->res.fattr == NULL)
3667 calldata->arg.bitmask = NULL;
3668 else if (calldata->arg.bitmask == NULL)
3669 calldata->res.fattr = NULL;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003670 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05003671 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
Trond Myklebust9d12b212012-01-17 22:04:25 -05003672 &calldata->arg.seq_args,
3673 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04003674 task) != 0)
3675 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05003676 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003677 return;
3678out_no_action:
3679 task->tk_action = NULL;
3680out_wait:
3681 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682}
3683
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003684static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003685 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003686 .rpc_call_done = nfs4_close_done,
3687 .rpc_release = nfs4_free_closedata,
3688};
3689
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690/*
3691 * It is possible for data to be read/written from a mem-mapped file
3692 * after the sys_close call (which hits the vfs layer as a flush).
3693 * This means that we can't safely call nfsv4 close on a file until
3694 * the inode is cleared. This in turn means that we are not good
3695 * NFSv4 citizens - we do not indicate to the server to update the file's
3696 * share state even when we are done with one of the three share
3697 * stateid's in the inode.
3698 *
3699 * NOTE: Caller must be holding the sp->so_owner semaphore!
3700 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04003701int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003703 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05003704 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04003706 struct nfs4_state_owner *sp = state->owner;
3707 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003708 struct rpc_message msg = {
3709 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3710 .rpc_cred = state->owner->so_cred,
3711 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003712 struct rpc_task_setup task_setup_data = {
3713 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003714 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003715 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003716 .workqueue = nfsiod_workqueue,
Trond Myklebust61296502020-02-07 19:38:12 -05003717 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003718 };
Trond Myklebust95121352005-10-18 14:20:12 -07003719 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04003721 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3722 &task_setup_data.rpc_client, &msg);
3723
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003724 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07003726 goto out;
Anna Schumakerfba83f32018-05-04 16:22:50 -04003727 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1, 0);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003728 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003730 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebustc82bac62017-11-06 15:28:06 -05003731 if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state))
3732 goto out_free_calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05003734 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3735 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05003736 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07003737 goto out_free_calldata;
Trond Myklebustd8d84982016-12-19 12:14:44 -05003738 nfs_fattr_init(&calldata->fattr);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003739 calldata->arg.fmode = 0;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003740 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003741 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003742 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003743 calldata->res.server = server;
Trond Myklebustcf805162016-11-15 14:56:07 -05003744 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003745 calldata->lr.roc = pnfs_roc(state->inode,
3746 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3747 if (calldata->lr.roc) {
3748 calldata->arg.lr_args = &calldata->lr.arg;
3749 calldata->res.lr_res = &calldata->lr.res;
3750 }
Al Viro643168c2011-06-22 18:20:23 -04003751 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003752
Trond Myklebust1174dd12010-12-21 10:52:24 -05003753 msg.rpc_argp = &calldata->arg;
3754 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003755 task_setup_data.callback_data = calldata;
3756 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003757 if (IS_ERR(task))
3758 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003759 status = 0;
3760 if (wait)
3761 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003762 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003763 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003764out_free_calldata:
3765 kfree(calldata);
3766out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003767 nfs4_put_open_state(state);
3768 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003769 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770}
3771
Trond Myklebust2b484292010-09-17 10:56:51 -04003772static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003773nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3774 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003777 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3778
3779 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780
Trond Myklebust565277f2007-10-15 18:17:53 -04003781 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003782 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003783
3784 nfs4_label_release_security(label);
3785
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003786 if (IS_ERR(state))
3787 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003788 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789}
3790
Trond Myklebust1185a552009-12-03 15:54:02 -05003791static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003792{
3793 if (ctx->state == NULL)
3794 return;
3795 if (is_sync)
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003796 nfs4_close_sync(ctx->state, _nfs4_ctx_to_openmode(ctx));
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003797 else
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003798 nfs4_close_state(ctx->state, _nfs4_ctx_to_openmode(ctx));
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003799}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800
Trond Myklebustb944dba2013-11-04 15:20:20 -05003801#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3802#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
Frank van der Lindenb78ef842020-06-23 22:38:55 +00003803#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_XATTR_SUPPORT - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003804
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3806{
Kinglong Mee8c612822015-08-26 21:12:58 +08003807 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003808 struct nfs4_server_caps_arg args = {
3809 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003810 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003811 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812 struct nfs4_server_caps_res res = {};
3813 struct rpc_message msg = {
3814 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003815 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816 .rpc_resp = &res,
3817 };
3818 int status;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003819 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820
Kinglong Mee8c612822015-08-26 21:12:58 +08003821 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3822 FATTR4_WORD0_FH_EXPIRE_TYPE |
3823 FATTR4_WORD0_LINK_SUPPORT |
3824 FATTR4_WORD0_SYMLINK_SUPPORT |
3825 FATTR4_WORD0_ACLSUPPORT;
3826 if (minorversion)
3827 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3828
Bryan Schumaker7c513052011-03-24 17:12:24 +00003829 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003831 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003832 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003833 case 0:
3834 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3835 res.attr_bitmask[2] = 0;
3836 break;
3837 case 1:
3838 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3839 break;
3840 case 2:
3841 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3842 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04003844 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3845 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3846 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3847 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003848 NFS_CAP_CTIME|NFS_CAP_MTIME|
3849 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003850 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3851 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852 server->caps |= NFS_CAP_ACLS;
3853 if (res.has_links != 0)
3854 server->caps |= NFS_CAP_HARDLINKS;
3855 if (res.has_symlinks != 0)
3856 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003857 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3858 server->caps |= NFS_CAP_FILEID;
3859 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3860 server->caps |= NFS_CAP_MODE;
3861 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3862 server->caps |= NFS_CAP_NLINK;
3863 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3864 server->caps |= NFS_CAP_OWNER;
3865 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3866 server->caps |= NFS_CAP_OWNER_GROUP;
3867 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3868 server->caps |= NFS_CAP_ATIME;
3869 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3870 server->caps |= NFS_CAP_CTIME;
3871 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3872 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003873#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3874 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3875 server->caps |= NFS_CAP_SECURITY_LABEL;
3876#endif
3877 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3878 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003879 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003880
Trond Myklebusta65318b2009-03-11 14:10:28 -04003881 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3882 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3883 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003884 server->cache_consistency_bitmask[2] = 0;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003885
3886 /* Avoid a regression due to buggy server */
3887 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3888 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
Kinglong Mee8c612822015-08-26 21:12:58 +08003889 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3890 sizeof(server->exclcreat_bitmask));
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003891
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003893 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003894 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003895
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896 return status;
3897}
3898
Trond Myklebust55a97592006-06-09 09:34:19 -04003899int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900{
Trond Myklebust0688e642019-04-07 13:59:09 -04003901 struct nfs4_exception exception = {
3902 .interruptible = true,
3903 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904 int err;
3905 do {
3906 err = nfs4_handle_exception(server,
3907 _nfs4_server_capabilities(server, fhandle),
3908 &exception);
3909 } while (exception.retry);
3910 return err;
3911}
3912
3913static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3914 struct nfs_fsinfo *info)
3915{
David Quigleyaa9c2662013-05-22 12:50:44 -04003916 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003918 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919 };
3920 struct nfs4_lookup_res res = {
3921 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003922 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923 .fh = fhandle,
3924 };
3925 struct rpc_message msg = {
3926 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3927 .rpc_argp = &args,
3928 .rpc_resp = &res,
3929 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003930
David Quigleyaa9c2662013-05-22 12:50:44 -04003931 bitmask[0] = nfs4_fattr_bitmap[0];
3932 bitmask[1] = nfs4_fattr_bitmap[1];
3933 /*
3934 * Process the label in the upcoming getfattr
3935 */
3936 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3937
Trond Myklebust0e574af2005-10-27 22:12:38 -04003938 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003939 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940}
3941
3942static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3943 struct nfs_fsinfo *info)
3944{
Trond Myklebust0688e642019-04-07 13:59:09 -04003945 struct nfs4_exception exception = {
3946 .interruptible = true,
3947 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948 int err;
3949 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003950 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003951 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003952 switch (err) {
3953 case 0:
3954 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003955 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003956 default:
3957 err = nfs4_handle_exception(server, err, &exception);
3958 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003960out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961 return err;
3962}
3963
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003964static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3965 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3966{
Trond Myklebustc2190662013-08-26 19:23:04 -04003967 struct rpc_auth_create_args auth_args = {
3968 .pseudoflavor = flavor,
3969 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003970 struct rpc_auth *auth;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003971
Trond Myklebustc2190662013-08-26 19:23:04 -04003972 auth = rpcauth_create(&auth_args, server->client);
Anna Schumaker9df13362017-01-11 16:30:08 -05003973 if (IS_ERR(auth))
3974 return -EACCES;
3975 return nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003976}
3977
Chuck Lever9a744ba2013-03-16 15:56:02 -04003978/*
3979 * Retry pseudoroot lookup with various security flavors. We do this when:
3980 *
3981 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3982 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3983 *
3984 * Returns zero on success, or a negative NFS4ERR value, or a
3985 * negative errno value.
3986 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003987static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003988 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003990 /* Per 3530bis 15.33.5 */
3991 static const rpc_authflavor_t flav_array[] = {
3992 RPC_AUTH_GSS_KRB5P,
3993 RPC_AUTH_GSS_KRB5I,
3994 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003995 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003996 RPC_AUTH_NULL,
3997 };
3998 int status = -EPERM;
3999 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04004001 if (server->auth_info.flavor_len > 0) {
4002 /* try each flavor specified by user */
4003 for (i = 0; i < server->auth_info.flavor_len; i++) {
4004 status = nfs4_lookup_root_sec(server, fhandle, info,
4005 server->auth_info.flavors[i]);
4006 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
4007 continue;
4008 break;
4009 }
4010 } else {
4011 /* no flavors specified by user, try default list */
4012 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
4013 status = nfs4_lookup_root_sec(server, fhandle, info,
4014 flav_array[i]);
4015 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
4016 continue;
4017 break;
4018 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00004019 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04004020
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04004021 /*
Colin Ian Kingd3787af2018-10-26 19:10:31 +01004022 * -EACCES could mean that the user doesn't have correct permissions
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04004023 * to access the mount. It could also mean that we tried to mount
4024 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
4025 * existing mount programs don't handle -EACCES very well so it should
4026 * be mapped to -EPERM instead.
4027 */
4028 if (status == -EACCES)
4029 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04004030 return status;
4031}
4032
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04004033/**
4034 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
4035 * @server: initialized nfs_server handle
4036 * @fhandle: we fill in the pseudo-fs root file handle
4037 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04004038 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04004039 *
4040 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04004041 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04004042int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04004043 struct nfs_fsinfo *info,
4044 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04004045{
Andre Przywarac7757072015-04-23 17:17:40 +01004046 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04004047
Andre Przywarac7757072015-04-23 17:17:40 +01004048 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04004049 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01004050
4051 if (auth_probe || status == NFS4ERR_WRONGSEC)
Trond Myklebust698c9372016-07-25 13:31:14 -04004052 status = server->nfs_client->cl_mvops->find_root_sec(server,
4053 fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04004054
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055 if (status == 0)
4056 status = nfs4_server_capabilities(server, fhandle);
4057 if (status == 0)
4058 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04004059
Trond Myklebustc12e87f2006-03-13 21:20:47 -08004060 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061}
4062
Bryan Schumakerbae36242012-05-10 15:07:31 -04004063static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
4064 struct nfs_fsinfo *info)
4065{
4066 int error;
4067 struct nfs_fattr *fattr = info->fattr;
Scott Mayhew779df6a2020-03-03 17:58:37 -05004068 struct nfs4_label *label = fattr->label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04004069
4070 error = nfs4_server_capabilities(server, mntfh);
4071 if (error < 0) {
4072 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
4073 return error;
4074 }
4075
Trond Myklebusta841b542018-04-07 13:50:59 -04004076 error = nfs4_proc_getattr(server, mntfh, fattr, label, NULL);
Bryan Schumakerbae36242012-05-10 15:07:31 -04004077 if (error < 0) {
4078 dprintk("nfs4_get_root: getattr error = %d\n", -error);
Scott Mayhew779df6a2020-03-03 17:58:37 -05004079 goto out;
Bryan Schumakerbae36242012-05-10 15:07:31 -04004080 }
4081
4082 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
4083 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
4084 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
4085
Scott Mayhew779df6a2020-03-03 17:58:37 -05004086out:
Bryan Schumakerbae36242012-05-10 15:07:31 -04004087 return error;
4088}
4089
Manoj Naik6b97fd32006-06-09 09:34:29 -04004090/*
4091 * Get locations and (maybe) other attributes of a referral.
4092 * Note that we'll actually follow the referral later when
4093 * we detect fsid mismatch in inode revalidation
4094 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004095static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
4096 const struct qstr *name, struct nfs_fattr *fattr,
4097 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04004098{
4099 int status = -ENOMEM;
4100 struct page *page = NULL;
4101 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04004102
4103 page = alloc_page(GFP_KERNEL);
4104 if (page == NULL)
4105 goto out;
4106 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
4107 if (locations == NULL)
4108 goto out;
4109
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004110 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04004111 if (status != 0)
4112 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04004113
4114 /*
4115 * If the fsid didn't change, this is a migration event, not a
4116 * referral. Cause us to drop into the exception handler, which
4117 * will kick off migration recovery.
4118 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04004119 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04004120 dprintk("%s: server did not return a different fsid for"
4121 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04004122 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04004123 goto out;
4124 }
Andy Adamson533eb462011-06-13 18:25:56 -04004125 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
4126 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04004127
Andy Adamson533eb462011-06-13 18:25:56 -04004128 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04004129 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04004130 memset(fhandle, 0, sizeof(struct nfs_fh));
4131out:
4132 if (page)
4133 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04004134 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04004135 return status;
4136}
4137
David Quigley1775fd32013-05-22 12:50:42 -04004138static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebusta841b542018-04-07 13:50:59 -04004139 struct nfs_fattr *fattr, struct nfs4_label *label,
4140 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141{
Trond Myklebust771734f2018-04-07 13:54:23 -04004142 __u32 bitmask[NFS4_BITMASK_SZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143 struct nfs4_getattr_arg args = {
4144 .fh = fhandle,
Trond Myklebust771734f2018-04-07 13:54:23 -04004145 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146 };
4147 struct nfs4_getattr_res res = {
4148 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04004149 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150 .server = server,
4151 };
4152 struct rpc_message msg = {
4153 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
4154 .rpc_argp = &args,
4155 .rpc_resp = &res,
4156 };
Trond Myklebustc74dfe92020-01-06 15:39:37 -05004157 unsigned short task_flags = 0;
4158
4159 /* Is this is an attribute revalidation, subject to softreval? */
4160 if (inode && (server->flags & NFS_MOUNT_SOFTREVAL))
4161 task_flags |= RPC_TASK_TIMEOUT;
David Quigleyaa9c2662013-05-22 12:50:44 -04004162
Trond Myklebusta71029b2021-04-10 00:23:03 -04004163 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, label), inode, 0);
Trond Myklebust0e574af2005-10-27 22:12:38 -04004164 nfs_fattr_init(fattr);
Trond Myklebustc74dfe92020-01-06 15:39:37 -05004165 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
4166 return nfs4_do_call_sync(server->client, server, &msg,
4167 &args.seq_args, &res.seq_res, task_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168}
4169
Olga Kornievskaiaec4b0922019-10-08 16:33:53 -04004170int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebusta841b542018-04-07 13:50:59 -04004171 struct nfs_fattr *fattr, struct nfs4_label *label,
4172 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173{
Trond Myklebust0688e642019-04-07 13:59:09 -04004174 struct nfs4_exception exception = {
4175 .interruptible = true,
4176 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177 int err;
4178 do {
Trond Myklebusta841b542018-04-07 13:50:59 -04004179 err = _nfs4_proc_getattr(server, fhandle, fattr, label, inode);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004180 trace_nfs4_getattr(server, fhandle, fattr, err);
4181 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182 &exception);
4183 } while (exception.retry);
4184 return err;
4185}
4186
4187/*
4188 * The file is not closed if it is opened due to the a request to change
4189 * the size of the file. The open call will not be needed once the
4190 * VFS layer lookup-intents are implemented.
4191 *
4192 * Close is called when the inode is destroyed.
4193 * If we haven't opened the file for O_WRONLY, we
4194 * need to in the size_change case to obtain a stateid.
4195 *
4196 * Got race?
4197 * Because OPEN is always done by name in nfsv4, it is
4198 * possible that we opened a different file by the same
4199 * name. We can recognize this race condition, but we
4200 * can't do anything about it besides returning an error.
4201 *
4202 * This will be fixed with VFS changes (lookup-intent).
4203 */
4204static int
4205nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
4206 struct iattr *sattr)
4207{
David Howells2b0143b2015-03-17 22:25:59 +00004208 struct inode *inode = d_inode(dentry);
NeilBrowna52458b2018-12-03 11:30:31 +11004209 const struct cred *cred = NULL;
NeilBrown29b59f92016-10-13 15:26:47 +11004210 struct nfs_open_context *ctx = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04004211 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212 int status;
4213
Peng Tao88ac8152014-09-12 11:04:10 +08004214 if (pnfs_ld_layoutret_on_setattr(inode) &&
4215 sattr->ia_valid & ATTR_SIZE &&
4216 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04004217 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04004218
Trond Myklebust0e574af2005-10-27 22:12:38 -04004219 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220
Andy Adamson26699402012-05-30 16:12:24 -04004221 /* Deal with open(O_TRUNC) */
4222 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03004223 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04004224
4225 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03004226 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04004227 return 0;
4228
Trond Myklebustd5308382005-11-04 15:33:38 -05004229 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04004230 if (sattr->ia_valid & ATTR_FILE) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00004231
Trond Myklebust659bfcd2008-06-10 19:39:41 -04004232 ctx = nfs_file_open_context(sattr->ia_file);
NeilBrown29b59f92016-10-13 15:26:47 +11004233 if (ctx)
Neil Brown504e5182008-10-16 14:15:16 +11004234 cred = ctx->cred;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04004235 }
4236
David Quigley14c43f72013-05-22 12:50:43 -04004237 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4238 if (IS_ERR(label))
4239 return PTR_ERR(label);
4240
Trond Myklebust199366f2018-03-20 16:43:18 -04004241 /* Return any delegations if we're going to change ACLs */
4242 if ((sattr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
Trond Myklebustc01d3642018-03-20 16:43:20 -04004243 nfs4_inode_make_writeable(inode);
Trond Myklebust199366f2018-03-20 16:43:18 -04004244
NeilBrown29b59f92016-10-13 15:26:47 +11004245 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04004246 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05004247 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04004248 nfs_setsecurity(inode, fattr, label);
4249 }
David Quigley14c43f72013-05-22 12:50:43 -04004250 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251 return status;
4252}
4253
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004254static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004255 struct dentry *dentry, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04004256 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04004257{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004258 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04004259 int status;
4260 struct nfs4_lookup_arg args = {
4261 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004262 .dir_fh = NFS_FH(dir),
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004263 .name = &dentry->d_name,
David Howells2b3de442006-08-22 20:06:09 -04004264 };
4265 struct nfs4_lookup_res res = {
4266 .server = server,
4267 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04004268 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04004269 .fh = fhandle,
4270 };
4271 struct rpc_message msg = {
4272 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
4273 .rpc_argp = &args,
4274 .rpc_resp = &res,
4275 };
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004276 unsigned short task_flags = 0;
4277
4278 /* Is this is an attribute revalidation, subject to softreval? */
4279 if (nfs_lookup_is_soft_revalidate(dentry))
4280 task_flags |= RPC_TASK_TIMEOUT;
David Howells2b3de442006-08-22 20:06:09 -04004281
David Quigleyaa9c2662013-05-22 12:50:44 -04004282 args.bitmask = nfs4_bitmask(server, label);
4283
David Howells2b3de442006-08-22 20:06:09 -04004284 nfs_fattr_init(fattr);
4285
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004286 dprintk("NFS call lookup %pd2\n", dentry);
4287 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
4288 status = nfs4_do_call_sync(clnt, server, &msg,
4289 &args.seq_args, &res.seq_res, task_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290 dprintk("NFS reply lookup: %d\n", status);
4291 return status;
4292}
4293
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004294static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00004295{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00004296 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004297 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00004298 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4299 fattr->nlink = 2;
4300}
4301
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004302static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004303 struct dentry *dentry, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04004304 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004305{
Trond Myklebust0688e642019-04-07 13:59:09 -04004306 struct nfs4_exception exception = {
4307 .interruptible = true,
4308 };
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004309 struct rpc_clnt *client = *clnt;
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004310 const struct qstr *name = &dentry->d_name;
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004311 int err;
4312 do {
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004313 err = _nfs4_proc_lookup(client, dir, dentry, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004314 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004315 switch (err) {
4316 case -NFS4ERR_BADNAME:
4317 err = -ENOENT;
4318 goto out;
4319 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004320 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02004321 if (err == -NFS4ERR_MOVED)
4322 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004323 goto out;
4324 case -NFS4ERR_WRONGSEC:
4325 err = -EPERM;
4326 if (client != *clnt)
4327 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04004328 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004329 if (IS_ERR(client))
4330 return PTR_ERR(client);
4331
4332 exception.retry = 1;
4333 break;
4334 default:
4335 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4336 }
4337 } while (exception.retry);
4338
4339out:
4340 if (err == 0)
4341 *clnt = client;
4342 else if (client != *clnt)
4343 rpc_shutdown_client(client);
4344
4345 return err;
4346}
4347
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004348static int nfs4_proc_lookup(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004349 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4350 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004352 int status;
4353 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004354
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004355 status = nfs4_proc_lookup_common(&client, dir, dentry, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004356 if (client != NFS_CLIENT(dir)) {
4357 rpc_shutdown_client(client);
4358 nfs_fixup_secinfo_attributes(fattr);
4359 }
4360 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361}
4362
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004363struct rpc_clnt *
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004364nfs4_proc_lookup_mountpoint(struct inode *dir, struct dentry *dentry,
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004365 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4366{
Trond Myklebustb72888c2013-08-07 20:38:07 -04004367 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004368 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004369
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004370 status = nfs4_proc_lookup_common(&client, dir, dentry, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04004371 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004372 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04004373 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004374}
4375
Jeff Layton5b5faaf2017-06-29 06:34:52 -07004376static int _nfs4_proc_lookupp(struct inode *inode,
4377 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4378 struct nfs4_label *label)
4379{
4380 struct rpc_clnt *clnt = NFS_CLIENT(inode);
4381 struct nfs_server *server = NFS_SERVER(inode);
4382 int status;
4383 struct nfs4_lookupp_arg args = {
4384 .bitmask = server->attr_bitmask,
4385 .fh = NFS_FH(inode),
4386 };
4387 struct nfs4_lookupp_res res = {
4388 .server = server,
4389 .fattr = fattr,
4390 .label = label,
4391 .fh = fhandle,
4392 };
4393 struct rpc_message msg = {
4394 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
4395 .rpc_argp = &args,
4396 .rpc_resp = &res,
4397 };
Trond Myklebust76998eb2020-10-20 14:30:35 -04004398 unsigned short task_flags = 0;
4399
4400 if (NFS_SERVER(inode)->flags & NFS_MOUNT_SOFTREVAL)
4401 task_flags |= RPC_TASK_TIMEOUT;
Jeff Layton5b5faaf2017-06-29 06:34:52 -07004402
4403 args.bitmask = nfs4_bitmask(server, label);
4404
4405 nfs_fattr_init(fattr);
4406
4407 dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino);
4408 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
Trond Myklebust76998eb2020-10-20 14:30:35 -04004409 &res.seq_res, task_flags);
Jeff Layton5b5faaf2017-06-29 06:34:52 -07004410 dprintk("NFS reply lookupp: %d\n", status);
4411 return status;
4412}
4413
4414static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
4415 struct nfs_fattr *fattr, struct nfs4_label *label)
4416{
Trond Myklebust0688e642019-04-07 13:59:09 -04004417 struct nfs4_exception exception = {
4418 .interruptible = true,
4419 };
Jeff Layton5b5faaf2017-06-29 06:34:52 -07004420 int err;
4421 do {
4422 err = _nfs4_proc_lookupp(inode, fhandle, fattr, label);
4423 trace_nfs4_lookupp(inode, err);
4424 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4425 &exception);
4426 } while (exception.retry);
4427 return err;
4428}
4429
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4431{
Trond Myklebust76b32992007-08-10 17:45:11 -04004432 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433 struct nfs4_accessargs args = {
4434 .fh = NFS_FH(inode),
Anna Schumaker1750d922017-07-26 12:00:21 -04004435 .access = entry->mask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436 };
Trond Myklebust76b32992007-08-10 17:45:11 -04004437 struct nfs4_accessres res = {
4438 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04004439 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440 struct rpc_message msg = {
4441 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
4442 .rpc_argp = &args,
4443 .rpc_resp = &res,
4444 .rpc_cred = entry->cred,
4445 };
David Quigleyaa9c2662013-05-22 12:50:44 -04004446 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447
Trond Myklebust7c672652018-06-04 15:00:53 -04004448 if (!nfs4_have_delegation(inode, FMODE_READ)) {
Trond Myklebust8bcbe7d2018-03-20 17:03:11 -04004449 res.fattr = nfs_alloc_fattr();
4450 if (res.fattr == NULL)
4451 return -ENOMEM;
4452 args.bitmask = server->cache_consistency_bitmask;
4453 }
Bryan Schumaker7c513052011-03-24 17:12:24 +00004454 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04004456 nfs_access_set_mask(entry, res.access);
Trond Myklebust8bcbe7d2018-03-20 17:03:11 -04004457 if (res.fattr)
4458 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459 }
Trond Myklebustc407d412010-04-16 16:22:48 -04004460 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461 return status;
4462}
4463
4464static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4465{
Trond Myklebust0688e642019-04-07 13:59:09 -04004466 struct nfs4_exception exception = {
4467 .interruptible = true,
4468 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469 int err;
4470 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004471 err = _nfs4_proc_access(inode, entry);
4472 trace_nfs4_access(inode, err);
4473 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474 &exception);
4475 } while (exception.retry);
4476 return err;
4477}
4478
4479/*
4480 * TODO: For the time being, we don't try to get any attributes
4481 * along with any of the zero-copy operations READ, READDIR,
4482 * READLINK, WRITE.
4483 *
4484 * In the case of the first three, we want to put the GETATTR
4485 * after the read-type operation -- this is because it is hard
4486 * to predict the length of a GETATTR response in v4, and thus
4487 * align the READ data correctly. This means that the GETATTR
4488 * may end up partially falling into the page cache, and we should
4489 * shift it into the 'tail' of the xdr_buf before processing.
4490 * To do this efficiently, we need to know the total length
4491 * of data received, which doesn't seem to be available outside
4492 * of the RPC layer.
4493 *
4494 * In the case of WRITE, we also want to put the GETATTR after
4495 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04004496 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004497 *
4498 * Both of these changes to the XDR layer would in fact be quite
4499 * minor, but I decided to leave them for a subsequent patch.
4500 */
4501static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4502 unsigned int pgbase, unsigned int pglen)
4503{
4504 struct nfs4_readlink args = {
4505 .fh = NFS_FH(inode),
4506 .pgbase = pgbase,
4507 .pglen = pglen,
4508 .pages = &page,
4509 };
Benny Halevyf50c7002009-04-01 09:21:55 -04004510 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511 struct rpc_message msg = {
4512 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4513 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04004514 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515 };
4516
Bryan Schumaker7c513052011-03-24 17:12:24 +00004517 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 -07004518}
4519
4520static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4521 unsigned int pgbase, unsigned int pglen)
4522{
Trond Myklebust0688e642019-04-07 13:59:09 -04004523 struct nfs4_exception exception = {
4524 .interruptible = true,
4525 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526 int err;
4527 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004528 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4529 trace_nfs4_readlink(inode, err);
4530 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004531 &exception);
4532 } while (exception.retry);
4533 return err;
4534}
4535
Linus Torvalds1da177e2005-04-16 15:20:36 -07004536/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004537 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539static int
4540nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004541 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004543 struct nfs_server *server = NFS_SERVER(dir);
David Quigleyaa9c2662013-05-22 12:50:44 -04004544 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004545 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547 int status = 0;
4548
NeilBrown532d4de2016-10-13 15:26:47 +11004549 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004550 if (IS_ERR(ctx))
4551 return PTR_ERR(ctx);
4552
David Quigleyaa9c2662013-05-22 12:50:44 -04004553 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4554
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004555 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4556 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08004557 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558 if (IS_ERR(state)) {
4559 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04004560 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562out:
David Quigleyaa9c2662013-05-22 12:50:44 -04004563 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004564 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565 return status;
4566}
4567
Trond Myklebust3c591172018-07-31 15:54:10 -04004568static int
4569_nfs4_proc_remove(struct inode *dir, const struct qstr *name, u32 ftype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570{
Trond Myklebust16e42952005-10-27 22:12:44 -04004571 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004572 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004573 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07004574 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004575 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004576 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04004577 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04004578 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004580 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4581 .rpc_argp = &args,
4582 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583 };
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004584 unsigned long timestamp = jiffies;
Trond Myklebust778d2812012-04-27 13:48:19 -04004585 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04004586
Bryan Schumaker7c513052011-03-24 17:12:24 +00004587 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust3c591172018-07-31 15:54:10 -04004588 if (status == 0) {
4589 spin_lock(&dir->i_lock);
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004590 nfs4_update_changeattr_locked(dir, &res.cinfo, timestamp,
4591 NFS_INO_INVALID_DATA);
Trond Myklebust3c591172018-07-31 15:54:10 -04004592 /* Removing a directory decrements nlink in the parent */
4593 if (ftype == NF4DIR && dir->i_nlink > 2)
4594 nfs4_dec_nlink_locked(dir);
4595 spin_unlock(&dir->i_lock);
4596 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597 return status;
4598}
4599
Trond Myklebust912678d2018-03-20 16:43:15 -04004600static int nfs4_proc_remove(struct inode *dir, struct dentry *dentry)
4601{
Trond Myklebust0688e642019-04-07 13:59:09 -04004602 struct nfs4_exception exception = {
4603 .interruptible = true,
4604 };
Trond Myklebust912678d2018-03-20 16:43:15 -04004605 struct inode *inode = d_inode(dentry);
4606 int err;
4607
Trond Myklebustc01d3642018-03-20 16:43:20 -04004608 if (inode) {
4609 if (inode->i_nlink == 1)
4610 nfs4_inode_return_delegation(inode);
4611 else
4612 nfs4_inode_make_writeable(inode);
4613 }
Trond Myklebust912678d2018-03-20 16:43:15 -04004614 do {
Trond Myklebust3c591172018-07-31 15:54:10 -04004615 err = _nfs4_proc_remove(dir, &dentry->d_name, NF4REG);
Trond Myklebust912678d2018-03-20 16:43:15 -04004616 trace_nfs4_remove(dir, &dentry->d_name, err);
4617 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4618 &exception);
4619 } while (exception.retry);
4620 return err;
4621}
4622
4623static int nfs4_proc_rmdir(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624{
Trond Myklebust0688e642019-04-07 13:59:09 -04004625 struct nfs4_exception exception = {
4626 .interruptible = true,
4627 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004628 int err;
Trond Myklebust912678d2018-03-20 16:43:15 -04004629
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630 do {
Trond Myklebust3c591172018-07-31 15:54:10 -04004631 err = _nfs4_proc_remove(dir, name, NF4DIR);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004632 trace_nfs4_remove(dir, name, err);
4633 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634 &exception);
4635 } while (exception.retry);
4636 return err;
4637}
4638
Trond Myklebusted7e9ad2018-05-30 16:11:52 -04004639static void nfs4_proc_unlink_setup(struct rpc_message *msg,
4640 struct dentry *dentry,
4641 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004643 struct nfs_removeargs *args = msg->rpc_argp;
4644 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004645
Trond Myklebust977fcc22018-03-20 16:43:17 -04004646 res->server = NFS_SB(dentry->d_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Anna Schumakerfba83f32018-05-04 16:22:50 -04004648 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04004649
4650 nfs_fattr_init(res->dir_attr);
Trond Myklebust977fcc22018-03-20 16:43:17 -04004651
4652 if (inode)
4653 nfs4_inode_return_delegation(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654}
4655
Bryan Schumaker34e137c2012-03-19 14:54:41 -04004656static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4657{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004658 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004659 &data->args.seq_args,
4660 &data->res.seq_res,
4661 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662}
4663
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004664static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004666 struct nfs_unlinkdata *data = task->tk_calldata;
4667 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004668
Trond Myklebust14516c32010-07-31 14:29:06 -04004669 if (!nfs4_sequence_done(task, &res->seq_res))
4670 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004671 if (nfs4_async_handle_error(task, res->server, NULL,
4672 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004673 return 0;
Trond Myklebustc40d52f2017-01-11 12:36:11 -05004674 if (task->tk_status == 0)
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004675 nfs4_update_changeattr(dir, &res->cinfo,
4676 res->dir_attr->time_start,
4677 NFS_INO_INVALID_DATA);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004678 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679}
4680
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004681static void nfs4_proc_rename_setup(struct rpc_message *msg,
4682 struct dentry *old_dentry,
4683 struct dentry *new_dentry)
Jeff Laytond3d41522010-09-17 17:31:57 -04004684{
Jeff Laytond3d41522010-09-17 17:31:57 -04004685 struct nfs_renameargs *arg = msg->rpc_argp;
4686 struct nfs_renameres *res = msg->rpc_resp;
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004687 struct inode *old_inode = d_inode(old_dentry);
4688 struct inode *new_inode = d_inode(new_dentry);
Jeff Laytond3d41522010-09-17 17:31:57 -04004689
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004690 if (old_inode)
Trond Myklebustc01d3642018-03-20 16:43:20 -04004691 nfs4_inode_make_writeable(old_inode);
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004692 if (new_inode)
4693 nfs4_inode_return_delegation(new_inode);
Jeff Laytond3d41522010-09-17 17:31:57 -04004694 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004695 res->server = NFS_SB(old_dentry->d_sb);
Anna Schumakerfba83f32018-05-04 16:22:50 -04004696 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1, 0);
Jeff Laytond3d41522010-09-17 17:31:57 -04004697}
4698
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04004699static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4700{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004701 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004702 &data->args.seq_args,
4703 &data->res.seq_res,
4704 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04004705}
4706
4707static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4708 struct inode *new_dir)
4709{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04004710 struct nfs_renamedata *data = task->tk_calldata;
4711 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04004712
4713 if (!nfs4_sequence_done(task, &res->seq_res))
4714 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004715 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04004716 return 0;
4717
Trond Myklebustc733c492017-01-11 12:32:26 -05004718 if (task->tk_status == 0) {
Trond Myklebust5636ec42018-07-31 15:54:11 -04004719 if (new_dir != old_dir) {
4720 /* Note: If we moved a directory, nlink will change */
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004721 nfs4_update_changeattr(old_dir, &res->old_cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04004722 res->old_fattr->time_start,
Trond Myklebustfabf2b32021-03-25 13:14:42 -04004723 NFS_INO_INVALID_NLINK |
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004724 NFS_INO_INVALID_DATA);
4725 nfs4_update_changeattr(new_dir, &res->new_cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04004726 res->new_fattr->time_start,
Trond Myklebustfabf2b32021-03-25 13:14:42 -04004727 NFS_INO_INVALID_NLINK |
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004728 NFS_INO_INVALID_DATA);
Trond Myklebust5636ec42018-07-31 15:54:11 -04004729 } else
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004730 nfs4_update_changeattr(old_dir, &res->old_cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04004731 res->old_fattr->time_start,
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004732 NFS_INO_INVALID_DATA);
Trond Myklebustc733c492017-01-11 12:32:26 -05004733 }
Jeff Laytond3d41522010-09-17 17:31:57 -04004734 return 1;
4735}
4736
Al Virobeffb8f2016-07-20 16:34:42 -04004737static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004738{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004739 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebust2f28dc32018-04-08 21:06:40 -04004740 __u32 bitmask[NFS4_BITMASK_SZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741 struct nfs4_link_arg arg = {
4742 .fh = NFS_FH(inode),
4743 .dir_fh = NFS_FH(dir),
4744 .name = name,
Trond Myklebust2f28dc32018-04-08 21:06:40 -04004745 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004747 struct nfs4_link_res res = {
4748 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04004749 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004750 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004751 struct rpc_message msg = {
4752 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4753 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004754 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755 };
Trond Myklebust136f2622010-04-16 16:22:49 -04004756 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757
Trond Myklebust136f2622010-04-16 16:22:49 -04004758 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04004759 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04004760 goto out;
4761
David Quigley14c43f72013-05-22 12:50:43 -04004762 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4763 if (IS_ERR(res.label)) {
4764 status = PTR_ERR(res.label);
4765 goto out;
4766 }
4767
Trond Myklebustc01d3642018-03-20 16:43:20 -04004768 nfs4_inode_make_writeable(inode);
Trond Myklebusta71029b2021-04-10 00:23:03 -04004769 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, res.label), inode,
4770 NFS_INO_INVALID_CHANGE);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004771 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004772 if (!status) {
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004773 nfs4_update_changeattr(dir, &res.cinfo, res.fattr->time_start,
4774 NFS_INO_INVALID_DATA);
David Quigleyaa9c2662013-05-22 12:50:44 -04004775 status = nfs_post_op_update_inode(inode, res.fattr);
4776 if (!status)
4777 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004778 }
David Quigley14c43f72013-05-22 12:50:43 -04004779
4780
4781 nfs4_label_free(res.label);
4782
Trond Myklebust136f2622010-04-16 16:22:49 -04004783out:
Trond Myklebust136f2622010-04-16 16:22:49 -04004784 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785 return status;
4786}
4787
Al Virobeffb8f2016-07-20 16:34:42 -04004788static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004789{
Trond Myklebust0688e642019-04-07 13:59:09 -04004790 struct nfs4_exception exception = {
4791 .interruptible = true,
4792 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004793 int err;
4794 do {
4795 err = nfs4_handle_exception(NFS_SERVER(inode),
4796 _nfs4_proc_link(inode, dir, name),
4797 &exception);
4798 } while (exception.retry);
4799 return err;
4800}
4801
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004802struct nfs4_createdata {
4803 struct rpc_message msg;
4804 struct nfs4_create_arg arg;
4805 struct nfs4_create_res res;
4806 struct nfs_fh fh;
4807 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004808 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004809};
4810
4811static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004812 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004813{
4814 struct nfs4_createdata *data;
4815
4816 data = kzalloc(sizeof(*data), GFP_KERNEL);
4817 if (data != NULL) {
4818 struct nfs_server *server = NFS_SERVER(dir);
4819
David Quigley14c43f72013-05-22 12:50:43 -04004820 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4821 if (IS_ERR(data->label))
4822 goto out_free;
4823
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004824 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4825 data->msg.rpc_argp = &data->arg;
4826 data->msg.rpc_resp = &data->res;
4827 data->arg.dir_fh = NFS_FH(dir);
4828 data->arg.server = server;
4829 data->arg.name = name;
4830 data->arg.attrs = sattr;
4831 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04004832 data->arg.bitmask = nfs4_bitmask(server, data->label);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004833 data->arg.umask = current_umask();
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004834 data->res.server = server;
4835 data->res.fh = &data->fh;
4836 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004837 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004838 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004839 }
4840 return data;
David Quigley14c43f72013-05-22 12:50:43 -04004841out_free:
4842 kfree(data);
4843 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004844}
4845
4846static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4847{
Bryan Schumaker7c513052011-03-24 17:12:24 +00004848 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00004849 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004850 if (status == 0) {
Trond Myklebust3c591172018-07-31 15:54:10 -04004851 spin_lock(&dir->i_lock);
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004852 nfs4_update_changeattr_locked(dir, &data->res.dir_cinfo,
4853 data->res.fattr->time_start,
4854 NFS_INO_INVALID_DATA);
Trond Myklebust3c591172018-07-31 15:54:10 -04004855 /* Creating a directory bumps nlink in the parent */
4856 if (data->arg.ftype == NF4DIR)
4857 nfs4_inc_nlink_locked(dir);
4858 spin_unlock(&dir->i_lock);
David Quigley1775fd32013-05-22 12:50:42 -04004859 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004860 }
4861 return status;
4862}
4863
4864static void nfs4_free_createdata(struct nfs4_createdata *data)
4865{
David Quigley14c43f72013-05-22 12:50:43 -04004866 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004867 kfree(data);
4868}
4869
Chuck Lever4f390c12006-08-22 20:06:22 -04004870static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004871 struct page *page, unsigned int len, struct iattr *sattr,
4872 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004874 struct nfs4_createdata *data;
4875 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876
Chuck Lever94a6d752006-08-22 20:06:23 -04004877 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004878 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04004879
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004880 status = -ENOMEM;
4881 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4882 if (data == NULL)
4883 goto out;
4884
4885 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4886 data->arg.u.symlink.pages = &page;
4887 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004888 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004889
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004890 status = nfs4_do_create(dir, dentry, data);
4891
4892 nfs4_free_createdata(data);
4893out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004894 return status;
4895}
4896
Chuck Lever4f390c12006-08-22 20:06:22 -04004897static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004898 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004899{
Trond Myklebust0688e642019-04-07 13:59:09 -04004900 struct nfs4_exception exception = {
4901 .interruptible = true,
4902 };
David Quigleyaa9c2662013-05-22 12:50:44 -04004903 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004904 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004905
4906 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4907
Linus Torvalds1da177e2005-04-16 15:20:36 -07004908 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004909 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4910 trace_nfs4_symlink(dir, &dentry->d_name, err);
4911 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004912 &exception);
4913 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004914
4915 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916 return err;
4917}
4918
4919static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004920 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004922 struct nfs4_createdata *data;
4923 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004925 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4926 if (data == NULL)
4927 goto out;
4928
David Quigley1775fd32013-05-22 12:50:42 -04004929 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004930 status = nfs4_do_create(dir, dentry, data);
4931
4932 nfs4_free_createdata(data);
4933out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004934 return status;
4935}
4936
4937static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4938 struct iattr *sattr)
4939{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004940 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust0688e642019-04-07 13:59:09 -04004941 struct nfs4_exception exception = {
4942 .interruptible = true,
4943 };
David Quigleyaa9c2662013-05-22 12:50:44 -04004944 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004946
David Quigleyaa9c2662013-05-22 12:50:44 -04004947 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4948
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004949 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4950 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004952 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4953 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4954 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955 &exception);
4956 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004957 nfs4_label_release_security(label);
4958
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959 return err;
4960}
4961
Trond Myklebust82e22a52020-11-02 17:34:23 -05004962static int _nfs4_proc_readdir(struct nfs_readdir_arg *nr_arg,
4963 struct nfs_readdir_res *nr_res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004964{
Trond Myklebust82e22a52020-11-02 17:34:23 -05004965 struct inode *dir = d_inode(nr_arg->dentry);
Olga Kornievskaia05ad9172020-11-06 16:03:38 -05004966 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967 struct nfs4_readdir_arg args = {
4968 .fh = NFS_FH(dir),
Trond Myklebust82e22a52020-11-02 17:34:23 -05004969 .pages = nr_arg->pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004970 .pgbase = 0,
Trond Myklebust82e22a52020-11-02 17:34:23 -05004971 .count = nr_arg->page_len,
4972 .plus = nr_arg->plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973 };
4974 struct nfs4_readdir_res res;
4975 struct rpc_message msg = {
4976 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4977 .rpc_argp = &args,
4978 .rpc_resp = &res,
Trond Myklebust82e22a52020-11-02 17:34:23 -05004979 .rpc_cred = nr_arg->cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980 };
4981 int status;
4982
Trond Myklebust82e22a52020-11-02 17:34:23 -05004983 dprintk("%s: dentry = %pd2, cookie = %llu\n", __func__,
4984 nr_arg->dentry, (unsigned long long)nr_arg->cookie);
Olga Kornievskaia05ad9172020-11-06 16:03:38 -05004985 if (!(server->caps & NFS_CAP_SECURITY_LABEL))
4986 args.bitmask = server->attr_bitmask_nl;
4987 else
4988 args.bitmask = server->attr_bitmask;
4989
Trond Myklebust82e22a52020-11-02 17:34:23 -05004990 nfs4_setup_readdir(nr_arg->cookie, nr_arg->verf, nr_arg->dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991 res.pgbase = args.pgbase;
Olga Kornievskaia05ad9172020-11-06 16:03:38 -05004992 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
4993 &res.seq_res, 0);
Trond Myklebustac396122010-11-15 20:26:22 -05004994 if (status >= 0) {
Trond Myklebust82e22a52020-11-02 17:34:23 -05004995 memcpy(nr_res->verf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05004996 status += args.pgbase;
4997 }
Trond Myklebustc4812992007-09-28 17:11:45 -04004998
4999 nfs_invalidate_atime(dir);
5000
Harvey Harrison3110ff82008-05-02 13:42:44 -07005001 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005002 return status;
5003}
5004
Trond Myklebust82e22a52020-11-02 17:34:23 -05005005static int nfs4_proc_readdir(struct nfs_readdir_arg *arg,
5006 struct nfs_readdir_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005007{
Trond Myklebust0688e642019-04-07 13:59:09 -04005008 struct nfs4_exception exception = {
5009 .interruptible = true,
5010 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005011 int err;
5012 do {
Trond Myklebust82e22a52020-11-02 17:34:23 -05005013 err = _nfs4_proc_readdir(arg, res);
5014 trace_nfs4_readdir(d_inode(arg->dentry), err);
5015 err = nfs4_handle_exception(NFS_SERVER(d_inode(arg->dentry)),
5016 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005017 } while (exception.retry);
5018 return err;
5019}
5020
5021static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04005022 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04005024 struct nfs4_createdata *data;
5025 int mode = sattr->ia_mode;
5026 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005027
Trond Myklebust57dc9a52008-06-20 15:35:32 -04005028 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
5029 if (data == NULL)
5030 goto out;
5031
Linus Torvalds1da177e2005-04-16 15:20:36 -07005032 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04005033 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005034 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04005035 data->arg.ftype = NF4BLK;
5036 data->arg.u.device.specdata1 = MAJOR(rdev);
5037 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005038 }
5039 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04005040 data->arg.ftype = NF4CHR;
5041 data->arg.u.device.specdata1 = MAJOR(rdev);
5042 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04005043 } else if (!S_ISSOCK(mode)) {
5044 status = -EINVAL;
5045 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005046 }
David Quigley1775fd32013-05-22 12:50:42 -04005047
David Quigleyaa9c2662013-05-22 12:50:44 -04005048 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04005049 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04005050out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04005051 nfs4_free_createdata(data);
5052out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005053 return status;
5054}
5055
5056static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
5057 struct iattr *sattr, dev_t rdev)
5058{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05005059 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust0688e642019-04-07 13:59:09 -04005060 struct nfs4_exception exception = {
5061 .interruptible = true,
5062 };
David Quigleyaa9c2662013-05-22 12:50:44 -04005063 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005064 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00005065
David Quigleyaa9c2662013-05-22 12:50:44 -04005066 label = nfs4_label_init_security(dir, dentry, sattr, &l);
5067
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05005068 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
5069 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005070 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04005071 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
5072 trace_nfs4_mknod(dir, &dentry->d_name, err);
5073 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005074 &exception);
5075 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04005076
5077 nfs4_label_release_security(label);
5078
Linus Torvalds1da177e2005-04-16 15:20:36 -07005079 return err;
5080}
5081
5082static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
5083 struct nfs_fsstat *fsstat)
5084{
5085 struct nfs4_statfs_arg args = {
5086 .fh = fhandle,
5087 .bitmask = server->attr_bitmask,
5088 };
Benny Halevy24ad1482009-04-01 09:21:56 -04005089 struct nfs4_statfs_res res = {
5090 .fsstat = fsstat,
5091 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005092 struct rpc_message msg = {
5093 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
5094 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04005095 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005096 };
5097
Trond Myklebust0e574af2005-10-27 22:12:38 -04005098 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005099 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005100}
5101
5102static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
5103{
Trond Myklebust0688e642019-04-07 13:59:09 -04005104 struct nfs4_exception exception = {
5105 .interruptible = true,
5106 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005107 int err;
5108 do {
5109 err = nfs4_handle_exception(server,
5110 _nfs4_proc_statfs(server, fhandle, fsstat),
5111 &exception);
5112 } while (exception.retry);
5113 return err;
5114}
5115
5116static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
5117 struct nfs_fsinfo *fsinfo)
5118{
5119 struct nfs4_fsinfo_arg args = {
5120 .fh = fhandle,
5121 .bitmask = server->attr_bitmask,
5122 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04005123 struct nfs4_fsinfo_res res = {
5124 .fsinfo = fsinfo,
5125 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005126 struct rpc_message msg = {
5127 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
5128 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04005129 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005130 };
5131
Bryan Schumaker7c513052011-03-24 17:12:24 +00005132 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005133}
5134
5135static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5136{
Trond Myklebust0688e642019-04-07 13:59:09 -04005137 struct nfs4_exception exception = {
5138 .interruptible = true,
5139 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005140 int err;
5141
5142 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04005143 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04005144 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04005145 if (err == 0) {
Robert Milkowski7dc29932020-01-30 09:43:25 +00005146 nfs4_set_lease_period(server->nfs_client, fsinfo->lease_time * HZ);
Chuck Lever83ca7f52013-03-16 15:55:53 -04005147 break;
5148 }
5149 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005150 } while (exception.retry);
5151 return err;
5152}
5153
5154static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5155{
Bryan Schumakere38eb652012-06-20 15:53:40 -04005156 int error;
5157
Trond Myklebust0e574af2005-10-27 22:12:38 -04005158 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04005159 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08005160 if (error == 0) {
5161 /* block layout checks this! */
5162 server->pnfs_blksize = fsinfo->blksize;
Jeff Laytonca440c32016-09-15 14:40:49 -04005163 set_pnfs_layoutdriver(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08005164 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04005165
5166 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005167}
5168
5169static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5170 struct nfs_pathconf *pathconf)
5171{
5172 struct nfs4_pathconf_arg args = {
5173 .fh = fhandle,
5174 .bitmask = server->attr_bitmask,
5175 };
Benny Halevyd45b2982009-04-01 09:21:58 -04005176 struct nfs4_pathconf_res res = {
5177 .pathconf = pathconf,
5178 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005179 struct rpc_message msg = {
5180 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
5181 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04005182 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005183 };
5184
5185 /* None of the pathconf attributes are mandatory to implement */
5186 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
5187 memset(pathconf, 0, sizeof(*pathconf));
5188 return 0;
5189 }
5190
Trond Myklebust0e574af2005-10-27 22:12:38 -04005191 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005192 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005193}
5194
5195static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5196 struct nfs_pathconf *pathconf)
5197{
Trond Myklebust0688e642019-04-07 13:59:09 -04005198 struct nfs4_exception exception = {
5199 .interruptible = true,
5200 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005201 int err;
5202
5203 do {
5204 err = nfs4_handle_exception(server,
5205 _nfs4_proc_pathconf(server, fhandle, pathconf),
5206 &exception);
5207 } while (exception.retry);
5208 return err;
5209}
5210
Trond Myklebust5521abf2013-03-16 20:54:34 -04005211int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04005212 const struct nfs_open_context *ctx,
5213 const struct nfs_lock_context *l_ctx,
5214 fmode_t fmode)
5215{
NeilBrown17393472016-10-13 15:26:47 +11005216 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04005217}
5218EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
5219
Trond Myklebust5521abf2013-03-16 20:54:34 -04005220static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
5221 const struct nfs_open_context *ctx,
5222 const struct nfs_lock_context *l_ctx,
5223 fmode_t fmode)
5224{
Ben Dooksd49dd112019-10-16 17:28:21 +01005225 nfs4_stateid _current_stateid;
Trond Myklebust5521abf2013-03-16 20:54:34 -04005226
Trond Myklebuste1253be2014-03-05 08:44:23 -05005227 /* If the current stateid represents a lost lock, then exit */
Ben Dooksd49dd112019-10-16 17:28:21 +01005228 if (nfs4_set_rw_stateid(&_current_stateid, ctx, l_ctx, fmode) == -EIO)
Trond Myklebuste1253be2014-03-05 08:44:23 -05005229 return true;
Ben Dooksd49dd112019-10-16 17:28:21 +01005230 return nfs4_stateid_match(stateid, &_current_stateid);
Trond Myklebust5521abf2013-03-16 20:54:34 -04005231}
5232
5233static bool nfs4_error_stateid_expired(int err)
5234{
5235 switch (err) {
5236 case -NFS4ERR_DELEG_REVOKED:
5237 case -NFS4ERR_ADMIN_REVOKED:
5238 case -NFS4ERR_BAD_STATEID:
5239 case -NFS4ERR_STALE_STATEID:
5240 case -NFS4ERR_OLD_STATEID:
5241 case -NFS4ERR_OPENMODE:
5242 case -NFS4ERR_EXPIRED:
5243 return true;
5244 }
5245 return false;
5246}
5247
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005248static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005249{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005250 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005251
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005252 trace_nfs4_read(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04005253 if (task->tk_status < 0) {
5254 struct nfs4_exception exception = {
5255 .inode = hdr->inode,
5256 .state = hdr->args.context->state,
5257 .stateid = &hdr->args.stateid,
5258 };
5259 task->tk_status = nfs4_async_handle_exception(task,
5260 server, task->tk_status, &exception);
5261 if (exception.retry) {
5262 rpc_restart_call_prepare(task);
5263 return -EAGAIN;
5264 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005265 }
Trond Myklebust8850df92007-09-28 17:20:07 -04005266
Linus Torvalds1da177e2005-04-16 15:20:36 -07005267 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005268 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05005269 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005270}
5271
Trond Myklebust5521abf2013-03-16 20:54:34 -04005272static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04005273 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04005274{
5275
5276 if (!nfs4_error_stateid_expired(task->tk_status) ||
5277 nfs4_stateid_is_current(&args->stateid,
5278 args->context,
5279 args->lock_context,
5280 FMODE_READ))
5281 return false;
5282 rpc_restart_call_prepare(task);
5283 return true;
5284}
5285
Anna Schumakerc5675522014-05-28 13:41:22 -04005286static bool nfs4_read_plus_not_supported(struct rpc_task *task,
5287 struct nfs_pgio_header *hdr)
5288{
5289 struct nfs_server *server = NFS_SERVER(hdr->inode);
5290 struct rpc_message *msg = &task->tk_msg;
5291
5292 if (msg->rpc_proc == &nfs4_procedures[NFSPROC4_CLNT_READ_PLUS] &&
5293 server->caps & NFS_CAP_READ_PLUS && task->tk_status == -ENOTSUPP) {
5294 server->caps &= ~NFS_CAP_READ_PLUS;
5295 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
5296 rpc_restart_call_prepare(task);
5297 return true;
5298 }
5299 return false;
5300}
5301
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005302static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc72011-03-01 01:34:20 +00005303{
Andy Adamsoncbdabc72011-03-01 01:34:20 +00005304 dprintk("--> %s\n", __func__);
5305
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005306 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc72011-03-01 01:34:20 +00005307 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005308 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04005309 return -EAGAIN;
Anna Schumakerc5675522014-05-28 13:41:22 -04005310 if (nfs4_read_plus_not_supported(task, hdr))
5311 return -EAGAIN;
Trond Myklebustbfc505d2016-09-15 18:26:05 -04005312 if (task->tk_status > 0)
5313 nfs_invalidate_atime(hdr->inode);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005314 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5315 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00005316}
5317
Anna Schumaker21e31402020-12-03 15:18:39 -05005318#if defined CONFIG_NFS_V4_2 && defined CONFIG_NFS_V4_2_READ_PLUS
Trond Myklebust5c3485b2020-12-10 09:34:34 -05005319static void nfs42_read_plus_support(struct nfs_pgio_header *hdr,
5320 struct rpc_message *msg)
Anna Schumakerc5675522014-05-28 13:41:22 -04005321{
Trond Myklebust5c3485b2020-12-10 09:34:34 -05005322 /* Note: We don't use READ_PLUS with pNFS yet */
5323 if (nfs_server_capable(hdr->inode, NFS_CAP_READ_PLUS) && !hdr->ds_clp)
Anna Schumakerc5675522014-05-28 13:41:22 -04005324 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ_PLUS];
Anna Schumakerc5675522014-05-28 13:41:22 -04005325}
5326#else
Trond Myklebust5c3485b2020-12-10 09:34:34 -05005327static void nfs42_read_plus_support(struct nfs_pgio_header *hdr,
5328 struct rpc_message *msg)
Anna Schumakerc5675522014-05-28 13:41:22 -04005329{
Anna Schumakerc5675522014-05-28 13:41:22 -04005330}
5331#endif /* CONFIG_NFS_V4_2 */
5332
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005333static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
5334 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005335{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005336 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04005337 if (!hdr->pgio_done_cb)
5338 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebust5c3485b2020-12-10 09:34:34 -05005339 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
5340 nfs42_read_plus_support(hdr, msg);
Anna Schumakerfba83f32018-05-04 16:22:50 -04005341 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005342}
5343
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005344static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
5345 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04005346{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005347 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005348 &hdr->args.seq_args,
5349 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04005350 task))
NeilBrownef1820f2013-09-04 17:04:49 +10005351 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005352 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
5353 hdr->args.lock_context,
Benjamin Coddingtonfbe77c32017-04-19 10:11:35 -04005354 hdr->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10005355 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005356 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10005357 return -EIO;
5358 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005359}
5360
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005361static int nfs4_write_done_cb(struct rpc_task *task,
5362 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005363{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005364 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10005365
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005366 trace_nfs4_write(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04005367 if (task->tk_status < 0) {
5368 struct nfs4_exception exception = {
5369 .inode = hdr->inode,
5370 .state = hdr->args.context->state,
5371 .stateid = &hdr->args.stateid,
5372 };
5373 task->tk_status = nfs4_async_handle_exception(task,
5374 NFS_SERVER(inode), task->tk_status,
5375 &exception);
5376 if (exception.retry) {
5377 rpc_restart_call_prepare(task);
5378 return -EAGAIN;
5379 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005380 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04005381 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005382 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05005383 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04005384 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05005385 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005386}
5387
Trond Myklebust5521abf2013-03-16 20:54:34 -04005388static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04005389 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04005390{
5391
5392 if (!nfs4_error_stateid_expired(task->tk_status) ||
5393 nfs4_stateid_is_current(&args->stateid,
5394 args->context,
5395 args->lock_context,
5396 FMODE_WRITE))
5397 return false;
5398 rpc_restart_call_prepare(task);
5399 return true;
5400}
5401
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005402static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00005403{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005404 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00005405 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005406 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04005407 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005408 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5409 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00005410}
5411
Trond Myklebust5a37f852012-04-28 14:55:16 -04005412static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005413bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00005414{
Trond Myklebust5a37f852012-04-28 14:55:16 -04005415 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005416 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04005417 return false;
5418 /* Otherwise, request attributes if and only if we don't hold
5419 * a delegation
5420 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04005421 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00005422}
Fred Isamana69aef12011-03-03 15:13:47 +00005423
Trond Myklebust332d1a02021-03-25 18:15:36 -04005424static void nfs4_bitmask_set(__u32 bitmask[NFS4_BITMASK_SZ], const __u32 *src,
5425 struct inode *inode, struct nfs_server *server,
5426 struct nfs4_label *label)
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005427{
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005428 unsigned long cache_validity = READ_ONCE(NFS_I(inode)->cache_validity);
Trond Myklebust332d1a02021-03-25 18:15:36 -04005429 unsigned int i;
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005430
Trond Myklebust332d1a02021-03-25 18:15:36 -04005431 memcpy(bitmask, src, sizeof(*bitmask) * NFS4_BITMASK_SZ);
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005432
Trond Myklebust13c0b082021-03-25 21:07:21 -04005433 if (cache_validity & NFS_INO_INVALID_CHANGE)
Trond Myklebust332d1a02021-03-25 18:15:36 -04005434 bitmask[0] |= FATTR4_WORD0_CHANGE;
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005435 if (cache_validity & NFS_INO_INVALID_ATIME)
5436 bitmask[1] |= FATTR4_WORD1_TIME_ACCESS;
Trond Myklebust720869e2021-04-13 09:41:16 -04005437 if (cache_validity & NFS_INO_INVALID_MODE)
5438 bitmask[1] |= FATTR4_WORD1_MODE;
Trond Myklebust45901a22021-02-08 08:49:32 -05005439 if (cache_validity & NFS_INO_INVALID_OTHER)
Trond Myklebust720869e2021-04-13 09:41:16 -04005440 bitmask[1] |= FATTR4_WORD1_OWNER | FATTR4_WORD1_OWNER_GROUP;
Trond Myklebustfabf2b32021-03-25 13:14:42 -04005441 if (cache_validity & NFS_INO_INVALID_NLINK)
5442 bitmask[1] |= FATTR4_WORD1_NUMLINKS;
Trond Myklebust45901a22021-02-08 08:49:32 -05005443 if (label && label->len && cache_validity & NFS_INO_INVALID_LABEL)
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005444 bitmask[2] |= FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust45901a22021-02-08 08:49:32 -05005445 if (cache_validity & NFS_INO_INVALID_CTIME)
5446 bitmask[1] |= FATTR4_WORD1_TIME_METADATA;
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005447 if (cache_validity & NFS_INO_INVALID_MTIME)
5448 bitmask[1] |= FATTR4_WORD1_TIME_MODIFY;
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005449 if (cache_validity & NFS_INO_INVALID_BLOCKS)
5450 bitmask[1] |= FATTR4_WORD1_SPACE_USED;
Trond Myklebust332d1a02021-03-25 18:15:36 -04005451
5452 if (nfs4_have_delegation(inode, FMODE_READ) &&
5453 !(cache_validity & NFS_INO_REVAL_FORCED))
5454 bitmask[0] &= ~FATTR4_WORD0_SIZE;
Trond Myklebust13c0b082021-03-25 21:07:21 -04005455 else if (cache_validity & NFS_INO_INVALID_SIZE)
Trond Myklebust332d1a02021-03-25 18:15:36 -04005456 bitmask[0] |= FATTR4_WORD0_SIZE;
5457
5458 for (i = 0; i < NFS4_BITMASK_SZ; i++)
5459 bitmask[i] &= server->attr_bitmask[i];
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005460}
5461
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005462static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
Anna Schumakerfb91fb02018-05-04 16:22:48 -04005463 struct rpc_message *msg,
5464 struct rpc_clnt **clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005465{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005466 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005467
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005468 if (!nfs4_write_need_cache_consistency_data(hdr)) {
5469 hdr->args.bitmask = NULL;
5470 hdr->res.fattr = NULL;
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005471 } else {
Trond Myklebust332d1a02021-03-25 18:15:36 -04005472 nfs4_bitmask_set(hdr->args.bitmask_store,
5473 server->cache_consistency_bitmask,
5474 hdr->inode, server, NULL);
5475 hdr->args.bitmask = hdr->args.bitmask_store;
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005476 }
Trond Myklebust5a37f852012-04-28 14:55:16 -04005477
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005478 if (!hdr->pgio_done_cb)
5479 hdr->pgio_done_cb = nfs4_write_done_cb;
5480 hdr->res.server = server;
5481 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005482
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005483 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Olga Kornievskaiacd1b6592020-02-12 17:32:12 -05005484 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
Anna Schumakerfb91fb02018-05-04 16:22:48 -04005485 nfs4_state_protect_write(server->nfs_client, clnt, msg, hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005486}
5487
Fred Isaman0b7c0152012-04-20 14:47:39 -04005488static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
5489{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005490 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005491 &data->args.seq_args,
5492 &data->res.seq_res,
5493 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005494}
5495
Fred Isaman0b7c0152012-04-20 14:47:39 -04005496static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005497{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005498 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04005499
Trond Myklebustcc668ab2013-08-14 15:31:28 -04005500 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10005501 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
5502 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005503 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05005504 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005505 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05005506 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005507}
5508
Fred Isaman0b7c0152012-04-20 14:47:39 -04005509static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00005510{
5511 if (!nfs4_sequence_done(task, &data->res.seq_res))
5512 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04005513 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00005514}
5515
Anna Schumakere9ae1ee2018-05-04 16:22:49 -04005516static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg,
5517 struct rpc_clnt **clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005518{
Trond Myklebust788e7a82006-03-20 13:44:27 -05005519 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00005520
Fred Isaman0b7c0152012-04-20 14:47:39 -04005521 if (data->commit_done_cb == NULL)
5522 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04005523 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005524 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Anna Schumakerfba83f32018-05-04 16:22:50 -04005525 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Anna Schumakere9ae1ee2018-05-04 16:22:49 -04005526 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_COMMIT, clnt, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005527}
5528
Olga Kornievskaia6b8d84e2018-07-09 15:13:36 -04005529static int _nfs4_proc_commit(struct file *dst, struct nfs_commitargs *args,
5530 struct nfs_commitres *res)
5531{
5532 struct inode *dst_inode = file_inode(dst);
5533 struct nfs_server *server = NFS_SERVER(dst_inode);
5534 struct rpc_message msg = {
5535 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
5536 .rpc_argp = args,
5537 .rpc_resp = res,
5538 };
5539
5540 args->fh = NFS_FH(dst_inode);
5541 return nfs4_call_sync(server->client, server, &msg,
5542 &args->seq_args, &res->seq_res, 1);
5543}
5544
5545int nfs4_proc_commit(struct file *dst, __u64 offset, __u32 count, struct nfs_commitres *res)
5546{
5547 struct nfs_commitargs args = {
5548 .offset = offset,
5549 .count = count,
5550 };
5551 struct nfs_server *dst_server = NFS_SERVER(file_inode(dst));
5552 struct nfs4_exception exception = { };
5553 int status;
5554
5555 do {
5556 status = _nfs4_proc_commit(dst, &args, res);
5557 status = nfs4_handle_exception(dst_server, status, &exception);
5558 } while (exception.retry);
5559
5560 return status;
5561}
5562
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005563struct nfs4_renewdata {
5564 struct nfs_client *client;
5565 unsigned long timestamp;
5566};
5567
Linus Torvalds1da177e2005-04-16 15:20:36 -07005568/*
5569 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
5570 * standalone procedure for queueing an asynchronous RENEW.
5571 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005572static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005573{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005574 struct nfs4_renewdata *data = calldata;
5575 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005576
Elena Reshetova212bf412017-10-20 12:53:38 +03005577 if (refcount_read(&clp->cl_count) > 1)
Alexandros Batsakis0851de062010-02-05 03:45:06 -08005578 nfs4_schedule_state_renewal(clp);
5579 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005580 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005581}
5582
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005583static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005584{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005585 struct nfs4_renewdata *data = calldata;
5586 struct nfs_client *clp = data->client;
5587 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005588
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005589 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04005590 switch (task->tk_status) {
5591 case 0:
5592 break;
5593 case -NFS4ERR_LEASE_MOVED:
5594 nfs4_schedule_lease_moved_recovery(clp);
5595 break;
5596 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04005597 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04005598 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
5599 return;
5600 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005601 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04005602 return;
5603 }
5604 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005605 }
Trond Myklebust452e9352010-07-31 14:29:06 -04005606 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005607}
5608
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005609static const struct rpc_call_ops nfs4_renew_ops = {
5610 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005611 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005612};
5613
NeilBrowna52458b2018-12-03 11:30:31 +11005614static int nfs4_proc_async_renew(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005615{
5616 struct rpc_message msg = {
5617 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5618 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01005619 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005620 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005621 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005622
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005623 if (renew_flags == 0)
5624 return 0;
Elena Reshetova212bf412017-10-20 12:53:38 +03005625 if (!refcount_inc_not_zero(&clp->cl_count))
Alexandros Batsakis0851de062010-02-05 03:45:06 -08005626 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04005627 data = kmalloc(sizeof(*data), GFP_NOFS);
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04005628 if (data == NULL) {
5629 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005630 return -ENOMEM;
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04005631 }
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005632 data->client = clp;
5633 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005634 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005635 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005636}
5637
NeilBrowna52458b2018-12-03 11:30:31 +11005638static int nfs4_proc_renew(struct nfs_client *clp, const struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005639{
5640 struct rpc_message msg = {
5641 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5642 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01005643 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005644 };
5645 unsigned long now = jiffies;
5646 int status;
5647
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005648 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005649 if (status < 0)
5650 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04005651 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005652 return 0;
5653}
5654
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005655static inline int nfs4_server_supports_acls(struct nfs_server *server)
5656{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06005657 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005658}
5659
Trond Myklebust21f498c2012-08-24 10:59:25 -04005660/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
5661 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005662 * the stack.
5663 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04005664#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005665
Frank van der Lindenccde1e92020-06-23 22:39:01 +00005666int nfs4_buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005667 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05005668{
5669 struct page *newpage, **spages;
5670 int rc = 0;
5671 size_t len;
5672 spages = pages;
5673
5674 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04005675 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05005676 newpage = alloc_page(GFP_KERNEL);
5677
5678 if (newpage == NULL)
5679 goto unwind;
5680 memcpy(page_address(newpage), buf, len);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005681 buf += len;
5682 buflen -= len;
Neil Hormane9e3d722011-03-04 19:26:03 -05005683 *pages++ = newpage;
5684 rc++;
5685 } while (buflen != 0);
5686
5687 return rc;
5688
5689unwind:
5690 for(; rc > 0; rc--)
5691 __free_page(spages[rc-1]);
5692 return -ENOMEM;
5693}
5694
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005695struct nfs4_cached_acl {
5696 int cached;
5697 size_t len;
Gustavo A. R. Silva5601cda2020-03-09 13:24:42 -05005698 char data[];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005699};
5700
5701static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005702{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005703 struct nfs_inode *nfsi = NFS_I(inode);
5704
5705 spin_lock(&inode->i_lock);
5706 kfree(nfsi->nfs4_acl);
5707 nfsi->nfs4_acl = acl;
5708 spin_unlock(&inode->i_lock);
5709}
5710
5711static void nfs4_zap_acl_attr(struct inode *inode)
5712{
5713 nfs4_set_cached_acl(inode, NULL);
5714}
5715
5716static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
5717{
5718 struct nfs_inode *nfsi = NFS_I(inode);
5719 struct nfs4_cached_acl *acl;
5720 int ret = -ENOENT;
5721
5722 spin_lock(&inode->i_lock);
5723 acl = nfsi->nfs4_acl;
5724 if (acl == NULL)
5725 goto out;
5726 if (buf == NULL) /* user is just asking for length */
5727 goto out_len;
5728 if (acl->cached == 0)
5729 goto out;
5730 ret = -ERANGE; /* see getxattr(2) man page */
5731 if (acl->len > buflen)
5732 goto out;
5733 memcpy(buf, acl->data, acl->len);
5734out_len:
5735 ret = acl->len;
5736out:
5737 spin_unlock(&inode->i_lock);
5738 return ret;
5739}
5740
Sachin Prabhu5794d212012-04-17 14:36:40 +01005741static 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 +00005742{
5743 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005744 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005745
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005746 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005747 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005748 if (acl == NULL)
5749 goto out;
5750 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01005751 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005752 } else {
5753 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5754 if (acl == NULL)
5755 goto out;
5756 acl->cached = 0;
5757 }
5758 acl->len = acl_len;
5759out:
5760 nfs4_set_cached_acl(inode, acl);
5761}
5762
Andy Adamsonbf118a32011-12-07 11:55:27 -05005763/*
5764 * The getxattr API returns the required buffer length when called with a
5765 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5766 * the required buf. On a NULL buf, we send a page of data to the server
5767 * guessing that the ACL request can be serviced by a page. If so, we cache
5768 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5769 * the cache. If not so, we throw away the page, and cache the required
5770 * length. The next getxattr call will then produce another round trip to
5771 * the server, this time with the input buf of the required size.
5772 */
Trond Myklebust16b42892006-08-24 12:27:15 -04005773static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005774{
Olga Kornievskaia62a15732020-01-02 17:09:54 -05005775 struct page **pages;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005776 struct nfs_getaclargs args = {
5777 .fh = NFS_FH(inode),
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005778 .acl_len = buflen,
5779 };
Benny Halevy663c79b2009-04-01 09:21:59 -04005780 struct nfs_getaclres res = {
5781 .acl_len = buflen,
5782 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005783 struct rpc_message msg = {
5784 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5785 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04005786 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005787 };
Olga Kornievskaia62a15732020-01-02 17:09:54 -05005788 unsigned int npages;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005789 int ret = -ENOMEM, i;
Olga Kornievskaia62a15732020-01-02 17:09:54 -05005790 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005791
Olga Kornievskaia62a15732020-01-02 17:09:54 -05005792 if (buflen == 0)
5793 buflen = server->rsize;
5794
5795 npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
5796 pages = kmalloc_array(npages, sizeof(struct page *), GFP_NOFS);
5797 if (!pages)
5798 return -ENOMEM;
5799
5800 args.acl_pages = pages;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005801
Andy Adamsonbf118a32011-12-07 11:55:27 -05005802 for (i = 0; i < npages; i++) {
5803 pages[i] = alloc_page(GFP_KERNEL);
5804 if (!pages[i])
5805 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005806 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01005807
5808 /* for decoding across pages */
5809 res.acl_scratch = alloc_page(GFP_KERNEL);
5810 if (!res.acl_scratch)
5811 goto out_free;
5812
Andy Adamsonbf118a32011-12-07 11:55:27 -05005813 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005814
Peng Taode040be2012-01-10 22:42:47 +08005815 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05005816 __func__, buf, buflen, npages, args.acl_len);
5817 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5818 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005819 if (ret)
5820 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05005821
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005822 /* Handle the case where the passed-in buffer is too short */
5823 if (res.acl_flags & NFS4_ACL_TRUNC) {
5824 /* Did the user only issue a request for the acl length? */
5825 if (buf == NULL)
5826 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005827 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005828 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005829 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005830 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005831 if (buf) {
5832 if (res.acl_len > buflen) {
5833 ret = -ERANGE;
5834 goto out_free;
5835 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005836 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005837 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005838out_ok:
5839 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005840out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05005841 for (i = 0; i < npages; i++)
5842 if (pages[i])
5843 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05005844 if (res.acl_scratch)
5845 __free_page(res.acl_scratch);
Olga Kornievskaia62a15732020-01-02 17:09:54 -05005846 kfree(pages);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005847 return ret;
5848}
5849
Trond Myklebust16b42892006-08-24 12:27:15 -04005850static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5851{
Trond Myklebust0688e642019-04-07 13:59:09 -04005852 struct nfs4_exception exception = {
5853 .interruptible = true,
5854 };
Trond Myklebust16b42892006-08-24 12:27:15 -04005855 ssize_t ret;
5856 do {
5857 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04005858 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b42892006-08-24 12:27:15 -04005859 if (ret >= 0)
5860 break;
5861 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5862 } while (exception.retry);
5863 return ret;
5864}
5865
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005866static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5867{
5868 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005869 int ret;
5870
5871 if (!nfs4_server_supports_acls(server))
5872 return -EOPNOTSUPP;
Trond Myklebust1f3208b2021-03-25 11:04:34 -04005873 ret = nfs_revalidate_inode(inode, NFS_INO_INVALID_CHANGE);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005874 if (ret < 0)
5875 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005876 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5877 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005878 ret = nfs4_read_cached_acl(inode, buf, buflen);
5879 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05005880 /* -ENOENT is returned if there is no ACL or if there is an ACL
5881 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005882 return ret;
5883 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005884}
5885
Trond Myklebust16b42892006-08-24 12:27:15 -04005886static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005887{
5888 struct nfs_server *server = NFS_SERVER(inode);
5889 struct page *pages[NFS4ACL_MAXPAGES];
5890 struct nfs_setaclargs arg = {
5891 .fh = NFS_FH(inode),
5892 .acl_pages = pages,
5893 .acl_len = buflen,
5894 };
Benny Halevy73c403a2009-04-01 09:22:01 -04005895 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005896 struct rpc_message msg = {
5897 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5898 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04005899 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005900 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005901 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05005902 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005903
J. Bruce Fields4f8be1f2021-01-28 17:36:38 -05005904 /* You can't remove system.nfs4_acl: */
5905 if (buflen == 0)
5906 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005907 if (!nfs4_server_supports_acls(server))
5908 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005909 if (npages > ARRAY_SIZE(pages))
5910 return -ERANGE;
Frank van der Lindenccde1e92020-06-23 22:39:01 +00005911 i = nfs4_buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05005912 if (i < 0)
5913 return i;
Trond Myklebustc01d3642018-03-20 16:43:20 -04005914 nfs4_inode_make_writeable(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005915 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05005916
5917 /*
5918 * Free each page after tx, so the only ref left is
5919 * held by the network stack
5920 */
5921 for (; i > 0; i--)
5922 put_page(pages[i-1]);
5923
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005924 /*
5925 * Acl update can result in inode attribute update.
5926 * so mark the attribute cache invalid.
5927 */
5928 spin_lock(&inode->i_lock);
Trond Myklebustb6f80a22021-03-08 14:42:55 -05005929 nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE |
5930 NFS_INO_INVALID_CTIME |
5931 NFS_INO_REVAL_FORCED);
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005932 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04005933 nfs_access_zap_cache(inode);
5934 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005935 return ret;
5936}
5937
Trond Myklebust16b42892006-08-24 12:27:15 -04005938static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5939{
5940 struct nfs4_exception exception = { };
5941 int err;
5942 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005943 err = __nfs4_proc_set_acl(inode, buf, buflen);
5944 trace_nfs4_set_acl(inode, err);
5945 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b42892006-08-24 12:27:15 -04005946 &exception);
5947 } while (exception.retry);
5948 return err;
5949}
5950
David Quigleyaa9c2662013-05-22 12:50:44 -04005951#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5952static int _nfs4_get_security_label(struct inode *inode, void *buf,
5953 size_t buflen)
5954{
5955 struct nfs_server *server = NFS_SERVER(inode);
5956 struct nfs_fattr fattr;
5957 struct nfs4_label label = {0, 0, buflen, buf};
5958
5959 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005960 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005961 .fh = NFS_FH(inode),
5962 .bitmask = bitmask,
5963 };
5964 struct nfs4_getattr_res res = {
5965 .fattr = &fattr,
5966 .label = &label,
5967 .server = server,
5968 };
5969 struct rpc_message msg = {
5970 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005971 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005972 .rpc_resp = &res,
5973 };
5974 int ret;
5975
5976 nfs_fattr_init(&fattr);
5977
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005978 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04005979 if (ret)
5980 return ret;
5981 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5982 return -ENOENT;
Ondrej Mosnacek53cb2452021-01-15 18:43:56 +01005983 return label.len;
David Quigleyaa9c2662013-05-22 12:50:44 -04005984}
5985
5986static int nfs4_get_security_label(struct inode *inode, void *buf,
5987 size_t buflen)
5988{
Trond Myklebust0688e642019-04-07 13:59:09 -04005989 struct nfs4_exception exception = {
5990 .interruptible = true,
5991 };
David Quigleyaa9c2662013-05-22 12:50:44 -04005992 int err;
5993
5994 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5995 return -EOPNOTSUPP;
5996
5997 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005998 err = _nfs4_get_security_label(inode, buf, buflen);
5999 trace_nfs4_get_security_label(inode, err);
6000 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04006001 &exception);
6002 } while (exception.retry);
6003 return err;
6004}
6005
6006static int _nfs4_do_set_security_label(struct inode *inode,
6007 struct nfs4_label *ilabel,
6008 struct nfs_fattr *fattr,
6009 struct nfs4_label *olabel)
6010{
6011
6012 struct iattr sattr = {0};
6013 struct nfs_server *server = NFS_SERVER(inode);
6014 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04006015 struct nfs_setattrargs arg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05006016 .fh = NFS_FH(inode),
6017 .iap = &sattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04006018 .server = server,
6019 .bitmask = bitmask,
6020 .label = ilabel,
6021 };
6022 struct nfs_setattrres res = {
6023 .fattr = fattr,
6024 .label = olabel,
6025 .server = server,
6026 };
6027 struct rpc_message msg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05006028 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
6029 .rpc_argp = &arg,
6030 .rpc_resp = &res,
David Quigleyaa9c2662013-05-22 12:50:44 -04006031 };
6032 int status;
6033
Jeff Layton12207f62013-11-01 10:49:32 -04006034 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04006035
Jeff Layton12207f62013-11-01 10:49:32 -04006036 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04006037 if (status)
6038 dprintk("%s failed: %d\n", __func__, status);
6039
6040 return status;
6041}
6042
6043static int nfs4_do_set_security_label(struct inode *inode,
6044 struct nfs4_label *ilabel,
6045 struct nfs_fattr *fattr,
6046 struct nfs4_label *olabel)
6047{
6048 struct nfs4_exception exception = { };
6049 int err;
6050
6051 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04006052 err = _nfs4_do_set_security_label(inode, ilabel,
6053 fattr, olabel);
6054 trace_nfs4_set_security_label(inode, err);
6055 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04006056 &exception);
6057 } while (exception.retry);
6058 return err;
6059}
6060
6061static int
Al Viro59301222016-05-27 10:19:30 -04006062nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04006063{
6064 struct nfs4_label ilabel, *olabel = NULL;
6065 struct nfs_fattr fattr;
David Quigleyaa9c2662013-05-22 12:50:44 -04006066 int status;
6067
6068 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
6069 return -EOPNOTSUPP;
6070
6071 nfs_fattr_init(&fattr);
6072
6073 ilabel.pi = 0;
6074 ilabel.lfs = 0;
6075 ilabel.label = (char *)buf;
6076 ilabel.len = buflen;
6077
David Quigleyaa9c2662013-05-22 12:50:44 -04006078 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
6079 if (IS_ERR(olabel)) {
6080 status = -PTR_ERR(olabel);
6081 goto out;
6082 }
6083
6084 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
6085 if (status == 0)
6086 nfs_setsecurity(inode, &fattr, olabel);
6087
6088 nfs4_label_free(olabel);
6089out:
David Quigleyaa9c2662013-05-22 12:50:44 -04006090 return status;
6091}
6092#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
6093
6094
Chuck Leverf0920752012-05-21 22:45:41 -04006095static void nfs4_init_boot_verifier(const struct nfs_client *clp,
6096 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05006097{
6098 __be32 verf[2];
6099
Chuck Lever2c820d92012-05-21 22:45:33 -04006100 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
6101 /* An impossible timestamp guarantees this value
6102 * will never match a generated boot time. */
Deepa Dinamani2f86e092016-10-01 16:46:26 -07006103 verf[0] = cpu_to_be32(U32_MAX);
6104 verf[1] = cpu_to_be32(U32_MAX);
Chuck Lever2c820d92012-05-21 22:45:33 -04006105 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04006106 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Deepa Dinamani2f86e092016-10-01 16:46:26 -07006107 u64 ns = ktime_to_ns(nn->boot_time);
6108
6109 verf[0] = cpu_to_be32(ns >> 32);
6110 verf[1] = cpu_to_be32(ns);
Chuck Lever2c820d92012-05-21 22:45:33 -04006111 }
Chuck Levercd937102012-03-02 17:14:31 -05006112 memcpy(bootverf->data, verf, sizeof(bootverf->data));
6113}
6114
Trond Myklebust1aee5512020-10-07 18:24:17 -04006115static size_t
Trond Myklebust39d43d12020-10-07 18:24:18 -04006116nfs4_get_uniquifier(struct nfs_client *clp, char *buf, size_t buflen)
Trond Myklebust1aee5512020-10-07 18:24:17 -04006117{
Trond Myklebust39d43d12020-10-07 18:24:18 -04006118 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
6119 struct nfs_netns_client *nn_clp = nn->nfs_client;
6120 const char *id;
6121
Trond Myklebust1aee5512020-10-07 18:24:17 -04006122 buf[0] = '\0';
6123
Trond Myklebust39d43d12020-10-07 18:24:18 -04006124 if (nn_clp) {
6125 rcu_read_lock();
6126 id = rcu_dereference(nn_clp->identifier);
6127 if (id)
6128 strscpy(buf, id, buflen);
6129 rcu_read_unlock();
6130 }
6131
6132 if (nfs4_client_id_uniquifier[0] != '\0' && buf[0] == '\0')
Trond Myklebust1aee5512020-10-07 18:24:17 -04006133 strscpy(buf, nfs4_client_id_uniquifier, buflen);
6134
6135 return strlen(buf);
6136}
6137
Jeff Laytona3192682015-06-09 19:43:59 -04006138static int
6139nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04006140{
Trond Myklebust1aee5512020-10-07 18:24:17 -04006141 char buf[NFS4_CLIENT_ID_UNIQ_LEN];
6142 size_t buflen;
Jeff Laytona3192682015-06-09 19:43:59 -04006143 size_t len;
6144 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04006145
Trond Myklebustceb3a162015-01-03 15:16:04 -05006146 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04006147 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08006148
Jeff Laytona3192682015-06-09 19:43:59 -04006149 rcu_read_lock();
Chuck Lever848a4eb2018-06-04 10:53:29 -04006150 len = 14 +
6151 strlen(clp->cl_rpcclient->cl_nodename) +
6152 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04006153 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
Jeff Laytona3192682015-06-09 19:43:59 -04006154 1;
6155 rcu_read_unlock();
Trond Myklebust1aee5512020-10-07 18:24:17 -04006156
Trond Myklebust39d43d12020-10-07 18:24:18 -04006157 buflen = nfs4_get_uniquifier(clp, buf, sizeof(buf));
Trond Myklebust1aee5512020-10-07 18:24:17 -04006158 if (buflen)
6159 len += buflen + 1;
6160
Jeff Laytona3192682015-06-09 19:43:59 -04006161 if (len > NFS4_OPAQUE_LIMIT + 1)
6162 return -EINVAL;
6163
6164 /*
6165 * Since this string is allocated at mount time, and held until the
6166 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6167 * about a memory-reclaim deadlock.
6168 */
6169 str = kmalloc(len, GFP_KERNEL);
6170 if (!str)
6171 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05006172
Chuck Levere984a552012-09-14 17:24:21 -04006173 rcu_read_lock();
Trond Myklebust1aee5512020-10-07 18:24:17 -04006174 if (buflen)
Chuck Lever025bb9f2018-06-04 10:53:34 -04006175 scnprintf(str, len, "Linux NFSv4.0 %s/%s/%s",
Trond Myklebust1aee5512020-10-07 18:24:17 -04006176 clp->cl_rpcclient->cl_nodename, buf,
Chuck Lever848a4eb2018-06-04 10:53:29 -04006177 rpc_peeraddr2str(clp->cl_rpcclient,
Chuck Lever025bb9f2018-06-04 10:53:34 -04006178 RPC_DISPLAY_ADDR));
Chuck Lever848a4eb2018-06-04 10:53:29 -04006179 else
Chuck Lever025bb9f2018-06-04 10:53:34 -04006180 scnprintf(str, len, "Linux NFSv4.0 %s/%s",
Chuck Lever848a4eb2018-06-04 10:53:29 -04006181 clp->cl_rpcclient->cl_nodename,
6182 rpc_peeraddr2str(clp->cl_rpcclient,
Chuck Lever025bb9f2018-06-04 10:53:34 -04006183 RPC_DISPLAY_ADDR));
Chuck Levere984a552012-09-14 17:24:21 -04006184 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04006185
Jeff Laytona3192682015-06-09 19:43:59 -04006186 clp->cl_owner_id = str;
6187 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04006188}
6189
Jeff Layton873e3852015-06-09 19:44:00 -04006190static int
Jeff Layton873e3852015-06-09 19:44:00 -04006191nfs4_init_uniform_client_string(struct nfs_client *clp)
6192{
Trond Myklebust1aee5512020-10-07 18:24:17 -04006193 char buf[NFS4_CLIENT_ID_UNIQ_LEN];
6194 size_t buflen;
Jeff Layton873e3852015-06-09 19:44:00 -04006195 size_t len;
6196 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05006197
6198 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04006199 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04006200
Jeff Layton873e3852015-06-09 19:44:00 -04006201 len = 10 + 10 + 1 + 10 + 1 +
6202 strlen(clp->cl_rpcclient->cl_nodename) + 1;
6203
Trond Myklebust39d43d12020-10-07 18:24:18 -04006204 buflen = nfs4_get_uniquifier(clp, buf, sizeof(buf));
Trond Myklebust1aee5512020-10-07 18:24:17 -04006205 if (buflen)
6206 len += buflen + 1;
6207
Jeff Layton873e3852015-06-09 19:44:00 -04006208 if (len > NFS4_OPAQUE_LIMIT + 1)
6209 return -EINVAL;
6210
6211 /*
6212 * Since this string is allocated at mount time, and held until the
6213 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6214 * about a memory-reclaim deadlock.
6215 */
6216 str = kmalloc(len, GFP_KERNEL);
6217 if (!str)
6218 return -ENOMEM;
6219
Trond Myklebust1aee5512020-10-07 18:24:17 -04006220 if (buflen)
6221 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
6222 clp->rpc_ops->version, clp->cl_minorversion,
6223 buf, clp->cl_rpcclient->cl_nodename);
6224 else
6225 scnprintf(str, len, "Linux NFSv%u.%u %s",
6226 clp->rpc_ops->version, clp->cl_minorversion,
6227 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04006228 clp->cl_owner_id = str;
6229 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04006230}
6231
Chuck Lever706cb8d2014-03-12 12:51:47 -04006232/*
6233 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
6234 * services. Advertise one based on the address family of the
6235 * clientaddr.
6236 */
6237static unsigned int
6238nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
6239{
6240 if (strchr(clp->cl_ipaddr, ':') != NULL)
6241 return scnprintf(buf, len, "tcp6");
6242 else
6243 return scnprintf(buf, len, "tcp");
6244}
6245
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006246static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
6247{
6248 struct nfs4_setclientid *sc = calldata;
6249
6250 if (task->tk_status == 0)
6251 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
6252}
6253
6254static const struct rpc_call_ops nfs4_setclientid_ops = {
6255 .rpc_call_done = nfs4_setclientid_done,
6256};
6257
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006258/**
6259 * nfs4_proc_setclientid - Negotiate client ID
6260 * @clp: state data structure
6261 * @program: RPC program for NFSv4 callback service
6262 * @port: IP port number for NFS4 callback service
NeilBrowna52458b2018-12-03 11:30:31 +11006263 * @cred: credential to use for this call
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006264 * @res: where to place the result
6265 *
6266 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6267 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006268int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
NeilBrowna52458b2018-12-03 11:30:31 +11006269 unsigned short port, const struct cred *cred,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006270 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006271{
6272 nfs4_verifier sc_verifier;
6273 struct nfs4_setclientid setclientid = {
6274 .sc_verifier = &sc_verifier,
6275 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04006276 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006277 };
6278 struct rpc_message msg = {
6279 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
6280 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006281 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01006282 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006283 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006284 struct rpc_task_setup task_setup_data = {
6285 .rpc_client = clp->cl_rpcclient,
6286 .rpc_message = &msg,
6287 .callback_ops = &nfs4_setclientid_ops,
6288 .callback_data = &setclientid,
NeilBrown5a0c2572019-05-30 10:41:28 +10006289 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006290 };
Robert Milkowski7dc29932020-01-30 09:43:25 +00006291 unsigned long now = jiffies;
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006292 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006293
Chuck Leverde734832012-07-11 16:30:50 -04006294 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04006295 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04006296
6297 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
6298 status = nfs4_init_uniform_client_string(clp);
6299 else
Jeff Laytona3192682015-06-09 19:43:59 -04006300 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04006301
6302 if (status)
6303 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04006304
Chuck Leverde734832012-07-11 16:30:50 -04006305 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04006306 setclientid.sc_netid_len =
6307 nfs4_init_callback_netid(clp,
6308 setclientid.sc_netid,
6309 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04006310 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05006311 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006312 clp->cl_ipaddr, port >> 8, port & 255);
6313
Jeff Layton3a6bb732015-06-09 19:43:57 -04006314 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006315 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04006316 clp->cl_owner_id);
Anna Schumakerdae40962019-08-14 15:28:28 -04006317
6318 status = nfs4_call_sync_custom(&task_setup_data);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006319 if (setclientid.sc_cred) {
Chuck Lever1047ec82019-10-04 09:58:54 -04006320 kfree(clp->cl_acceptor);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006321 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
6322 put_rpccred(setclientid.sc_cred);
6323 }
Robert Milkowski7dc29932020-01-30 09:43:25 +00006324
6325 if (status == 0)
6326 do_renew_lease(clp, now);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006327out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006328 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006329 dprintk("NFS reply setclientid: %d\n", status);
6330 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006331}
6332
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006333/**
6334 * nfs4_proc_setclientid_confirm - Confirm client ID
6335 * @clp: state data structure
Trond Myklebust302fad72019-02-18 13:32:38 -05006336 * @arg: result of a previous SETCLIENTID
NeilBrowna52458b2018-12-03 11:30:31 +11006337 * @cred: credential to use for this call
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006338 *
6339 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6340 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04006341int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006342 struct nfs4_setclientid_res *arg,
NeilBrowna52458b2018-12-03 11:30:31 +11006343 const struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006344{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006345 struct rpc_message msg = {
6346 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006347 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01006348 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006349 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006350 int status;
6351
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006352 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
6353 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6354 clp->cl_clientid);
NeilBrown5a0c2572019-05-30 10:41:28 +10006355 status = rpc_call_sync(clp->cl_rpcclient, &msg,
6356 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006357 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006358 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006359 return status;
6360}
6361
Trond Myklebustfe650402006-01-03 09:55:18 +01006362struct nfs4_delegreturndata {
6363 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01006364 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01006365 struct nfs_fh fh;
6366 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006367 unsigned long timestamp;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006368 struct {
6369 struct nfs4_layoutreturn_args arg;
6370 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04006371 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006372 u32 roc_barrier;
6373 bool roc;
6374 } lr;
Trond Myklebustfa178f22006-01-03 09:55:38 +01006375 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01006376 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08006377 struct inode *inode;
Trond Myklebustfe650402006-01-03 09:55:18 +01006378};
6379
Trond Myklebustfe650402006-01-03 09:55:18 +01006380static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
6381{
6382 struct nfs4_delegreturndata *data = calldata;
Trond Myklebuste0dba012017-11-07 11:02:32 -05006383 struct nfs4_exception exception = {
6384 .inode = data->inode,
6385 .stateid = &data->stateid,
6386 };
Andy Adamson938e1012009-04-01 09:22:28 -04006387
Trond Myklebust14516c32010-07-31 14:29:06 -04006388 if (!nfs4_sequence_done(task, &data->res.seq_res))
6389 return;
Andy Adamson938e1012009-04-01 09:22:28 -04006390
Trond Myklebustca8acf82013-08-13 10:36:56 -04006391 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Trond Myklebust586f1c32016-11-15 15:03:33 -05006392
6393 /* Handle Layoutreturn errors */
Trond Myklebust078000d2021-01-04 13:18:03 -05006394 if (pnfs_roc_done(task, &data->args.lr_args, &data->res.lr_res,
6395 &data->res.lr_ret) == -EAGAIN)
Trond Myklebust287a9c52019-09-20 07:23:41 -04006396 goto out_restart;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006397
Ricardo Labiaga79708862009-12-07 09:23:21 -05006398 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05006399 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01006400 renew_lease(data->res.server, data->timestamp);
Trond Myklebust23ea44c2016-11-10 16:06:28 -05006401 break;
Trond Myklebustc97cf602013-11-19 16:34:14 -05006402 case -NFS4ERR_ADMIN_REVOKED:
6403 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust26d36302016-09-22 13:39:05 -04006404 case -NFS4ERR_EXPIRED:
6405 nfs4_free_revoked_stateid(data->res.server,
6406 data->args.stateid,
6407 task->tk_msg.rpc_cred);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05006408 fallthrough;
Trond Myklebustc97cf602013-11-19 16:34:14 -05006409 case -NFS4ERR_BAD_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05006410 case -NFS4ERR_STALE_STATEID:
Trond Myklebust244fcd22019-12-20 10:43:37 -05006411 case -ETIMEDOUT:
Trond Myklebustc97cf602013-11-19 16:34:14 -05006412 task->tk_status = 0;
6413 break;
Trond Myklebust12f275c2017-11-06 15:28:05 -05006414 case -NFS4ERR_OLD_STATEID:
Trond Myklebust246afc0a2019-10-24 18:00:35 -04006415 if (!nfs4_refresh_delegation_stateid(&data->stateid, data->inode))
6416 nfs4_stateid_seqid_inc(&data->stateid);
Trond Myklebust70d136b2019-10-26 22:37:40 -04006417 if (data->args.bitmask) {
6418 data->args.bitmask = NULL;
6419 data->res.fattr = NULL;
6420 }
Trond Myklebust246afc0a2019-10-24 18:00:35 -04006421 goto out_restart;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05006422 case -NFS4ERR_ACCESS:
6423 if (data->args.bitmask) {
6424 data->args.bitmask = NULL;
6425 data->res.fattr = NULL;
Trond Myklebust140087fd2017-11-06 15:28:10 -05006426 goto out_restart;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05006427 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05006428 fallthrough;
Ricardo Labiaga79708862009-12-07 09:23:21 -05006429 default:
Trond Myklebuste0dba012017-11-07 11:02:32 -05006430 task->tk_status = nfs4_async_handle_exception(task,
6431 data->res.server, task->tk_status,
6432 &exception);
6433 if (exception.retry)
Trond Myklebust140087fd2017-11-06 15:28:10 -05006434 goto out_restart;
Ricardo Labiaga79708862009-12-07 09:23:21 -05006435 }
Trond Myklebustd51f91d2019-10-21 14:22:14 -04006436 nfs_delegation_mark_returned(data->inode, data->args.stateid);
Ricardo Labiaga79708862009-12-07 09:23:21 -05006437 data->rpc_status = task->tk_status;
Trond Myklebust140087fd2017-11-06 15:28:10 -05006438 return;
Trond Myklebust140087fd2017-11-06 15:28:10 -05006439out_restart:
6440 task->tk_status = 0;
6441 rpc_restart_call_prepare(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01006442}
6443
6444static void nfs4_delegreturn_release(void *calldata)
6445{
Peng Tao039b7562014-07-03 13:05:02 +08006446 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006447 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08006448
Trond Myklebust078000d2021-01-04 13:18:03 -05006449 if (data->lr.roc)
6450 pnfs_roc_release(&data->lr.arg, &data->lr.res,
6451 data->res.lr_ret);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006452 if (inode) {
Trond Myklebust0bc2c9b2016-12-16 19:48:09 -05006453 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006454 nfs_iput_and_deactive(inode);
6455 }
Trond Myklebustfe650402006-01-03 09:55:18 +01006456 kfree(calldata);
6457}
6458
Andy Adamson938e1012009-04-01 09:22:28 -04006459static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
6460{
6461 struct nfs4_delegreturndata *d_data;
Trond Myklebustc8bf7072018-06-15 16:31:02 -04006462 struct pnfs_layout_hdr *lo;
Andy Adamson938e1012009-04-01 09:22:28 -04006463
6464 d_data = (struct nfs4_delegreturndata *)data;
6465
Trond Myklebust5326de92019-11-13 09:39:36 +01006466 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task)) {
6467 nfs4_sequence_done(task, &d_data->res.seq_res);
Peng Tao500d7012015-09-22 11:35:22 +08006468 return;
Trond Myklebust5326de92019-11-13 09:39:36 +01006469 }
Peng Tao500d7012015-09-22 11:35:22 +08006470
Trond Myklebustc8bf7072018-06-15 16:31:02 -04006471 lo = d_data->args.lr_args ? d_data->args.lr_args->layout : NULL;
6472 if (lo && !pnfs_layout_is_valid(lo)) {
6473 d_data->args.lr_args = NULL;
6474 d_data->res.lr_res = NULL;
6475 }
6476
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006477 nfs4_setup_sequence(d_data->res.server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006478 &d_data->args.seq_args,
6479 &d_data->res.seq_res,
6480 task);
Andy Adamson938e1012009-04-01 09:22:28 -04006481}
Andy Adamson938e1012009-04-01 09:22:28 -04006482
Jesper Juhlc8d149f2006-03-20 13:44:07 -05006483static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04006484 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01006485 .rpc_call_done = nfs4_delegreturn_done,
6486 .rpc_release = nfs4_delegreturn_release,
6487};
6488
NeilBrowna52458b2018-12-03 11:30:31 +11006489static int _nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01006490{
6491 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01006492 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01006493 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006494 struct rpc_message msg = {
6495 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
6496 .rpc_cred = cred,
6497 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006498 struct rpc_task_setup task_setup_data = {
6499 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04006500 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006501 .callback_ops = &nfs4_delegreturn_ops,
Trond Myklebustf304a802020-05-11 10:42:04 -04006502 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006503 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05006504 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01006505
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006506 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01006507 if (data == NULL)
6508 return -ENOMEM;
Anna Schumakerfba83f32018-05-04 16:22:50 -04006509 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Andrew Elble99ade3c2015-12-02 09:39:51 -05006510
6511 nfs4_state_protect(server->nfs_client,
6512 NFS_SP4_MACH_CRED_CLEANUP,
6513 &task_setup_data.rpc_client, &msg);
6514
Trond Myklebustfe650402006-01-03 09:55:18 +01006515 data->args.fhandle = &data->fh;
6516 data->args.stateid = &data->stateid;
Trond Myklebust332d1a02021-03-25 18:15:36 -04006517 nfs4_bitmask_set(data->args.bitmask_store,
6518 server->cache_consistency_bitmask, inode, server,
6519 NULL);
6520 data->args.bitmask = data->args.bitmask_store;
Trond Myklebustfe650402006-01-03 09:55:18 +01006521 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05006522 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01006523 data->res.fattr = &data->fattr;
6524 data->res.server = server;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006525 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust4d796d72016-09-23 11:38:08 -04006526 data->lr.arg.ld_private = &data->lr.ld_private;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006527 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01006528 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01006529 data->rpc_status = 0;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006530 data->inode = nfs_igrab_and_active(inode);
Trond Myklebust078000d2021-01-04 13:18:03 -05006531 if (data->inode || issync) {
6532 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res,
6533 cred);
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006534 if (data->lr.roc) {
6535 data->args.lr_args = &data->lr.arg;
6536 data->res.lr_res = &data->lr.res;
6537 }
6538 }
Trond Myklebustfe650402006-01-03 09:55:18 +01006539
Trond Myklebustc970aa82007-07-14 15:39:59 -04006540 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05006541 msg.rpc_argp = &data->args;
6542 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006543 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006544 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01006545 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05006546 if (!issync)
6547 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05006548 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05006549 if (status != 0)
6550 goto out;
6551 status = data->rpc_status;
Trond Myklebuste6f81072008-01-24 18:14:34 -05006552out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006553 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01006554 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006555}
6556
NeilBrowna52458b2018-12-03 11:30:31 +11006557int nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006558{
6559 struct nfs_server *server = NFS_SERVER(inode);
6560 struct nfs4_exception exception = { };
6561 int err;
6562 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05006563 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006564 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006565 switch (err) {
6566 case -NFS4ERR_STALE_STATEID:
6567 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006568 case 0:
6569 return 0;
6570 }
6571 err = nfs4_handle_exception(server, err, &exception);
6572 } while (exception.retry);
6573 return err;
6574}
6575
Linus Torvalds1da177e2005-04-16 15:20:36 -07006576static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6577{
6578 struct inode *inode = state->inode;
6579 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04006580 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006581 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006582 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006583 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006584 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006585 struct nfs_lockt_res res = {
6586 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006587 };
6588 struct rpc_message msg = {
6589 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
Anna Schumakerd9b67e12017-01-11 15:04:25 -05006590 .rpc_argp = &arg,
6591 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006592 .rpc_cred = state->owner->so_cred,
6593 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006594 struct nfs4_lock_state *lsp;
6595 int status;
6596
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006597 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00006598 status = nfs4_set_lock_state(state, request);
6599 if (status != 0)
6600 goto out;
6601 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006602 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006603 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00006604 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006605 switch (status) {
6606 case 0:
6607 request->fl_type = F_UNLCK;
6608 break;
6609 case -NFS4ERR_DENIED:
6610 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006611 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05006612 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04006613 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00006614out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006615 return status;
6616}
6617
6618static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6619{
Trond Myklebust0688e642019-04-07 13:59:09 -04006620 struct nfs4_exception exception = {
6621 .interruptible = true,
6622 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006623 int err;
6624
6625 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006626 err = _nfs4_proc_getlk(state, cmd, request);
6627 trace_nfs4_get_lock(request, state, cmd, err);
6628 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006629 &exception);
6630 } while (exception.retry);
6631 return err;
6632}
6633
Trond Myklebust32c6e7e2019-09-20 07:23:48 -04006634/*
6635 * Update the seqid of a lock stateid after receiving
6636 * NFS4ERR_OLD_STATEID
6637 */
6638static bool nfs4_refresh_lock_old_stateid(nfs4_stateid *dst,
6639 struct nfs4_lock_state *lsp)
6640{
6641 struct nfs4_state *state = lsp->ls_state;
6642 bool ret = false;
6643
6644 spin_lock(&state->state_lock);
6645 if (!nfs4_stateid_match_other(dst, &lsp->ls_stateid))
6646 goto out;
6647 if (!nfs4_stateid_is_newer(&lsp->ls_stateid, dst))
6648 nfs4_stateid_seqid_inc(dst);
6649 else
6650 dst->seqid = lsp->ls_stateid.seqid;
6651 ret = true;
6652out:
6653 spin_unlock(&state->state_lock);
6654 return ret;
6655}
6656
6657static bool nfs4_sync_lock_stateid(nfs4_stateid *dst,
6658 struct nfs4_lock_state *lsp)
6659{
6660 struct nfs4_state *state = lsp->ls_state;
6661 bool ret;
6662
6663 spin_lock(&state->state_lock);
6664 ret = !nfs4_stateid_match_other(dst, &lsp->ls_stateid);
6665 nfs4_stateid_copy(dst, &lsp->ls_stateid);
6666 spin_unlock(&state->state_lock);
6667 return ret;
6668}
6669
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006670struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006671 struct nfs_locku_args arg;
6672 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006673 struct nfs4_lock_state *lsp;
6674 struct nfs_open_context *ctx;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006675 struct nfs_lock_context *l_ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006676 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04006677 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006678 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006679};
6680
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006681static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
6682 struct nfs_open_context *ctx,
6683 struct nfs4_lock_state *lsp,
6684 struct nfs_seqid *seqid)
6685{
6686 struct nfs4_unlockdata *p;
Trond Myklebust32c6e7e2019-09-20 07:23:48 -04006687 struct nfs4_state *state = lsp->ls_state;
6688 struct inode *inode = state->inode;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006689
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006690 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006691 if (p == NULL)
6692 return NULL;
6693 p->arg.fh = NFS_FH(inode);
6694 p->arg.fl = &p->fl;
6695 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006696 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006697 p->lsp = lsp;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006698 /* Ensure we don't close file until we're done freeing locks! */
6699 p->ctx = get_nfs_open_context(ctx);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006700 p->l_ctx = nfs_get_lock_context(ctx);
NeilBrown7b587e12018-11-30 10:04:08 +11006701 locks_init_lock(&p->fl);
6702 locks_copy_lock(&p->fl, fl);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006703 p->server = NFS_SERVER(inode);
Trond Myklebust32c6e7e2019-09-20 07:23:48 -04006704 spin_lock(&state->state_lock);
6705 nfs4_stateid_copy(&p->arg.stateid, &lsp->ls_stateid);
6706 spin_unlock(&state->state_lock);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006707 return p;
6708}
6709
Trond Myklebust06f814a2006-01-03 09:55:07 +01006710static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006711{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006712 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006713 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006714 nfs4_put_lock_state(calldata->lsp);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006715 nfs_put_lock_context(calldata->l_ctx);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006716 put_nfs_open_context(calldata->ctx);
6717 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006718}
6719
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006720static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006721{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006722 struct nfs4_unlockdata *calldata = data;
Trond Myklebust82571552017-11-07 11:14:49 -05006723 struct nfs4_exception exception = {
6724 .inode = calldata->lsp->ls_state->inode,
6725 .stateid = &calldata->arg.stateid,
6726 };
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006727
Trond Myklebust14516c32010-07-31 14:29:06 -04006728 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
6729 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006730 switch (task->tk_status) {
6731 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01006732 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton75575dd2016-09-17 18:17:32 -04006733 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006734 if (nfs4_update_lock_stateid(calldata->lsp,
6735 &calldata->res.stateid))
6736 break;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05006737 fallthrough;
Trond Myklebust26d36302016-09-22 13:39:05 -04006738 case -NFS4ERR_ADMIN_REVOKED:
6739 case -NFS4ERR_EXPIRED:
6740 nfs4_free_revoked_stateid(calldata->server,
6741 &calldata->arg.stateid,
6742 task->tk_msg.rpc_cred);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05006743 fallthrough;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05006744 case -NFS4ERR_BAD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006745 case -NFS4ERR_STALE_STATEID:
Trond Myklebust32c6e7e2019-09-20 07:23:48 -04006746 if (nfs4_sync_lock_stateid(&calldata->arg.stateid,
6747 calldata->lsp))
6748 rpc_restart_call_prepare(task);
6749 break;
6750 case -NFS4ERR_OLD_STATEID:
6751 if (nfs4_refresh_lock_old_stateid(&calldata->arg.stateid,
6752 calldata->lsp))
Trond Myklebust425c1d42015-01-24 14:57:53 -05006753 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006754 break;
6755 default:
Trond Myklebust82571552017-11-07 11:14:49 -05006756 task->tk_status = nfs4_async_handle_exception(task,
6757 calldata->server, task->tk_status,
6758 &exception);
6759 if (exception.retry)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006760 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006761 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04006762 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006763}
6764
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006765static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006766{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006767 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006768
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006769 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
6770 nfs_async_iocounter_wait(task, calldata->l_ctx))
6771 return;
6772
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006773 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006774 goto out_wait;
Trond Myklebust795a88c2012-09-10 13:26:49 -04006775 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006776 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006777 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006778 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006779 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006780 if (nfs4_setup_sequence(calldata->server->nfs_client,
Andy Adamsona8936932009-04-01 09:22:23 -04006781 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006782 &calldata->res.seq_res,
6783 task) != 0)
6784 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006785 return;
6786out_no_action:
6787 task->tk_action = NULL;
6788out_wait:
6789 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006790}
6791
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006792static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006793 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006794 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01006795 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006796};
6797
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006798static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6799 struct nfs_open_context *ctx,
6800 struct nfs4_lock_state *lsp,
6801 struct nfs_seqid *seqid)
6802{
6803 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006804 struct rpc_message msg = {
6805 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6806 .rpc_cred = ctx->cred,
6807 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006808 struct rpc_task_setup task_setup_data = {
6809 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006810 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006811 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006812 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006813 .flags = RPC_TASK_ASYNC,
6814 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006815
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006816 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6817 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6818
Frank Filz137d6ac2007-07-09 15:32:29 -07006819 /* Ensure this is an unlock - when canceling a lock, the
6820 * canceled lock is passed in, and it won't be an unlock.
6821 */
6822 fl->fl_type = F_UNLCK;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006823 if (fl->fl_flags & FL_CLOSE)
6824 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
Frank Filz137d6ac2007-07-09 15:32:29 -07006825
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006826 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6827 if (data == NULL) {
6828 nfs_free_seqid(seqid);
6829 return ERR_PTR(-ENOMEM);
6830 }
6831
Anna Schumakerfba83f32018-05-04 16:22:50 -04006832 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1, 0);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006833 msg.rpc_argp = &data->arg;
6834 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006835 task_setup_data.callback_data = data;
6836 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006837}
6838
Linus Torvalds1da177e2005-04-16 15:20:36 -07006839static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6840{
Trond Myklebust65b62a22013-02-07 10:54:07 -05006841 struct inode *inode = state->inode;
6842 struct nfs4_state_owner *sp = state->owner;
6843 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006844 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006845 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01006846 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006847 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006848 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006849 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006850
Trond Myklebust9b073572006-06-29 16:38:34 -04006851 status = nfs4_set_lock_state(state, request);
6852 /* Unlock _before_ we do the RPC call */
6853 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05006854 /* Exclude nfs_delegation_claim_locks() */
6855 mutex_lock(&sp->so_delegreturn_mutex);
6856 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006857 down_read(&nfsi->rwsem);
Jeff Layton75575dd2016-09-17 18:17:32 -04006858 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05006859 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006860 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006861 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05006862 }
6863 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006864 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006865 if (status != 0)
6866 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006867 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006868 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04006869 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6870 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006871 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6872 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04006873 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006874 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04006875 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006876 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006877 status = PTR_ERR(task);
6878 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04006879 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05006880 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006881 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04006882out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006883 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006884 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006885 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006886}
6887
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006888struct nfs4_lockdata {
6889 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006890 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006891 struct nfs4_lock_state *lsp;
6892 struct nfs_open_context *ctx;
6893 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006894 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006895 int rpc_status;
6896 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04006897 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006898};
6899
6900static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006901 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6902 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006903{
6904 struct nfs4_lockdata *p;
6905 struct inode *inode = lsp->ls_state->inode;
6906 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05006907 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006908
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006909 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006910 if (p == NULL)
6911 return NULL;
6912
6913 p->arg.fh = NFS_FH(inode);
6914 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006915 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006916 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006917 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006918 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6919 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006920 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006921 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04006922 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006923 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006924 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006925 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006926 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04006927 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006928 p->ctx = get_nfs_open_context(ctx);
NeilBrown7b587e12018-11-30 10:04:08 +11006929 locks_init_lock(&p->fl);
6930 locks_copy_lock(&p->fl, fl);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006931 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006932out_free_seqid:
6933 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006934out_free:
6935 kfree(p);
6936 return NULL;
6937}
6938
6939static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6940{
6941 struct nfs4_lockdata *data = calldata;
6942 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006943
Harvey Harrison3110ff82008-05-02 13:42:44 -07006944 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006945 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006946 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006947 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05006948 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006949 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006950 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006951 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05006952 nfs4_stateid_copy(&data->arg.open_stateid,
6953 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006954 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006955 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006956 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006957 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006958 nfs4_stateid_copy(&data->arg.lock_stateid,
6959 &data->lsp->ls_stateid);
6960 }
Trond Myklebust5d422302013-03-14 16:57:48 -04006961 if (!nfs4_valid_open_stateid(state)) {
6962 data->rpc_status = -EBADF;
6963 task->tk_action = NULL;
6964 goto out_release_open_seqid;
6965 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006966 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006967 if (nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust035168ab2010-06-16 09:52:26 -04006968 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006969 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006970 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04006971 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04006972out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006973 nfs_release_seqid(data->arg.open_seqid);
6974out_release_lock_seqid:
6975 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006976out_wait:
6977 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006978 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006979}
6980
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006981static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6982{
6983 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05006984 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006985
Harvey Harrison3110ff82008-05-02 13:42:44 -07006986 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006987
Trond Myklebust14516c32010-07-31 14:29:06 -04006988 if (!nfs4_sequence_done(task, &data->res.seq_res))
6989 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04006990
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006991 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006992 switch (task->tk_status) {
6993 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00006994 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05006995 data->timestamp);
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04006996 if (data->arg.new_lock && !data->cancelled) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05006997 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04006998 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0)
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006999 goto out_restart;
Trond Myklebustc69899a2015-01-24 16:03:52 -05007000 }
Trond Myklebust39071e62015-01-24 15:07:56 -05007001 if (data->arg.new_lock_owner != 0) {
7002 nfs_confirm_seqid(&lsp->ls_seqid, 0);
7003 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
7004 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04007005 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
7006 goto out_restart;
Trond Myklebust425c1d42015-01-24 14:57:53 -05007007 break;
7008 case -NFS4ERR_BAD_STATEID:
7009 case -NFS4ERR_OLD_STATEID:
7010 case -NFS4ERR_STALE_STATEID:
7011 case -NFS4ERR_EXPIRED:
7012 if (data->arg.new_lock_owner != 0) {
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04007013 if (!nfs4_stateid_match(&data->arg.open_stateid,
Trond Myklebust425c1d42015-01-24 14:57:53 -05007014 &lsp->ls_state->open_stateid))
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04007015 goto out_restart;
7016 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
Trond Myklebust425c1d42015-01-24 14:57:53 -05007017 &lsp->ls_stateid))
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04007018 goto out_restart;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007019 }
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04007020out_done:
Harvey Harrison3110ff82008-05-02 13:42:44 -07007021 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04007022 return;
7023out_restart:
7024 if (!data->cancelled)
7025 rpc_restart_call_prepare(task);
7026 goto out_done;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007027}
7028
7029static void nfs4_lock_release(void *calldata)
7030{
7031 struct nfs4_lockdata *data = calldata;
7032
Harvey Harrison3110ff82008-05-02 13:42:44 -07007033 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05007034 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04007035 if (data->cancelled && data->rpc_status == 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007036 struct rpc_task *task;
7037 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
7038 data->arg.lock_seqid);
7039 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08007040 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07007041 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007042 } else
7043 nfs_free_seqid(data->arg.lock_seqid);
7044 nfs4_put_lock_state(data->lsp);
7045 put_nfs_open_context(data->ctx);
7046 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07007047 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007048}
7049
7050static const struct rpc_call_ops nfs4_lock_ops = {
7051 .rpc_call_prepare = nfs4_lock_prepare,
7052 .rpc_call_done = nfs4_lock_done,
7053 .rpc_release = nfs4_lock_release,
7054};
7055
Trond Myklebust2bee72a2010-01-26 15:42:21 -05007056static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
7057{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05007058 switch (error) {
7059 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustd7f3e4b2016-09-22 13:39:09 -04007060 case -NFS4ERR_EXPIRED:
Trond Myklebust2bee72a2010-01-26 15:42:21 -05007061 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05007062 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05007063 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04007064 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05007065 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05007066 break;
7067 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05007068 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05007069 nfs4_schedule_lease_recovery(server->nfs_client);
zhengbin8b98a532019-12-19 18:34:47 +08007070 }
Trond Myklebust2bee72a2010-01-26 15:42:21 -05007071}
7072
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08007073static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007074{
7075 struct nfs4_lockdata *data;
7076 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04007077 struct rpc_message msg = {
7078 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
7079 .rpc_cred = state->owner->so_cred,
7080 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04007081 struct rpc_task_setup task_setup_data = {
7082 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04007083 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04007084 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05007085 .workqueue = nfsiod_workqueue,
Trond Myklebust61296502020-02-07 19:38:12 -05007086 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
Trond Myklebustc970aa82007-07-14 15:39:59 -04007087 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007088 int ret;
7089
Harvey Harrison3110ff82008-05-02 13:42:44 -07007090 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04007091 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04007092 fl->fl_u.nfs4_fl.owner,
7093 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007094 if (data == NULL)
7095 return -ENOMEM;
7096 if (IS_SETLKW(cmd))
7097 data->arg.block = 1;
Anna Schumakerfba83f32018-05-04 16:22:50 -04007098 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1,
7099 recovery_type > NFS_LOCK_NEW);
Trond Myklebust1174dd12010-12-21 10:52:24 -05007100 msg.rpc_argp = &data->arg;
7101 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04007102 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007103 if (recovery_type > NFS_LOCK_NEW) {
7104 if (recovery_type == NFS_LOCK_RECLAIM)
7105 data->arg.reclaim = NFS_LOCK_RECLAIM;
Trond Myklebustc69899a2015-01-24 16:03:52 -05007106 } else
7107 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04007108 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05007109 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007110 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05007111 ret = rpc_wait_for_completion_task(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007112 if (ret == 0) {
7113 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05007114 if (ret)
7115 nfs4_handle_setlk_error(data->server, data->lsp,
7116 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007117 } else
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04007118 data->cancelled = true;
Dave Wysochanski3d1a90a2020-12-11 05:12:51 -05007119 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05007120 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07007121 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007122 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007123}
7124
7125static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
7126{
Trond Myklebust202b50d2005-06-22 17:16:29 +00007127 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04007128 struct nfs4_exception exception = {
7129 .inode = state->inode,
7130 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00007131 int err;
7132
7133 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04007134 /* Cache the lock if possible... */
7135 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
7136 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08007137 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04007138 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00007139 break;
7140 nfs4_handle_exception(server, err, &exception);
7141 } while (exception.retry);
7142 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007143}
7144
7145static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
7146{
Trond Myklebust202b50d2005-06-22 17:16:29 +00007147 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04007148 struct nfs4_exception exception = {
7149 .inode = state->inode,
7150 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00007151 int err;
7152
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05007153 err = nfs4_set_lock_state(state, request);
7154 if (err != 0)
7155 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04007156 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10007157 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
7158 return 0;
7159 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00007160 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04007161 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
7162 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08007163 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05007164 switch (err) {
7165 default:
7166 goto out;
7167 case -NFS4ERR_GRACE:
7168 case -NFS4ERR_DELAY:
7169 nfs4_handle_exception(server, err, &exception);
7170 err = 0;
7171 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00007172 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05007173out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00007174 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007175}
7176
Bryan Schumakerf062eb62011-06-02 14:59:10 -04007177#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05007178static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
7179{
Trond Myklebustc5896fc2016-09-22 13:39:03 -04007180 struct nfs4_lock_state *lsp;
7181 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05007182
Trond Myklebustc5896fc2016-09-22 13:39:03 -04007183 status = nfs4_set_lock_state(state, request);
7184 if (status != 0)
7185 return status;
7186 lsp = request->fl_u.nfs4_fl.owner;
7187 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
7188 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
7189 return 0;
Anna Schumaker81b68de2017-01-11 16:41:34 -05007190 return nfs4_lock_expired(state, request);
Bryan Schumakerf062eb62011-06-02 14:59:10 -04007191}
7192#endif
7193
Linus Torvalds1da177e2005-04-16 15:20:36 -07007194static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7195{
Trond Myklebust19e03c52008-12-23 15:21:44 -05007196 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04007197 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04007198 unsigned char fl_flags = request->fl_flags;
Jeff Layton1ea67db2016-09-17 18:17:37 -04007199 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007200
Trond Myklebust01c3b862006-06-29 16:38:39 -04007201 request->fl_flags |= FL_ACCESS;
Jeff Layton75575dd2016-09-17 18:17:32 -04007202 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04007203 if (status < 0)
7204 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04007205 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05007206 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04007207 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04007208 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04007209 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05007210 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton75575dd2016-09-17 18:17:32 -04007211 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05007212 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04007213 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05007214 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04007215 }
Trond Myklebust9a99af42013-02-04 20:17:49 -05007216 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04007217 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08007218 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04007219out:
7220 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007221 return status;
7222}
7223
7224static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7225{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05007226 struct nfs4_exception exception = {
7227 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04007228 .inode = state->inode,
Trond Myklebust0688e642019-04-07 13:59:09 -04007229 .interruptible = true,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05007230 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07007231 int err;
7232
7233 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07007234 err = _nfs4_proc_setlk(state, cmd, request);
7235 if (err == -NFS4ERR_DENIED)
7236 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007237 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07007238 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007239 } while (exception.retry);
7240 return err;
7241}
7242
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04007243#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
7244#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
7245
7246static int
Jeff Laytona1d617d82016-09-17 18:17:39 -04007247nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
7248 struct file_lock *request)
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04007249{
7250 int status = -ERESTARTSYS;
7251 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
7252
7253 while(!signalled()) {
7254 status = nfs4_proc_setlk(state, cmd, request);
7255 if ((status != -EAGAIN) || IS_SETLK(cmd))
7256 break;
7257 freezable_schedule_timeout_interruptible(timeout);
7258 timeout *= 2;
7259 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
7260 status = -ERESTARTSYS;
7261 }
7262 return status;
7263}
7264
Jeff Laytona1d617d82016-09-17 18:17:39 -04007265#ifdef CONFIG_NFS_V4_1
7266struct nfs4_lock_waiter {
Jeff Laytona1d617d82016-09-17 18:17:39 -04007267 struct inode *inode;
Trond Myklebust86438182021-02-02 14:22:51 -05007268 struct nfs_lowner owner;
7269 wait_queue_entry_t wait;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007270};
7271
7272static int
Ingo Molnarac6424b2017-06-20 12:06:13 +02007273nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
Jeff Laytona1d617d82016-09-17 18:17:39 -04007274{
Trond Myklebust86438182021-02-02 14:22:51 -05007275 struct nfs4_lock_waiter *waiter =
7276 container_of(wait, struct nfs4_lock_waiter, wait);
Jeff Laytona1d617d82016-09-17 18:17:39 -04007277
Jeff Layton57174592018-03-18 08:37:03 -04007278 /* NULL key means to wake up everyone */
7279 if (key) {
7280 struct cb_notify_lock_args *cbnl = key;
7281 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
Trond Myklebust86438182021-02-02 14:22:51 -05007282 *wowner = &waiter->owner;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007283
Jeff Layton57174592018-03-18 08:37:03 -04007284 /* Only wake if the callback was for the same owner. */
7285 if (lowner->id != wowner->id || lowner->s_dev != wowner->s_dev)
7286 return 0;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007287
Jeff Layton57174592018-03-18 08:37:03 -04007288 /* Make sure it's for the right inode */
7289 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
7290 return 0;
Jeff Layton57174592018-03-18 08:37:03 -04007291 }
Jeff Laytona1d617d82016-09-17 18:17:39 -04007292
Trond Myklebust86438182021-02-02 14:22:51 -05007293 return woken_wake_function(wait, mode, flags, key);
Jeff Laytona1d617d82016-09-17 18:17:39 -04007294}
7295
7296static int
7297nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7298{
Jeff Laytona1d617d82016-09-17 18:17:39 -04007299 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
7300 struct nfs_server *server = NFS_SERVER(state->inode);
7301 struct nfs_client *clp = server->nfs_client;
7302 wait_queue_head_t *q = &clp->cl_lock_waitq;
Trond Myklebust86438182021-02-02 14:22:51 -05007303 struct nfs4_lock_waiter waiter = {
7304 .inode = state->inode,
7305 .owner = { .clientid = clp->cl_clientid,
7306 .id = lsp->ls_seqid.owner_id,
7307 .s_dev = server->s_dev },
7308 };
7309 int status;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007310
7311 /* Don't bother with waitqueue if we don't expect a callback */
7312 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
7313 return nfs4_retry_setlk_simple(state, cmd, request);
7314
Trond Myklebust86438182021-02-02 14:22:51 -05007315 init_wait(&waiter.wait);
7316 waiter.wait.func = nfs4_wake_lock_waiter;
7317 add_wait_queue(q, &waiter.wait);
Jeff Laytona1d617d82016-09-17 18:17:39 -04007318
Trond Myklebust86438182021-02-02 14:22:51 -05007319 do {
Jeff Laytona1d617d82016-09-17 18:17:39 -04007320 status = nfs4_proc_setlk(state, cmd, request);
Trond Myklebust86438182021-02-02 14:22:51 -05007321 if (status != -EAGAIN || IS_SETLK(cmd))
Jeff Laytona1d617d82016-09-17 18:17:39 -04007322 break;
7323
7324 status = -ERESTARTSYS;
Yihao Wu52b042a2019-05-22 01:57:10 +08007325 freezer_do_not_count();
Trond Myklebust86438182021-02-02 14:22:51 -05007326 wait_woken(&waiter.wait, TASK_INTERRUPTIBLE,
7327 NFS4_LOCK_MAXTIMEOUT);
Yihao Wu52b042a2019-05-22 01:57:10 +08007328 freezer_count();
Trond Myklebust86438182021-02-02 14:22:51 -05007329 } while (!signalled());
7330
7331 remove_wait_queue(q, &waiter.wait);
Jeff Laytona1d617d82016-09-17 18:17:39 -04007332
Jeff Laytona1d617d82016-09-17 18:17:39 -04007333 return status;
7334}
7335#else /* !CONFIG_NFS_V4_1 */
7336static inline int
7337nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7338{
7339 return nfs4_retry_setlk_simple(state, cmd, request);
7340}
7341#endif
7342
Linus Torvalds1da177e2005-04-16 15:20:36 -07007343static int
7344nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
7345{
7346 struct nfs_open_context *ctx;
7347 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007348 int status;
7349
7350 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04007351 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007352 state = ctx->state;
7353
Trond Myklebustd9531262009-07-21 19:22:38 -04007354 if (IS_GETLK(cmd)) {
7355 if (state != NULL)
7356 return nfs4_proc_getlk(state, F_GETLK, request);
7357 return 0;
7358 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007359
7360 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
7361 return -EINVAL;
7362
Trond Myklebustd9531262009-07-21 19:22:38 -04007363 if (request->fl_type == F_UNLCK) {
7364 if (state != NULL)
7365 return nfs4_proc_unlck(state, cmd, request);
7366 return 0;
7367 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007368
Trond Myklebustd9531262009-07-21 19:22:38 -04007369 if (state == NULL)
7370 return -ENOLCK;
Jeff Layton1ea67db2016-09-17 18:17:37 -04007371
7372 if ((request->fl_flags & FL_POSIX) &&
7373 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
7374 return -ENOLCK;
7375
Benjamin Coddingtonfcfa4472017-11-10 06:27:49 -05007376 /*
7377 * Don't rely on the VFS having checked the file open mode,
7378 * since it won't do this for flock() locks.
7379 */
7380 switch (request->fl_type) {
7381 case F_RDLCK:
7382 if (!(filp->f_mode & FMODE_READ))
7383 return -EBADF;
7384 break;
7385 case F_WRLCK:
7386 if (!(filp->f_mode & FMODE_WRITE))
7387 return -EBADF;
7388 }
7389
Jeff Layton1ea67db2016-09-17 18:17:37 -04007390 status = nfs4_set_lock_state(state, request);
7391 if (status != 0)
7392 return status;
7393
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04007394 return nfs4_retry_setlk(state, cmd, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007395}
7396
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04007397int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05007398{
7399 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05007400 int err;
7401
7402 err = nfs4_set_lock_state(state, fl);
7403 if (err != 0)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04007404 return err;
Olga Kornievskaia3d7a9522020-08-20 18:52:43 -04007405 do {
7406 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
7407 if (err != -NFS4ERR_DELAY)
7408 break;
7409 ssleep(1);
7410 } while (err == -NFS4ERR_DELAY);
NeilBrowndce26302017-12-13 09:57:09 +11007411 return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05007412}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007413
Trond Myklebustcf470c32012-03-07 13:49:12 -05007414struct nfs_release_lockowner_data {
7415 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04007416 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007417 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08007418 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04007419 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007420};
7421
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007422static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
7423{
7424 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08007425 struct nfs_server *server = data->server;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05007426 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
7427 &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08007428 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04007429 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007430}
7431
7432static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
7433{
7434 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04007435 struct nfs_server *server = data->server;
7436
Trond Myklebustb7e63a12014-02-26 11:19:14 -08007437 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04007438
7439 switch (task->tk_status) {
7440 case 0:
7441 renew_lease(server, data->timestamp);
7442 break;
7443 case -NFS4ERR_STALE_CLIENTID:
7444 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08007445 nfs4_schedule_lease_recovery(server->nfs_client);
7446 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04007447 case -NFS4ERR_LEASE_MOVED:
7448 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10007449 if (nfs4_async_handle_error(task, server,
7450 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04007451 rpc_restart_call_prepare(task);
7452 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007453}
7454
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007455static void nfs4_release_lockowner_release(void *calldata)
7456{
Trond Myklebustcf470c32012-03-07 13:49:12 -05007457 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04007458 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007459 kfree(calldata);
7460}
7461
Trond Myklebust17280172012-03-11 13:11:00 -04007462static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007463 .rpc_call_prepare = nfs4_release_lockowner_prepare,
7464 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007465 .rpc_release = nfs4_release_lockowner_release,
7466};
7467
Jeff Laytonf1cdae82014-05-01 06:28:47 -04007468static void
7469nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007470{
Trond Myklebustcf470c32012-03-07 13:49:12 -05007471 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007472 struct rpc_message msg = {
7473 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
7474 };
7475
7476 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04007477 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007478
Trond Myklebustcf470c32012-03-07 13:49:12 -05007479 data = kmalloc(sizeof(*data), GFP_NOFS);
7480 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04007481 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007482 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04007483 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007484 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7485 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
7486 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007487
Trond Myklebustcf470c32012-03-07 13:49:12 -05007488 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08007489 msg.rpc_resp = &data->res;
Anna Schumakerfba83f32018-05-04 16:22:50 -04007490 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05007491 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007492}
7493
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00007494#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
7495
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007496static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Christian Braunere65ce2a2021-01-21 14:19:27 +01007497 struct user_namespace *mnt_userns,
Al Viro59301222016-05-27 10:19:30 -04007498 struct dentry *unused, struct inode *inode,
7499 const char *key, const void *buf,
7500 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007501{
Al Viro59301222016-05-27 10:19:30 -04007502 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007503}
7504
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007505static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04007506 struct dentry *unused, struct inode *inode,
7507 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007508{
Al Virob2968212016-04-10 20:48:24 -04007509 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007510}
7511
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01007512static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007513{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01007514 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007515}
7516
David Quigleyc9bccef2013-05-22 12:50:45 -04007517#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04007518
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007519static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Christian Braunere65ce2a2021-01-21 14:19:27 +01007520 struct user_namespace *mnt_userns,
Al Viro59301222016-05-27 10:19:30 -04007521 struct dentry *unused, struct inode *inode,
7522 const char *key, const void *buf,
7523 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04007524{
7525 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04007526 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04007527
7528 return -EOPNOTSUPP;
7529}
7530
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007531static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04007532 struct dentry *unused, struct inode *inode,
7533 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04007534{
7535 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04007536 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04007537 return -EOPNOTSUPP;
7538}
7539
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007540static ssize_t
7541nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04007542{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007543 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04007544
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007545 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
7546 len = security_inode_listsecurity(inode, list, list_len);
Chengguang Xu82c596e2020-06-17 09:09:39 +08007547 if (len >= 0 && list_len && len > list_len)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007548 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04007549 }
7550 return len;
7551}
7552
7553static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
7554 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04007555 .get = nfs4_xattr_get_nfs4_label,
7556 .set = nfs4_xattr_set_nfs4_label,
7557};
David Quigleyc9bccef2013-05-22 12:50:45 -04007558
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007559#else
7560
7561static ssize_t
7562nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7563{
7564 return 0;
7565}
7566
7567#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04007568
Frank van der Linden012a2112020-06-23 22:39:03 +00007569#ifdef CONFIG_NFS_V4_2
7570static int nfs4_xattr_set_nfs4_user(const struct xattr_handler *handler,
Christian Braunere65ce2a2021-01-21 14:19:27 +01007571 struct user_namespace *mnt_userns,
Frank van der Linden012a2112020-06-23 22:39:03 +00007572 struct dentry *unused, struct inode *inode,
7573 const char *key, const void *buf,
7574 size_t buflen, int flags)
7575{
7576 struct nfs_access_entry cache;
Frank van der Linden95ad37f2020-06-23 22:39:04 +00007577 int ret;
Frank van der Linden012a2112020-06-23 22:39:03 +00007578
7579 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7580 return -EOPNOTSUPP;
7581
7582 /*
7583 * There is no mapping from the MAY_* flags to the NFS_ACCESS_XA*
7584 * flags right now. Handling of xattr operations use the normal
7585 * file read/write permissions.
7586 *
7587 * Just in case the server has other ideas (which RFC 8276 allows),
7588 * do a cached access check for the XA* flags to possibly avoid
7589 * doing an RPC and getting EACCES back.
7590 */
7591 if (!nfs_access_get_cached(inode, current_cred(), &cache, true)) {
7592 if (!(cache.mask & NFS_ACCESS_XAWRITE))
7593 return -EACCES;
7594 }
7595
Frank van der Linden95ad37f2020-06-23 22:39:04 +00007596 if (buf == NULL) {
7597 ret = nfs42_proc_removexattr(inode, key);
7598 if (!ret)
7599 nfs4_xattr_cache_remove(inode, key);
7600 } else {
7601 ret = nfs42_proc_setxattr(inode, key, buf, buflen, flags);
7602 if (!ret)
7603 nfs4_xattr_cache_add(inode, key, buf, NULL, buflen);
7604 }
7605
7606 return ret;
Frank van der Linden012a2112020-06-23 22:39:03 +00007607}
7608
7609static int nfs4_xattr_get_nfs4_user(const struct xattr_handler *handler,
7610 struct dentry *unused, struct inode *inode,
7611 const char *key, void *buf, size_t buflen)
7612{
7613 struct nfs_access_entry cache;
Frank van der Linden95ad37f2020-06-23 22:39:04 +00007614 ssize_t ret;
Frank van der Linden012a2112020-06-23 22:39:03 +00007615
7616 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7617 return -EOPNOTSUPP;
7618
7619 if (!nfs_access_get_cached(inode, current_cred(), &cache, true)) {
7620 if (!(cache.mask & NFS_ACCESS_XAREAD))
7621 return -EACCES;
7622 }
7623
Trond Myklebust1f3208b2021-03-25 11:04:34 -04007624 ret = nfs_revalidate_inode(inode, NFS_INO_INVALID_CHANGE);
Frank van der Linden95ad37f2020-06-23 22:39:04 +00007625 if (ret)
7626 return ret;
7627
7628 ret = nfs4_xattr_cache_get(inode, key, buf, buflen);
7629 if (ret >= 0 || (ret < 0 && ret != -ENOENT))
7630 return ret;
7631
7632 ret = nfs42_proc_getxattr(inode, key, buf, buflen);
7633
7634 return ret;
Frank van der Linden012a2112020-06-23 22:39:03 +00007635}
7636
7637static ssize_t
7638nfs4_listxattr_nfs4_user(struct inode *inode, char *list, size_t list_len)
7639{
7640 u64 cookie;
7641 bool eof;
Frank van der Linden95ad37f2020-06-23 22:39:04 +00007642 ssize_t ret, size;
Frank van der Linden012a2112020-06-23 22:39:03 +00007643 char *buf;
7644 size_t buflen;
7645 struct nfs_access_entry cache;
7646
7647 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7648 return 0;
7649
7650 if (!nfs_access_get_cached(inode, current_cred(), &cache, true)) {
7651 if (!(cache.mask & NFS_ACCESS_XALIST))
7652 return 0;
7653 }
7654
Trond Myklebust1f3208b2021-03-25 11:04:34 -04007655 ret = nfs_revalidate_inode(inode, NFS_INO_INVALID_CHANGE);
Frank van der Linden95ad37f2020-06-23 22:39:04 +00007656 if (ret)
7657 return ret;
7658
7659 ret = nfs4_xattr_cache_list(inode, list, list_len);
7660 if (ret >= 0 || (ret < 0 && ret != -ENOENT))
7661 return ret;
7662
Frank van der Linden012a2112020-06-23 22:39:03 +00007663 cookie = 0;
7664 eof = false;
7665 buflen = list_len ? list_len : XATTR_LIST_MAX;
7666 buf = list_len ? list : NULL;
7667 size = 0;
7668
7669 while (!eof) {
7670 ret = nfs42_proc_listxattrs(inode, buf, buflen,
7671 &cookie, &eof);
7672 if (ret < 0)
7673 return ret;
7674
7675 if (list_len) {
7676 buf += ret;
7677 buflen -= ret;
7678 }
7679 size += ret;
7680 }
7681
Frank van der Linden95ad37f2020-06-23 22:39:04 +00007682 if (list_len)
7683 nfs4_xattr_cache_set_list(inode, list, size);
7684
Frank van der Linden012a2112020-06-23 22:39:03 +00007685 return size;
7686}
7687
7688#else
7689
7690static ssize_t
7691nfs4_listxattr_nfs4_user(struct inode *inode, char *list, size_t list_len)
7692{
7693 return 0;
7694}
7695#endif /* CONFIG_NFS_V4_2 */
7696
Andy Adamson533eb462011-06-13 18:25:56 -04007697/*
7698 * nfs_fhget will use either the mounted_on_fileid or the fileid
7699 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007700static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
7701{
Andy Adamson533eb462011-06-13 18:25:56 -04007702 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
7703 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
7704 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05007705 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007706 return;
7707
7708 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05007709 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007710 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
7711 fattr->nlink = 2;
7712}
7713
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007714static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7715 const struct qstr *name,
7716 struct nfs4_fs_locations *fs_locations,
7717 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04007718{
7719 struct nfs_server *server = NFS_SERVER(dir);
Chuck Leverc05cefc2017-11-05 15:45:22 -05007720 u32 bitmask[3];
Trond Myklebust683b57b2006-06-09 09:34:22 -04007721 struct nfs4_fs_locations_arg args = {
7722 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05007723 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04007724 .page = page,
7725 .bitmask = bitmask,
7726 };
Benny Halevy22958462009-04-01 09:22:02 -04007727 struct nfs4_fs_locations_res res = {
7728 .fs_locations = fs_locations,
7729 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04007730 struct rpc_message msg = {
7731 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7732 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04007733 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04007734 };
7735 int status;
7736
Harvey Harrison3110ff82008-05-02 13:42:44 -07007737 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04007738
Chuck Leverc05cefc2017-11-05 15:45:22 -05007739 bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
7740 bitmask[1] = nfs4_fattr_bitmap[1];
7741
Andy Adamson533eb462011-06-13 18:25:56 -04007742 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
7743 * is not supported */
7744 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
Chuck Leverc05cefc2017-11-05 15:45:22 -05007745 bitmask[0] &= ~FATTR4_WORD0_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04007746 else
Chuck Leverc05cefc2017-11-05 15:45:22 -05007747 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04007748
Trond Myklebustc228fd32007-01-13 02:28:11 -05007749 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04007750 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04007751 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007752 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07007753 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04007754 return status;
7755}
7756
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007757int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7758 const struct qstr *name,
7759 struct nfs4_fs_locations *fs_locations,
7760 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007761{
Trond Myklebust0688e642019-04-07 13:59:09 -04007762 struct nfs4_exception exception = {
7763 .interruptible = true,
7764 };
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007765 int err;
7766 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007767 err = _nfs4_proc_fs_locations(client, dir, name,
7768 fs_locations, page);
7769 trace_nfs4_get_fs_locations(dir, name, err);
7770 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007771 &exception);
7772 } while (exception.retry);
7773 return err;
7774}
7775
Chuck Leverb03d7352013-10-17 14:12:50 -04007776/*
7777 * This operation also signals the server that this client is
7778 * performing migration recovery. The server can stop returning
7779 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
7780 * appended to this compound to identify the client ID which is
7781 * performing recovery.
7782 */
7783static int _nfs40_proc_get_locations(struct inode *inode,
7784 struct nfs4_fs_locations *locations,
NeilBrowna52458b2018-12-03 11:30:31 +11007785 struct page *page, const struct cred *cred)
Chuck Leverb03d7352013-10-17 14:12:50 -04007786{
7787 struct nfs_server *server = NFS_SERVER(inode);
7788 struct rpc_clnt *clnt = server->client;
7789 u32 bitmask[2] = {
7790 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7791 };
7792 struct nfs4_fs_locations_arg args = {
7793 .clientid = server->nfs_client->cl_clientid,
7794 .fh = NFS_FH(inode),
7795 .page = page,
7796 .bitmask = bitmask,
7797 .migration = 1, /* skip LOOKUP */
7798 .renew = 1, /* append RENEW */
7799 };
7800 struct nfs4_fs_locations_res res = {
7801 .fs_locations = locations,
7802 .migration = 1,
7803 .renew = 1,
7804 };
7805 struct rpc_message msg = {
7806 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7807 .rpc_argp = &args,
7808 .rpc_resp = &res,
7809 .rpc_cred = cred,
7810 };
7811 unsigned long now = jiffies;
7812 int status;
7813
7814 nfs_fattr_init(&locations->fattr);
7815 locations->server = server;
7816 locations->nlocations = 0;
7817
Anna Schumakerfba83f32018-05-04 16:22:50 -04007818 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Leverb03d7352013-10-17 14:12:50 -04007819 status = nfs4_call_sync_sequence(clnt, server, &msg,
7820 &args.seq_args, &res.seq_res);
7821 if (status)
7822 return status;
7823
7824 renew_lease(server, now);
7825 return 0;
7826}
7827
7828#ifdef CONFIG_NFS_V4_1
7829
7830/*
7831 * This operation also signals the server that this client is
7832 * performing migration recovery. The server can stop asserting
7833 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
7834 * performing this operation is identified in the SEQUENCE
7835 * operation in this compound.
7836 *
7837 * When the client supports GETATTR(fs_locations_info), it can
7838 * be plumbed in here.
7839 */
7840static int _nfs41_proc_get_locations(struct inode *inode,
7841 struct nfs4_fs_locations *locations,
NeilBrowna52458b2018-12-03 11:30:31 +11007842 struct page *page, const struct cred *cred)
Chuck Leverb03d7352013-10-17 14:12:50 -04007843{
7844 struct nfs_server *server = NFS_SERVER(inode);
7845 struct rpc_clnt *clnt = server->client;
7846 u32 bitmask[2] = {
7847 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7848 };
7849 struct nfs4_fs_locations_arg args = {
7850 .fh = NFS_FH(inode),
7851 .page = page,
7852 .bitmask = bitmask,
7853 .migration = 1, /* skip LOOKUP */
7854 };
7855 struct nfs4_fs_locations_res res = {
7856 .fs_locations = locations,
7857 .migration = 1,
7858 };
7859 struct rpc_message msg = {
7860 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7861 .rpc_argp = &args,
7862 .rpc_resp = &res,
7863 .rpc_cred = cred,
7864 };
7865 int status;
7866
7867 nfs_fattr_init(&locations->fattr);
7868 locations->server = server;
7869 locations->nlocations = 0;
7870
Anna Schumakerfba83f32018-05-04 16:22:50 -04007871 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Leverb03d7352013-10-17 14:12:50 -04007872 status = nfs4_call_sync_sequence(clnt, server, &msg,
7873 &args.seq_args, &res.seq_res);
7874 if (status == NFS4_OK &&
7875 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7876 status = -NFS4ERR_LEASE_MOVED;
7877 return status;
7878}
7879
7880#endif /* CONFIG_NFS_V4_1 */
7881
7882/**
7883 * nfs4_proc_get_locations - discover locations for a migrated FSID
7884 * @inode: inode on FSID that is migrating
7885 * @locations: result of query
7886 * @page: buffer
7887 * @cred: credential to use for this operation
7888 *
7889 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
7890 * operation failed, or a negative errno if a local error occurred.
7891 *
7892 * On success, "locations" is filled in, but if the server has
7893 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
7894 * asserted.
7895 *
7896 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
7897 * from this client that require migration recovery.
7898 */
7899int nfs4_proc_get_locations(struct inode *inode,
7900 struct nfs4_fs_locations *locations,
NeilBrowna52458b2018-12-03 11:30:31 +11007901 struct page *page, const struct cred *cred)
Chuck Leverb03d7352013-10-17 14:12:50 -04007902{
7903 struct nfs_server *server = NFS_SERVER(inode);
7904 struct nfs_client *clp = server->nfs_client;
7905 const struct nfs4_mig_recovery_ops *ops =
7906 clp->cl_mvops->mig_recovery_ops;
Trond Myklebust0688e642019-04-07 13:59:09 -04007907 struct nfs4_exception exception = {
7908 .interruptible = true,
7909 };
Chuck Leverb03d7352013-10-17 14:12:50 -04007910 int status;
7911
7912 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7913 (unsigned long long)server->fsid.major,
7914 (unsigned long long)server->fsid.minor,
7915 clp->cl_hostname);
7916 nfs_display_fhandle(NFS_FH(inode), __func__);
7917
7918 do {
7919 status = ops->get_locations(inode, locations, page, cred);
7920 if (status != -NFS4ERR_DELAY)
7921 break;
7922 nfs4_handle_exception(server, status, &exception);
7923 } while (exception.retry);
7924 return status;
7925}
7926
Chuck Lever44c99932013-10-17 14:13:30 -04007927/*
7928 * This operation also signals the server that this client is
7929 * performing "lease moved" recovery. The server can stop
7930 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
7931 * is appended to this compound to identify the client ID which is
7932 * performing recovery.
7933 */
NeilBrowna52458b2018-12-03 11:30:31 +11007934static int _nfs40_proc_fsid_present(struct inode *inode, const struct cred *cred)
Chuck Lever44c99932013-10-17 14:13:30 -04007935{
7936 struct nfs_server *server = NFS_SERVER(inode);
7937 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
7938 struct rpc_clnt *clnt = server->client;
7939 struct nfs4_fsid_present_arg args = {
7940 .fh = NFS_FH(inode),
7941 .clientid = clp->cl_clientid,
7942 .renew = 1, /* append RENEW */
7943 };
7944 struct nfs4_fsid_present_res res = {
7945 .renew = 1,
7946 };
7947 struct rpc_message msg = {
7948 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7949 .rpc_argp = &args,
7950 .rpc_resp = &res,
7951 .rpc_cred = cred,
7952 };
7953 unsigned long now = jiffies;
7954 int status;
7955
7956 res.fh = nfs_alloc_fhandle();
7957 if (res.fh == NULL)
7958 return -ENOMEM;
7959
Anna Schumakerfba83f32018-05-04 16:22:50 -04007960 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Lever44c99932013-10-17 14:13:30 -04007961 status = nfs4_call_sync_sequence(clnt, server, &msg,
7962 &args.seq_args, &res.seq_res);
7963 nfs_free_fhandle(res.fh);
7964 if (status)
7965 return status;
7966
7967 do_renew_lease(clp, now);
7968 return 0;
7969}
7970
7971#ifdef CONFIG_NFS_V4_1
7972
7973/*
7974 * This operation also signals the server that this client is
7975 * performing "lease moved" recovery. The server can stop asserting
7976 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
7977 * this operation is identified in the SEQUENCE operation in this
7978 * compound.
7979 */
NeilBrowna52458b2018-12-03 11:30:31 +11007980static int _nfs41_proc_fsid_present(struct inode *inode, const struct cred *cred)
Chuck Lever44c99932013-10-17 14:13:30 -04007981{
7982 struct nfs_server *server = NFS_SERVER(inode);
7983 struct rpc_clnt *clnt = server->client;
7984 struct nfs4_fsid_present_arg args = {
7985 .fh = NFS_FH(inode),
7986 };
7987 struct nfs4_fsid_present_res res = {
7988 };
7989 struct rpc_message msg = {
7990 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7991 .rpc_argp = &args,
7992 .rpc_resp = &res,
7993 .rpc_cred = cred,
7994 };
7995 int status;
7996
7997 res.fh = nfs_alloc_fhandle();
7998 if (res.fh == NULL)
7999 return -ENOMEM;
8000
Anna Schumakerfba83f32018-05-04 16:22:50 -04008001 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Lever44c99932013-10-17 14:13:30 -04008002 status = nfs4_call_sync_sequence(clnt, server, &msg,
8003 &args.seq_args, &res.seq_res);
8004 nfs_free_fhandle(res.fh);
8005 if (status == NFS4_OK &&
8006 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
8007 status = -NFS4ERR_LEASE_MOVED;
8008 return status;
8009}
8010
8011#endif /* CONFIG_NFS_V4_1 */
8012
8013/**
8014 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
8015 * @inode: inode on FSID to check
8016 * @cred: credential to use for this operation
8017 *
8018 * Server indicates whether the FSID is present, moved, or not
8019 * recognized. This operation is necessary to clear a LEASE_MOVED
8020 * condition for this client ID.
8021 *
8022 * Returns NFS4_OK if the FSID is present on this server,
8023 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
8024 * NFS4ERR code if some error occurred on the server, or a
8025 * negative errno if a local failure occurred.
8026 */
NeilBrowna52458b2018-12-03 11:30:31 +11008027int nfs4_proc_fsid_present(struct inode *inode, const struct cred *cred)
Chuck Lever44c99932013-10-17 14:13:30 -04008028{
8029 struct nfs_server *server = NFS_SERVER(inode);
8030 struct nfs_client *clp = server->nfs_client;
8031 const struct nfs4_mig_recovery_ops *ops =
8032 clp->cl_mvops->mig_recovery_ops;
Trond Myklebust0688e642019-04-07 13:59:09 -04008033 struct nfs4_exception exception = {
8034 .interruptible = true,
8035 };
Chuck Lever44c99932013-10-17 14:13:30 -04008036 int status;
8037
8038 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
8039 (unsigned long long)server->fsid.major,
8040 (unsigned long long)server->fsid.minor,
8041 clp->cl_hostname);
8042 nfs_display_fhandle(NFS_FH(inode), __func__);
8043
8044 do {
8045 status = ops->fsid_present(inode, cred);
8046 if (status != -NFS4ERR_DELAY)
8047 break;
8048 nfs4_handle_exception(server, status, &exception);
8049 } while (exception.retry);
8050 return status;
8051}
8052
Trond Myklebust302fad72019-02-18 13:32:38 -05008053/*
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04008054 * If 'use_integrity' is true and the state managment nfs_client
8055 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
8056 * and the machine credential as per RFC3530bis and RFC5661 Security
8057 * Considerations sections. Otherwise, just use the user cred with the
8058 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04008059 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04008060static 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 +00008061{
8062 int status;
Anna Schumaker50493362019-08-14 15:30:16 -04008063 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
8064 struct nfs_client *clp = NFS_SERVER(dir)->nfs_client;
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00008065 struct nfs4_secinfo_arg args = {
8066 .dir_fh = NFS_FH(dir),
8067 .name = name,
8068 };
8069 struct nfs4_secinfo_res res = {
8070 .flavors = flavors,
8071 };
8072 struct rpc_message msg = {
8073 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
8074 .rpc_argp = &args,
8075 .rpc_resp = &res,
8076 };
Anna Schumaker50493362019-08-14 15:30:16 -04008077 struct nfs4_call_sync_data data = {
8078 .seq_server = NFS_SERVER(dir),
8079 .seq_args = &args.seq_args,
8080 .seq_res = &res.seq_res,
8081 };
8082 struct rpc_task_setup task_setup = {
8083 .rpc_client = clnt,
8084 .rpc_message = &msg,
8085 .callback_ops = clp->cl_mvops->call_sync_ops,
8086 .callback_data = &data,
8087 .flags = RPC_TASK_NO_ROUND_ROBIN,
8088 };
NeilBrowna52458b2018-12-03 11:30:31 +11008089 const struct cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04008090
8091 if (use_integrity) {
Anna Schumaker50493362019-08-14 15:30:16 -04008092 clnt = clp->cl_rpcclient;
8093 task_setup.rpc_client = clnt;
8094
8095 cred = nfs4_get_clid_cred(clp);
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008096 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04008097 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00008098
8099 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008100
Anna Schumaker50493362019-08-14 15:30:16 -04008101 nfs4_state_protect(clp, NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
8102 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
8103 status = nfs4_call_sync_custom(&task_setup);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008104
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00008105 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04008106
NeilBrowna52458b2018-12-03 11:30:31 +11008107 put_cred(cred);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00008108 return status;
8109}
8110
Bryan Schumaker72de53e2012-04-27 13:27:40 -04008111int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
8112 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00008113{
Trond Myklebust0688e642019-04-07 13:59:09 -04008114 struct nfs4_exception exception = {
8115 .interruptible = true,
8116 };
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00008117 int err;
8118 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04008119 err = -NFS4ERR_WRONGSEC;
8120
8121 /* try to use integrity protection with machine cred */
8122 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
8123 err = _nfs4_proc_secinfo(dir, name, flavors, true);
8124
8125 /*
8126 * if unable to use integrity protection, or SECINFO with
8127 * integrity protection returns NFS4ERR_WRONGSEC (which is
8128 * disallowed by spec, but exists in deployed servers) use
8129 * the current filesystem's rpc_client and the user cred.
8130 */
8131 if (err == -NFS4ERR_WRONGSEC)
8132 err = _nfs4_proc_secinfo(dir, name, flavors, false);
8133
Trond Myklebust078ea3d2013-08-12 16:45:55 -04008134 trace_nfs4_secinfo(dir, name, err);
8135 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00008136 &exception);
8137 } while (exception.retry);
8138 return err;
8139}
8140
Andy Adamson557134a2009-04-01 09:21:53 -04008141#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04008142/*
Andy Adamson357f54d2010-12-14 10:11:57 -05008143 * Check the exchange flags returned by the server for invalid flags, having
8144 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
8145 * DS flags set.
8146 */
Olga Kornievskaia8c390762020-10-16 09:25:45 -04008147static int nfs4_check_cl_exchange_flags(u32 flags, u32 version)
Andy Adamson357f54d2010-12-14 10:11:57 -05008148{
Olga Kornievskaia8c390762020-10-16 09:25:45 -04008149 if (version >= 2 && (flags & ~EXCHGID4_2_FLAG_MASK_R))
8150 goto out_inval;
8151 else if (version < 2 && (flags & ~EXCHGID4_FLAG_MASK_R))
Andy Adamson357f54d2010-12-14 10:11:57 -05008152 goto out_inval;
8153 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
8154 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
8155 goto out_inval;
8156 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
8157 goto out_inval;
8158 return NFS_OK;
8159out_inval:
8160 return -NFS4ERR_INVAL;
8161}
8162
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04008163static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04008164nfs41_same_server_scope(struct nfs41_server_scope *a,
8165 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04008166{
Anna Schumaker49ad0142017-01-11 16:51:59 -05008167 if (a->server_scope_sz != b->server_scope_sz)
8168 return false;
8169 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04008170}
8171
Andy Adamson02a95de2016-02-05 16:08:37 -05008172static void
8173nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
8174{
Trond Myklebust5c441542019-11-13 08:34:00 +01008175 struct nfs41_bind_conn_to_session_args *args = task->tk_msg.rpc_argp;
Olga Kornievskaiadff58532020-04-24 17:45:50 -04008176 struct nfs41_bind_conn_to_session_res *res = task->tk_msg.rpc_resp;
Trond Myklebust5c441542019-11-13 08:34:00 +01008177 struct nfs_client *clp = args->client;
8178
8179 switch (task->tk_status) {
8180 case -NFS4ERR_BADSESSION:
8181 case -NFS4ERR_DEADSESSION:
8182 nfs4_schedule_session_recovery(clp->cl_session,
8183 task->tk_status);
8184 }
Olga Kornievskaiadff58532020-04-24 17:45:50 -04008185 if (args->dir == NFS4_CDFC4_FORE_OR_BOTH &&
8186 res->dir != NFS4_CDFS4_BOTH) {
8187 rpc_task_close_connection(task);
8188 if (args->retries++ < MAX_BIND_CONN_TO_SESSION_RETRIES)
8189 rpc_restart_call(task);
8190 }
Andy Adamson02a95de2016-02-05 16:08:37 -05008191}
8192
8193static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
Olga Kornievskaia1c709b72020-04-26 11:30:00 -04008194 .rpc_call_done = nfs4_bind_one_conn_to_session_done,
Andy Adamson02a95de2016-02-05 16:08:37 -05008195};
8196
Andy Adamson357f54d2010-12-14 10:11:57 -05008197/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008198 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008199 *
8200 * The 4.1 client currently uses the same TCP connection for the
8201 * fore and backchannel.
8202 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008203static
8204int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
8205 struct rpc_xprt *xprt,
8206 struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008207 const struct cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008208{
8209 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08008210 struct nfs41_bind_conn_to_session_args args = {
8211 .client = clp,
8212 .dir = NFS4_CDFC4_FORE_OR_BOTH,
Olga Kornievskaiadff58532020-04-24 17:45:50 -04008213 .retries = 0,
Trond Myklebust71a097c2015-02-18 09:27:18 -08008214 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008215 struct nfs41_bind_conn_to_session_res res;
8216 struct rpc_message msg = {
8217 .rpc_proc =
8218 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08008219 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008220 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04008221 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008222 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008223 struct rpc_task_setup task_setup_data = {
8224 .rpc_client = clnt,
8225 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05008226 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008227 .rpc_message = &msg,
8228 .flags = RPC_TASK_TIMEOUT,
8229 };
8230 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008231
Trond Myklebust71a097c2015-02-18 09:27:18 -08008232 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
8233 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
8234 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008235
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008236 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
8237 if (xprt != rcu_access_pointer(clnt->cl_xprt))
8238 args.dir = NFS4_CDFC4_FORE;
8239
8240 task = rpc_run_task(&task_setup_data);
8241 if (!IS_ERR(task)) {
8242 status = task->tk_status;
8243 rpc_put_task(task);
8244 } else
8245 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008246 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008247 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08008248 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008249 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
8250 dprintk("NFS: %s: Session ID mismatch\n", __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04008251 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008252 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08008253 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008254 dprintk("NFS: %s: Unexpected direction from server\n",
8255 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04008256 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008257 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08008258 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008259 dprintk("NFS: %s: Server returned RDMA mode = true\n",
8260 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04008261 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008262 }
8263 }
Anna Schumakerc7ae7632017-04-07 14:15:21 -04008264
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008265 return status;
8266}
8267
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008268struct rpc_bind_conn_calldata {
8269 struct nfs_client *clp;
NeilBrowna52458b2018-12-03 11:30:31 +11008270 const struct cred *cred;
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008271};
8272
8273static int
8274nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
8275 struct rpc_xprt *xprt,
8276 void *calldata)
8277{
8278 struct rpc_bind_conn_calldata *p = calldata;
8279
8280 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
8281}
8282
NeilBrowna52458b2018-12-03 11:30:31 +11008283int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, const struct cred *cred)
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008284{
8285 struct rpc_bind_conn_calldata data = {
8286 .clp = clp,
8287 .cred = cred,
8288 };
8289 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
8290 nfs4_proc_bind_conn_to_session_callback, &data);
8291}
8292
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008293/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04008294 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
8295 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04008296 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008297static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
8298 .how = SP4_MACH_CRED,
8299 .enforce.u.words = {
8300 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
8301 1 << (OP_EXCHANGE_ID - 32) |
8302 1 << (OP_CREATE_SESSION - 32) |
8303 1 << (OP_DESTROY_SESSION - 32) |
8304 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04008305 },
8306 .allow.u.words = {
8307 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05008308 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04008309 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05008310 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04008311 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008312 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008313 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05008314 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008315 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04008316 1 << (OP_FREE_STATEID - 32) |
8317 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008318 }
8319};
8320
8321/*
8322 * Select the state protection mode for client `clp' given the server results
8323 * from exchange_id in `sp'.
8324 *
8325 * Returns 0 on success, negative errno otherwise.
8326 */
8327static int nfs4_sp4_select_mode(struct nfs_client *clp,
8328 struct nfs41_state_protection *sp)
8329{
8330 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
8331 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
8332 1 << (OP_EXCHANGE_ID - 32) |
8333 1 << (OP_CREATE_SESSION - 32) |
8334 1 << (OP_DESTROY_SESSION - 32) |
8335 1 << (OP_DESTROY_CLIENTID - 32)
8336 };
Trond Myklebust937e3132017-08-01 07:32:50 -04008337 unsigned long flags = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008338 unsigned int i;
Trond Myklebust937e3132017-08-01 07:32:50 -04008339 int ret = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008340
8341 if (sp->how == SP4_MACH_CRED) {
8342 /* Print state protect result */
8343 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
8344 for (i = 0; i <= LAST_NFS4_OP; i++) {
8345 if (test_bit(i, sp->enforce.u.longs))
8346 dfprintk(MOUNT, " enforce op %d\n", i);
8347 if (test_bit(i, sp->allow.u.longs))
8348 dfprintk(MOUNT, " allow op %d\n", i);
8349 }
8350
8351 /* make sure nothing is on enforce list that isn't supported */
8352 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
8353 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
8354 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008355 ret = -EINVAL;
8356 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008357 }
8358 }
8359
8360 /*
8361 * Minimal mode - state operations are allowed to use machine
8362 * credential. Note this already happens by default, so the
8363 * client doesn't have to do anything more than the negotiation.
8364 *
8365 * NOTE: we don't care if EXCHANGE_ID is in the list -
8366 * we're already using the machine cred for exchange_id
8367 * and will never use a different cred.
8368 */
8369 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
8370 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
8371 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
8372 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
8373 dfprintk(MOUNT, "sp4_mach_cred:\n");
8374 dfprintk(MOUNT, " minimal mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008375 __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008376 } else {
8377 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008378 ret = -EINVAL;
8379 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008380 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04008381
8382 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05008383 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
8384 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04008385 test_bit(OP_LOCKU, sp->allow.u.longs)) {
8386 dfprintk(MOUNT, " cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008387 __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04008388 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008389
Andrew Elble99ade3c2015-12-02 09:39:51 -05008390 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
8391 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008392 __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
Andrew Elble99ade3c2015-12-02 09:39:51 -05008393 }
8394
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008395 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
8396 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
8397 dfprintk(MOUNT, " secinfo mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008398 __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008399 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008400
8401 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
8402 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
8403 dfprintk(MOUNT, " stateid mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008404 __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008405 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04008406
8407 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
8408 dfprintk(MOUNT, " write mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008409 __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04008410 }
8411
8412 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
8413 dfprintk(MOUNT, " commit mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008414 __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04008415 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008416 }
Trond Myklebust937e3132017-08-01 07:32:50 -04008417out:
8418 clp->cl_sp4_flags = flags;
Wei Yongjun72bf75c2018-08-02 05:42:04 +00008419 return ret;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008420}
8421
Andy Adamson8d89bd72016-09-09 09:22:18 -04008422struct nfs41_exchange_id_data {
8423 struct nfs41_exchange_id_res res;
8424 struct nfs41_exchange_id_args args;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008425};
8426
Andy Adamson8d89bd72016-09-09 09:22:18 -04008427static void nfs4_exchange_id_release(void *data)
8428{
8429 struct nfs41_exchange_id_data *cdata =
8430 (struct nfs41_exchange_id_data *)data;
8431
Olga Kornievskaia63513232017-03-13 10:36:19 -04008432 nfs_put_client(cdata->args.client);
Andy Adamson8d89bd72016-09-09 09:22:18 -04008433 kfree(cdata->res.impl_id);
8434 kfree(cdata->res.server_scope);
8435 kfree(cdata->res.server_owner);
8436 kfree(cdata);
8437}
8438
8439static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
Andy Adamson8d89bd72016-09-09 09:22:18 -04008440 .rpc_release = nfs4_exchange_id_release,
8441};
8442
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008443/*
8444 * _nfs4_proc_exchange_id()
8445 *
8446 * Wrapper for EXCHANGE_ID operation.
8447 */
Trond Myklebust9c760d12017-07-31 18:38:50 -04008448static struct rpc_task *
NeilBrowna52458b2018-12-03 11:30:31 +11008449nfs4_run_exchange_id(struct nfs_client *clp, const struct cred *cred,
Andy Adamsonad0849a2016-09-09 09:22:28 -04008450 u32 sp4_how, struct rpc_xprt *xprt)
Benny Halevy99fe60d2009-04-01 09:22:29 -04008451{
Benny Halevy99fe60d2009-04-01 09:22:29 -04008452 struct rpc_message msg = {
8453 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
Benny Halevy99fe60d2009-04-01 09:22:29 -04008454 .rpc_cred = cred,
8455 };
Andy Adamson8d89bd72016-09-09 09:22:18 -04008456 struct rpc_task_setup task_setup_data = {
8457 .rpc_client = clp->cl_rpcclient,
8458 .callback_ops = &nfs4_exchange_id_call_ops,
8459 .rpc_message = &msg,
NeilBrown5a0c2572019-05-30 10:41:28 +10008460 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
Andy Adamson8d89bd72016-09-09 09:22:18 -04008461 };
8462 struct nfs41_exchange_id_data *calldata;
Anna Schumakere917f0d2017-04-07 14:15:22 -04008463 int status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008464
Elena Reshetova212bf412017-10-20 12:53:38 +03008465 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust9c760d12017-07-31 18:38:50 -04008466 return ERR_PTR(-EIO);
Andy Adamson8d89bd72016-09-09 09:22:18 -04008467
Trond Myklebust9c760d12017-07-31 18:38:50 -04008468 status = -ENOMEM;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008469 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust9c760d12017-07-31 18:38:50 -04008470 if (!calldata)
8471 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04008472
Trond Myklebustfd405592017-08-01 16:02:47 -04008473 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04008474
8475 status = nfs4_init_uniform_client_string(clp);
8476 if (status)
Andy Adamson8d89bd72016-09-09 09:22:18 -04008477 goto out_calldata;
Jeff Layton3a6bb732015-06-09 19:43:57 -04008478
Andy Adamson8d89bd72016-09-09 09:22:18 -04008479 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
8480 GFP_NOFS);
8481 status = -ENOMEM;
8482 if (unlikely(calldata->res.server_owner == NULL))
8483 goto out_calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04008484
Andy Adamson8d89bd72016-09-09 09:22:18 -04008485 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04008486 GFP_NOFS);
Andy Adamson8d89bd72016-09-09 09:22:18 -04008487 if (unlikely(calldata->res.server_scope == NULL))
Chuck Leveracdeb692012-05-21 22:46:16 -04008488 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04008489
Andy Adamson8d89bd72016-09-09 09:22:18 -04008490 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
8491 if (unlikely(calldata->res.impl_id == NULL))
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05008492 goto out_server_scope;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05008493
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008494 switch (sp4_how) {
8495 case SP4_NONE:
Andy Adamson8d89bd72016-09-09 09:22:18 -04008496 calldata->args.state_protect.how = SP4_NONE;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008497 break;
8498
8499 case SP4_MACH_CRED:
Andy Adamson8d89bd72016-09-09 09:22:18 -04008500 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008501 break;
8502
8503 default:
8504 /* unsupported! */
8505 WARN_ON_ONCE(1);
8506 status = -EINVAL;
Kinglong Mee6b559702015-07-01 11:54:53 +08008507 goto out_impl_id;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008508 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04008509 if (xprt) {
Andy Adamsonad0849a2016-09-09 09:22:28 -04008510 task_setup_data.rpc_xprt = xprt;
Trond Myklebustd9cb7332017-08-01 16:02:48 -04008511 task_setup_data.flags |= RPC_TASK_SOFTCONN;
Trond Myklebustfd405592017-08-01 16:02:47 -04008512 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
8513 sizeof(calldata->args.verifier.data));
Andy Adamsonad0849a2016-09-09 09:22:28 -04008514 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04008515 calldata->args.client = clp;
Trond Myklebustbfab2812017-08-01 08:17:34 -04008516 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
8517 EXCHGID4_FLAG_BIND_PRINC_STATEID;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008518#ifdef CONFIG_NFS_V4_1_MIGRATION
Trond Myklebustbfab2812017-08-01 08:17:34 -04008519 calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008520#endif
8521 msg.rpc_argp = &calldata->args;
8522 msg.rpc_resp = &calldata->res;
8523 task_setup_data.callback_data = calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04008524
Trond Myklebust9c760d12017-07-31 18:38:50 -04008525 return rpc_run_task(&task_setup_data);
Andy Adamson8d89bd72016-09-09 09:22:18 -04008526
8527out_impl_id:
8528 kfree(calldata->res.impl_id);
8529out_server_scope:
8530 kfree(calldata->res.server_scope);
8531out_server_owner:
8532 kfree(calldata->res.server_owner);
8533out_calldata:
8534 kfree(calldata);
Trond Myklebust9c760d12017-07-31 18:38:50 -04008535out:
Olga Kornievskaia63513232017-03-13 10:36:19 -04008536 nfs_put_client(clp);
Trond Myklebust9c760d12017-07-31 18:38:50 -04008537 return ERR_PTR(status);
8538}
8539
8540/*
8541 * _nfs4_proc_exchange_id()
8542 *
8543 * Wrapper for EXCHANGE_ID operation.
8544 */
NeilBrowna52458b2018-12-03 11:30:31 +11008545static int _nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred,
Trond Myklebust9c760d12017-07-31 18:38:50 -04008546 u32 sp4_how)
8547{
8548 struct rpc_task *task;
8549 struct nfs41_exchange_id_args *argp;
8550 struct nfs41_exchange_id_res *resp;
Robert Milkowski7dc29932020-01-30 09:43:25 +00008551 unsigned long now = jiffies;
Trond Myklebust9c760d12017-07-31 18:38:50 -04008552 int status;
8553
8554 task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
8555 if (IS_ERR(task))
8556 return PTR_ERR(task);
8557
8558 argp = task->tk_msg.rpc_argp;
8559 resp = task->tk_msg.rpc_resp;
8560 status = task->tk_status;
8561 if (status != 0)
8562 goto out;
8563
Olga Kornievskaia8c390762020-10-16 09:25:45 -04008564 status = nfs4_check_cl_exchange_flags(resp->flags,
8565 clp->cl_mvops->minor_version);
Trond Myklebust9c760d12017-07-31 18:38:50 -04008566 if (status != 0)
8567 goto out;
8568
8569 status = nfs4_sp4_select_mode(clp, &resp->state_protect);
8570 if (status != 0)
8571 goto out;
8572
Robert Milkowski7dc29932020-01-30 09:43:25 +00008573 do_renew_lease(clp, now);
8574
Trond Myklebust9c760d12017-07-31 18:38:50 -04008575 clp->cl_clientid = resp->clientid;
8576 clp->cl_exchange_flags = resp->flags;
8577 clp->cl_seqid = resp->seqid;
8578 /* Client ID is not confirmed */
8579 if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
8580 clear_bit(NFS4_SESSION_ESTABLISHED,
8581 &clp->cl_session->session_state);
8582
8583 if (clp->cl_serverscope != NULL &&
8584 !nfs41_same_server_scope(clp->cl_serverscope,
8585 resp->server_scope)) {
8586 dprintk("%s: server_scope mismatch detected\n",
8587 __func__);
8588 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
8589 }
8590
8591 swap(clp->cl_serverowner, resp->server_owner);
8592 swap(clp->cl_serverscope, resp->server_scope);
8593 swap(clp->cl_implid, resp->impl_id);
8594
8595 /* Save the EXCHANGE_ID verifier session trunk tests */
8596 memcpy(clp->cl_confirm.data, argp->verifier.data,
8597 sizeof(clp->cl_confirm.data));
8598out:
8599 trace_nfs4_exchange_id(clp, status);
8600 rpc_put_task(task);
8601 return status;
Benny Halevy99fe60d2009-04-01 09:22:29 -04008602}
8603
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008604/*
8605 * nfs4_proc_exchange_id()
8606 *
8607 * Returns zero, a negative errno, or a negative NFS4ERR status code.
8608 *
8609 * Since the clientid has expired, all compounds using sessions
8610 * associated with the stale clientid will be returning
8611 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
8612 * be in some phase of session reset.
8613 *
8614 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
8615 */
NeilBrowna52458b2018-12-03 11:30:31 +11008616int nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008617{
8618 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
8619 int status;
8620
8621 /* try SP4_MACH_CRED if krb5i/p */
8622 if (authflavor == RPC_AUTH_GSS_KRB5I ||
8623 authflavor == RPC_AUTH_GSS_KRB5P) {
Trond Myklebust9c760d12017-07-31 18:38:50 -04008624 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008625 if (!status)
8626 return 0;
8627 }
8628
8629 /* try SP4_NONE */
Trond Myklebust9c760d12017-07-31 18:38:50 -04008630 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008631}
8632
Andy Adamson04fa2c62016-09-09 09:22:29 -04008633/**
8634 * nfs4_test_session_trunk
8635 *
8636 * This is an add_xprt_test() test function called from
8637 * rpc_clnt_setup_test_and_add_xprt.
8638 *
8639 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
8640 * and is dereferrenced in nfs4_exchange_id_release
8641 *
8642 * Upon success, add the new transport to the rpc_clnt
8643 *
8644 * @clnt: struct rpc_clnt to get new transport
8645 * @xprt: the rpc_xprt to test
8646 * @data: call data for _nfs4_proc_exchange_id.
8647 */
Santosh kumar pradhan10e037d2018-12-19 12:29:57 +05308648void nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
Andy Adamson04fa2c62016-09-09 09:22:29 -04008649 void *data)
8650{
8651 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
Trond Myklebust9c760d12017-07-31 18:38:50 -04008652 struct rpc_task *task;
8653 int status;
8654
Andy Adamson04fa2c62016-09-09 09:22:29 -04008655 u32 sp4_how;
8656
8657 dprintk("--> %s try %s\n", __func__,
8658 xprt->address_strings[RPC_DISPLAY_ADDR]);
8659
8660 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
8661
8662 /* Test connection for session trunking. Async exchange_id call */
Trond Myklebust9c760d12017-07-31 18:38:50 -04008663 task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
8664 if (IS_ERR(task))
Santosh kumar pradhan10e037d2018-12-19 12:29:57 +05308665 return;
Trond Myklebust9c760d12017-07-31 18:38:50 -04008666
8667 status = task->tk_status;
8668 if (status == 0)
8669 status = nfs4_detect_session_trunking(adata->clp,
8670 task->tk_msg.rpc_resp, xprt);
8671
Santosh kumar pradhan10e037d2018-12-19 12:29:57 +05308672 if (status == 0)
8673 rpc_clnt_xprt_switch_add_xprt(clnt, xprt);
8674
Trond Myklebust9c760d12017-07-31 18:38:50 -04008675 rpc_put_task(task);
Andy Adamson04fa2c62016-09-09 09:22:29 -04008676}
8677EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
8678
Trond Myklebust66245532012-05-25 17:18:09 -04008679static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008680 const struct cred *cred)
Trond Myklebust66245532012-05-25 17:18:09 -04008681{
8682 struct rpc_message msg = {
8683 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
8684 .rpc_argp = clp,
8685 .rpc_cred = cred,
8686 };
8687 int status;
8688
NeilBrown5a0c2572019-05-30 10:41:28 +10008689 status = rpc_call_sync(clp->cl_rpcclient, &msg,
8690 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008691 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04008692 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04008693 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04008694 "DESTROY_CLIENTID.", status, clp->cl_hostname);
8695 return status;
8696}
8697
8698static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008699 const struct cred *cred)
Trond Myklebust66245532012-05-25 17:18:09 -04008700{
8701 unsigned int loop;
8702 int ret;
8703
8704 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
8705 ret = _nfs4_proc_destroy_clientid(clp, cred);
8706 switch (ret) {
8707 case -NFS4ERR_DELAY:
8708 case -NFS4ERR_CLIENTID_BUSY:
8709 ssleep(1);
8710 break;
8711 default:
8712 return ret;
8713 }
8714 }
8715 return 0;
8716}
8717
8718int nfs4_destroy_clientid(struct nfs_client *clp)
8719{
NeilBrowna52458b2018-12-03 11:30:31 +11008720 const struct cred *cred;
Trond Myklebust66245532012-05-25 17:18:09 -04008721 int ret = 0;
8722
8723 if (clp->cl_mvops->minor_version < 1)
8724 goto out;
8725 if (clp->cl_exchange_flags == 0)
8726 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04008727 if (clp->cl_preserve_clid)
8728 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04008729 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04008730 ret = nfs4_proc_destroy_clientid(clp, cred);
NeilBrowna52458b2018-12-03 11:30:31 +11008731 put_cred(cred);
Trond Myklebust66245532012-05-25 17:18:09 -04008732 switch (ret) {
8733 case 0:
8734 case -NFS4ERR_STALE_CLIENTID:
8735 clp->cl_exchange_flags = 0;
8736 }
8737out:
8738 return ret;
8739}
8740
Donald Buczek0efb01b2019-07-07 21:26:08 +02008741#endif /* CONFIG_NFS_V4_1 */
8742
Andy Adamson2050f0c2009-04-01 09:22:30 -04008743struct nfs4_get_lease_time_data {
8744 struct nfs4_get_lease_time_args *args;
8745 struct nfs4_get_lease_time_res *res;
8746 struct nfs_client *clp;
8747};
8748
8749static void nfs4_get_lease_time_prepare(struct rpc_task *task,
8750 void *calldata)
8751{
Andy Adamson2050f0c2009-04-01 09:22:30 -04008752 struct nfs4_get_lease_time_data *data =
8753 (struct nfs4_get_lease_time_data *)calldata;
8754
8755 dprintk("--> %s\n", __func__);
8756 /* just setup sequence, do not trigger session recovery
8757 since we're invoked within one */
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008758 nfs4_setup_sequence(data->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008759 &data->args->la_seq_args,
8760 &data->res->lr_seq_res,
8761 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008762 dprintk("<-- %s\n", __func__);
8763}
8764
8765/*
8766 * Called from nfs4_state_manager thread for session setup, so don't recover
8767 * from sequence operation or clientid errors.
8768 */
8769static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
8770{
8771 struct nfs4_get_lease_time_data *data =
8772 (struct nfs4_get_lease_time_data *)calldata;
8773
8774 dprintk("--> %s\n", __func__);
Donald Buczek0efb01b2019-07-07 21:26:08 +02008775 if (!nfs4_sequence_done(task, &data->res->lr_seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04008776 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04008777 switch (task->tk_status) {
8778 case -NFS4ERR_DELAY:
8779 case -NFS4ERR_GRACE:
8780 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
8781 rpc_delay(task, NFS4_POLL_RETRY_MIN);
8782 task->tk_status = 0;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05008783 fallthrough;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008784 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008785 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008786 return;
8787 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04008788 dprintk("<-- %s\n", __func__);
8789}
8790
Trond Myklebust17280172012-03-11 13:11:00 -04008791static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04008792 .rpc_call_prepare = nfs4_get_lease_time_prepare,
8793 .rpc_call_done = nfs4_get_lease_time_done,
8794};
8795
8796int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
8797{
Andy Adamson2050f0c2009-04-01 09:22:30 -04008798 struct nfs4_get_lease_time_args args;
8799 struct nfs4_get_lease_time_res res = {
8800 .lr_fsinfo = fsinfo,
8801 };
8802 struct nfs4_get_lease_time_data data = {
8803 .args = &args,
8804 .res = &res,
8805 .clp = clp,
8806 };
8807 struct rpc_message msg = {
8808 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
8809 .rpc_argp = &args,
8810 .rpc_resp = &res,
8811 };
8812 struct rpc_task_setup task_setup = {
8813 .rpc_client = clp->cl_rpcclient,
8814 .rpc_message = &msg,
8815 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008816 .callback_data = &data,
8817 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04008818 };
Andy Adamson2050f0c2009-04-01 09:22:30 -04008819
Anna Schumakerfba83f32018-05-04 16:22:50 -04008820 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0, 1);
Anna Schumakerf836b272019-08-19 10:18:28 -04008821 return nfs4_call_sync_custom(&task_setup);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008822}
8823
Donald Buczek0efb01b2019-07-07 21:26:08 +02008824#ifdef CONFIG_NFS_V4_1
8825
Andy Adamsonfc931582009-04-01 09:22:31 -04008826/*
8827 * Initialize the values to be used by the client in CREATE_SESSION
8828 * If nfs4_init_session set the fore channel request and response sizes,
8829 * use them.
8830 *
8831 * Set the back channel max_resp_sz_cached to zero to force the client to
8832 * always set csa_cachethis to FALSE because the current implementation
8833 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
8834 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04008835static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
8836 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04008837{
Andy Adamson18aad3d2013-06-26 12:21:49 -04008838 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04008839 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Trond Myklebust7402a4f2019-07-16 13:51:29 -04008840 unsigned int max_bc_slots = rpc_num_bc_slots(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04008841
Andy Adamson18aad3d2013-06-26 12:21:49 -04008842 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
8843 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
8844
Andy Adamsonfc931582009-04-01 09:22:31 -04008845 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04008846 args->fc_attrs.max_rqst_sz = max_rqst_sz;
8847 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04008848 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05008849 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04008850
8851 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05008852 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04008853 __func__,
8854 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05008855 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04008856
8857 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04008858 args->bc_attrs.max_rqst_sz = max_bc_payload;
8859 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04008860 args->bc_attrs.max_resp_sz_cached = 0;
8861 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust62421cd2018-08-11 11:52:39 -04008862 args->bc_attrs.max_reqs = max_t(unsigned short, max_session_cb_slots, 1);
Trond Myklebust7402a4f2019-07-16 13:51:29 -04008863 if (args->bc_attrs.max_reqs > max_bc_slots)
8864 args->bc_attrs.max_reqs = max_bc_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04008865
8866 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
8867 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
8868 __func__,
8869 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
8870 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
8871 args->bc_attrs.max_reqs);
8872}
8873
Trond Myklebust79969dd2015-02-18 11:30:18 -08008874static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
8875 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008876{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008877 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008878 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008879
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008880 if (rcvd->max_resp_sz > sent->max_resp_sz)
8881 return -EINVAL;
8882 /*
8883 * Our requested max_ops is the minimum we need; we're not
8884 * prepared to break up compounds into smaller pieces than that.
8885 * So, no point even trying to continue if the server won't
8886 * cooperate:
8887 */
8888 if (rcvd->max_ops < sent->max_ops)
8889 return -EINVAL;
8890 if (rcvd->max_reqs == 0)
8891 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04008892 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
8893 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008894 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04008895}
8896
Trond Myklebust79969dd2015-02-18 11:30:18 -08008897static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
8898 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008899{
8900 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008901 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04008902
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008903 if (!(res->flags & SESSION4_BACK_CHAN))
8904 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008905 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
8906 return -EINVAL;
8907 if (rcvd->max_resp_sz < sent->max_resp_sz)
8908 return -EINVAL;
8909 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
8910 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008911 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008912 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008913 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008914 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008915out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008916 return 0;
8917}
Andy Adamson8d353012009-04-01 09:22:32 -04008918
Andy Adamson8d353012009-04-01 09:22:32 -04008919static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08008920 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008921{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008922 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04008923
Trond Myklebust79969dd2015-02-18 11:30:18 -08008924 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008925 if (ret)
8926 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008927 return nfs4_verify_back_channel_attrs(args, res);
8928}
8929
8930static void nfs4_update_session(struct nfs4_session *session,
8931 struct nfs41_create_session_res *res)
8932{
8933 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05008934 /* Mark client id and session as being confirmed */
8935 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
8936 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08008937 session->flags = res->flags;
8938 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008939 if (res->flags & SESSION4_BACK_CHAN)
8940 memcpy(&session->bc_attrs, &res->bc_attrs,
8941 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04008942}
8943
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008944static int _nfs4_proc_create_session(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008945 const struct cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008946{
8947 struct nfs4_session *session = clp->cl_session;
8948 struct nfs41_create_session_args args = {
8949 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08008950 .clientid = clp->cl_clientid,
8951 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04008952 .cb_program = NFS4_CALLBACK,
8953 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08008954 struct nfs41_create_session_res res;
8955
Andy Adamsonfc931582009-04-01 09:22:31 -04008956 struct rpc_message msg = {
8957 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
8958 .rpc_argp = &args,
8959 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008960 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04008961 };
8962 int status;
8963
Chuck Lever6b26cc82016-05-02 14:40:40 -04008964 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04008965 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04008966
NeilBrown5a0c2572019-05-30 10:41:28 +10008967 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
8968 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008969 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008970
Trond Myklebustb519d402016-09-11 14:50:01 -04008971 switch (status) {
8972 case -NFS4ERR_STALE_CLIENTID:
8973 case -NFS4ERR_DELAY:
8974 case -ETIMEDOUT:
8975 case -EACCES:
8976 case -EAGAIN:
8977 goto out;
zhengbin8b98a532019-12-19 18:34:47 +08008978 }
Trond Myklebustb519d402016-09-11 14:50:01 -04008979
8980 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05008981 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04008982 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08008983 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008984 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08008985 if (status)
8986 goto out;
8987 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008988 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08008989out:
Andy Adamsonfc931582009-04-01 09:22:31 -04008990 return status;
8991}
8992
8993/*
8994 * Issues a CREATE_SESSION operation to the server.
8995 * It is the responsibility of the caller to verify the session is
8996 * expired before calling this routine.
8997 */
NeilBrowna52458b2018-12-03 11:30:31 +11008998int nfs4_proc_create_session(struct nfs_client *clp, const struct cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008999{
9000 int status;
9001 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04009002 struct nfs4_session *session = clp->cl_session;
9003
9004 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
9005
Trond Myklebust848f5bd2012-05-25 17:51:23 -04009006 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04009007 if (status)
9008 goto out;
9009
Andy Adamsonaacd5532011-11-09 13:58:21 -05009010 /* Init or reset the session slot tables */
9011 status = nfs4_setup_session_slot_tables(session);
9012 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04009013 if (status)
9014 goto out;
9015
9016 ptr = (unsigned *)&session->sess_id.data[0];
9017 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
9018 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04009019out:
9020 dprintk("<-- %s\n", __func__);
9021 return status;
9022}
9023
Andy Adamson0f3e66c2009-04-01 09:22:34 -04009024/*
9025 * Issue the over-the-wire RPC DESTROY_SESSION.
9026 * The caller must serialize access to this routine.
9027 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04009028int nfs4_proc_destroy_session(struct nfs4_session *session,
NeilBrowna52458b2018-12-03 11:30:31 +11009029 const struct cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04009030{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04009031 struct rpc_message msg = {
9032 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
9033 .rpc_argp = session,
9034 .rpc_cred = cred,
9035 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04009036 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04009037
9038 dprintk("--> nfs4_proc_destroy_session\n");
9039
9040 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05009041 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
9042 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04009043
NeilBrown5a0c2572019-05-30 10:41:28 +10009044 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
9045 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04009046 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04009047
9048 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04009049 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04009050 "Session has been destroyed regardless...\n", status);
9051
9052 dprintk("<-- nfs4_proc_destroy_session\n");
9053 return status;
9054}
9055
Trond Myklebust7b38c362012-05-23 13:23:31 -04009056/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009057 * Renew the cl_session lease.
9058 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04009059struct nfs4_sequence_data {
9060 struct nfs_client *clp;
9061 struct nfs4_sequence_args args;
9062 struct nfs4_sequence_res res;
9063};
9064
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08009065static void nfs41_sequence_release(void *data)
9066{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04009067 struct nfs4_sequence_data *calldata = data;
9068 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08009069
Elena Reshetova212bf412017-10-20 12:53:38 +03009070 if (refcount_read(&clp->cl_count) > 1)
Alexandros Batsakis71358402010-02-05 03:45:05 -08009071 nfs4_schedule_state_renewal(clp);
9072 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04009073 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08009074}
9075
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009076static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
9077{
9078 switch(task->tk_status) {
9079 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009080 rpc_delay(task, NFS4_POLL_RETRY_MAX);
9081 return -EAGAIN;
9082 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05009083 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009084 }
9085 return 0;
9086}
9087
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08009088static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009089{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04009090 struct nfs4_sequence_data *calldata = data;
9091 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009092
Trond Myklebust14516c32010-07-31 14:29:06 -04009093 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
9094 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009095
Trond Myklebustc6d01c62013-08-09 11:51:26 -04009096 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009097 if (task->tk_status < 0) {
9098 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Elena Reshetova212bf412017-10-20 12:53:38 +03009099 if (refcount_read(&clp->cl_count) == 1)
Alexandros Batsakis71358402010-02-05 03:45:05 -08009100 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009101
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009102 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
9103 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009104 return;
9105 }
9106 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009107 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08009108out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009109 dprintk("<-- %s\n", __func__);
9110}
9111
9112static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
9113{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04009114 struct nfs4_sequence_data *calldata = data;
9115 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009116 struct nfs4_sequence_args *args;
9117 struct nfs4_sequence_res *res;
9118
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009119 args = task->tk_msg.rpc_argp;
9120 res = task->tk_msg.rpc_resp;
9121
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009122 nfs4_setup_sequence(clp, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009123}
9124
9125static const struct rpc_call_ops nfs41_sequence_ops = {
9126 .rpc_call_done = nfs41_sequence_call_done,
9127 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08009128 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009129};
9130
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009131static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11009132 const struct cred *cred,
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009133 struct nfs4_slot *slot,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009134 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009135{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04009136 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009137 struct rpc_message msg = {
9138 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
9139 .rpc_cred = cred,
9140 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009141 struct rpc_task_setup task_setup_data = {
9142 .rpc_client = clp->cl_rpcclient,
9143 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009144 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04009145 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009146 };
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009147 struct rpc_task *ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009148
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009149 ret = ERR_PTR(-EIO);
Elena Reshetova212bf412017-10-20 12:53:38 +03009150 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009151 goto out_err;
9152
9153 ret = ERR_PTR(-ENOMEM);
Benny Halevydfb4f3092010-09-24 09:17:01 -04009154 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009155 if (calldata == NULL)
9156 goto out_put_clp;
Anna Schumakerfba83f32018-05-04 16:22:50 -04009157 nfs4_init_sequence(&calldata->args, &calldata->res, 0, is_privileged);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009158 nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04009159 msg.rpc_argp = &calldata->args;
9160 msg.rpc_resp = &calldata->res;
9161 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009162 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009163
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009164 ret = rpc_run_task(&task_setup_data);
9165 if (IS_ERR(ret))
9166 goto out_err;
9167 return ret;
9168out_put_clp:
9169 nfs_put_client(clp);
9170out_err:
9171 nfs41_release_slot(slot);
9172 return ret;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009173}
9174
NeilBrowna52458b2018-12-03 11:30:31 +11009175static int nfs41_proc_async_sequence(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009176{
9177 struct rpc_task *task;
9178 int ret = 0;
9179
Trond Myklebust2f60ea62011-08-24 15:07:37 -04009180 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04009181 return -EAGAIN;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009182 task = _nfs41_proc_sequence(clp, cred, NULL, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009183 if (IS_ERR(task))
9184 ret = PTR_ERR(task);
9185 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08009186 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009187 dprintk("<-- %s status=%d\n", __func__, ret);
9188 return ret;
9189}
9190
NeilBrowna52458b2018-12-03 11:30:31 +11009191static int nfs4_proc_sequence(struct nfs_client *clp, const struct cred *cred)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009192{
9193 struct rpc_task *task;
9194 int ret;
9195
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009196 task = _nfs41_proc_sequence(clp, cred, NULL, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009197 if (IS_ERR(task)) {
9198 ret = PTR_ERR(task);
9199 goto out;
9200 }
9201 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04009202 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009203 ret = task->tk_status;
9204 rpc_put_task(task);
9205out:
9206 dprintk("<-- %s status=%d\n", __func__, ret);
9207 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009208}
9209
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009210struct nfs4_reclaim_complete_data {
9211 struct nfs_client *clp;
9212 struct nfs41_reclaim_complete_args arg;
9213 struct nfs41_reclaim_complete_res res;
9214};
9215
9216static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
9217{
9218 struct nfs4_reclaim_complete_data *calldata = data;
9219
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009220 nfs4_setup_sequence(calldata->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04009221 &calldata->arg.seq_args,
9222 &calldata->res.seq_res,
9223 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009224}
9225
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009226static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
9227{
9228 switch(task->tk_status) {
9229 case 0:
Jeff Layton57174592018-03-18 08:37:03 -04009230 wake_up_all(&clp->cl_lock_waitq);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05009231 fallthrough;
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009232 case -NFS4ERR_COMPLETE_ALREADY:
9233 case -NFS4ERR_WRONG_CRED: /* What to do here? */
9234 break;
9235 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009236 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05009237 fallthrough;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04009238 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009239 return -EAGAIN;
Trond Myklebust0048fdd2017-05-04 13:44:04 -04009240 case -NFS4ERR_BADSESSION:
9241 case -NFS4ERR_DEADSESSION:
9242 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
Trond Myklebust0048fdd2017-05-04 13:44:04 -04009243 break;
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009244 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05009245 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009246 }
9247 return 0;
9248}
9249
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009250static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
9251{
9252 struct nfs4_reclaim_complete_data *calldata = data;
9253 struct nfs_client *clp = calldata->clp;
9254 struct nfs4_sequence_res *res = &calldata->res.seq_res;
9255
9256 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04009257 if (!nfs41_sequence_done(task, res))
9258 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009259
Trond Myklebustc6d01c62013-08-09 11:51:26 -04009260 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009261 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
9262 rpc_restart_call_prepare(task);
9263 return;
9264 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009265 dprintk("<-- %s\n", __func__);
9266}
9267
9268static void nfs4_free_reclaim_complete_data(void *data)
9269{
9270 struct nfs4_reclaim_complete_data *calldata = data;
9271
9272 kfree(calldata);
9273}
9274
9275static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
9276 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
9277 .rpc_call_done = nfs4_reclaim_complete_done,
9278 .rpc_release = nfs4_free_reclaim_complete_data,
9279};
9280
9281/*
9282 * Issue a global reclaim complete.
9283 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04009284static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11009285 const struct cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009286{
9287 struct nfs4_reclaim_complete_data *calldata;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009288 struct rpc_message msg = {
9289 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04009290 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009291 };
9292 struct rpc_task_setup task_setup_data = {
9293 .rpc_client = clp->cl_rpcclient,
9294 .rpc_message = &msg,
9295 .callback_ops = &nfs4_reclaim_complete_call_ops,
Anna Schumaker4c952e32019-08-14 15:46:48 -04009296 .flags = RPC_TASK_NO_ROUND_ROBIN,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009297 };
9298 int status = -ENOMEM;
9299
9300 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04009301 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009302 if (calldata == NULL)
9303 goto out;
9304 calldata->clp = clp;
9305 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009306
Anna Schumakerfba83f32018-05-04 16:22:50 -04009307 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0, 1);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009308 msg.rpc_argp = &calldata->arg;
9309 msg.rpc_resp = &calldata->res;
9310 task_setup_data.callback_data = calldata;
Anna Schumaker4c952e32019-08-14 15:46:48 -04009311 status = nfs4_call_sync_custom(&task_setup_data);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009312out:
9313 dprintk("<-- %s status=%d\n", __func__, status);
9314 return status;
9315}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009316
9317static void
9318nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
9319{
9320 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00009321 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009322
9323 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009324 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
Jeff Layton183d9e72016-05-17 12:28:47 -04009325 &lgp->res.seq_res, task);
9326 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009327}
9328
9329static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
9330{
9331 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04009332
9333 dprintk("--> %s\n", __func__);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04009334 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04009335 dprintk("<-- %s\n", __func__);
9336}
9337
9338static int
9339nfs4_layoutget_handle_exception(struct rpc_task *task,
9340 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
9341{
Trond Myklebustee314c22012-10-01 17:25:48 -07009342 struct inode *inode = lgp->args.inode;
9343 struct nfs_server *server = NFS_SERVER(inode);
9344 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04009345 int nfs4err = task->tk_status;
9346 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04009347 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009348
Boaz Harroshed7e5422014-01-22 20:34:54 +02009349 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009350
Trond Myklebust2dbf8df2018-06-15 15:58:45 -04009351 nfs4_sequence_free_slot(&lgp->res.seq_res);
9352
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04009353 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009354 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07009355 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08009356
9357 /*
9358 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
9359 * on the file. set tk_status to -ENODATA to tell upper layer to
9360 * retry go inband.
9361 */
9362 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04009363 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08009364 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02009365 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07009366 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
9367 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
9368 */
9369 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04009370 status = -EOVERFLOW;
9371 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07009372 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02009373 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07009374 * (or clients) writing to the same RAID stripe except when
9375 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04009376 *
9377 * Treat it like we would RECALLCONFLICT -- we retry for a little
9378 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02009379 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009380 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04009381 if (lgp->args.minlength == 0) {
9382 status = -EOVERFLOW;
9383 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02009384 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04009385 status = -EBUSY;
9386 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04009387 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04009388 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04009389 break;
Trond Myklebust26f47442016-09-22 13:39:10 -04009390 case -NFS4ERR_DELEG_REVOKED:
9391 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustee314c22012-10-01 17:25:48 -07009392 case -NFS4ERR_EXPIRED:
9393 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04009394 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07009395 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04009396 lo = NFS_I(inode)->layout;
9397 /* If the open stateid was bad, then recover it. */
9398 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
Trond Myklebuste8fa33a2017-10-04 13:49:12 -04009399 !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07009400 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04009401 exception->state = lgp->args.ctx->state;
Trond Myklebust26f47442016-09-22 13:39:10 -04009402 exception->stateid = &lgp->args.stateid;
Trond Myklebust2259f962015-09-20 13:30:30 -04009403 break;
9404 }
Trond Myklebustee314c22012-10-01 17:25:48 -07009405
Trond Myklebustf7db0b22016-07-14 15:14:02 -04009406 /*
9407 * Mark the bad layout state as invalid, then retry
9408 */
Trond Myklebust668f4552016-07-24 17:08:59 -04009409 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04009410 spin_unlock(&inode->i_lock);
Trond Myklebust1f18b822017-04-29 10:10:17 -04009411 nfs_commit_inode(inode, 0);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04009412 pnfs_free_lseg_list(&head);
9413 status = -EAGAIN;
9414 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009415 }
Jeff Layton183d9e72016-05-17 12:28:47 -04009416
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04009417 err = nfs4_handle_exception(server, nfs4err, exception);
9418 if (!status) {
9419 if (exception->retry)
9420 status = -EAGAIN;
9421 else
9422 status = err;
9423 }
Trond Myklebustee314c22012-10-01 17:25:48 -07009424out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009425 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04009426 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009427}
9428
Fred Isamandacb4522016-09-19 17:47:09 -04009429size_t max_response_pages(struct nfs_server *server)
Idan Kedar85541162012-08-02 11:47:10 +03009430{
9431 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
9432 return nfs_page_array_len(0, max_resp_sz);
9433}
9434
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009435static void nfs4_layoutget_release(void *calldata)
9436{
9437 struct nfs4_layoutget *lgp = calldata;
9438
9439 dprintk("--> %s\n", __func__);
Trond Myklebustbd171932017-06-27 17:33:38 -04009440 nfs4_sequence_free_slot(&lgp->res.seq_res);
Trond Myklebust29a8bfe2018-05-30 17:16:20 -04009441 pnfs_layoutget_free(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009442 dprintk("<-- %s\n", __func__);
9443}
9444
9445static const struct rpc_call_ops nfs4_layoutget_call_ops = {
9446 .rpc_call_prepare = nfs4_layoutget_prepare,
9447 .rpc_call_done = nfs4_layoutget_done,
9448 .rpc_release = nfs4_layoutget_release,
9449};
9450
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04009451struct pnfs_layout_segment *
Fred Isamandacb4522016-09-19 17:47:09 -04009452nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009453{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05009454 struct inode *inode = lgp->args.inode;
9455 struct nfs_server *server = NFS_SERVER(inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009456 struct rpc_task *task;
9457 struct rpc_message msg = {
9458 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
9459 .rpc_argp = &lgp->args,
9460 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04009461 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009462 };
9463 struct rpc_task_setup task_setup_data = {
9464 .rpc_client = server->client,
9465 .rpc_message = &msg,
9466 .callback_ops = &nfs4_layoutget_call_ops,
9467 .callback_data = lgp,
Trond Myklebust63ec2b62020-02-07 19:40:14 -05009468 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009469 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04009470 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04009471 struct nfs4_exception exception = {
9472 .inode = inode,
9473 .timeout = *timeout,
9474 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009475 int status = 0;
9476
9477 dprintk("--> %s\n", __func__);
9478
Peng Tao4bd5a982014-11-17 11:05:17 +08009479 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
9480 pnfs_get_layout_hdr(NFS_I(inode)->layout);
9481
Anna Schumakerfba83f32018-05-04 16:22:50 -04009482 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05009483
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009484 task = rpc_run_task(&task_setup_data);
Xiyu Yang6e476662020-04-25 21:04:40 +08009485
Anna Schumaker820bf852017-01-11 15:01:43 -05009486 status = rpc_wait_for_completion_task(task);
Trond Myklebust2dbf8df2018-06-15 15:58:45 -04009487 if (status != 0)
9488 goto out;
9489
Trond Myklebust18c07782019-02-13 07:55:31 -05009490 if (task->tk_status < 0) {
Jeff Layton183d9e72016-05-17 12:28:47 -04009491 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
9492 *timeout = exception.timeout;
Trond Myklebust18c07782019-02-13 07:55:31 -05009493 } else if (lgp->res.layoutp->len == 0) {
9494 status = -EAGAIN;
9495 *timeout = nfs4_update_delay(&exception.timeout);
Trond Myklebust2dbf8df2018-06-15 15:58:45 -04009496 } else
9497 lseg = pnfs_layout_process(lgp);
9498out:
Trond Myklebust1037e6e2013-08-14 16:36:51 -04009499 trace_nfs4_layoutget(lgp->args.ctx,
9500 &lgp->args.range,
9501 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05009502 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04009503 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04009504
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009505 rpc_put_task(task);
9506 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04009507 if (status)
9508 return ERR_PTR(status);
9509 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009510}
9511
Benny Halevycbe82602011-05-22 19:52:37 +03009512static void
9513nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
9514{
9515 struct nfs4_layoutreturn *lrp = calldata;
9516
9517 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009518 nfs4_setup_sequence(lrp->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04009519 &lrp->args.seq_args,
9520 &lrp->res.seq_res,
9521 task);
Trond Myklebustc8bf7072018-06-15 16:31:02 -04009522 if (!pnfs_layout_is_valid(lrp->args.layout))
9523 rpc_exit(task, 0);
Benny Halevycbe82602011-05-22 19:52:37 +03009524}
9525
9526static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
9527{
9528 struct nfs4_layoutreturn *lrp = calldata;
9529 struct nfs_server *server;
9530
9531 dprintk("--> %s\n", __func__);
9532
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04009533 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03009534 return;
9535
Trond Myklebust6109bcf2019-09-20 07:23:43 -04009536 /*
9537 * Was there an RPC level error? Assume the call succeeded,
9538 * and that we need to release the layout
9539 */
9540 if (task->tk_rpc_status != 0 && RPC_WAS_SENT(task)) {
9541 lrp->res.lrs_present = 0;
9542 return;
9543 }
9544
Benny Halevycbe82602011-05-22 19:52:37 +03009545 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05009546 switch (task->tk_status) {
Trond Myklebustff905142017-11-06 15:28:08 -05009547 case -NFS4ERR_OLD_STATEID:
Trond Myklebust30cb3ee2019-09-20 07:23:45 -04009548 if (nfs4_layout_refresh_old_stateid(&lrp->args.stateid,
Trond Myklebustecf84022018-08-15 21:35:46 -04009549 &lrp->args.range,
Trond Myklebustff905142017-11-06 15:28:08 -05009550 lrp->args.inode))
9551 goto out_restart;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05009552 fallthrough;
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05009553 default:
9554 task->tk_status = 0;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05009555 fallthrough;
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05009556 case 0:
9557 break;
9558 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10009559 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05009560 break;
Trond Myklebustff905142017-11-06 15:28:08 -05009561 goto out_restart;
Benny Halevycbe82602011-05-22 19:52:37 +03009562 }
Benny Halevycbe82602011-05-22 19:52:37 +03009563 dprintk("<-- %s\n", __func__);
Trond Myklebustff905142017-11-06 15:28:08 -05009564 return;
9565out_restart:
9566 task->tk_status = 0;
9567 nfs4_sequence_free_slot(&lrp->res.seq_res);
9568 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03009569}
9570
9571static void nfs4_layoutreturn_release(void *calldata)
9572{
9573 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04009574 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03009575
9576 dprintk("--> %s\n", __func__);
Trond Myklebust2a974422016-11-20 13:13:54 -05009577 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
Trond Myklebust68f74472016-10-12 19:50:54 -04009578 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04009579 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebust4d796d72016-09-23 11:38:08 -04009580 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
9581 lrp->ld_private.ops->free(&lrp->ld_private);
Trond Myklebust2f065dd2016-12-07 12:29:26 -05009582 pnfs_put_layout_hdr(lrp->args.layout);
9583 nfs_iput_and_deactive(lrp->inode);
Trond Myklebust44ea8df2020-04-02 15:37:02 -04009584 put_cred(lrp->cred);
Benny Halevycbe82602011-05-22 19:52:37 +03009585 kfree(calldata);
9586 dprintk("<-- %s\n", __func__);
9587}
9588
9589static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
9590 .rpc_call_prepare = nfs4_layoutreturn_prepare,
9591 .rpc_call_done = nfs4_layoutreturn_done,
9592 .rpc_release = nfs4_layoutreturn_release,
9593};
9594
Peng Tao6c166052014-11-17 09:30:40 +08009595int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03009596{
9597 struct rpc_task *task;
9598 struct rpc_message msg = {
9599 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
9600 .rpc_argp = &lrp->args,
9601 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04009602 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03009603 };
9604 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04009605 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03009606 .rpc_message = &msg,
9607 .callback_ops = &nfs4_layoutreturn_call_ops,
9608 .callback_data = lrp,
9609 };
Peng Tao6c166052014-11-17 09:30:40 +08009610 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03009611
Andrew Elble99ade3c2015-12-02 09:39:51 -05009612 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
9613 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
9614 &task_setup_data.rpc_client, &msg);
9615
Benny Halevycbe82602011-05-22 19:52:37 +03009616 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05009617 if (!sync) {
9618 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
9619 if (!lrp->inode) {
9620 nfs4_layoutreturn_release(lrp);
9621 return -EAGAIN;
9622 }
9623 task_setup_data.flags |= RPC_TASK_ASYNC;
9624 }
Anna Schumakerfba83f32018-05-04 16:22:50 -04009625 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1, 0);
Benny Halevycbe82602011-05-22 19:52:37 +03009626 task = rpc_run_task(&task_setup_data);
9627 if (IS_ERR(task))
9628 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05009629 if (sync)
9630 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05009631 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03009632 dprintk("<-- %s status=%d\n", __func__, status);
9633 rpc_put_task(task);
9634 return status;
9635}
9636
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009637static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009638_nfs4_proc_getdeviceinfo(struct nfs_server *server,
9639 struct pnfs_device *pdev,
NeilBrowna52458b2018-12-03 11:30:31 +11009640 const struct cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009641{
9642 struct nfs4_getdeviceinfo_args args = {
9643 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04009644 .notify_types = NOTIFY_DEVICEID4_CHANGE |
9645 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009646 };
9647 struct nfs4_getdeviceinfo_res res = {
9648 .pdev = pdev,
9649 };
9650 struct rpc_message msg = {
9651 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
9652 .rpc_argp = &args,
9653 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009654 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009655 };
9656 int status;
9657
9658 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00009659 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04009660 if (res.notification & ~args.notify_types)
9661 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04009662 if (res.notification != args.notify_types)
9663 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04009664
Trond Myklebustcac1d3a2020-12-16 16:31:26 -05009665 trace_nfs4_getdeviceinfo(server, &pdev->dev_id, status);
9666
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009667 dprintk("<-- %s status=%d\n", __func__, status);
9668
9669 return status;
9670}
9671
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009672int 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_exception exception = { };
9677 int err;
9678
9679 do {
9680 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009681 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009682 &exception);
9683 } while (exception.retry);
9684 return err;
9685}
9686EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
9687
Andy Adamson863a3c62011-03-23 13:27:54 +00009688static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
9689{
9690 struct nfs4_layoutcommit_data *data = calldata;
9691 struct nfs_server *server = NFS_SERVER(data->args.inode);
9692
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009693 nfs4_setup_sequence(server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04009694 &data->args.seq_args,
9695 &data->res.seq_res,
9696 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00009697}
9698
9699static void
9700nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
9701{
9702 struct nfs4_layoutcommit_data *data = calldata;
9703 struct nfs_server *server = NFS_SERVER(data->args.inode);
9704
Trond Myklebust6ba7db32012-10-22 20:07:20 -04009705 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00009706 return;
9707
9708 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009709 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
9710 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
9711 case -NFS4ERR_BADLAYOUT: /* no layout */
9712 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00009713 task->tk_status = 0;
Gustavo A. R. Silvaffb81712020-11-20 12:26:46 -06009714 break;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009715 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009716 break;
9717 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10009718 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009719 rpc_restart_call_prepare(task);
9720 return;
9721 }
9722 }
Andy Adamson863a3c62011-03-23 13:27:54 +00009723}
9724
9725static void nfs4_layoutcommit_release(void *calldata)
9726{
9727 struct nfs4_layoutcommit_data *data = calldata;
9728
Andy Adamsondb29c082011-07-30 20:52:38 -04009729 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05009730 nfs_post_op_update_inode_force_wcc(data->args.inode,
9731 data->res.fattr);
NeilBrowna52458b2018-12-03 11:30:31 +11009732 put_cred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05009733 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00009734 kfree(data);
9735}
9736
9737static const struct rpc_call_ops nfs4_layoutcommit_ops = {
9738 .rpc_call_prepare = nfs4_layoutcommit_prepare,
9739 .rpc_call_done = nfs4_layoutcommit_done,
9740 .rpc_release = nfs4_layoutcommit_release,
9741};
9742
9743int
Andy Adamsonef311532011-03-12 02:58:10 -05009744nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00009745{
9746 struct rpc_message msg = {
9747 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
9748 .rpc_argp = &data->args,
9749 .rpc_resp = &data->res,
9750 .rpc_cred = data->cred,
9751 };
9752 struct rpc_task_setup task_setup_data = {
9753 .task = &data->task,
9754 .rpc_client = NFS_CLIENT(data->args.inode),
9755 .rpc_message = &msg,
9756 .callback_ops = &nfs4_layoutcommit_ops,
9757 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00009758 };
9759 struct rpc_task *task;
9760 int status = 0;
9761
Kinglong Meeb4839eb2015-07-01 12:00:13 +08009762 dprintk("NFS: initiating layoutcommit call. sync %d "
9763 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00009764 data->args.lastbytewritten,
9765 data->args.inode->i_ino);
9766
Trond Myklebust472e2592015-02-05 16:50:30 -05009767 if (!sync) {
9768 data->inode = nfs_igrab_and_active(data->args.inode);
9769 if (data->inode == NULL) {
9770 nfs4_layoutcommit_release(data);
9771 return -EAGAIN;
9772 }
9773 task_setup_data.flags = RPC_TASK_ASYNC;
9774 }
Anna Schumakerfba83f32018-05-04 16:22:50 -04009775 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Andy Adamson863a3c62011-03-23 13:27:54 +00009776 task = rpc_run_task(&task_setup_data);
9777 if (IS_ERR(task))
9778 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05009779 if (sync)
9780 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05009781 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00009782 dprintk("%s: status %d\n", __func__, status);
9783 rpc_put_task(task);
9784 return status;
9785}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009786
Trond Myklebust302fad72019-02-18 13:32:38 -05009787/*
Andy Adamson97431202013-08-08 10:57:56 -04009788 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
9789 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
9790 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009791static int
9792_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009793 struct nfs_fsinfo *info,
9794 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009795{
9796 struct nfs41_secinfo_no_name_args args = {
9797 .style = SECINFO_STYLE_CURRENT_FH,
9798 };
9799 struct nfs4_secinfo_res res = {
9800 .flavors = flavors,
9801 };
9802 struct rpc_message msg = {
9803 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
9804 .rpc_argp = &args,
9805 .rpc_resp = &res,
9806 };
Anna Schumakercc15e242019-08-14 16:22:31 -04009807 struct nfs4_call_sync_data data = {
9808 .seq_server = server,
9809 .seq_args = &args.seq_args,
9810 .seq_res = &res.seq_res,
9811 };
9812 struct rpc_task_setup task_setup = {
9813 .rpc_client = server->client,
9814 .rpc_message = &msg,
9815 .callback_ops = server->nfs_client->cl_mvops->call_sync_ops,
9816 .callback_data = &data,
9817 .flags = RPC_TASK_NO_ROUND_ROBIN,
9818 };
NeilBrowna52458b2018-12-03 11:30:31 +11009819 const struct cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009820 int status;
9821
9822 if (use_integrity) {
Colin Ian King48bb6ec2020-06-10 00:22:57 +01009823 task_setup.rpc_client = server->nfs_client->cl_rpcclient;
Anna Schumakercc15e242019-08-14 16:22:31 -04009824
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04009825 cred = nfs4_get_clid_cred(server->nfs_client);
9826 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009827 }
9828
9829 dprintk("--> %s\n", __func__);
Anna Schumakercc15e242019-08-14 16:22:31 -04009830 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
9831 status = nfs4_call_sync_custom(&task_setup);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009832 dprintk("<-- %s status=%d\n", __func__, status);
9833
NeilBrowna52458b2018-12-03 11:30:31 +11009834 put_cred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009835
9836 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009837}
9838
9839static int
9840nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9841 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
9842{
Trond Myklebust0688e642019-04-07 13:59:09 -04009843 struct nfs4_exception exception = {
9844 .interruptible = true,
9845 };
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009846 int err;
9847 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009848 /* first try using integrity protection */
9849 err = -NFS4ERR_WRONGSEC;
9850
9851 /* try to use integrity protection with machine cred */
9852 if (_nfs4_is_integrity_protected(server->nfs_client))
9853 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9854 flavors, true);
9855
9856 /*
9857 * if unable to use integrity protection, or SECINFO with
9858 * integrity protection returns NFS4ERR_WRONGSEC (which is
9859 * disallowed by spec, but exists in deployed servers) use
9860 * the current filesystem's rpc_client and the user cred.
9861 */
9862 if (err == -NFS4ERR_WRONGSEC)
9863 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9864 flavors, false);
9865
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009866 switch (err) {
9867 case 0:
9868 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05009869 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009870 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009871 default:
9872 err = nfs4_handle_exception(server, err, &exception);
9873 }
9874 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009875out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009876 return err;
9877}
9878
9879static int
9880nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
9881 struct nfs_fsinfo *info)
9882{
9883 int err;
9884 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04009885 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009886 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009887 struct nfs4_secinfo4 *secinfo;
9888 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009889
9890 page = alloc_page(GFP_KERNEL);
9891 if (!page) {
9892 err = -ENOMEM;
9893 goto out;
9894 }
9895
9896 flavors = page_address(page);
9897 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
9898
9899 /*
9900 * Fall back on "guess and check" method if
9901 * the server doesn't support SECINFO_NO_NAME
9902 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05009903 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009904 err = nfs4_find_root_sec(server, fhandle, info);
9905 goto out_freepage;
9906 }
9907 if (err)
9908 goto out_freepage;
9909
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009910 for (i = 0; i < flavors->num_flavors; i++) {
9911 secinfo = &flavors->flavors[i];
9912
9913 switch (secinfo->flavor) {
9914 case RPC_AUTH_NULL:
9915 case RPC_AUTH_UNIX:
9916 case RPC_AUTH_GSS:
9917 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
9918 &secinfo->flavor_info);
9919 break;
9920 default:
9921 flavor = RPC_AUTH_MAXFLAVOR;
9922 break;
9923 }
9924
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04009925 if (!nfs_auth_info_match(&server->auth_info, flavor))
9926 flavor = RPC_AUTH_MAXFLAVOR;
9927
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009928 if (flavor != RPC_AUTH_MAXFLAVOR) {
9929 err = nfs4_lookup_root_sec(server, fhandle,
9930 info, flavor);
9931 if (!err)
9932 break;
9933 }
9934 }
9935
9936 if (flavor == RPC_AUTH_MAXFLAVOR)
9937 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009938
9939out_freepage:
9940 put_page(page);
9941 if (err == -EACCES)
9942 return -EPERM;
9943out:
9944 return err;
9945}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009946
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009947static int _nfs41_test_stateid(struct nfs_server *server,
9948 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11009949 const struct cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009950{
9951 int status;
9952 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009953 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009954 };
9955 struct nfs41_test_stateid_res res;
9956 struct rpc_message msg = {
9957 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
9958 .rpc_argp = &args,
9959 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009960 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009961 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009962 struct rpc_clnt *rpc_client = server->client;
9963
9964 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9965 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009966
Chuck Lever38527b12012-07-11 16:30:23 -04009967 dprintk("NFS call test_stateid %p\n", stateid);
Anna Schumakerfba83f32018-05-04 16:22:50 -04009968 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009969 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009970 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04009971 if (status != NFS_OK) {
9972 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04009973 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04009974 }
9975 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04009976 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04009977}
9978
Trond Myklebust43912bb2016-09-22 13:38:56 -04009979static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
9980 int err, struct nfs4_exception *exception)
9981{
9982 exception->retry = 0;
9983 switch(err) {
9984 case -NFS4ERR_DELAY:
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009985 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust43912bb2016-09-22 13:38:56 -04009986 nfs4_handle_exception(server, err, exception);
9987 break;
9988 case -NFS4ERR_BADSESSION:
9989 case -NFS4ERR_BADSLOT:
9990 case -NFS4ERR_BAD_HIGH_SLOT:
9991 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9992 case -NFS4ERR_DEADSESSION:
9993 nfs4_do_handle_exception(server, err, exception);
9994 }
9995}
9996
Chuck Lever38527b12012-07-11 16:30:23 -04009997/**
9998 * nfs41_test_stateid - perform a TEST_STATEID operation
9999 *
10000 * @server: server / transport on which to perform the operation
10001 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -040010002 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -040010003 *
10004 * Returns NFS_OK if the server recognizes that "stateid" is valid.
10005 * Otherwise a negative NFS4ERR value is returned if the operation
10006 * failed or the state ID is not currently valid.
10007 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -040010008static int nfs41_test_stateid(struct nfs_server *server,
10009 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +110010010 const struct cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -040010011{
Trond Myklebust0688e642019-04-07 13:59:09 -040010012 struct nfs4_exception exception = {
10013 .interruptible = true,
10014 };
Bryan Schumaker7d974792011-06-02 14:59:08 -040010015 int err;
10016 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -040010017 err = _nfs41_test_stateid(server, stateid, cred);
Trond Myklebust43912bb2016-09-22 13:38:56 -040010018 nfs4_handle_delay_or_session_error(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -040010019 } while (exception.retry);
10020 return err;
10021}
Bryan Schumaker9aeda352011-06-02 14:59:09 -040010022
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010023struct nfs_free_stateid_data {
10024 struct nfs_server *server;
10025 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -040010026 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010027};
10028
10029static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
10030{
10031 struct nfs_free_stateid_data *data = calldata;
Anna Schumaker7981c8a2017-01-10 11:39:53 -050010032 nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010033 &data->args.seq_args,
10034 &data->res.seq_res,
10035 task);
10036}
10037
10038static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
10039{
10040 struct nfs_free_stateid_data *data = calldata;
10041
10042 nfs41_sequence_done(task, &data->res.seq_res);
10043
10044 switch (task->tk_status) {
10045 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +100010046 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010047 rpc_restart_call_prepare(task);
10048 }
10049}
10050
10051static void nfs41_free_stateid_release(void *calldata)
10052{
10053 kfree(calldata);
10054}
10055
Trond Myklebust17f26b12013-08-21 15:48:42 -040010056static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010057 .rpc_call_prepare = nfs41_free_stateid_prepare,
10058 .rpc_call_done = nfs41_free_stateid_done,
10059 .rpc_release = nfs41_free_stateid_release,
10060};
10061
Anna Schumaker2f261022018-05-15 13:03:39 -040010062/**
10063 * nfs41_free_stateid - perform a FREE_STATEID operation
10064 *
10065 * @server: server / transport on which to perform the operation
10066 * @stateid: state ID to release
10067 * @cred: credential
Trond Myklebust302fad72019-02-18 13:32:38 -050010068 * @privileged: set to true if this call needs to be privileged
Anna Schumaker2f261022018-05-15 13:03:39 -040010069 *
10070 * Note: this function is always asynchronous.
10071 */
10072static int nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -040010073 const nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +110010074 const struct cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010075 bool privileged)
10076{
Bryan Schumaker9aeda352011-06-02 14:59:09 -040010077 struct rpc_message msg = {
10078 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -040010079 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -040010080 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010081 struct rpc_task_setup task_setup = {
10082 .rpc_client = server->client,
10083 .rpc_message = &msg,
10084 .callback_ops = &nfs41_free_stateid_ops,
10085 .flags = RPC_TASK_ASYNC,
10086 };
10087 struct nfs_free_stateid_data *data;
Anna Schumaker2f261022018-05-15 13:03:39 -040010088 struct rpc_task *task;
Bryan Schumaker9aeda352011-06-02 14:59:09 -040010089
Weston Andros Adamson3787d502013-08-13 16:37:36 -040010090 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
10091 &task_setup.rpc_client, &msg);
10092
Chuck Lever38527b12012-07-11 16:30:23 -040010093 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010094 data = kmalloc(sizeof(*data), GFP_NOFS);
10095 if (!data)
Anna Schumaker2f261022018-05-15 13:03:39 -040010096 return -ENOMEM;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010097 data->server = server;
10098 nfs4_stateid_copy(&data->args.stateid, stateid);
10099
10100 task_setup.callback_data = data;
10101
10102 msg.rpc_argp = &data->args;
10103 msg.rpc_resp = &data->res;
Anna Schumakerfba83f32018-05-04 16:22:50 -040010104 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, privileged);
Anna Schumaker2f261022018-05-15 13:03:39 -040010105 task = rpc_run_task(&task_setup);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010106 if (IS_ERR(task))
10107 return PTR_ERR(task);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010108 rpc_put_task(task);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -040010109 return 0;
Bryan Schumaker9aeda352011-06-02 14:59:09 -040010110}
Trond Myklebust36281ca2012-03-04 18:13:56 -050010111
Jeff Laytonf1cdae82014-05-01 06:28:47 -040010112static void
10113nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -040010114{
NeilBrowna52458b2018-12-03 11:30:31 +110010115 const struct cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -040010116
Trond Myklebustf0b0bf82016-09-22 13:39:04 -040010117 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -040010118 nfs4_free_lock_state(server, lsp);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -040010119}
10120
Trond Myklebust36281ca2012-03-04 18:13:56 -050010121static bool nfs41_match_stateid(const nfs4_stateid *s1,
10122 const nfs4_stateid *s2)
10123{
Trond Myklebust93b717f2016-05-16 17:42:43 -040010124 if (s1->type != s2->type)
10125 return false;
10126
Trond Myklebust2d2f24a2012-03-04 18:13:57 -050010127 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -050010128 return false;
10129
Trond Myklebust2d2f24a2012-03-04 18:13:57 -050010130 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -050010131 return true;
Trond Myklebust36281ca2012-03-04 18:13:56 -050010132
Anna Schumaker045c5512017-01-11 16:59:48 -050010133 return s1->seqid == 0 || s2->seqid == 0;
Trond Myklebust36281ca2012-03-04 18:13:56 -050010134}
10135
Andy Adamson557134a2009-04-01 09:21:53 -040010136#endif /* CONFIG_NFS_V4_1 */
10137
Trond Myklebust36281ca2012-03-04 18:13:56 -050010138static bool nfs4_match_stateid(const nfs4_stateid *s1,
10139 const nfs4_stateid *s2)
10140{
Trond Myklebustf597c532012-03-04 18:13:56 -050010141 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -050010142}
10143
10144
Trond Myklebust17280172012-03-11 13:11:00 -040010145static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -050010146 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -050010147 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010148 .recover_open = nfs4_open_reclaim,
10149 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -040010150 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -040010151 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010152};
10153
Andy Adamson591d71c2009-04-01 09:22:47 -040010154#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -040010155static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -040010156 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
10157 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
10158 .recover_open = nfs4_open_reclaim,
10159 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -050010160 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -050010161 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -040010162 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -040010163};
10164#endif /* CONFIG_NFS_V4_1 */
10165
Trond Myklebust17280172012-03-11 13:11:00 -040010166static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -050010167 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -050010168 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +030010169 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010170 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -040010171 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010172};
10173
Andy Adamson591d71c2009-04-01 09:22:47 -040010174#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -040010175static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -040010176 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
10177 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -040010178 .recover_open = nfs41_open_expired,
10179 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -050010180 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -040010181};
10182#endif /* CONFIG_NFS_V4_1 */
10183
Trond Myklebust17280172012-03-11 13:11:00 -040010184static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -040010185 .sched_state_renewal = nfs4_proc_async_renew,
NeilBrownf15e1e82018-12-03 11:30:30 +110010186 .get_state_renewal_cred = nfs4_get_renew_cred,
Benny Halevy8e69514f2009-04-01 09:22:45 -040010187 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -040010188};
10189
10190#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -040010191static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -040010192 .sched_state_renewal = nfs41_proc_async_sequence,
NeilBrownf15e1e82018-12-03 11:30:30 +110010193 .get_state_renewal_cred = nfs4_get_machine_cred,
Benny Halevy8e69514f2009-04-01 09:22:45 -040010194 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -040010195};
10196#endif
10197
Chuck Leverec011fe2013-10-17 14:12:39 -040010198static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -040010199 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -040010200 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -040010201};
10202
10203#if defined(CONFIG_NFS_V4_1)
10204static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -040010205 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -040010206 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -040010207};
10208#endif /* CONFIG_NFS_V4_1 */
10209
Trond Myklebust97dc1352010-06-16 09:52:26 -040010210static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
10211 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -040010212 .init_caps = NFS_CAP_READDIRPLUS
10213 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -040010214 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -040010215 .init_client = nfs40_init_client,
10216 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -050010217 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -040010218 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -040010219 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust45870d62016-09-22 13:38:59 -040010220 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -050010221 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -040010222 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -040010223 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
10224 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
10225 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -040010226 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -040010227};
10228
10229#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -050010230static struct nfs_seqid *
10231nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
10232{
10233 return NULL;
10234}
10235
Trond Myklebust97dc1352010-06-16 09:52:26 -040010236static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
10237 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -040010238 .init_caps = NFS_CAP_READDIRPLUS
10239 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -040010240 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -040010241 | NFS_CAP_STATEID_NFSV41
Fred Isaman6e012602016-10-04 15:26:41 -040010242 | NFS_CAP_ATOMIC_OPEN_V1
10243 | NFS_CAP_LGOPEN,
Chuck Leverabf79bb2013-08-09 12:49:11 -040010244 .init_client = nfs41_init_client,
10245 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -050010246 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -040010247 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -040010248 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust45870d62016-09-22 13:38:59 -040010249 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -050010250 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -040010251 .session_trunk = nfs4_test_session_trunk,
Chuck Lever9915ea72013-08-09 12:48:27 -040010252 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -040010253 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
10254 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
10255 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -040010256 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -040010257};
10258#endif
10259
Steve Dickson42c2c422013-05-22 12:50:38 -040010260#if defined(CONFIG_NFS_V4_2)
10261static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
10262 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -040010263 .init_caps = NFS_CAP_READDIRPLUS
10264 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -040010265 | NFS_CAP_POSIX_LOCK
10266 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -040010267 | NFS_CAP_ATOMIC_OPEN_V1
Fred Isaman6e012602016-10-04 15:26:41 -040010268 | NFS_CAP_LGOPEN
Anna Schumakerf4ac1672014-11-25 13:18:15 -050010269 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -040010270 | NFS_CAP_COPY
Olga Kornievskaiacb95dee2018-07-09 15:13:29 -040010271 | NFS_CAP_OFFLOAD_CANCEL
Olga Kornievskaia04915672019-06-04 16:14:30 -040010272 | NFS_CAP_COPY_NOTIFY
Anna Schumaker624bd5b2014-11-25 13:18:16 -050010273 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -040010274 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +080010275 | NFS_CAP_LAYOUTSTATS
Trond Myklebust3eb86092019-02-08 10:31:05 -050010276 | NFS_CAP_CLONE
Anna Schumakerc5675522014-05-28 13:41:22 -040010277 | NFS_CAP_LAYOUTERROR
10278 | NFS_CAP_READ_PLUS,
Chuck Leverabf79bb2013-08-09 12:49:11 -040010279 .init_client = nfs41_init_client,
10280 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -040010281 .match_stateid = nfs41_match_stateid,
10282 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -040010283 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -040010284 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust45870d62016-09-22 13:38:59 -040010285 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -050010286 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -040010287 .session_trunk = nfs4_test_session_trunk,
Steve Dickson42c2c422013-05-22 12:50:38 -040010288 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
10289 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
10290 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +080010291 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -040010292};
10293#endif
10294
Trond Myklebust97dc1352010-06-16 09:52:26 -040010295const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
10296 [0] = &nfs_v4_0_minor_ops,
10297#if defined(CONFIG_NFS_V4_1)
10298 [1] = &nfs_v4_1_minor_ops,
10299#endif
Steve Dickson42c2c422013-05-22 12:50:38 -040010300#if defined(CONFIG_NFS_V4_2)
10301 [2] = &nfs_v4_2_minor_ops,
10302#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -040010303};
10304
Trond Myklebust13997822016-07-24 17:10:52 -040010305static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +010010306{
Frank van der Linden012a2112020-06-23 22:39:03 +000010307 ssize_t error, error2, error3;
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +010010308
10309 error = generic_listxattr(dentry, list, size);
10310 if (error < 0)
10311 return error;
10312 if (list) {
10313 list += error;
10314 size -= error;
10315 }
10316
10317 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
10318 if (error2 < 0)
10319 return error2;
Frank van der Linden012a2112020-06-23 22:39:03 +000010320
10321 if (list) {
10322 list += error2;
10323 size -= error2;
10324 }
10325
10326 error3 = nfs4_listxattr_nfs4_user(d_inode(dentry), list, size);
10327 if (error3 < 0)
10328 return error3;
10329
10330 return error + error2 + error3;
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +010010331}
10332
Trond Myklebust17f26b12013-08-21 15:48:42 -040010333static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -040010334 .create = nfs_create,
10335 .lookup = nfs_lookup,
10336 .atomic_open = nfs_atomic_open,
10337 .link = nfs_link,
10338 .unlink = nfs_unlink,
10339 .symlink = nfs_symlink,
10340 .mkdir = nfs_mkdir,
10341 .rmdir = nfs_rmdir,
10342 .mknod = nfs_mknod,
10343 .rename = nfs_rename,
10344 .permission = nfs_permission,
10345 .getattr = nfs_getattr,
10346 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +010010347 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -040010348};
10349
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -080010350static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +000010351 .permission = nfs_permission,
10352 .getattr = nfs_getattr,
10353 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +010010354 .listxattr = nfs4_listxattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +000010355};
10356
David Howells509de812006-08-22 20:06:11 -040010357const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010358 .version = 4, /* protocol version */
10359 .dentry_ops = &nfs4_dentry_operations,
10360 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +000010361 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -040010362 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010363 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -040010364 .submount = nfs4_submount,
David Howellsf2aedb72019-12-10 07:31:13 -050010365 .try_get_tree = nfs4_try_get_tree,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010366 .getattr = nfs4_proc_getattr,
10367 .setattr = nfs4_proc_setattr,
10368 .lookup = nfs4_proc_lookup,
Jeff Layton5b5faaf2017-06-29 06:34:52 -070010369 .lookupp = nfs4_proc_lookupp,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010370 .access = nfs4_proc_access,
10371 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010372 .create = nfs4_proc_create,
10373 .remove = nfs4_proc_remove,
10374 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -040010375 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010376 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -040010377 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -040010378 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -040010379 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010380 .link = nfs4_proc_link,
10381 .symlink = nfs4_proc_symlink,
10382 .mkdir = nfs4_proc_mkdir,
Trond Myklebust912678d2018-03-20 16:43:15 -040010383 .rmdir = nfs4_proc_rmdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010384 .readdir = nfs4_proc_readdir,
10385 .mknod = nfs4_proc_mknod,
10386 .statfs = nfs4_proc_statfs,
10387 .fsinfo = nfs4_proc_fsinfo,
10388 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -040010389 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010390 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -040010391 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010392 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -050010393 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010394 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -050010395 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010396 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -040010397 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -050010398 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010399 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +000010400 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -040010401 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -040010402 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -040010403 .have_delegation = nfs4_have_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -040010404 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +000010405 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -040010406 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -040010407 .create_server = nfs4_create_server,
10408 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010409};
10410
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000010411static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +010010412 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000010413 .list = nfs4_xattr_list_nfs4_acl,
10414 .get = nfs4_xattr_get_nfs4_acl,
10415 .set = nfs4_xattr_set_nfs4_acl,
10416};
10417
Frank van der Linden012a2112020-06-23 22:39:03 +000010418#ifdef CONFIG_NFS_V4_2
10419static const struct xattr_handler nfs4_xattr_nfs4_user_handler = {
10420 .prefix = XATTR_USER_PREFIX,
10421 .get = nfs4_xattr_get_nfs4_user,
10422 .set = nfs4_xattr_set_nfs4_user,
10423};
10424#endif
10425
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000010426const struct xattr_handler *nfs4_xattr_handlers[] = {
10427 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -040010428#ifdef CONFIG_NFS_V4_SECURITY_LABEL
10429 &nfs4_xattr_nfs4_label_handler,
10430#endif
Frank van der Linden012a2112020-06-23 22:39:03 +000010431#ifdef CONFIG_NFS_V4_2
10432 &nfs4_xattr_nfs4_user_handler,
10433#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000010434 NULL
10435};
10436
Linus Torvalds1da177e2005-04-16 15:20:36 -070010437/*
10438 * Local variables:
10439 * c-basic-offset: 8
10440 * End:
10441 */