blob: a74c1c3c41924e30a0ba0a2c9d983b2c391fcd12 [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 Myklebusta71029b2021-04-10 00:23:03 -0400305 if (!(cache_validity & NFS_INO_INVALID_OTHER))
306 dst[1] &= ~(FATTR4_WORD1_MODE | FATTR4_WORD1_OWNER |
307 FATTR4_WORD1_OWNER_GROUP);
Trond Myklebust30846df2018-04-07 13:44:28 -0400308}
309
Al Virobc4785c2006-10-19 23:28:51 -0700310static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 struct nfs4_readdir_arg *readdir)
312{
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400313 unsigned int attrs = FATTR4_WORD0_FILEID | FATTR4_WORD0_TYPE;
Al Viro0dbb4c62006-10-19 23:28:49 -0700314 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000317 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
319 return;
320 }
321
322 readdir->cookie = 0;
323 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
324 if (cookie == 2)
325 return;
326
327 /*
328 * NFSv4 servers do not return entries for '.' and '..'
329 * Therefore, we fake these entries here. We let '.'
330 * have cookie 0 and '..' have cookie 1. Note that
331 * when talking to the server, we always send cookie 0
332 * instead of 1 or 2.
333 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800334 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
336 if (cookie == 0) {
337 *p++ = xdr_one; /* next */
338 *p++ = xdr_zero; /* cookie, first word */
339 *p++ = xdr_one; /* cookie, second word */
340 *p++ = xdr_one; /* entry len */
341 memcpy(p, ".\0\0\0", 4); /* entry */
342 p++;
343 *p++ = xdr_one; /* bitmap length */
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400344 *p++ = htonl(attrs); /* bitmap */
345 *p++ = htonl(12); /* attribute buffer length */
346 *p++ = htonl(NF4DIR);
David Howells2b0143b2015-03-17 22:25:59 +0000347 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 }
349
350 *p++ = xdr_one; /* next */
351 *p++ = xdr_zero; /* cookie, first word */
352 *p++ = xdr_two; /* cookie, second word */
353 *p++ = xdr_two; /* entry len */
354 memcpy(p, "..\0\0", 4); /* entry */
355 p++;
356 *p++ = xdr_one; /* bitmap length */
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400357 *p++ = htonl(attrs); /* bitmap */
358 *p++ = htonl(12); /* attribute buffer length */
359 *p++ = htonl(NF4DIR);
David Howells2b0143b2015-03-17 22:25:59 +0000360 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
362 readdir->pgbase = (char *)p - (char *)start;
363 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800364 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365}
366
Trond Myklebust26d36302016-09-22 13:39:05 -0400367static void nfs4_test_and_free_stateid(struct nfs_server *server,
368 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +1100369 const struct cred *cred)
Trond Myklebust26d36302016-09-22 13:39:05 -0400370{
371 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
372
373 ops->test_and_free_expired(server, stateid, cred);
374}
375
376static void __nfs4_free_revoked_stateid(struct nfs_server *server,
377 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +1100378 const struct cred *cred)
Trond Myklebust26d36302016-09-22 13:39:05 -0400379{
380 stateid->type = NFS4_REVOKED_STATEID_TYPE;
381 nfs4_test_and_free_stateid(server, stateid, cred);
382}
383
384static void nfs4_free_revoked_stateid(struct nfs_server *server,
385 const nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +1100386 const struct cred *cred)
Trond Myklebust26d36302016-09-22 13:39:05 -0400387{
388 nfs4_stateid tmp;
389
390 nfs4_stateid_copy(&tmp, stateid);
391 __nfs4_free_revoked_stateid(server, &tmp, cred);
392}
393
NeilBrown8478eaa2014-09-18 16:09:27 +1000394static long nfs4_update_delay(long *timeout)
395{
396 long ret;
397 if (!timeout)
398 return NFS4_POLL_RETRY_MAX;
399 if (*timeout <= 0)
400 *timeout = NFS4_POLL_RETRY_MIN;
401 if (*timeout > NFS4_POLL_RETRY_MAX)
402 *timeout = NFS4_POLL_RETRY_MAX;
403 ret = *timeout;
404 *timeout <<= 1;
405 return ret;
406}
407
Trond Myklebust0688e642019-04-07 13:59:09 -0400408static int nfs4_delay_killable(long *timeout)
Trond Myklebust65de8722008-12-23 15:21:44 -0500409{
Trond Myklebust65de8722008-12-23 15:21:44 -0500410 might_sleep();
411
NeilBrown8478eaa2014-09-18 16:09:27 +1000412 freezable_schedule_timeout_killable_unsafe(
413 nfs4_update_delay(timeout));
Trond Myklebust0688e642019-04-07 13:59:09 -0400414 if (!__fatal_signal_pending(current))
415 return 0;
416 return -EINTR;
417}
418
419static int nfs4_delay_interruptible(long *timeout)
420{
421 might_sleep();
422
He Zhe59679d92020-07-06 17:52:24 +0800423 freezable_schedule_timeout_interruptible_unsafe(nfs4_update_delay(timeout));
Trond Myklebust0688e642019-04-07 13:59:09 -0400424 if (!signal_pending(current))
425 return 0;
426 return __fatal_signal_pending(current) ? -EINTR :-ERESTARTSYS;
427}
428
429static int nfs4_delay(long *timeout, bool interruptible)
430{
431 if (interruptible)
432 return nfs4_delay_interruptible(timeout);
433 return nfs4_delay_killable(timeout);
Trond Myklebust65de8722008-12-23 15:21:44 -0500434}
435
Trond Myklebust50c80002019-07-11 19:02:18 -0400436static const nfs4_stateid *
437nfs4_recoverable_stateid(const nfs4_stateid *stateid)
438{
439 if (!stateid)
440 return NULL;
441 switch (stateid->type) {
442 case NFS4_OPEN_STATEID_TYPE:
443 case NFS4_LOCK_STATEID_TYPE:
444 case NFS4_DELEGATION_STATEID_TYPE:
445 return stateid;
446 default:
447 break;
448 }
449 return NULL;
450}
451
Trond Myklebust65de8722008-12-23 15:21:44 -0500452/* This is the error handling routine for processes that are allowed
453 * to sleep.
454 */
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400455static int nfs4_do_handle_exception(struct nfs_server *server,
456 int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500457{
458 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500459 struct nfs4_state *state = exception->state;
Trond Myklebust50c80002019-07-11 19:02:18 -0400460 const nfs4_stateid *stateid;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500461 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500462 int ret = errorcode;
463
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400464 exception->delay = 0;
465 exception->recovering = 0;
Trond Myklebust65de8722008-12-23 15:21:44 -0500466 exception->retry = 0;
Trond Myklebust272289a2016-09-22 13:39:15 -0400467
Trond Myklebust50c80002019-07-11 19:02:18 -0400468 stateid = nfs4_recoverable_stateid(exception->stateid);
Trond Myklebust272289a2016-09-22 13:39:15 -0400469 if (stateid == NULL && state != NULL)
Trond Myklebust50c80002019-07-11 19:02:18 -0400470 stateid = nfs4_recoverable_stateid(&state->stateid);
Trond Myklebust272289a2016-09-22 13:39:15 -0400471
Trond Myklebust65de8722008-12-23 15:21:44 -0500472 switch(errorcode) {
473 case 0:
474 return 0;
Trond Myklebustcf61eb22018-05-29 22:06:08 -0400475 case -NFS4ERR_BADHANDLE:
476 case -ESTALE:
477 if (inode != NULL && S_ISREG(inode->i_mode))
478 pnfs_destroy_layout(NFS_I(inode));
479 break;
Trond Myklebust5ba12442015-06-16 11:26:35 -0400480 case -NFS4ERR_DELEG_REVOKED:
481 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust272289a2016-09-22 13:39:15 -0400482 case -NFS4ERR_EXPIRED:
Trond Myklebust5ba12442015-06-16 11:26:35 -0400483 case -NFS4ERR_BAD_STATEID:
Olga Kornievskaiafefa1a82019-06-14 14:22:12 -0400484 case -NFS4ERR_PARTNER_NO_AUTH:
Trond Myklebust272289a2016-09-22 13:39:15 -0400485 if (inode != NULL && stateid != NULL) {
486 nfs_inode_find_state_and_recover(inode,
487 stateid);
488 goto wait_on_recovery;
489 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500490 fallthrough;
Trond Myklebust272289a2016-09-22 13:39:15 -0400491 case -NFS4ERR_OPENMODE:
Trond Myklebust8487c472016-06-26 08:44:35 -0400492 if (inode) {
493 int err;
494
495 err = nfs_async_inode_return_delegation(inode,
496 stateid);
497 if (err == 0)
498 goto wait_on_recovery;
499 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
500 exception->retry = 1;
501 break;
502 }
503 }
Trond Myklebust3114ea72012-03-07 16:39:06 -0500504 if (state == NULL)
505 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400506 ret = nfs4_schedule_stateid_recovery(server, state);
507 if (ret < 0)
508 break;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500509 goto wait_on_recovery;
Trond Myklebust65de8722008-12-23 15:21:44 -0500510 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500511 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500512 nfs4_schedule_lease_recovery(clp);
513 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -0400514 case -NFS4ERR_MOVED:
515 ret = nfs4_schedule_migration_recovery(server);
516 if (ret < 0)
517 break;
518 goto wait_on_recovery;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -0400519 case -NFS4ERR_LEASE_MOVED:
520 nfs4_schedule_lease_moved_recovery(clp);
521 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500522#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400523 case -NFS4ERR_BADSESSION:
524 case -NFS4ERR_BADSLOT:
525 case -NFS4ERR_BAD_HIGH_SLOT:
526 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
527 case -NFS4ERR_DEADSESSION:
528 case -NFS4ERR_SEQ_FALSE_RETRY:
529 case -NFS4ERR_SEQ_MISORDERED:
Trond Myklebust5c441542019-11-13 08:34:00 +0100530 /* Handled in nfs41_sequence_process() */
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400531 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500532#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500533 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500534 if (exception->timeout > HZ) {
535 /* We have retried a decent amount, time to
536 * fail
537 */
538 ret = -EBUSY;
539 break;
540 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500541 fallthrough;
Trond Myklebust65de8722008-12-23 15:21:44 -0500542 case -NFS4ERR_DELAY:
Trond Myklebust2598ed32015-09-20 16:10:18 -0400543 nfs_inc_server_stats(server, NFSIOS_DELAY);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500544 fallthrough;
Trond Myklebust2598ed32015-09-20 16:10:18 -0400545 case -NFS4ERR_GRACE:
Trond Myklebuste85d7ee2016-07-14 18:46:24 -0400546 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -0400547 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400548 exception->delay = 1;
549 return 0;
550
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400551 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500552 case -NFS4ERR_OLD_STATEID:
553 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800554 break;
555 case -NFS4ERR_BADOWNER:
556 /* The following works around a Linux server bug! */
557 case -NFS4ERR_BADNAME:
558 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
559 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
560 exception->retry = 1;
561 printk(KERN_WARNING "NFS: v4 server %s "
562 "does not accept raw "
563 "uid/gids. "
564 "Reenabling the idmapper.\n",
565 server->nfs_client->cl_hostname);
566 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500567 }
568 /* We failed to handle the error */
569 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500570wait_on_recovery:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400571 exception->recovering = 1;
572 return 0;
573}
574
575/* This is the error handling routine for processes that are allowed
576 * to sleep.
577 */
578int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
579{
580 struct nfs_client *clp = server->nfs_client;
581 int ret;
582
583 ret = nfs4_do_handle_exception(server, errorcode, exception);
584 if (exception->delay) {
Trond Myklebust0688e642019-04-07 13:59:09 -0400585 ret = nfs4_delay(&exception->timeout,
586 exception->interruptible);
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400587 goto out_retry;
588 }
589 if (exception->recovering) {
590 ret = nfs4_wait_clnt_recover(clp);
591 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
592 return -EIO;
593 goto out_retry;
594 }
595 return ret;
596out_retry:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500597 if (ret == 0)
598 exception->retry = 1;
599 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500600}
601
Trond Myklebust037fc982015-09-20 15:51:00 -0400602static int
603nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
604 int errorcode, struct nfs4_exception *exception)
605{
606 struct nfs_client *clp = server->nfs_client;
607 int ret;
608
609 ret = nfs4_do_handle_exception(server, errorcode, exception);
610 if (exception->delay) {
611 rpc_delay(task, nfs4_update_delay(&exception->timeout));
612 goto out_retry;
613 }
614 if (exception->recovering) {
615 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
616 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
617 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
618 goto out_retry;
619 }
620 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
621 ret = -EIO;
622 return ret;
623out_retry:
Bill Baker0f90be12018-06-19 16:24:58 -0500624 if (ret == 0) {
Trond Myklebust037fc982015-09-20 15:51:00 -0400625 exception->retry = 1;
Bill Baker0f90be12018-06-19 16:24:58 -0500626 /*
627 * For NFS4ERR_MOVED, the client transport will need to
628 * be recomputed after migration recovery has completed.
629 */
630 if (errorcode == -NFS4ERR_MOVED)
631 rpc_task_release_transport(task);
632 }
Trond Myklebust037fc982015-09-20 15:51:00 -0400633 return ret;
634}
635
Olga Kornievskaia0f913a52018-07-09 15:13:33 -0400636int
Trond Myklebust037fc982015-09-20 15:51:00 -0400637nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
638 struct nfs4_state *state, long *timeout)
639{
640 struct nfs4_exception exception = {
641 .state = state,
642 };
643
644 if (task->tk_status >= 0)
645 return 0;
646 if (timeout)
647 exception.timeout = *timeout;
648 task->tk_status = nfs4_async_handle_exception(task, server,
649 task->tk_status,
650 &exception);
651 if (exception.delay && timeout)
652 *timeout = exception.timeout;
653 if (exception.retry)
654 return -EAGAIN;
655 return 0;
656}
657
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400658/*
659 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
660 * or 'false' otherwise.
661 */
662static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
663{
664 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
Anna Schumakereeea5362017-01-11 16:01:21 -0500665 return (flavor == RPC_AUTH_GSS_KRB5I) || (flavor == RPC_AUTH_GSS_KRB5P);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400666}
Trond Myklebust65de8722008-12-23 15:21:44 -0500667
Trond Myklebust452e9352010-07-31 14:29:06 -0400668static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 spin_lock(&clp->cl_lock);
671 if (time_before(clp->cl_last_renewal,timestamp))
672 clp->cl_last_renewal = timestamp;
673 spin_unlock(&clp->cl_lock);
674}
675
Trond Myklebust452e9352010-07-31 14:29:06 -0400676static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
677{
Trond Myklebustbe824162015-07-05 14:50:46 -0400678 struct nfs_client *clp = server->nfs_client;
679
680 if (!nfs4_has_session(clp))
681 do_renew_lease(clp, timestamp);
Trond Myklebust452e9352010-07-31 14:29:06 -0400682}
683
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400684struct nfs4_call_sync_data {
685 const struct nfs_server *seq_server;
686 struct nfs4_sequence_args *seq_args;
687 struct nfs4_sequence_res *seq_res;
688};
689
Trond Myklebustbe3a5d22015-06-23 19:51:55 +0800690void nfs4_init_sequence(struct nfs4_sequence_args *args,
Anna Schumakerfba83f32018-05-04 16:22:50 -0400691 struct nfs4_sequence_res *res, int cache_reply,
692 int privileged)
Chuck Levera9c92d62013-08-09 12:48:18 -0400693{
694 args->sa_slot = NULL;
695 args->sa_cache_this = cache_reply;
Anna Schumakerfba83f32018-05-04 16:22:50 -0400696 args->sa_privileged = privileged;
Chuck Levera9c92d62013-08-09 12:48:18 -0400697
698 res->sr_slot = NULL;
699}
700
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400701static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
Chuck Lever3bd23842013-08-09 12:49:19 -0400702{
703 struct nfs4_slot *slot = res->sr_slot;
704 struct nfs4_slot_table *tbl;
705
Chuck Lever3bd23842013-08-09 12:49:19 -0400706 tbl = slot->table;
707 spin_lock(&tbl->slot_tbl_lock);
708 if (!nfs41_wake_and_assign_slot(tbl, slot))
709 nfs4_free_slot(tbl, slot);
710 spin_unlock(&tbl->slot_tbl_lock);
711
712 res->sr_slot = NULL;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400713}
714
715static int nfs40_sequence_done(struct rpc_task *task,
716 struct nfs4_sequence_res *res)
717{
718 if (res->sr_slot != NULL)
719 nfs40_sequence_free_slot(res);
Chuck Lever3bd23842013-08-09 12:49:19 -0400720 return 1;
721}
722
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400723#if defined(CONFIG_NFS_V4_1)
724
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400725static void nfs41_release_slot(struct nfs4_slot *slot)
Andy Adamson13615872009-04-01 09:22:17 -0400726{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500727 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400728 struct nfs4_slot_table *tbl;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500729 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400730
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400731 if (!slot)
732 return;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500733 tbl = slot->table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500734 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500735
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400736 /* Bump the slot sequence number */
737 if (slot->seq_done)
738 slot->seq_nr++;
739 slot->seq_done = 0;
740
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500741 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500742 /* Be nice to the server: try to ensure that the last transmitted
743 * value for highest_user_slotid <= target_highest_slotid
744 */
745 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
746 send_new_highest_used_slotid = true;
747
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500748 if (nfs41_wake_and_assign_slot(tbl, slot)) {
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500749 send_new_highest_used_slotid = false;
750 goto out_unlock;
751 }
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500752 nfs4_free_slot(tbl, slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500753
754 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
755 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500756out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500757 spin_unlock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500758 if (send_new_highest_used_slotid)
Anna Schumaker3f10a6a2015-07-13 14:01:31 -0400759 nfs41_notify_server(session->clp);
Trond Myklebust045d2a62016-08-28 13:25:43 -0400760 if (waitqueue_active(&tbl->slot_waitq))
761 wake_up_all(&tbl->slot_waitq);
Andy Adamson13615872009-04-01 09:22:17 -0400762}
763
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400764static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
765{
766 nfs41_release_slot(res->sr_slot);
767 res->sr_slot = NULL;
768}
769
Trond Myklebust3453d572018-06-20 17:53:34 -0400770static void nfs4_slot_sequence_record_sent(struct nfs4_slot *slot,
771 u32 seqnr)
772{
773 if ((s32)(seqnr - slot->seq_nr_highest_sent) > 0)
774 slot->seq_nr_highest_sent = seqnr;
775}
776static void nfs4_slot_sequence_acked(struct nfs4_slot *slot,
777 u32 seqnr)
778{
779 slot->seq_nr_highest_sent = seqnr;
780 slot->seq_nr_last_acked = seqnr;
781}
782
Anna Schumaker913fadc2020-07-08 10:33:40 -0400783static void nfs4_probe_sequence(struct nfs_client *client, const struct cred *cred,
784 struct nfs4_slot *slot)
785{
786 struct rpc_task *task = _nfs41_proc_sequence(client, cred, slot, true);
787 if (!IS_ERR(task))
788 rpc_put_task_async(task);
789}
790
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400791static int nfs41_sequence_process(struct rpc_task *task,
792 struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400793{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500794 struct nfs4_session *session;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500795 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400796 struct nfs_client *clp;
Trond Myklebust5c441542019-11-13 08:34:00 +0100797 int status;
Trond Myklebust85563072012-12-11 10:31:12 -0500798 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400799
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500800 if (slot == NULL)
801 goto out_noaction;
Bryan Schumaker468f8612011-04-18 15:57:32 -0400802 /* don't increment the sequence number if the task wasn't sent */
Trond Myklebustc71c46f2019-03-01 11:40:05 -0500803 if (!RPC_WAS_SENT(task) || slot->seq_done)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400804 goto out;
805
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500806 session = slot->table->session;
Anna Schumaker913fadc2020-07-08 10:33:40 -0400807 clp = session->clp;
Trond Myklebust933602e2012-11-16 12:12:38 -0500808
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400809 trace_nfs4_sequence_done(session, res);
Trond Myklebust5c441542019-11-13 08:34:00 +0100810
811 status = res->sr_status;
812 if (task->tk_status == -NFS4ERR_DEADSESSION)
813 status = -NFS4ERR_DEADSESSION;
814
Andy Adamson691daf32009-12-04 15:55:39 -0500815 /* Check the SEQUENCE operation status */
Trond Myklebust5c441542019-11-13 08:34:00 +0100816 switch (status) {
Trond Myklebust14516c32010-07-31 14:29:06 -0400817 case 0:
Trond Myklebust3453d572018-06-20 17:53:34 -0400818 /* Mark this sequence number as having been acked */
819 nfs4_slot_sequence_acked(slot, slot->seq_nr);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400820 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400821 slot->seq_done = 1;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500822 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500823 /* Check sequence flags */
Trond Myklebust0a014a42016-09-22 13:38:51 -0400824 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
825 !!slot->privileged);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500826 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400827 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500828 case 1:
829 /*
830 * sr_status remains 1 if an RPC level error occurred.
831 * The server may or may not have processed the sequence
832 * operation..
Trond Myklebustac20d162012-12-15 15:36:07 -0500833 */
Trond Myklebust3453d572018-06-20 17:53:34 -0400834 nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
835 slot->seq_done = 1;
Trond Myklebustac20d162012-12-15 15:36:07 -0500836 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400837 case -NFS4ERR_DELAY:
838 /* The server detected a resend of the RPC call and
839 * returned NFS4ERR_DELAY as per Section 2.10.6.2
840 * of RFC5661.
841 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500842 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400843 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500844 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500845 slot->seq_nr);
Trond Myklebust3453d572018-06-20 17:53:34 -0400846 nfs4_slot_sequence_acked(slot, slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400847 goto out_retry;
Trond Myklebustf9312a52018-06-09 19:10:31 -0400848 case -NFS4ERR_RETRY_UNCACHED_REP:
849 case -NFS4ERR_SEQ_FALSE_RETRY:
850 /*
851 * The server thinks we tried to replay a request.
852 * Retry the call after bumping the sequence ID.
853 */
Trond Myklebust3453d572018-06-20 17:53:34 -0400854 nfs4_slot_sequence_acked(slot, slot->seq_nr);
Trond Myklebustf9312a52018-06-09 19:10:31 -0400855 goto retry_new_seq;
Trond Myklebust85563072012-12-11 10:31:12 -0500856 case -NFS4ERR_BADSLOT:
857 /*
858 * The slot id we used was probably retired. Try again
859 * using a different slot id.
860 */
Trond Myklebust99589102018-06-09 12:50:50 -0400861 if (slot->slot_nr < slot->table->target_highest_slotid)
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400862 goto session_recover;
Trond Myklebuste8794442012-12-15 13:56:18 -0500863 goto retry_nowait;
864 case -NFS4ERR_SEQ_MISORDERED:
Trond Myklebust3453d572018-06-20 17:53:34 -0400865 nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
Trond Myklebuste8794442012-12-15 13:56:18 -0500866 /*
Trond Myklebust3453d572018-06-20 17:53:34 -0400867 * Were one or more calls using this slot interrupted?
868 * If the server never received the request, then our
Anna Schumaker913fadc2020-07-08 10:33:40 -0400869 * transmitted slot sequence number may be too high. However,
870 * if the server did receive the request then it might
871 * accidentally give us a reply with a mismatched operation.
872 * We can sort this out by sending a lone sequence operation
873 * to the server on the same slot.
Trond Myklebustac20d162012-12-15 15:36:07 -0500874 */
Trond Myklebust3453d572018-06-20 17:53:34 -0400875 if ((s32)(slot->seq_nr - slot->seq_nr_last_acked) > 1) {
876 slot->seq_nr--;
Anna Schumaker913fadc2020-07-08 10:33:40 -0400877 if (task->tk_msg.rpc_proc != &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE]) {
878 nfs4_probe_sequence(clp, task->tk_msg.rpc_cred, slot);
879 res->sr_slot = NULL;
880 }
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500881 goto retry_nowait;
882 }
Trond Myklebust3453d572018-06-20 17:53:34 -0400883 /*
884 * RFC5661:
885 * A retry might be sent while the original request is
886 * still in progress on the replier. The replier SHOULD
887 * deal with the issue by returning NFS4ERR_DELAY as the
888 * reply to SEQUENCE or CB_SEQUENCE operation, but
889 * implementations MAY return NFS4ERR_SEQ_MISORDERED.
890 *
891 * Restart the search after a delay.
892 */
893 slot->seq_nr = slot->seq_nr_highest_sent;
894 goto out_retry;
Trond Myklebust5c441542019-11-13 08:34:00 +0100895 case -NFS4ERR_BADSESSION:
896 case -NFS4ERR_DEADSESSION:
897 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
898 goto session_recover;
Trond Myklebust14516c32010-07-31 14:29:06 -0400899 default:
900 /* Just update the slot sequence no. */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400901 slot->seq_done = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400902 }
903out:
904 /* The session may be reset by one of the error handlers. */
905 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500906out_noaction:
Trond Myklebust85563072012-12-11 10:31:12 -0500907 return ret;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400908session_recover:
Trond Myklebust5c441542019-11-13 08:34:00 +0100909 nfs4_schedule_session_recovery(session, status);
910 dprintk("%s ERROR: %d Reset session\n", __func__, status);
911 nfs41_sequence_free_slot(res);
912 goto out;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400913retry_new_seq:
914 ++slot->seq_nr;
Trond Myklebuste8794442012-12-15 13:56:18 -0500915retry_nowait:
916 if (rpc_restart_call_prepare(task)) {
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400917 nfs41_sequence_free_slot(res);
Trond Myklebuste8794442012-12-15 13:56:18 -0500918 task->tk_status = 0;
919 ret = 0;
920 }
921 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400922out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400923 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400924 goto out;
925 rpc_delay(task, NFS4_POLL_RETRY_MAX);
926 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400927}
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400928
929int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
930{
931 if (!nfs41_sequence_process(task, res))
932 return 0;
933 if (res->sr_slot != NULL)
934 nfs41_sequence_free_slot(res);
935 return 1;
936
937}
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500938EXPORT_SYMBOL_GPL(nfs41_sequence_done);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400939
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400940static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
941{
942 if (res->sr_slot == NULL)
943 return 1;
944 if (res->sr_slot->table->session != NULL)
945 return nfs41_sequence_process(task, res);
946 return nfs40_sequence_done(task, res);
947}
948
949static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
950{
951 if (res->sr_slot != NULL) {
952 if (res->sr_slot->table->session != NULL)
953 nfs41_sequence_free_slot(res);
954 else
955 nfs40_sequence_free_slot(res);
956 }
957}
958
Peng Tao2c4b1312014-06-11 05:24:16 +0800959int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400960{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500961 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400962 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400963 if (!res->sr_slot->table->session)
964 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400965 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400966}
Peng Tao2c4b1312014-06-11 05:24:16 +0800967EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Trond Myklebustdf896452010-06-16 09:52:26 -0400968
Andy Adamsonce5039c2009-04-01 09:22:13 -0400969static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
970{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400971 struct nfs4_call_sync_data *data = calldata;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400972
Trond Myklebust035168ab2010-06-16 09:52:26 -0400973 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
974
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500975 nfs4_setup_sequence(data->seq_server->nfs_client,
976 data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400977}
978
Andy Adamson69ab40c2009-04-01 09:22:19 -0400979static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
980{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400981 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400982
Trond Myklebust14516c32010-07-31 14:29:06 -0400983 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400984}
985
Trond Myklebust17280172012-03-11 13:11:00 -0400986static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400987 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400988 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400989};
990
Chuck Lever3bd23842013-08-09 12:49:19 -0400991#else /* !CONFIG_NFS_V4_1 */
992
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400993static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
994{
995 return nfs40_sequence_done(task, res);
996}
997
998static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
999{
1000 if (res->sr_slot != NULL)
1001 nfs40_sequence_free_slot(res);
1002}
1003
Peng Tao2c4b1312014-06-11 05:24:16 +08001004int nfs4_sequence_done(struct rpc_task *task,
1005 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -04001006{
Chuck Lever3bd23842013-08-09 12:49:19 -04001007 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -04001008}
Peng Tao2c4b1312014-06-11 05:24:16 +08001009EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Chuck Lever3bd23842013-08-09 12:49:19 -04001010
1011#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001012
Trond Myklebustc1dffe02019-03-01 12:13:34 -05001013static void nfs41_sequence_res_init(struct nfs4_sequence_res *res)
1014{
1015 res->sr_timestamp = jiffies;
1016 res->sr_status_flags = 0;
1017 res->sr_status = 1;
1018}
1019
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04001020static
1021void nfs4_sequence_attach_slot(struct nfs4_sequence_args *args,
1022 struct nfs4_sequence_res *res,
1023 struct nfs4_slot *slot)
1024{
1025 if (!slot)
1026 return;
1027 slot->privileged = args->sa_privileged ? 1 : 0;
1028 args->sa_slot = slot;
1029
1030 res->sr_slot = slot;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04001031}
1032
1033int nfs4_setup_sequence(struct nfs_client *client,
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001034 struct nfs4_sequence_args *args,
1035 struct nfs4_sequence_res *res,
1036 struct rpc_task *task)
1037{
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001038 struct nfs4_session *session = nfs4_get_session(client);
Anna Schumaker76ee0352017-01-10 16:49:31 -05001039 struct nfs4_slot_table *tbl = client->cl_slot_tbl;
Anna Schumaker3d358082017-01-11 10:54:04 -05001040 struct nfs4_slot *slot;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001041
Anna Schumaker9dd9107f2017-01-10 12:01:46 -05001042 /* slot already allocated? */
1043 if (res->sr_slot != NULL)
1044 goto out_start;
1045
Trond Myklebust6b2e6852019-04-07 13:58:49 -04001046 if (session)
Anna Schumaker76ee0352017-01-10 16:49:31 -05001047 tbl = &session->fc_slot_table;
Anna Schumaker76ee0352017-01-10 16:49:31 -05001048
Trond Myklebust3453d572018-06-20 17:53:34 -04001049 spin_lock(&tbl->slot_tbl_lock);
1050 /* The state manager will wait until the slot table is empty */
1051 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
1052 goto out_sleep;
Anna Schumaker6994cdd2017-01-10 16:13:27 -05001053
Trond Myklebust3453d572018-06-20 17:53:34 -04001054 slot = nfs4_alloc_slot(tbl);
1055 if (IS_ERR(slot)) {
Trond Myklebust3453d572018-06-20 17:53:34 -04001056 if (slot == ERR_PTR(-ENOMEM))
Trond Myklebust6b2e6852019-04-07 13:58:49 -04001057 goto out_sleep_timeout;
Trond Myklebust3453d572018-06-20 17:53:34 -04001058 goto out_sleep;
Anna Schumaker3d358082017-01-11 10:54:04 -05001059 }
Trond Myklebust3453d572018-06-20 17:53:34 -04001060 spin_unlock(&tbl->slot_tbl_lock);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001061
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04001062 nfs4_sequence_attach_slot(args, res, slot);
Anna Schumaker3d358082017-01-11 10:54:04 -05001063
Anna Schumakerad05cc02017-01-11 13:37:06 -05001064 trace_nfs4_setup_sequence(session, args);
Anna Schumaker9dd9107f2017-01-10 12:01:46 -05001065out_start:
Trond Myklebustc1dffe02019-03-01 12:13:34 -05001066 nfs41_sequence_res_init(res);
Anna Schumaker9dd9107f2017-01-10 12:01:46 -05001067 rpc_call_start(task);
1068 return 0;
Trond Myklebust6b2e6852019-04-07 13:58:49 -04001069out_sleep_timeout:
1070 /* Try again in 1/4 second */
1071 if (args->sa_privileged)
1072 rpc_sleep_on_priority_timeout(&tbl->slot_tbl_waitq, task,
1073 jiffies + (HZ >> 2), RPC_PRIORITY_PRIVILEGED);
1074 else
1075 rpc_sleep_on_timeout(&tbl->slot_tbl_waitq, task,
1076 NULL, jiffies + (HZ >> 2));
1077 spin_unlock(&tbl->slot_tbl_lock);
1078 return -EAGAIN;
Anna Schumaker0dcee8b2017-01-10 16:29:54 -05001079out_sleep:
1080 if (args->sa_privileged)
1081 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
Trond Myklebust8357a9b2019-04-07 13:58:48 -04001082 RPC_PRIORITY_PRIVILEGED);
Anna Schumaker0dcee8b2017-01-10 16:29:54 -05001083 else
1084 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
1085 spin_unlock(&tbl->slot_tbl_lock);
1086 return -EAGAIN;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001087}
1088EXPORT_SYMBOL_GPL(nfs4_setup_sequence);
1089
Chuck Lever9915ea72013-08-09 12:48:27 -04001090static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
1091{
1092 struct nfs4_call_sync_data *data = calldata;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05001093 nfs4_setup_sequence(data->seq_server->nfs_client,
Chuck Lever9915ea72013-08-09 12:48:27 -04001094 data->seq_args, data->seq_res, task);
1095}
1096
1097static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
1098{
1099 struct nfs4_call_sync_data *data = calldata;
1100 nfs4_sequence_done(task, data->seq_res);
1101}
1102
1103static const struct rpc_call_ops nfs40_call_sync_ops = {
1104 .rpc_call_prepare = nfs40_call_sync_prepare,
1105 .rpc_call_done = nfs40_call_sync_done,
1106};
1107
Anna Schumaker48c05852019-08-14 15:27:00 -04001108static int nfs4_call_sync_custom(struct rpc_task_setup *task_setup)
1109{
1110 int ret;
1111 struct rpc_task *task;
1112
1113 task = rpc_run_task(task_setup);
1114 if (IS_ERR(task))
1115 return PTR_ERR(task);
1116
1117 ret = task->tk_status;
1118 rpc_put_task(task);
1119 return ret;
1120}
1121
Trond Myklebustc74dfe92020-01-06 15:39:37 -05001122static int nfs4_do_call_sync(struct rpc_clnt *clnt,
1123 struct nfs_server *server,
1124 struct rpc_message *msg,
1125 struct nfs4_sequence_args *args,
1126 struct nfs4_sequence_res *res,
1127 unsigned short task_flags)
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001128{
Chuck Lever9915ea72013-08-09 12:48:27 -04001129 struct nfs_client *clp = server->nfs_client;
1130 struct nfs4_call_sync_data data = {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001131 .seq_server = server,
Trond Myklebustad389da2007-06-05 12:30:00 -04001132 .seq_args = args,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001133 .seq_res = res,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001134 };
1135 struct rpc_task_setup task_setup = {
Trond Myklebustad389da2007-06-05 12:30:00 -04001136 .rpc_client = clnt,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001137 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -04001138 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebustc74dfe92020-01-06 15:39:37 -05001139 .callback_data = &data,
1140 .flags = task_flags,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001141 };
1142
Anna Schumaker48c05852019-08-14 15:27:00 -04001143 return nfs4_call_sync_custom(&task_setup);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001144}
1145
Trond Myklebustc74dfe92020-01-06 15:39:37 -05001146static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
1147 struct nfs_server *server,
1148 struct rpc_message *msg,
1149 struct nfs4_sequence_args *args,
1150 struct nfs4_sequence_res *res)
1151{
1152 return nfs4_do_call_sync(clnt, server, msg, args, res, 0);
1153}
1154
1155
Bryan Schumaker7c513052011-03-24 17:12:24 +00001156int nfs4_call_sync(struct rpc_clnt *clnt,
1157 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001158 struct rpc_message *msg,
1159 struct nfs4_sequence_args *args,
1160 struct nfs4_sequence_res *res,
1161 int cache_reply)
1162{
Anna Schumakerfba83f32018-05-04 16:22:50 -04001163 nfs4_init_sequence(args, res, cache_reply, 0);
Chuck Lever9915ea72013-08-09 12:48:27 -04001164 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001165}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
Trond Myklebust3c591172018-07-31 15:54:10 -04001167static void
1168nfs4_inc_nlink_locked(struct inode *inode)
1169{
Trond Myklebustfabf2b32021-03-25 13:14:42 -04001170 nfs_set_cache_invalid(inode, NFS_INO_INVALID_NLINK);
Trond Myklebust3c591172018-07-31 15:54:10 -04001171 inc_nlink(inode);
1172}
1173
1174static void
1175nfs4_dec_nlink_locked(struct inode *inode)
1176{
Trond Myklebustfabf2b32021-03-25 13:14:42 -04001177 nfs_set_cache_invalid(inode, NFS_INO_INVALID_NLINK);
Trond Myklebust3c591172018-07-31 15:54:10 -04001178 drop_nlink(inode);
1179}
1180
1181static void
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001182nfs4_update_changeattr_locked(struct inode *inode,
1183 struct nfs4_change_info *cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04001184 unsigned long timestamp, unsigned long cache_validity)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185{
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001186 struct nfs_inode *nfsi = NFS_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
Trond Myklebustb6f80a22021-03-08 14:42:55 -05001188 cache_validity |= NFS_INO_INVALID_CTIME | NFS_INO_INVALID_MTIME;
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001189
1190 if (cinfo->atomic && cinfo->before == inode_peek_iversion_raw(inode)) {
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001191 nfsi->attrtimeo_timestamp = jiffies;
1192 } else {
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001193 if (S_ISDIR(inode->i_mode)) {
Trond Myklebustb6f80a22021-03-08 14:42:55 -05001194 cache_validity |= NFS_INO_INVALID_DATA;
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001195 nfs_force_lookup_revalidate(inode);
1196 } else {
1197 if (!NFS_PROTO(inode)->have_delegation(inode,
1198 FMODE_READ))
Trond Myklebustb6f80a22021-03-08 14:42:55 -05001199 cache_validity |= NFS_INO_REVAL_PAGECACHE;
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001200 }
1201
1202 if (cinfo->before != inode_peek_iversion_raw(inode))
Trond Myklebustb6f80a22021-03-08 14:42:55 -05001203 cache_validity |= NFS_INO_INVALID_ACCESS |
1204 NFS_INO_INVALID_ACL |
1205 NFS_INO_INVALID_XATTR;
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001206 }
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001207 inode_set_iversion_raw(inode, cinfo->after);
Trond Myklebustd3129ef2017-01-11 22:07:28 -05001208 nfsi->read_cache_jiffies = timestamp;
Trond Myklebust3235b402015-02-26 19:52:06 -05001209 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
Trond Myklebustb6f80a22021-03-08 14:42:55 -05001210 nfs_set_cache_invalid(inode, cache_validity);
Trond Myklebustc8d07152018-07-31 15:54:12 -04001211 nfsi->cache_validity &= ~NFS_INO_INVALID_CHANGE;
Trond Myklebust3c591172018-07-31 15:54:10 -04001212}
1213
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001214void
1215nfs4_update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04001216 unsigned long timestamp, unsigned long cache_validity)
Trond Myklebust3c591172018-07-31 15:54:10 -04001217{
1218 spin_lock(&dir->i_lock);
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001219 nfs4_update_changeattr_locked(dir, cinfo, timestamp, cache_validity);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 spin_unlock(&dir->i_lock);
1221}
1222
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001223struct nfs4_open_createattrs {
1224 struct nfs4_label *label;
1225 struct iattr *sattr;
1226 const __u32 verf[2];
1227};
1228
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001229static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1230 int err, struct nfs4_exception *exception)
1231{
1232 if (err != -EINVAL)
1233 return false;
1234 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1235 return false;
1236 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1237 exception->retry = 1;
1238 return true;
1239}
1240
Trond Myklebust1bf85d82019-06-27 06:30:48 -04001241static fmode_t _nfs4_ctx_to_accessmode(const struct nfs_open_context *ctx)
1242{
1243 return ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
1244}
1245
1246static fmode_t _nfs4_ctx_to_openmode(const struct nfs_open_context *ctx)
1247{
1248 fmode_t ret = ctx->mode & (FMODE_READ|FMODE_WRITE);
1249
1250 return (ctx->mode & FMODE_EXEC) ? FMODE_READ | ret : ret;
1251}
1252
Trond Myklebust6ae37332015-01-30 14:21:14 -05001253static u32
1254nfs4_map_atomic_open_share(struct nfs_server *server,
1255 fmode_t fmode, int openflags)
1256{
1257 u32 res = 0;
1258
1259 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1260 case FMODE_READ:
1261 res = NFS4_SHARE_ACCESS_READ;
1262 break;
1263 case FMODE_WRITE:
1264 res = NFS4_SHARE_ACCESS_WRITE;
1265 break;
1266 case FMODE_READ|FMODE_WRITE:
1267 res = NFS4_SHARE_ACCESS_BOTH;
1268 }
1269 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1270 goto out;
1271 /* Want no delegation if we're using O_DIRECT */
1272 if (openflags & O_DIRECT)
1273 res |= NFS4_SHARE_WANT_NO_DELEG;
1274out:
1275 return res;
1276}
1277
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001278static enum open_claim_type4
1279nfs4_map_atomic_open_claim(struct nfs_server *server,
1280 enum open_claim_type4 claim)
1281{
1282 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1283 return claim;
1284 switch (claim) {
1285 default:
1286 return claim;
1287 case NFS4_OPEN_CLAIM_FH:
1288 return NFS4_OPEN_CLAIM_NULL;
1289 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1290 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1291 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1292 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1293 }
1294}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
1296static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001297{
1298 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001299 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001300 p->o_res.seqid = p->o_arg.seqid;
1301 p->c_res.seqid = p->c_arg.seqid;
1302 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -04001303 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001304 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd12012-01-07 13:22:46 -05001305 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001306}
1307
Al Viro82a2c1b2011-06-22 18:30:55 -04001308static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001309 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001310 const struct nfs4_open_createattrs *c,
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001311 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001312 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001313{
Al Viro82a2c1b2011-06-22 18:30:55 -04001314 struct dentry *parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001315 struct inode *dir = d_inode(parent);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001316 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust63f5f792015-01-23 19:19:25 -05001317 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001318 struct nfs4_label *label = (c != NULL) ? c->label : NULL;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001319 struct nfs4_opendata *p;
1320
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001321 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001322 if (p == NULL)
1323 goto err;
David Quigley14c43f72013-05-22 12:50:43 -04001324
1325 p->f_label = nfs4_label_alloc(server, gfp_mask);
1326 if (IS_ERR(p->f_label))
1327 goto err_free_p;
1328
Kinglong Meea49c2692015-07-27 15:31:38 +08001329 p->a_label = nfs4_label_alloc(server, gfp_mask);
1330 if (IS_ERR(p->a_label))
1331 goto err_free_f;
1332
Trond Myklebust63f5f792015-01-23 19:19:25 -05001333 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1334 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05001335 if (IS_ERR(p->o_arg.seqid))
David Quigley14c43f72013-05-22 12:50:43 -04001336 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -04001337 nfs_sb_active(dentry->d_sb);
1338 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001339 p->dir = parent;
1340 p->owner = sp;
1341 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001342 p->o_arg.open_flags = flags;
1343 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Trond Myklebust536585c2016-11-10 15:40:34 -05001344 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
Trond Myklebust6ae37332015-01-30 14:21:14 -05001345 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1346 fmode, flags);
Benjamin Coddington90910512019-06-07 06:37:30 -04001347 if (flags & O_CREAT) {
1348 p->o_arg.umask = current_umask();
1349 p->o_arg.label = nfs4_label_copy(p->a_label, label);
1350 if (c->sattr != NULL && c->sattr->ia_valid != 0) {
1351 p->o_arg.u.attrs = &p->attrs;
1352 memcpy(&p->attrs, c->sattr, sizeof(p->attrs));
1353
1354 memcpy(p->o_arg.u.verifier.data, c->verf,
1355 sizeof(p->o_arg.u.verifier.data));
1356 }
1357 }
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001358 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1359 * will return permission denied for all bits until close */
1360 if (!(flags & O_EXCL)) {
1361 /* ask server to check for all possible rights as results
1362 * are cached */
Trond Myklebust536585c2016-11-10 15:40:34 -05001363 switch (p->o_arg.claim) {
1364 default:
1365 break;
1366 case NFS4_OPEN_CLAIM_NULL:
1367 case NFS4_OPEN_CLAIM_FH:
1368 p->o_arg.access = NFS4_ACCESS_READ |
1369 NFS4_ACCESS_MODIFY |
1370 NFS4_ACCESS_EXTEND |
1371 NFS4_ACCESS_EXECUTE;
Frank van der Linden72832a22020-06-23 22:38:58 +00001372#ifdef CONFIG_NFS_V4_2
1373 if (server->caps & NFS_CAP_XATTR)
1374 p->o_arg.access |= NFS4_ACCESS_XAREAD |
1375 NFS4_ACCESS_XAWRITE |
1376 NFS4_ACCESS_XALIST;
1377#endif
Trond Myklebust536585c2016-11-10 15:40:34 -05001378 }
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001379 }
David Howells7539bba2006-08-22 20:06:09 -04001380 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001381 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1382 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001383 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001384 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001385 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001386 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001387 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001388 case NFS4_OPEN_CLAIM_NULL:
1389 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1390 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1391 p->o_arg.fh = NFS_FH(dir);
1392 break;
1393 case NFS4_OPEN_CLAIM_PREVIOUS:
1394 case NFS4_OPEN_CLAIM_FH:
1395 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1396 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
David Howells2b0143b2015-03-17 22:25:59 +00001397 p->o_arg.fh = NFS_FH(d_inode(dentry));
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001398 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001399 p->c_arg.fh = &p->o_res.fh;
1400 p->c_arg.stateid = &p->o_res.stateid;
1401 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001402 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001403 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001404 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001405
1406err_free_label:
Kinglong Meea49c2692015-07-27 15:31:38 +08001407 nfs4_label_free(p->a_label);
1408err_free_f:
David Quigley14c43f72013-05-22 12:50:43 -04001409 nfs4_label_free(p->f_label);
1410err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001411 kfree(p);
1412err:
1413 dput(parent);
1414 return NULL;
1415}
1416
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001417static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001418{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001419 struct nfs4_opendata *p = container_of(kref,
1420 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001421 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001422
Fred Isaman30ae2412016-10-18 13:39:51 -04001423 nfs4_lgopen_release(p->lgp);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001424 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001425 nfs4_sequence_free_slot(&p->o_res.seq_res);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001426 if (p->state != NULL)
1427 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001428 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001429
Kinglong Meea49c2692015-07-27 15:31:38 +08001430 nfs4_label_free(p->a_label);
David Quigley14c43f72013-05-22 12:50:43 -04001431 nfs4_label_free(p->f_label);
1432
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001433 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001434 dput(p->dentry);
1435 nfs_sb_deactive(sb);
Trond Myklebust6926afd12012-01-07 13:22:46 -05001436 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001437 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001438 kfree(p);
1439}
1440
1441static void nfs4_opendata_put(struct nfs4_opendata *p)
1442{
1443 if (p != NULL)
1444 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001445}
1446
Trond Myklebust24311f82015-09-20 10:50:17 -04001447static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1448 fmode_t fmode)
1449{
1450 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1451 case FMODE_READ|FMODE_WRITE:
1452 return state->n_rdwr != 0;
1453 case FMODE_WRITE:
1454 return state->n_wronly != 0;
1455 case FMODE_READ:
1456 return state->n_rdonly != 0;
1457 }
1458 WARN_ON_ONCE(1);
1459 return false;
1460}
1461
Trond Myklebustbe189f72018-09-27 17:12:33 -04001462static int can_open_cached(struct nfs4_state *state, fmode_t mode,
1463 int open_mode, enum open_claim_type4 claim)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001464{
1465 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001466
Trond Myklebust536e43d2012-01-17 22:04:26 -05001467 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001468 goto out;
Trond Myklebustbe189f72018-09-27 17:12:33 -04001469 switch (claim) {
1470 case NFS4_OPEN_CLAIM_NULL:
1471 case NFS4_OPEN_CLAIM_FH:
1472 goto out;
1473 default:
1474 break;
1475 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001476 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001477 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001478 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1479 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001480 break;
1481 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001482 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1483 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001484 break;
1485 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001486 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1487 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001488 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001489out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001490 return ret;
1491}
1492
Trond Myklebust2a606182015-08-19 22:30:00 -05001493static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1494 enum open_claim_type4 claim)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001495{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001496 if (delegation == NULL)
1497 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001498 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001499 return 0;
Trond Myklebust2a606182015-08-19 22:30:00 -05001500 switch (claim) {
1501 case NFS4_OPEN_CLAIM_NULL:
1502 case NFS4_OPEN_CLAIM_FH:
1503 break;
1504 case NFS4_OPEN_CLAIM_PREVIOUS:
1505 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1506 break;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001507 fallthrough;
Trond Myklebust2a606182015-08-19 22:30:00 -05001508 default:
1509 return 0;
1510 }
Trond Myklebustb7391f42008-12-23 15:21:52 -05001511 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001512 return 1;
1513}
1514
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001515static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001516{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001517 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001518 case FMODE_WRITE:
1519 state->n_wronly++;
1520 break;
1521 case FMODE_READ:
1522 state->n_rdonly++;
1523 break;
1524 case FMODE_READ|FMODE_WRITE:
1525 state->n_rdwr++;
1526 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001527 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001528}
1529
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04001530#ifdef CONFIG_NFS_V4_1
1531static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1532{
1533 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1534 return true;
1535 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1536 return true;
1537 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1538 return true;
1539 return false;
1540}
1541#endif /* CONFIG_NFS_V4_1 */
1542
Trond Myklebustc9399f22017-11-06 15:28:01 -05001543static void nfs_state_log_update_open_stateid(struct nfs4_state *state)
1544{
1545 if (test_and_clear_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1546 wake_up_all(&state->waitq);
1547}
1548
Trond Myklebust4f14c192014-02-12 19:15:06 -05001549static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001550{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001551 struct nfs_client *clp = state->owner->so_server->nfs_client;
1552 bool need_recover = false;
1553
1554 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1555 need_recover = true;
1556 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1557 need_recover = true;
1558 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1559 need_recover = true;
1560 if (need_recover)
1561 nfs4_state_mark_reclaim_nograce(clp, state);
1562}
1563
Trond Myklebustc9399f22017-11-06 15:28:01 -05001564/*
1565 * Check for whether or not the caller may update the open stateid
1566 * to the value passed in by stateid.
1567 *
1568 * Note: This function relies heavily on the server implementing
1569 * RFC7530 Section 9.1.4.2, and RFC5661 Section 8.2.2
1570 * correctly.
1571 * i.e. The stateid seqids have to be initialised to 1, and
1572 * are then incremented on every state transition.
1573 */
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04001574static bool nfs_stateid_is_sequential(struct nfs4_state *state,
Trond Myklebustc9399f22017-11-06 15:28:01 -05001575 const nfs4_stateid *stateid)
Trond Myklebuste999e802014-02-10 18:20:47 -05001576{
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04001577 if (test_bit(NFS_OPEN_STATE, &state->flags)) {
1578 /* The common case - we're updating to a new sequence number */
1579 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1580 nfs4_stateid_is_next(&state->open_stateid, stateid)) {
1581 return true;
1582 }
1583 } else {
1584 /* This is the first OPEN in this generation */
Trond Myklebustc9399f22017-11-06 15:28:01 -05001585 if (stateid->seqid == cpu_to_be32(1))
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04001586 return true;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001587 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001588 return false;
1589}
1590
Trond Myklebustf95549c2015-01-23 18:06:09 -05001591static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1592{
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001593 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1594 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001595 if (state->n_wronly)
1596 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1597 if (state->n_rdonly)
1598 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1599 if (state->n_rdwr)
1600 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001601 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustf95549c2015-01-23 18:06:09 -05001602}
1603
Trond Myklebust226056c2014-02-11 10:41:07 -05001604static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1605 nfs4_stateid *stateid, fmode_t fmode)
1606{
1607 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1608 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1609 case FMODE_WRITE:
1610 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1611 break;
1612 case FMODE_READ:
1613 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1614 break;
1615 case 0:
1616 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1617 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1618 clear_bit(NFS_OPEN_STATE, &state->flags);
1619 }
1620 if (stateid == NULL)
1621 return;
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001622 /* Handle OPEN+OPEN_DOWNGRADE races */
1623 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1624 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
Trond Myklebustf95549c2015-01-23 18:06:09 -05001625 nfs_resync_open_stateid_locked(state);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001626 goto out;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001627 }
Trond Myklebust003707c2007-07-05 18:07:55 -04001628 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001629 nfs4_stateid_copy(&state->stateid, stateid);
1630 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001631 trace_nfs4_open_stateid_update(state->inode, stateid, 0);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001632out:
1633 nfs_state_log_update_open_stateid(state);
Trond Myklebust226056c2014-02-11 10:41:07 -05001634}
1635
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001636static void nfs_clear_open_stateid(struct nfs4_state *state,
1637 nfs4_stateid *arg_stateid,
1638 nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust226056c2014-02-11 10:41:07 -05001639{
1640 write_seqlock(&state->seqlock);
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001641 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1642 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1643 nfs_clear_open_stateid_locked(state, stateid, fmode);
Trond Myklebust226056c2014-02-11 10:41:07 -05001644 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001645 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1646 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001647}
1648
Trond Myklebust1393d962016-09-22 13:39:13 -04001649static void nfs_set_open_stateid_locked(struct nfs4_state *state,
Trond Myklebustc9399f22017-11-06 15:28:01 -05001650 const nfs4_stateid *stateid, nfs4_stateid *freeme)
Trond Myklebuste9acf212019-01-22 14:01:16 -05001651 __must_hold(&state->owner->so_lock)
1652 __must_hold(&state->seqlock)
1653 __must_hold(RCU)
1654
Trond Myklebust003707c2007-07-05 18:07:55 -04001655{
Trond Myklebustc9399f22017-11-06 15:28:01 -05001656 DEFINE_WAIT(wait);
1657 int status = 0;
1658 for (;;) {
1659
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04001660 if (nfs_stateid_is_sequential(state, stateid))
Trond Myklebust003707c2007-07-05 18:07:55 -04001661 break;
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04001662
Trond Myklebustc9399f22017-11-06 15:28:01 -05001663 if (status)
Trond Myklebust003707c2007-07-05 18:07:55 -04001664 break;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001665 /* Rely on seqids for serialisation with NFSv4.0 */
1666 if (!nfs4_has_session(NFS_SERVER(state->inode)->nfs_client))
1667 break;
1668
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04001669 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001670 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
1671 /*
1672 * Ensure we process the state changes in the same order
1673 * in which the server processed them by delaying the
1674 * update of the stateid until we are in sequence.
1675 */
1676 write_sequnlock(&state->seqlock);
1677 spin_unlock(&state->owner->so_lock);
1678 rcu_read_unlock();
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001679 trace_nfs4_open_stateid_update_wait(state->inode, stateid, 0);
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04001680
Trond Myklebustc9399f22017-11-06 15:28:01 -05001681 if (!signal_pending(current)) {
1682 if (schedule_timeout(5*HZ) == 0)
1683 status = -EAGAIN;
1684 else
1685 status = 0;
1686 } else
1687 status = -EINTR;
1688 finish_wait(&state->waitq, &wait);
1689 rcu_read_lock();
1690 spin_lock(&state->owner->so_lock);
1691 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001692 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001693
Trond Myklebuste1fff5d2017-11-07 13:10:46 -05001694 if (test_bit(NFS_OPEN_STATE, &state->flags) &&
1695 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
Trond Myklebustc9399f22017-11-06 15:28:01 -05001696 nfs4_stateid_copy(freeme, &state->open_stateid);
1697 nfs_test_and_clear_all_open_stateid(state);
1698 }
1699
Trond Myklebuste999e802014-02-10 18:20:47 -05001700 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1701 nfs4_stateid_copy(&state->stateid, stateid);
1702 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001703 trace_nfs4_open_stateid_update(state->inode, stateid, status);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001704 nfs_state_log_update_open_stateid(state);
Trond Myklebust003707c2007-07-05 18:07:55 -04001705}
1706
Trond Myklebustc9399f22017-11-06 15:28:01 -05001707static void nfs_state_set_open_stateid(struct nfs4_state *state,
Trond Myklebust1393d962016-09-22 13:39:13 -04001708 const nfs4_stateid *open_stateid,
Trond Myklebust1393d962016-09-22 13:39:13 -04001709 fmode_t fmode,
1710 nfs4_stateid *freeme)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001712 /*
1713 * Protect the call to nfs4_state_set_mode_locked and
1714 * serialise the stateid update
1715 */
1716 write_seqlock(&state->seqlock);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001717 nfs_set_open_stateid_locked(state, open_stateid, freeme);
1718 switch (fmode) {
1719 case FMODE_READ:
1720 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1721 break;
1722 case FMODE_WRITE:
1723 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1724 break;
1725 case FMODE_READ|FMODE_WRITE:
1726 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust003707c2007-07-05 18:07:55 -04001727 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001728 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001729 write_sequnlock(&state->seqlock);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001730}
1731
Trond Myklebust27a30cf2019-07-22 18:32:59 +01001732static void nfs_state_clear_open_state_flags(struct nfs4_state *state)
1733{
1734 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1735 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1736 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1737 clear_bit(NFS_OPEN_STATE, &state->flags);
1738}
1739
Trond Myklebustc9399f22017-11-06 15:28:01 -05001740static void nfs_state_set_delegation(struct nfs4_state *state,
1741 const nfs4_stateid *deleg_stateid,
1742 fmode_t fmode)
1743{
1744 /*
1745 * Protect the call to nfs4_state_set_mode_locked and
1746 * serialise the stateid update
1747 */
1748 write_seqlock(&state->seqlock);
1749 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1750 set_bit(NFS_DELEGATED_STATE, &state->flags);
1751 write_sequnlock(&state->seqlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752}
1753
Trond Myklebust9f0c5122018-09-05 14:07:15 -04001754static void nfs_state_clear_delegation(struct nfs4_state *state)
1755{
1756 write_seqlock(&state->seqlock);
1757 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1758 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1759 write_sequnlock(&state->seqlock);
1760}
1761
Olga Kornievskaiaec4b0922019-10-08 16:33:53 -04001762int update_open_stateid(struct nfs4_state *state,
Trond Myklebust1393d962016-09-22 13:39:13 -04001763 const nfs4_stateid *open_stateid,
1764 const nfs4_stateid *delegation,
1765 fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001766{
Trond Myklebust1393d962016-09-22 13:39:13 -04001767 struct nfs_server *server = NFS_SERVER(state->inode);
1768 struct nfs_client *clp = server->nfs_client;
Trond Myklebust34310432008-12-23 15:21:38 -05001769 struct nfs_inode *nfsi = NFS_I(state->inode);
1770 struct nfs_delegation *deleg_cur;
Arnd Bergmann83aa3e02016-10-18 17:21:30 +02001771 nfs4_stateid freeme = { };
Trond Myklebust34310432008-12-23 15:21:38 -05001772 int ret = 0;
1773
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001774 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001775
1776 rcu_read_lock();
Trond Myklebustc9399f22017-11-06 15:28:01 -05001777 spin_lock(&state->owner->so_lock);
1778 if (open_stateid != NULL) {
1779 nfs_state_set_open_stateid(state, open_stateid, fmode, &freeme);
1780 ret = 1;
1781 }
1782
Trond Myklebust333ac782019-10-22 12:12:17 -04001783 deleg_cur = nfs4_get_valid_delegation(state->inode);
Trond Myklebust34310432008-12-23 15:21:38 -05001784 if (deleg_cur == NULL)
1785 goto no_delegation;
1786
1787 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001788 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001789 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001790 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001791 goto no_delegation_unlock;
1792
1793 if (delegation == NULL)
1794 delegation = &deleg_cur->stateid;
Trond Myklebust333ac782019-10-22 12:12:17 -04001795 else if (!nfs4_stateid_match_other(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001796 goto no_delegation_unlock;
1797
Trond Myklebustb7391f42008-12-23 15:21:52 -05001798 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001799 nfs_state_set_delegation(state, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001800 ret = 1;
1801no_delegation_unlock:
1802 spin_unlock(&deleg_cur->lock);
1803no_delegation:
Trond Myklebustc9399f22017-11-06 15:28:01 -05001804 if (ret)
1805 update_open_stateflags(state, fmode);
1806 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001807 rcu_read_unlock();
1808
Trond Myklebust4f14c192014-02-12 19:15:06 -05001809 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
Trond Myklebust1393d962016-09-22 13:39:13 -04001810 nfs4_schedule_state_manager(clp);
1811 if (freeme.type != 0)
1812 nfs4_test_and_free_stateid(server, &freeme,
1813 state->owner->so_cred);
Trond Myklebust34310432008-12-23 15:21:38 -05001814
1815 return ret;
1816}
1817
Trond Myklebust39071e62015-01-24 15:07:56 -05001818static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1819 const nfs4_stateid *stateid)
1820{
1821 struct nfs4_state *state = lsp->ls_state;
1822 bool ret = false;
1823
1824 spin_lock(&state->state_lock);
1825 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1826 goto out_noupdate;
1827 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1828 goto out_noupdate;
1829 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1830 ret = true;
1831out_noupdate:
1832 spin_unlock(&state->state_lock);
1833 return ret;
1834}
Trond Myklebust34310432008-12-23 15:21:38 -05001835
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001836static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001837{
1838 struct nfs_delegation *delegation;
1839
Trond Myklebustf5086242018-03-20 16:43:13 -04001840 fmode &= FMODE_READ|FMODE_WRITE;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001841 rcu_read_lock();
Trond Myklebust40e6aa12019-10-27 13:38:45 -04001842 delegation = nfs4_get_valid_delegation(inode);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001843 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001844 rcu_read_unlock();
1845 return;
1846 }
1847 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001848 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001849}
1850
Trond Myklebust6ee41262007-07-08 14:11:36 -04001851static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001852{
1853 struct nfs4_state *state = opendata->state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001854 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001855 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001856 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebust2a606182015-08-19 22:30:00 -05001857 enum open_claim_type4 claim = opendata->o_arg.claim;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001858 nfs4_stateid stateid;
1859 int ret = -EAGAIN;
1860
Trond Myklebustaac00a82007-07-05 19:02:21 -04001861 for (;;) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001862 spin_lock(&state->owner->so_lock);
Trond Myklebustbe189f72018-09-27 17:12:33 -04001863 if (can_open_cached(state, fmode, open_mode, claim)) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001864 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001865 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef72014-09-03 14:15:40 -04001866 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001867 }
Anna Schumaker61beef72014-09-03 14:15:40 -04001868 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001869 rcu_read_lock();
Trond Myklebustbe3df3d2019-10-31 18:40:32 -04001870 delegation = nfs4_get_valid_delegation(state->inode);
Trond Myklebust2a606182015-08-19 22:30:00 -05001871 if (!can_open_delegated(delegation, fmode, claim)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001872 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001873 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001874 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001875 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001876 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001877 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001878 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001879 if (!opendata->is_recover) {
1880 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1881 if (ret != 0)
1882 goto out;
1883 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001884 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001885
1886 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001887 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001888 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001889 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001890out:
1891 return ERR_PTR(ret);
1892out_return_state:
Trond Myklebustace9fad2018-09-02 19:19:07 -04001893 refcount_inc(&state->count);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001894 return state;
1895}
1896
Andy Adamsone23008e2012-10-02 21:07:32 -04001897static void
1898nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1899{
1900 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1901 struct nfs_delegation *delegation;
1902 int delegation_flags = 0;
1903
1904 rcu_read_lock();
1905 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1906 if (delegation)
1907 delegation_flags = delegation->flags;
1908 rcu_read_unlock();
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001909 switch (data->o_arg.claim) {
1910 default:
1911 break;
1912 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1913 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001914 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1915 "returning a delegation for "
1916 "OPEN(CLAIM_DELEGATE_CUR)\n",
1917 clp->cl_hostname);
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001918 return;
1919 }
1920 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Andy Adamsone23008e2012-10-02 21:07:32 -04001921 nfs_inode_set_delegation(state->inode,
Trond Myklebust35156bf2018-03-20 17:03:13 -04001922 data->owner->so_cred,
1923 data->o_res.delegation_type,
1924 &data->o_res.delegation,
1925 data->o_res.pagemod_limit);
Andy Adamsone23008e2012-10-02 21:07:32 -04001926 else
1927 nfs_inode_reclaim_delegation(state->inode,
Trond Myklebust35156bf2018-03-20 17:03:13 -04001928 data->owner->so_cred,
1929 data->o_res.delegation_type,
1930 &data->o_res.delegation,
1931 data->o_res.pagemod_limit);
Jeff Layton8b199e52018-07-05 05:48:14 -04001932
1933 if (data->o_res.do_recall)
1934 nfs_async_inode_return_delegation(state->inode,
1935 &data->o_res.delegation);
Andy Adamsone23008e2012-10-02 21:07:32 -04001936}
1937
1938/*
1939 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1940 * and update the nfs4_state.
1941 */
1942static struct nfs4_state *
1943_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1944{
1945 struct inode *inode = data->state->inode;
1946 struct nfs4_state *state = data->state;
1947 int ret;
1948
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001949 if (!data->rpc_done) {
Anna Schumaker37a84842017-01-11 16:08:35 -05001950 if (data->rpc_status)
1951 return ERR_PTR(data->rpc_status);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001952 /* cached opens have already been processed */
1953 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001954 }
1955
Andy Adamsone23008e2012-10-02 21:07:32 -04001956 ret = nfs_refresh_inode(inode, &data->f_attr);
1957 if (ret)
Anna Schumaker37a84842017-01-11 16:08:35 -05001958 return ERR_PTR(ret);
Andy Adamsone23008e2012-10-02 21:07:32 -04001959
1960 if (data->o_res.delegation_type != 0)
1961 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001962update:
Trond Myklebuste3c8dc72019-07-29 18:25:00 +01001963 if (!update_open_stateid(state, &data->o_res.stateid,
1964 NULL, data->o_arg.fmode))
1965 return ERR_PTR(-EAGAIN);
Trond Myklebustace9fad2018-09-02 19:19:07 -04001966 refcount_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001967
1968 return state;
Andy Adamsone23008e2012-10-02 21:07:32 -04001969}
1970
Trond Myklebust4e2fcac2017-08-08 09:06:18 -04001971static struct inode *
1972nfs4_opendata_get_inode(struct nfs4_opendata *data)
1973{
1974 struct inode *inode;
1975
1976 switch (data->o_arg.claim) {
1977 case NFS4_OPEN_CLAIM_NULL:
1978 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1979 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1980 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1981 return ERR_PTR(-EAGAIN);
1982 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh,
1983 &data->f_attr, data->f_label);
1984 break;
1985 default:
1986 inode = d_inode(data->dentry);
1987 ihold(inode);
1988 nfs_refresh_inode(inode, &data->f_attr);
1989 }
1990 return inode;
1991}
1992
Andy Adamsone23008e2012-10-02 21:07:32 -04001993static struct nfs4_state *
Trond Myklebust75e8c482017-08-08 10:38:07 -04001994nfs4_opendata_find_nfs4_state(struct nfs4_opendata *data)
1995{
1996 struct nfs4_state *state;
1997 struct inode *inode;
1998
1999 inode = nfs4_opendata_get_inode(data);
2000 if (IS_ERR(inode))
2001 return ERR_CAST(inode);
2002 if (data->state != NULL && data->state->inode == inode) {
2003 state = data->state;
Trond Myklebustace9fad2018-09-02 19:19:07 -04002004 refcount_inc(&state->count);
Trond Myklebust75e8c482017-08-08 10:38:07 -04002005 } else
2006 state = nfs4_get_open_state(inode, data->owner);
2007 iput(inode);
2008 if (state == NULL)
2009 state = ERR_PTR(-ENOMEM);
2010 return state;
2011}
2012
Andy Adamsone23008e2012-10-02 21:07:32 -04002013static struct nfs4_state *
2014_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002015{
Trond Myklebust75e8c482017-08-08 10:38:07 -04002016 struct nfs4_state *state;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002017
Trond Myklebustaac00a82007-07-05 19:02:21 -04002018 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04002019 state = nfs4_try_open_cached(data);
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002020 trace_nfs4_cached_open(data->state);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002021 goto out;
2022 }
2023
Trond Myklebust75e8c482017-08-08 10:38:07 -04002024 state = nfs4_opendata_find_nfs4_state(data);
2025 if (IS_ERR(state))
2026 goto out;
2027
Andy Adamsone23008e2012-10-02 21:07:32 -04002028 if (data->o_res.delegation_type != 0)
2029 nfs4_opendata_check_deleg(data, state);
Trond Myklebuste3c8dc72019-07-29 18:25:00 +01002030 if (!update_open_stateid(state, &data->o_res.stateid,
2031 NULL, data->o_arg.fmode)) {
2032 nfs4_put_open_state(state);
2033 state = ERR_PTR(-EAGAIN);
2034 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002035out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08002036 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002037 return state;
2038}
2039
Andy Adamsone23008e2012-10-02 21:07:32 -04002040static struct nfs4_state *
2041nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
2042{
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002043 struct nfs4_state *ret;
2044
Andy Adamsone23008e2012-10-02 21:07:32 -04002045 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002046 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
2047 else
2048 ret = _nfs4_opendata_to_nfs4_state(data);
2049 nfs4_sequence_free_slot(&data->o_res.seq_res);
2050 return ret;
Andy Adamsone23008e2012-10-02 21:07:32 -04002051}
2052
Trond Myklebust0de43972018-09-02 15:57:01 -04002053static struct nfs_open_context *
2054nfs4_state_find_open_context_mode(struct nfs4_state *state, fmode_t mode)
Trond Myklebust864472e2006-01-03 09:55:15 +01002055{
2056 struct nfs_inode *nfsi = NFS_I(state->inode);
2057 struct nfs_open_context *ctx;
2058
Trond Myklebust0de43972018-09-02 15:57:01 -04002059 rcu_read_lock();
2060 list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
Trond Myklebust864472e2006-01-03 09:55:15 +01002061 if (ctx->state != state)
2062 continue;
Trond Myklebust0de43972018-09-02 15:57:01 -04002063 if ((ctx->mode & mode) != mode)
2064 continue;
2065 if (!get_nfs_open_context(ctx))
2066 continue;
2067 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01002068 return ctx;
2069 }
Trond Myklebust0de43972018-09-02 15:57:01 -04002070 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01002071 return ERR_PTR(-ENOENT);
2072}
2073
Trond Myklebust0de43972018-09-02 15:57:01 -04002074static struct nfs_open_context *
2075nfs4_state_find_open_context(struct nfs4_state *state)
2076{
2077 struct nfs_open_context *ctx;
2078
2079 ctx = nfs4_state_find_open_context_mode(state, FMODE_READ|FMODE_WRITE);
2080 if (!IS_ERR(ctx))
2081 return ctx;
2082 ctx = nfs4_state_find_open_context_mode(state, FMODE_WRITE);
2083 if (!IS_ERR(ctx))
2084 return ctx;
2085 return nfs4_state_find_open_context_mode(state, FMODE_READ);
2086}
2087
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002088static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
2089 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002090{
2091 struct nfs4_opendata *opendata;
2092
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002093 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002094 NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002095 if (opendata == NULL)
2096 return ERR_PTR(-ENOMEM);
2097 opendata->state = state;
Trond Myklebustace9fad2018-09-02 19:19:07 -04002098 refcount_inc(&state->count);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002099 return opendata;
2100}
2101
Trond Myklebust24311f82015-09-20 10:50:17 -04002102static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
2103 fmode_t fmode)
Trond Myklebust864472e2006-01-03 09:55:15 +01002104{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002105 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01002106 int ret;
2107
Trond Myklebust24311f82015-09-20 10:50:17 -04002108 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
NeilBrown39f897f2015-06-29 14:28:54 +10002109 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002110 opendata->o_arg.open_flags = 0;
2111 opendata->o_arg.fmode = fmode;
Trond Myklebustbe36e182015-02-27 17:04:17 -05002112 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
2113 NFS_SB(opendata->dentry->d_sb),
2114 fmode, 0);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002115 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
2116 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
2117 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002118 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002119 if (ret != 0)
2120 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002121 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002122 if (IS_ERR(newstate))
2123 return PTR_ERR(newstate);
Trond Myklebust24311f82015-09-20 10:50:17 -04002124 if (newstate != opendata->state)
2125 ret = -ESTALE;
Al Viro643168c2011-06-22 18:20:23 -04002126 nfs4_close_state(newstate, fmode);
Trond Myklebust24311f82015-09-20 10:50:17 -04002127 return ret;
Trond Myklebust864472e2006-01-03 09:55:15 +01002128}
2129
2130static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
2131{
Trond Myklebust864472e2006-01-03 09:55:15 +01002132 int ret;
2133
2134 /* memory barrier prior to reading state->n_* */
2135 smp_rmb();
Trond Myklebust24311f82015-09-20 10:50:17 -04002136 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2137 if (ret != 0)
2138 return ret;
2139 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2140 if (ret != 0)
2141 return ret;
2142 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
2143 if (ret != 0)
2144 return ret;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002145 /*
2146 * We may have performed cached opens for all three recoveries.
2147 * Check if we need to update the current stateid.
2148 */
2149 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05002150 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04002151 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002152 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05002153 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04002154 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002155 }
Trond Myklebust864472e2006-01-03 09:55:15 +01002156 return 0;
2157}
2158
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159/*
2160 * OPEN_RECLAIM:
2161 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002163static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002165 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01002166 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002167 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 int status;
2169
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002170 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2171 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002172 if (IS_ERR(opendata))
2173 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002174 rcu_read_lock();
2175 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05002176 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04002177 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002178 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01002179 opendata->o_arg.u.delegation_type = delegation_type;
2180 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002181 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 return status;
2183}
2184
Trond Myklebust539cd032007-06-05 11:46:42 -04002185static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186{
2187 struct nfs_server *server = NFS_SERVER(state->inode);
2188 struct nfs4_exception exception = { };
2189 int err;
2190 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002191 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002192 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002193 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2194 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04002195 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002196 break;
2197 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 } while (exception.retry);
2199 return err;
2200}
2201
Trond Myklebust864472e2006-01-03 09:55:15 +01002202static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
2203{
2204 struct nfs_open_context *ctx;
2205 int ret;
2206
2207 ctx = nfs4_state_find_open_context(state);
2208 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002209 return -EAGAIN;
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002210 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2211 nfs_state_clear_open_state_flags(state);
Trond Myklebust539cd032007-06-05 11:46:42 -04002212 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002213 put_nfs_open_context(ctx);
2214 return ret;
2215}
2216
NeilBrowndce26302017-12-13 09:57:09 +11002217static 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 -07002218{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002219 switch (err) {
2220 default:
2221 printk(KERN_ERR "NFS: %s: unhandled error "
2222 "%d.\n", __func__, err);
Gustavo A. R. Silvaffb81712020-11-20 12:26:46 -06002223 fallthrough;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002224 case 0:
2225 case -ENOENT:
Trond Myklebust8eee52a2015-06-04 13:51:13 -04002226 case -EAGAIN:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002227 case -ESTALE:
Trond Myklebust67e7b522019-08-07 07:31:27 -04002228 case -ETIMEDOUT:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002229 break;
2230 case -NFS4ERR_BADSESSION:
2231 case -NFS4ERR_BADSLOT:
2232 case -NFS4ERR_BAD_HIGH_SLOT:
2233 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
2234 case -NFS4ERR_DEADSESSION:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002235 return -EAGAIN;
2236 case -NFS4ERR_STALE_CLIENTID:
2237 case -NFS4ERR_STALE_STATEID:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002238 /* Don't recall a delegation if it was lost */
2239 nfs4_schedule_lease_recovery(server->nfs_client);
2240 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04002241 case -NFS4ERR_MOVED:
2242 nfs4_schedule_migration_recovery(server);
2243 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04002244 case -NFS4ERR_LEASE_MOVED:
2245 nfs4_schedule_lease_moved_recovery(server->nfs_client);
2246 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002247 case -NFS4ERR_DELEG_REVOKED:
2248 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust404ea3562016-09-22 13:39:08 -04002249 case -NFS4ERR_EXPIRED:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002250 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002251 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002252 nfs_inode_find_state_and_recover(state->inode,
2253 stateid);
2254 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05002255 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002256 case -NFS4ERR_DELAY:
2257 case -NFS4ERR_GRACE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002258 ssleep(1);
2259 return -EAGAIN;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002260 case -ENOMEM:
2261 case -NFS4ERR_DENIED:
NeilBrowndce26302017-12-13 09:57:09 +11002262 if (fl) {
2263 struct nfs4_lock_state *lsp = fl->fl_u.nfs4_fl.owner;
2264 if (lsp)
2265 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2266 }
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002267 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002268 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 return err;
2270}
2271
Trond Myklebust24311f82015-09-20 10:50:17 -04002272int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002273 struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002274{
2275 struct nfs_server *server = NFS_SERVER(state->inode);
2276 struct nfs4_opendata *opendata;
Trond Myklebust24311f82015-09-20 10:50:17 -04002277 int err = 0;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002278
2279 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2280 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2281 if (IS_ERR(opendata))
2282 return PTR_ERR(opendata);
2283 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002284 if (!test_bit(NFS_O_RDWR_STATE, &state->flags)) {
Trond Myklebust24311f82015-09-20 10:50:17 -04002285 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2286 if (err)
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002287 goto out;
2288 }
2289 if (!test_bit(NFS_O_WRONLY_STATE, &state->flags)) {
Trond Myklebust24311f82015-09-20 10:50:17 -04002290 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2291 if (err)
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002292 goto out;
Trond Myklebust24311f82015-09-20 10:50:17 -04002293 }
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002294 if (!test_bit(NFS_O_RDONLY_STATE, &state->flags)) {
2295 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2296 if (err)
2297 goto out;
2298 }
2299 nfs_state_clear_delegation(state);
2300out:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002301 nfs4_opendata_put(opendata);
NeilBrowndce26302017-12-13 09:57:09 +11002302 return nfs4_handle_delegation_recall_error(server, state, stateid, NULL, err);
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002303}
2304
Chuck Leverbe05c862013-08-09 12:49:47 -04002305static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2306{
2307 struct nfs4_opendata *data = calldata;
2308
Anna Schumaker7981c8a2017-01-10 11:39:53 -05002309 nfs4_setup_sequence(data->o_arg.server->nfs_client,
2310 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04002311}
2312
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002313static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2314{
2315 struct nfs4_opendata *data = calldata;
2316
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002317 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04002318
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002319 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002320 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05002321 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05002322 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002323 renew_lease(data->o_res.server, data->timestamp);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002324 data->rpc_done = true;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002325 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002326}
2327
2328static void nfs4_open_confirm_release(void *calldata)
2329{
2330 struct nfs4_opendata *data = calldata;
2331 struct nfs4_state *state = NULL;
2332
2333 /* If this request hasn't been cancelled, do nothing */
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002334 if (!data->cancelled)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002335 goto out_free;
2336 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002337 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002338 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002339 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002340 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002341 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002342out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002343 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002344}
2345
2346static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04002347 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002348 .rpc_call_done = nfs4_open_confirm_done,
2349 .rpc_release = nfs4_open_confirm_release,
2350};
2351
2352/*
2353 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2354 */
2355static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2356{
David Howells2b0143b2015-03-17 22:25:59 +00002357 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002358 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002359 struct rpc_message msg = {
2360 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2361 .rpc_argp = &data->c_arg,
2362 .rpc_resp = &data->c_res,
2363 .rpc_cred = data->owner->so_cred,
2364 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002365 struct rpc_task_setup task_setup_data = {
2366 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002367 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002368 .callback_ops = &nfs4_open_confirm_ops,
2369 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002370 .workqueue = nfsiod_workqueue,
Trond Myklebust61296502020-02-07 19:38:12 -05002371 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002372 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 int status;
2374
Anna Schumakerfba83f32018-05-04 16:22:50 -04002375 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1,
2376 data->is_recover);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002377 kref_get(&data->kref);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002378 data->rpc_done = false;
Trond Myklebust3e309912007-07-07 13:19:59 -04002379 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002380 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002381 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05002382 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002383 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002384 status = rpc_wait_for_completion_task(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002385 if (status != 0) {
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002386 data->cancelled = true;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002387 smp_wmb();
2388 } else
2389 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05002390 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 return status;
2392}
2393
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002394static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002396 struct nfs4_opendata *data = calldata;
2397 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04002398 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust2a606182015-08-19 22:30:00 -05002399 enum open_claim_type4 claim = data->o_arg.claim;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002400
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002401 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002402 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002403 /*
2404 * Check if we still need to send an OPEN call, or if we can use
2405 * a delegation instead.
2406 */
2407 if (data->state != NULL) {
2408 struct nfs_delegation *delegation;
2409
Trond Myklebustbe189f72018-09-27 17:12:33 -04002410 if (can_open_cached(data->state, data->o_arg.fmode,
2411 data->o_arg.open_flags, claim))
Trond Myklebust6ee41262007-07-08 14:11:36 -04002412 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002413 rcu_read_lock();
Trond Myklebustbe3df3d2019-10-31 18:40:32 -04002414 delegation = nfs4_get_valid_delegation(data->state->inode);
Trond Myklebust2a606182015-08-19 22:30:00 -05002415 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
Trond Myklebust652f89f2011-12-09 19:05:58 -05002416 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002417 rcu_read_unlock();
2418 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04002419 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04002420 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust2a606182015-08-19 22:30:00 -05002421 switch (claim) {
2422 default:
2423 break;
Trond Myklebust8188df12013-04-23 14:31:19 -04002424 case NFS4_OPEN_CLAIM_PREVIOUS:
2425 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2426 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04002427 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002428 fallthrough;
Trond Myklebust8188df12013-04-23 14:31:19 -04002429 case NFS4_OPEN_CLAIM_FH:
2430 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002431 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002432 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05002433 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
Andy Adamsond8985282009-04-01 09:22:21 -04002434 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002435 &data->o_res.seq_res,
2436 task) != 0)
2437 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04002438
2439 /* Set the create mode (note dependency on the session type) */
2440 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2441 if (data->o_arg.open_flags & O_EXCL) {
2442 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2443 if (nfs4_has_persistent_session(clp))
2444 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2445 else if (clp->cl_mvops->minor_version > 0)
2446 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2447 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04002448 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05002449unlock_no_action:
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002450 trace_nfs4_cached_open(data->state);
Trond Myklebust652f89f2011-12-09 19:05:58 -05002451 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04002452out_no_action:
2453 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002454out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002455 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002456}
2457
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002458static void nfs4_open_done(struct rpc_task *task, void *calldata)
2459{
2460 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002462 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04002463
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002464 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04002465 return;
Andy Adamsond8985282009-04-01 09:22:21 -04002466
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002467 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07002468 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2469 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07002470 case S_IFREG:
2471 break;
2472 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002473 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002474 break;
2475 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002476 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002477 break;
2478 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002479 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07002480 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07002481 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002482 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04002483 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2484 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07002485 }
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002486 data->rpc_done = true;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002487}
Trond Myklebust6f926b52005-10-18 14:20:18 -07002488
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002489static void nfs4_open_release(void *calldata)
2490{
2491 struct nfs4_opendata *data = calldata;
2492 struct nfs4_state *state = NULL;
2493
2494 /* If this request hasn't been cancelled, do nothing */
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002495 if (!data->cancelled)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002496 goto out_free;
2497 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002498 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002499 goto out_free;
2500 /* In case we need an open_confirm, no cleanup! */
2501 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2502 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002503 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002504 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002505 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002506out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002507 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002508}
2509
2510static const struct rpc_call_ops nfs4_open_ops = {
2511 .rpc_call_prepare = nfs4_open_prepare,
2512 .rpc_call_done = nfs4_open_done,
2513 .rpc_release = nfs4_open_release,
2514};
2515
Fred Isaman3b65a302016-09-19 10:06:49 -04002516static int nfs4_run_open_task(struct nfs4_opendata *data,
2517 struct nfs_open_context *ctx)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002518{
David Howells2b0143b2015-03-17 22:25:59 +00002519 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002520 struct nfs_server *server = NFS_SERVER(dir);
2521 struct nfs_openargs *o_arg = &data->o_arg;
2522 struct nfs_openres *o_res = &data->o_res;
2523 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002524 struct rpc_message msg = {
2525 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2526 .rpc_argp = o_arg,
2527 .rpc_resp = o_res,
2528 .rpc_cred = data->owner->so_cred,
2529 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002530 struct rpc_task_setup task_setup_data = {
2531 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002532 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002533 .callback_ops = &nfs4_open_ops,
2534 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002535 .workqueue = nfsiod_workqueue,
Trond Myklebust61296502020-02-07 19:38:12 -05002536 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002537 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002538 int status;
2539
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002540 kref_get(&data->kref);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002541 data->rpc_done = false;
Trond Myklebust3e309912007-07-07 13:19:59 -04002542 data->rpc_status = 0;
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002543 data->cancelled = false;
2544 data->is_recover = false;
Fred Isaman3b65a302016-09-19 10:06:49 -04002545 if (!ctx) {
2546 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 1);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002547 data->is_recover = true;
Trond Myklebust67e7b522019-08-07 07:31:27 -04002548 task_setup_data.flags |= RPC_TASK_TIMEOUT;
Fred Isaman2409a972016-10-06 12:11:21 -04002549 } else {
Fred Isaman3b65a302016-09-19 10:06:49 -04002550 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 0);
Fred Isaman2409a972016-10-06 12:11:21 -04002551 pnfs_lgopen_prepare(data, ctx);
2552 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04002553 task = rpc_run_task(&task_setup_data);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002554 if (IS_ERR(task))
2555 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002556 status = rpc_wait_for_completion_task(task);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002557 if (status != 0) {
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002558 data->cancelled = true;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002559 smp_wmb();
2560 } else
2561 status = data->rpc_status;
2562 rpc_put_task(task);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002563
2564 return status;
2565}
2566
2567static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2568{
David Howells2b0143b2015-03-17 22:25:59 +00002569 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002570 struct nfs_openres *o_res = &data->o_res;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002571 int status;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002572
Fred Isaman3b65a302016-09-19 10:06:49 -04002573 status = nfs4_run_open_task(data, NULL);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002574 if (status != 0 || !data->rpc_done)
2575 return status;
2576
Trond Myklebust6926afd12012-01-07 13:22:46 -05002577 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2578
Anna Schumakerd7e98252017-01-11 16:13:29 -05002579 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002580 status = _nfs4_proc_open_confirm(data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002581
2582 return status;
2583}
2584
Trond Myklebustf3792d62014-07-10 08:54:32 -04002585/*
2586 * Additional permission checks in order to distinguish between an
2587 * open for read, and an open for execute. This works around the
2588 * fact that NFSv4 OPEN treats read and execute permissions as being
2589 * the same.
2590 * Note that in the non-execute case, we want to turn off permission
2591 * checking if we just created a new file (POSIX open() semantics).
2592 */
NeilBrowna52458b2018-12-03 11:30:31 +11002593static int nfs4_opendata_access(const struct cred *cred,
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002594 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002595 struct nfs4_state *state, fmode_t fmode,
2596 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002597{
2598 struct nfs_access_entry cache;
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002599 u32 mask, flags;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002600
2601 /* access call failed or for some reason the server doesn't
2602 * support any access modes -- defer access call until later */
2603 if (opendata->o_res.access_supported == 0)
2604 return 0;
2605
2606 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002607 /*
2608 * Use openflags to check for exec, because fmode won't
2609 * always have FMODE_EXEC set when file open for exec.
2610 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002611 if (openflags & __FMODE_EXEC) {
2612 /* ONLY check for exec rights */
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002613 if (S_ISDIR(state->inode->i_mode))
2614 mask = NFS4_ACCESS_LOOKUP;
2615 else
2616 mask = NFS4_ACCESS_EXECUTE;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002617 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002618 mask = NFS4_ACCESS_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002619
2620 cache.cred = cred;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002621 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2622 nfs_access_add_cache(state->inode, &cache);
2623
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002624 flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP;
2625 if ((mask & ~cache.mask & flags) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002626 return 0;
2627
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002628 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002629}
2630
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002631/*
2632 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2633 */
Fred Isaman3b65a302016-09-19 10:06:49 -04002634static int _nfs4_proc_open(struct nfs4_opendata *data,
2635 struct nfs_open_context *ctx)
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002636{
David Howells2b0143b2015-03-17 22:25:59 +00002637 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002638 struct nfs_server *server = NFS_SERVER(dir);
2639 struct nfs_openargs *o_arg = &data->o_arg;
2640 struct nfs_openres *o_res = &data->o_res;
2641 int status;
2642
Fred Isaman3b65a302016-09-19 10:06:49 -04002643 status = nfs4_run_open_task(data, ctx);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002644 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002645 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002646 if (status != 0) {
2647 if (status == -NFS4ERR_BADNAME &&
2648 !(o_arg->open_flags & O_CREAT))
2649 return -ENOENT;
2650 return status;
2651 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002652
Trond Myklebust6926afd12012-01-07 13:22:46 -05002653 nfs_fattr_map_and_free_names(server, &data->f_attr);
2654
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002655 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002656 if (o_arg->open_flags & O_EXCL)
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002657 data->file_created = true;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002658 else if (o_res->cinfo.before != o_res->cinfo.after)
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002659 data->file_created = true;
Jeff Layton1eb5d982018-01-09 08:21:17 -05002660 if (data->file_created ||
2661 inode_peek_iversion_raw(dir) != o_res->cinfo.after)
Frank van der Linden1b523ca2020-06-23 22:38:59 +00002662 nfs4_update_changeattr(dir, &o_res->cinfo,
2663 o_res->f_attr->time_start,
2664 NFS_INO_INVALID_DATA);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002665 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002666 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2667 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002669 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002671 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 }
Trond Myklebust56e0d712017-04-15 19:20:01 -04002673 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2674 nfs4_sequence_free_slot(&o_res->seq_res);
Trond Myklebusta841b542018-04-07 13:50:59 -04002675 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr,
2676 o_res->f_label, NULL);
Trond Myklebust56e0d712017-04-15 19:20:01 -04002677 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002678 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679}
2680
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681/*
2682 * OPEN_EXPIRED:
2683 * reclaim state on the server after a network partition.
2684 * Assumes caller holds the appropriate lock
2685 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002686static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002688 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002689 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002691 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002692 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002693 if (IS_ERR(opendata))
2694 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002695 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002696 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002697 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002698 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002699 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700}
2701
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002702static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002703{
Trond Myklebust539cd032007-06-05 11:46:42 -04002704 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002705 struct nfs4_exception exception = { };
2706 int err;
2707
2708 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002709 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002710 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002711 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2712 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002713 switch (err) {
2714 default:
2715 goto out;
2716 case -NFS4ERR_GRACE:
2717 case -NFS4ERR_DELAY:
2718 nfs4_handle_exception(server, err, &exception);
2719 err = 0;
2720 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002721 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002722out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002723 return err;
2724}
2725
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2727{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002729 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730
Trond Myklebust864472e2006-01-03 09:55:15 +01002731 ctx = nfs4_state_find_open_context(state);
2732 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002733 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002734 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002735 put_nfs_open_context(ctx);
2736 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737}
2738
Trond Myklebust41020b62016-09-22 13:38:58 -04002739static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2740 const nfs4_stateid *stateid)
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002741{
Trond Myklebust41020b62016-09-22 13:38:58 -04002742 nfs_remove_bad_delegation(state->inode, stateid);
Trond Myklebust9f0c5122018-09-05 14:07:15 -04002743 nfs_state_clear_delegation(state);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002744}
2745
2746static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2747{
2748 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
Trond Myklebust41020b62016-09-22 13:38:58 -04002749 nfs_finish_clear_delegation_stateid(state, NULL);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002750}
2751
2752static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2753{
2754 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2755 nfs40_clear_delegation_stateid(state);
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002756 nfs_state_clear_open_state_flags(state);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002757 return nfs4_open_expired(sp, state);
2758}
2759
Trond Myklebust45870d62016-09-22 13:38:59 -04002760static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2761 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11002762 const struct cred *cred)
Trond Myklebust45870d62016-09-22 13:38:59 -04002763{
2764 return -NFS4ERR_BAD_STATEID;
2765}
2766
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002767#if defined(CONFIG_NFS_V4_1)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002768static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2769 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11002770 const struct cred *cred)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002771{
2772 int status;
2773
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002774 switch (stateid->type) {
2775 default:
2776 break;
2777 case NFS4_INVALID_STATEID_TYPE:
2778 case NFS4_SPECIAL_STATEID_TYPE:
2779 return -NFS4ERR_BAD_STATEID;
2780 case NFS4_REVOKED_STATEID_TYPE:
2781 goto out_free;
2782 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002783
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002784 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002785 switch (status) {
2786 case -NFS4ERR_EXPIRED:
2787 case -NFS4ERR_ADMIN_REVOKED:
2788 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002789 break;
2790 default:
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002791 return status;
2792 }
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002793out_free:
2794 /* Ack the revoked state to the server */
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04002795 nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002796 return -NFS4ERR_EXPIRED;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002797}
2798
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002799static int nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002800{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002801 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002802 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002803 struct nfs_delegation *delegation;
NeilBrowna52458b2018-12-03 11:30:31 +11002804 const struct cred *cred = NULL;
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002805 int status, ret = NFS_OK;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002806
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002807 /* Get the delegation credential for use by test/free_stateid */
2808 rcu_read_lock();
2809 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002810 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002811 rcu_read_unlock();
Trond Myklebust9f0c5122018-09-05 14:07:15 -04002812 nfs_state_clear_delegation(state);
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002813 return NFS_OK;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002814 }
2815
Trond Myklebustfc51b1c2020-04-02 15:27:09 -04002816 spin_lock(&delegation->lock);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002817 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002818
Trond Myklebust994b15b2018-09-05 14:07:14 -04002819 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2820 &delegation->flags)) {
Trond Myklebustfc51b1c2020-04-02 15:27:09 -04002821 spin_unlock(&delegation->lock);
Trond Myklebust994b15b2018-09-05 14:07:14 -04002822 rcu_read_unlock();
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002823 return NFS_OK;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002824 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002825
NeilBrowna52458b2018-12-03 11:30:31 +11002826 if (delegation->cred)
2827 cred = get_cred(delegation->cred);
Trond Myklebustfc51b1c2020-04-02 15:27:09 -04002828 spin_unlock(&delegation->lock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002829 rcu_read_unlock();
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002830 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002831 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002832 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
Trond Myklebust41020b62016-09-22 13:38:58 -04002833 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002834 else
2835 ret = status;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002836
Trond Myklebust8c39a392019-07-19 13:48:44 -04002837 put_cred(cred);
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002838 return ret;
2839}
2840
2841static void nfs41_delegation_recover_stateid(struct nfs4_state *state)
2842{
2843 nfs4_stateid tmp;
2844
2845 if (test_bit(NFS_DELEGATED_STATE, &state->flags) &&
2846 nfs4_copy_delegation_stateid(state->inode, state->state,
2847 &tmp, NULL) &&
2848 nfs4_stateid_match_other(&state->stateid, &tmp))
2849 nfs_state_set_delegation(state, &tmp, state->state);
2850 else
2851 nfs_state_clear_delegation(state);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002852}
2853
2854/**
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002855 * nfs41_check_expired_locks - possibly free a lock stateid
2856 *
2857 * @state: NFSv4 state for an inode
2858 *
2859 * Returns NFS_OK if recovery for this stateid is now finished.
2860 * Otherwise a negative NFS4ERR value is returned.
2861 */
2862static int nfs41_check_expired_locks(struct nfs4_state *state)
2863{
2864 int status, ret = NFS_OK;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002865 struct nfs4_lock_state *lsp, *prev = NULL;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002866 struct nfs_server *server = NFS_SERVER(state->inode);
2867
2868 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2869 goto out;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002870
2871 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002872 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2873 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
NeilBrowna52458b2018-12-03 11:30:31 +11002874 const struct cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002875
Elena Reshetova194bc1f2017-10-20 12:53:36 +03002876 refcount_inc(&lsp->ls_count);
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002877 spin_unlock(&state->state_lock);
2878
2879 nfs4_put_lock_state(prev);
2880 prev = lsp;
2881
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002882 status = nfs41_test_and_free_expired_stateid(server,
2883 &lsp->ls_stateid,
2884 cred);
2885 trace_nfs4_test_lock_stateid(state, lsp, status);
2886 if (status == -NFS4ERR_EXPIRED ||
2887 status == -NFS4ERR_BAD_STATEID) {
2888 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002889 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002890 if (!recover_lost_locks)
2891 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2892 } else if (status != NFS_OK) {
2893 ret = status;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002894 nfs4_put_lock_state(prev);
2895 goto out;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002896 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002897 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002898 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002899 }
2900 spin_unlock(&state->state_lock);
2901 nfs4_put_lock_state(prev);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002902out:
2903 return ret;
2904}
2905
2906/**
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002907 * nfs41_check_open_stateid - possibly free an open stateid
2908 *
2909 * @state: NFSv4 state for an inode
2910 *
2911 * Returns NFS_OK if recovery for this stateid is now finished.
2912 * Otherwise a negative NFS4ERR value is returned.
2913 */
2914static int nfs41_check_open_stateid(struct nfs4_state *state)
2915{
2916 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002917 nfs4_stateid *stateid = &state->open_stateid;
NeilBrowna52458b2018-12-03 11:30:31 +11002918 const struct cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002919 int status;
2920
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002921 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002922 return -NFS4ERR_BAD_STATEID;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002923 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002924 trace_nfs4_test_open_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002925 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002926 nfs_state_clear_open_state_flags(state);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002927 stateid->type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002928 return status;
Trond Myklebustc0ca0e52017-08-08 21:39:28 -04002929 }
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002930 if (nfs_open_stateid_recover_openmode(state))
2931 return -NFS4ERR_OPENMODE;
2932 return NFS_OK;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002933}
2934
2935static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2936{
Chuck Levereb64cf92012-07-11 16:30:05 -04002937 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002938
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002939 status = nfs41_check_delegation_stateid(state);
2940 if (status != NFS_OK)
2941 return status;
2942 nfs41_delegation_recover_stateid(state);
2943
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002944 status = nfs41_check_expired_locks(state);
2945 if (status != NFS_OK)
2946 return status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002947 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002948 if (status != NFS_OK)
2949 status = nfs4_open_expired(sp, state);
2950 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002951}
2952#endif
2953
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002955 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2956 * fields corresponding to attributes that were used to store the verifier.
2957 * Make sure we clobber those fields in the later setattr call
2958 */
Trond Myklebust609339c2018-03-28 16:18:17 -04002959static unsigned nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002960 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002961{
Trond Myklebust609339c2018-03-28 16:18:17 -04002962 const __u32 *bitmask = opendata->o_arg.server->exclcreat_bitmask;
2963 __u32 attrset[3];
2964 unsigned ret;
2965 unsigned i;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002966
Trond Myklebust609339c2018-03-28 16:18:17 -04002967 for (i = 0; i < ARRAY_SIZE(attrset); i++) {
2968 attrset[i] = opendata->o_res.attrset[i];
2969 if (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE4_1)
2970 attrset[i] &= ~bitmask[i];
2971 }
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002972
Trond Myklebust609339c2018-03-28 16:18:17 -04002973 ret = (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE) ?
2974 sattr->ia_valid : 0;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002975
Trond Myklebust609339c2018-03-28 16:18:17 -04002976 if ((attrset[1] & (FATTR4_WORD1_TIME_ACCESS|FATTR4_WORD1_TIME_ACCESS_SET))) {
2977 if (sattr->ia_valid & ATTR_ATIME_SET)
2978 ret |= ATTR_ATIME_SET;
2979 else
2980 ret |= ATTR_ATIME;
2981 }
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002982
Trond Myklebust609339c2018-03-28 16:18:17 -04002983 if ((attrset[1] & (FATTR4_WORD1_TIME_MODIFY|FATTR4_WORD1_TIME_MODIFY_SET))) {
2984 if (sattr->ia_valid & ATTR_MTIME_SET)
2985 ret |= ATTR_MTIME_SET;
2986 else
2987 ret |= ATTR_MTIME;
2988 }
2989
2990 if (!(attrset[2] & FATTR4_WORD2_SECURITY_LABEL))
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002991 *label = NULL;
Trond Myklebust609339c2018-03-28 16:18:17 -04002992 return ret;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002993}
2994
Trond Myklebustc21443c2013-02-07 14:26:21 -05002995static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
Trond Myklebust1bf85d82019-06-27 06:30:48 -04002996 int flags, struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002997{
2998 struct nfs4_state_owner *sp = opendata->owner;
2999 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04003000 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05003001 struct nfs4_state *state;
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003002 fmode_t acc_mode = _nfs4_ctx_to_accessmode(ctx);
Trond Myklebustcf5b4052020-02-05 09:01:53 -05003003 struct inode *dir = d_inode(opendata->dir);
3004 unsigned long dir_verifier;
Trond Myklebustc21443c2013-02-07 14:26:21 -05003005 unsigned int seq;
3006 int ret;
3007
3008 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
Trond Myklebustcf5b4052020-02-05 09:01:53 -05003009 dir_verifier = nfs_save_change_attribute(dir);
Trond Myklebustc21443c2013-02-07 14:26:21 -05003010
Fred Isaman3b65a302016-09-19 10:06:49 -04003011 ret = _nfs4_proc_open(opendata, ctx);
Trond Myklebustdca780012014-10-23 19:23:03 +03003012 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05003013 goto out;
3014
Trond Myklebustae55e592018-05-22 11:17:16 -04003015 state = _nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebustc21443c2013-02-07 14:26:21 -05003016 ret = PTR_ERR(state);
3017 if (IS_ERR(state))
3018 goto out;
Trond Myklebusta974dee2017-02-08 11:29:46 -05003019 ctx->state = state;
Trond Myklebustc21443c2013-02-07 14:26:21 -05003020 if (server->caps & NFS_CAP_POSIX_LOCK)
3021 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Jeff Laytona8ce3772016-09-17 18:17:35 -04003022 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
3023 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05003024
Trond Myklebust275bb302013-05-29 13:11:28 -04003025 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00003026 if (d_really_is_negative(dentry)) {
Al Viro668d0cd2016-03-08 12:44:17 -05003027 struct dentry *alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04003028 d_drop(dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05003029 alias = d_exact_alias(dentry, state->inode);
3030 if (!alias)
3031 alias = d_splice_alias(igrab(state->inode), dentry);
3032 /* d_splice_alias() can't fail here - it's a non-directory */
3033 if (alias) {
Trond Myklebust275bb302013-05-29 13:11:28 -04003034 dput(ctx->dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05003035 ctx->dentry = dentry = alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04003036 }
Trond Myklebustcf5b4052020-02-05 09:01:53 -05003037 }
3038
3039 switch(opendata->o_arg.claim) {
3040 default:
3041 break;
3042 case NFS4_OPEN_CLAIM_NULL:
3043 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
3044 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
3045 if (!opendata->rpc_done)
3046 break;
3047 if (opendata->o_res.delegation_type != 0)
3048 dir_verifier = nfs_save_change_attribute(dir);
3049 nfs_set_verifier(dentry, dir_verifier);
Trond Myklebust275bb302013-05-29 13:11:28 -04003050 }
3051
Trond Myklebustaf9b6d72018-06-29 12:45:53 -04003052 /* Parse layoutget results before we check for access */
3053 pnfs_parse_lgopen(state->inode, opendata->lgp, ctx);
3054
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003055 ret = nfs4_opendata_access(sp->so_cred, opendata, state,
3056 acc_mode, flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05003057 if (ret != 0)
3058 goto out;
3059
David Howells2b0143b2015-03-17 22:25:59 +00003060 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04003061 nfs_inode_attach_open_context(ctx);
3062 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
3063 nfs4_schedule_stateid_recovery(server, state);
3064 }
Fred Isaman2409a972016-10-06 12:11:21 -04003065
Trond Myklebustc21443c2013-02-07 14:26:21 -05003066out:
Olga Kornievskaia0cb98ab2019-03-19 12:12:13 -04003067 if (!opendata->cancelled)
3068 nfs4_sequence_free_slot(&opendata->o_res.seq_res);
Trond Myklebustc21443c2013-02-07 14:26:21 -05003069 return ret;
3070}
3071
Jeff Laytonaa53ed52007-06-05 14:49:03 -04003072/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01003073 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 */
Andy Adamson82be4172012-05-23 05:02:35 -04003075static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04003076 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04003077 int flags,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003078 const struct nfs4_open_createattrs *c,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003079 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080{
3081 struct nfs4_state_owner *sp;
3082 struct nfs4_state *state = NULL;
3083 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01003084 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04003085 struct dentry *dentry = ctx->dentry;
NeilBrowna52458b2018-12-03 11:30:31 +11003086 const struct cred *cred = ctx->cred;
Trond Myklebust4197a052013-05-29 12:37:49 -04003087 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003088 fmode_t fmode = _nfs4_ctx_to_openmode(ctx);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003089 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003090 struct iattr *sattr = c->sattr;
3091 struct nfs4_label *label = c->label;
David Quigley1775fd32013-05-22 12:50:42 -04003092 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04003093 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094
3095 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05003097 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
3098 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
3100 goto out_err;
3101 }
Anna Schumaker334f87d2017-01-11 16:17:17 -05003102 status = nfs4_client_recover_expired_lease(server->nfs_client);
Trond Myklebust58d97142006-01-03 09:55:24 +01003103 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003104 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00003105 if (d_really_is_positive(dentry))
3106 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01003107 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00003108 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003109 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003110 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
3111 c, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01003112 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05003113 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114
David Quigley14c43f72013-05-22 12:50:43 -04003115 if (label) {
3116 olabel = nfs4_label_alloc(server, GFP_KERNEL);
3117 if (IS_ERR(olabel)) {
3118 status = PTR_ERR(olabel);
3119 goto err_opendata_put;
3120 }
3121 }
3122
Trond Myklebuste911b812014-03-26 13:24:37 -07003123 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
3124 if (!opendata->f_attr.mdsthreshold) {
3125 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
3126 if (!opendata->f_attr.mdsthreshold)
3127 goto err_free_label;
3128 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04003129 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04003130 }
David Howells2b0143b2015-03-17 22:25:59 +00003131 if (d_really_is_positive(dentry))
3132 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04003133
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003134 status = _nfs4_open_and_get_state(opendata, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003135 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04003136 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04003137 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003138
NeilBrownefcbc042015-07-30 13:00:56 +10003139 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04003140 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Trond Myklebust609339c2018-03-28 16:18:17 -04003141 unsigned attrs = nfs4_exclusive_attrset(opendata, sattr, &label);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02003142 /*
3143 * send create attributes which was not set by open
3144 * with an extra setattr.
3145 */
Trond Myklebust609339c2018-03-28 16:18:17 -04003146 if (attrs || label) {
3147 unsigned ia_old = sattr->ia_valid;
3148
3149 sattr->ia_valid = attrs;
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02003150 nfs_fattr_init(opendata->o_res.f_attr);
3151 status = nfs4_do_setattr(state->inode, cred,
3152 opendata->o_res.f_attr, sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11003153 ctx, label, olabel);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02003154 if (status == 0) {
3155 nfs_setattr_update_inode(state->inode, sattr,
3156 opendata->o_res.f_attr);
3157 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
3158 }
Trond Myklebust609339c2018-03-28 16:18:17 -04003159 sattr->ia_valid = ia_old;
David Quigley1775fd32013-05-22 12:50:42 -04003160 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04003161 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08003162 if (opened && opendata->file_created)
Al Viro73a09dd2018-06-08 13:22:02 -04003163 *opened = 1;
Andy Adamson82be4172012-05-23 05:02:35 -04003164
Trond Myklebuste911b812014-03-26 13:24:37 -07003165 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04003166 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07003167 opendata->f_attr.mdsthreshold = NULL;
3168 }
Andy Adamson82be4172012-05-23 05:02:35 -04003169
David Quigley14c43f72013-05-22 12:50:43 -04003170 nfs4_label_free(olabel);
3171
Trond Myklebustc6d00e62007-06-17 16:02:44 -04003172 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04003175err_free_label:
3176 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04003177err_opendata_put:
3178 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01003179err_put_state_owner:
3180 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182 return status;
3183}
3184
3185
Andy Adamson82be4172012-05-23 05:02:35 -04003186static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04003187 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04003188 int flags,
3189 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003190 struct nfs4_label *label,
3191 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003193 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust0688e642019-04-07 13:59:09 -04003194 struct nfs4_exception exception = {
3195 .interruptible = true,
3196 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 struct nfs4_state *res;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003198 struct nfs4_open_createattrs c = {
3199 .label = label,
3200 .sattr = sattr,
3201 .verf = {
3202 [0] = (__u32)jiffies,
3203 [1] = (__u32)current->pid,
3204 },
3205 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 int status;
3207
3208 do {
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003209 status = _nfs4_do_open(dir, ctx, flags, &c, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04003210 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04003211 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212 if (status == 0)
3213 break;
3214 /* NOTE: BAD_SEQID means the server and client disagree about the
3215 * book-keeping w.r.t. state-changing operations
3216 * (OPEN/CLOSE/LOCK/LOCKU...)
3217 * It is actually a sign of a bug on the client or on the server.
3218 *
3219 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07003220 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 * have unhashed the old state_owner for us, and that we can
3222 * therefore safely retry using a new one. We should still warn
3223 * the user though...
3224 */
3225 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04003226 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04003227 " returned a bad sequence-id error!\n",
3228 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 exception.retry = 1;
3230 continue;
3231 }
Trond Myklebust550f5742005-10-18 14:20:21 -07003232 /*
3233 * BAD_STATEID on OPEN means that the server cancelled our
3234 * state before it received the OPEN_CONFIRM.
3235 * Recover by retrying the request as per the discussion
3236 * on Page 181 of RFC3530.
3237 */
3238 if (status == -NFS4ERR_BAD_STATEID) {
3239 exception.retry = 1;
3240 continue;
3241 }
Robert Milkowski924491f2020-01-28 08:37:47 +00003242 if (status == -NFS4ERR_EXPIRED) {
3243 nfs4_schedule_lease_recovery(server->nfs_client);
3244 exception.retry = 1;
3245 continue;
3246 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04003247 if (status == -EAGAIN) {
3248 /* We must have found a delegation */
3249 exception.retry = 1;
3250 continue;
3251 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003252 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
3253 continue;
3254 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255 status, &exception));
3256 } while (exception.retry);
3257 return res;
3258}
3259
Trond Myklebust8487c472016-06-26 08:44:35 -04003260static int _nfs4_do_setattr(struct inode *inode,
3261 struct nfs_setattrargs *arg,
3262 struct nfs_setattrres *res,
NeilBrowna52458b2018-12-03 11:30:31 +11003263 const struct cred *cred,
NeilBrown29b59f92016-10-13 15:26:47 +11003264 struct nfs_open_context *ctx)
Trond Myklebust8487c472016-06-26 08:44:35 -04003265{
3266 struct nfs_server *server = NFS_SERVER(inode);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003267 struct rpc_message msg = {
Trond Myklebust8487c472016-06-26 08:44:35 -04003268 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
3269 .rpc_argp = arg,
3270 .rpc_resp = res,
3271 .rpc_cred = cred,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003272 };
NeilBrowna52458b2018-12-03 11:30:31 +11003273 const struct cred *delegation_cred = NULL;
Trond Myklebust8487c472016-06-26 08:44:35 -04003274 unsigned long timestamp = jiffies;
Trond Myklebust8487c472016-06-26 08:44:35 -04003275 bool truncate;
3276 int status;
3277
3278 nfs_fattr_init(res->fattr);
3279
3280 /* Servers should only apply open mode checks for file size changes */
3281 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
Chuck Lever644c9f42020-09-04 17:39:12 -04003282 if (!truncate) {
3283 nfs4_inode_make_writeable(inode);
Trond Myklebust991eedb2018-04-09 11:15:30 -04003284 goto zero_stateid;
Chuck Lever644c9f42020-09-04 17:39:12 -04003285 }
Trond Myklebust8487c472016-06-26 08:44:35 -04003286
Trond Myklebust991eedb2018-04-09 11:15:30 -04003287 if (nfs4_copy_delegation_stateid(inode, FMODE_WRITE, &arg->stateid, &delegation_cred)) {
Trond Myklebust8487c472016-06-26 08:44:35 -04003288 /* Use that stateid */
Trond Myklebust09a54f02019-08-03 10:28:18 -04003289 } else if (ctx != NULL && ctx->state) {
NeilBrown17393472016-10-13 15:26:47 +11003290 struct nfs_lock_context *l_ctx;
NeilBrown29b59f92016-10-13 15:26:47 +11003291 if (!nfs4_valid_open_stateid(ctx->state))
Trond Myklebust8487c472016-06-26 08:44:35 -04003292 return -EBADF;
NeilBrown17393472016-10-13 15:26:47 +11003293 l_ctx = nfs_get_lock_context(ctx);
3294 if (IS_ERR(l_ctx))
3295 return PTR_ERR(l_ctx);
NeilBrown7a0566b2016-12-06 15:50:06 -05003296 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
3297 &arg->stateid, &delegation_cred);
3298 nfs_put_lock_context(l_ctx);
3299 if (status == -EIO)
Trond Myklebust8487c472016-06-26 08:44:35 -04003300 return -EBADF;
Olga Kornievskaiad826e5b2019-12-18 16:50:42 -05003301 else if (status == -EAGAIN)
3302 goto zero_stateid;
Trond Myklebust991eedb2018-04-09 11:15:30 -04003303 } else {
3304zero_stateid:
Trond Myklebust8487c472016-06-26 08:44:35 -04003305 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
Trond Myklebust991eedb2018-04-09 11:15:30 -04003306 }
Trond Myklebust8487c472016-06-26 08:44:35 -04003307 if (delegation_cred)
3308 msg.rpc_cred = delegation_cred;
3309
3310 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
3311
NeilBrowna52458b2018-12-03 11:30:31 +11003312 put_cred(delegation_cred);
NeilBrown29b59f92016-10-13 15:26:47 +11003313 if (status == 0 && ctx != NULL)
Trond Myklebust8487c472016-06-26 08:44:35 -04003314 renew_lease(server, timestamp);
3315 trace_nfs4_setattr(inode, &arg->stateid, status);
3316 return status;
3317}
3318
NeilBrowna52458b2018-12-03 11:30:31 +11003319static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
Trond Myklebust8487c472016-06-26 08:44:35 -04003320 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11003321 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
Trond Myklebust8487c472016-06-26 08:44:35 -04003322 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05003324 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebust30846df2018-04-07 13:44:28 -04003325 __u32 bitmask[NFS4_BITMASK_SZ];
NeilBrown29b59f92016-10-13 15:26:47 +11003326 struct nfs4_state *state = ctx ? ctx->state : NULL;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003327 struct nfs_setattrargs arg = {
3328 .fh = NFS_FH(inode),
3329 .iap = sattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330 .server = server,
Trond Myklebust30846df2018-04-07 13:44:28 -04003331 .bitmask = bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04003332 .label = ilabel,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003333 };
3334 struct nfs_setattrres res = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003336 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337 .server = server,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003338 };
Trond Myklebust8487c472016-06-26 08:44:35 -04003339 struct nfs4_exception exception = {
3340 .state = state,
3341 .inode = inode,
3342 .stateid = &arg.stateid,
3343 };
Trond Myklebusta71029b2021-04-10 00:23:03 -04003344 unsigned long adjust_flags = NFS_INO_INVALID_CHANGE;
Trond Myklebust8487c472016-06-26 08:44:35 -04003345 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346
Trond Myklebusta71029b2021-04-10 00:23:03 -04003347 if (sattr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID))
3348 adjust_flags |= NFS_INO_INVALID_OTHER;
3349
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350 do {
Trond Myklebusta71029b2021-04-10 00:23:03 -04003351 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, olabel),
3352 inode, adjust_flags);
Trond Myklebust30846df2018-04-07 13:44:28 -04003353
NeilBrown29b59f92016-10-13 15:26:47 +11003354 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
Trond Myklebust451146b2012-04-18 16:29:11 -04003355 switch (err) {
3356 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04003357 if (!(sattr->ia_valid & ATTR_SIZE)) {
3358 pr_warn_once("NFSv4: server %s is incorrectly "
3359 "applying open mode checks to "
3360 "a SETATTR that is not "
3361 "changing file size.\n",
3362 server->nfs_client->cl_hostname);
3363 }
Trond Myklebust451146b2012-04-18 16:29:11 -04003364 if (state && !(state->state & FMODE_WRITE)) {
3365 err = -EBADF;
3366 if (sattr->ia_valid & ATTR_OPEN)
3367 err = -EACCES;
3368 goto out;
3369 }
3370 }
3371 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04003373out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374 return err;
3375}
3376
Peng Tao500d7012015-09-22 11:35:22 +08003377static bool
3378nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3379{
3380 if (inode == NULL || !nfs_have_layout(inode))
3381 return false;
3382
3383 return pnfs_wait_on_layoutreturn(inode, task);
3384}
3385
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003386/*
3387 * Update the seqid of an open stateid
3388 */
3389static void nfs4_sync_open_stateid(nfs4_stateid *dst,
3390 struct nfs4_state *state)
3391{
3392 __be32 seqid_open;
3393 u32 dst_seqid;
3394 int seq;
3395
3396 for (;;) {
3397 if (!nfs4_valid_open_stateid(state))
3398 break;
3399 seq = read_seqbegin(&state->seqlock);
3400 if (!nfs4_state_match_open_stateid_other(state, dst)) {
3401 nfs4_stateid_copy(dst, &state->open_stateid);
3402 if (read_seqretry(&state->seqlock, seq))
3403 continue;
3404 break;
3405 }
3406 seqid_open = state->open_stateid.seqid;
3407 if (read_seqretry(&state->seqlock, seq))
3408 continue;
3409
3410 dst_seqid = be32_to_cpu(dst->seqid);
3411 if ((s32)(dst_seqid - be32_to_cpu(seqid_open)) < 0)
3412 dst->seqid = seqid_open;
3413 break;
3414 }
3415}
3416
3417/*
3418 * Update the seqid of an open stateid after receiving
3419 * NFS4ERR_OLD_STATEID
3420 */
3421static bool nfs4_refresh_open_old_stateid(nfs4_stateid *dst,
3422 struct nfs4_state *state)
3423{
3424 __be32 seqid_open;
3425 u32 dst_seqid;
3426 bool ret;
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04003427 int seq, status = -EAGAIN;
3428 DEFINE_WAIT(wait);
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003429
3430 for (;;) {
3431 ret = false;
3432 if (!nfs4_valid_open_stateid(state))
3433 break;
3434 seq = read_seqbegin(&state->seqlock);
3435 if (!nfs4_state_match_open_stateid_other(state, dst)) {
3436 if (read_seqretry(&state->seqlock, seq))
3437 continue;
3438 break;
3439 }
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04003440
3441 write_seqlock(&state->seqlock);
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003442 seqid_open = state->open_stateid.seqid;
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003443
3444 dst_seqid = be32_to_cpu(dst->seqid);
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04003445
3446 /* Did another OPEN bump the state's seqid? try again: */
3447 if ((s32)(be32_to_cpu(seqid_open) - dst_seqid) > 0) {
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003448 dst->seqid = seqid_open;
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04003449 write_sequnlock(&state->seqlock);
3450 ret = true;
3451 break;
3452 }
3453
3454 /* server says we're behind but we haven't seen the update yet */
3455 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
3456 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
3457 write_sequnlock(&state->seqlock);
3458 trace_nfs4_close_stateid_update_wait(state->inode, dst, 0);
3459
3460 if (signal_pending(current))
3461 status = -EINTR;
3462 else
3463 if (schedule_timeout(5*HZ) != 0)
3464 status = 0;
3465
3466 finish_wait(&state->waitq, &wait);
3467
3468 if (!status)
3469 continue;
3470 if (status == -EINTR)
3471 break;
3472
3473 /* we slept the whole 5 seconds, we must have lost a seqid */
3474 dst->seqid = cpu_to_be32(dst_seqid + 1);
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003475 ret = true;
3476 break;
3477 }
3478
3479 return ret;
3480}
3481
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482struct nfs4_closedata {
3483 struct inode *inode;
3484 struct nfs4_state *state;
3485 struct nfs_closeargs arg;
3486 struct nfs_closeres res;
Trond Myklebustcf805162016-11-15 14:56:07 -05003487 struct {
3488 struct nfs4_layoutreturn_args arg;
3489 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003490 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebustcf805162016-11-15 14:56:07 -05003491 u32 roc_barrier;
3492 bool roc;
3493 } lr;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003494 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003495 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496};
3497
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003498static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07003499{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003500 struct nfs4_closedata *calldata = data;
3501 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04003502 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07003503
Trond Myklebustcf805162016-11-15 14:56:07 -05003504 if (calldata->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003505 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3506 calldata->res.lr_ret);
Trond Myklebust95121352005-10-18 14:20:12 -07003507 nfs4_put_open_state(calldata->state);
3508 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07003509 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05003510 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003511 kfree(calldata);
3512}
3513
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003514static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003516 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003519 nfs4_stateid *res_stateid = NULL;
Trond Myklebustb8b8d222017-11-07 10:51:37 -05003520 struct nfs4_exception exception = {
3521 .state = state,
3522 .inode = calldata->inode,
3523 .stateid = &calldata->arg.stateid,
3524 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525
Chuck Levera3ca5652012-03-01 17:00:40 -05003526 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04003527 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3528 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04003529 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Trond Myklebustcf805162016-11-15 14:56:07 -05003530
3531 /* Handle Layoutreturn errors */
Trond Myklebust078000d2021-01-04 13:18:03 -05003532 if (pnfs_roc_done(task, &calldata->arg.lr_args, &calldata->res.lr_res,
3533 &calldata->res.lr_ret) == -EAGAIN)
Trond Myklebust287a9c52019-09-20 07:23:41 -04003534 goto out_restart;
Trond Myklebustcf805162016-11-15 14:56:07 -05003535
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003536 /* hmm. we are done with the inode, and in the process of freeing
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537 * the state_owner. we keep this around to process errors
3538 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539 switch (task->tk_status) {
3540 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04003541 res_stateid = &calldata->res.stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003542 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003543 break;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003544 case -NFS4ERR_ACCESS:
3545 if (calldata->arg.bitmask != NULL) {
3546 calldata->arg.bitmask = NULL;
3547 calldata->res.fattr = NULL;
Trond Myklebust91b30d22017-11-06 15:28:09 -05003548 goto out_restart;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003549
3550 }
3551 break;
Trond Myklebust12f275c2017-11-06 15:28:05 -05003552 case -NFS4ERR_OLD_STATEID:
3553 /* Did we race with OPEN? */
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003554 if (nfs4_refresh_open_old_stateid(&calldata->arg.stateid,
Trond Myklebust91b30d22017-11-06 15:28:09 -05003555 state))
3556 goto out_restart;
Trond Myklebust12f275c2017-11-06 15:28:05 -05003557 goto out_release;
Trond Myklebust69794ad2013-11-20 12:57:19 -05003558 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559 case -NFS4ERR_STALE_STATEID:
Trond Myklebust26d36302016-09-22 13:39:05 -04003560 case -NFS4ERR_EXPIRED:
3561 nfs4_free_revoked_stateid(server,
3562 &calldata->arg.stateid,
3563 task->tk_msg.rpc_cred);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05003564 fallthrough;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003565 case -NFS4ERR_BAD_STATEID:
Trond Myklebuste217e822019-09-20 07:23:46 -04003566 if (calldata->arg.fmode == 0)
3567 break;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05003568 fallthrough;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569 default:
Trond Myklebustb8b8d222017-11-07 10:51:37 -05003570 task->tk_status = nfs4_async_handle_exception(task,
3571 server, task->tk_status, &exception);
3572 if (exception.retry)
Trond Myklebust91b30d22017-11-06 15:28:09 -05003573 goto out_restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07003575 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3576 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003577out_release:
Trond Myklebust91b30d22017-11-06 15:28:09 -05003578 task->tk_status = 0;
Trond Myklebust72211db2009-12-15 14:47:36 -05003579 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustd8d84982016-12-19 12:14:44 -05003580 nfs_refresh_inode(calldata->inode, &calldata->fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05003581 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Trond Myklebust91b30d22017-11-06 15:28:09 -05003582 return;
Trond Myklebust91b30d22017-11-06 15:28:09 -05003583out_restart:
3584 task->tk_status = 0;
3585 rpc_restart_call_prepare(task);
3586 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587}
3588
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003589static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003591 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07003592 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003593 struct inode *inode = calldata->inode;
Trond Myklebust332d1a02021-03-25 18:15:36 -04003594 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustc8bf7072018-06-15 16:31:02 -04003595 struct pnfs_layout_hdr *lo;
Trond Myklebustaee7af32014-08-25 22:33:12 -04003596 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05003597 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07003598
Chuck Levera3ca5652012-03-01 17:00:40 -05003599 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003600 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003601 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04003602
Trond Myklebust88069f72009-12-08 08:33:16 -05003603 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05003604 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04003605 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3606 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3607 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust003707c2007-07-05 18:07:55 -04003608 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003609 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01003610 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003611 if (state->n_rdonly == 0)
3612 call_close |= is_rdonly;
3613 else if (is_rdonly)
3614 calldata->arg.fmode |= FMODE_READ;
3615 if (state->n_wronly == 0)
3616 call_close |= is_wronly;
3617 else if (is_wronly)
3618 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebuste547f262016-06-25 19:19:28 -04003619 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3620 call_close |= is_rdwr;
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003621 } else if (is_rdwr)
3622 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3623
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003624 nfs4_sync_open_stateid(&calldata->arg.stateid, state);
3625 if (!nfs4_valid_open_stateid(state))
Trond Myklebust5d422302013-03-14 16:57:48 -04003626 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05003627 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05003628
3629 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003630 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003631 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07003632 }
Trond Myklebust88069f72009-12-08 08:33:16 -05003633
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003634 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
Peng Tao500d7012015-09-22 11:35:22 +08003635 nfs_release_seqid(calldata->arg.seqid);
3636 goto out_wait;
3637 }
3638
Trond Myklebustc8bf7072018-06-15 16:31:02 -04003639 lo = calldata->arg.lr_args ? calldata->arg.lr_args->layout : NULL;
3640 if (lo && !pnfs_layout_is_valid(lo)) {
3641 calldata->arg.lr_args = NULL;
3642 calldata->res.lr_res = NULL;
3643 }
3644
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003645 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05003646 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003647
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003648 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003649 /* Close-to-open cache consistency revalidation */
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04003650 if (!nfs4_have_delegation(inode, FMODE_READ)) {
Trond Myklebust332d1a02021-03-25 18:15:36 -04003651 nfs4_bitmask_set(calldata->arg.bitmask_store,
3652 server->cache_consistency_bitmask,
3653 inode, server, NULL);
3654 calldata->arg.bitmask = calldata->arg.bitmask_store;
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04003655 } else
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003656 calldata->arg.bitmask = NULL;
3657 }
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003658
Trond Myklebust6ae37332015-01-30 14:21:14 -05003659 calldata->arg.share_access =
3660 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3661 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05003662
Trond Myklebustd8d84982016-12-19 12:14:44 -05003663 if (calldata->res.fattr == NULL)
3664 calldata->arg.bitmask = NULL;
3665 else if (calldata->arg.bitmask == NULL)
3666 calldata->res.fattr = NULL;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003667 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05003668 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
Trond Myklebust9d12b212012-01-17 22:04:25 -05003669 &calldata->arg.seq_args,
3670 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04003671 task) != 0)
3672 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05003673 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003674 return;
3675out_no_action:
3676 task->tk_action = NULL;
3677out_wait:
3678 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679}
3680
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003681static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003682 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003683 .rpc_call_done = nfs4_close_done,
3684 .rpc_release = nfs4_free_closedata,
3685};
3686
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687/*
3688 * It is possible for data to be read/written from a mem-mapped file
3689 * after the sys_close call (which hits the vfs layer as a flush).
3690 * This means that we can't safely call nfsv4 close on a file until
3691 * the inode is cleared. This in turn means that we are not good
3692 * NFSv4 citizens - we do not indicate to the server to update the file's
3693 * share state even when we are done with one of the three share
3694 * stateid's in the inode.
3695 *
3696 * NOTE: Caller must be holding the sp->so_owner semaphore!
3697 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04003698int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003700 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05003701 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04003703 struct nfs4_state_owner *sp = state->owner;
3704 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003705 struct rpc_message msg = {
3706 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3707 .rpc_cred = state->owner->so_cred,
3708 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003709 struct rpc_task_setup task_setup_data = {
3710 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003711 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003712 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003713 .workqueue = nfsiod_workqueue,
Trond Myklebust61296502020-02-07 19:38:12 -05003714 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003715 };
Trond Myklebust95121352005-10-18 14:20:12 -07003716 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04003718 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3719 &task_setup_data.rpc_client, &msg);
3720
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003721 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07003723 goto out;
Anna Schumakerfba83f32018-05-04 16:22:50 -04003724 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1, 0);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003725 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003727 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebustc82bac62017-11-06 15:28:06 -05003728 if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state))
3729 goto out_free_calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05003731 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3732 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05003733 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07003734 goto out_free_calldata;
Trond Myklebustd8d84982016-12-19 12:14:44 -05003735 nfs_fattr_init(&calldata->fattr);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003736 calldata->arg.fmode = 0;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003737 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003738 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003739 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003740 calldata->res.server = server;
Trond Myklebustcf805162016-11-15 14:56:07 -05003741 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003742 calldata->lr.roc = pnfs_roc(state->inode,
3743 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3744 if (calldata->lr.roc) {
3745 calldata->arg.lr_args = &calldata->lr.arg;
3746 calldata->res.lr_res = &calldata->lr.res;
3747 }
Al Viro643168c2011-06-22 18:20:23 -04003748 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003749
Trond Myklebust1174dd12010-12-21 10:52:24 -05003750 msg.rpc_argp = &calldata->arg;
3751 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003752 task_setup_data.callback_data = calldata;
3753 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003754 if (IS_ERR(task))
3755 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003756 status = 0;
3757 if (wait)
3758 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003759 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003760 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003761out_free_calldata:
3762 kfree(calldata);
3763out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003764 nfs4_put_open_state(state);
3765 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003766 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767}
3768
Trond Myklebust2b484292010-09-17 10:56:51 -04003769static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003770nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3771 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003774 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3775
3776 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777
Trond Myklebust565277f2007-10-15 18:17:53 -04003778 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003779 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003780
3781 nfs4_label_release_security(label);
3782
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003783 if (IS_ERR(state))
3784 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003785 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786}
3787
Trond Myklebust1185a552009-12-03 15:54:02 -05003788static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003789{
3790 if (ctx->state == NULL)
3791 return;
3792 if (is_sync)
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003793 nfs4_close_sync(ctx->state, _nfs4_ctx_to_openmode(ctx));
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003794 else
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003795 nfs4_close_state(ctx->state, _nfs4_ctx_to_openmode(ctx));
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003796}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797
Trond Myklebustb944dba2013-11-04 15:20:20 -05003798#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3799#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
Frank van der Lindenb78ef842020-06-23 22:38:55 +00003800#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_XATTR_SUPPORT - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003801
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3803{
Kinglong Mee8c612822015-08-26 21:12:58 +08003804 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003805 struct nfs4_server_caps_arg args = {
3806 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003807 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003808 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809 struct nfs4_server_caps_res res = {};
3810 struct rpc_message msg = {
3811 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003812 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813 .rpc_resp = &res,
3814 };
3815 int status;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003816 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817
Kinglong Mee8c612822015-08-26 21:12:58 +08003818 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3819 FATTR4_WORD0_FH_EXPIRE_TYPE |
3820 FATTR4_WORD0_LINK_SUPPORT |
3821 FATTR4_WORD0_SYMLINK_SUPPORT |
3822 FATTR4_WORD0_ACLSUPPORT;
3823 if (minorversion)
3824 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3825
Bryan Schumaker7c513052011-03-24 17:12:24 +00003826 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003828 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003829 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003830 case 0:
3831 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3832 res.attr_bitmask[2] = 0;
3833 break;
3834 case 1:
3835 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3836 break;
3837 case 2:
3838 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3839 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003840 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04003841 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3842 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3843 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3844 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003845 NFS_CAP_CTIME|NFS_CAP_MTIME|
3846 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003847 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3848 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849 server->caps |= NFS_CAP_ACLS;
3850 if (res.has_links != 0)
3851 server->caps |= NFS_CAP_HARDLINKS;
3852 if (res.has_symlinks != 0)
3853 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003854 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3855 server->caps |= NFS_CAP_FILEID;
3856 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3857 server->caps |= NFS_CAP_MODE;
3858 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3859 server->caps |= NFS_CAP_NLINK;
3860 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3861 server->caps |= NFS_CAP_OWNER;
3862 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3863 server->caps |= NFS_CAP_OWNER_GROUP;
3864 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3865 server->caps |= NFS_CAP_ATIME;
3866 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3867 server->caps |= NFS_CAP_CTIME;
3868 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3869 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003870#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3871 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3872 server->caps |= NFS_CAP_SECURITY_LABEL;
3873#endif
3874 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3875 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003876 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003877
Trond Myklebusta65318b2009-03-11 14:10:28 -04003878 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3879 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3880 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003881 server->cache_consistency_bitmask[2] = 0;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003882
3883 /* Avoid a regression due to buggy server */
3884 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3885 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
Kinglong Mee8c612822015-08-26 21:12:58 +08003886 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3887 sizeof(server->exclcreat_bitmask));
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003888
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003890 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003892
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893 return status;
3894}
3895
Trond Myklebust55a97592006-06-09 09:34:19 -04003896int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003897{
Trond Myklebust0688e642019-04-07 13:59:09 -04003898 struct nfs4_exception exception = {
3899 .interruptible = true,
3900 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901 int err;
3902 do {
3903 err = nfs4_handle_exception(server,
3904 _nfs4_server_capabilities(server, fhandle),
3905 &exception);
3906 } while (exception.retry);
3907 return err;
3908}
3909
3910static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3911 struct nfs_fsinfo *info)
3912{
David Quigleyaa9c2662013-05-22 12:50:44 -04003913 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003915 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916 };
3917 struct nfs4_lookup_res res = {
3918 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003919 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003920 .fh = fhandle,
3921 };
3922 struct rpc_message msg = {
3923 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3924 .rpc_argp = &args,
3925 .rpc_resp = &res,
3926 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003927
David Quigleyaa9c2662013-05-22 12:50:44 -04003928 bitmask[0] = nfs4_fattr_bitmap[0];
3929 bitmask[1] = nfs4_fattr_bitmap[1];
3930 /*
3931 * Process the label in the upcoming getfattr
3932 */
3933 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3934
Trond Myklebust0e574af2005-10-27 22:12:38 -04003935 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003936 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937}
3938
3939static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3940 struct nfs_fsinfo *info)
3941{
Trond Myklebust0688e642019-04-07 13:59:09 -04003942 struct nfs4_exception exception = {
3943 .interruptible = true,
3944 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945 int err;
3946 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003947 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003948 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003949 switch (err) {
3950 case 0:
3951 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003952 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003953 default:
3954 err = nfs4_handle_exception(server, err, &exception);
3955 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003957out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958 return err;
3959}
3960
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003961static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3962 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3963{
Trond Myklebustc2190662013-08-26 19:23:04 -04003964 struct rpc_auth_create_args auth_args = {
3965 .pseudoflavor = flavor,
3966 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003967 struct rpc_auth *auth;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003968
Trond Myklebustc2190662013-08-26 19:23:04 -04003969 auth = rpcauth_create(&auth_args, server->client);
Anna Schumaker9df13362017-01-11 16:30:08 -05003970 if (IS_ERR(auth))
3971 return -EACCES;
3972 return nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003973}
3974
Chuck Lever9a744ba2013-03-16 15:56:02 -04003975/*
3976 * Retry pseudoroot lookup with various security flavors. We do this when:
3977 *
3978 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3979 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3980 *
3981 * Returns zero on success, or a negative NFS4ERR value, or a
3982 * negative errno value.
3983 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003984static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003985 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003987 /* Per 3530bis 15.33.5 */
3988 static const rpc_authflavor_t flav_array[] = {
3989 RPC_AUTH_GSS_KRB5P,
3990 RPC_AUTH_GSS_KRB5I,
3991 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003992 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003993 RPC_AUTH_NULL,
3994 };
3995 int status = -EPERM;
3996 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003998 if (server->auth_info.flavor_len > 0) {
3999 /* try each flavor specified by user */
4000 for (i = 0; i < server->auth_info.flavor_len; i++) {
4001 status = nfs4_lookup_root_sec(server, fhandle, info,
4002 server->auth_info.flavors[i]);
4003 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
4004 continue;
4005 break;
4006 }
4007 } else {
4008 /* no flavors specified by user, try default list */
4009 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
4010 status = nfs4_lookup_root_sec(server, fhandle, info,
4011 flav_array[i]);
4012 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
4013 continue;
4014 break;
4015 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00004016 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04004017
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04004018 /*
Colin Ian Kingd3787af2018-10-26 19:10:31 +01004019 * -EACCES could mean that the user doesn't have correct permissions
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04004020 * to access the mount. It could also mean that we tried to mount
4021 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
4022 * existing mount programs don't handle -EACCES very well so it should
4023 * be mapped to -EPERM instead.
4024 */
4025 if (status == -EACCES)
4026 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04004027 return status;
4028}
4029
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04004030/**
4031 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
4032 * @server: initialized nfs_server handle
4033 * @fhandle: we fill in the pseudo-fs root file handle
4034 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04004035 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04004036 *
4037 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04004038 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04004039int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04004040 struct nfs_fsinfo *info,
4041 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04004042{
Andre Przywarac7757072015-04-23 17:17:40 +01004043 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04004044
Andre Przywarac7757072015-04-23 17:17:40 +01004045 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04004046 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01004047
4048 if (auth_probe || status == NFS4ERR_WRONGSEC)
Trond Myklebust698c9372016-07-25 13:31:14 -04004049 status = server->nfs_client->cl_mvops->find_root_sec(server,
4050 fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04004051
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052 if (status == 0)
4053 status = nfs4_server_capabilities(server, fhandle);
4054 if (status == 0)
4055 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04004056
Trond Myklebustc12e87f2006-03-13 21:20:47 -08004057 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058}
4059
Bryan Schumakerbae36242012-05-10 15:07:31 -04004060static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
4061 struct nfs_fsinfo *info)
4062{
4063 int error;
4064 struct nfs_fattr *fattr = info->fattr;
Scott Mayhew779df6a2020-03-03 17:58:37 -05004065 struct nfs4_label *label = fattr->label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04004066
4067 error = nfs4_server_capabilities(server, mntfh);
4068 if (error < 0) {
4069 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
4070 return error;
4071 }
4072
Trond Myklebusta841b542018-04-07 13:50:59 -04004073 error = nfs4_proc_getattr(server, mntfh, fattr, label, NULL);
Bryan Schumakerbae36242012-05-10 15:07:31 -04004074 if (error < 0) {
4075 dprintk("nfs4_get_root: getattr error = %d\n", -error);
Scott Mayhew779df6a2020-03-03 17:58:37 -05004076 goto out;
Bryan Schumakerbae36242012-05-10 15:07:31 -04004077 }
4078
4079 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
4080 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
4081 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
4082
Scott Mayhew779df6a2020-03-03 17:58:37 -05004083out:
Bryan Schumakerbae36242012-05-10 15:07:31 -04004084 return error;
4085}
4086
Manoj Naik6b97fd32006-06-09 09:34:29 -04004087/*
4088 * Get locations and (maybe) other attributes of a referral.
4089 * Note that we'll actually follow the referral later when
4090 * we detect fsid mismatch in inode revalidation
4091 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004092static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
4093 const struct qstr *name, struct nfs_fattr *fattr,
4094 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04004095{
4096 int status = -ENOMEM;
4097 struct page *page = NULL;
4098 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04004099
4100 page = alloc_page(GFP_KERNEL);
4101 if (page == NULL)
4102 goto out;
4103 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
4104 if (locations == NULL)
4105 goto out;
4106
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004107 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04004108 if (status != 0)
4109 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04004110
4111 /*
4112 * If the fsid didn't change, this is a migration event, not a
4113 * referral. Cause us to drop into the exception handler, which
4114 * will kick off migration recovery.
4115 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04004116 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04004117 dprintk("%s: server did not return a different fsid for"
4118 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04004119 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04004120 goto out;
4121 }
Andy Adamson533eb462011-06-13 18:25:56 -04004122 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
4123 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04004124
Andy Adamson533eb462011-06-13 18:25:56 -04004125 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04004126 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04004127 memset(fhandle, 0, sizeof(struct nfs_fh));
4128out:
4129 if (page)
4130 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04004131 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04004132 return status;
4133}
4134
David Quigley1775fd32013-05-22 12:50:42 -04004135static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebusta841b542018-04-07 13:50:59 -04004136 struct nfs_fattr *fattr, struct nfs4_label *label,
4137 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138{
Trond Myklebust771734f2018-04-07 13:54:23 -04004139 __u32 bitmask[NFS4_BITMASK_SZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140 struct nfs4_getattr_arg args = {
4141 .fh = fhandle,
Trond Myklebust771734f2018-04-07 13:54:23 -04004142 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143 };
4144 struct nfs4_getattr_res res = {
4145 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04004146 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147 .server = server,
4148 };
4149 struct rpc_message msg = {
4150 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
4151 .rpc_argp = &args,
4152 .rpc_resp = &res,
4153 };
Trond Myklebustc74dfe92020-01-06 15:39:37 -05004154 unsigned short task_flags = 0;
4155
4156 /* Is this is an attribute revalidation, subject to softreval? */
4157 if (inode && (server->flags & NFS_MOUNT_SOFTREVAL))
4158 task_flags |= RPC_TASK_TIMEOUT;
David Quigleyaa9c2662013-05-22 12:50:44 -04004159
Trond Myklebusta71029b2021-04-10 00:23:03 -04004160 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, label), inode, 0);
Trond Myklebust0e574af2005-10-27 22:12:38 -04004161 nfs_fattr_init(fattr);
Trond Myklebustc74dfe92020-01-06 15:39:37 -05004162 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
4163 return nfs4_do_call_sync(server->client, server, &msg,
4164 &args.seq_args, &res.seq_res, task_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165}
4166
Olga Kornievskaiaec4b0922019-10-08 16:33:53 -04004167int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebusta841b542018-04-07 13:50:59 -04004168 struct nfs_fattr *fattr, struct nfs4_label *label,
4169 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170{
Trond Myklebust0688e642019-04-07 13:59:09 -04004171 struct nfs4_exception exception = {
4172 .interruptible = true,
4173 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174 int err;
4175 do {
Trond Myklebusta841b542018-04-07 13:50:59 -04004176 err = _nfs4_proc_getattr(server, fhandle, fattr, label, inode);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004177 trace_nfs4_getattr(server, fhandle, fattr, err);
4178 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179 &exception);
4180 } while (exception.retry);
4181 return err;
4182}
4183
4184/*
4185 * The file is not closed if it is opened due to the a request to change
4186 * the size of the file. The open call will not be needed once the
4187 * VFS layer lookup-intents are implemented.
4188 *
4189 * Close is called when the inode is destroyed.
4190 * If we haven't opened the file for O_WRONLY, we
4191 * need to in the size_change case to obtain a stateid.
4192 *
4193 * Got race?
4194 * Because OPEN is always done by name in nfsv4, it is
4195 * possible that we opened a different file by the same
4196 * name. We can recognize this race condition, but we
4197 * can't do anything about it besides returning an error.
4198 *
4199 * This will be fixed with VFS changes (lookup-intent).
4200 */
4201static int
4202nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
4203 struct iattr *sattr)
4204{
David Howells2b0143b2015-03-17 22:25:59 +00004205 struct inode *inode = d_inode(dentry);
NeilBrowna52458b2018-12-03 11:30:31 +11004206 const struct cred *cred = NULL;
NeilBrown29b59f92016-10-13 15:26:47 +11004207 struct nfs_open_context *ctx = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04004208 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209 int status;
4210
Peng Tao88ac8152014-09-12 11:04:10 +08004211 if (pnfs_ld_layoutret_on_setattr(inode) &&
4212 sattr->ia_valid & ATTR_SIZE &&
4213 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04004214 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04004215
Trond Myklebust0e574af2005-10-27 22:12:38 -04004216 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217
Andy Adamson26699402012-05-30 16:12:24 -04004218 /* Deal with open(O_TRUNC) */
4219 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03004220 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04004221
4222 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03004223 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04004224 return 0;
4225
Trond Myklebustd5308382005-11-04 15:33:38 -05004226 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04004227 if (sattr->ia_valid & ATTR_FILE) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00004228
Trond Myklebust659bfcd2008-06-10 19:39:41 -04004229 ctx = nfs_file_open_context(sattr->ia_file);
NeilBrown29b59f92016-10-13 15:26:47 +11004230 if (ctx)
Neil Brown504e5182008-10-16 14:15:16 +11004231 cred = ctx->cred;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04004232 }
4233
David Quigley14c43f72013-05-22 12:50:43 -04004234 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4235 if (IS_ERR(label))
4236 return PTR_ERR(label);
4237
Trond Myklebust199366f2018-03-20 16:43:18 -04004238 /* Return any delegations if we're going to change ACLs */
4239 if ((sattr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
Trond Myklebustc01d3642018-03-20 16:43:20 -04004240 nfs4_inode_make_writeable(inode);
Trond Myklebust199366f2018-03-20 16:43:18 -04004241
NeilBrown29b59f92016-10-13 15:26:47 +11004242 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04004243 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05004244 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04004245 nfs_setsecurity(inode, fattr, label);
4246 }
David Quigley14c43f72013-05-22 12:50:43 -04004247 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248 return status;
4249}
4250
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004251static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004252 struct dentry *dentry, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04004253 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04004254{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004255 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04004256 int status;
4257 struct nfs4_lookup_arg args = {
4258 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004259 .dir_fh = NFS_FH(dir),
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004260 .name = &dentry->d_name,
David Howells2b3de442006-08-22 20:06:09 -04004261 };
4262 struct nfs4_lookup_res res = {
4263 .server = server,
4264 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04004265 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04004266 .fh = fhandle,
4267 };
4268 struct rpc_message msg = {
4269 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
4270 .rpc_argp = &args,
4271 .rpc_resp = &res,
4272 };
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004273 unsigned short task_flags = 0;
4274
4275 /* Is this is an attribute revalidation, subject to softreval? */
4276 if (nfs_lookup_is_soft_revalidate(dentry))
4277 task_flags |= RPC_TASK_TIMEOUT;
David Howells2b3de442006-08-22 20:06:09 -04004278
David Quigleyaa9c2662013-05-22 12:50:44 -04004279 args.bitmask = nfs4_bitmask(server, label);
4280
David Howells2b3de442006-08-22 20:06:09 -04004281 nfs_fattr_init(fattr);
4282
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004283 dprintk("NFS call lookup %pd2\n", dentry);
4284 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
4285 status = nfs4_do_call_sync(clnt, server, &msg,
4286 &args.seq_args, &res.seq_res, task_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287 dprintk("NFS reply lookup: %d\n", status);
4288 return status;
4289}
4290
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004291static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00004292{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00004293 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004294 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00004295 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4296 fattr->nlink = 2;
4297}
4298
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004299static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004300 struct dentry *dentry, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04004301 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004302{
Trond Myklebust0688e642019-04-07 13:59:09 -04004303 struct nfs4_exception exception = {
4304 .interruptible = true,
4305 };
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004306 struct rpc_clnt *client = *clnt;
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004307 const struct qstr *name = &dentry->d_name;
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004308 int err;
4309 do {
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004310 err = _nfs4_proc_lookup(client, dir, dentry, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004311 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004312 switch (err) {
4313 case -NFS4ERR_BADNAME:
4314 err = -ENOENT;
4315 goto out;
4316 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004317 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02004318 if (err == -NFS4ERR_MOVED)
4319 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004320 goto out;
4321 case -NFS4ERR_WRONGSEC:
4322 err = -EPERM;
4323 if (client != *clnt)
4324 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04004325 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004326 if (IS_ERR(client))
4327 return PTR_ERR(client);
4328
4329 exception.retry = 1;
4330 break;
4331 default:
4332 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4333 }
4334 } while (exception.retry);
4335
4336out:
4337 if (err == 0)
4338 *clnt = client;
4339 else if (client != *clnt)
4340 rpc_shutdown_client(client);
4341
4342 return err;
4343}
4344
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004345static int nfs4_proc_lookup(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004346 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4347 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004349 int status;
4350 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004351
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004352 status = nfs4_proc_lookup_common(&client, dir, dentry, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004353 if (client != NFS_CLIENT(dir)) {
4354 rpc_shutdown_client(client);
4355 nfs_fixup_secinfo_attributes(fattr);
4356 }
4357 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004358}
4359
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004360struct rpc_clnt *
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004361nfs4_proc_lookup_mountpoint(struct inode *dir, struct dentry *dentry,
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004362 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4363{
Trond Myklebustb72888c2013-08-07 20:38:07 -04004364 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004365 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004366
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004367 status = nfs4_proc_lookup_common(&client, dir, dentry, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04004368 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004369 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04004370 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004371}
4372
Jeff Layton5b5faaf2017-06-29 06:34:52 -07004373static int _nfs4_proc_lookupp(struct inode *inode,
4374 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4375 struct nfs4_label *label)
4376{
4377 struct rpc_clnt *clnt = NFS_CLIENT(inode);
4378 struct nfs_server *server = NFS_SERVER(inode);
4379 int status;
4380 struct nfs4_lookupp_arg args = {
4381 .bitmask = server->attr_bitmask,
4382 .fh = NFS_FH(inode),
4383 };
4384 struct nfs4_lookupp_res res = {
4385 .server = server,
4386 .fattr = fattr,
4387 .label = label,
4388 .fh = fhandle,
4389 };
4390 struct rpc_message msg = {
4391 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
4392 .rpc_argp = &args,
4393 .rpc_resp = &res,
4394 };
Trond Myklebust76998eb2020-10-20 14:30:35 -04004395 unsigned short task_flags = 0;
4396
4397 if (NFS_SERVER(inode)->flags & NFS_MOUNT_SOFTREVAL)
4398 task_flags |= RPC_TASK_TIMEOUT;
Jeff Layton5b5faaf2017-06-29 06:34:52 -07004399
4400 args.bitmask = nfs4_bitmask(server, label);
4401
4402 nfs_fattr_init(fattr);
4403
4404 dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino);
4405 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
Trond Myklebust76998eb2020-10-20 14:30:35 -04004406 &res.seq_res, task_flags);
Jeff Layton5b5faaf2017-06-29 06:34:52 -07004407 dprintk("NFS reply lookupp: %d\n", status);
4408 return status;
4409}
4410
4411static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
4412 struct nfs_fattr *fattr, struct nfs4_label *label)
4413{
Trond Myklebust0688e642019-04-07 13:59:09 -04004414 struct nfs4_exception exception = {
4415 .interruptible = true,
4416 };
Jeff Layton5b5faaf2017-06-29 06:34:52 -07004417 int err;
4418 do {
4419 err = _nfs4_proc_lookupp(inode, fhandle, fattr, label);
4420 trace_nfs4_lookupp(inode, err);
4421 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4422 &exception);
4423 } while (exception.retry);
4424 return err;
4425}
4426
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4428{
Trond Myklebust76b32992007-08-10 17:45:11 -04004429 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430 struct nfs4_accessargs args = {
4431 .fh = NFS_FH(inode),
Anna Schumaker1750d922017-07-26 12:00:21 -04004432 .access = entry->mask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433 };
Trond Myklebust76b32992007-08-10 17:45:11 -04004434 struct nfs4_accessres res = {
4435 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04004436 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437 struct rpc_message msg = {
4438 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
4439 .rpc_argp = &args,
4440 .rpc_resp = &res,
4441 .rpc_cred = entry->cred,
4442 };
David Quigleyaa9c2662013-05-22 12:50:44 -04004443 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444
Trond Myklebust7c672652018-06-04 15:00:53 -04004445 if (!nfs4_have_delegation(inode, FMODE_READ)) {
Trond Myklebust8bcbe7d2018-03-20 17:03:11 -04004446 res.fattr = nfs_alloc_fattr();
4447 if (res.fattr == NULL)
4448 return -ENOMEM;
4449 args.bitmask = server->cache_consistency_bitmask;
4450 }
Bryan Schumaker7c513052011-03-24 17:12:24 +00004451 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004452 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04004453 nfs_access_set_mask(entry, res.access);
Trond Myklebust8bcbe7d2018-03-20 17:03:11 -04004454 if (res.fattr)
4455 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456 }
Trond Myklebustc407d412010-04-16 16:22:48 -04004457 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004458 return status;
4459}
4460
4461static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4462{
Trond Myklebust0688e642019-04-07 13:59:09 -04004463 struct nfs4_exception exception = {
4464 .interruptible = true,
4465 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466 int err;
4467 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004468 err = _nfs4_proc_access(inode, entry);
4469 trace_nfs4_access(inode, err);
4470 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471 &exception);
4472 } while (exception.retry);
4473 return err;
4474}
4475
4476/*
4477 * TODO: For the time being, we don't try to get any attributes
4478 * along with any of the zero-copy operations READ, READDIR,
4479 * READLINK, WRITE.
4480 *
4481 * In the case of the first three, we want to put the GETATTR
4482 * after the read-type operation -- this is because it is hard
4483 * to predict the length of a GETATTR response in v4, and thus
4484 * align the READ data correctly. This means that the GETATTR
4485 * may end up partially falling into the page cache, and we should
4486 * shift it into the 'tail' of the xdr_buf before processing.
4487 * To do this efficiently, we need to know the total length
4488 * of data received, which doesn't seem to be available outside
4489 * of the RPC layer.
4490 *
4491 * In the case of WRITE, we also want to put the GETATTR after
4492 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04004493 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494 *
4495 * Both of these changes to the XDR layer would in fact be quite
4496 * minor, but I decided to leave them for a subsequent patch.
4497 */
4498static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4499 unsigned int pgbase, unsigned int pglen)
4500{
4501 struct nfs4_readlink args = {
4502 .fh = NFS_FH(inode),
4503 .pgbase = pgbase,
4504 .pglen = pglen,
4505 .pages = &page,
4506 };
Benny Halevyf50c7002009-04-01 09:21:55 -04004507 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508 struct rpc_message msg = {
4509 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4510 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04004511 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512 };
4513
Bryan Schumaker7c513052011-03-24 17:12:24 +00004514 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 -07004515}
4516
4517static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4518 unsigned int pgbase, unsigned int pglen)
4519{
Trond Myklebust0688e642019-04-07 13:59:09 -04004520 struct nfs4_exception exception = {
4521 .interruptible = true,
4522 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004523 int err;
4524 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004525 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4526 trace_nfs4_readlink(inode, err);
4527 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528 &exception);
4529 } while (exception.retry);
4530 return err;
4531}
4532
Linus Torvalds1da177e2005-04-16 15:20:36 -07004533/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004534 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004536static int
4537nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004538 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004540 struct nfs_server *server = NFS_SERVER(dir);
David Quigleyaa9c2662013-05-22 12:50:44 -04004541 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004542 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004543 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544 int status = 0;
4545
NeilBrown532d4de2016-10-13 15:26:47 +11004546 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004547 if (IS_ERR(ctx))
4548 return PTR_ERR(ctx);
4549
David Quigleyaa9c2662013-05-22 12:50:44 -04004550 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4551
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004552 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4553 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08004554 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555 if (IS_ERR(state)) {
4556 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04004557 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559out:
David Quigleyaa9c2662013-05-22 12:50:44 -04004560 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004561 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562 return status;
4563}
4564
Trond Myklebust3c591172018-07-31 15:54:10 -04004565static int
4566_nfs4_proc_remove(struct inode *dir, const struct qstr *name, u32 ftype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567{
Trond Myklebust16e42952005-10-27 22:12:44 -04004568 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004569 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07004571 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004573 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04004574 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04004575 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004577 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4578 .rpc_argp = &args,
4579 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004580 };
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004581 unsigned long timestamp = jiffies;
Trond Myklebust778d2812012-04-27 13:48:19 -04004582 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04004583
Bryan Schumaker7c513052011-03-24 17:12:24 +00004584 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust3c591172018-07-31 15:54:10 -04004585 if (status == 0) {
4586 spin_lock(&dir->i_lock);
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004587 nfs4_update_changeattr_locked(dir, &res.cinfo, timestamp,
4588 NFS_INO_INVALID_DATA);
Trond Myklebust3c591172018-07-31 15:54:10 -04004589 /* Removing a directory decrements nlink in the parent */
4590 if (ftype == NF4DIR && dir->i_nlink > 2)
4591 nfs4_dec_nlink_locked(dir);
4592 spin_unlock(&dir->i_lock);
4593 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004594 return status;
4595}
4596
Trond Myklebust912678d2018-03-20 16:43:15 -04004597static int nfs4_proc_remove(struct inode *dir, struct dentry *dentry)
4598{
Trond Myklebust0688e642019-04-07 13:59:09 -04004599 struct nfs4_exception exception = {
4600 .interruptible = true,
4601 };
Trond Myklebust912678d2018-03-20 16:43:15 -04004602 struct inode *inode = d_inode(dentry);
4603 int err;
4604
Trond Myklebustc01d3642018-03-20 16:43:20 -04004605 if (inode) {
4606 if (inode->i_nlink == 1)
4607 nfs4_inode_return_delegation(inode);
4608 else
4609 nfs4_inode_make_writeable(inode);
4610 }
Trond Myklebust912678d2018-03-20 16:43:15 -04004611 do {
Trond Myklebust3c591172018-07-31 15:54:10 -04004612 err = _nfs4_proc_remove(dir, &dentry->d_name, NF4REG);
Trond Myklebust912678d2018-03-20 16:43:15 -04004613 trace_nfs4_remove(dir, &dentry->d_name, err);
4614 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4615 &exception);
4616 } while (exception.retry);
4617 return err;
4618}
4619
4620static int nfs4_proc_rmdir(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621{
Trond Myklebust0688e642019-04-07 13:59:09 -04004622 struct nfs4_exception exception = {
4623 .interruptible = true,
4624 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625 int err;
Trond Myklebust912678d2018-03-20 16:43:15 -04004626
Linus Torvalds1da177e2005-04-16 15:20:36 -07004627 do {
Trond Myklebust3c591172018-07-31 15:54:10 -04004628 err = _nfs4_proc_remove(dir, name, NF4DIR);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004629 trace_nfs4_remove(dir, name, err);
4630 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004631 &exception);
4632 } while (exception.retry);
4633 return err;
4634}
4635
Trond Myklebusted7e9ad2018-05-30 16:11:52 -04004636static void nfs4_proc_unlink_setup(struct rpc_message *msg,
4637 struct dentry *dentry,
4638 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004640 struct nfs_removeargs *args = msg->rpc_argp;
4641 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642
Trond Myklebust977fcc22018-03-20 16:43:17 -04004643 res->server = NFS_SB(dentry->d_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Anna Schumakerfba83f32018-05-04 16:22:50 -04004645 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04004646
4647 nfs_fattr_init(res->dir_attr);
Trond Myklebust977fcc22018-03-20 16:43:17 -04004648
4649 if (inode)
4650 nfs4_inode_return_delegation(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651}
4652
Bryan Schumaker34e137c2012-03-19 14:54:41 -04004653static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4654{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004655 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004656 &data->args.seq_args,
4657 &data->res.seq_res,
4658 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004659}
4660
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004661static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004663 struct nfs_unlinkdata *data = task->tk_calldata;
4664 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004665
Trond Myklebust14516c32010-07-31 14:29:06 -04004666 if (!nfs4_sequence_done(task, &res->seq_res))
4667 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004668 if (nfs4_async_handle_error(task, res->server, NULL,
4669 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004670 return 0;
Trond Myklebustc40d52f2017-01-11 12:36:11 -05004671 if (task->tk_status == 0)
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004672 nfs4_update_changeattr(dir, &res->cinfo,
4673 res->dir_attr->time_start,
4674 NFS_INO_INVALID_DATA);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004675 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676}
4677
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004678static void nfs4_proc_rename_setup(struct rpc_message *msg,
4679 struct dentry *old_dentry,
4680 struct dentry *new_dentry)
Jeff Laytond3d41522010-09-17 17:31:57 -04004681{
Jeff Laytond3d41522010-09-17 17:31:57 -04004682 struct nfs_renameargs *arg = msg->rpc_argp;
4683 struct nfs_renameres *res = msg->rpc_resp;
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004684 struct inode *old_inode = d_inode(old_dentry);
4685 struct inode *new_inode = d_inode(new_dentry);
Jeff Laytond3d41522010-09-17 17:31:57 -04004686
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004687 if (old_inode)
Trond Myklebustc01d3642018-03-20 16:43:20 -04004688 nfs4_inode_make_writeable(old_inode);
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004689 if (new_inode)
4690 nfs4_inode_return_delegation(new_inode);
Jeff Laytond3d41522010-09-17 17:31:57 -04004691 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004692 res->server = NFS_SB(old_dentry->d_sb);
Anna Schumakerfba83f32018-05-04 16:22:50 -04004693 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1, 0);
Jeff Laytond3d41522010-09-17 17:31:57 -04004694}
4695
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04004696static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4697{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004698 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004699 &data->args.seq_args,
4700 &data->res.seq_res,
4701 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04004702}
4703
4704static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4705 struct inode *new_dir)
4706{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04004707 struct nfs_renamedata *data = task->tk_calldata;
4708 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04004709
4710 if (!nfs4_sequence_done(task, &res->seq_res))
4711 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004712 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04004713 return 0;
4714
Trond Myklebustc733c492017-01-11 12:32:26 -05004715 if (task->tk_status == 0) {
Trond Myklebust5636ec42018-07-31 15:54:11 -04004716 if (new_dir != old_dir) {
4717 /* Note: If we moved a directory, nlink will change */
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004718 nfs4_update_changeattr(old_dir, &res->old_cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04004719 res->old_fattr->time_start,
Trond Myklebustfabf2b32021-03-25 13:14:42 -04004720 NFS_INO_INVALID_NLINK |
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004721 NFS_INO_INVALID_DATA);
4722 nfs4_update_changeattr(new_dir, &res->new_cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04004723 res->new_fattr->time_start,
Trond Myklebustfabf2b32021-03-25 13:14:42 -04004724 NFS_INO_INVALID_NLINK |
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004725 NFS_INO_INVALID_DATA);
Trond Myklebust5636ec42018-07-31 15:54:11 -04004726 } else
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004727 nfs4_update_changeattr(old_dir, &res->old_cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04004728 res->old_fattr->time_start,
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004729 NFS_INO_INVALID_DATA);
Trond Myklebustc733c492017-01-11 12:32:26 -05004730 }
Jeff Laytond3d41522010-09-17 17:31:57 -04004731 return 1;
4732}
4733
Al Virobeffb8f2016-07-20 16:34:42 -04004734static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004735{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004736 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebust2f28dc32018-04-08 21:06:40 -04004737 __u32 bitmask[NFS4_BITMASK_SZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004738 struct nfs4_link_arg arg = {
4739 .fh = NFS_FH(inode),
4740 .dir_fh = NFS_FH(dir),
4741 .name = name,
Trond Myklebust2f28dc32018-04-08 21:06:40 -04004742 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004743 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004744 struct nfs4_link_res res = {
4745 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04004746 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004747 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004748 struct rpc_message msg = {
4749 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4750 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004751 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004752 };
Trond Myklebust136f2622010-04-16 16:22:49 -04004753 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754
Trond Myklebust136f2622010-04-16 16:22:49 -04004755 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04004756 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04004757 goto out;
4758
David Quigley14c43f72013-05-22 12:50:43 -04004759 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4760 if (IS_ERR(res.label)) {
4761 status = PTR_ERR(res.label);
4762 goto out;
4763 }
4764
Trond Myklebustc01d3642018-03-20 16:43:20 -04004765 nfs4_inode_make_writeable(inode);
Trond Myklebusta71029b2021-04-10 00:23:03 -04004766 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, res.label), inode,
4767 NFS_INO_INVALID_CHANGE);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004768 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004769 if (!status) {
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004770 nfs4_update_changeattr(dir, &res.cinfo, res.fattr->time_start,
4771 NFS_INO_INVALID_DATA);
David Quigleyaa9c2662013-05-22 12:50:44 -04004772 status = nfs_post_op_update_inode(inode, res.fattr);
4773 if (!status)
4774 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004775 }
David Quigley14c43f72013-05-22 12:50:43 -04004776
4777
4778 nfs4_label_free(res.label);
4779
Trond Myklebust136f2622010-04-16 16:22:49 -04004780out:
Trond Myklebust136f2622010-04-16 16:22:49 -04004781 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782 return status;
4783}
4784
Al Virobeffb8f2016-07-20 16:34:42 -04004785static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786{
Trond Myklebust0688e642019-04-07 13:59:09 -04004787 struct nfs4_exception exception = {
4788 .interruptible = true,
4789 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004790 int err;
4791 do {
4792 err = nfs4_handle_exception(NFS_SERVER(inode),
4793 _nfs4_proc_link(inode, dir, name),
4794 &exception);
4795 } while (exception.retry);
4796 return err;
4797}
4798
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004799struct nfs4_createdata {
4800 struct rpc_message msg;
4801 struct nfs4_create_arg arg;
4802 struct nfs4_create_res res;
4803 struct nfs_fh fh;
4804 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004805 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004806};
4807
4808static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004809 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004810{
4811 struct nfs4_createdata *data;
4812
4813 data = kzalloc(sizeof(*data), GFP_KERNEL);
4814 if (data != NULL) {
4815 struct nfs_server *server = NFS_SERVER(dir);
4816
David Quigley14c43f72013-05-22 12:50:43 -04004817 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4818 if (IS_ERR(data->label))
4819 goto out_free;
4820
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004821 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4822 data->msg.rpc_argp = &data->arg;
4823 data->msg.rpc_resp = &data->res;
4824 data->arg.dir_fh = NFS_FH(dir);
4825 data->arg.server = server;
4826 data->arg.name = name;
4827 data->arg.attrs = sattr;
4828 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04004829 data->arg.bitmask = nfs4_bitmask(server, data->label);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004830 data->arg.umask = current_umask();
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004831 data->res.server = server;
4832 data->res.fh = &data->fh;
4833 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004834 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004835 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004836 }
4837 return data;
David Quigley14c43f72013-05-22 12:50:43 -04004838out_free:
4839 kfree(data);
4840 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004841}
4842
4843static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4844{
Bryan Schumaker7c513052011-03-24 17:12:24 +00004845 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00004846 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004847 if (status == 0) {
Trond Myklebust3c591172018-07-31 15:54:10 -04004848 spin_lock(&dir->i_lock);
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004849 nfs4_update_changeattr_locked(dir, &data->res.dir_cinfo,
4850 data->res.fattr->time_start,
4851 NFS_INO_INVALID_DATA);
Trond Myklebust3c591172018-07-31 15:54:10 -04004852 /* Creating a directory bumps nlink in the parent */
4853 if (data->arg.ftype == NF4DIR)
4854 nfs4_inc_nlink_locked(dir);
4855 spin_unlock(&dir->i_lock);
David Quigley1775fd32013-05-22 12:50:42 -04004856 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004857 }
4858 return status;
4859}
4860
4861static void nfs4_free_createdata(struct nfs4_createdata *data)
4862{
David Quigley14c43f72013-05-22 12:50:43 -04004863 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004864 kfree(data);
4865}
4866
Chuck Lever4f390c12006-08-22 20:06:22 -04004867static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004868 struct page *page, unsigned int len, struct iattr *sattr,
4869 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004870{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004871 struct nfs4_createdata *data;
4872 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873
Chuck Lever94a6d752006-08-22 20:06:23 -04004874 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004875 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04004876
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004877 status = -ENOMEM;
4878 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4879 if (data == NULL)
4880 goto out;
4881
4882 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4883 data->arg.u.symlink.pages = &page;
4884 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004885 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004886
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004887 status = nfs4_do_create(dir, dentry, data);
4888
4889 nfs4_free_createdata(data);
4890out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004891 return status;
4892}
4893
Chuck Lever4f390c12006-08-22 20:06:22 -04004894static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004895 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004896{
Trond Myklebust0688e642019-04-07 13:59:09 -04004897 struct nfs4_exception exception = {
4898 .interruptible = true,
4899 };
David Quigleyaa9c2662013-05-22 12:50:44 -04004900 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004901 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004902
4903 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4904
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004906 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4907 trace_nfs4_symlink(dir, &dentry->d_name, err);
4908 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004909 &exception);
4910 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004911
4912 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004913 return err;
4914}
4915
4916static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004917 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004918{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004919 struct nfs4_createdata *data;
4920 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004922 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4923 if (data == NULL)
4924 goto out;
4925
David Quigley1775fd32013-05-22 12:50:42 -04004926 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004927 status = nfs4_do_create(dir, dentry, data);
4928
4929 nfs4_free_createdata(data);
4930out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004931 return status;
4932}
4933
4934static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4935 struct iattr *sattr)
4936{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004937 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust0688e642019-04-07 13:59:09 -04004938 struct nfs4_exception exception = {
4939 .interruptible = true,
4940 };
David Quigleyaa9c2662013-05-22 12:50:44 -04004941 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004943
David Quigleyaa9c2662013-05-22 12:50:44 -04004944 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4945
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004946 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4947 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004948 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004949 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4950 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4951 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004952 &exception);
4953 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004954 nfs4_label_release_security(label);
4955
Linus Torvalds1da177e2005-04-16 15:20:36 -07004956 return err;
4957}
4958
Trond Myklebust82e22a52020-11-02 17:34:23 -05004959static int _nfs4_proc_readdir(struct nfs_readdir_arg *nr_arg,
4960 struct nfs_readdir_res *nr_res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961{
Trond Myklebust82e22a52020-11-02 17:34:23 -05004962 struct inode *dir = d_inode(nr_arg->dentry);
Olga Kornievskaia05ad9172020-11-06 16:03:38 -05004963 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004964 struct nfs4_readdir_arg args = {
4965 .fh = NFS_FH(dir),
Trond Myklebust82e22a52020-11-02 17:34:23 -05004966 .pages = nr_arg->pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967 .pgbase = 0,
Trond Myklebust82e22a52020-11-02 17:34:23 -05004968 .count = nr_arg->page_len,
4969 .plus = nr_arg->plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004970 };
4971 struct nfs4_readdir_res res;
4972 struct rpc_message msg = {
4973 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4974 .rpc_argp = &args,
4975 .rpc_resp = &res,
Trond Myklebust82e22a52020-11-02 17:34:23 -05004976 .rpc_cred = nr_arg->cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977 };
4978 int status;
4979
Trond Myklebust82e22a52020-11-02 17:34:23 -05004980 dprintk("%s: dentry = %pd2, cookie = %llu\n", __func__,
4981 nr_arg->dentry, (unsigned long long)nr_arg->cookie);
Olga Kornievskaia05ad9172020-11-06 16:03:38 -05004982 if (!(server->caps & NFS_CAP_SECURITY_LABEL))
4983 args.bitmask = server->attr_bitmask_nl;
4984 else
4985 args.bitmask = server->attr_bitmask;
4986
Trond Myklebust82e22a52020-11-02 17:34:23 -05004987 nfs4_setup_readdir(nr_arg->cookie, nr_arg->verf, nr_arg->dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004988 res.pgbase = args.pgbase;
Olga Kornievskaia05ad9172020-11-06 16:03:38 -05004989 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
4990 &res.seq_res, 0);
Trond Myklebustac396122010-11-15 20:26:22 -05004991 if (status >= 0) {
Trond Myklebust82e22a52020-11-02 17:34:23 -05004992 memcpy(nr_res->verf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05004993 status += args.pgbase;
4994 }
Trond Myklebustc4812992007-09-28 17:11:45 -04004995
4996 nfs_invalidate_atime(dir);
4997
Harvey Harrison3110ff82008-05-02 13:42:44 -07004998 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004999 return status;
5000}
5001
Trond Myklebust82e22a52020-11-02 17:34:23 -05005002static int nfs4_proc_readdir(struct nfs_readdir_arg *arg,
5003 struct nfs_readdir_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005004{
Trond Myklebust0688e642019-04-07 13:59:09 -04005005 struct nfs4_exception exception = {
5006 .interruptible = true,
5007 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005008 int err;
5009 do {
Trond Myklebust82e22a52020-11-02 17:34:23 -05005010 err = _nfs4_proc_readdir(arg, res);
5011 trace_nfs4_readdir(d_inode(arg->dentry), err);
5012 err = nfs4_handle_exception(NFS_SERVER(d_inode(arg->dentry)),
5013 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005014 } while (exception.retry);
5015 return err;
5016}
5017
5018static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04005019 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005020{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04005021 struct nfs4_createdata *data;
5022 int mode = sattr->ia_mode;
5023 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005024
Trond Myklebust57dc9a52008-06-20 15:35:32 -04005025 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
5026 if (data == NULL)
5027 goto out;
5028
Linus Torvalds1da177e2005-04-16 15:20:36 -07005029 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04005030 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005031 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04005032 data->arg.ftype = NF4BLK;
5033 data->arg.u.device.specdata1 = MAJOR(rdev);
5034 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005035 }
5036 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04005037 data->arg.ftype = NF4CHR;
5038 data->arg.u.device.specdata1 = MAJOR(rdev);
5039 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04005040 } else if (!S_ISSOCK(mode)) {
5041 status = -EINVAL;
5042 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005043 }
David Quigley1775fd32013-05-22 12:50:42 -04005044
David Quigleyaa9c2662013-05-22 12:50:44 -04005045 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04005046 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04005047out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04005048 nfs4_free_createdata(data);
5049out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005050 return status;
5051}
5052
5053static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
5054 struct iattr *sattr, dev_t rdev)
5055{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05005056 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust0688e642019-04-07 13:59:09 -04005057 struct nfs4_exception exception = {
5058 .interruptible = true,
5059 };
David Quigleyaa9c2662013-05-22 12:50:44 -04005060 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005061 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00005062
David Quigleyaa9c2662013-05-22 12:50:44 -04005063 label = nfs4_label_init_security(dir, dentry, sattr, &l);
5064
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05005065 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
5066 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005067 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04005068 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
5069 trace_nfs4_mknod(dir, &dentry->d_name, err);
5070 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005071 &exception);
5072 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04005073
5074 nfs4_label_release_security(label);
5075
Linus Torvalds1da177e2005-04-16 15:20:36 -07005076 return err;
5077}
5078
5079static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
5080 struct nfs_fsstat *fsstat)
5081{
5082 struct nfs4_statfs_arg args = {
5083 .fh = fhandle,
5084 .bitmask = server->attr_bitmask,
5085 };
Benny Halevy24ad1482009-04-01 09:21:56 -04005086 struct nfs4_statfs_res res = {
5087 .fsstat = fsstat,
5088 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005089 struct rpc_message msg = {
5090 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
5091 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04005092 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005093 };
5094
Trond Myklebust0e574af2005-10-27 22:12:38 -04005095 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005096 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005097}
5098
5099static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
5100{
Trond Myklebust0688e642019-04-07 13:59:09 -04005101 struct nfs4_exception exception = {
5102 .interruptible = true,
5103 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005104 int err;
5105 do {
5106 err = nfs4_handle_exception(server,
5107 _nfs4_proc_statfs(server, fhandle, fsstat),
5108 &exception);
5109 } while (exception.retry);
5110 return err;
5111}
5112
5113static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
5114 struct nfs_fsinfo *fsinfo)
5115{
5116 struct nfs4_fsinfo_arg args = {
5117 .fh = fhandle,
5118 .bitmask = server->attr_bitmask,
5119 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04005120 struct nfs4_fsinfo_res res = {
5121 .fsinfo = fsinfo,
5122 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005123 struct rpc_message msg = {
5124 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
5125 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04005126 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005127 };
5128
Bryan Schumaker7c513052011-03-24 17:12:24 +00005129 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005130}
5131
5132static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5133{
Trond Myklebust0688e642019-04-07 13:59:09 -04005134 struct nfs4_exception exception = {
5135 .interruptible = true,
5136 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005137 int err;
5138
5139 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04005140 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04005141 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04005142 if (err == 0) {
Robert Milkowski7dc29932020-01-30 09:43:25 +00005143 nfs4_set_lease_period(server->nfs_client, fsinfo->lease_time * HZ);
Chuck Lever83ca7f52013-03-16 15:55:53 -04005144 break;
5145 }
5146 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005147 } while (exception.retry);
5148 return err;
5149}
5150
5151static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5152{
Bryan Schumakere38eb652012-06-20 15:53:40 -04005153 int error;
5154
Trond Myklebust0e574af2005-10-27 22:12:38 -04005155 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04005156 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08005157 if (error == 0) {
5158 /* block layout checks this! */
5159 server->pnfs_blksize = fsinfo->blksize;
Jeff Laytonca440c32016-09-15 14:40:49 -04005160 set_pnfs_layoutdriver(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08005161 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04005162
5163 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005164}
5165
5166static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5167 struct nfs_pathconf *pathconf)
5168{
5169 struct nfs4_pathconf_arg args = {
5170 .fh = fhandle,
5171 .bitmask = server->attr_bitmask,
5172 };
Benny Halevyd45b2982009-04-01 09:21:58 -04005173 struct nfs4_pathconf_res res = {
5174 .pathconf = pathconf,
5175 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005176 struct rpc_message msg = {
5177 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
5178 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04005179 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005180 };
5181
5182 /* None of the pathconf attributes are mandatory to implement */
5183 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
5184 memset(pathconf, 0, sizeof(*pathconf));
5185 return 0;
5186 }
5187
Trond Myklebust0e574af2005-10-27 22:12:38 -04005188 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005189 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005190}
5191
5192static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5193 struct nfs_pathconf *pathconf)
5194{
Trond Myklebust0688e642019-04-07 13:59:09 -04005195 struct nfs4_exception exception = {
5196 .interruptible = true,
5197 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005198 int err;
5199
5200 do {
5201 err = nfs4_handle_exception(server,
5202 _nfs4_proc_pathconf(server, fhandle, pathconf),
5203 &exception);
5204 } while (exception.retry);
5205 return err;
5206}
5207
Trond Myklebust5521abf2013-03-16 20:54:34 -04005208int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04005209 const struct nfs_open_context *ctx,
5210 const struct nfs_lock_context *l_ctx,
5211 fmode_t fmode)
5212{
NeilBrown17393472016-10-13 15:26:47 +11005213 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04005214}
5215EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
5216
Trond Myklebust5521abf2013-03-16 20:54:34 -04005217static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
5218 const struct nfs_open_context *ctx,
5219 const struct nfs_lock_context *l_ctx,
5220 fmode_t fmode)
5221{
Ben Dooksd49dd112019-10-16 17:28:21 +01005222 nfs4_stateid _current_stateid;
Trond Myklebust5521abf2013-03-16 20:54:34 -04005223
Trond Myklebuste1253be2014-03-05 08:44:23 -05005224 /* If the current stateid represents a lost lock, then exit */
Ben Dooksd49dd112019-10-16 17:28:21 +01005225 if (nfs4_set_rw_stateid(&_current_stateid, ctx, l_ctx, fmode) == -EIO)
Trond Myklebuste1253be2014-03-05 08:44:23 -05005226 return true;
Ben Dooksd49dd112019-10-16 17:28:21 +01005227 return nfs4_stateid_match(stateid, &_current_stateid);
Trond Myklebust5521abf2013-03-16 20:54:34 -04005228}
5229
5230static bool nfs4_error_stateid_expired(int err)
5231{
5232 switch (err) {
5233 case -NFS4ERR_DELEG_REVOKED:
5234 case -NFS4ERR_ADMIN_REVOKED:
5235 case -NFS4ERR_BAD_STATEID:
5236 case -NFS4ERR_STALE_STATEID:
5237 case -NFS4ERR_OLD_STATEID:
5238 case -NFS4ERR_OPENMODE:
5239 case -NFS4ERR_EXPIRED:
5240 return true;
5241 }
5242 return false;
5243}
5244
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005245static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005246{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005247 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005248
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005249 trace_nfs4_read(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04005250 if (task->tk_status < 0) {
5251 struct nfs4_exception exception = {
5252 .inode = hdr->inode,
5253 .state = hdr->args.context->state,
5254 .stateid = &hdr->args.stateid,
5255 };
5256 task->tk_status = nfs4_async_handle_exception(task,
5257 server, task->tk_status, &exception);
5258 if (exception.retry) {
5259 rpc_restart_call_prepare(task);
5260 return -EAGAIN;
5261 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005262 }
Trond Myklebust8850df92007-09-28 17:20:07 -04005263
Linus Torvalds1da177e2005-04-16 15:20:36 -07005264 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005265 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05005266 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005267}
5268
Trond Myklebust5521abf2013-03-16 20:54:34 -04005269static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04005270 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04005271{
5272
5273 if (!nfs4_error_stateid_expired(task->tk_status) ||
5274 nfs4_stateid_is_current(&args->stateid,
5275 args->context,
5276 args->lock_context,
5277 FMODE_READ))
5278 return false;
5279 rpc_restart_call_prepare(task);
5280 return true;
5281}
5282
Anna Schumakerc5675522014-05-28 13:41:22 -04005283static bool nfs4_read_plus_not_supported(struct rpc_task *task,
5284 struct nfs_pgio_header *hdr)
5285{
5286 struct nfs_server *server = NFS_SERVER(hdr->inode);
5287 struct rpc_message *msg = &task->tk_msg;
5288
5289 if (msg->rpc_proc == &nfs4_procedures[NFSPROC4_CLNT_READ_PLUS] &&
5290 server->caps & NFS_CAP_READ_PLUS && task->tk_status == -ENOTSUPP) {
5291 server->caps &= ~NFS_CAP_READ_PLUS;
5292 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
5293 rpc_restart_call_prepare(task);
5294 return true;
5295 }
5296 return false;
5297}
5298
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005299static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc72011-03-01 01:34:20 +00005300{
Andy Adamsoncbdabc72011-03-01 01:34:20 +00005301 dprintk("--> %s\n", __func__);
5302
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005303 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc72011-03-01 01:34:20 +00005304 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005305 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04005306 return -EAGAIN;
Anna Schumakerc5675522014-05-28 13:41:22 -04005307 if (nfs4_read_plus_not_supported(task, hdr))
5308 return -EAGAIN;
Trond Myklebustbfc505d2016-09-15 18:26:05 -04005309 if (task->tk_status > 0)
5310 nfs_invalidate_atime(hdr->inode);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005311 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5312 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00005313}
5314
Anna Schumaker21e31402020-12-03 15:18:39 -05005315#if defined CONFIG_NFS_V4_2 && defined CONFIG_NFS_V4_2_READ_PLUS
Trond Myklebust5c3485b2020-12-10 09:34:34 -05005316static void nfs42_read_plus_support(struct nfs_pgio_header *hdr,
5317 struct rpc_message *msg)
Anna Schumakerc5675522014-05-28 13:41:22 -04005318{
Trond Myklebust5c3485b2020-12-10 09:34:34 -05005319 /* Note: We don't use READ_PLUS with pNFS yet */
5320 if (nfs_server_capable(hdr->inode, NFS_CAP_READ_PLUS) && !hdr->ds_clp)
Anna Schumakerc5675522014-05-28 13:41:22 -04005321 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ_PLUS];
Anna Schumakerc5675522014-05-28 13:41:22 -04005322}
5323#else
Trond Myklebust5c3485b2020-12-10 09:34:34 -05005324static void nfs42_read_plus_support(struct nfs_pgio_header *hdr,
5325 struct rpc_message *msg)
Anna Schumakerc5675522014-05-28 13:41:22 -04005326{
Anna Schumakerc5675522014-05-28 13:41:22 -04005327}
5328#endif /* CONFIG_NFS_V4_2 */
5329
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005330static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
5331 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005332{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005333 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04005334 if (!hdr->pgio_done_cb)
5335 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebust5c3485b2020-12-10 09:34:34 -05005336 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
5337 nfs42_read_plus_support(hdr, msg);
Anna Schumakerfba83f32018-05-04 16:22:50 -04005338 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005339}
5340
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005341static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
5342 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04005343{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005344 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005345 &hdr->args.seq_args,
5346 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04005347 task))
NeilBrownef1820f2013-09-04 17:04:49 +10005348 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005349 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
5350 hdr->args.lock_context,
Benjamin Coddingtonfbe77c32017-04-19 10:11:35 -04005351 hdr->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10005352 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005353 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10005354 return -EIO;
5355 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005356}
5357
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005358static int nfs4_write_done_cb(struct rpc_task *task,
5359 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005360{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005361 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10005362
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005363 trace_nfs4_write(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04005364 if (task->tk_status < 0) {
5365 struct nfs4_exception exception = {
5366 .inode = hdr->inode,
5367 .state = hdr->args.context->state,
5368 .stateid = &hdr->args.stateid,
5369 };
5370 task->tk_status = nfs4_async_handle_exception(task,
5371 NFS_SERVER(inode), task->tk_status,
5372 &exception);
5373 if (exception.retry) {
5374 rpc_restart_call_prepare(task);
5375 return -EAGAIN;
5376 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005377 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04005378 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005379 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05005380 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04005381 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05005382 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005383}
5384
Trond Myklebust5521abf2013-03-16 20:54:34 -04005385static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04005386 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04005387{
5388
5389 if (!nfs4_error_stateid_expired(task->tk_status) ||
5390 nfs4_stateid_is_current(&args->stateid,
5391 args->context,
5392 args->lock_context,
5393 FMODE_WRITE))
5394 return false;
5395 rpc_restart_call_prepare(task);
5396 return true;
5397}
5398
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005399static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00005400{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005401 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00005402 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005403 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04005404 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005405 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5406 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00005407}
5408
Trond Myklebust5a37f852012-04-28 14:55:16 -04005409static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005410bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00005411{
Trond Myklebust5a37f852012-04-28 14:55:16 -04005412 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005413 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04005414 return false;
5415 /* Otherwise, request attributes if and only if we don't hold
5416 * a delegation
5417 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04005418 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00005419}
Fred Isamana69aef12011-03-03 15:13:47 +00005420
Trond Myklebust332d1a02021-03-25 18:15:36 -04005421static void nfs4_bitmask_set(__u32 bitmask[NFS4_BITMASK_SZ], const __u32 *src,
5422 struct inode *inode, struct nfs_server *server,
5423 struct nfs4_label *label)
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005424{
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005425 unsigned long cache_validity = READ_ONCE(NFS_I(inode)->cache_validity);
Trond Myklebust332d1a02021-03-25 18:15:36 -04005426 unsigned int i;
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005427
Trond Myklebust332d1a02021-03-25 18:15:36 -04005428 memcpy(bitmask, src, sizeof(*bitmask) * NFS4_BITMASK_SZ);
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005429
Trond Myklebust13c0b082021-03-25 21:07:21 -04005430 if (cache_validity & NFS_INO_INVALID_CHANGE)
Trond Myklebust332d1a02021-03-25 18:15:36 -04005431 bitmask[0] |= FATTR4_WORD0_CHANGE;
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005432 if (cache_validity & NFS_INO_INVALID_ATIME)
5433 bitmask[1] |= FATTR4_WORD1_TIME_ACCESS;
Trond Myklebust45901a22021-02-08 08:49:32 -05005434 if (cache_validity & NFS_INO_INVALID_OTHER)
5435 bitmask[1] |= FATTR4_WORD1_MODE | FATTR4_WORD1_OWNER |
Trond Myklebustfabf2b32021-03-25 13:14:42 -04005436 FATTR4_WORD1_OWNER_GROUP;
5437 if (cache_validity & NFS_INO_INVALID_NLINK)
5438 bitmask[1] |= FATTR4_WORD1_NUMLINKS;
Trond Myklebust45901a22021-02-08 08:49:32 -05005439 if (label && label->len && cache_validity & NFS_INO_INVALID_LABEL)
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005440 bitmask[2] |= FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust45901a22021-02-08 08:49:32 -05005441 if (cache_validity & NFS_INO_INVALID_CTIME)
5442 bitmask[1] |= FATTR4_WORD1_TIME_METADATA;
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005443 if (cache_validity & NFS_INO_INVALID_MTIME)
5444 bitmask[1] |= FATTR4_WORD1_TIME_MODIFY;
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005445 if (cache_validity & NFS_INO_INVALID_BLOCKS)
5446 bitmask[1] |= FATTR4_WORD1_SPACE_USED;
Trond Myklebust332d1a02021-03-25 18:15:36 -04005447
5448 if (nfs4_have_delegation(inode, FMODE_READ) &&
5449 !(cache_validity & NFS_INO_REVAL_FORCED))
5450 bitmask[0] &= ~FATTR4_WORD0_SIZE;
Trond Myklebust13c0b082021-03-25 21:07:21 -04005451 else if (cache_validity & NFS_INO_INVALID_SIZE)
Trond Myklebust332d1a02021-03-25 18:15:36 -04005452 bitmask[0] |= FATTR4_WORD0_SIZE;
5453
5454 for (i = 0; i < NFS4_BITMASK_SZ; i++)
5455 bitmask[i] &= server->attr_bitmask[i];
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005456}
5457
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005458static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
Anna Schumakerfb91fb02018-05-04 16:22:48 -04005459 struct rpc_message *msg,
5460 struct rpc_clnt **clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005461{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005462 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005463
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005464 if (!nfs4_write_need_cache_consistency_data(hdr)) {
5465 hdr->args.bitmask = NULL;
5466 hdr->res.fattr = NULL;
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005467 } else {
Trond Myklebust332d1a02021-03-25 18:15:36 -04005468 nfs4_bitmask_set(hdr->args.bitmask_store,
5469 server->cache_consistency_bitmask,
5470 hdr->inode, server, NULL);
5471 hdr->args.bitmask = hdr->args.bitmask_store;
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005472 }
Trond Myklebust5a37f852012-04-28 14:55:16 -04005473
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005474 if (!hdr->pgio_done_cb)
5475 hdr->pgio_done_cb = nfs4_write_done_cb;
5476 hdr->res.server = server;
5477 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005479 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Olga Kornievskaiacd1b6592020-02-12 17:32:12 -05005480 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
Anna Schumakerfb91fb02018-05-04 16:22:48 -04005481 nfs4_state_protect_write(server->nfs_client, clnt, msg, hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005482}
5483
Fred Isaman0b7c0152012-04-20 14:47:39 -04005484static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
5485{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005486 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005487 &data->args.seq_args,
5488 &data->res.seq_res,
5489 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005490}
5491
Fred Isaman0b7c0152012-04-20 14:47:39 -04005492static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005493{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005494 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04005495
Trond Myklebustcc668ab2013-08-14 15:31:28 -04005496 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10005497 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
5498 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005499 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05005500 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005501 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05005502 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005503}
5504
Fred Isaman0b7c0152012-04-20 14:47:39 -04005505static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00005506{
5507 if (!nfs4_sequence_done(task, &data->res.seq_res))
5508 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04005509 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00005510}
5511
Anna Schumakere9ae1ee2018-05-04 16:22:49 -04005512static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg,
5513 struct rpc_clnt **clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005514{
Trond Myklebust788e7a82006-03-20 13:44:27 -05005515 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00005516
Fred Isaman0b7c0152012-04-20 14:47:39 -04005517 if (data->commit_done_cb == NULL)
5518 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04005519 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005520 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Anna Schumakerfba83f32018-05-04 16:22:50 -04005521 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Anna Schumakere9ae1ee2018-05-04 16:22:49 -04005522 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_COMMIT, clnt, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005523}
5524
Olga Kornievskaia6b8d84e2018-07-09 15:13:36 -04005525static int _nfs4_proc_commit(struct file *dst, struct nfs_commitargs *args,
5526 struct nfs_commitres *res)
5527{
5528 struct inode *dst_inode = file_inode(dst);
5529 struct nfs_server *server = NFS_SERVER(dst_inode);
5530 struct rpc_message msg = {
5531 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
5532 .rpc_argp = args,
5533 .rpc_resp = res,
5534 };
5535
5536 args->fh = NFS_FH(dst_inode);
5537 return nfs4_call_sync(server->client, server, &msg,
5538 &args->seq_args, &res->seq_res, 1);
5539}
5540
5541int nfs4_proc_commit(struct file *dst, __u64 offset, __u32 count, struct nfs_commitres *res)
5542{
5543 struct nfs_commitargs args = {
5544 .offset = offset,
5545 .count = count,
5546 };
5547 struct nfs_server *dst_server = NFS_SERVER(file_inode(dst));
5548 struct nfs4_exception exception = { };
5549 int status;
5550
5551 do {
5552 status = _nfs4_proc_commit(dst, &args, res);
5553 status = nfs4_handle_exception(dst_server, status, &exception);
5554 } while (exception.retry);
5555
5556 return status;
5557}
5558
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005559struct nfs4_renewdata {
5560 struct nfs_client *client;
5561 unsigned long timestamp;
5562};
5563
Linus Torvalds1da177e2005-04-16 15:20:36 -07005564/*
5565 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
5566 * standalone procedure for queueing an asynchronous RENEW.
5567 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005568static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005569{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005570 struct nfs4_renewdata *data = calldata;
5571 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005572
Elena Reshetova212bf412017-10-20 12:53:38 +03005573 if (refcount_read(&clp->cl_count) > 1)
Alexandros Batsakis0851de062010-02-05 03:45:06 -08005574 nfs4_schedule_state_renewal(clp);
5575 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005576 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005577}
5578
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005579static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005580{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005581 struct nfs4_renewdata *data = calldata;
5582 struct nfs_client *clp = data->client;
5583 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005584
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005585 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04005586 switch (task->tk_status) {
5587 case 0:
5588 break;
5589 case -NFS4ERR_LEASE_MOVED:
5590 nfs4_schedule_lease_moved_recovery(clp);
5591 break;
5592 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04005593 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04005594 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
5595 return;
5596 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005597 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04005598 return;
5599 }
5600 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005601 }
Trond Myklebust452e9352010-07-31 14:29:06 -04005602 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005603}
5604
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005605static const struct rpc_call_ops nfs4_renew_ops = {
5606 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005607 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005608};
5609
NeilBrowna52458b2018-12-03 11:30:31 +11005610static int nfs4_proc_async_renew(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005611{
5612 struct rpc_message msg = {
5613 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5614 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01005615 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005616 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005617 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005618
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005619 if (renew_flags == 0)
5620 return 0;
Elena Reshetova212bf412017-10-20 12:53:38 +03005621 if (!refcount_inc_not_zero(&clp->cl_count))
Alexandros Batsakis0851de062010-02-05 03:45:06 -08005622 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04005623 data = kmalloc(sizeof(*data), GFP_NOFS);
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04005624 if (data == NULL) {
5625 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005626 return -ENOMEM;
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04005627 }
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005628 data->client = clp;
5629 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005630 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005631 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005632}
5633
NeilBrowna52458b2018-12-03 11:30:31 +11005634static int nfs4_proc_renew(struct nfs_client *clp, const struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005635{
5636 struct rpc_message msg = {
5637 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5638 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01005639 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005640 };
5641 unsigned long now = jiffies;
5642 int status;
5643
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005644 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005645 if (status < 0)
5646 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04005647 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005648 return 0;
5649}
5650
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005651static inline int nfs4_server_supports_acls(struct nfs_server *server)
5652{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06005653 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005654}
5655
Trond Myklebust21f498c2012-08-24 10:59:25 -04005656/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
5657 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005658 * the stack.
5659 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04005660#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005661
Frank van der Lindenccde1e92020-06-23 22:39:01 +00005662int nfs4_buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005663 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05005664{
5665 struct page *newpage, **spages;
5666 int rc = 0;
5667 size_t len;
5668 spages = pages;
5669
5670 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04005671 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05005672 newpage = alloc_page(GFP_KERNEL);
5673
5674 if (newpage == NULL)
5675 goto unwind;
5676 memcpy(page_address(newpage), buf, len);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005677 buf += len;
5678 buflen -= len;
Neil Hormane9e3d722011-03-04 19:26:03 -05005679 *pages++ = newpage;
5680 rc++;
5681 } while (buflen != 0);
5682
5683 return rc;
5684
5685unwind:
5686 for(; rc > 0; rc--)
5687 __free_page(spages[rc-1]);
5688 return -ENOMEM;
5689}
5690
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005691struct nfs4_cached_acl {
5692 int cached;
5693 size_t len;
Gustavo A. R. Silva5601cda2020-03-09 13:24:42 -05005694 char data[];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005695};
5696
5697static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005698{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005699 struct nfs_inode *nfsi = NFS_I(inode);
5700
5701 spin_lock(&inode->i_lock);
5702 kfree(nfsi->nfs4_acl);
5703 nfsi->nfs4_acl = acl;
5704 spin_unlock(&inode->i_lock);
5705}
5706
5707static void nfs4_zap_acl_attr(struct inode *inode)
5708{
5709 nfs4_set_cached_acl(inode, NULL);
5710}
5711
5712static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
5713{
5714 struct nfs_inode *nfsi = NFS_I(inode);
5715 struct nfs4_cached_acl *acl;
5716 int ret = -ENOENT;
5717
5718 spin_lock(&inode->i_lock);
5719 acl = nfsi->nfs4_acl;
5720 if (acl == NULL)
5721 goto out;
5722 if (buf == NULL) /* user is just asking for length */
5723 goto out_len;
5724 if (acl->cached == 0)
5725 goto out;
5726 ret = -ERANGE; /* see getxattr(2) man page */
5727 if (acl->len > buflen)
5728 goto out;
5729 memcpy(buf, acl->data, acl->len);
5730out_len:
5731 ret = acl->len;
5732out:
5733 spin_unlock(&inode->i_lock);
5734 return ret;
5735}
5736
Sachin Prabhu5794d212012-04-17 14:36:40 +01005737static 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 +00005738{
5739 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005740 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005741
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005742 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005743 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005744 if (acl == NULL)
5745 goto out;
5746 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01005747 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005748 } else {
5749 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5750 if (acl == NULL)
5751 goto out;
5752 acl->cached = 0;
5753 }
5754 acl->len = acl_len;
5755out:
5756 nfs4_set_cached_acl(inode, acl);
5757}
5758
Andy Adamsonbf118a32011-12-07 11:55:27 -05005759/*
5760 * The getxattr API returns the required buffer length when called with a
5761 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5762 * the required buf. On a NULL buf, we send a page of data to the server
5763 * guessing that the ACL request can be serviced by a page. If so, we cache
5764 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5765 * the cache. If not so, we throw away the page, and cache the required
5766 * length. The next getxattr call will then produce another round trip to
5767 * the server, this time with the input buf of the required size.
5768 */
Trond Myklebust16b42892006-08-24 12:27:15 -04005769static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005770{
Olga Kornievskaia62a15732020-01-02 17:09:54 -05005771 struct page **pages;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005772 struct nfs_getaclargs args = {
5773 .fh = NFS_FH(inode),
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005774 .acl_len = buflen,
5775 };
Benny Halevy663c79b2009-04-01 09:21:59 -04005776 struct nfs_getaclres res = {
5777 .acl_len = buflen,
5778 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005779 struct rpc_message msg = {
5780 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5781 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04005782 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005783 };
Olga Kornievskaia62a15732020-01-02 17:09:54 -05005784 unsigned int npages;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005785 int ret = -ENOMEM, i;
Olga Kornievskaia62a15732020-01-02 17:09:54 -05005786 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005787
Olga Kornievskaia62a15732020-01-02 17:09:54 -05005788 if (buflen == 0)
5789 buflen = server->rsize;
5790
5791 npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
5792 pages = kmalloc_array(npages, sizeof(struct page *), GFP_NOFS);
5793 if (!pages)
5794 return -ENOMEM;
5795
5796 args.acl_pages = pages;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005797
Andy Adamsonbf118a32011-12-07 11:55:27 -05005798 for (i = 0; i < npages; i++) {
5799 pages[i] = alloc_page(GFP_KERNEL);
5800 if (!pages[i])
5801 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005802 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01005803
5804 /* for decoding across pages */
5805 res.acl_scratch = alloc_page(GFP_KERNEL);
5806 if (!res.acl_scratch)
5807 goto out_free;
5808
Andy Adamsonbf118a32011-12-07 11:55:27 -05005809 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005810
Peng Taode040be2012-01-10 22:42:47 +08005811 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05005812 __func__, buf, buflen, npages, args.acl_len);
5813 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5814 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005815 if (ret)
5816 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05005817
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005818 /* Handle the case where the passed-in buffer is too short */
5819 if (res.acl_flags & NFS4_ACL_TRUNC) {
5820 /* Did the user only issue a request for the acl length? */
5821 if (buf == NULL)
5822 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005823 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005824 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005825 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005826 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005827 if (buf) {
5828 if (res.acl_len > buflen) {
5829 ret = -ERANGE;
5830 goto out_free;
5831 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005832 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005833 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005834out_ok:
5835 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005836out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05005837 for (i = 0; i < npages; i++)
5838 if (pages[i])
5839 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05005840 if (res.acl_scratch)
5841 __free_page(res.acl_scratch);
Olga Kornievskaia62a15732020-01-02 17:09:54 -05005842 kfree(pages);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005843 return ret;
5844}
5845
Trond Myklebust16b42892006-08-24 12:27:15 -04005846static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5847{
Trond Myklebust0688e642019-04-07 13:59:09 -04005848 struct nfs4_exception exception = {
5849 .interruptible = true,
5850 };
Trond Myklebust16b42892006-08-24 12:27:15 -04005851 ssize_t ret;
5852 do {
5853 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04005854 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b42892006-08-24 12:27:15 -04005855 if (ret >= 0)
5856 break;
5857 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5858 } while (exception.retry);
5859 return ret;
5860}
5861
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005862static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5863{
5864 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005865 int ret;
5866
5867 if (!nfs4_server_supports_acls(server))
5868 return -EOPNOTSUPP;
Trond Myklebust1f3208b2021-03-25 11:04:34 -04005869 ret = nfs_revalidate_inode(inode, NFS_INO_INVALID_CHANGE);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005870 if (ret < 0)
5871 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005872 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5873 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005874 ret = nfs4_read_cached_acl(inode, buf, buflen);
5875 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05005876 /* -ENOENT is returned if there is no ACL or if there is an ACL
5877 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005878 return ret;
5879 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005880}
5881
Trond Myklebust16b42892006-08-24 12:27:15 -04005882static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005883{
5884 struct nfs_server *server = NFS_SERVER(inode);
5885 struct page *pages[NFS4ACL_MAXPAGES];
5886 struct nfs_setaclargs arg = {
5887 .fh = NFS_FH(inode),
5888 .acl_pages = pages,
5889 .acl_len = buflen,
5890 };
Benny Halevy73c403a2009-04-01 09:22:01 -04005891 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005892 struct rpc_message msg = {
5893 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5894 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04005895 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005896 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005897 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05005898 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005899
J. Bruce Fields4f8be1f2021-01-28 17:36:38 -05005900 /* You can't remove system.nfs4_acl: */
5901 if (buflen == 0)
5902 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005903 if (!nfs4_server_supports_acls(server))
5904 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005905 if (npages > ARRAY_SIZE(pages))
5906 return -ERANGE;
Frank van der Lindenccde1e92020-06-23 22:39:01 +00005907 i = nfs4_buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05005908 if (i < 0)
5909 return i;
Trond Myklebustc01d3642018-03-20 16:43:20 -04005910 nfs4_inode_make_writeable(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005911 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05005912
5913 /*
5914 * Free each page after tx, so the only ref left is
5915 * held by the network stack
5916 */
5917 for (; i > 0; i--)
5918 put_page(pages[i-1]);
5919
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005920 /*
5921 * Acl update can result in inode attribute update.
5922 * so mark the attribute cache invalid.
5923 */
5924 spin_lock(&inode->i_lock);
Trond Myklebustb6f80a22021-03-08 14:42:55 -05005925 nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE |
5926 NFS_INO_INVALID_CTIME |
5927 NFS_INO_REVAL_FORCED);
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005928 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04005929 nfs_access_zap_cache(inode);
5930 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005931 return ret;
5932}
5933
Trond Myklebust16b42892006-08-24 12:27:15 -04005934static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5935{
5936 struct nfs4_exception exception = { };
5937 int err;
5938 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005939 err = __nfs4_proc_set_acl(inode, buf, buflen);
5940 trace_nfs4_set_acl(inode, err);
5941 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b42892006-08-24 12:27:15 -04005942 &exception);
5943 } while (exception.retry);
5944 return err;
5945}
5946
David Quigleyaa9c2662013-05-22 12:50:44 -04005947#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5948static int _nfs4_get_security_label(struct inode *inode, void *buf,
5949 size_t buflen)
5950{
5951 struct nfs_server *server = NFS_SERVER(inode);
5952 struct nfs_fattr fattr;
5953 struct nfs4_label label = {0, 0, buflen, buf};
5954
5955 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005956 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005957 .fh = NFS_FH(inode),
5958 .bitmask = bitmask,
5959 };
5960 struct nfs4_getattr_res res = {
5961 .fattr = &fattr,
5962 .label = &label,
5963 .server = server,
5964 };
5965 struct rpc_message msg = {
5966 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005967 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005968 .rpc_resp = &res,
5969 };
5970 int ret;
5971
5972 nfs_fattr_init(&fattr);
5973
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005974 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04005975 if (ret)
5976 return ret;
5977 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5978 return -ENOENT;
Ondrej Mosnacek53cb2452021-01-15 18:43:56 +01005979 return label.len;
David Quigleyaa9c2662013-05-22 12:50:44 -04005980}
5981
5982static int nfs4_get_security_label(struct inode *inode, void *buf,
5983 size_t buflen)
5984{
Trond Myklebust0688e642019-04-07 13:59:09 -04005985 struct nfs4_exception exception = {
5986 .interruptible = true,
5987 };
David Quigleyaa9c2662013-05-22 12:50:44 -04005988 int err;
5989
5990 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5991 return -EOPNOTSUPP;
5992
5993 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005994 err = _nfs4_get_security_label(inode, buf, buflen);
5995 trace_nfs4_get_security_label(inode, err);
5996 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005997 &exception);
5998 } while (exception.retry);
5999 return err;
6000}
6001
6002static int _nfs4_do_set_security_label(struct inode *inode,
6003 struct nfs4_label *ilabel,
6004 struct nfs_fattr *fattr,
6005 struct nfs4_label *olabel)
6006{
6007
6008 struct iattr sattr = {0};
6009 struct nfs_server *server = NFS_SERVER(inode);
6010 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04006011 struct nfs_setattrargs arg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05006012 .fh = NFS_FH(inode),
6013 .iap = &sattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04006014 .server = server,
6015 .bitmask = bitmask,
6016 .label = ilabel,
6017 };
6018 struct nfs_setattrres res = {
6019 .fattr = fattr,
6020 .label = olabel,
6021 .server = server,
6022 };
6023 struct rpc_message msg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05006024 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
6025 .rpc_argp = &arg,
6026 .rpc_resp = &res,
David Quigleyaa9c2662013-05-22 12:50:44 -04006027 };
6028 int status;
6029
Jeff Layton12207f62013-11-01 10:49:32 -04006030 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04006031
Jeff Layton12207f62013-11-01 10:49:32 -04006032 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04006033 if (status)
6034 dprintk("%s failed: %d\n", __func__, status);
6035
6036 return status;
6037}
6038
6039static int nfs4_do_set_security_label(struct inode *inode,
6040 struct nfs4_label *ilabel,
6041 struct nfs_fattr *fattr,
6042 struct nfs4_label *olabel)
6043{
6044 struct nfs4_exception exception = { };
6045 int err;
6046
6047 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04006048 err = _nfs4_do_set_security_label(inode, ilabel,
6049 fattr, olabel);
6050 trace_nfs4_set_security_label(inode, err);
6051 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04006052 &exception);
6053 } while (exception.retry);
6054 return err;
6055}
6056
6057static int
Al Viro59301222016-05-27 10:19:30 -04006058nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04006059{
6060 struct nfs4_label ilabel, *olabel = NULL;
6061 struct nfs_fattr fattr;
David Quigleyaa9c2662013-05-22 12:50:44 -04006062 int status;
6063
6064 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
6065 return -EOPNOTSUPP;
6066
6067 nfs_fattr_init(&fattr);
6068
6069 ilabel.pi = 0;
6070 ilabel.lfs = 0;
6071 ilabel.label = (char *)buf;
6072 ilabel.len = buflen;
6073
David Quigleyaa9c2662013-05-22 12:50:44 -04006074 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
6075 if (IS_ERR(olabel)) {
6076 status = -PTR_ERR(olabel);
6077 goto out;
6078 }
6079
6080 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
6081 if (status == 0)
6082 nfs_setsecurity(inode, &fattr, olabel);
6083
6084 nfs4_label_free(olabel);
6085out:
David Quigleyaa9c2662013-05-22 12:50:44 -04006086 return status;
6087}
6088#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
6089
6090
Chuck Leverf0920752012-05-21 22:45:41 -04006091static void nfs4_init_boot_verifier(const struct nfs_client *clp,
6092 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05006093{
6094 __be32 verf[2];
6095
Chuck Lever2c820d92012-05-21 22:45:33 -04006096 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
6097 /* An impossible timestamp guarantees this value
6098 * will never match a generated boot time. */
Deepa Dinamani2f86e092016-10-01 16:46:26 -07006099 verf[0] = cpu_to_be32(U32_MAX);
6100 verf[1] = cpu_to_be32(U32_MAX);
Chuck Lever2c820d92012-05-21 22:45:33 -04006101 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04006102 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Deepa Dinamani2f86e092016-10-01 16:46:26 -07006103 u64 ns = ktime_to_ns(nn->boot_time);
6104
6105 verf[0] = cpu_to_be32(ns >> 32);
6106 verf[1] = cpu_to_be32(ns);
Chuck Lever2c820d92012-05-21 22:45:33 -04006107 }
Chuck Levercd937102012-03-02 17:14:31 -05006108 memcpy(bootverf->data, verf, sizeof(bootverf->data));
6109}
6110
Trond Myklebust1aee5512020-10-07 18:24:17 -04006111static size_t
Trond Myklebust39d43d12020-10-07 18:24:18 -04006112nfs4_get_uniquifier(struct nfs_client *clp, char *buf, size_t buflen)
Trond Myklebust1aee5512020-10-07 18:24:17 -04006113{
Trond Myklebust39d43d12020-10-07 18:24:18 -04006114 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
6115 struct nfs_netns_client *nn_clp = nn->nfs_client;
6116 const char *id;
6117
Trond Myklebust1aee5512020-10-07 18:24:17 -04006118 buf[0] = '\0';
6119
Trond Myklebust39d43d12020-10-07 18:24:18 -04006120 if (nn_clp) {
6121 rcu_read_lock();
6122 id = rcu_dereference(nn_clp->identifier);
6123 if (id)
6124 strscpy(buf, id, buflen);
6125 rcu_read_unlock();
6126 }
6127
6128 if (nfs4_client_id_uniquifier[0] != '\0' && buf[0] == '\0')
Trond Myklebust1aee5512020-10-07 18:24:17 -04006129 strscpy(buf, nfs4_client_id_uniquifier, buflen);
6130
6131 return strlen(buf);
6132}
6133
Jeff Laytona3192682015-06-09 19:43:59 -04006134static int
6135nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04006136{
Trond Myklebust1aee5512020-10-07 18:24:17 -04006137 char buf[NFS4_CLIENT_ID_UNIQ_LEN];
6138 size_t buflen;
Jeff Laytona3192682015-06-09 19:43:59 -04006139 size_t len;
6140 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04006141
Trond Myklebustceb3a162015-01-03 15:16:04 -05006142 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04006143 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08006144
Jeff Laytona3192682015-06-09 19:43:59 -04006145 rcu_read_lock();
Chuck Lever848a4eb2018-06-04 10:53:29 -04006146 len = 14 +
6147 strlen(clp->cl_rpcclient->cl_nodename) +
6148 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04006149 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
Jeff Laytona3192682015-06-09 19:43:59 -04006150 1;
6151 rcu_read_unlock();
Trond Myklebust1aee5512020-10-07 18:24:17 -04006152
Trond Myklebust39d43d12020-10-07 18:24:18 -04006153 buflen = nfs4_get_uniquifier(clp, buf, sizeof(buf));
Trond Myklebust1aee5512020-10-07 18:24:17 -04006154 if (buflen)
6155 len += buflen + 1;
6156
Jeff Laytona3192682015-06-09 19:43:59 -04006157 if (len > NFS4_OPAQUE_LIMIT + 1)
6158 return -EINVAL;
6159
6160 /*
6161 * Since this string is allocated at mount time, and held until the
6162 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6163 * about a memory-reclaim deadlock.
6164 */
6165 str = kmalloc(len, GFP_KERNEL);
6166 if (!str)
6167 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05006168
Chuck Levere984a552012-09-14 17:24:21 -04006169 rcu_read_lock();
Trond Myklebust1aee5512020-10-07 18:24:17 -04006170 if (buflen)
Chuck Lever025bb9f2018-06-04 10:53:34 -04006171 scnprintf(str, len, "Linux NFSv4.0 %s/%s/%s",
Trond Myklebust1aee5512020-10-07 18:24:17 -04006172 clp->cl_rpcclient->cl_nodename, buf,
Chuck Lever848a4eb2018-06-04 10:53:29 -04006173 rpc_peeraddr2str(clp->cl_rpcclient,
Chuck Lever025bb9f2018-06-04 10:53:34 -04006174 RPC_DISPLAY_ADDR));
Chuck Lever848a4eb2018-06-04 10:53:29 -04006175 else
Chuck Lever025bb9f2018-06-04 10:53:34 -04006176 scnprintf(str, len, "Linux NFSv4.0 %s/%s",
Chuck Lever848a4eb2018-06-04 10:53:29 -04006177 clp->cl_rpcclient->cl_nodename,
6178 rpc_peeraddr2str(clp->cl_rpcclient,
Chuck Lever025bb9f2018-06-04 10:53:34 -04006179 RPC_DISPLAY_ADDR));
Chuck Levere984a552012-09-14 17:24:21 -04006180 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04006181
Jeff Laytona3192682015-06-09 19:43:59 -04006182 clp->cl_owner_id = str;
6183 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04006184}
6185
Jeff Layton873e3852015-06-09 19:44:00 -04006186static int
Jeff Layton873e3852015-06-09 19:44:00 -04006187nfs4_init_uniform_client_string(struct nfs_client *clp)
6188{
Trond Myklebust1aee5512020-10-07 18:24:17 -04006189 char buf[NFS4_CLIENT_ID_UNIQ_LEN];
6190 size_t buflen;
Jeff Layton873e3852015-06-09 19:44:00 -04006191 size_t len;
6192 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05006193
6194 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04006195 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04006196
Jeff Layton873e3852015-06-09 19:44:00 -04006197 len = 10 + 10 + 1 + 10 + 1 +
6198 strlen(clp->cl_rpcclient->cl_nodename) + 1;
6199
Trond Myklebust39d43d12020-10-07 18:24:18 -04006200 buflen = nfs4_get_uniquifier(clp, buf, sizeof(buf));
Trond Myklebust1aee5512020-10-07 18:24:17 -04006201 if (buflen)
6202 len += buflen + 1;
6203
Jeff Layton873e3852015-06-09 19:44:00 -04006204 if (len > NFS4_OPAQUE_LIMIT + 1)
6205 return -EINVAL;
6206
6207 /*
6208 * Since this string is allocated at mount time, and held until the
6209 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6210 * about a memory-reclaim deadlock.
6211 */
6212 str = kmalloc(len, GFP_KERNEL);
6213 if (!str)
6214 return -ENOMEM;
6215
Trond Myklebust1aee5512020-10-07 18:24:17 -04006216 if (buflen)
6217 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
6218 clp->rpc_ops->version, clp->cl_minorversion,
6219 buf, clp->cl_rpcclient->cl_nodename);
6220 else
6221 scnprintf(str, len, "Linux NFSv%u.%u %s",
6222 clp->rpc_ops->version, clp->cl_minorversion,
6223 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04006224 clp->cl_owner_id = str;
6225 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04006226}
6227
Chuck Lever706cb8d2014-03-12 12:51:47 -04006228/*
6229 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
6230 * services. Advertise one based on the address family of the
6231 * clientaddr.
6232 */
6233static unsigned int
6234nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
6235{
6236 if (strchr(clp->cl_ipaddr, ':') != NULL)
6237 return scnprintf(buf, len, "tcp6");
6238 else
6239 return scnprintf(buf, len, "tcp");
6240}
6241
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006242static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
6243{
6244 struct nfs4_setclientid *sc = calldata;
6245
6246 if (task->tk_status == 0)
6247 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
6248}
6249
6250static const struct rpc_call_ops nfs4_setclientid_ops = {
6251 .rpc_call_done = nfs4_setclientid_done,
6252};
6253
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006254/**
6255 * nfs4_proc_setclientid - Negotiate client ID
6256 * @clp: state data structure
6257 * @program: RPC program for NFSv4 callback service
6258 * @port: IP port number for NFS4 callback service
NeilBrowna52458b2018-12-03 11:30:31 +11006259 * @cred: credential to use for this call
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006260 * @res: where to place the result
6261 *
6262 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6263 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006264int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
NeilBrowna52458b2018-12-03 11:30:31 +11006265 unsigned short port, const struct cred *cred,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006266 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006267{
6268 nfs4_verifier sc_verifier;
6269 struct nfs4_setclientid setclientid = {
6270 .sc_verifier = &sc_verifier,
6271 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04006272 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006273 };
6274 struct rpc_message msg = {
6275 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
6276 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006277 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01006278 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006279 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006280 struct rpc_task_setup task_setup_data = {
6281 .rpc_client = clp->cl_rpcclient,
6282 .rpc_message = &msg,
6283 .callback_ops = &nfs4_setclientid_ops,
6284 .callback_data = &setclientid,
NeilBrown5a0c2572019-05-30 10:41:28 +10006285 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006286 };
Robert Milkowski7dc29932020-01-30 09:43:25 +00006287 unsigned long now = jiffies;
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006288 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006289
Chuck Leverde734832012-07-11 16:30:50 -04006290 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04006291 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04006292
6293 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
6294 status = nfs4_init_uniform_client_string(clp);
6295 else
Jeff Laytona3192682015-06-09 19:43:59 -04006296 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04006297
6298 if (status)
6299 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04006300
Chuck Leverde734832012-07-11 16:30:50 -04006301 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04006302 setclientid.sc_netid_len =
6303 nfs4_init_callback_netid(clp,
6304 setclientid.sc_netid,
6305 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04006306 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05006307 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006308 clp->cl_ipaddr, port >> 8, port & 255);
6309
Jeff Layton3a6bb732015-06-09 19:43:57 -04006310 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006311 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04006312 clp->cl_owner_id);
Anna Schumakerdae40962019-08-14 15:28:28 -04006313
6314 status = nfs4_call_sync_custom(&task_setup_data);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006315 if (setclientid.sc_cred) {
Chuck Lever1047ec82019-10-04 09:58:54 -04006316 kfree(clp->cl_acceptor);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006317 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
6318 put_rpccred(setclientid.sc_cred);
6319 }
Robert Milkowski7dc29932020-01-30 09:43:25 +00006320
6321 if (status == 0)
6322 do_renew_lease(clp, now);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006323out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006324 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006325 dprintk("NFS reply setclientid: %d\n", status);
6326 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006327}
6328
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006329/**
6330 * nfs4_proc_setclientid_confirm - Confirm client ID
6331 * @clp: state data structure
Trond Myklebust302fad72019-02-18 13:32:38 -05006332 * @arg: result of a previous SETCLIENTID
NeilBrowna52458b2018-12-03 11:30:31 +11006333 * @cred: credential to use for this call
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006334 *
6335 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6336 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04006337int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006338 struct nfs4_setclientid_res *arg,
NeilBrowna52458b2018-12-03 11:30:31 +11006339 const struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006340{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006341 struct rpc_message msg = {
6342 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006343 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01006344 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006345 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006346 int status;
6347
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006348 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
6349 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6350 clp->cl_clientid);
NeilBrown5a0c2572019-05-30 10:41:28 +10006351 status = rpc_call_sync(clp->cl_rpcclient, &msg,
6352 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006353 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006354 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006355 return status;
6356}
6357
Trond Myklebustfe650402006-01-03 09:55:18 +01006358struct nfs4_delegreturndata {
6359 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01006360 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01006361 struct nfs_fh fh;
6362 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006363 unsigned long timestamp;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006364 struct {
6365 struct nfs4_layoutreturn_args arg;
6366 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04006367 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006368 u32 roc_barrier;
6369 bool roc;
6370 } lr;
Trond Myklebustfa178f22006-01-03 09:55:38 +01006371 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01006372 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08006373 struct inode *inode;
Trond Myklebustfe650402006-01-03 09:55:18 +01006374};
6375
Trond Myklebustfe650402006-01-03 09:55:18 +01006376static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
6377{
6378 struct nfs4_delegreturndata *data = calldata;
Trond Myklebuste0dba012017-11-07 11:02:32 -05006379 struct nfs4_exception exception = {
6380 .inode = data->inode,
6381 .stateid = &data->stateid,
6382 };
Andy Adamson938e1012009-04-01 09:22:28 -04006383
Trond Myklebust14516c32010-07-31 14:29:06 -04006384 if (!nfs4_sequence_done(task, &data->res.seq_res))
6385 return;
Andy Adamson938e1012009-04-01 09:22:28 -04006386
Trond Myklebustca8acf82013-08-13 10:36:56 -04006387 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Trond Myklebust586f1c32016-11-15 15:03:33 -05006388
6389 /* Handle Layoutreturn errors */
Trond Myklebust078000d2021-01-04 13:18:03 -05006390 if (pnfs_roc_done(task, &data->args.lr_args, &data->res.lr_res,
6391 &data->res.lr_ret) == -EAGAIN)
Trond Myklebust287a9c52019-09-20 07:23:41 -04006392 goto out_restart;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006393
Ricardo Labiaga79708862009-12-07 09:23:21 -05006394 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05006395 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01006396 renew_lease(data->res.server, data->timestamp);
Trond Myklebust23ea44c2016-11-10 16:06:28 -05006397 break;
Trond Myklebustc97cf602013-11-19 16:34:14 -05006398 case -NFS4ERR_ADMIN_REVOKED:
6399 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust26d36302016-09-22 13:39:05 -04006400 case -NFS4ERR_EXPIRED:
6401 nfs4_free_revoked_stateid(data->res.server,
6402 data->args.stateid,
6403 task->tk_msg.rpc_cred);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05006404 fallthrough;
Trond Myklebustc97cf602013-11-19 16:34:14 -05006405 case -NFS4ERR_BAD_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05006406 case -NFS4ERR_STALE_STATEID:
Trond Myklebust244fcd22019-12-20 10:43:37 -05006407 case -ETIMEDOUT:
Trond Myklebustc97cf602013-11-19 16:34:14 -05006408 task->tk_status = 0;
6409 break;
Trond Myklebust12f275c2017-11-06 15:28:05 -05006410 case -NFS4ERR_OLD_STATEID:
Trond Myklebust246afc0a2019-10-24 18:00:35 -04006411 if (!nfs4_refresh_delegation_stateid(&data->stateid, data->inode))
6412 nfs4_stateid_seqid_inc(&data->stateid);
Trond Myklebust70d136b2019-10-26 22:37:40 -04006413 if (data->args.bitmask) {
6414 data->args.bitmask = NULL;
6415 data->res.fattr = NULL;
6416 }
Trond Myklebust246afc0a2019-10-24 18:00:35 -04006417 goto out_restart;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05006418 case -NFS4ERR_ACCESS:
6419 if (data->args.bitmask) {
6420 data->args.bitmask = NULL;
6421 data->res.fattr = NULL;
Trond Myklebust140087fd2017-11-06 15:28:10 -05006422 goto out_restart;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05006423 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05006424 fallthrough;
Ricardo Labiaga79708862009-12-07 09:23:21 -05006425 default:
Trond Myklebuste0dba012017-11-07 11:02:32 -05006426 task->tk_status = nfs4_async_handle_exception(task,
6427 data->res.server, task->tk_status,
6428 &exception);
6429 if (exception.retry)
Trond Myklebust140087fd2017-11-06 15:28:10 -05006430 goto out_restart;
Ricardo Labiaga79708862009-12-07 09:23:21 -05006431 }
Trond Myklebustd51f91d2019-10-21 14:22:14 -04006432 nfs_delegation_mark_returned(data->inode, data->args.stateid);
Ricardo Labiaga79708862009-12-07 09:23:21 -05006433 data->rpc_status = task->tk_status;
Trond Myklebust140087fd2017-11-06 15:28:10 -05006434 return;
Trond Myklebust140087fd2017-11-06 15:28:10 -05006435out_restart:
6436 task->tk_status = 0;
6437 rpc_restart_call_prepare(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01006438}
6439
6440static void nfs4_delegreturn_release(void *calldata)
6441{
Peng Tao039b7562014-07-03 13:05:02 +08006442 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006443 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08006444
Trond Myklebust078000d2021-01-04 13:18:03 -05006445 if (data->lr.roc)
6446 pnfs_roc_release(&data->lr.arg, &data->lr.res,
6447 data->res.lr_ret);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006448 if (inode) {
Trond Myklebust0bc2c9b2016-12-16 19:48:09 -05006449 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006450 nfs_iput_and_deactive(inode);
6451 }
Trond Myklebustfe650402006-01-03 09:55:18 +01006452 kfree(calldata);
6453}
6454
Andy Adamson938e1012009-04-01 09:22:28 -04006455static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
6456{
6457 struct nfs4_delegreturndata *d_data;
Trond Myklebustc8bf7072018-06-15 16:31:02 -04006458 struct pnfs_layout_hdr *lo;
Andy Adamson938e1012009-04-01 09:22:28 -04006459
6460 d_data = (struct nfs4_delegreturndata *)data;
6461
Trond Myklebust5326de92019-11-13 09:39:36 +01006462 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task)) {
6463 nfs4_sequence_done(task, &d_data->res.seq_res);
Peng Tao500d7012015-09-22 11:35:22 +08006464 return;
Trond Myklebust5326de92019-11-13 09:39:36 +01006465 }
Peng Tao500d7012015-09-22 11:35:22 +08006466
Trond Myklebustc8bf7072018-06-15 16:31:02 -04006467 lo = d_data->args.lr_args ? d_data->args.lr_args->layout : NULL;
6468 if (lo && !pnfs_layout_is_valid(lo)) {
6469 d_data->args.lr_args = NULL;
6470 d_data->res.lr_res = NULL;
6471 }
6472
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006473 nfs4_setup_sequence(d_data->res.server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006474 &d_data->args.seq_args,
6475 &d_data->res.seq_res,
6476 task);
Andy Adamson938e1012009-04-01 09:22:28 -04006477}
Andy Adamson938e1012009-04-01 09:22:28 -04006478
Jesper Juhlc8d149f2006-03-20 13:44:07 -05006479static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04006480 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01006481 .rpc_call_done = nfs4_delegreturn_done,
6482 .rpc_release = nfs4_delegreturn_release,
6483};
6484
NeilBrowna52458b2018-12-03 11:30:31 +11006485static int _nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01006486{
6487 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01006488 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01006489 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006490 struct rpc_message msg = {
6491 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
6492 .rpc_cred = cred,
6493 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006494 struct rpc_task_setup task_setup_data = {
6495 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04006496 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006497 .callback_ops = &nfs4_delegreturn_ops,
Trond Myklebustf304a802020-05-11 10:42:04 -04006498 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006499 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05006500 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01006501
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006502 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01006503 if (data == NULL)
6504 return -ENOMEM;
Anna Schumakerfba83f32018-05-04 16:22:50 -04006505 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Andrew Elble99ade3c2015-12-02 09:39:51 -05006506
6507 nfs4_state_protect(server->nfs_client,
6508 NFS_SP4_MACH_CRED_CLEANUP,
6509 &task_setup_data.rpc_client, &msg);
6510
Trond Myklebustfe650402006-01-03 09:55:18 +01006511 data->args.fhandle = &data->fh;
6512 data->args.stateid = &data->stateid;
Trond Myklebust332d1a02021-03-25 18:15:36 -04006513 nfs4_bitmask_set(data->args.bitmask_store,
6514 server->cache_consistency_bitmask, inode, server,
6515 NULL);
6516 data->args.bitmask = data->args.bitmask_store;
Trond Myklebustfe650402006-01-03 09:55:18 +01006517 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05006518 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01006519 data->res.fattr = &data->fattr;
6520 data->res.server = server;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006521 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust4d796d72016-09-23 11:38:08 -04006522 data->lr.arg.ld_private = &data->lr.ld_private;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006523 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01006524 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01006525 data->rpc_status = 0;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006526 data->inode = nfs_igrab_and_active(inode);
Trond Myklebust078000d2021-01-04 13:18:03 -05006527 if (data->inode || issync) {
6528 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res,
6529 cred);
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006530 if (data->lr.roc) {
6531 data->args.lr_args = &data->lr.arg;
6532 data->res.lr_res = &data->lr.res;
6533 }
6534 }
Trond Myklebustfe650402006-01-03 09:55:18 +01006535
Trond Myklebustc970aa82007-07-14 15:39:59 -04006536 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05006537 msg.rpc_argp = &data->args;
6538 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006539 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006540 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01006541 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05006542 if (!issync)
6543 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05006544 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05006545 if (status != 0)
6546 goto out;
6547 status = data->rpc_status;
Trond Myklebuste6f81072008-01-24 18:14:34 -05006548out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006549 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01006550 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006551}
6552
NeilBrowna52458b2018-12-03 11:30:31 +11006553int nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006554{
6555 struct nfs_server *server = NFS_SERVER(inode);
6556 struct nfs4_exception exception = { };
6557 int err;
6558 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05006559 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006560 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006561 switch (err) {
6562 case -NFS4ERR_STALE_STATEID:
6563 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006564 case 0:
6565 return 0;
6566 }
6567 err = nfs4_handle_exception(server, err, &exception);
6568 } while (exception.retry);
6569 return err;
6570}
6571
Linus Torvalds1da177e2005-04-16 15:20:36 -07006572static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6573{
6574 struct inode *inode = state->inode;
6575 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04006576 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006577 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006578 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006579 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006580 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006581 struct nfs_lockt_res res = {
6582 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006583 };
6584 struct rpc_message msg = {
6585 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
Anna Schumakerd9b67e12017-01-11 15:04:25 -05006586 .rpc_argp = &arg,
6587 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006588 .rpc_cred = state->owner->so_cred,
6589 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006590 struct nfs4_lock_state *lsp;
6591 int status;
6592
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006593 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00006594 status = nfs4_set_lock_state(state, request);
6595 if (status != 0)
6596 goto out;
6597 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006598 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006599 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00006600 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006601 switch (status) {
6602 case 0:
6603 request->fl_type = F_UNLCK;
6604 break;
6605 case -NFS4ERR_DENIED:
6606 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006607 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05006608 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04006609 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00006610out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006611 return status;
6612}
6613
6614static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6615{
Trond Myklebust0688e642019-04-07 13:59:09 -04006616 struct nfs4_exception exception = {
6617 .interruptible = true,
6618 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006619 int err;
6620
6621 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006622 err = _nfs4_proc_getlk(state, cmd, request);
6623 trace_nfs4_get_lock(request, state, cmd, err);
6624 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006625 &exception);
6626 } while (exception.retry);
6627 return err;
6628}
6629
Trond Myklebust32c6e7e2019-09-20 07:23:48 -04006630/*
6631 * Update the seqid of a lock stateid after receiving
6632 * NFS4ERR_OLD_STATEID
6633 */
6634static bool nfs4_refresh_lock_old_stateid(nfs4_stateid *dst,
6635 struct nfs4_lock_state *lsp)
6636{
6637 struct nfs4_state *state = lsp->ls_state;
6638 bool ret = false;
6639
6640 spin_lock(&state->state_lock);
6641 if (!nfs4_stateid_match_other(dst, &lsp->ls_stateid))
6642 goto out;
6643 if (!nfs4_stateid_is_newer(&lsp->ls_stateid, dst))
6644 nfs4_stateid_seqid_inc(dst);
6645 else
6646 dst->seqid = lsp->ls_stateid.seqid;
6647 ret = true;
6648out:
6649 spin_unlock(&state->state_lock);
6650 return ret;
6651}
6652
6653static bool nfs4_sync_lock_stateid(nfs4_stateid *dst,
6654 struct nfs4_lock_state *lsp)
6655{
6656 struct nfs4_state *state = lsp->ls_state;
6657 bool ret;
6658
6659 spin_lock(&state->state_lock);
6660 ret = !nfs4_stateid_match_other(dst, &lsp->ls_stateid);
6661 nfs4_stateid_copy(dst, &lsp->ls_stateid);
6662 spin_unlock(&state->state_lock);
6663 return ret;
6664}
6665
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006666struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006667 struct nfs_locku_args arg;
6668 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006669 struct nfs4_lock_state *lsp;
6670 struct nfs_open_context *ctx;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006671 struct nfs_lock_context *l_ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006672 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04006673 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006674 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006675};
6676
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006677static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
6678 struct nfs_open_context *ctx,
6679 struct nfs4_lock_state *lsp,
6680 struct nfs_seqid *seqid)
6681{
6682 struct nfs4_unlockdata *p;
Trond Myklebust32c6e7e2019-09-20 07:23:48 -04006683 struct nfs4_state *state = lsp->ls_state;
6684 struct inode *inode = state->inode;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006685
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006686 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006687 if (p == NULL)
6688 return NULL;
6689 p->arg.fh = NFS_FH(inode);
6690 p->arg.fl = &p->fl;
6691 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006692 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006693 p->lsp = lsp;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006694 /* Ensure we don't close file until we're done freeing locks! */
6695 p->ctx = get_nfs_open_context(ctx);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006696 p->l_ctx = nfs_get_lock_context(ctx);
NeilBrown7b587e12018-11-30 10:04:08 +11006697 locks_init_lock(&p->fl);
6698 locks_copy_lock(&p->fl, fl);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006699 p->server = NFS_SERVER(inode);
Trond Myklebust32c6e7e2019-09-20 07:23:48 -04006700 spin_lock(&state->state_lock);
6701 nfs4_stateid_copy(&p->arg.stateid, &lsp->ls_stateid);
6702 spin_unlock(&state->state_lock);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006703 return p;
6704}
6705
Trond Myklebust06f814a2006-01-03 09:55:07 +01006706static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006707{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006708 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006709 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006710 nfs4_put_lock_state(calldata->lsp);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006711 nfs_put_lock_context(calldata->l_ctx);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006712 put_nfs_open_context(calldata->ctx);
6713 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006714}
6715
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006716static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006717{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006718 struct nfs4_unlockdata *calldata = data;
Trond Myklebust82571552017-11-07 11:14:49 -05006719 struct nfs4_exception exception = {
6720 .inode = calldata->lsp->ls_state->inode,
6721 .stateid = &calldata->arg.stateid,
6722 };
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006723
Trond Myklebust14516c32010-07-31 14:29:06 -04006724 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
6725 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006726 switch (task->tk_status) {
6727 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01006728 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton75575dd2016-09-17 18:17:32 -04006729 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006730 if (nfs4_update_lock_stateid(calldata->lsp,
6731 &calldata->res.stateid))
6732 break;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05006733 fallthrough;
Trond Myklebust26d36302016-09-22 13:39:05 -04006734 case -NFS4ERR_ADMIN_REVOKED:
6735 case -NFS4ERR_EXPIRED:
6736 nfs4_free_revoked_stateid(calldata->server,
6737 &calldata->arg.stateid,
6738 task->tk_msg.rpc_cred);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05006739 fallthrough;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05006740 case -NFS4ERR_BAD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006741 case -NFS4ERR_STALE_STATEID:
Trond Myklebust32c6e7e2019-09-20 07:23:48 -04006742 if (nfs4_sync_lock_stateid(&calldata->arg.stateid,
6743 calldata->lsp))
6744 rpc_restart_call_prepare(task);
6745 break;
6746 case -NFS4ERR_OLD_STATEID:
6747 if (nfs4_refresh_lock_old_stateid(&calldata->arg.stateid,
6748 calldata->lsp))
Trond Myklebust425c1d42015-01-24 14:57:53 -05006749 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006750 break;
6751 default:
Trond Myklebust82571552017-11-07 11:14:49 -05006752 task->tk_status = nfs4_async_handle_exception(task,
6753 calldata->server, task->tk_status,
6754 &exception);
6755 if (exception.retry)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006756 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006757 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04006758 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006759}
6760
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006761static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006762{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006763 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006764
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006765 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
6766 nfs_async_iocounter_wait(task, calldata->l_ctx))
6767 return;
6768
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006769 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006770 goto out_wait;
Trond Myklebust795a88c2012-09-10 13:26:49 -04006771 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006772 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006773 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006774 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006775 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006776 if (nfs4_setup_sequence(calldata->server->nfs_client,
Andy Adamsona8936932009-04-01 09:22:23 -04006777 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006778 &calldata->res.seq_res,
6779 task) != 0)
6780 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006781 return;
6782out_no_action:
6783 task->tk_action = NULL;
6784out_wait:
6785 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006786}
6787
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006788static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006789 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006790 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01006791 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006792};
6793
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006794static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6795 struct nfs_open_context *ctx,
6796 struct nfs4_lock_state *lsp,
6797 struct nfs_seqid *seqid)
6798{
6799 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006800 struct rpc_message msg = {
6801 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6802 .rpc_cred = ctx->cred,
6803 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006804 struct rpc_task_setup task_setup_data = {
6805 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006806 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006807 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006808 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006809 .flags = RPC_TASK_ASYNC,
6810 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006811
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006812 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6813 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6814
Frank Filz137d6ac2007-07-09 15:32:29 -07006815 /* Ensure this is an unlock - when canceling a lock, the
6816 * canceled lock is passed in, and it won't be an unlock.
6817 */
6818 fl->fl_type = F_UNLCK;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006819 if (fl->fl_flags & FL_CLOSE)
6820 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
Frank Filz137d6ac2007-07-09 15:32:29 -07006821
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006822 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6823 if (data == NULL) {
6824 nfs_free_seqid(seqid);
6825 return ERR_PTR(-ENOMEM);
6826 }
6827
Anna Schumakerfba83f32018-05-04 16:22:50 -04006828 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1, 0);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006829 msg.rpc_argp = &data->arg;
6830 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006831 task_setup_data.callback_data = data;
6832 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006833}
6834
Linus Torvalds1da177e2005-04-16 15:20:36 -07006835static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6836{
Trond Myklebust65b62a22013-02-07 10:54:07 -05006837 struct inode *inode = state->inode;
6838 struct nfs4_state_owner *sp = state->owner;
6839 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006840 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006841 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01006842 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006843 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006844 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006845 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006846
Trond Myklebust9b073572006-06-29 16:38:34 -04006847 status = nfs4_set_lock_state(state, request);
6848 /* Unlock _before_ we do the RPC call */
6849 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05006850 /* Exclude nfs_delegation_claim_locks() */
6851 mutex_lock(&sp->so_delegreturn_mutex);
6852 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006853 down_read(&nfsi->rwsem);
Jeff Layton75575dd2016-09-17 18:17:32 -04006854 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05006855 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006856 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006857 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05006858 }
6859 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 if (status != 0)
6862 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006863 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006864 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04006865 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6866 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006867 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6868 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04006869 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006870 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04006871 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006872 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006873 status = PTR_ERR(task);
6874 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04006875 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05006876 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006877 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04006878out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006879 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006880 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006881 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006882}
6883
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006884struct nfs4_lockdata {
6885 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006886 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006887 struct nfs4_lock_state *lsp;
6888 struct nfs_open_context *ctx;
6889 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006890 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006891 int rpc_status;
6892 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04006893 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006894};
6895
6896static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006897 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6898 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006899{
6900 struct nfs4_lockdata *p;
6901 struct inode *inode = lsp->ls_state->inode;
6902 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05006903 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006904
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006905 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006906 if (p == NULL)
6907 return NULL;
6908
6909 p->arg.fh = NFS_FH(inode);
6910 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006911 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006912 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006913 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006914 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6915 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006916 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006917 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04006918 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006919 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006920 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006921 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006922 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04006923 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006924 p->ctx = get_nfs_open_context(ctx);
NeilBrown7b587e12018-11-30 10:04:08 +11006925 locks_init_lock(&p->fl);
6926 locks_copy_lock(&p->fl, fl);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006927 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006928out_free_seqid:
6929 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006930out_free:
6931 kfree(p);
6932 return NULL;
6933}
6934
6935static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6936{
6937 struct nfs4_lockdata *data = calldata;
6938 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006939
Harvey Harrison3110ff82008-05-02 13:42:44 -07006940 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006941 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006942 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006943 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05006944 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006945 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006946 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006947 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05006948 nfs4_stateid_copy(&data->arg.open_stateid,
6949 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006950 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006951 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006952 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006953 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006954 nfs4_stateid_copy(&data->arg.lock_stateid,
6955 &data->lsp->ls_stateid);
6956 }
Trond Myklebust5d422302013-03-14 16:57:48 -04006957 if (!nfs4_valid_open_stateid(state)) {
6958 data->rpc_status = -EBADF;
6959 task->tk_action = NULL;
6960 goto out_release_open_seqid;
6961 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006962 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006963 if (nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust035168ab2010-06-16 09:52:26 -04006964 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006965 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006966 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04006967 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04006968out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006969 nfs_release_seqid(data->arg.open_seqid);
6970out_release_lock_seqid:
6971 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006972out_wait:
6973 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006974 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006975}
6976
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006977static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6978{
6979 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05006980 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006981
Harvey Harrison3110ff82008-05-02 13:42:44 -07006982 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006983
Trond Myklebust14516c32010-07-31 14:29:06 -04006984 if (!nfs4_sequence_done(task, &data->res.seq_res))
6985 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04006986
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006987 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006988 switch (task->tk_status) {
6989 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00006990 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05006991 data->timestamp);
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04006992 if (data->arg.new_lock && !data->cancelled) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05006993 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04006994 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0)
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006995 goto out_restart;
Trond Myklebustc69899a2015-01-24 16:03:52 -05006996 }
Trond Myklebust39071e62015-01-24 15:07:56 -05006997 if (data->arg.new_lock_owner != 0) {
6998 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6999 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
7000 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04007001 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
7002 goto out_restart;
Trond Myklebust425c1d42015-01-24 14:57:53 -05007003 break;
7004 case -NFS4ERR_BAD_STATEID:
7005 case -NFS4ERR_OLD_STATEID:
7006 case -NFS4ERR_STALE_STATEID:
7007 case -NFS4ERR_EXPIRED:
7008 if (data->arg.new_lock_owner != 0) {
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04007009 if (!nfs4_stateid_match(&data->arg.open_stateid,
Trond Myklebust425c1d42015-01-24 14:57:53 -05007010 &lsp->ls_state->open_stateid))
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04007011 goto out_restart;
7012 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
Trond Myklebust425c1d42015-01-24 14:57:53 -05007013 &lsp->ls_stateid))
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04007014 goto out_restart;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007015 }
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04007016out_done:
Harvey Harrison3110ff82008-05-02 13:42:44 -07007017 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04007018 return;
7019out_restart:
7020 if (!data->cancelled)
7021 rpc_restart_call_prepare(task);
7022 goto out_done;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007023}
7024
7025static void nfs4_lock_release(void *calldata)
7026{
7027 struct nfs4_lockdata *data = calldata;
7028
Harvey Harrison3110ff82008-05-02 13:42:44 -07007029 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05007030 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04007031 if (data->cancelled && data->rpc_status == 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007032 struct rpc_task *task;
7033 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
7034 data->arg.lock_seqid);
7035 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08007036 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07007037 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007038 } else
7039 nfs_free_seqid(data->arg.lock_seqid);
7040 nfs4_put_lock_state(data->lsp);
7041 put_nfs_open_context(data->ctx);
7042 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07007043 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007044}
7045
7046static const struct rpc_call_ops nfs4_lock_ops = {
7047 .rpc_call_prepare = nfs4_lock_prepare,
7048 .rpc_call_done = nfs4_lock_done,
7049 .rpc_release = nfs4_lock_release,
7050};
7051
Trond Myklebust2bee72a2010-01-26 15:42:21 -05007052static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
7053{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05007054 switch (error) {
7055 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustd7f3e4b2016-09-22 13:39:09 -04007056 case -NFS4ERR_EXPIRED:
Trond Myklebust2bee72a2010-01-26 15:42:21 -05007057 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05007058 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05007059 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04007060 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05007061 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05007062 break;
7063 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05007064 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05007065 nfs4_schedule_lease_recovery(server->nfs_client);
zhengbin8b98a532019-12-19 18:34:47 +08007066 }
Trond Myklebust2bee72a2010-01-26 15:42:21 -05007067}
7068
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08007069static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007070{
7071 struct nfs4_lockdata *data;
7072 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04007073 struct rpc_message msg = {
7074 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
7075 .rpc_cred = state->owner->so_cred,
7076 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04007077 struct rpc_task_setup task_setup_data = {
7078 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04007079 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04007080 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05007081 .workqueue = nfsiod_workqueue,
Trond Myklebust61296502020-02-07 19:38:12 -05007082 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
Trond Myklebustc970aa82007-07-14 15:39:59 -04007083 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007084 int ret;
7085
Harvey Harrison3110ff82008-05-02 13:42:44 -07007086 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04007087 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04007088 fl->fl_u.nfs4_fl.owner,
7089 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007090 if (data == NULL)
7091 return -ENOMEM;
7092 if (IS_SETLKW(cmd))
7093 data->arg.block = 1;
Anna Schumakerfba83f32018-05-04 16:22:50 -04007094 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1,
7095 recovery_type > NFS_LOCK_NEW);
Trond Myklebust1174dd12010-12-21 10:52:24 -05007096 msg.rpc_argp = &data->arg;
7097 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04007098 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007099 if (recovery_type > NFS_LOCK_NEW) {
7100 if (recovery_type == NFS_LOCK_RECLAIM)
7101 data->arg.reclaim = NFS_LOCK_RECLAIM;
Trond Myklebustc69899a2015-01-24 16:03:52 -05007102 } else
7103 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04007104 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05007105 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007106 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05007107 ret = rpc_wait_for_completion_task(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007108 if (ret == 0) {
7109 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05007110 if (ret)
7111 nfs4_handle_setlk_error(data->server, data->lsp,
7112 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007113 } else
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04007114 data->cancelled = true;
Dave Wysochanski3d1a90a2020-12-11 05:12:51 -05007115 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05007116 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07007117 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007118 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007119}
7120
7121static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
7122{
Trond Myklebust202b50d2005-06-22 17:16:29 +00007123 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04007124 struct nfs4_exception exception = {
7125 .inode = state->inode,
7126 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00007127 int err;
7128
7129 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04007130 /* Cache the lock if possible... */
7131 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
7132 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08007133 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04007134 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00007135 break;
7136 nfs4_handle_exception(server, err, &exception);
7137 } while (exception.retry);
7138 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007139}
7140
7141static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
7142{
Trond Myklebust202b50d2005-06-22 17:16:29 +00007143 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04007144 struct nfs4_exception exception = {
7145 .inode = state->inode,
7146 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00007147 int err;
7148
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05007149 err = nfs4_set_lock_state(state, request);
7150 if (err != 0)
7151 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04007152 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10007153 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
7154 return 0;
7155 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00007156 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04007157 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
7158 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08007159 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05007160 switch (err) {
7161 default:
7162 goto out;
7163 case -NFS4ERR_GRACE:
7164 case -NFS4ERR_DELAY:
7165 nfs4_handle_exception(server, err, &exception);
7166 err = 0;
7167 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00007168 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05007169out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00007170 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007171}
7172
Bryan Schumakerf062eb62011-06-02 14:59:10 -04007173#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05007174static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
7175{
Trond Myklebustc5896fc2016-09-22 13:39:03 -04007176 struct nfs4_lock_state *lsp;
7177 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05007178
Trond Myklebustc5896fc2016-09-22 13:39:03 -04007179 status = nfs4_set_lock_state(state, request);
7180 if (status != 0)
7181 return status;
7182 lsp = request->fl_u.nfs4_fl.owner;
7183 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
7184 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
7185 return 0;
Anna Schumaker81b68de2017-01-11 16:41:34 -05007186 return nfs4_lock_expired(state, request);
Bryan Schumakerf062eb62011-06-02 14:59:10 -04007187}
7188#endif
7189
Linus Torvalds1da177e2005-04-16 15:20:36 -07007190static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7191{
Trond Myklebust19e03c52008-12-23 15:21:44 -05007192 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04007193 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04007194 unsigned char fl_flags = request->fl_flags;
Jeff Layton1ea67db2016-09-17 18:17:37 -04007195 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007196
Trond Myklebust01c3b862006-06-29 16:38:39 -04007197 request->fl_flags |= FL_ACCESS;
Jeff Layton75575dd2016-09-17 18:17:32 -04007198 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04007199 if (status < 0)
7200 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04007201 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05007202 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04007203 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04007204 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04007205 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05007206 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton75575dd2016-09-17 18:17:32 -04007207 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05007208 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04007209 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05007210 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04007211 }
Trond Myklebust9a99af42013-02-04 20:17:49 -05007212 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04007213 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08007214 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04007215out:
7216 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007217 return status;
7218}
7219
7220static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7221{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05007222 struct nfs4_exception exception = {
7223 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04007224 .inode = state->inode,
Trond Myklebust0688e642019-04-07 13:59:09 -04007225 .interruptible = true,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05007226 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07007227 int err;
7228
7229 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07007230 err = _nfs4_proc_setlk(state, cmd, request);
7231 if (err == -NFS4ERR_DENIED)
7232 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007233 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07007234 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007235 } while (exception.retry);
7236 return err;
7237}
7238
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04007239#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
7240#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
7241
7242static int
Jeff Laytona1d617d82016-09-17 18:17:39 -04007243nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
7244 struct file_lock *request)
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04007245{
7246 int status = -ERESTARTSYS;
7247 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
7248
7249 while(!signalled()) {
7250 status = nfs4_proc_setlk(state, cmd, request);
7251 if ((status != -EAGAIN) || IS_SETLK(cmd))
7252 break;
7253 freezable_schedule_timeout_interruptible(timeout);
7254 timeout *= 2;
7255 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
7256 status = -ERESTARTSYS;
7257 }
7258 return status;
7259}
7260
Jeff Laytona1d617d82016-09-17 18:17:39 -04007261#ifdef CONFIG_NFS_V4_1
7262struct nfs4_lock_waiter {
Jeff Laytona1d617d82016-09-17 18:17:39 -04007263 struct inode *inode;
Trond Myklebust86438182021-02-02 14:22:51 -05007264 struct nfs_lowner owner;
7265 wait_queue_entry_t wait;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007266};
7267
7268static int
Ingo Molnarac6424b2017-06-20 12:06:13 +02007269nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
Jeff Laytona1d617d82016-09-17 18:17:39 -04007270{
Trond Myklebust86438182021-02-02 14:22:51 -05007271 struct nfs4_lock_waiter *waiter =
7272 container_of(wait, struct nfs4_lock_waiter, wait);
Jeff Laytona1d617d82016-09-17 18:17:39 -04007273
Jeff Layton57174592018-03-18 08:37:03 -04007274 /* NULL key means to wake up everyone */
7275 if (key) {
7276 struct cb_notify_lock_args *cbnl = key;
7277 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
Trond Myklebust86438182021-02-02 14:22:51 -05007278 *wowner = &waiter->owner;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007279
Jeff Layton57174592018-03-18 08:37:03 -04007280 /* Only wake if the callback was for the same owner. */
7281 if (lowner->id != wowner->id || lowner->s_dev != wowner->s_dev)
7282 return 0;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007283
Jeff Layton57174592018-03-18 08:37:03 -04007284 /* Make sure it's for the right inode */
7285 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
7286 return 0;
Jeff Layton57174592018-03-18 08:37:03 -04007287 }
Jeff Laytona1d617d82016-09-17 18:17:39 -04007288
Trond Myklebust86438182021-02-02 14:22:51 -05007289 return woken_wake_function(wait, mode, flags, key);
Jeff Laytona1d617d82016-09-17 18:17:39 -04007290}
7291
7292static int
7293nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7294{
Jeff Laytona1d617d82016-09-17 18:17:39 -04007295 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
7296 struct nfs_server *server = NFS_SERVER(state->inode);
7297 struct nfs_client *clp = server->nfs_client;
7298 wait_queue_head_t *q = &clp->cl_lock_waitq;
Trond Myklebust86438182021-02-02 14:22:51 -05007299 struct nfs4_lock_waiter waiter = {
7300 .inode = state->inode,
7301 .owner = { .clientid = clp->cl_clientid,
7302 .id = lsp->ls_seqid.owner_id,
7303 .s_dev = server->s_dev },
7304 };
7305 int status;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007306
7307 /* Don't bother with waitqueue if we don't expect a callback */
7308 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
7309 return nfs4_retry_setlk_simple(state, cmd, request);
7310
Trond Myklebust86438182021-02-02 14:22:51 -05007311 init_wait(&waiter.wait);
7312 waiter.wait.func = nfs4_wake_lock_waiter;
7313 add_wait_queue(q, &waiter.wait);
Jeff Laytona1d617d82016-09-17 18:17:39 -04007314
Trond Myklebust86438182021-02-02 14:22:51 -05007315 do {
Jeff Laytona1d617d82016-09-17 18:17:39 -04007316 status = nfs4_proc_setlk(state, cmd, request);
Trond Myklebust86438182021-02-02 14:22:51 -05007317 if (status != -EAGAIN || IS_SETLK(cmd))
Jeff Laytona1d617d82016-09-17 18:17:39 -04007318 break;
7319
7320 status = -ERESTARTSYS;
Yihao Wu52b042a2019-05-22 01:57:10 +08007321 freezer_do_not_count();
Trond Myklebust86438182021-02-02 14:22:51 -05007322 wait_woken(&waiter.wait, TASK_INTERRUPTIBLE,
7323 NFS4_LOCK_MAXTIMEOUT);
Yihao Wu52b042a2019-05-22 01:57:10 +08007324 freezer_count();
Trond Myklebust86438182021-02-02 14:22:51 -05007325 } while (!signalled());
7326
7327 remove_wait_queue(q, &waiter.wait);
Jeff Laytona1d617d82016-09-17 18:17:39 -04007328
Jeff Laytona1d617d82016-09-17 18:17:39 -04007329 return status;
7330}
7331#else /* !CONFIG_NFS_V4_1 */
7332static inline int
7333nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7334{
7335 return nfs4_retry_setlk_simple(state, cmd, request);
7336}
7337#endif
7338
Linus Torvalds1da177e2005-04-16 15:20:36 -07007339static int
7340nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
7341{
7342 struct nfs_open_context *ctx;
7343 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007344 int status;
7345
7346 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04007347 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007348 state = ctx->state;
7349
Trond Myklebustd9531262009-07-21 19:22:38 -04007350 if (IS_GETLK(cmd)) {
7351 if (state != NULL)
7352 return nfs4_proc_getlk(state, F_GETLK, request);
7353 return 0;
7354 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007355
7356 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
7357 return -EINVAL;
7358
Trond Myklebustd9531262009-07-21 19:22:38 -04007359 if (request->fl_type == F_UNLCK) {
7360 if (state != NULL)
7361 return nfs4_proc_unlck(state, cmd, request);
7362 return 0;
7363 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007364
Trond Myklebustd9531262009-07-21 19:22:38 -04007365 if (state == NULL)
7366 return -ENOLCK;
Jeff Layton1ea67db2016-09-17 18:17:37 -04007367
7368 if ((request->fl_flags & FL_POSIX) &&
7369 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
7370 return -ENOLCK;
7371
Benjamin Coddingtonfcfa4472017-11-10 06:27:49 -05007372 /*
7373 * Don't rely on the VFS having checked the file open mode,
7374 * since it won't do this for flock() locks.
7375 */
7376 switch (request->fl_type) {
7377 case F_RDLCK:
7378 if (!(filp->f_mode & FMODE_READ))
7379 return -EBADF;
7380 break;
7381 case F_WRLCK:
7382 if (!(filp->f_mode & FMODE_WRITE))
7383 return -EBADF;
7384 }
7385
Jeff Layton1ea67db2016-09-17 18:17:37 -04007386 status = nfs4_set_lock_state(state, request);
7387 if (status != 0)
7388 return status;
7389
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04007390 return nfs4_retry_setlk(state, cmd, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007391}
7392
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04007393int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05007394{
7395 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05007396 int err;
7397
7398 err = nfs4_set_lock_state(state, fl);
7399 if (err != 0)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04007400 return err;
Olga Kornievskaia3d7a9522020-08-20 18:52:43 -04007401 do {
7402 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
7403 if (err != -NFS4ERR_DELAY)
7404 break;
7405 ssleep(1);
7406 } while (err == -NFS4ERR_DELAY);
NeilBrowndce26302017-12-13 09:57:09 +11007407 return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05007408}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007409
Trond Myklebustcf470c32012-03-07 13:49:12 -05007410struct nfs_release_lockowner_data {
7411 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04007412 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007413 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08007414 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04007415 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007416};
7417
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007418static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
7419{
7420 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08007421 struct nfs_server *server = data->server;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05007422 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
7423 &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08007424 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04007425 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007426}
7427
7428static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
7429{
7430 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04007431 struct nfs_server *server = data->server;
7432
Trond Myklebustb7e63a12014-02-26 11:19:14 -08007433 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04007434
7435 switch (task->tk_status) {
7436 case 0:
7437 renew_lease(server, data->timestamp);
7438 break;
7439 case -NFS4ERR_STALE_CLIENTID:
7440 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08007441 nfs4_schedule_lease_recovery(server->nfs_client);
7442 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04007443 case -NFS4ERR_LEASE_MOVED:
7444 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10007445 if (nfs4_async_handle_error(task, server,
7446 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04007447 rpc_restart_call_prepare(task);
7448 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007449}
7450
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007451static void nfs4_release_lockowner_release(void *calldata)
7452{
Trond Myklebustcf470c32012-03-07 13:49:12 -05007453 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04007454 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007455 kfree(calldata);
7456}
7457
Trond Myklebust17280172012-03-11 13:11:00 -04007458static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007459 .rpc_call_prepare = nfs4_release_lockowner_prepare,
7460 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007461 .rpc_release = nfs4_release_lockowner_release,
7462};
7463
Jeff Laytonf1cdae82014-05-01 06:28:47 -04007464static void
7465nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007466{
Trond Myklebustcf470c32012-03-07 13:49:12 -05007467 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007468 struct rpc_message msg = {
7469 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
7470 };
7471
7472 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04007473 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007474
Trond Myklebustcf470c32012-03-07 13:49:12 -05007475 data = kmalloc(sizeof(*data), GFP_NOFS);
7476 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04007477 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007478 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04007479 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007480 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7481 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
7482 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007483
Trond Myklebustcf470c32012-03-07 13:49:12 -05007484 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08007485 msg.rpc_resp = &data->res;
Anna Schumakerfba83f32018-05-04 16:22:50 -04007486 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05007487 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007488}
7489
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00007490#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
7491
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007492static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Christian Braunere65ce2a2021-01-21 14:19:27 +01007493 struct user_namespace *mnt_userns,
Al Viro59301222016-05-27 10:19:30 -04007494 struct dentry *unused, struct inode *inode,
7495 const char *key, const void *buf,
7496 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007497{
Al Viro59301222016-05-27 10:19:30 -04007498 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007499}
7500
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007501static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04007502 struct dentry *unused, struct inode *inode,
7503 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007504{
Al Virob2968212016-04-10 20:48:24 -04007505 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007506}
7507
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01007508static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007509{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01007510 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007511}
7512
David Quigleyc9bccef2013-05-22 12:50:45 -04007513#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04007514
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007515static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Christian Braunere65ce2a2021-01-21 14:19:27 +01007516 struct user_namespace *mnt_userns,
Al Viro59301222016-05-27 10:19:30 -04007517 struct dentry *unused, struct inode *inode,
7518 const char *key, const void *buf,
7519 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04007520{
7521 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04007522 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04007523
7524 return -EOPNOTSUPP;
7525}
7526
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007527static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04007528 struct dentry *unused, struct inode *inode,
7529 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04007530{
7531 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04007532 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04007533 return -EOPNOTSUPP;
7534}
7535
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007536static ssize_t
7537nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04007538{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007539 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04007540
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007541 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
7542 len = security_inode_listsecurity(inode, list, list_len);
Chengguang Xu82c596e2020-06-17 09:09:39 +08007543 if (len >= 0 && list_len && len > list_len)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007544 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04007545 }
7546 return len;
7547}
7548
7549static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
7550 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04007551 .get = nfs4_xattr_get_nfs4_label,
7552 .set = nfs4_xattr_set_nfs4_label,
7553};
David Quigleyc9bccef2013-05-22 12:50:45 -04007554
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007555#else
7556
7557static ssize_t
7558nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7559{
7560 return 0;
7561}
7562
7563#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04007564
Frank van der Linden012a2112020-06-23 22:39:03 +00007565#ifdef CONFIG_NFS_V4_2
7566static int nfs4_xattr_set_nfs4_user(const struct xattr_handler *handler,
Christian Braunere65ce2a2021-01-21 14:19:27 +01007567 struct user_namespace *mnt_userns,
Frank van der Linden012a2112020-06-23 22:39:03 +00007568 struct dentry *unused, struct inode *inode,
7569 const char *key, const void *buf,
7570 size_t buflen, int flags)
7571{
7572 struct nfs_access_entry cache;
Frank van der Linden95ad37f2020-06-23 22:39:04 +00007573 int ret;
Frank van der Linden012a2112020-06-23 22:39:03 +00007574
7575 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7576 return -EOPNOTSUPP;
7577
7578 /*
7579 * There is no mapping from the MAY_* flags to the NFS_ACCESS_XA*
7580 * flags right now. Handling of xattr operations use the normal
7581 * file read/write permissions.
7582 *
7583 * Just in case the server has other ideas (which RFC 8276 allows),
7584 * do a cached access check for the XA* flags to possibly avoid
7585 * doing an RPC and getting EACCES back.
7586 */
7587 if (!nfs_access_get_cached(inode, current_cred(), &cache, true)) {
7588 if (!(cache.mask & NFS_ACCESS_XAWRITE))
7589 return -EACCES;
7590 }
7591
Frank van der Linden95ad37f2020-06-23 22:39:04 +00007592 if (buf == NULL) {
7593 ret = nfs42_proc_removexattr(inode, key);
7594 if (!ret)
7595 nfs4_xattr_cache_remove(inode, key);
7596 } else {
7597 ret = nfs42_proc_setxattr(inode, key, buf, buflen, flags);
7598 if (!ret)
7599 nfs4_xattr_cache_add(inode, key, buf, NULL, buflen);
7600 }
7601
7602 return ret;
Frank van der Linden012a2112020-06-23 22:39:03 +00007603}
7604
7605static int nfs4_xattr_get_nfs4_user(const struct xattr_handler *handler,
7606 struct dentry *unused, struct inode *inode,
7607 const char *key, void *buf, size_t buflen)
7608{
7609 struct nfs_access_entry cache;
Frank van der Linden95ad37f2020-06-23 22:39:04 +00007610 ssize_t ret;
Frank van der Linden012a2112020-06-23 22:39:03 +00007611
7612 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7613 return -EOPNOTSUPP;
7614
7615 if (!nfs_access_get_cached(inode, current_cred(), &cache, true)) {
7616 if (!(cache.mask & NFS_ACCESS_XAREAD))
7617 return -EACCES;
7618 }
7619
Trond Myklebust1f3208b2021-03-25 11:04:34 -04007620 ret = nfs_revalidate_inode(inode, NFS_INO_INVALID_CHANGE);
Frank van der Linden95ad37f2020-06-23 22:39:04 +00007621 if (ret)
7622 return ret;
7623
7624 ret = nfs4_xattr_cache_get(inode, key, buf, buflen);
7625 if (ret >= 0 || (ret < 0 && ret != -ENOENT))
7626 return ret;
7627
7628 ret = nfs42_proc_getxattr(inode, key, buf, buflen);
7629
7630 return ret;
Frank van der Linden012a2112020-06-23 22:39:03 +00007631}
7632
7633static ssize_t
7634nfs4_listxattr_nfs4_user(struct inode *inode, char *list, size_t list_len)
7635{
7636 u64 cookie;
7637 bool eof;
Frank van der Linden95ad37f2020-06-23 22:39:04 +00007638 ssize_t ret, size;
Frank van der Linden012a2112020-06-23 22:39:03 +00007639 char *buf;
7640 size_t buflen;
7641 struct nfs_access_entry cache;
7642
7643 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7644 return 0;
7645
7646 if (!nfs_access_get_cached(inode, current_cred(), &cache, true)) {
7647 if (!(cache.mask & NFS_ACCESS_XALIST))
7648 return 0;
7649 }
7650
Trond Myklebust1f3208b2021-03-25 11:04:34 -04007651 ret = nfs_revalidate_inode(inode, NFS_INO_INVALID_CHANGE);
Frank van der Linden95ad37f2020-06-23 22:39:04 +00007652 if (ret)
7653 return ret;
7654
7655 ret = nfs4_xattr_cache_list(inode, list, list_len);
7656 if (ret >= 0 || (ret < 0 && ret != -ENOENT))
7657 return ret;
7658
Frank van der Linden012a2112020-06-23 22:39:03 +00007659 cookie = 0;
7660 eof = false;
7661 buflen = list_len ? list_len : XATTR_LIST_MAX;
7662 buf = list_len ? list : NULL;
7663 size = 0;
7664
7665 while (!eof) {
7666 ret = nfs42_proc_listxattrs(inode, buf, buflen,
7667 &cookie, &eof);
7668 if (ret < 0)
7669 return ret;
7670
7671 if (list_len) {
7672 buf += ret;
7673 buflen -= ret;
7674 }
7675 size += ret;
7676 }
7677
Frank van der Linden95ad37f2020-06-23 22:39:04 +00007678 if (list_len)
7679 nfs4_xattr_cache_set_list(inode, list, size);
7680
Frank van der Linden012a2112020-06-23 22:39:03 +00007681 return size;
7682}
7683
7684#else
7685
7686static ssize_t
7687nfs4_listxattr_nfs4_user(struct inode *inode, char *list, size_t list_len)
7688{
7689 return 0;
7690}
7691#endif /* CONFIG_NFS_V4_2 */
7692
Andy Adamson533eb462011-06-13 18:25:56 -04007693/*
7694 * nfs_fhget will use either the mounted_on_fileid or the fileid
7695 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007696static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
7697{
Andy Adamson533eb462011-06-13 18:25:56 -04007698 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
7699 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
7700 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05007701 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007702 return;
7703
7704 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05007705 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007706 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
7707 fattr->nlink = 2;
7708}
7709
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007710static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7711 const struct qstr *name,
7712 struct nfs4_fs_locations *fs_locations,
7713 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04007714{
7715 struct nfs_server *server = NFS_SERVER(dir);
Chuck Leverc05cefc2017-11-05 15:45:22 -05007716 u32 bitmask[3];
Trond Myklebust683b57b2006-06-09 09:34:22 -04007717 struct nfs4_fs_locations_arg args = {
7718 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05007719 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04007720 .page = page,
7721 .bitmask = bitmask,
7722 };
Benny Halevy22958462009-04-01 09:22:02 -04007723 struct nfs4_fs_locations_res res = {
7724 .fs_locations = fs_locations,
7725 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04007726 struct rpc_message msg = {
7727 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7728 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04007729 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04007730 };
7731 int status;
7732
Harvey Harrison3110ff82008-05-02 13:42:44 -07007733 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04007734
Chuck Leverc05cefc2017-11-05 15:45:22 -05007735 bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
7736 bitmask[1] = nfs4_fattr_bitmap[1];
7737
Andy Adamson533eb462011-06-13 18:25:56 -04007738 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
7739 * is not supported */
7740 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
Chuck Leverc05cefc2017-11-05 15:45:22 -05007741 bitmask[0] &= ~FATTR4_WORD0_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04007742 else
Chuck Leverc05cefc2017-11-05 15:45:22 -05007743 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04007744
Trond Myklebustc228fd32007-01-13 02:28:11 -05007745 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04007746 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04007747 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007748 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07007749 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04007750 return status;
7751}
7752
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007753int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7754 const struct qstr *name,
7755 struct nfs4_fs_locations *fs_locations,
7756 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007757{
Trond Myklebust0688e642019-04-07 13:59:09 -04007758 struct nfs4_exception exception = {
7759 .interruptible = true,
7760 };
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007761 int err;
7762 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007763 err = _nfs4_proc_fs_locations(client, dir, name,
7764 fs_locations, page);
7765 trace_nfs4_get_fs_locations(dir, name, err);
7766 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007767 &exception);
7768 } while (exception.retry);
7769 return err;
7770}
7771
Chuck Leverb03d7352013-10-17 14:12:50 -04007772/*
7773 * This operation also signals the server that this client is
7774 * performing migration recovery. The server can stop returning
7775 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
7776 * appended to this compound to identify the client ID which is
7777 * performing recovery.
7778 */
7779static int _nfs40_proc_get_locations(struct inode *inode,
7780 struct nfs4_fs_locations *locations,
NeilBrowna52458b2018-12-03 11:30:31 +11007781 struct page *page, const struct cred *cred)
Chuck Leverb03d7352013-10-17 14:12:50 -04007782{
7783 struct nfs_server *server = NFS_SERVER(inode);
7784 struct rpc_clnt *clnt = server->client;
7785 u32 bitmask[2] = {
7786 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7787 };
7788 struct nfs4_fs_locations_arg args = {
7789 .clientid = server->nfs_client->cl_clientid,
7790 .fh = NFS_FH(inode),
7791 .page = page,
7792 .bitmask = bitmask,
7793 .migration = 1, /* skip LOOKUP */
7794 .renew = 1, /* append RENEW */
7795 };
7796 struct nfs4_fs_locations_res res = {
7797 .fs_locations = locations,
7798 .migration = 1,
7799 .renew = 1,
7800 };
7801 struct rpc_message msg = {
7802 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7803 .rpc_argp = &args,
7804 .rpc_resp = &res,
7805 .rpc_cred = cred,
7806 };
7807 unsigned long now = jiffies;
7808 int status;
7809
7810 nfs_fattr_init(&locations->fattr);
7811 locations->server = server;
7812 locations->nlocations = 0;
7813
Anna Schumakerfba83f32018-05-04 16:22:50 -04007814 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Leverb03d7352013-10-17 14:12:50 -04007815 status = nfs4_call_sync_sequence(clnt, server, &msg,
7816 &args.seq_args, &res.seq_res);
7817 if (status)
7818 return status;
7819
7820 renew_lease(server, now);
7821 return 0;
7822}
7823
7824#ifdef CONFIG_NFS_V4_1
7825
7826/*
7827 * This operation also signals the server that this client is
7828 * performing migration recovery. The server can stop asserting
7829 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
7830 * performing this operation is identified in the SEQUENCE
7831 * operation in this compound.
7832 *
7833 * When the client supports GETATTR(fs_locations_info), it can
7834 * be plumbed in here.
7835 */
7836static int _nfs41_proc_get_locations(struct inode *inode,
7837 struct nfs4_fs_locations *locations,
NeilBrowna52458b2018-12-03 11:30:31 +11007838 struct page *page, const struct cred *cred)
Chuck Leverb03d7352013-10-17 14:12:50 -04007839{
7840 struct nfs_server *server = NFS_SERVER(inode);
7841 struct rpc_clnt *clnt = server->client;
7842 u32 bitmask[2] = {
7843 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7844 };
7845 struct nfs4_fs_locations_arg args = {
7846 .fh = NFS_FH(inode),
7847 .page = page,
7848 .bitmask = bitmask,
7849 .migration = 1, /* skip LOOKUP */
7850 };
7851 struct nfs4_fs_locations_res res = {
7852 .fs_locations = locations,
7853 .migration = 1,
7854 };
7855 struct rpc_message msg = {
7856 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7857 .rpc_argp = &args,
7858 .rpc_resp = &res,
7859 .rpc_cred = cred,
7860 };
7861 int status;
7862
7863 nfs_fattr_init(&locations->fattr);
7864 locations->server = server;
7865 locations->nlocations = 0;
7866
Anna Schumakerfba83f32018-05-04 16:22:50 -04007867 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Leverb03d7352013-10-17 14:12:50 -04007868 status = nfs4_call_sync_sequence(clnt, server, &msg,
7869 &args.seq_args, &res.seq_res);
7870 if (status == NFS4_OK &&
7871 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7872 status = -NFS4ERR_LEASE_MOVED;
7873 return status;
7874}
7875
7876#endif /* CONFIG_NFS_V4_1 */
7877
7878/**
7879 * nfs4_proc_get_locations - discover locations for a migrated FSID
7880 * @inode: inode on FSID that is migrating
7881 * @locations: result of query
7882 * @page: buffer
7883 * @cred: credential to use for this operation
7884 *
7885 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
7886 * operation failed, or a negative errno if a local error occurred.
7887 *
7888 * On success, "locations" is filled in, but if the server has
7889 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
7890 * asserted.
7891 *
7892 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
7893 * from this client that require migration recovery.
7894 */
7895int nfs4_proc_get_locations(struct inode *inode,
7896 struct nfs4_fs_locations *locations,
NeilBrowna52458b2018-12-03 11:30:31 +11007897 struct page *page, const struct cred *cred)
Chuck Leverb03d7352013-10-17 14:12:50 -04007898{
7899 struct nfs_server *server = NFS_SERVER(inode);
7900 struct nfs_client *clp = server->nfs_client;
7901 const struct nfs4_mig_recovery_ops *ops =
7902 clp->cl_mvops->mig_recovery_ops;
Trond Myklebust0688e642019-04-07 13:59:09 -04007903 struct nfs4_exception exception = {
7904 .interruptible = true,
7905 };
Chuck Leverb03d7352013-10-17 14:12:50 -04007906 int status;
7907
7908 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7909 (unsigned long long)server->fsid.major,
7910 (unsigned long long)server->fsid.minor,
7911 clp->cl_hostname);
7912 nfs_display_fhandle(NFS_FH(inode), __func__);
7913
7914 do {
7915 status = ops->get_locations(inode, locations, page, cred);
7916 if (status != -NFS4ERR_DELAY)
7917 break;
7918 nfs4_handle_exception(server, status, &exception);
7919 } while (exception.retry);
7920 return status;
7921}
7922
Chuck Lever44c99932013-10-17 14:13:30 -04007923/*
7924 * This operation also signals the server that this client is
7925 * performing "lease moved" recovery. The server can stop
7926 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
7927 * is appended to this compound to identify the client ID which is
7928 * performing recovery.
7929 */
NeilBrowna52458b2018-12-03 11:30:31 +11007930static int _nfs40_proc_fsid_present(struct inode *inode, const struct cred *cred)
Chuck Lever44c99932013-10-17 14:13:30 -04007931{
7932 struct nfs_server *server = NFS_SERVER(inode);
7933 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
7934 struct rpc_clnt *clnt = server->client;
7935 struct nfs4_fsid_present_arg args = {
7936 .fh = NFS_FH(inode),
7937 .clientid = clp->cl_clientid,
7938 .renew = 1, /* append RENEW */
7939 };
7940 struct nfs4_fsid_present_res res = {
7941 .renew = 1,
7942 };
7943 struct rpc_message msg = {
7944 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7945 .rpc_argp = &args,
7946 .rpc_resp = &res,
7947 .rpc_cred = cred,
7948 };
7949 unsigned long now = jiffies;
7950 int status;
7951
7952 res.fh = nfs_alloc_fhandle();
7953 if (res.fh == NULL)
7954 return -ENOMEM;
7955
Anna Schumakerfba83f32018-05-04 16:22:50 -04007956 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Lever44c99932013-10-17 14:13:30 -04007957 status = nfs4_call_sync_sequence(clnt, server, &msg,
7958 &args.seq_args, &res.seq_res);
7959 nfs_free_fhandle(res.fh);
7960 if (status)
7961 return status;
7962
7963 do_renew_lease(clp, now);
7964 return 0;
7965}
7966
7967#ifdef CONFIG_NFS_V4_1
7968
7969/*
7970 * This operation also signals the server that this client is
7971 * performing "lease moved" recovery. The server can stop asserting
7972 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
7973 * this operation is identified in the SEQUENCE operation in this
7974 * compound.
7975 */
NeilBrowna52458b2018-12-03 11:30:31 +11007976static int _nfs41_proc_fsid_present(struct inode *inode, const struct cred *cred)
Chuck Lever44c99932013-10-17 14:13:30 -04007977{
7978 struct nfs_server *server = NFS_SERVER(inode);
7979 struct rpc_clnt *clnt = server->client;
7980 struct nfs4_fsid_present_arg args = {
7981 .fh = NFS_FH(inode),
7982 };
7983 struct nfs4_fsid_present_res res = {
7984 };
7985 struct rpc_message msg = {
7986 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7987 .rpc_argp = &args,
7988 .rpc_resp = &res,
7989 .rpc_cred = cred,
7990 };
7991 int status;
7992
7993 res.fh = nfs_alloc_fhandle();
7994 if (res.fh == NULL)
7995 return -ENOMEM;
7996
Anna Schumakerfba83f32018-05-04 16:22:50 -04007997 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Lever44c99932013-10-17 14:13:30 -04007998 status = nfs4_call_sync_sequence(clnt, server, &msg,
7999 &args.seq_args, &res.seq_res);
8000 nfs_free_fhandle(res.fh);
8001 if (status == NFS4_OK &&
8002 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
8003 status = -NFS4ERR_LEASE_MOVED;
8004 return status;
8005}
8006
8007#endif /* CONFIG_NFS_V4_1 */
8008
8009/**
8010 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
8011 * @inode: inode on FSID to check
8012 * @cred: credential to use for this operation
8013 *
8014 * Server indicates whether the FSID is present, moved, or not
8015 * recognized. This operation is necessary to clear a LEASE_MOVED
8016 * condition for this client ID.
8017 *
8018 * Returns NFS4_OK if the FSID is present on this server,
8019 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
8020 * NFS4ERR code if some error occurred on the server, or a
8021 * negative errno if a local failure occurred.
8022 */
NeilBrowna52458b2018-12-03 11:30:31 +11008023int nfs4_proc_fsid_present(struct inode *inode, const struct cred *cred)
Chuck Lever44c99932013-10-17 14:13:30 -04008024{
8025 struct nfs_server *server = NFS_SERVER(inode);
8026 struct nfs_client *clp = server->nfs_client;
8027 const struct nfs4_mig_recovery_ops *ops =
8028 clp->cl_mvops->mig_recovery_ops;
Trond Myklebust0688e642019-04-07 13:59:09 -04008029 struct nfs4_exception exception = {
8030 .interruptible = true,
8031 };
Chuck Lever44c99932013-10-17 14:13:30 -04008032 int status;
8033
8034 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
8035 (unsigned long long)server->fsid.major,
8036 (unsigned long long)server->fsid.minor,
8037 clp->cl_hostname);
8038 nfs_display_fhandle(NFS_FH(inode), __func__);
8039
8040 do {
8041 status = ops->fsid_present(inode, cred);
8042 if (status != -NFS4ERR_DELAY)
8043 break;
8044 nfs4_handle_exception(server, status, &exception);
8045 } while (exception.retry);
8046 return status;
8047}
8048
Trond Myklebust302fad72019-02-18 13:32:38 -05008049/*
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04008050 * If 'use_integrity' is true and the state managment nfs_client
8051 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
8052 * and the machine credential as per RFC3530bis and RFC5661 Security
8053 * Considerations sections. Otherwise, just use the user cred with the
8054 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04008055 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04008056static 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 +00008057{
8058 int status;
Anna Schumaker50493362019-08-14 15:30:16 -04008059 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
8060 struct nfs_client *clp = NFS_SERVER(dir)->nfs_client;
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00008061 struct nfs4_secinfo_arg args = {
8062 .dir_fh = NFS_FH(dir),
8063 .name = name,
8064 };
8065 struct nfs4_secinfo_res res = {
8066 .flavors = flavors,
8067 };
8068 struct rpc_message msg = {
8069 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
8070 .rpc_argp = &args,
8071 .rpc_resp = &res,
8072 };
Anna Schumaker50493362019-08-14 15:30:16 -04008073 struct nfs4_call_sync_data data = {
8074 .seq_server = NFS_SERVER(dir),
8075 .seq_args = &args.seq_args,
8076 .seq_res = &res.seq_res,
8077 };
8078 struct rpc_task_setup task_setup = {
8079 .rpc_client = clnt,
8080 .rpc_message = &msg,
8081 .callback_ops = clp->cl_mvops->call_sync_ops,
8082 .callback_data = &data,
8083 .flags = RPC_TASK_NO_ROUND_ROBIN,
8084 };
NeilBrowna52458b2018-12-03 11:30:31 +11008085 const struct cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04008086
8087 if (use_integrity) {
Anna Schumaker50493362019-08-14 15:30:16 -04008088 clnt = clp->cl_rpcclient;
8089 task_setup.rpc_client = clnt;
8090
8091 cred = nfs4_get_clid_cred(clp);
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008092 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04008093 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00008094
8095 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008096
Anna Schumaker50493362019-08-14 15:30:16 -04008097 nfs4_state_protect(clp, NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
8098 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
8099 status = nfs4_call_sync_custom(&task_setup);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008100
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00008101 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04008102
NeilBrowna52458b2018-12-03 11:30:31 +11008103 put_cred(cred);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00008104 return status;
8105}
8106
Bryan Schumaker72de53e2012-04-27 13:27:40 -04008107int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
8108 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00008109{
Trond Myklebust0688e642019-04-07 13:59:09 -04008110 struct nfs4_exception exception = {
8111 .interruptible = true,
8112 };
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00008113 int err;
8114 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04008115 err = -NFS4ERR_WRONGSEC;
8116
8117 /* try to use integrity protection with machine cred */
8118 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
8119 err = _nfs4_proc_secinfo(dir, name, flavors, true);
8120
8121 /*
8122 * if unable to use integrity protection, or SECINFO with
8123 * integrity protection returns NFS4ERR_WRONGSEC (which is
8124 * disallowed by spec, but exists in deployed servers) use
8125 * the current filesystem's rpc_client and the user cred.
8126 */
8127 if (err == -NFS4ERR_WRONGSEC)
8128 err = _nfs4_proc_secinfo(dir, name, flavors, false);
8129
Trond Myklebust078ea3d2013-08-12 16:45:55 -04008130 trace_nfs4_secinfo(dir, name, err);
8131 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00008132 &exception);
8133 } while (exception.retry);
8134 return err;
8135}
8136
Andy Adamson557134a2009-04-01 09:21:53 -04008137#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04008138/*
Andy Adamson357f54d2010-12-14 10:11:57 -05008139 * Check the exchange flags returned by the server for invalid flags, having
8140 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
8141 * DS flags set.
8142 */
Olga Kornievskaia8c390762020-10-16 09:25:45 -04008143static int nfs4_check_cl_exchange_flags(u32 flags, u32 version)
Andy Adamson357f54d2010-12-14 10:11:57 -05008144{
Olga Kornievskaia8c390762020-10-16 09:25:45 -04008145 if (version >= 2 && (flags & ~EXCHGID4_2_FLAG_MASK_R))
8146 goto out_inval;
8147 else if (version < 2 && (flags & ~EXCHGID4_FLAG_MASK_R))
Andy Adamson357f54d2010-12-14 10:11:57 -05008148 goto out_inval;
8149 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
8150 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
8151 goto out_inval;
8152 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
8153 goto out_inval;
8154 return NFS_OK;
8155out_inval:
8156 return -NFS4ERR_INVAL;
8157}
8158
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04008159static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04008160nfs41_same_server_scope(struct nfs41_server_scope *a,
8161 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04008162{
Anna Schumaker49ad0142017-01-11 16:51:59 -05008163 if (a->server_scope_sz != b->server_scope_sz)
8164 return false;
8165 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04008166}
8167
Andy Adamson02a95de2016-02-05 16:08:37 -05008168static void
8169nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
8170{
Trond Myklebust5c441542019-11-13 08:34:00 +01008171 struct nfs41_bind_conn_to_session_args *args = task->tk_msg.rpc_argp;
Olga Kornievskaiadff58532020-04-24 17:45:50 -04008172 struct nfs41_bind_conn_to_session_res *res = task->tk_msg.rpc_resp;
Trond Myklebust5c441542019-11-13 08:34:00 +01008173 struct nfs_client *clp = args->client;
8174
8175 switch (task->tk_status) {
8176 case -NFS4ERR_BADSESSION:
8177 case -NFS4ERR_DEADSESSION:
8178 nfs4_schedule_session_recovery(clp->cl_session,
8179 task->tk_status);
8180 }
Olga Kornievskaiadff58532020-04-24 17:45:50 -04008181 if (args->dir == NFS4_CDFC4_FORE_OR_BOTH &&
8182 res->dir != NFS4_CDFS4_BOTH) {
8183 rpc_task_close_connection(task);
8184 if (args->retries++ < MAX_BIND_CONN_TO_SESSION_RETRIES)
8185 rpc_restart_call(task);
8186 }
Andy Adamson02a95de2016-02-05 16:08:37 -05008187}
8188
8189static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
Olga Kornievskaia1c709b72020-04-26 11:30:00 -04008190 .rpc_call_done = nfs4_bind_one_conn_to_session_done,
Andy Adamson02a95de2016-02-05 16:08:37 -05008191};
8192
Andy Adamson357f54d2010-12-14 10:11:57 -05008193/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008194 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008195 *
8196 * The 4.1 client currently uses the same TCP connection for the
8197 * fore and backchannel.
8198 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008199static
8200int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
8201 struct rpc_xprt *xprt,
8202 struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008203 const struct cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008204{
8205 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08008206 struct nfs41_bind_conn_to_session_args args = {
8207 .client = clp,
8208 .dir = NFS4_CDFC4_FORE_OR_BOTH,
Olga Kornievskaiadff58532020-04-24 17:45:50 -04008209 .retries = 0,
Trond Myklebust71a097c2015-02-18 09:27:18 -08008210 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008211 struct nfs41_bind_conn_to_session_res res;
8212 struct rpc_message msg = {
8213 .rpc_proc =
8214 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08008215 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008216 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04008217 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008218 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008219 struct rpc_task_setup task_setup_data = {
8220 .rpc_client = clnt,
8221 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05008222 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008223 .rpc_message = &msg,
8224 .flags = RPC_TASK_TIMEOUT,
8225 };
8226 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008227
Trond Myklebust71a097c2015-02-18 09:27:18 -08008228 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
8229 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
8230 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008231
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008232 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
8233 if (xprt != rcu_access_pointer(clnt->cl_xprt))
8234 args.dir = NFS4_CDFC4_FORE;
8235
8236 task = rpc_run_task(&task_setup_data);
8237 if (!IS_ERR(task)) {
8238 status = task->tk_status;
8239 rpc_put_task(task);
8240 } else
8241 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008242 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008243 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08008244 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008245 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
8246 dprintk("NFS: %s: Session ID mismatch\n", __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04008247 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008248 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08008249 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008250 dprintk("NFS: %s: Unexpected direction from server\n",
8251 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04008252 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008253 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08008254 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008255 dprintk("NFS: %s: Server returned RDMA mode = true\n",
8256 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04008257 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008258 }
8259 }
Anna Schumakerc7ae7632017-04-07 14:15:21 -04008260
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008261 return status;
8262}
8263
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008264struct rpc_bind_conn_calldata {
8265 struct nfs_client *clp;
NeilBrowna52458b2018-12-03 11:30:31 +11008266 const struct cred *cred;
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008267};
8268
8269static int
8270nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
8271 struct rpc_xprt *xprt,
8272 void *calldata)
8273{
8274 struct rpc_bind_conn_calldata *p = calldata;
8275
8276 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
8277}
8278
NeilBrowna52458b2018-12-03 11:30:31 +11008279int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, const struct cred *cred)
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008280{
8281 struct rpc_bind_conn_calldata data = {
8282 .clp = clp,
8283 .cred = cred,
8284 };
8285 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
8286 nfs4_proc_bind_conn_to_session_callback, &data);
8287}
8288
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008289/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04008290 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
8291 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04008292 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008293static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
8294 .how = SP4_MACH_CRED,
8295 .enforce.u.words = {
8296 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
8297 1 << (OP_EXCHANGE_ID - 32) |
8298 1 << (OP_CREATE_SESSION - 32) |
8299 1 << (OP_DESTROY_SESSION - 32) |
8300 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04008301 },
8302 .allow.u.words = {
8303 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05008304 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04008305 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05008306 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04008307 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008308 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008309 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05008310 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008311 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04008312 1 << (OP_FREE_STATEID - 32) |
8313 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008314 }
8315};
8316
8317/*
8318 * Select the state protection mode for client `clp' given the server results
8319 * from exchange_id in `sp'.
8320 *
8321 * Returns 0 on success, negative errno otherwise.
8322 */
8323static int nfs4_sp4_select_mode(struct nfs_client *clp,
8324 struct nfs41_state_protection *sp)
8325{
8326 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
8327 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
8328 1 << (OP_EXCHANGE_ID - 32) |
8329 1 << (OP_CREATE_SESSION - 32) |
8330 1 << (OP_DESTROY_SESSION - 32) |
8331 1 << (OP_DESTROY_CLIENTID - 32)
8332 };
Trond Myklebust937e3132017-08-01 07:32:50 -04008333 unsigned long flags = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008334 unsigned int i;
Trond Myklebust937e3132017-08-01 07:32:50 -04008335 int ret = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008336
8337 if (sp->how == SP4_MACH_CRED) {
8338 /* Print state protect result */
8339 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
8340 for (i = 0; i <= LAST_NFS4_OP; i++) {
8341 if (test_bit(i, sp->enforce.u.longs))
8342 dfprintk(MOUNT, " enforce op %d\n", i);
8343 if (test_bit(i, sp->allow.u.longs))
8344 dfprintk(MOUNT, " allow op %d\n", i);
8345 }
8346
8347 /* make sure nothing is on enforce list that isn't supported */
8348 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
8349 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
8350 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008351 ret = -EINVAL;
8352 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008353 }
8354 }
8355
8356 /*
8357 * Minimal mode - state operations are allowed to use machine
8358 * credential. Note this already happens by default, so the
8359 * client doesn't have to do anything more than the negotiation.
8360 *
8361 * NOTE: we don't care if EXCHANGE_ID is in the list -
8362 * we're already using the machine cred for exchange_id
8363 * and will never use a different cred.
8364 */
8365 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
8366 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
8367 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
8368 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
8369 dfprintk(MOUNT, "sp4_mach_cred:\n");
8370 dfprintk(MOUNT, " minimal mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008371 __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008372 } else {
8373 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008374 ret = -EINVAL;
8375 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008376 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04008377
8378 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05008379 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
8380 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04008381 test_bit(OP_LOCKU, sp->allow.u.longs)) {
8382 dfprintk(MOUNT, " cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008383 __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04008384 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008385
Andrew Elble99ade3c2015-12-02 09:39:51 -05008386 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
8387 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008388 __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
Andrew Elble99ade3c2015-12-02 09:39:51 -05008389 }
8390
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008391 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
8392 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
8393 dfprintk(MOUNT, " secinfo mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008394 __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008395 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008396
8397 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
8398 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
8399 dfprintk(MOUNT, " stateid mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008400 __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008401 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04008402
8403 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
8404 dfprintk(MOUNT, " write mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008405 __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04008406 }
8407
8408 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
8409 dfprintk(MOUNT, " commit mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008410 __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04008411 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008412 }
Trond Myklebust937e3132017-08-01 07:32:50 -04008413out:
8414 clp->cl_sp4_flags = flags;
Wei Yongjun72bf75c2018-08-02 05:42:04 +00008415 return ret;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008416}
8417
Andy Adamson8d89bd72016-09-09 09:22:18 -04008418struct nfs41_exchange_id_data {
8419 struct nfs41_exchange_id_res res;
8420 struct nfs41_exchange_id_args args;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008421};
8422
Andy Adamson8d89bd72016-09-09 09:22:18 -04008423static void nfs4_exchange_id_release(void *data)
8424{
8425 struct nfs41_exchange_id_data *cdata =
8426 (struct nfs41_exchange_id_data *)data;
8427
Olga Kornievskaia63513232017-03-13 10:36:19 -04008428 nfs_put_client(cdata->args.client);
Andy Adamson8d89bd72016-09-09 09:22:18 -04008429 kfree(cdata->res.impl_id);
8430 kfree(cdata->res.server_scope);
8431 kfree(cdata->res.server_owner);
8432 kfree(cdata);
8433}
8434
8435static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
Andy Adamson8d89bd72016-09-09 09:22:18 -04008436 .rpc_release = nfs4_exchange_id_release,
8437};
8438
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008439/*
8440 * _nfs4_proc_exchange_id()
8441 *
8442 * Wrapper for EXCHANGE_ID operation.
8443 */
Trond Myklebust9c760d12017-07-31 18:38:50 -04008444static struct rpc_task *
NeilBrowna52458b2018-12-03 11:30:31 +11008445nfs4_run_exchange_id(struct nfs_client *clp, const struct cred *cred,
Andy Adamsonad0849a2016-09-09 09:22:28 -04008446 u32 sp4_how, struct rpc_xprt *xprt)
Benny Halevy99fe60d2009-04-01 09:22:29 -04008447{
Benny Halevy99fe60d2009-04-01 09:22:29 -04008448 struct rpc_message msg = {
8449 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
Benny Halevy99fe60d2009-04-01 09:22:29 -04008450 .rpc_cred = cred,
8451 };
Andy Adamson8d89bd72016-09-09 09:22:18 -04008452 struct rpc_task_setup task_setup_data = {
8453 .rpc_client = clp->cl_rpcclient,
8454 .callback_ops = &nfs4_exchange_id_call_ops,
8455 .rpc_message = &msg,
NeilBrown5a0c2572019-05-30 10:41:28 +10008456 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
Andy Adamson8d89bd72016-09-09 09:22:18 -04008457 };
8458 struct nfs41_exchange_id_data *calldata;
Anna Schumakere917f0d2017-04-07 14:15:22 -04008459 int status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008460
Elena Reshetova212bf412017-10-20 12:53:38 +03008461 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust9c760d12017-07-31 18:38:50 -04008462 return ERR_PTR(-EIO);
Andy Adamson8d89bd72016-09-09 09:22:18 -04008463
Trond Myklebust9c760d12017-07-31 18:38:50 -04008464 status = -ENOMEM;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008465 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust9c760d12017-07-31 18:38:50 -04008466 if (!calldata)
8467 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04008468
Trond Myklebustfd405592017-08-01 16:02:47 -04008469 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04008470
8471 status = nfs4_init_uniform_client_string(clp);
8472 if (status)
Andy Adamson8d89bd72016-09-09 09:22:18 -04008473 goto out_calldata;
Jeff Layton3a6bb732015-06-09 19:43:57 -04008474
Andy Adamson8d89bd72016-09-09 09:22:18 -04008475 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
8476 GFP_NOFS);
8477 status = -ENOMEM;
8478 if (unlikely(calldata->res.server_owner == NULL))
8479 goto out_calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04008480
Andy Adamson8d89bd72016-09-09 09:22:18 -04008481 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04008482 GFP_NOFS);
Andy Adamson8d89bd72016-09-09 09:22:18 -04008483 if (unlikely(calldata->res.server_scope == NULL))
Chuck Leveracdeb692012-05-21 22:46:16 -04008484 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04008485
Andy Adamson8d89bd72016-09-09 09:22:18 -04008486 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
8487 if (unlikely(calldata->res.impl_id == NULL))
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05008488 goto out_server_scope;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05008489
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008490 switch (sp4_how) {
8491 case SP4_NONE:
Andy Adamson8d89bd72016-09-09 09:22:18 -04008492 calldata->args.state_protect.how = SP4_NONE;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008493 break;
8494
8495 case SP4_MACH_CRED:
Andy Adamson8d89bd72016-09-09 09:22:18 -04008496 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008497 break;
8498
8499 default:
8500 /* unsupported! */
8501 WARN_ON_ONCE(1);
8502 status = -EINVAL;
Kinglong Mee6b559702015-07-01 11:54:53 +08008503 goto out_impl_id;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008504 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04008505 if (xprt) {
Andy Adamsonad0849a2016-09-09 09:22:28 -04008506 task_setup_data.rpc_xprt = xprt;
Trond Myklebustd9cb7332017-08-01 16:02:48 -04008507 task_setup_data.flags |= RPC_TASK_SOFTCONN;
Trond Myklebustfd405592017-08-01 16:02:47 -04008508 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
8509 sizeof(calldata->args.verifier.data));
Andy Adamsonad0849a2016-09-09 09:22:28 -04008510 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04008511 calldata->args.client = clp;
Trond Myklebustbfab2812017-08-01 08:17:34 -04008512 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
8513 EXCHGID4_FLAG_BIND_PRINC_STATEID;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008514#ifdef CONFIG_NFS_V4_1_MIGRATION
Trond Myklebustbfab2812017-08-01 08:17:34 -04008515 calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008516#endif
8517 msg.rpc_argp = &calldata->args;
8518 msg.rpc_resp = &calldata->res;
8519 task_setup_data.callback_data = calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04008520
Trond Myklebust9c760d12017-07-31 18:38:50 -04008521 return rpc_run_task(&task_setup_data);
Andy Adamson8d89bd72016-09-09 09:22:18 -04008522
8523out_impl_id:
8524 kfree(calldata->res.impl_id);
8525out_server_scope:
8526 kfree(calldata->res.server_scope);
8527out_server_owner:
8528 kfree(calldata->res.server_owner);
8529out_calldata:
8530 kfree(calldata);
Trond Myklebust9c760d12017-07-31 18:38:50 -04008531out:
Olga Kornievskaia63513232017-03-13 10:36:19 -04008532 nfs_put_client(clp);
Trond Myklebust9c760d12017-07-31 18:38:50 -04008533 return ERR_PTR(status);
8534}
8535
8536/*
8537 * _nfs4_proc_exchange_id()
8538 *
8539 * Wrapper for EXCHANGE_ID operation.
8540 */
NeilBrowna52458b2018-12-03 11:30:31 +11008541static int _nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred,
Trond Myklebust9c760d12017-07-31 18:38:50 -04008542 u32 sp4_how)
8543{
8544 struct rpc_task *task;
8545 struct nfs41_exchange_id_args *argp;
8546 struct nfs41_exchange_id_res *resp;
Robert Milkowski7dc29932020-01-30 09:43:25 +00008547 unsigned long now = jiffies;
Trond Myklebust9c760d12017-07-31 18:38:50 -04008548 int status;
8549
8550 task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
8551 if (IS_ERR(task))
8552 return PTR_ERR(task);
8553
8554 argp = task->tk_msg.rpc_argp;
8555 resp = task->tk_msg.rpc_resp;
8556 status = task->tk_status;
8557 if (status != 0)
8558 goto out;
8559
Olga Kornievskaia8c390762020-10-16 09:25:45 -04008560 status = nfs4_check_cl_exchange_flags(resp->flags,
8561 clp->cl_mvops->minor_version);
Trond Myklebust9c760d12017-07-31 18:38:50 -04008562 if (status != 0)
8563 goto out;
8564
8565 status = nfs4_sp4_select_mode(clp, &resp->state_protect);
8566 if (status != 0)
8567 goto out;
8568
Robert Milkowski7dc29932020-01-30 09:43:25 +00008569 do_renew_lease(clp, now);
8570
Trond Myklebust9c760d12017-07-31 18:38:50 -04008571 clp->cl_clientid = resp->clientid;
8572 clp->cl_exchange_flags = resp->flags;
8573 clp->cl_seqid = resp->seqid;
8574 /* Client ID is not confirmed */
8575 if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
8576 clear_bit(NFS4_SESSION_ESTABLISHED,
8577 &clp->cl_session->session_state);
8578
8579 if (clp->cl_serverscope != NULL &&
8580 !nfs41_same_server_scope(clp->cl_serverscope,
8581 resp->server_scope)) {
8582 dprintk("%s: server_scope mismatch detected\n",
8583 __func__);
8584 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
8585 }
8586
8587 swap(clp->cl_serverowner, resp->server_owner);
8588 swap(clp->cl_serverscope, resp->server_scope);
8589 swap(clp->cl_implid, resp->impl_id);
8590
8591 /* Save the EXCHANGE_ID verifier session trunk tests */
8592 memcpy(clp->cl_confirm.data, argp->verifier.data,
8593 sizeof(clp->cl_confirm.data));
8594out:
8595 trace_nfs4_exchange_id(clp, status);
8596 rpc_put_task(task);
8597 return status;
Benny Halevy99fe60d2009-04-01 09:22:29 -04008598}
8599
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008600/*
8601 * nfs4_proc_exchange_id()
8602 *
8603 * Returns zero, a negative errno, or a negative NFS4ERR status code.
8604 *
8605 * Since the clientid has expired, all compounds using sessions
8606 * associated with the stale clientid will be returning
8607 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
8608 * be in some phase of session reset.
8609 *
8610 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
8611 */
NeilBrowna52458b2018-12-03 11:30:31 +11008612int nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008613{
8614 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
8615 int status;
8616
8617 /* try SP4_MACH_CRED if krb5i/p */
8618 if (authflavor == RPC_AUTH_GSS_KRB5I ||
8619 authflavor == RPC_AUTH_GSS_KRB5P) {
Trond Myklebust9c760d12017-07-31 18:38:50 -04008620 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008621 if (!status)
8622 return 0;
8623 }
8624
8625 /* try SP4_NONE */
Trond Myklebust9c760d12017-07-31 18:38:50 -04008626 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008627}
8628
Andy Adamson04fa2c62016-09-09 09:22:29 -04008629/**
8630 * nfs4_test_session_trunk
8631 *
8632 * This is an add_xprt_test() test function called from
8633 * rpc_clnt_setup_test_and_add_xprt.
8634 *
8635 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
8636 * and is dereferrenced in nfs4_exchange_id_release
8637 *
8638 * Upon success, add the new transport to the rpc_clnt
8639 *
8640 * @clnt: struct rpc_clnt to get new transport
8641 * @xprt: the rpc_xprt to test
8642 * @data: call data for _nfs4_proc_exchange_id.
8643 */
Santosh kumar pradhan10e037d2018-12-19 12:29:57 +05308644void nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
Andy Adamson04fa2c62016-09-09 09:22:29 -04008645 void *data)
8646{
8647 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
Trond Myklebust9c760d12017-07-31 18:38:50 -04008648 struct rpc_task *task;
8649 int status;
8650
Andy Adamson04fa2c62016-09-09 09:22:29 -04008651 u32 sp4_how;
8652
8653 dprintk("--> %s try %s\n", __func__,
8654 xprt->address_strings[RPC_DISPLAY_ADDR]);
8655
8656 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
8657
8658 /* Test connection for session trunking. Async exchange_id call */
Trond Myklebust9c760d12017-07-31 18:38:50 -04008659 task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
8660 if (IS_ERR(task))
Santosh kumar pradhan10e037d2018-12-19 12:29:57 +05308661 return;
Trond Myklebust9c760d12017-07-31 18:38:50 -04008662
8663 status = task->tk_status;
8664 if (status == 0)
8665 status = nfs4_detect_session_trunking(adata->clp,
8666 task->tk_msg.rpc_resp, xprt);
8667
Santosh kumar pradhan10e037d2018-12-19 12:29:57 +05308668 if (status == 0)
8669 rpc_clnt_xprt_switch_add_xprt(clnt, xprt);
8670
Trond Myklebust9c760d12017-07-31 18:38:50 -04008671 rpc_put_task(task);
Andy Adamson04fa2c62016-09-09 09:22:29 -04008672}
8673EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
8674
Trond Myklebust66245532012-05-25 17:18:09 -04008675static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008676 const struct cred *cred)
Trond Myklebust66245532012-05-25 17:18:09 -04008677{
8678 struct rpc_message msg = {
8679 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
8680 .rpc_argp = clp,
8681 .rpc_cred = cred,
8682 };
8683 int status;
8684
NeilBrown5a0c2572019-05-30 10:41:28 +10008685 status = rpc_call_sync(clp->cl_rpcclient, &msg,
8686 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008687 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04008688 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04008689 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04008690 "DESTROY_CLIENTID.", status, clp->cl_hostname);
8691 return status;
8692}
8693
8694static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008695 const struct cred *cred)
Trond Myklebust66245532012-05-25 17:18:09 -04008696{
8697 unsigned int loop;
8698 int ret;
8699
8700 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
8701 ret = _nfs4_proc_destroy_clientid(clp, cred);
8702 switch (ret) {
8703 case -NFS4ERR_DELAY:
8704 case -NFS4ERR_CLIENTID_BUSY:
8705 ssleep(1);
8706 break;
8707 default:
8708 return ret;
8709 }
8710 }
8711 return 0;
8712}
8713
8714int nfs4_destroy_clientid(struct nfs_client *clp)
8715{
NeilBrowna52458b2018-12-03 11:30:31 +11008716 const struct cred *cred;
Trond Myklebust66245532012-05-25 17:18:09 -04008717 int ret = 0;
8718
8719 if (clp->cl_mvops->minor_version < 1)
8720 goto out;
8721 if (clp->cl_exchange_flags == 0)
8722 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04008723 if (clp->cl_preserve_clid)
8724 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04008725 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04008726 ret = nfs4_proc_destroy_clientid(clp, cred);
NeilBrowna52458b2018-12-03 11:30:31 +11008727 put_cred(cred);
Trond Myklebust66245532012-05-25 17:18:09 -04008728 switch (ret) {
8729 case 0:
8730 case -NFS4ERR_STALE_CLIENTID:
8731 clp->cl_exchange_flags = 0;
8732 }
8733out:
8734 return ret;
8735}
8736
Donald Buczek0efb01b2019-07-07 21:26:08 +02008737#endif /* CONFIG_NFS_V4_1 */
8738
Andy Adamson2050f0c2009-04-01 09:22:30 -04008739struct nfs4_get_lease_time_data {
8740 struct nfs4_get_lease_time_args *args;
8741 struct nfs4_get_lease_time_res *res;
8742 struct nfs_client *clp;
8743};
8744
8745static void nfs4_get_lease_time_prepare(struct rpc_task *task,
8746 void *calldata)
8747{
Andy Adamson2050f0c2009-04-01 09:22:30 -04008748 struct nfs4_get_lease_time_data *data =
8749 (struct nfs4_get_lease_time_data *)calldata;
8750
8751 dprintk("--> %s\n", __func__);
8752 /* just setup sequence, do not trigger session recovery
8753 since we're invoked within one */
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008754 nfs4_setup_sequence(data->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008755 &data->args->la_seq_args,
8756 &data->res->lr_seq_res,
8757 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008758 dprintk("<-- %s\n", __func__);
8759}
8760
8761/*
8762 * Called from nfs4_state_manager thread for session setup, so don't recover
8763 * from sequence operation or clientid errors.
8764 */
8765static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
8766{
8767 struct nfs4_get_lease_time_data *data =
8768 (struct nfs4_get_lease_time_data *)calldata;
8769
8770 dprintk("--> %s\n", __func__);
Donald Buczek0efb01b2019-07-07 21:26:08 +02008771 if (!nfs4_sequence_done(task, &data->res->lr_seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04008772 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04008773 switch (task->tk_status) {
8774 case -NFS4ERR_DELAY:
8775 case -NFS4ERR_GRACE:
8776 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
8777 rpc_delay(task, NFS4_POLL_RETRY_MIN);
8778 task->tk_status = 0;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05008779 fallthrough;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008780 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008781 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008782 return;
8783 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04008784 dprintk("<-- %s\n", __func__);
8785}
8786
Trond Myklebust17280172012-03-11 13:11:00 -04008787static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04008788 .rpc_call_prepare = nfs4_get_lease_time_prepare,
8789 .rpc_call_done = nfs4_get_lease_time_done,
8790};
8791
8792int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
8793{
Andy Adamson2050f0c2009-04-01 09:22:30 -04008794 struct nfs4_get_lease_time_args args;
8795 struct nfs4_get_lease_time_res res = {
8796 .lr_fsinfo = fsinfo,
8797 };
8798 struct nfs4_get_lease_time_data data = {
8799 .args = &args,
8800 .res = &res,
8801 .clp = clp,
8802 };
8803 struct rpc_message msg = {
8804 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
8805 .rpc_argp = &args,
8806 .rpc_resp = &res,
8807 };
8808 struct rpc_task_setup task_setup = {
8809 .rpc_client = clp->cl_rpcclient,
8810 .rpc_message = &msg,
8811 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008812 .callback_data = &data,
8813 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04008814 };
Andy Adamson2050f0c2009-04-01 09:22:30 -04008815
Anna Schumakerfba83f32018-05-04 16:22:50 -04008816 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0, 1);
Anna Schumakerf836b272019-08-19 10:18:28 -04008817 return nfs4_call_sync_custom(&task_setup);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008818}
8819
Donald Buczek0efb01b2019-07-07 21:26:08 +02008820#ifdef CONFIG_NFS_V4_1
8821
Andy Adamsonfc931582009-04-01 09:22:31 -04008822/*
8823 * Initialize the values to be used by the client in CREATE_SESSION
8824 * If nfs4_init_session set the fore channel request and response sizes,
8825 * use them.
8826 *
8827 * Set the back channel max_resp_sz_cached to zero to force the client to
8828 * always set csa_cachethis to FALSE because the current implementation
8829 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
8830 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04008831static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
8832 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04008833{
Andy Adamson18aad3d2013-06-26 12:21:49 -04008834 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04008835 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Trond Myklebust7402a4f2019-07-16 13:51:29 -04008836 unsigned int max_bc_slots = rpc_num_bc_slots(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04008837
Andy Adamson18aad3d2013-06-26 12:21:49 -04008838 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
8839 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
8840
Andy Adamsonfc931582009-04-01 09:22:31 -04008841 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04008842 args->fc_attrs.max_rqst_sz = max_rqst_sz;
8843 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04008844 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05008845 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04008846
8847 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05008848 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04008849 __func__,
8850 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05008851 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04008852
8853 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04008854 args->bc_attrs.max_rqst_sz = max_bc_payload;
8855 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04008856 args->bc_attrs.max_resp_sz_cached = 0;
8857 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust62421cd2018-08-11 11:52:39 -04008858 args->bc_attrs.max_reqs = max_t(unsigned short, max_session_cb_slots, 1);
Trond Myklebust7402a4f2019-07-16 13:51:29 -04008859 if (args->bc_attrs.max_reqs > max_bc_slots)
8860 args->bc_attrs.max_reqs = max_bc_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04008861
8862 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
8863 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
8864 __func__,
8865 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
8866 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
8867 args->bc_attrs.max_reqs);
8868}
8869
Trond Myklebust79969dd2015-02-18 11:30:18 -08008870static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
8871 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008872{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008873 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008874 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008875
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008876 if (rcvd->max_resp_sz > sent->max_resp_sz)
8877 return -EINVAL;
8878 /*
8879 * Our requested max_ops is the minimum we need; we're not
8880 * prepared to break up compounds into smaller pieces than that.
8881 * So, no point even trying to continue if the server won't
8882 * cooperate:
8883 */
8884 if (rcvd->max_ops < sent->max_ops)
8885 return -EINVAL;
8886 if (rcvd->max_reqs == 0)
8887 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04008888 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
8889 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008890 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04008891}
8892
Trond Myklebust79969dd2015-02-18 11:30:18 -08008893static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
8894 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008895{
8896 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008897 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04008898
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008899 if (!(res->flags & SESSION4_BACK_CHAN))
8900 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008901 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
8902 return -EINVAL;
8903 if (rcvd->max_resp_sz < sent->max_resp_sz)
8904 return -EINVAL;
8905 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
8906 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008907 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008908 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008909 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008910 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008911out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008912 return 0;
8913}
Andy Adamson8d353012009-04-01 09:22:32 -04008914
Andy Adamson8d353012009-04-01 09:22:32 -04008915static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08008916 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008917{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008918 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04008919
Trond Myklebust79969dd2015-02-18 11:30:18 -08008920 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008921 if (ret)
8922 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008923 return nfs4_verify_back_channel_attrs(args, res);
8924}
8925
8926static void nfs4_update_session(struct nfs4_session *session,
8927 struct nfs41_create_session_res *res)
8928{
8929 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05008930 /* Mark client id and session as being confirmed */
8931 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
8932 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08008933 session->flags = res->flags;
8934 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008935 if (res->flags & SESSION4_BACK_CHAN)
8936 memcpy(&session->bc_attrs, &res->bc_attrs,
8937 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04008938}
8939
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008940static int _nfs4_proc_create_session(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008941 const struct cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008942{
8943 struct nfs4_session *session = clp->cl_session;
8944 struct nfs41_create_session_args args = {
8945 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08008946 .clientid = clp->cl_clientid,
8947 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04008948 .cb_program = NFS4_CALLBACK,
8949 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08008950 struct nfs41_create_session_res res;
8951
Andy Adamsonfc931582009-04-01 09:22:31 -04008952 struct rpc_message msg = {
8953 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
8954 .rpc_argp = &args,
8955 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008956 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04008957 };
8958 int status;
8959
Chuck Lever6b26cc82016-05-02 14:40:40 -04008960 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04008961 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04008962
NeilBrown5a0c2572019-05-30 10:41:28 +10008963 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
8964 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008965 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008966
Trond Myklebustb519d402016-09-11 14:50:01 -04008967 switch (status) {
8968 case -NFS4ERR_STALE_CLIENTID:
8969 case -NFS4ERR_DELAY:
8970 case -ETIMEDOUT:
8971 case -EACCES:
8972 case -EAGAIN:
8973 goto out;
zhengbin8b98a532019-12-19 18:34:47 +08008974 }
Trond Myklebustb519d402016-09-11 14:50:01 -04008975
8976 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05008977 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04008978 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08008979 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008980 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08008981 if (status)
8982 goto out;
8983 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008984 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08008985out:
Andy Adamsonfc931582009-04-01 09:22:31 -04008986 return status;
8987}
8988
8989/*
8990 * Issues a CREATE_SESSION operation to the server.
8991 * It is the responsibility of the caller to verify the session is
8992 * expired before calling this routine.
8993 */
NeilBrowna52458b2018-12-03 11:30:31 +11008994int nfs4_proc_create_session(struct nfs_client *clp, const struct cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008995{
8996 int status;
8997 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04008998 struct nfs4_session *session = clp->cl_session;
8999
9000 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
9001
Trond Myklebust848f5bd2012-05-25 17:51:23 -04009002 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04009003 if (status)
9004 goto out;
9005
Andy Adamsonaacd5532011-11-09 13:58:21 -05009006 /* Init or reset the session slot tables */
9007 status = nfs4_setup_session_slot_tables(session);
9008 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04009009 if (status)
9010 goto out;
9011
9012 ptr = (unsigned *)&session->sess_id.data[0];
9013 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
9014 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04009015out:
9016 dprintk("<-- %s\n", __func__);
9017 return status;
9018}
9019
Andy Adamson0f3e66c2009-04-01 09:22:34 -04009020/*
9021 * Issue the over-the-wire RPC DESTROY_SESSION.
9022 * The caller must serialize access to this routine.
9023 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04009024int nfs4_proc_destroy_session(struct nfs4_session *session,
NeilBrowna52458b2018-12-03 11:30:31 +11009025 const struct cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04009026{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04009027 struct rpc_message msg = {
9028 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
9029 .rpc_argp = session,
9030 .rpc_cred = cred,
9031 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04009032 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04009033
9034 dprintk("--> nfs4_proc_destroy_session\n");
9035
9036 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05009037 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
9038 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04009039
NeilBrown5a0c2572019-05-30 10:41:28 +10009040 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
9041 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04009042 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04009043
9044 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04009045 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04009046 "Session has been destroyed regardless...\n", status);
9047
9048 dprintk("<-- nfs4_proc_destroy_session\n");
9049 return status;
9050}
9051
Trond Myklebust7b38c362012-05-23 13:23:31 -04009052/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009053 * Renew the cl_session lease.
9054 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04009055struct nfs4_sequence_data {
9056 struct nfs_client *clp;
9057 struct nfs4_sequence_args args;
9058 struct nfs4_sequence_res res;
9059};
9060
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08009061static void nfs41_sequence_release(void *data)
9062{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04009063 struct nfs4_sequence_data *calldata = data;
9064 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08009065
Elena Reshetova212bf412017-10-20 12:53:38 +03009066 if (refcount_read(&clp->cl_count) > 1)
Alexandros Batsakis71358402010-02-05 03:45:05 -08009067 nfs4_schedule_state_renewal(clp);
9068 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04009069 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08009070}
9071
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009072static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
9073{
9074 switch(task->tk_status) {
9075 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009076 rpc_delay(task, NFS4_POLL_RETRY_MAX);
9077 return -EAGAIN;
9078 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05009079 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009080 }
9081 return 0;
9082}
9083
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08009084static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009085{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04009086 struct nfs4_sequence_data *calldata = data;
9087 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009088
Trond Myklebust14516c32010-07-31 14:29:06 -04009089 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
9090 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009091
Trond Myklebustc6d01c62013-08-09 11:51:26 -04009092 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009093 if (task->tk_status < 0) {
9094 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Elena Reshetova212bf412017-10-20 12:53:38 +03009095 if (refcount_read(&clp->cl_count) == 1)
Alexandros Batsakis71358402010-02-05 03:45:05 -08009096 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009097
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009098 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
9099 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009100 return;
9101 }
9102 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009103 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08009104out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009105 dprintk("<-- %s\n", __func__);
9106}
9107
9108static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
9109{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04009110 struct nfs4_sequence_data *calldata = data;
9111 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009112 struct nfs4_sequence_args *args;
9113 struct nfs4_sequence_res *res;
9114
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009115 args = task->tk_msg.rpc_argp;
9116 res = task->tk_msg.rpc_resp;
9117
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009118 nfs4_setup_sequence(clp, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009119}
9120
9121static const struct rpc_call_ops nfs41_sequence_ops = {
9122 .rpc_call_done = nfs41_sequence_call_done,
9123 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08009124 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009125};
9126
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009127static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11009128 const struct cred *cred,
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009129 struct nfs4_slot *slot,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009130 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009131{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04009132 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009133 struct rpc_message msg = {
9134 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
9135 .rpc_cred = cred,
9136 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009137 struct rpc_task_setup task_setup_data = {
9138 .rpc_client = clp->cl_rpcclient,
9139 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009140 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04009141 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009142 };
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009143 struct rpc_task *ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009144
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009145 ret = ERR_PTR(-EIO);
Elena Reshetova212bf412017-10-20 12:53:38 +03009146 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009147 goto out_err;
9148
9149 ret = ERR_PTR(-ENOMEM);
Benny Halevydfb4f3092010-09-24 09:17:01 -04009150 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009151 if (calldata == NULL)
9152 goto out_put_clp;
Anna Schumakerfba83f32018-05-04 16:22:50 -04009153 nfs4_init_sequence(&calldata->args, &calldata->res, 0, is_privileged);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009154 nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04009155 msg.rpc_argp = &calldata->args;
9156 msg.rpc_resp = &calldata->res;
9157 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009158 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009159
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009160 ret = rpc_run_task(&task_setup_data);
9161 if (IS_ERR(ret))
9162 goto out_err;
9163 return ret;
9164out_put_clp:
9165 nfs_put_client(clp);
9166out_err:
9167 nfs41_release_slot(slot);
9168 return ret;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009169}
9170
NeilBrowna52458b2018-12-03 11:30:31 +11009171static int nfs41_proc_async_sequence(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009172{
9173 struct rpc_task *task;
9174 int ret = 0;
9175
Trond Myklebust2f60ea62011-08-24 15:07:37 -04009176 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04009177 return -EAGAIN;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009178 task = _nfs41_proc_sequence(clp, cred, NULL, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009179 if (IS_ERR(task))
9180 ret = PTR_ERR(task);
9181 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08009182 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009183 dprintk("<-- %s status=%d\n", __func__, ret);
9184 return ret;
9185}
9186
NeilBrowna52458b2018-12-03 11:30:31 +11009187static int nfs4_proc_sequence(struct nfs_client *clp, const struct cred *cred)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009188{
9189 struct rpc_task *task;
9190 int ret;
9191
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009192 task = _nfs41_proc_sequence(clp, cred, NULL, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009193 if (IS_ERR(task)) {
9194 ret = PTR_ERR(task);
9195 goto out;
9196 }
9197 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04009198 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009199 ret = task->tk_status;
9200 rpc_put_task(task);
9201out:
9202 dprintk("<-- %s status=%d\n", __func__, ret);
9203 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009204}
9205
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009206struct nfs4_reclaim_complete_data {
9207 struct nfs_client *clp;
9208 struct nfs41_reclaim_complete_args arg;
9209 struct nfs41_reclaim_complete_res res;
9210};
9211
9212static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
9213{
9214 struct nfs4_reclaim_complete_data *calldata = data;
9215
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009216 nfs4_setup_sequence(calldata->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04009217 &calldata->arg.seq_args,
9218 &calldata->res.seq_res,
9219 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009220}
9221
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009222static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
9223{
9224 switch(task->tk_status) {
9225 case 0:
Jeff Layton57174592018-03-18 08:37:03 -04009226 wake_up_all(&clp->cl_lock_waitq);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05009227 fallthrough;
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009228 case -NFS4ERR_COMPLETE_ALREADY:
9229 case -NFS4ERR_WRONG_CRED: /* What to do here? */
9230 break;
9231 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009232 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05009233 fallthrough;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04009234 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009235 return -EAGAIN;
Trond Myklebust0048fdd2017-05-04 13:44:04 -04009236 case -NFS4ERR_BADSESSION:
9237 case -NFS4ERR_DEADSESSION:
9238 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
Trond Myklebust0048fdd2017-05-04 13:44:04 -04009239 break;
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009240 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05009241 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009242 }
9243 return 0;
9244}
9245
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009246static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
9247{
9248 struct nfs4_reclaim_complete_data *calldata = data;
9249 struct nfs_client *clp = calldata->clp;
9250 struct nfs4_sequence_res *res = &calldata->res.seq_res;
9251
9252 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04009253 if (!nfs41_sequence_done(task, res))
9254 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009255
Trond Myklebustc6d01c62013-08-09 11:51:26 -04009256 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009257 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
9258 rpc_restart_call_prepare(task);
9259 return;
9260 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009261 dprintk("<-- %s\n", __func__);
9262}
9263
9264static void nfs4_free_reclaim_complete_data(void *data)
9265{
9266 struct nfs4_reclaim_complete_data *calldata = data;
9267
9268 kfree(calldata);
9269}
9270
9271static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
9272 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
9273 .rpc_call_done = nfs4_reclaim_complete_done,
9274 .rpc_release = nfs4_free_reclaim_complete_data,
9275};
9276
9277/*
9278 * Issue a global reclaim complete.
9279 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04009280static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11009281 const struct cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009282{
9283 struct nfs4_reclaim_complete_data *calldata;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009284 struct rpc_message msg = {
9285 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04009286 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009287 };
9288 struct rpc_task_setup task_setup_data = {
9289 .rpc_client = clp->cl_rpcclient,
9290 .rpc_message = &msg,
9291 .callback_ops = &nfs4_reclaim_complete_call_ops,
Anna Schumaker4c952e32019-08-14 15:46:48 -04009292 .flags = RPC_TASK_NO_ROUND_ROBIN,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009293 };
9294 int status = -ENOMEM;
9295
9296 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04009297 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009298 if (calldata == NULL)
9299 goto out;
9300 calldata->clp = clp;
9301 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009302
Anna Schumakerfba83f32018-05-04 16:22:50 -04009303 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0, 1);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009304 msg.rpc_argp = &calldata->arg;
9305 msg.rpc_resp = &calldata->res;
9306 task_setup_data.callback_data = calldata;
Anna Schumaker4c952e32019-08-14 15:46:48 -04009307 status = nfs4_call_sync_custom(&task_setup_data);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009308out:
9309 dprintk("<-- %s status=%d\n", __func__, status);
9310 return status;
9311}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009312
9313static void
9314nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
9315{
9316 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00009317 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009318
9319 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009320 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
Jeff Layton183d9e72016-05-17 12:28:47 -04009321 &lgp->res.seq_res, task);
9322 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009323}
9324
9325static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
9326{
9327 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04009328
9329 dprintk("--> %s\n", __func__);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04009330 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04009331 dprintk("<-- %s\n", __func__);
9332}
9333
9334static int
9335nfs4_layoutget_handle_exception(struct rpc_task *task,
9336 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
9337{
Trond Myklebustee314c22012-10-01 17:25:48 -07009338 struct inode *inode = lgp->args.inode;
9339 struct nfs_server *server = NFS_SERVER(inode);
9340 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04009341 int nfs4err = task->tk_status;
9342 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04009343 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009344
Boaz Harroshed7e5422014-01-22 20:34:54 +02009345 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009346
Trond Myklebust2dbf8df2018-06-15 15:58:45 -04009347 nfs4_sequence_free_slot(&lgp->res.seq_res);
9348
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04009349 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009350 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07009351 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08009352
9353 /*
9354 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
9355 * on the file. set tk_status to -ENODATA to tell upper layer to
9356 * retry go inband.
9357 */
9358 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04009359 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08009360 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02009361 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07009362 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
9363 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
9364 */
9365 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04009366 status = -EOVERFLOW;
9367 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07009368 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02009369 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07009370 * (or clients) writing to the same RAID stripe except when
9371 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04009372 *
9373 * Treat it like we would RECALLCONFLICT -- we retry for a little
9374 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02009375 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009376 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04009377 if (lgp->args.minlength == 0) {
9378 status = -EOVERFLOW;
9379 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02009380 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04009381 status = -EBUSY;
9382 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04009383 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04009384 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04009385 break;
Trond Myklebust26f47442016-09-22 13:39:10 -04009386 case -NFS4ERR_DELEG_REVOKED:
9387 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustee314c22012-10-01 17:25:48 -07009388 case -NFS4ERR_EXPIRED:
9389 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04009390 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07009391 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04009392 lo = NFS_I(inode)->layout;
9393 /* If the open stateid was bad, then recover it. */
9394 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
Trond Myklebuste8fa33a2017-10-04 13:49:12 -04009395 !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07009396 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04009397 exception->state = lgp->args.ctx->state;
Trond Myklebust26f47442016-09-22 13:39:10 -04009398 exception->stateid = &lgp->args.stateid;
Trond Myklebust2259f962015-09-20 13:30:30 -04009399 break;
9400 }
Trond Myklebustee314c22012-10-01 17:25:48 -07009401
Trond Myklebustf7db0b22016-07-14 15:14:02 -04009402 /*
9403 * Mark the bad layout state as invalid, then retry
9404 */
Trond Myklebust668f4552016-07-24 17:08:59 -04009405 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04009406 spin_unlock(&inode->i_lock);
Trond Myklebust1f18b822017-04-29 10:10:17 -04009407 nfs_commit_inode(inode, 0);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04009408 pnfs_free_lseg_list(&head);
9409 status = -EAGAIN;
9410 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009411 }
Jeff Layton183d9e72016-05-17 12:28:47 -04009412
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04009413 err = nfs4_handle_exception(server, nfs4err, exception);
9414 if (!status) {
9415 if (exception->retry)
9416 status = -EAGAIN;
9417 else
9418 status = err;
9419 }
Trond Myklebustee314c22012-10-01 17:25:48 -07009420out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009421 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04009422 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009423}
9424
Fred Isamandacb4522016-09-19 17:47:09 -04009425size_t max_response_pages(struct nfs_server *server)
Idan Kedar85541162012-08-02 11:47:10 +03009426{
9427 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
9428 return nfs_page_array_len(0, max_resp_sz);
9429}
9430
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009431static void nfs4_layoutget_release(void *calldata)
9432{
9433 struct nfs4_layoutget *lgp = calldata;
9434
9435 dprintk("--> %s\n", __func__);
Trond Myklebustbd171932017-06-27 17:33:38 -04009436 nfs4_sequence_free_slot(&lgp->res.seq_res);
Trond Myklebust29a8bfe2018-05-30 17:16:20 -04009437 pnfs_layoutget_free(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009438 dprintk("<-- %s\n", __func__);
9439}
9440
9441static const struct rpc_call_ops nfs4_layoutget_call_ops = {
9442 .rpc_call_prepare = nfs4_layoutget_prepare,
9443 .rpc_call_done = nfs4_layoutget_done,
9444 .rpc_release = nfs4_layoutget_release,
9445};
9446
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04009447struct pnfs_layout_segment *
Fred Isamandacb4522016-09-19 17:47:09 -04009448nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009449{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05009450 struct inode *inode = lgp->args.inode;
9451 struct nfs_server *server = NFS_SERVER(inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009452 struct rpc_task *task;
9453 struct rpc_message msg = {
9454 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
9455 .rpc_argp = &lgp->args,
9456 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04009457 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009458 };
9459 struct rpc_task_setup task_setup_data = {
9460 .rpc_client = server->client,
9461 .rpc_message = &msg,
9462 .callback_ops = &nfs4_layoutget_call_ops,
9463 .callback_data = lgp,
Trond Myklebust63ec2b62020-02-07 19:40:14 -05009464 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009465 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04009466 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04009467 struct nfs4_exception exception = {
9468 .inode = inode,
9469 .timeout = *timeout,
9470 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009471 int status = 0;
9472
9473 dprintk("--> %s\n", __func__);
9474
Peng Tao4bd5a982014-11-17 11:05:17 +08009475 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
9476 pnfs_get_layout_hdr(NFS_I(inode)->layout);
9477
Anna Schumakerfba83f32018-05-04 16:22:50 -04009478 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05009479
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009480 task = rpc_run_task(&task_setup_data);
Xiyu Yang6e476662020-04-25 21:04:40 +08009481
Anna Schumaker820bf852017-01-11 15:01:43 -05009482 status = rpc_wait_for_completion_task(task);
Trond Myklebust2dbf8df2018-06-15 15:58:45 -04009483 if (status != 0)
9484 goto out;
9485
Trond Myklebust18c07782019-02-13 07:55:31 -05009486 if (task->tk_status < 0) {
Jeff Layton183d9e72016-05-17 12:28:47 -04009487 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
9488 *timeout = exception.timeout;
Trond Myklebust18c07782019-02-13 07:55:31 -05009489 } else if (lgp->res.layoutp->len == 0) {
9490 status = -EAGAIN;
9491 *timeout = nfs4_update_delay(&exception.timeout);
Trond Myklebust2dbf8df2018-06-15 15:58:45 -04009492 } else
9493 lseg = pnfs_layout_process(lgp);
9494out:
Trond Myklebust1037e6e2013-08-14 16:36:51 -04009495 trace_nfs4_layoutget(lgp->args.ctx,
9496 &lgp->args.range,
9497 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05009498 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04009499 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04009500
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009501 rpc_put_task(task);
9502 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04009503 if (status)
9504 return ERR_PTR(status);
9505 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009506}
9507
Benny Halevycbe82602011-05-22 19:52:37 +03009508static void
9509nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
9510{
9511 struct nfs4_layoutreturn *lrp = calldata;
9512
9513 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009514 nfs4_setup_sequence(lrp->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04009515 &lrp->args.seq_args,
9516 &lrp->res.seq_res,
9517 task);
Trond Myklebustc8bf7072018-06-15 16:31:02 -04009518 if (!pnfs_layout_is_valid(lrp->args.layout))
9519 rpc_exit(task, 0);
Benny Halevycbe82602011-05-22 19:52:37 +03009520}
9521
9522static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
9523{
9524 struct nfs4_layoutreturn *lrp = calldata;
9525 struct nfs_server *server;
9526
9527 dprintk("--> %s\n", __func__);
9528
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04009529 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03009530 return;
9531
Trond Myklebust6109bcf2019-09-20 07:23:43 -04009532 /*
9533 * Was there an RPC level error? Assume the call succeeded,
9534 * and that we need to release the layout
9535 */
9536 if (task->tk_rpc_status != 0 && RPC_WAS_SENT(task)) {
9537 lrp->res.lrs_present = 0;
9538 return;
9539 }
9540
Benny Halevycbe82602011-05-22 19:52:37 +03009541 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05009542 switch (task->tk_status) {
Trond Myklebustff905142017-11-06 15:28:08 -05009543 case -NFS4ERR_OLD_STATEID:
Trond Myklebust30cb3ee2019-09-20 07:23:45 -04009544 if (nfs4_layout_refresh_old_stateid(&lrp->args.stateid,
Trond Myklebustecf84022018-08-15 21:35:46 -04009545 &lrp->args.range,
Trond Myklebustff905142017-11-06 15:28:08 -05009546 lrp->args.inode))
9547 goto out_restart;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05009548 fallthrough;
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05009549 default:
9550 task->tk_status = 0;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05009551 fallthrough;
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05009552 case 0:
9553 break;
9554 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10009555 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05009556 break;
Trond Myklebustff905142017-11-06 15:28:08 -05009557 goto out_restart;
Benny Halevycbe82602011-05-22 19:52:37 +03009558 }
Benny Halevycbe82602011-05-22 19:52:37 +03009559 dprintk("<-- %s\n", __func__);
Trond Myklebustff905142017-11-06 15:28:08 -05009560 return;
9561out_restart:
9562 task->tk_status = 0;
9563 nfs4_sequence_free_slot(&lrp->res.seq_res);
9564 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03009565}
9566
9567static void nfs4_layoutreturn_release(void *calldata)
9568{
9569 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04009570 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03009571
9572 dprintk("--> %s\n", __func__);
Trond Myklebust2a974422016-11-20 13:13:54 -05009573 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
Trond Myklebust68f74472016-10-12 19:50:54 -04009574 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04009575 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebust4d796d72016-09-23 11:38:08 -04009576 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
9577 lrp->ld_private.ops->free(&lrp->ld_private);
Trond Myklebust2f065dd2016-12-07 12:29:26 -05009578 pnfs_put_layout_hdr(lrp->args.layout);
9579 nfs_iput_and_deactive(lrp->inode);
Trond Myklebust44ea8df2020-04-02 15:37:02 -04009580 put_cred(lrp->cred);
Benny Halevycbe82602011-05-22 19:52:37 +03009581 kfree(calldata);
9582 dprintk("<-- %s\n", __func__);
9583}
9584
9585static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
9586 .rpc_call_prepare = nfs4_layoutreturn_prepare,
9587 .rpc_call_done = nfs4_layoutreturn_done,
9588 .rpc_release = nfs4_layoutreturn_release,
9589};
9590
Peng Tao6c166052014-11-17 09:30:40 +08009591int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03009592{
9593 struct rpc_task *task;
9594 struct rpc_message msg = {
9595 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
9596 .rpc_argp = &lrp->args,
9597 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04009598 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03009599 };
9600 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04009601 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03009602 .rpc_message = &msg,
9603 .callback_ops = &nfs4_layoutreturn_call_ops,
9604 .callback_data = lrp,
9605 };
Peng Tao6c166052014-11-17 09:30:40 +08009606 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03009607
Andrew Elble99ade3c2015-12-02 09:39:51 -05009608 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
9609 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
9610 &task_setup_data.rpc_client, &msg);
9611
Benny Halevycbe82602011-05-22 19:52:37 +03009612 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05009613 if (!sync) {
9614 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
9615 if (!lrp->inode) {
9616 nfs4_layoutreturn_release(lrp);
9617 return -EAGAIN;
9618 }
9619 task_setup_data.flags |= RPC_TASK_ASYNC;
9620 }
Anna Schumakerfba83f32018-05-04 16:22:50 -04009621 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1, 0);
Benny Halevycbe82602011-05-22 19:52:37 +03009622 task = rpc_run_task(&task_setup_data);
9623 if (IS_ERR(task))
9624 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05009625 if (sync)
9626 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05009627 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03009628 dprintk("<-- %s status=%d\n", __func__, status);
9629 rpc_put_task(task);
9630 return status;
9631}
9632
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009633static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009634_nfs4_proc_getdeviceinfo(struct nfs_server *server,
9635 struct pnfs_device *pdev,
NeilBrowna52458b2018-12-03 11:30:31 +11009636 const struct cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009637{
9638 struct nfs4_getdeviceinfo_args args = {
9639 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04009640 .notify_types = NOTIFY_DEVICEID4_CHANGE |
9641 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009642 };
9643 struct nfs4_getdeviceinfo_res res = {
9644 .pdev = pdev,
9645 };
9646 struct rpc_message msg = {
9647 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
9648 .rpc_argp = &args,
9649 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009650 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009651 };
9652 int status;
9653
9654 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00009655 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04009656 if (res.notification & ~args.notify_types)
9657 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04009658 if (res.notification != args.notify_types)
9659 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04009660
Trond Myklebustcac1d3a2020-12-16 16:31:26 -05009661 trace_nfs4_getdeviceinfo(server, &pdev->dev_id, status);
9662
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009663 dprintk("<-- %s status=%d\n", __func__, status);
9664
9665 return status;
9666}
9667
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009668int nfs4_proc_getdeviceinfo(struct nfs_server *server,
9669 struct pnfs_device *pdev,
NeilBrowna52458b2018-12-03 11:30:31 +11009670 const struct cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009671{
9672 struct nfs4_exception exception = { };
9673 int err;
9674
9675 do {
9676 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009677 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009678 &exception);
9679 } while (exception.retry);
9680 return err;
9681}
9682EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
9683
Andy Adamson863a3c62011-03-23 13:27:54 +00009684static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
9685{
9686 struct nfs4_layoutcommit_data *data = calldata;
9687 struct nfs_server *server = NFS_SERVER(data->args.inode);
9688
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009689 nfs4_setup_sequence(server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04009690 &data->args.seq_args,
9691 &data->res.seq_res,
9692 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00009693}
9694
9695static void
9696nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
9697{
9698 struct nfs4_layoutcommit_data *data = calldata;
9699 struct nfs_server *server = NFS_SERVER(data->args.inode);
9700
Trond Myklebust6ba7db32012-10-22 20:07:20 -04009701 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00009702 return;
9703
9704 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009705 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
9706 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
9707 case -NFS4ERR_BADLAYOUT: /* no layout */
9708 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00009709 task->tk_status = 0;
Gustavo A. R. Silvaffb81712020-11-20 12:26:46 -06009710 break;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009711 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009712 break;
9713 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10009714 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009715 rpc_restart_call_prepare(task);
9716 return;
9717 }
9718 }
Andy Adamson863a3c62011-03-23 13:27:54 +00009719}
9720
9721static void nfs4_layoutcommit_release(void *calldata)
9722{
9723 struct nfs4_layoutcommit_data *data = calldata;
9724
Andy Adamsondb29c082011-07-30 20:52:38 -04009725 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05009726 nfs_post_op_update_inode_force_wcc(data->args.inode,
9727 data->res.fattr);
NeilBrowna52458b2018-12-03 11:30:31 +11009728 put_cred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05009729 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00009730 kfree(data);
9731}
9732
9733static const struct rpc_call_ops nfs4_layoutcommit_ops = {
9734 .rpc_call_prepare = nfs4_layoutcommit_prepare,
9735 .rpc_call_done = nfs4_layoutcommit_done,
9736 .rpc_release = nfs4_layoutcommit_release,
9737};
9738
9739int
Andy Adamsonef311532011-03-12 02:58:10 -05009740nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00009741{
9742 struct rpc_message msg = {
9743 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
9744 .rpc_argp = &data->args,
9745 .rpc_resp = &data->res,
9746 .rpc_cred = data->cred,
9747 };
9748 struct rpc_task_setup task_setup_data = {
9749 .task = &data->task,
9750 .rpc_client = NFS_CLIENT(data->args.inode),
9751 .rpc_message = &msg,
9752 .callback_ops = &nfs4_layoutcommit_ops,
9753 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00009754 };
9755 struct rpc_task *task;
9756 int status = 0;
9757
Kinglong Meeb4839eb2015-07-01 12:00:13 +08009758 dprintk("NFS: initiating layoutcommit call. sync %d "
9759 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00009760 data->args.lastbytewritten,
9761 data->args.inode->i_ino);
9762
Trond Myklebust472e2592015-02-05 16:50:30 -05009763 if (!sync) {
9764 data->inode = nfs_igrab_and_active(data->args.inode);
9765 if (data->inode == NULL) {
9766 nfs4_layoutcommit_release(data);
9767 return -EAGAIN;
9768 }
9769 task_setup_data.flags = RPC_TASK_ASYNC;
9770 }
Anna Schumakerfba83f32018-05-04 16:22:50 -04009771 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Andy Adamson863a3c62011-03-23 13:27:54 +00009772 task = rpc_run_task(&task_setup_data);
9773 if (IS_ERR(task))
9774 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05009775 if (sync)
9776 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05009777 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00009778 dprintk("%s: status %d\n", __func__, status);
9779 rpc_put_task(task);
9780 return status;
9781}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009782
Trond Myklebust302fad72019-02-18 13:32:38 -05009783/*
Andy Adamson97431202013-08-08 10:57:56 -04009784 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
9785 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
9786 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009787static int
9788_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009789 struct nfs_fsinfo *info,
9790 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009791{
9792 struct nfs41_secinfo_no_name_args args = {
9793 .style = SECINFO_STYLE_CURRENT_FH,
9794 };
9795 struct nfs4_secinfo_res res = {
9796 .flavors = flavors,
9797 };
9798 struct rpc_message msg = {
9799 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
9800 .rpc_argp = &args,
9801 .rpc_resp = &res,
9802 };
Anna Schumakercc15e242019-08-14 16:22:31 -04009803 struct nfs4_call_sync_data data = {
9804 .seq_server = server,
9805 .seq_args = &args.seq_args,
9806 .seq_res = &res.seq_res,
9807 };
9808 struct rpc_task_setup task_setup = {
9809 .rpc_client = server->client,
9810 .rpc_message = &msg,
9811 .callback_ops = server->nfs_client->cl_mvops->call_sync_ops,
9812 .callback_data = &data,
9813 .flags = RPC_TASK_NO_ROUND_ROBIN,
9814 };
NeilBrowna52458b2018-12-03 11:30:31 +11009815 const struct cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009816 int status;
9817
9818 if (use_integrity) {
Colin Ian King48bb6ec2020-06-10 00:22:57 +01009819 task_setup.rpc_client = server->nfs_client->cl_rpcclient;
Anna Schumakercc15e242019-08-14 16:22:31 -04009820
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04009821 cred = nfs4_get_clid_cred(server->nfs_client);
9822 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009823 }
9824
9825 dprintk("--> %s\n", __func__);
Anna Schumakercc15e242019-08-14 16:22:31 -04009826 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
9827 status = nfs4_call_sync_custom(&task_setup);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009828 dprintk("<-- %s status=%d\n", __func__, status);
9829
NeilBrowna52458b2018-12-03 11:30:31 +11009830 put_cred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009831
9832 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009833}
9834
9835static int
9836nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9837 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
9838{
Trond Myklebust0688e642019-04-07 13:59:09 -04009839 struct nfs4_exception exception = {
9840 .interruptible = true,
9841 };
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009842 int err;
9843 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009844 /* first try using integrity protection */
9845 err = -NFS4ERR_WRONGSEC;
9846
9847 /* try to use integrity protection with machine cred */
9848 if (_nfs4_is_integrity_protected(server->nfs_client))
9849 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9850 flavors, true);
9851
9852 /*
9853 * if unable to use integrity protection, or SECINFO with
9854 * integrity protection returns NFS4ERR_WRONGSEC (which is
9855 * disallowed by spec, but exists in deployed servers) use
9856 * the current filesystem's rpc_client and the user cred.
9857 */
9858 if (err == -NFS4ERR_WRONGSEC)
9859 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9860 flavors, false);
9861
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009862 switch (err) {
9863 case 0:
9864 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05009865 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009866 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009867 default:
9868 err = nfs4_handle_exception(server, err, &exception);
9869 }
9870 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009871out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009872 return err;
9873}
9874
9875static int
9876nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
9877 struct nfs_fsinfo *info)
9878{
9879 int err;
9880 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04009881 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009882 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009883 struct nfs4_secinfo4 *secinfo;
9884 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009885
9886 page = alloc_page(GFP_KERNEL);
9887 if (!page) {
9888 err = -ENOMEM;
9889 goto out;
9890 }
9891
9892 flavors = page_address(page);
9893 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
9894
9895 /*
9896 * Fall back on "guess and check" method if
9897 * the server doesn't support SECINFO_NO_NAME
9898 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05009899 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009900 err = nfs4_find_root_sec(server, fhandle, info);
9901 goto out_freepage;
9902 }
9903 if (err)
9904 goto out_freepage;
9905
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009906 for (i = 0; i < flavors->num_flavors; i++) {
9907 secinfo = &flavors->flavors[i];
9908
9909 switch (secinfo->flavor) {
9910 case RPC_AUTH_NULL:
9911 case RPC_AUTH_UNIX:
9912 case RPC_AUTH_GSS:
9913 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
9914 &secinfo->flavor_info);
9915 break;
9916 default:
9917 flavor = RPC_AUTH_MAXFLAVOR;
9918 break;
9919 }
9920
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04009921 if (!nfs_auth_info_match(&server->auth_info, flavor))
9922 flavor = RPC_AUTH_MAXFLAVOR;
9923
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009924 if (flavor != RPC_AUTH_MAXFLAVOR) {
9925 err = nfs4_lookup_root_sec(server, fhandle,
9926 info, flavor);
9927 if (!err)
9928 break;
9929 }
9930 }
9931
9932 if (flavor == RPC_AUTH_MAXFLAVOR)
9933 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009934
9935out_freepage:
9936 put_page(page);
9937 if (err == -EACCES)
9938 return -EPERM;
9939out:
9940 return err;
9941}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009942
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009943static int _nfs41_test_stateid(struct nfs_server *server,
9944 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11009945 const struct cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009946{
9947 int status;
9948 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009949 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009950 };
9951 struct nfs41_test_stateid_res res;
9952 struct rpc_message msg = {
9953 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
9954 .rpc_argp = &args,
9955 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009956 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009957 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009958 struct rpc_clnt *rpc_client = server->client;
9959
9960 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9961 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009962
Chuck Lever38527b12012-07-11 16:30:23 -04009963 dprintk("NFS call test_stateid %p\n", stateid);
Anna Schumakerfba83f32018-05-04 16:22:50 -04009964 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009965 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009966 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04009967 if (status != NFS_OK) {
9968 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04009969 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04009970 }
9971 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04009972 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04009973}
9974
Trond Myklebust43912bb2016-09-22 13:38:56 -04009975static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
9976 int err, struct nfs4_exception *exception)
9977{
9978 exception->retry = 0;
9979 switch(err) {
9980 case -NFS4ERR_DELAY:
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009981 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust43912bb2016-09-22 13:38:56 -04009982 nfs4_handle_exception(server, err, exception);
9983 break;
9984 case -NFS4ERR_BADSESSION:
9985 case -NFS4ERR_BADSLOT:
9986 case -NFS4ERR_BAD_HIGH_SLOT:
9987 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9988 case -NFS4ERR_DEADSESSION:
9989 nfs4_do_handle_exception(server, err, exception);
9990 }
9991}
9992
Chuck Lever38527b12012-07-11 16:30:23 -04009993/**
9994 * nfs41_test_stateid - perform a TEST_STATEID operation
9995 *
9996 * @server: server / transport on which to perform the operation
9997 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009998 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04009999 *
10000 * Returns NFS_OK if the server recognizes that "stateid" is valid.
10001 * Otherwise a negative NFS4ERR value is returned if the operation
10002 * failed or the state ID is not currently valid.
10003 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -040010004static int nfs41_test_stateid(struct nfs_server *server,
10005 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +110010006 const struct cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -040010007{
Trond Myklebust0688e642019-04-07 13:59:09 -040010008 struct nfs4_exception exception = {
10009 .interruptible = true,
10010 };
Bryan Schumaker7d974792011-06-02 14:59:08 -040010011 int err;
10012 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -040010013 err = _nfs41_test_stateid(server, stateid, cred);
Trond Myklebust43912bb2016-09-22 13:38:56 -040010014 nfs4_handle_delay_or_session_error(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -040010015 } while (exception.retry);
10016 return err;
10017}
Bryan Schumaker9aeda352011-06-02 14:59:09 -040010018
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010019struct nfs_free_stateid_data {
10020 struct nfs_server *server;
10021 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -040010022 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010023};
10024
10025static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
10026{
10027 struct nfs_free_stateid_data *data = calldata;
Anna Schumaker7981c8a2017-01-10 11:39:53 -050010028 nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010029 &data->args.seq_args,
10030 &data->res.seq_res,
10031 task);
10032}
10033
10034static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
10035{
10036 struct nfs_free_stateid_data *data = calldata;
10037
10038 nfs41_sequence_done(task, &data->res.seq_res);
10039
10040 switch (task->tk_status) {
10041 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +100010042 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010043 rpc_restart_call_prepare(task);
10044 }
10045}
10046
10047static void nfs41_free_stateid_release(void *calldata)
10048{
10049 kfree(calldata);
10050}
10051
Trond Myklebust17f26b12013-08-21 15:48:42 -040010052static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010053 .rpc_call_prepare = nfs41_free_stateid_prepare,
10054 .rpc_call_done = nfs41_free_stateid_done,
10055 .rpc_release = nfs41_free_stateid_release,
10056};
10057
Anna Schumaker2f261022018-05-15 13:03:39 -040010058/**
10059 * nfs41_free_stateid - perform a FREE_STATEID operation
10060 *
10061 * @server: server / transport on which to perform the operation
10062 * @stateid: state ID to release
10063 * @cred: credential
Trond Myklebust302fad72019-02-18 13:32:38 -050010064 * @privileged: set to true if this call needs to be privileged
Anna Schumaker2f261022018-05-15 13:03:39 -040010065 *
10066 * Note: this function is always asynchronous.
10067 */
10068static int nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -040010069 const nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +110010070 const struct cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010071 bool privileged)
10072{
Bryan Schumaker9aeda352011-06-02 14:59:09 -040010073 struct rpc_message msg = {
10074 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -040010075 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -040010076 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010077 struct rpc_task_setup task_setup = {
10078 .rpc_client = server->client,
10079 .rpc_message = &msg,
10080 .callback_ops = &nfs41_free_stateid_ops,
10081 .flags = RPC_TASK_ASYNC,
10082 };
10083 struct nfs_free_stateid_data *data;
Anna Schumaker2f261022018-05-15 13:03:39 -040010084 struct rpc_task *task;
Bryan Schumaker9aeda352011-06-02 14:59:09 -040010085
Weston Andros Adamson3787d502013-08-13 16:37:36 -040010086 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
10087 &task_setup.rpc_client, &msg);
10088
Chuck Lever38527b12012-07-11 16:30:23 -040010089 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010090 data = kmalloc(sizeof(*data), GFP_NOFS);
10091 if (!data)
Anna Schumaker2f261022018-05-15 13:03:39 -040010092 return -ENOMEM;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010093 data->server = server;
10094 nfs4_stateid_copy(&data->args.stateid, stateid);
10095
10096 task_setup.callback_data = data;
10097
10098 msg.rpc_argp = &data->args;
10099 msg.rpc_resp = &data->res;
Anna Schumakerfba83f32018-05-04 16:22:50 -040010100 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, privileged);
Anna Schumaker2f261022018-05-15 13:03:39 -040010101 task = rpc_run_task(&task_setup);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010102 if (IS_ERR(task))
10103 return PTR_ERR(task);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010104 rpc_put_task(task);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -040010105 return 0;
Bryan Schumaker9aeda352011-06-02 14:59:09 -040010106}
Trond Myklebust36281ca2012-03-04 18:13:56 -050010107
Jeff Laytonf1cdae82014-05-01 06:28:47 -040010108static void
10109nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -040010110{
NeilBrowna52458b2018-12-03 11:30:31 +110010111 const struct cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -040010112
Trond Myklebustf0b0bf82016-09-22 13:39:04 -040010113 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -040010114 nfs4_free_lock_state(server, lsp);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -040010115}
10116
Trond Myklebust36281ca2012-03-04 18:13:56 -050010117static bool nfs41_match_stateid(const nfs4_stateid *s1,
10118 const nfs4_stateid *s2)
10119{
Trond Myklebust93b717f2016-05-16 17:42:43 -040010120 if (s1->type != s2->type)
10121 return false;
10122
Trond Myklebust2d2f24a2012-03-04 18:13:57 -050010123 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -050010124 return false;
10125
Trond Myklebust2d2f24a2012-03-04 18:13:57 -050010126 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -050010127 return true;
Trond Myklebust36281ca2012-03-04 18:13:56 -050010128
Anna Schumaker045c5512017-01-11 16:59:48 -050010129 return s1->seqid == 0 || s2->seqid == 0;
Trond Myklebust36281ca2012-03-04 18:13:56 -050010130}
10131
Andy Adamson557134a2009-04-01 09:21:53 -040010132#endif /* CONFIG_NFS_V4_1 */
10133
Trond Myklebust36281ca2012-03-04 18:13:56 -050010134static bool nfs4_match_stateid(const nfs4_stateid *s1,
10135 const nfs4_stateid *s2)
10136{
Trond Myklebustf597c532012-03-04 18:13:56 -050010137 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -050010138}
10139
10140
Trond Myklebust17280172012-03-11 13:11:00 -040010141static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -050010142 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -050010143 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010144 .recover_open = nfs4_open_reclaim,
10145 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -040010146 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -040010147 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010148};
10149
Andy Adamson591d71c2009-04-01 09:22:47 -040010150#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -040010151static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -040010152 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
10153 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
10154 .recover_open = nfs4_open_reclaim,
10155 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -050010156 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -050010157 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -040010158 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -040010159};
10160#endif /* CONFIG_NFS_V4_1 */
10161
Trond Myklebust17280172012-03-11 13:11:00 -040010162static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -050010163 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -050010164 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +030010165 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010166 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -040010167 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010168};
10169
Andy Adamson591d71c2009-04-01 09:22:47 -040010170#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -040010171static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -040010172 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
10173 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -040010174 .recover_open = nfs41_open_expired,
10175 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -050010176 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -040010177};
10178#endif /* CONFIG_NFS_V4_1 */
10179
Trond Myklebust17280172012-03-11 13:11:00 -040010180static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -040010181 .sched_state_renewal = nfs4_proc_async_renew,
NeilBrownf15e1e82018-12-03 11:30:30 +110010182 .get_state_renewal_cred = nfs4_get_renew_cred,
Benny Halevy8e69514f2009-04-01 09:22:45 -040010183 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -040010184};
10185
10186#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -040010187static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -040010188 .sched_state_renewal = nfs41_proc_async_sequence,
NeilBrownf15e1e82018-12-03 11:30:30 +110010189 .get_state_renewal_cred = nfs4_get_machine_cred,
Benny Halevy8e69514f2009-04-01 09:22:45 -040010190 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -040010191};
10192#endif
10193
Chuck Leverec011fe2013-10-17 14:12:39 -040010194static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -040010195 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -040010196 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -040010197};
10198
10199#if defined(CONFIG_NFS_V4_1)
10200static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -040010201 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -040010202 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -040010203};
10204#endif /* CONFIG_NFS_V4_1 */
10205
Trond Myklebust97dc1352010-06-16 09:52:26 -040010206static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
10207 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -040010208 .init_caps = NFS_CAP_READDIRPLUS
10209 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -040010210 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -040010211 .init_client = nfs40_init_client,
10212 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -050010213 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -040010214 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -040010215 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust45870d62016-09-22 13:38:59 -040010216 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -050010217 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -040010218 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -040010219 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
10220 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
10221 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -040010222 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -040010223};
10224
10225#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -050010226static struct nfs_seqid *
10227nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
10228{
10229 return NULL;
10230}
10231
Trond Myklebust97dc1352010-06-16 09:52:26 -040010232static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
10233 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -040010234 .init_caps = NFS_CAP_READDIRPLUS
10235 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -040010236 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -040010237 | NFS_CAP_STATEID_NFSV41
Fred Isaman6e012602016-10-04 15:26:41 -040010238 | NFS_CAP_ATOMIC_OPEN_V1
10239 | NFS_CAP_LGOPEN,
Chuck Leverabf79bb2013-08-09 12:49:11 -040010240 .init_client = nfs41_init_client,
10241 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -050010242 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -040010243 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -040010244 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust45870d62016-09-22 13:38:59 -040010245 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -050010246 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -040010247 .session_trunk = nfs4_test_session_trunk,
Chuck Lever9915ea72013-08-09 12:48:27 -040010248 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -040010249 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
10250 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
10251 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -040010252 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -040010253};
10254#endif
10255
Steve Dickson42c2c422013-05-22 12:50:38 -040010256#if defined(CONFIG_NFS_V4_2)
10257static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
10258 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -040010259 .init_caps = NFS_CAP_READDIRPLUS
10260 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -040010261 | NFS_CAP_POSIX_LOCK
10262 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -040010263 | NFS_CAP_ATOMIC_OPEN_V1
Fred Isaman6e012602016-10-04 15:26:41 -040010264 | NFS_CAP_LGOPEN
Anna Schumakerf4ac1672014-11-25 13:18:15 -050010265 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -040010266 | NFS_CAP_COPY
Olga Kornievskaiacb95dee2018-07-09 15:13:29 -040010267 | NFS_CAP_OFFLOAD_CANCEL
Olga Kornievskaia04915672019-06-04 16:14:30 -040010268 | NFS_CAP_COPY_NOTIFY
Anna Schumaker624bd5b2014-11-25 13:18:16 -050010269 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -040010270 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +080010271 | NFS_CAP_LAYOUTSTATS
Trond Myklebust3eb86092019-02-08 10:31:05 -050010272 | NFS_CAP_CLONE
Anna Schumakerc5675522014-05-28 13:41:22 -040010273 | NFS_CAP_LAYOUTERROR
10274 | NFS_CAP_READ_PLUS,
Chuck Leverabf79bb2013-08-09 12:49:11 -040010275 .init_client = nfs41_init_client,
10276 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -040010277 .match_stateid = nfs41_match_stateid,
10278 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -040010279 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -040010280 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust45870d62016-09-22 13:38:59 -040010281 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -050010282 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -040010283 .session_trunk = nfs4_test_session_trunk,
Steve Dickson42c2c422013-05-22 12:50:38 -040010284 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
10285 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
10286 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +080010287 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -040010288};
10289#endif
10290
Trond Myklebust97dc1352010-06-16 09:52:26 -040010291const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
10292 [0] = &nfs_v4_0_minor_ops,
10293#if defined(CONFIG_NFS_V4_1)
10294 [1] = &nfs_v4_1_minor_ops,
10295#endif
Steve Dickson42c2c422013-05-22 12:50:38 -040010296#if defined(CONFIG_NFS_V4_2)
10297 [2] = &nfs_v4_2_minor_ops,
10298#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -040010299};
10300
Trond Myklebust13997822016-07-24 17:10:52 -040010301static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +010010302{
Frank van der Linden012a2112020-06-23 22:39:03 +000010303 ssize_t error, error2, error3;
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +010010304
10305 error = generic_listxattr(dentry, list, size);
10306 if (error < 0)
10307 return error;
10308 if (list) {
10309 list += error;
10310 size -= error;
10311 }
10312
10313 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
10314 if (error2 < 0)
10315 return error2;
Frank van der Linden012a2112020-06-23 22:39:03 +000010316
10317 if (list) {
10318 list += error2;
10319 size -= error2;
10320 }
10321
10322 error3 = nfs4_listxattr_nfs4_user(d_inode(dentry), list, size);
10323 if (error3 < 0)
10324 return error3;
10325
10326 return error + error2 + error3;
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +010010327}
10328
Trond Myklebust17f26b12013-08-21 15:48:42 -040010329static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -040010330 .create = nfs_create,
10331 .lookup = nfs_lookup,
10332 .atomic_open = nfs_atomic_open,
10333 .link = nfs_link,
10334 .unlink = nfs_unlink,
10335 .symlink = nfs_symlink,
10336 .mkdir = nfs_mkdir,
10337 .rmdir = nfs_rmdir,
10338 .mknod = nfs_mknod,
10339 .rename = nfs_rename,
10340 .permission = nfs_permission,
10341 .getattr = nfs_getattr,
10342 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +010010343 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -040010344};
10345
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -080010346static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +000010347 .permission = nfs_permission,
10348 .getattr = nfs_getattr,
10349 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +010010350 .listxattr = nfs4_listxattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +000010351};
10352
David Howells509de812006-08-22 20:06:11 -040010353const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010354 .version = 4, /* protocol version */
10355 .dentry_ops = &nfs4_dentry_operations,
10356 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +000010357 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -040010358 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010359 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -040010360 .submount = nfs4_submount,
David Howellsf2aedb72019-12-10 07:31:13 -050010361 .try_get_tree = nfs4_try_get_tree,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010362 .getattr = nfs4_proc_getattr,
10363 .setattr = nfs4_proc_setattr,
10364 .lookup = nfs4_proc_lookup,
Jeff Layton5b5faaf2017-06-29 06:34:52 -070010365 .lookupp = nfs4_proc_lookupp,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010366 .access = nfs4_proc_access,
10367 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010368 .create = nfs4_proc_create,
10369 .remove = nfs4_proc_remove,
10370 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -040010371 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010372 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -040010373 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -040010374 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -040010375 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010376 .link = nfs4_proc_link,
10377 .symlink = nfs4_proc_symlink,
10378 .mkdir = nfs4_proc_mkdir,
Trond Myklebust912678d2018-03-20 16:43:15 -040010379 .rmdir = nfs4_proc_rmdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010380 .readdir = nfs4_proc_readdir,
10381 .mknod = nfs4_proc_mknod,
10382 .statfs = nfs4_proc_statfs,
10383 .fsinfo = nfs4_proc_fsinfo,
10384 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -040010385 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010386 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -040010387 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010388 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -050010389 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010390 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -050010391 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010392 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -040010393 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -050010394 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010395 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +000010396 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -040010397 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -040010398 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -040010399 .have_delegation = nfs4_have_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -040010400 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +000010401 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -040010402 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -040010403 .create_server = nfs4_create_server,
10404 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010405};
10406
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000010407static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +010010408 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000010409 .list = nfs4_xattr_list_nfs4_acl,
10410 .get = nfs4_xattr_get_nfs4_acl,
10411 .set = nfs4_xattr_set_nfs4_acl,
10412};
10413
Frank van der Linden012a2112020-06-23 22:39:03 +000010414#ifdef CONFIG_NFS_V4_2
10415static const struct xattr_handler nfs4_xattr_nfs4_user_handler = {
10416 .prefix = XATTR_USER_PREFIX,
10417 .get = nfs4_xattr_get_nfs4_user,
10418 .set = nfs4_xattr_set_nfs4_user,
10419};
10420#endif
10421
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000010422const struct xattr_handler *nfs4_xattr_handlers[] = {
10423 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -040010424#ifdef CONFIG_NFS_V4_SECURITY_LABEL
10425 &nfs4_xattr_nfs4_label_handler,
10426#endif
Frank van der Linden012a2112020-06-23 22:39:03 +000010427#ifdef CONFIG_NFS_V4_2
10428 &nfs4_xattr_nfs4_user_handler,
10429#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000010430 NULL
10431};
10432
Linus Torvalds1da177e2005-04-16 15:20:36 -070010433/*
10434 * Local variables:
10435 * c-basic-offset: 8
10436 * End:
10437 */