blob: 2215f20e0e787ccf4f60aea38b7d3715762a6936 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/nfs/nfs4proc.c
3 *
4 * Client-side procedure declarations for NFSv4.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/delay.h>
40#include <linux/errno.h>
41#include <linux/string.h>
Trond Myklebust652f89f2011-12-09 19:05:58 -050042#include <linux/ratelimit.h>
43#include <linux/printk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/sunrpc/clnt.h>
46#include <linux/nfs.h>
47#include <linux/nfs4.h>
48#include <linux/nfs_fs.h>
49#include <linux/nfs_page.h>
Bryan Schumaker9b7160c2011-04-13 14:31:30 -040050#include <linux/nfs_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070052#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040053#include <linux/module.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000054#include <linux/xattr.h>
Andy Adamsonc7a360b2011-01-25 19:15:32 -050055#include <linux/utsname.h>
Jeff Laytond3103102011-12-01 22:44:39 +010056#include <linux/freezer.h>
Jeff Layton1eb5d982018-01-09 08:21:17 -050057#include <linux/iversion.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Trond Myklebust4ce79712005-06-22 17:16:21 +000059#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050061#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050062#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040063#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040064#include "pnfs.h"
Chuck Leverf0920752012-05-21 22:45:41 -040065#include "netns.h"
Trond Myklebust39d43d12020-10-07 18:24:18 -040066#include "sysfs.h"
Anna Schumaker40c64c22015-04-15 13:00:05 -040067#include "nfs4idmap.h"
Trond Myklebust73e39aa2012-11-26 12:49:34 -050068#include "nfs4session.h"
David Howellsde242c02012-12-20 21:52:38 +000069#include "fscache.h"
Frank van der Linden012a2112020-06-23 22:39:03 +000070#include "nfs42.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Trond Myklebustc6d01c62013-08-09 11:51:26 -040072#include "nfs4trace.h"
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#define NFSDBG_FACILITY NFSDBG_PROC
75
Trond Myklebust30846df2018-04-07 13:44:28 -040076#define NFS4_BITMASK_SZ 3
77
Trond Myklebust2066fe82006-09-15 08:30:46 -040078#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079#define NFS4_POLL_RETRY_MAX (15*HZ)
80
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +020081/* file attributes which can be mapped to nfs attributes */
82#define NFS4_VALID_ATTRS (ATTR_MODE \
83 | ATTR_UID \
84 | ATTR_GID \
85 | ATTR_SIZE \
86 | ATTR_ATIME \
87 | ATTR_MTIME \
88 | ATTR_CTIME \
89 | ATTR_ATIME_SET \
90 | ATTR_MTIME_SET)
91
Trond Myklebustcdd4e682006-01-03 09:55:12 +010092struct nfs4_opendata;
Alexandros Batsakisb2579572009-12-14 21:27:57 -080093static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Chuck Lever81934dd2012-03-01 17:01:57 -050095static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
Trond Myklebusta841b542018-04-07 13:50:59 -040096static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label, struct inode *inode);
NeilBrowna52458b2018-12-03 11:30:31 +110097static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
Trond Myklebust0ab64e02010-04-16 16:22:51 -040098 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +110099 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
David Quigley1775fd32013-05-22 12:50:42 -0400100 struct nfs4_label *olabel);
Bryan Schumakerf062eb62011-06-02 14:59:10 -0400101#ifdef CONFIG_NFS_V4_1
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400102static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +1100103 const struct cred *cred,
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400104 struct nfs4_slot *slot,
105 bool is_privileged);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -0400106static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
NeilBrowna52458b2018-12-03 11:30:31 +1100107 const struct cred *);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -0400108static int nfs41_free_stateid(struct nfs_server *, const nfs4_stateid *,
NeilBrowna52458b2018-12-03 11:30:31 +1100109 const struct cred *, bool);
Bryan Schumakerf062eb62011-06-02 14:59:10 -0400110#endif
Trond Myklebust332d1a02021-03-25 18:15:36 -0400111static void nfs4_bitmask_set(__u32 bitmask[NFS4_BITMASK_SZ],
112 const __u32 *src, struct inode *inode,
113 struct nfs_server *server,
114 struct nfs4_label *label);
David Quigleyaa9c2662013-05-22 12:50:44 -0400115
116#ifdef CONFIG_NFS_V4_SECURITY_LABEL
117static inline struct nfs4_label *
118nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
119 struct iattr *sattr, struct nfs4_label *label)
120{
121 int err;
122
123 if (label == NULL)
124 return NULL;
125
126 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
127 return NULL;
128
David Quigleyaa9c2662013-05-22 12:50:44 -0400129 err = security_dentry_init_security(dentry, sattr->ia_mode,
130 &dentry->d_name, (void **)&label->label, &label->len);
131 if (err == 0)
132 return label;
133
134 return NULL;
135}
136static inline void
137nfs4_label_release_security(struct nfs4_label *label)
138{
139 if (label)
140 security_release_secctx(label->label, label->len);
141}
142static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
143{
144 if (label)
145 return server->attr_bitmask;
146
147 return server->attr_bitmask_nl;
148}
149#else
150static inline struct nfs4_label *
151nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
152 struct iattr *sattr, struct nfs4_label *l)
153{ return NULL; }
154static inline void
155nfs4_label_release_security(struct nfs4_label *label)
156{ return; }
157static inline u32 *
158nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
159{ return server->attr_bitmask; }
160#endif
161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -0500163static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{
Trond Myklebust52567b02009-10-23 14:46:42 -0400165 if (err >= -1000)
166 return err;
167 switch (err) {
168 case -NFS4ERR_RESOURCE:
Weston Andros Adamson30005122013-02-28 20:30:10 -0500169 case -NFS4ERR_LAYOUTTRYLATER:
170 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebust52567b02009-10-23 14:46:42 -0400171 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000172 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson88975382013-08-13 16:37:38 -0400173 case -NFS4ERR_WRONG_CRED:
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000174 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800175 case -NFS4ERR_BADOWNER:
176 case -NFS4ERR_BADNAME:
177 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400178 case -NFS4ERR_SHARE_DENIED:
179 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400180 case -NFS4ERR_MINOR_VERS_MISMATCH:
181 return -EPROTONOSUPPORT;
Trond Myklebust6e3cf242013-03-23 15:22:45 -0400182 case -NFS4ERR_FILE_OPEN:
183 return -EBUSY;
Trond Myklebust9fff59e2020-11-02 20:11:32 -0500184 case -NFS4ERR_NOT_SAME:
185 return -ENOTSYNC;
Trond Myklebust52567b02009-10-23 14:46:42 -0400186 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700188 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400189 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400191 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192}
193
194/*
195 * This is our standard bitmap for GETATTR requests.
196 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400197const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 FATTR4_WORD0_TYPE
199 | FATTR4_WORD0_CHANGE
200 | FATTR4_WORD0_SIZE
201 | FATTR4_WORD0_FSID
202 | FATTR4_WORD0_FILEID,
203 FATTR4_WORD1_MODE
204 | FATTR4_WORD1_NUMLINKS
205 | FATTR4_WORD1_OWNER
206 | FATTR4_WORD1_OWNER_GROUP
207 | FATTR4_WORD1_RAWDEV
208 | FATTR4_WORD1_SPACE_USED
209 | FATTR4_WORD1_TIME_ACCESS
210 | FATTR4_WORD1_TIME_METADATA
Anna Schumakerea96d1e2015-04-03 14:35:59 -0400211 | FATTR4_WORD1_TIME_MODIFY
212 | FATTR4_WORD1_MOUNTED_ON_FILEID,
David Quigleyaa9c2662013-05-22 12:50:44 -0400213#ifdef CONFIG_NFS_V4_SECURITY_LABEL
214 FATTR4_WORD2_SECURITY_LABEL
215#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216};
217
Trond Myklebust1549210f2012-06-05 09:16:47 -0400218static const u32 nfs4_pnfs_open_bitmap[3] = {
219 FATTR4_WORD0_TYPE
220 | FATTR4_WORD0_CHANGE
221 | FATTR4_WORD0_SIZE
222 | FATTR4_WORD0_FSID
223 | FATTR4_WORD0_FILEID,
224 FATTR4_WORD1_MODE
225 | FATTR4_WORD1_NUMLINKS
226 | FATTR4_WORD1_OWNER
227 | FATTR4_WORD1_OWNER_GROUP
228 | FATTR4_WORD1_RAWDEV
229 | FATTR4_WORD1_SPACE_USED
230 | FATTR4_WORD1_TIME_ACCESS
231 | FATTR4_WORD1_TIME_METADATA
232 | FATTR4_WORD1_TIME_MODIFY,
233 FATTR4_WORD2_MDSTHRESHOLD
Trond Myklebust95864c92015-12-26 15:06:03 -0500234#ifdef CONFIG_NFS_V4_SECURITY_LABEL
235 | FATTR4_WORD2_SECURITY_LABEL
236#endif
Trond Myklebust1549210f2012-06-05 09:16:47 -0400237};
238
Andy Adamsone23008e2012-10-02 21:07:32 -0400239static const u32 nfs4_open_noattr_bitmap[3] = {
240 FATTR4_WORD0_TYPE
Andy Adamsone23008e2012-10-02 21:07:32 -0400241 | FATTR4_WORD0_FILEID,
242};
243
David Quigleya09df2c2013-05-22 12:50:41 -0400244const u32 nfs4_statfs_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 FATTR4_WORD0_FILES_AVAIL
246 | FATTR4_WORD0_FILES_FREE
247 | FATTR4_WORD0_FILES_TOTAL,
248 FATTR4_WORD1_SPACE_AVAIL
249 | FATTR4_WORD1_SPACE_FREE
250 | FATTR4_WORD1_SPACE_TOTAL
251};
252
David Quigleya09df2c2013-05-22 12:50:41 -0400253const u32 nfs4_pathconf_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 FATTR4_WORD0_MAXLINK
255 | FATTR4_WORD0_MAXNAME,
256 0
257};
258
Fred Isamandae100c2011-07-30 20:52:37 -0400259const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 | FATTR4_WORD0_MAXREAD
261 | FATTR4_WORD0_MAXWRITE
262 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700263 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400264 | FATTR4_WORD1_FS_LAYOUT_TYPES,
265 FATTR4_WORD2_LAYOUT_BLKSIZE
Peng Tao2a92ee92015-09-26 02:24:37 +0800266 | FATTR4_WORD2_CLONE_BLKSIZE
Trond Myklebust7f08a332021-03-26 09:50:19 -0400267 | FATTR4_WORD2_CHANGE_ATTR_TYPE
Frank van der Lindenb78ef842020-06-23 22:38:55 +0000268 | FATTR4_WORD2_XATTR_SUPPORT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269};
270
David Quigleya09df2c2013-05-22 12:50:41 -0400271const u32 nfs4_fs_locations_bitmap[3] = {
Chuck Leverc05cefc2017-11-05 15:45:22 -0500272 FATTR4_WORD0_CHANGE
Manoj Naik830b8e32006-06-09 09:34:25 -0400273 | FATTR4_WORD0_SIZE
274 | FATTR4_WORD0_FSID
275 | FATTR4_WORD0_FILEID
276 | FATTR4_WORD0_FS_LOCATIONS,
Chuck Leverc05cefc2017-11-05 15:45:22 -0500277 FATTR4_WORD1_OWNER
Manoj Naik830b8e32006-06-09 09:34:25 -0400278 | FATTR4_WORD1_OWNER_GROUP
279 | FATTR4_WORD1_RAWDEV
280 | FATTR4_WORD1_SPACE_USED
281 | FATTR4_WORD1_TIME_ACCESS
282 | FATTR4_WORD1_TIME_METADATA
283 | FATTR4_WORD1_TIME_MODIFY
David Quigleya09df2c2013-05-22 12:50:41 -0400284 | FATTR4_WORD1_MOUNTED_ON_FILEID,
Manoj Naik830b8e32006-06-09 09:34:25 -0400285};
286
Trond Myklebust30846df2018-04-07 13:44:28 -0400287static void nfs4_bitmap_copy_adjust(__u32 *dst, const __u32 *src,
Trond Myklebusta71029b2021-04-10 00:23:03 -0400288 struct inode *inode, unsigned long flags)
Trond Myklebust30846df2018-04-07 13:44:28 -0400289{
290 unsigned long cache_validity;
291
292 memcpy(dst, src, NFS4_BITMASK_SZ*sizeof(*dst));
293 if (!inode || !nfs4_have_delegation(inode, FMODE_READ))
294 return;
295
Trond Myklebusta71029b2021-04-10 00:23:03 -0400296 cache_validity = READ_ONCE(NFS_I(inode)->cache_validity) | flags;
Trond Myklebust30846df2018-04-07 13:44:28 -0400297
Trond Myklebusta71029b2021-04-10 00:23:03 -0400298 /* Remove the attributes over which we have full control */
299 dst[1] &= ~FATTR4_WORD1_RAWDEV;
Trond Myklebust30846df2018-04-07 13:44:28 -0400300 if (!(cache_validity & NFS_INO_INVALID_SIZE))
301 dst[0] &= ~FATTR4_WORD0_SIZE;
302
303 if (!(cache_validity & NFS_INO_INVALID_CHANGE))
304 dst[0] &= ~FATTR4_WORD0_CHANGE;
Trond Myklebust30846df2018-04-07 13:44:28 -0400305
Trond Myklebust720869e2021-04-13 09:41:16 -0400306 if (!(cache_validity & NFS_INO_INVALID_MODE))
307 dst[1] &= ~FATTR4_WORD1_MODE;
Trond Myklebusta71029b2021-04-10 00:23:03 -0400308 if (!(cache_validity & NFS_INO_INVALID_OTHER))
Trond Myklebust720869e2021-04-13 09:41:16 -0400309 dst[1] &= ~(FATTR4_WORD1_OWNER | FATTR4_WORD1_OWNER_GROUP);
Trond Myklebust30846df2018-04-07 13:44:28 -0400310}
311
Al Virobc4785c2006-10-19 23:28:51 -0700312static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 struct nfs4_readdir_arg *readdir)
314{
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400315 unsigned int attrs = FATTR4_WORD0_FILEID | FATTR4_WORD0_TYPE;
Al Viro0dbb4c62006-10-19 23:28:49 -0700316 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000319 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
321 return;
322 }
323
324 readdir->cookie = 0;
325 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
326 if (cookie == 2)
327 return;
328
329 /*
330 * NFSv4 servers do not return entries for '.' and '..'
331 * Therefore, we fake these entries here. We let '.'
332 * have cookie 0 and '..' have cookie 1. Note that
333 * when talking to the server, we always send cookie 0
334 * instead of 1 or 2.
335 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800336 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
338 if (cookie == 0) {
339 *p++ = xdr_one; /* next */
340 *p++ = xdr_zero; /* cookie, first word */
341 *p++ = xdr_one; /* cookie, second word */
342 *p++ = xdr_one; /* entry len */
343 memcpy(p, ".\0\0\0", 4); /* entry */
344 p++;
345 *p++ = xdr_one; /* bitmap length */
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400346 *p++ = htonl(attrs); /* bitmap */
347 *p++ = htonl(12); /* attribute buffer length */
348 *p++ = htonl(NF4DIR);
David Howells2b0143b2015-03-17 22:25:59 +0000349 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 }
351
352 *p++ = xdr_one; /* next */
353 *p++ = xdr_zero; /* cookie, first word */
354 *p++ = xdr_two; /* cookie, second word */
355 *p++ = xdr_two; /* entry len */
356 memcpy(p, "..\0\0", 4); /* entry */
357 p++;
358 *p++ = xdr_one; /* bitmap length */
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400359 *p++ = htonl(attrs); /* bitmap */
360 *p++ = htonl(12); /* attribute buffer length */
361 *p++ = htonl(NF4DIR);
David Howells2b0143b2015-03-17 22:25:59 +0000362 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
364 readdir->pgbase = (char *)p - (char *)start;
365 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800366 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367}
368
Trond Myklebust26d36302016-09-22 13:39:05 -0400369static void nfs4_test_and_free_stateid(struct nfs_server *server,
370 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +1100371 const struct cred *cred)
Trond Myklebust26d36302016-09-22 13:39:05 -0400372{
373 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
374
375 ops->test_and_free_expired(server, stateid, cred);
376}
377
378static void __nfs4_free_revoked_stateid(struct nfs_server *server,
379 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +1100380 const struct cred *cred)
Trond Myklebust26d36302016-09-22 13:39:05 -0400381{
382 stateid->type = NFS4_REVOKED_STATEID_TYPE;
383 nfs4_test_and_free_stateid(server, stateid, cred);
384}
385
386static void nfs4_free_revoked_stateid(struct nfs_server *server,
387 const nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +1100388 const struct cred *cred)
Trond Myklebust26d36302016-09-22 13:39:05 -0400389{
390 nfs4_stateid tmp;
391
392 nfs4_stateid_copy(&tmp, stateid);
393 __nfs4_free_revoked_stateid(server, &tmp, cred);
394}
395
NeilBrown8478eaa2014-09-18 16:09:27 +1000396static long nfs4_update_delay(long *timeout)
397{
398 long ret;
399 if (!timeout)
400 return NFS4_POLL_RETRY_MAX;
401 if (*timeout <= 0)
402 *timeout = NFS4_POLL_RETRY_MIN;
403 if (*timeout > NFS4_POLL_RETRY_MAX)
404 *timeout = NFS4_POLL_RETRY_MAX;
405 ret = *timeout;
406 *timeout <<= 1;
407 return ret;
408}
409
Trond Myklebust0688e642019-04-07 13:59:09 -0400410static int nfs4_delay_killable(long *timeout)
Trond Myklebust65de8722008-12-23 15:21:44 -0500411{
Trond Myklebust65de8722008-12-23 15:21:44 -0500412 might_sleep();
413
NeilBrown8478eaa2014-09-18 16:09:27 +1000414 freezable_schedule_timeout_killable_unsafe(
415 nfs4_update_delay(timeout));
Trond Myklebust0688e642019-04-07 13:59:09 -0400416 if (!__fatal_signal_pending(current))
417 return 0;
418 return -EINTR;
419}
420
421static int nfs4_delay_interruptible(long *timeout)
422{
423 might_sleep();
424
He Zhe59679d92020-07-06 17:52:24 +0800425 freezable_schedule_timeout_interruptible_unsafe(nfs4_update_delay(timeout));
Trond Myklebust0688e642019-04-07 13:59:09 -0400426 if (!signal_pending(current))
427 return 0;
428 return __fatal_signal_pending(current) ? -EINTR :-ERESTARTSYS;
429}
430
431static int nfs4_delay(long *timeout, bool interruptible)
432{
433 if (interruptible)
434 return nfs4_delay_interruptible(timeout);
435 return nfs4_delay_killable(timeout);
Trond Myklebust65de8722008-12-23 15:21:44 -0500436}
437
Trond Myklebust50c80002019-07-11 19:02:18 -0400438static const nfs4_stateid *
439nfs4_recoverable_stateid(const nfs4_stateid *stateid)
440{
441 if (!stateid)
442 return NULL;
443 switch (stateid->type) {
444 case NFS4_OPEN_STATEID_TYPE:
445 case NFS4_LOCK_STATEID_TYPE:
446 case NFS4_DELEGATION_STATEID_TYPE:
447 return stateid;
448 default:
449 break;
450 }
451 return NULL;
452}
453
Trond Myklebust65de8722008-12-23 15:21:44 -0500454/* This is the error handling routine for processes that are allowed
455 * to sleep.
456 */
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400457static int nfs4_do_handle_exception(struct nfs_server *server,
458 int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500459{
460 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500461 struct nfs4_state *state = exception->state;
Trond Myklebust50c80002019-07-11 19:02:18 -0400462 const nfs4_stateid *stateid;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500463 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500464 int ret = errorcode;
465
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400466 exception->delay = 0;
467 exception->recovering = 0;
Trond Myklebust65de8722008-12-23 15:21:44 -0500468 exception->retry = 0;
Trond Myklebust272289a2016-09-22 13:39:15 -0400469
Trond Myklebust50c80002019-07-11 19:02:18 -0400470 stateid = nfs4_recoverable_stateid(exception->stateid);
Trond Myklebust272289a2016-09-22 13:39:15 -0400471 if (stateid == NULL && state != NULL)
Trond Myklebust50c80002019-07-11 19:02:18 -0400472 stateid = nfs4_recoverable_stateid(&state->stateid);
Trond Myklebust272289a2016-09-22 13:39:15 -0400473
Trond Myklebust65de8722008-12-23 15:21:44 -0500474 switch(errorcode) {
475 case 0:
476 return 0;
Trond Myklebustcf61eb22018-05-29 22:06:08 -0400477 case -NFS4ERR_BADHANDLE:
478 case -ESTALE:
479 if (inode != NULL && S_ISREG(inode->i_mode))
480 pnfs_destroy_layout(NFS_I(inode));
481 break;
Trond Myklebust5ba12442015-06-16 11:26:35 -0400482 case -NFS4ERR_DELEG_REVOKED:
483 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust272289a2016-09-22 13:39:15 -0400484 case -NFS4ERR_EXPIRED:
Trond Myklebust5ba12442015-06-16 11:26:35 -0400485 case -NFS4ERR_BAD_STATEID:
Olga Kornievskaiafefa1a82019-06-14 14:22:12 -0400486 case -NFS4ERR_PARTNER_NO_AUTH:
Trond Myklebust272289a2016-09-22 13:39:15 -0400487 if (inode != NULL && stateid != NULL) {
488 nfs_inode_find_state_and_recover(inode,
489 stateid);
490 goto wait_on_recovery;
491 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500492 fallthrough;
Trond Myklebust272289a2016-09-22 13:39:15 -0400493 case -NFS4ERR_OPENMODE:
Trond Myklebust8487c472016-06-26 08:44:35 -0400494 if (inode) {
495 int err;
496
497 err = nfs_async_inode_return_delegation(inode,
498 stateid);
499 if (err == 0)
500 goto wait_on_recovery;
501 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
502 exception->retry = 1;
503 break;
504 }
505 }
Trond Myklebust3114ea72012-03-07 16:39:06 -0500506 if (state == NULL)
507 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400508 ret = nfs4_schedule_stateid_recovery(server, state);
509 if (ret < 0)
510 break;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500511 goto wait_on_recovery;
Trond Myklebust65de8722008-12-23 15:21:44 -0500512 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500513 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500514 nfs4_schedule_lease_recovery(clp);
515 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -0400516 case -NFS4ERR_MOVED:
517 ret = nfs4_schedule_migration_recovery(server);
518 if (ret < 0)
519 break;
520 goto wait_on_recovery;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -0400521 case -NFS4ERR_LEASE_MOVED:
522 nfs4_schedule_lease_moved_recovery(clp);
523 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500524#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400525 case -NFS4ERR_BADSESSION:
526 case -NFS4ERR_BADSLOT:
527 case -NFS4ERR_BAD_HIGH_SLOT:
528 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
529 case -NFS4ERR_DEADSESSION:
530 case -NFS4ERR_SEQ_FALSE_RETRY:
531 case -NFS4ERR_SEQ_MISORDERED:
Trond Myklebust5c441542019-11-13 08:34:00 +0100532 /* Handled in nfs41_sequence_process() */
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400533 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500534#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500535 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500536 if (exception->timeout > HZ) {
537 /* We have retried a decent amount, time to
538 * fail
539 */
540 ret = -EBUSY;
541 break;
542 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500543 fallthrough;
Trond Myklebust65de8722008-12-23 15:21:44 -0500544 case -NFS4ERR_DELAY:
Trond Myklebust2598ed32015-09-20 16:10:18 -0400545 nfs_inc_server_stats(server, NFSIOS_DELAY);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500546 fallthrough;
Trond Myklebust2598ed32015-09-20 16:10:18 -0400547 case -NFS4ERR_GRACE:
Trond Myklebuste85d7ee2016-07-14 18:46:24 -0400548 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -0400549 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400550 exception->delay = 1;
551 return 0;
552
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400553 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500554 case -NFS4ERR_OLD_STATEID:
555 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800556 break;
557 case -NFS4ERR_BADOWNER:
558 /* The following works around a Linux server bug! */
559 case -NFS4ERR_BADNAME:
560 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
561 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
562 exception->retry = 1;
563 printk(KERN_WARNING "NFS: v4 server %s "
564 "does not accept raw "
565 "uid/gids. "
566 "Reenabling the idmapper.\n",
567 server->nfs_client->cl_hostname);
568 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500569 }
570 /* We failed to handle the error */
571 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500572wait_on_recovery:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400573 exception->recovering = 1;
574 return 0;
575}
576
577/* This is the error handling routine for processes that are allowed
578 * to sleep.
579 */
580int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
581{
582 struct nfs_client *clp = server->nfs_client;
583 int ret;
584
585 ret = nfs4_do_handle_exception(server, errorcode, exception);
586 if (exception->delay) {
Trond Myklebust0688e642019-04-07 13:59:09 -0400587 ret = nfs4_delay(&exception->timeout,
588 exception->interruptible);
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400589 goto out_retry;
590 }
591 if (exception->recovering) {
592 ret = nfs4_wait_clnt_recover(clp);
593 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
594 return -EIO;
595 goto out_retry;
596 }
597 return ret;
598out_retry:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500599 if (ret == 0)
600 exception->retry = 1;
601 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500602}
603
Trond Myklebust037fc982015-09-20 15:51:00 -0400604static int
605nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
606 int errorcode, struct nfs4_exception *exception)
607{
608 struct nfs_client *clp = server->nfs_client;
609 int ret;
610
611 ret = nfs4_do_handle_exception(server, errorcode, exception);
612 if (exception->delay) {
613 rpc_delay(task, nfs4_update_delay(&exception->timeout));
614 goto out_retry;
615 }
616 if (exception->recovering) {
617 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
618 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
619 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
620 goto out_retry;
621 }
622 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
623 ret = -EIO;
624 return ret;
625out_retry:
Bill Baker0f90be12018-06-19 16:24:58 -0500626 if (ret == 0) {
Trond Myklebust037fc982015-09-20 15:51:00 -0400627 exception->retry = 1;
Bill Baker0f90be12018-06-19 16:24:58 -0500628 /*
629 * For NFS4ERR_MOVED, the client transport will need to
630 * be recomputed after migration recovery has completed.
631 */
632 if (errorcode == -NFS4ERR_MOVED)
633 rpc_task_release_transport(task);
634 }
Trond Myklebust037fc982015-09-20 15:51:00 -0400635 return ret;
636}
637
Olga Kornievskaia0f913a52018-07-09 15:13:33 -0400638int
Trond Myklebust037fc982015-09-20 15:51:00 -0400639nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
640 struct nfs4_state *state, long *timeout)
641{
642 struct nfs4_exception exception = {
643 .state = state,
644 };
645
646 if (task->tk_status >= 0)
647 return 0;
648 if (timeout)
649 exception.timeout = *timeout;
650 task->tk_status = nfs4_async_handle_exception(task, server,
651 task->tk_status,
652 &exception);
653 if (exception.delay && timeout)
654 *timeout = exception.timeout;
655 if (exception.retry)
656 return -EAGAIN;
657 return 0;
658}
659
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400660/*
661 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
662 * or 'false' otherwise.
663 */
664static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
665{
666 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
Anna Schumakereeea5362017-01-11 16:01:21 -0500667 return (flavor == RPC_AUTH_GSS_KRB5I) || (flavor == RPC_AUTH_GSS_KRB5P);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400668}
Trond Myklebust65de8722008-12-23 15:21:44 -0500669
Trond Myklebust452e9352010-07-31 14:29:06 -0400670static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 spin_lock(&clp->cl_lock);
673 if (time_before(clp->cl_last_renewal,timestamp))
674 clp->cl_last_renewal = timestamp;
675 spin_unlock(&clp->cl_lock);
676}
677
Trond Myklebust452e9352010-07-31 14:29:06 -0400678static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
679{
Trond Myklebustbe824162015-07-05 14:50:46 -0400680 struct nfs_client *clp = server->nfs_client;
681
682 if (!nfs4_has_session(clp))
683 do_renew_lease(clp, timestamp);
Trond Myklebust452e9352010-07-31 14:29:06 -0400684}
685
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400686struct nfs4_call_sync_data {
687 const struct nfs_server *seq_server;
688 struct nfs4_sequence_args *seq_args;
689 struct nfs4_sequence_res *seq_res;
690};
691
Trond Myklebustbe3a5d22015-06-23 19:51:55 +0800692void nfs4_init_sequence(struct nfs4_sequence_args *args,
Anna Schumakerfba83f32018-05-04 16:22:50 -0400693 struct nfs4_sequence_res *res, int cache_reply,
694 int privileged)
Chuck Levera9c92d62013-08-09 12:48:18 -0400695{
696 args->sa_slot = NULL;
697 args->sa_cache_this = cache_reply;
Anna Schumakerfba83f32018-05-04 16:22:50 -0400698 args->sa_privileged = privileged;
Chuck Levera9c92d62013-08-09 12:48:18 -0400699
700 res->sr_slot = NULL;
701}
702
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400703static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
Chuck Lever3bd23842013-08-09 12:49:19 -0400704{
705 struct nfs4_slot *slot = res->sr_slot;
706 struct nfs4_slot_table *tbl;
707
Chuck Lever3bd23842013-08-09 12:49:19 -0400708 tbl = slot->table;
709 spin_lock(&tbl->slot_tbl_lock);
710 if (!nfs41_wake_and_assign_slot(tbl, slot))
711 nfs4_free_slot(tbl, slot);
712 spin_unlock(&tbl->slot_tbl_lock);
713
714 res->sr_slot = NULL;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400715}
716
717static int nfs40_sequence_done(struct rpc_task *task,
718 struct nfs4_sequence_res *res)
719{
720 if (res->sr_slot != NULL)
721 nfs40_sequence_free_slot(res);
Chuck Lever3bd23842013-08-09 12:49:19 -0400722 return 1;
723}
724
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400725#if defined(CONFIG_NFS_V4_1)
726
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400727static void nfs41_release_slot(struct nfs4_slot *slot)
Andy Adamson13615872009-04-01 09:22:17 -0400728{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500729 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400730 struct nfs4_slot_table *tbl;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500731 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400732
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400733 if (!slot)
734 return;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500735 tbl = slot->table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500736 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500737
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400738 /* Bump the slot sequence number */
739 if (slot->seq_done)
740 slot->seq_nr++;
741 slot->seq_done = 0;
742
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500743 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500744 /* Be nice to the server: try to ensure that the last transmitted
745 * value for highest_user_slotid <= target_highest_slotid
746 */
747 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
748 send_new_highest_used_slotid = true;
749
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500750 if (nfs41_wake_and_assign_slot(tbl, slot)) {
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500751 send_new_highest_used_slotid = false;
752 goto out_unlock;
753 }
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500754 nfs4_free_slot(tbl, slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500755
756 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
757 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500758out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500759 spin_unlock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500760 if (send_new_highest_used_slotid)
Anna Schumaker3f10a6a2015-07-13 14:01:31 -0400761 nfs41_notify_server(session->clp);
Trond Myklebust045d2a62016-08-28 13:25:43 -0400762 if (waitqueue_active(&tbl->slot_waitq))
763 wake_up_all(&tbl->slot_waitq);
Andy Adamson13615872009-04-01 09:22:17 -0400764}
765
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400766static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
767{
768 nfs41_release_slot(res->sr_slot);
769 res->sr_slot = NULL;
770}
771
Trond Myklebust3453d572018-06-20 17:53:34 -0400772static void nfs4_slot_sequence_record_sent(struct nfs4_slot *slot,
773 u32 seqnr)
774{
775 if ((s32)(seqnr - slot->seq_nr_highest_sent) > 0)
776 slot->seq_nr_highest_sent = seqnr;
777}
778static void nfs4_slot_sequence_acked(struct nfs4_slot *slot,
779 u32 seqnr)
780{
781 slot->seq_nr_highest_sent = seqnr;
782 slot->seq_nr_last_acked = seqnr;
783}
784
Anna Schumaker913fadc2020-07-08 10:33:40 -0400785static void nfs4_probe_sequence(struct nfs_client *client, const struct cred *cred,
786 struct nfs4_slot *slot)
787{
788 struct rpc_task *task = _nfs41_proc_sequence(client, cred, slot, true);
789 if (!IS_ERR(task))
790 rpc_put_task_async(task);
791}
792
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400793static int nfs41_sequence_process(struct rpc_task *task,
794 struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400795{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500796 struct nfs4_session *session;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500797 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400798 struct nfs_client *clp;
Trond Myklebust5c441542019-11-13 08:34:00 +0100799 int status;
Trond Myklebust85563072012-12-11 10:31:12 -0500800 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400801
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500802 if (slot == NULL)
803 goto out_noaction;
Bryan Schumaker468f8612011-04-18 15:57:32 -0400804 /* don't increment the sequence number if the task wasn't sent */
Trond Myklebustc71c46f2019-03-01 11:40:05 -0500805 if (!RPC_WAS_SENT(task) || slot->seq_done)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400806 goto out;
807
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500808 session = slot->table->session;
Anna Schumaker913fadc2020-07-08 10:33:40 -0400809 clp = session->clp;
Trond Myklebust933602e2012-11-16 12:12:38 -0500810
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400811 trace_nfs4_sequence_done(session, res);
Trond Myklebust5c441542019-11-13 08:34:00 +0100812
813 status = res->sr_status;
814 if (task->tk_status == -NFS4ERR_DEADSESSION)
815 status = -NFS4ERR_DEADSESSION;
816
Andy Adamson691daf32009-12-04 15:55:39 -0500817 /* Check the SEQUENCE operation status */
Trond Myklebust5c441542019-11-13 08:34:00 +0100818 switch (status) {
Trond Myklebust14516c32010-07-31 14:29:06 -0400819 case 0:
Trond Myklebust3453d572018-06-20 17:53:34 -0400820 /* Mark this sequence number as having been acked */
821 nfs4_slot_sequence_acked(slot, slot->seq_nr);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400822 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400823 slot->seq_done = 1;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500824 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500825 /* Check sequence flags */
Trond Myklebust0a014a42016-09-22 13:38:51 -0400826 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
827 !!slot->privileged);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500828 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400829 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500830 case 1:
831 /*
832 * sr_status remains 1 if an RPC level error occurred.
833 * The server may or may not have processed the sequence
834 * operation..
Trond Myklebustac20d162012-12-15 15:36:07 -0500835 */
Trond Myklebust3453d572018-06-20 17:53:34 -0400836 nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
837 slot->seq_done = 1;
Trond Myklebustac20d162012-12-15 15:36:07 -0500838 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400839 case -NFS4ERR_DELAY:
840 /* The server detected a resend of the RPC call and
841 * returned NFS4ERR_DELAY as per Section 2.10.6.2
842 * of RFC5661.
843 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500844 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400845 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500846 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500847 slot->seq_nr);
Trond Myklebust3453d572018-06-20 17:53:34 -0400848 nfs4_slot_sequence_acked(slot, slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400849 goto out_retry;
Trond Myklebustf9312a52018-06-09 19:10:31 -0400850 case -NFS4ERR_RETRY_UNCACHED_REP:
851 case -NFS4ERR_SEQ_FALSE_RETRY:
852 /*
853 * The server thinks we tried to replay a request.
854 * Retry the call after bumping the sequence ID.
855 */
Trond Myklebust3453d572018-06-20 17:53:34 -0400856 nfs4_slot_sequence_acked(slot, slot->seq_nr);
Trond Myklebustf9312a52018-06-09 19:10:31 -0400857 goto retry_new_seq;
Trond Myklebust85563072012-12-11 10:31:12 -0500858 case -NFS4ERR_BADSLOT:
859 /*
860 * The slot id we used was probably retired. Try again
861 * using a different slot id.
862 */
Trond Myklebust99589102018-06-09 12:50:50 -0400863 if (slot->slot_nr < slot->table->target_highest_slotid)
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400864 goto session_recover;
Trond Myklebuste8794442012-12-15 13:56:18 -0500865 goto retry_nowait;
866 case -NFS4ERR_SEQ_MISORDERED:
Trond Myklebust3453d572018-06-20 17:53:34 -0400867 nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
Trond Myklebuste8794442012-12-15 13:56:18 -0500868 /*
Trond Myklebust3453d572018-06-20 17:53:34 -0400869 * Were one or more calls using this slot interrupted?
870 * If the server never received the request, then our
Anna Schumaker913fadc2020-07-08 10:33:40 -0400871 * transmitted slot sequence number may be too high. However,
872 * if the server did receive the request then it might
873 * accidentally give us a reply with a mismatched operation.
874 * We can sort this out by sending a lone sequence operation
875 * to the server on the same slot.
Trond Myklebustac20d162012-12-15 15:36:07 -0500876 */
Trond Myklebust3453d572018-06-20 17:53:34 -0400877 if ((s32)(slot->seq_nr - slot->seq_nr_last_acked) > 1) {
878 slot->seq_nr--;
Anna Schumaker913fadc2020-07-08 10:33:40 -0400879 if (task->tk_msg.rpc_proc != &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE]) {
880 nfs4_probe_sequence(clp, task->tk_msg.rpc_cred, slot);
881 res->sr_slot = NULL;
882 }
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500883 goto retry_nowait;
884 }
Trond Myklebust3453d572018-06-20 17:53:34 -0400885 /*
886 * RFC5661:
887 * A retry might be sent while the original request is
888 * still in progress on the replier. The replier SHOULD
889 * deal with the issue by returning NFS4ERR_DELAY as the
890 * reply to SEQUENCE or CB_SEQUENCE operation, but
891 * implementations MAY return NFS4ERR_SEQ_MISORDERED.
892 *
893 * Restart the search after a delay.
894 */
895 slot->seq_nr = slot->seq_nr_highest_sent;
896 goto out_retry;
Trond Myklebust5c441542019-11-13 08:34:00 +0100897 case -NFS4ERR_BADSESSION:
898 case -NFS4ERR_DEADSESSION:
899 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
900 goto session_recover;
Trond Myklebust14516c32010-07-31 14:29:06 -0400901 default:
902 /* Just update the slot sequence no. */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400903 slot->seq_done = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400904 }
905out:
906 /* The session may be reset by one of the error handlers. */
907 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500908out_noaction:
Trond Myklebust85563072012-12-11 10:31:12 -0500909 return ret;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400910session_recover:
Trond Myklebust5c441542019-11-13 08:34:00 +0100911 nfs4_schedule_session_recovery(session, status);
912 dprintk("%s ERROR: %d Reset session\n", __func__, status);
913 nfs41_sequence_free_slot(res);
914 goto out;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400915retry_new_seq:
916 ++slot->seq_nr;
Trond Myklebuste8794442012-12-15 13:56:18 -0500917retry_nowait:
918 if (rpc_restart_call_prepare(task)) {
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400919 nfs41_sequence_free_slot(res);
Trond Myklebuste8794442012-12-15 13:56:18 -0500920 task->tk_status = 0;
921 ret = 0;
922 }
923 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400924out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400925 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400926 goto out;
927 rpc_delay(task, NFS4_POLL_RETRY_MAX);
928 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400929}
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400930
931int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
932{
933 if (!nfs41_sequence_process(task, res))
934 return 0;
935 if (res->sr_slot != NULL)
936 nfs41_sequence_free_slot(res);
937 return 1;
938
939}
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500940EXPORT_SYMBOL_GPL(nfs41_sequence_done);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400941
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400942static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
943{
944 if (res->sr_slot == NULL)
945 return 1;
946 if (res->sr_slot->table->session != NULL)
947 return nfs41_sequence_process(task, res);
948 return nfs40_sequence_done(task, res);
949}
950
951static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
952{
953 if (res->sr_slot != NULL) {
954 if (res->sr_slot->table->session != NULL)
955 nfs41_sequence_free_slot(res);
956 else
957 nfs40_sequence_free_slot(res);
958 }
959}
960
Peng Tao2c4b1312014-06-11 05:24:16 +0800961int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400962{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500963 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400964 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400965 if (!res->sr_slot->table->session)
966 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400967 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400968}
Peng Tao2c4b1312014-06-11 05:24:16 +0800969EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Trond Myklebustdf896452010-06-16 09:52:26 -0400970
Andy Adamsonce5039c2009-04-01 09:22:13 -0400971static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
972{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400973 struct nfs4_call_sync_data *data = calldata;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400974
Trond Myklebust035168ab2010-06-16 09:52:26 -0400975 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
976
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500977 nfs4_setup_sequence(data->seq_server->nfs_client,
978 data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400979}
980
Andy Adamson69ab40c2009-04-01 09:22:19 -0400981static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
982{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400983 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400984
Trond Myklebust14516c32010-07-31 14:29:06 -0400985 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400986}
987
Trond Myklebust17280172012-03-11 13:11:00 -0400988static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400989 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400990 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400991};
992
Chuck Lever3bd23842013-08-09 12:49:19 -0400993#else /* !CONFIG_NFS_V4_1 */
994
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400995static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
996{
997 return nfs40_sequence_done(task, res);
998}
999
1000static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
1001{
1002 if (res->sr_slot != NULL)
1003 nfs40_sequence_free_slot(res);
1004}
1005
Peng Tao2c4b1312014-06-11 05:24:16 +08001006int nfs4_sequence_done(struct rpc_task *task,
1007 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -04001008{
Chuck Lever3bd23842013-08-09 12:49:19 -04001009 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -04001010}
Peng Tao2c4b1312014-06-11 05:24:16 +08001011EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Chuck Lever3bd23842013-08-09 12:49:19 -04001012
1013#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001014
Trond Myklebustc1dffe02019-03-01 12:13:34 -05001015static void nfs41_sequence_res_init(struct nfs4_sequence_res *res)
1016{
1017 res->sr_timestamp = jiffies;
1018 res->sr_status_flags = 0;
1019 res->sr_status = 1;
1020}
1021
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04001022static
1023void nfs4_sequence_attach_slot(struct nfs4_sequence_args *args,
1024 struct nfs4_sequence_res *res,
1025 struct nfs4_slot *slot)
1026{
1027 if (!slot)
1028 return;
1029 slot->privileged = args->sa_privileged ? 1 : 0;
1030 args->sa_slot = slot;
1031
1032 res->sr_slot = slot;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04001033}
1034
1035int nfs4_setup_sequence(struct nfs_client *client,
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001036 struct nfs4_sequence_args *args,
1037 struct nfs4_sequence_res *res,
1038 struct rpc_task *task)
1039{
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001040 struct nfs4_session *session = nfs4_get_session(client);
Anna Schumaker76ee0352017-01-10 16:49:31 -05001041 struct nfs4_slot_table *tbl = client->cl_slot_tbl;
Anna Schumaker3d358082017-01-11 10:54:04 -05001042 struct nfs4_slot *slot;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001043
Anna Schumaker9dd9107f2017-01-10 12:01:46 -05001044 /* slot already allocated? */
1045 if (res->sr_slot != NULL)
1046 goto out_start;
1047
Trond Myklebust6b2e6852019-04-07 13:58:49 -04001048 if (session)
Anna Schumaker76ee0352017-01-10 16:49:31 -05001049 tbl = &session->fc_slot_table;
Anna Schumaker76ee0352017-01-10 16:49:31 -05001050
Trond Myklebust3453d572018-06-20 17:53:34 -04001051 spin_lock(&tbl->slot_tbl_lock);
1052 /* The state manager will wait until the slot table is empty */
1053 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
1054 goto out_sleep;
Anna Schumaker6994cdd2017-01-10 16:13:27 -05001055
Trond Myklebust3453d572018-06-20 17:53:34 -04001056 slot = nfs4_alloc_slot(tbl);
1057 if (IS_ERR(slot)) {
Trond Myklebust3453d572018-06-20 17:53:34 -04001058 if (slot == ERR_PTR(-ENOMEM))
Trond Myklebust6b2e6852019-04-07 13:58:49 -04001059 goto out_sleep_timeout;
Trond Myklebust3453d572018-06-20 17:53:34 -04001060 goto out_sleep;
Anna Schumaker3d358082017-01-11 10:54:04 -05001061 }
Trond Myklebust3453d572018-06-20 17:53:34 -04001062 spin_unlock(&tbl->slot_tbl_lock);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001063
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04001064 nfs4_sequence_attach_slot(args, res, slot);
Anna Schumaker3d358082017-01-11 10:54:04 -05001065
Anna Schumakerad05cc02017-01-11 13:37:06 -05001066 trace_nfs4_setup_sequence(session, args);
Anna Schumaker9dd9107f2017-01-10 12:01:46 -05001067out_start:
Trond Myklebustc1dffe02019-03-01 12:13:34 -05001068 nfs41_sequence_res_init(res);
Anna Schumaker9dd9107f2017-01-10 12:01:46 -05001069 rpc_call_start(task);
1070 return 0;
Trond Myklebust6b2e6852019-04-07 13:58:49 -04001071out_sleep_timeout:
1072 /* Try again in 1/4 second */
1073 if (args->sa_privileged)
1074 rpc_sleep_on_priority_timeout(&tbl->slot_tbl_waitq, task,
1075 jiffies + (HZ >> 2), RPC_PRIORITY_PRIVILEGED);
1076 else
1077 rpc_sleep_on_timeout(&tbl->slot_tbl_waitq, task,
1078 NULL, jiffies + (HZ >> 2));
1079 spin_unlock(&tbl->slot_tbl_lock);
1080 return -EAGAIN;
Anna Schumaker0dcee8b2017-01-10 16:29:54 -05001081out_sleep:
1082 if (args->sa_privileged)
1083 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
Trond Myklebust8357a9b2019-04-07 13:58:48 -04001084 RPC_PRIORITY_PRIVILEGED);
Anna Schumaker0dcee8b2017-01-10 16:29:54 -05001085 else
1086 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
1087 spin_unlock(&tbl->slot_tbl_lock);
1088 return -EAGAIN;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001089}
1090EXPORT_SYMBOL_GPL(nfs4_setup_sequence);
1091
Chuck Lever9915ea72013-08-09 12:48:27 -04001092static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
1093{
1094 struct nfs4_call_sync_data *data = calldata;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05001095 nfs4_setup_sequence(data->seq_server->nfs_client,
Chuck Lever9915ea72013-08-09 12:48:27 -04001096 data->seq_args, data->seq_res, task);
1097}
1098
1099static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
1100{
1101 struct nfs4_call_sync_data *data = calldata;
1102 nfs4_sequence_done(task, data->seq_res);
1103}
1104
1105static const struct rpc_call_ops nfs40_call_sync_ops = {
1106 .rpc_call_prepare = nfs40_call_sync_prepare,
1107 .rpc_call_done = nfs40_call_sync_done,
1108};
1109
Anna Schumaker48c05852019-08-14 15:27:00 -04001110static int nfs4_call_sync_custom(struct rpc_task_setup *task_setup)
1111{
1112 int ret;
1113 struct rpc_task *task;
1114
1115 task = rpc_run_task(task_setup);
1116 if (IS_ERR(task))
1117 return PTR_ERR(task);
1118
1119 ret = task->tk_status;
1120 rpc_put_task(task);
1121 return ret;
1122}
1123
Trond Myklebustc74dfe92020-01-06 15:39:37 -05001124static int nfs4_do_call_sync(struct rpc_clnt *clnt,
1125 struct nfs_server *server,
1126 struct rpc_message *msg,
1127 struct nfs4_sequence_args *args,
1128 struct nfs4_sequence_res *res,
1129 unsigned short task_flags)
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001130{
Chuck Lever9915ea72013-08-09 12:48:27 -04001131 struct nfs_client *clp = server->nfs_client;
1132 struct nfs4_call_sync_data data = {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001133 .seq_server = server,
Trond Myklebustad389da2007-06-05 12:30:00 -04001134 .seq_args = args,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001135 .seq_res = res,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001136 };
1137 struct rpc_task_setup task_setup = {
Trond Myklebustad389da2007-06-05 12:30:00 -04001138 .rpc_client = clnt,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001139 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -04001140 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebustc74dfe92020-01-06 15:39:37 -05001141 .callback_data = &data,
1142 .flags = task_flags,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001143 };
1144
Anna Schumaker48c05852019-08-14 15:27:00 -04001145 return nfs4_call_sync_custom(&task_setup);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001146}
1147
Trond Myklebustc74dfe92020-01-06 15:39:37 -05001148static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
1149 struct nfs_server *server,
1150 struct rpc_message *msg,
1151 struct nfs4_sequence_args *args,
1152 struct nfs4_sequence_res *res)
1153{
1154 return nfs4_do_call_sync(clnt, server, msg, args, res, 0);
1155}
1156
1157
Bryan Schumaker7c513052011-03-24 17:12:24 +00001158int nfs4_call_sync(struct rpc_clnt *clnt,
1159 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001160 struct rpc_message *msg,
1161 struct nfs4_sequence_args *args,
1162 struct nfs4_sequence_res *res,
1163 int cache_reply)
1164{
Anna Schumakerfba83f32018-05-04 16:22:50 -04001165 nfs4_init_sequence(args, res, cache_reply, 0);
Chuck Lever9915ea72013-08-09 12:48:27 -04001166 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001167}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
Trond Myklebust3c591172018-07-31 15:54:10 -04001169static void
1170nfs4_inc_nlink_locked(struct inode *inode)
1171{
Trond Myklebust82eae5a2021-04-01 14:59:59 -04001172 nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE |
1173 NFS_INO_INVALID_CTIME |
1174 NFS_INO_INVALID_NLINK);
Trond Myklebust3c591172018-07-31 15:54:10 -04001175 inc_nlink(inode);
1176}
1177
1178static void
Trond Myklebust1301e422021-04-01 14:57:56 -04001179nfs4_inc_nlink(struct inode *inode)
1180{
1181 spin_lock(&inode->i_lock);
1182 nfs4_inc_nlink_locked(inode);
1183 spin_unlock(&inode->i_lock);
1184}
1185
1186static void
Trond Myklebust3c591172018-07-31 15:54:10 -04001187nfs4_dec_nlink_locked(struct inode *inode)
1188{
Trond Myklebust82eae5a2021-04-01 14:59:59 -04001189 nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE |
1190 NFS_INO_INVALID_CTIME |
1191 NFS_INO_INVALID_NLINK);
Trond Myklebust3c591172018-07-31 15:54:10 -04001192 drop_nlink(inode);
1193}
1194
1195static void
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001196nfs4_update_changeattr_locked(struct inode *inode,
1197 struct nfs4_change_info *cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04001198 unsigned long timestamp, unsigned long cache_validity)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199{
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001200 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust6f9be832021-03-26 11:01:19 -04001201 u64 change_attr = inode_peek_iversion_raw(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
Trond Myklebustb6f80a22021-03-08 14:42:55 -05001203 cache_validity |= NFS_INO_INVALID_CTIME | NFS_INO_INVALID_MTIME;
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001204
Trond Myklebust6f9be832021-03-26 11:01:19 -04001205 switch (NFS_SERVER(inode)->change_attr_type) {
1206 case NFS4_CHANGE_TYPE_IS_UNDEFINED:
1207 break;
1208 case NFS4_CHANGE_TYPE_IS_TIME_METADATA:
1209 if ((s64)(change_attr - cinfo->after) > 0)
1210 goto out;
1211 break;
1212 default:
1213 if ((s64)(change_attr - cinfo->after) >= 0)
1214 goto out;
1215 }
1216
1217 if (cinfo->atomic && cinfo->before == change_attr) {
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001218 nfsi->attrtimeo_timestamp = jiffies;
1219 } else {
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001220 if (S_ISDIR(inode->i_mode)) {
Trond Myklebustb6f80a22021-03-08 14:42:55 -05001221 cache_validity |= NFS_INO_INVALID_DATA;
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001222 nfs_force_lookup_revalidate(inode);
1223 } else {
1224 if (!NFS_PROTO(inode)->have_delegation(inode,
1225 FMODE_READ))
Trond Myklebustb6f80a22021-03-08 14:42:55 -05001226 cache_validity |= NFS_INO_REVAL_PAGECACHE;
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001227 }
1228
Trond Myklebust6f9be832021-03-26 11:01:19 -04001229 if (cinfo->before != change_attr)
Trond Myklebustb6f80a22021-03-08 14:42:55 -05001230 cache_validity |= NFS_INO_INVALID_ACCESS |
1231 NFS_INO_INVALID_ACL |
1232 NFS_INO_INVALID_XATTR;
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001233 }
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001234 inode_set_iversion_raw(inode, cinfo->after);
Trond Myklebustd3129ef2017-01-11 22:07:28 -05001235 nfsi->read_cache_jiffies = timestamp;
Trond Myklebust3235b402015-02-26 19:52:06 -05001236 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
Trond Myklebustc8d07152018-07-31 15:54:12 -04001237 nfsi->cache_validity &= ~NFS_INO_INVALID_CHANGE;
Trond Myklebust6f9be832021-03-26 11:01:19 -04001238out:
1239 nfs_set_cache_invalid(inode, cache_validity);
Trond Myklebust3c591172018-07-31 15:54:10 -04001240}
1241
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001242void
1243nfs4_update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04001244 unsigned long timestamp, unsigned long cache_validity)
Trond Myklebust3c591172018-07-31 15:54:10 -04001245{
1246 spin_lock(&dir->i_lock);
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001247 nfs4_update_changeattr_locked(dir, cinfo, timestamp, cache_validity);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 spin_unlock(&dir->i_lock);
1249}
1250
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001251struct nfs4_open_createattrs {
1252 struct nfs4_label *label;
1253 struct iattr *sattr;
1254 const __u32 verf[2];
1255};
1256
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001257static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1258 int err, struct nfs4_exception *exception)
1259{
1260 if (err != -EINVAL)
1261 return false;
1262 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1263 return false;
1264 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1265 exception->retry = 1;
1266 return true;
1267}
1268
Trond Myklebust1bf85d82019-06-27 06:30:48 -04001269static fmode_t _nfs4_ctx_to_accessmode(const struct nfs_open_context *ctx)
1270{
1271 return ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
1272}
1273
1274static fmode_t _nfs4_ctx_to_openmode(const struct nfs_open_context *ctx)
1275{
1276 fmode_t ret = ctx->mode & (FMODE_READ|FMODE_WRITE);
1277
1278 return (ctx->mode & FMODE_EXEC) ? FMODE_READ | ret : ret;
1279}
1280
Trond Myklebust6ae37332015-01-30 14:21:14 -05001281static u32
1282nfs4_map_atomic_open_share(struct nfs_server *server,
1283 fmode_t fmode, int openflags)
1284{
1285 u32 res = 0;
1286
1287 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1288 case FMODE_READ:
1289 res = NFS4_SHARE_ACCESS_READ;
1290 break;
1291 case FMODE_WRITE:
1292 res = NFS4_SHARE_ACCESS_WRITE;
1293 break;
1294 case FMODE_READ|FMODE_WRITE:
1295 res = NFS4_SHARE_ACCESS_BOTH;
1296 }
1297 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1298 goto out;
1299 /* Want no delegation if we're using O_DIRECT */
1300 if (openflags & O_DIRECT)
1301 res |= NFS4_SHARE_WANT_NO_DELEG;
1302out:
1303 return res;
1304}
1305
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001306static enum open_claim_type4
1307nfs4_map_atomic_open_claim(struct nfs_server *server,
1308 enum open_claim_type4 claim)
1309{
1310 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1311 return claim;
1312 switch (claim) {
1313 default:
1314 return claim;
1315 case NFS4_OPEN_CLAIM_FH:
1316 return NFS4_OPEN_CLAIM_NULL;
1317 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1318 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1319 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1320 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1321 }
1322}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
1324static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001325{
1326 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001327 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001328 p->o_res.seqid = p->o_arg.seqid;
1329 p->c_res.seqid = p->c_arg.seqid;
1330 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -04001331 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001332 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd12012-01-07 13:22:46 -05001333 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001334}
1335
Al Viro82a2c1b2011-06-22 18:30:55 -04001336static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001337 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001338 const struct nfs4_open_createattrs *c,
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001339 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001340 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001341{
Al Viro82a2c1b2011-06-22 18:30:55 -04001342 struct dentry *parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001343 struct inode *dir = d_inode(parent);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001344 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust63f5f792015-01-23 19:19:25 -05001345 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001346 struct nfs4_label *label = (c != NULL) ? c->label : NULL;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001347 struct nfs4_opendata *p;
1348
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001349 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001350 if (p == NULL)
1351 goto err;
David Quigley14c43f72013-05-22 12:50:43 -04001352
1353 p->f_label = nfs4_label_alloc(server, gfp_mask);
1354 if (IS_ERR(p->f_label))
1355 goto err_free_p;
1356
Kinglong Meea49c2692015-07-27 15:31:38 +08001357 p->a_label = nfs4_label_alloc(server, gfp_mask);
1358 if (IS_ERR(p->a_label))
1359 goto err_free_f;
1360
Trond Myklebust63f5f792015-01-23 19:19:25 -05001361 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1362 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05001363 if (IS_ERR(p->o_arg.seqid))
David Quigley14c43f72013-05-22 12:50:43 -04001364 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -04001365 nfs_sb_active(dentry->d_sb);
1366 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001367 p->dir = parent;
1368 p->owner = sp;
1369 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001370 p->o_arg.open_flags = flags;
1371 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Trond Myklebust536585c2016-11-10 15:40:34 -05001372 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
Trond Myklebust6ae37332015-01-30 14:21:14 -05001373 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1374 fmode, flags);
Benjamin Coddington90910512019-06-07 06:37:30 -04001375 if (flags & O_CREAT) {
1376 p->o_arg.umask = current_umask();
1377 p->o_arg.label = nfs4_label_copy(p->a_label, label);
1378 if (c->sattr != NULL && c->sattr->ia_valid != 0) {
1379 p->o_arg.u.attrs = &p->attrs;
1380 memcpy(&p->attrs, c->sattr, sizeof(p->attrs));
1381
1382 memcpy(p->o_arg.u.verifier.data, c->verf,
1383 sizeof(p->o_arg.u.verifier.data));
1384 }
1385 }
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001386 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1387 * will return permission denied for all bits until close */
1388 if (!(flags & O_EXCL)) {
1389 /* ask server to check for all possible rights as results
1390 * are cached */
Trond Myklebust536585c2016-11-10 15:40:34 -05001391 switch (p->o_arg.claim) {
1392 default:
1393 break;
1394 case NFS4_OPEN_CLAIM_NULL:
1395 case NFS4_OPEN_CLAIM_FH:
1396 p->o_arg.access = NFS4_ACCESS_READ |
1397 NFS4_ACCESS_MODIFY |
1398 NFS4_ACCESS_EXTEND |
1399 NFS4_ACCESS_EXECUTE;
Frank van der Linden72832a22020-06-23 22:38:58 +00001400#ifdef CONFIG_NFS_V4_2
1401 if (server->caps & NFS_CAP_XATTR)
1402 p->o_arg.access |= NFS4_ACCESS_XAREAD |
1403 NFS4_ACCESS_XAWRITE |
1404 NFS4_ACCESS_XALIST;
1405#endif
Trond Myklebust536585c2016-11-10 15:40:34 -05001406 }
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001407 }
David Howells7539bba2006-08-22 20:06:09 -04001408 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001409 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1410 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001411 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001412 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001413 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001414 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001415 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001416 case NFS4_OPEN_CLAIM_NULL:
1417 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1418 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1419 p->o_arg.fh = NFS_FH(dir);
1420 break;
1421 case NFS4_OPEN_CLAIM_PREVIOUS:
1422 case NFS4_OPEN_CLAIM_FH:
1423 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1424 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
David Howells2b0143b2015-03-17 22:25:59 +00001425 p->o_arg.fh = NFS_FH(d_inode(dentry));
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001426 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001427 p->c_arg.fh = &p->o_res.fh;
1428 p->c_arg.stateid = &p->o_res.stateid;
1429 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001430 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001431 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001432 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001433
1434err_free_label:
Kinglong Meea49c2692015-07-27 15:31:38 +08001435 nfs4_label_free(p->a_label);
1436err_free_f:
David Quigley14c43f72013-05-22 12:50:43 -04001437 nfs4_label_free(p->f_label);
1438err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001439 kfree(p);
1440err:
1441 dput(parent);
1442 return NULL;
1443}
1444
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001445static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001446{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001447 struct nfs4_opendata *p = container_of(kref,
1448 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001449 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001450
Fred Isaman30ae2412016-10-18 13:39:51 -04001451 nfs4_lgopen_release(p->lgp);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001452 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001453 nfs4_sequence_free_slot(&p->o_res.seq_res);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001454 if (p->state != NULL)
1455 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001456 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001457
Kinglong Meea49c2692015-07-27 15:31:38 +08001458 nfs4_label_free(p->a_label);
David Quigley14c43f72013-05-22 12:50:43 -04001459 nfs4_label_free(p->f_label);
1460
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001461 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001462 dput(p->dentry);
1463 nfs_sb_deactive(sb);
Trond Myklebust6926afd12012-01-07 13:22:46 -05001464 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001465 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001466 kfree(p);
1467}
1468
1469static void nfs4_opendata_put(struct nfs4_opendata *p)
1470{
1471 if (p != NULL)
1472 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001473}
1474
Trond Myklebust24311f82015-09-20 10:50:17 -04001475static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1476 fmode_t fmode)
1477{
1478 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1479 case FMODE_READ|FMODE_WRITE:
1480 return state->n_rdwr != 0;
1481 case FMODE_WRITE:
1482 return state->n_wronly != 0;
1483 case FMODE_READ:
1484 return state->n_rdonly != 0;
1485 }
1486 WARN_ON_ONCE(1);
1487 return false;
1488}
1489
Trond Myklebustbe189f72018-09-27 17:12:33 -04001490static int can_open_cached(struct nfs4_state *state, fmode_t mode,
1491 int open_mode, enum open_claim_type4 claim)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001492{
1493 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001494
Trond Myklebust536e43d2012-01-17 22:04:26 -05001495 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001496 goto out;
Trond Myklebustbe189f72018-09-27 17:12:33 -04001497 switch (claim) {
1498 case NFS4_OPEN_CLAIM_NULL:
1499 case NFS4_OPEN_CLAIM_FH:
1500 goto out;
1501 default:
1502 break;
1503 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001504 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001505 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001506 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1507 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001508 break;
1509 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001510 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1511 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001512 break;
1513 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001514 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1515 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001516 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001517out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001518 return ret;
1519}
1520
Trond Myklebust2a606182015-08-19 22:30:00 -05001521static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1522 enum open_claim_type4 claim)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001523{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001524 if (delegation == NULL)
1525 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001526 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001527 return 0;
Trond Myklebust2a606182015-08-19 22:30:00 -05001528 switch (claim) {
1529 case NFS4_OPEN_CLAIM_NULL:
1530 case NFS4_OPEN_CLAIM_FH:
1531 break;
1532 case NFS4_OPEN_CLAIM_PREVIOUS:
1533 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1534 break;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001535 fallthrough;
Trond Myklebust2a606182015-08-19 22:30:00 -05001536 default:
1537 return 0;
1538 }
Trond Myklebustb7391f42008-12-23 15:21:52 -05001539 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001540 return 1;
1541}
1542
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001543static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001544{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001545 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001546 case FMODE_WRITE:
1547 state->n_wronly++;
1548 break;
1549 case FMODE_READ:
1550 state->n_rdonly++;
1551 break;
1552 case FMODE_READ|FMODE_WRITE:
1553 state->n_rdwr++;
1554 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001555 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001556}
1557
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04001558#ifdef CONFIG_NFS_V4_1
1559static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1560{
1561 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1562 return true;
1563 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1564 return true;
1565 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1566 return true;
1567 return false;
1568}
1569#endif /* CONFIG_NFS_V4_1 */
1570
Trond Myklebustc9399f22017-11-06 15:28:01 -05001571static void nfs_state_log_update_open_stateid(struct nfs4_state *state)
1572{
1573 if (test_and_clear_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1574 wake_up_all(&state->waitq);
1575}
1576
Trond Myklebust4f14c192014-02-12 19:15:06 -05001577static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001578{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001579 struct nfs_client *clp = state->owner->so_server->nfs_client;
1580 bool need_recover = false;
1581
1582 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1583 need_recover = true;
1584 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1585 need_recover = true;
1586 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1587 need_recover = true;
1588 if (need_recover)
1589 nfs4_state_mark_reclaim_nograce(clp, state);
1590}
1591
Trond Myklebustc9399f22017-11-06 15:28:01 -05001592/*
1593 * Check for whether or not the caller may update the open stateid
1594 * to the value passed in by stateid.
1595 *
1596 * Note: This function relies heavily on the server implementing
1597 * RFC7530 Section 9.1.4.2, and RFC5661 Section 8.2.2
1598 * correctly.
1599 * i.e. The stateid seqids have to be initialised to 1, and
1600 * are then incremented on every state transition.
1601 */
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04001602static bool nfs_stateid_is_sequential(struct nfs4_state *state,
Trond Myklebustc9399f22017-11-06 15:28:01 -05001603 const nfs4_stateid *stateid)
Trond Myklebuste999e802014-02-10 18:20:47 -05001604{
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04001605 if (test_bit(NFS_OPEN_STATE, &state->flags)) {
1606 /* The common case - we're updating to a new sequence number */
1607 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1608 nfs4_stateid_is_next(&state->open_stateid, stateid)) {
1609 return true;
1610 }
1611 } else {
1612 /* This is the first OPEN in this generation */
Trond Myklebustc9399f22017-11-06 15:28:01 -05001613 if (stateid->seqid == cpu_to_be32(1))
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04001614 return true;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001615 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001616 return false;
1617}
1618
Trond Myklebustf95549c2015-01-23 18:06:09 -05001619static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1620{
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001621 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1622 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001623 if (state->n_wronly)
1624 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1625 if (state->n_rdonly)
1626 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1627 if (state->n_rdwr)
1628 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001629 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustf95549c2015-01-23 18:06:09 -05001630}
1631
Trond Myklebust226056c2014-02-11 10:41:07 -05001632static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1633 nfs4_stateid *stateid, fmode_t fmode)
1634{
1635 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1636 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1637 case FMODE_WRITE:
1638 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1639 break;
1640 case FMODE_READ:
1641 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1642 break;
1643 case 0:
1644 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1645 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1646 clear_bit(NFS_OPEN_STATE, &state->flags);
1647 }
1648 if (stateid == NULL)
1649 return;
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001650 /* Handle OPEN+OPEN_DOWNGRADE races */
1651 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1652 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
Trond Myklebustf95549c2015-01-23 18:06:09 -05001653 nfs_resync_open_stateid_locked(state);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001654 goto out;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001655 }
Trond Myklebust003707c2007-07-05 18:07:55 -04001656 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001657 nfs4_stateid_copy(&state->stateid, stateid);
1658 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001659 trace_nfs4_open_stateid_update(state->inode, stateid, 0);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001660out:
1661 nfs_state_log_update_open_stateid(state);
Trond Myklebust226056c2014-02-11 10:41:07 -05001662}
1663
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001664static void nfs_clear_open_stateid(struct nfs4_state *state,
1665 nfs4_stateid *arg_stateid,
1666 nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust226056c2014-02-11 10:41:07 -05001667{
1668 write_seqlock(&state->seqlock);
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001669 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1670 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1671 nfs_clear_open_stateid_locked(state, stateid, fmode);
Trond Myklebust226056c2014-02-11 10:41:07 -05001672 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001673 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1674 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001675}
1676
Trond Myklebust1393d962016-09-22 13:39:13 -04001677static void nfs_set_open_stateid_locked(struct nfs4_state *state,
Trond Myklebustc9399f22017-11-06 15:28:01 -05001678 const nfs4_stateid *stateid, nfs4_stateid *freeme)
Trond Myklebuste9acf212019-01-22 14:01:16 -05001679 __must_hold(&state->owner->so_lock)
1680 __must_hold(&state->seqlock)
1681 __must_hold(RCU)
1682
Trond Myklebust003707c2007-07-05 18:07:55 -04001683{
Trond Myklebustc9399f22017-11-06 15:28:01 -05001684 DEFINE_WAIT(wait);
1685 int status = 0;
1686 for (;;) {
1687
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04001688 if (nfs_stateid_is_sequential(state, stateid))
Trond Myklebust003707c2007-07-05 18:07:55 -04001689 break;
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04001690
Trond Myklebustc9399f22017-11-06 15:28:01 -05001691 if (status)
Trond Myklebust003707c2007-07-05 18:07:55 -04001692 break;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001693 /* Rely on seqids for serialisation with NFSv4.0 */
1694 if (!nfs4_has_session(NFS_SERVER(state->inode)->nfs_client))
1695 break;
1696
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04001697 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001698 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
1699 /*
1700 * Ensure we process the state changes in the same order
1701 * in which the server processed them by delaying the
1702 * update of the stateid until we are in sequence.
1703 */
1704 write_sequnlock(&state->seqlock);
1705 spin_unlock(&state->owner->so_lock);
1706 rcu_read_unlock();
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001707 trace_nfs4_open_stateid_update_wait(state->inode, stateid, 0);
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04001708
Trond Myklebustc9399f22017-11-06 15:28:01 -05001709 if (!signal_pending(current)) {
1710 if (schedule_timeout(5*HZ) == 0)
1711 status = -EAGAIN;
1712 else
1713 status = 0;
1714 } else
1715 status = -EINTR;
1716 finish_wait(&state->waitq, &wait);
1717 rcu_read_lock();
1718 spin_lock(&state->owner->so_lock);
1719 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001720 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001721
Trond Myklebuste1fff5d2017-11-07 13:10:46 -05001722 if (test_bit(NFS_OPEN_STATE, &state->flags) &&
1723 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
Trond Myklebustc9399f22017-11-06 15:28:01 -05001724 nfs4_stateid_copy(freeme, &state->open_stateid);
1725 nfs_test_and_clear_all_open_stateid(state);
1726 }
1727
Trond Myklebuste999e802014-02-10 18:20:47 -05001728 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1729 nfs4_stateid_copy(&state->stateid, stateid);
1730 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001731 trace_nfs4_open_stateid_update(state->inode, stateid, status);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001732 nfs_state_log_update_open_stateid(state);
Trond Myklebust003707c2007-07-05 18:07:55 -04001733}
1734
Trond Myklebustc9399f22017-11-06 15:28:01 -05001735static void nfs_state_set_open_stateid(struct nfs4_state *state,
Trond Myklebust1393d962016-09-22 13:39:13 -04001736 const nfs4_stateid *open_stateid,
Trond Myklebust1393d962016-09-22 13:39:13 -04001737 fmode_t fmode,
1738 nfs4_stateid *freeme)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001740 /*
1741 * Protect the call to nfs4_state_set_mode_locked and
1742 * serialise the stateid update
1743 */
1744 write_seqlock(&state->seqlock);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001745 nfs_set_open_stateid_locked(state, open_stateid, freeme);
1746 switch (fmode) {
1747 case FMODE_READ:
1748 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1749 break;
1750 case FMODE_WRITE:
1751 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1752 break;
1753 case FMODE_READ|FMODE_WRITE:
1754 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust003707c2007-07-05 18:07:55 -04001755 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001756 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001757 write_sequnlock(&state->seqlock);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001758}
1759
Trond Myklebust27a30cf2019-07-22 18:32:59 +01001760static void nfs_state_clear_open_state_flags(struct nfs4_state *state)
1761{
1762 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1763 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1764 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1765 clear_bit(NFS_OPEN_STATE, &state->flags);
1766}
1767
Trond Myklebustc9399f22017-11-06 15:28:01 -05001768static void nfs_state_set_delegation(struct nfs4_state *state,
1769 const nfs4_stateid *deleg_stateid,
1770 fmode_t fmode)
1771{
1772 /*
1773 * Protect the call to nfs4_state_set_mode_locked and
1774 * serialise the stateid update
1775 */
1776 write_seqlock(&state->seqlock);
1777 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1778 set_bit(NFS_DELEGATED_STATE, &state->flags);
1779 write_sequnlock(&state->seqlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780}
1781
Trond Myklebust9f0c5122018-09-05 14:07:15 -04001782static void nfs_state_clear_delegation(struct nfs4_state *state)
1783{
1784 write_seqlock(&state->seqlock);
1785 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1786 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1787 write_sequnlock(&state->seqlock);
1788}
1789
Olga Kornievskaiaec4b0922019-10-08 16:33:53 -04001790int update_open_stateid(struct nfs4_state *state,
Trond Myklebust1393d962016-09-22 13:39:13 -04001791 const nfs4_stateid *open_stateid,
1792 const nfs4_stateid *delegation,
1793 fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001794{
Trond Myklebust1393d962016-09-22 13:39:13 -04001795 struct nfs_server *server = NFS_SERVER(state->inode);
1796 struct nfs_client *clp = server->nfs_client;
Trond Myklebust34310432008-12-23 15:21:38 -05001797 struct nfs_inode *nfsi = NFS_I(state->inode);
1798 struct nfs_delegation *deleg_cur;
Arnd Bergmann83aa3e02016-10-18 17:21:30 +02001799 nfs4_stateid freeme = { };
Trond Myklebust34310432008-12-23 15:21:38 -05001800 int ret = 0;
1801
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001802 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001803
1804 rcu_read_lock();
Trond Myklebustc9399f22017-11-06 15:28:01 -05001805 spin_lock(&state->owner->so_lock);
1806 if (open_stateid != NULL) {
1807 nfs_state_set_open_stateid(state, open_stateid, fmode, &freeme);
1808 ret = 1;
1809 }
1810
Trond Myklebust333ac782019-10-22 12:12:17 -04001811 deleg_cur = nfs4_get_valid_delegation(state->inode);
Trond Myklebust34310432008-12-23 15:21:38 -05001812 if (deleg_cur == NULL)
1813 goto no_delegation;
1814
1815 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001816 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001817 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001818 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001819 goto no_delegation_unlock;
1820
1821 if (delegation == NULL)
1822 delegation = &deleg_cur->stateid;
Trond Myklebust333ac782019-10-22 12:12:17 -04001823 else if (!nfs4_stateid_match_other(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001824 goto no_delegation_unlock;
1825
Trond Myklebustb7391f42008-12-23 15:21:52 -05001826 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001827 nfs_state_set_delegation(state, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001828 ret = 1;
1829no_delegation_unlock:
1830 spin_unlock(&deleg_cur->lock);
1831no_delegation:
Trond Myklebustc9399f22017-11-06 15:28:01 -05001832 if (ret)
1833 update_open_stateflags(state, fmode);
1834 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001835 rcu_read_unlock();
1836
Trond Myklebust4f14c192014-02-12 19:15:06 -05001837 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
Trond Myklebust1393d962016-09-22 13:39:13 -04001838 nfs4_schedule_state_manager(clp);
1839 if (freeme.type != 0)
1840 nfs4_test_and_free_stateid(server, &freeme,
1841 state->owner->so_cred);
Trond Myklebust34310432008-12-23 15:21:38 -05001842
1843 return ret;
1844}
1845
Trond Myklebust39071e62015-01-24 15:07:56 -05001846static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1847 const nfs4_stateid *stateid)
1848{
1849 struct nfs4_state *state = lsp->ls_state;
1850 bool ret = false;
1851
1852 spin_lock(&state->state_lock);
1853 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1854 goto out_noupdate;
1855 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1856 goto out_noupdate;
1857 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1858 ret = true;
1859out_noupdate:
1860 spin_unlock(&state->state_lock);
1861 return ret;
1862}
Trond Myklebust34310432008-12-23 15:21:38 -05001863
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001864static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001865{
1866 struct nfs_delegation *delegation;
1867
Trond Myklebustf5086242018-03-20 16:43:13 -04001868 fmode &= FMODE_READ|FMODE_WRITE;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001869 rcu_read_lock();
Trond Myklebust40e6aa12019-10-27 13:38:45 -04001870 delegation = nfs4_get_valid_delegation(inode);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001871 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001872 rcu_read_unlock();
1873 return;
1874 }
1875 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001876 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001877}
1878
Trond Myklebust6ee41262007-07-08 14:11:36 -04001879static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001880{
1881 struct nfs4_state *state = opendata->state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001882 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001883 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001884 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebust2a606182015-08-19 22:30:00 -05001885 enum open_claim_type4 claim = opendata->o_arg.claim;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001886 nfs4_stateid stateid;
1887 int ret = -EAGAIN;
1888
Trond Myklebustaac00a82007-07-05 19:02:21 -04001889 for (;;) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001890 spin_lock(&state->owner->so_lock);
Trond Myklebustbe189f72018-09-27 17:12:33 -04001891 if (can_open_cached(state, fmode, open_mode, claim)) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001892 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001893 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef72014-09-03 14:15:40 -04001894 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001895 }
Anna Schumaker61beef72014-09-03 14:15:40 -04001896 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001897 rcu_read_lock();
Trond Myklebustbe3df3d2019-10-31 18:40:32 -04001898 delegation = nfs4_get_valid_delegation(state->inode);
Trond Myklebust2a606182015-08-19 22:30:00 -05001899 if (!can_open_delegated(delegation, fmode, claim)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001900 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001901 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001902 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001903 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001904 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001905 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001906 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001907 if (!opendata->is_recover) {
1908 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1909 if (ret != 0)
1910 goto out;
1911 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001912 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001913
1914 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001915 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001916 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001917 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001918out:
1919 return ERR_PTR(ret);
1920out_return_state:
Trond Myklebustace9fad2018-09-02 19:19:07 -04001921 refcount_inc(&state->count);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001922 return state;
1923}
1924
Andy Adamsone23008e2012-10-02 21:07:32 -04001925static void
1926nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1927{
1928 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1929 struct nfs_delegation *delegation;
1930 int delegation_flags = 0;
1931
1932 rcu_read_lock();
1933 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1934 if (delegation)
1935 delegation_flags = delegation->flags;
1936 rcu_read_unlock();
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001937 switch (data->o_arg.claim) {
1938 default:
1939 break;
1940 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1941 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001942 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1943 "returning a delegation for "
1944 "OPEN(CLAIM_DELEGATE_CUR)\n",
1945 clp->cl_hostname);
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001946 return;
1947 }
1948 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Andy Adamsone23008e2012-10-02 21:07:32 -04001949 nfs_inode_set_delegation(state->inode,
Trond Myklebust35156bf2018-03-20 17:03:13 -04001950 data->owner->so_cred,
1951 data->o_res.delegation_type,
1952 &data->o_res.delegation,
1953 data->o_res.pagemod_limit);
Andy Adamsone23008e2012-10-02 21:07:32 -04001954 else
1955 nfs_inode_reclaim_delegation(state->inode,
Trond Myklebust35156bf2018-03-20 17:03:13 -04001956 data->owner->so_cred,
1957 data->o_res.delegation_type,
1958 &data->o_res.delegation,
1959 data->o_res.pagemod_limit);
Jeff Layton8b199e52018-07-05 05:48:14 -04001960
1961 if (data->o_res.do_recall)
1962 nfs_async_inode_return_delegation(state->inode,
1963 &data->o_res.delegation);
Andy Adamsone23008e2012-10-02 21:07:32 -04001964}
1965
1966/*
1967 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1968 * and update the nfs4_state.
1969 */
1970static struct nfs4_state *
1971_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1972{
1973 struct inode *inode = data->state->inode;
1974 struct nfs4_state *state = data->state;
1975 int ret;
1976
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001977 if (!data->rpc_done) {
Anna Schumaker37a84842017-01-11 16:08:35 -05001978 if (data->rpc_status)
1979 return ERR_PTR(data->rpc_status);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001980 /* cached opens have already been processed */
1981 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001982 }
1983
Andy Adamsone23008e2012-10-02 21:07:32 -04001984 ret = nfs_refresh_inode(inode, &data->f_attr);
1985 if (ret)
Anna Schumaker37a84842017-01-11 16:08:35 -05001986 return ERR_PTR(ret);
Andy Adamsone23008e2012-10-02 21:07:32 -04001987
1988 if (data->o_res.delegation_type != 0)
1989 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001990update:
Trond Myklebuste3c8dc72019-07-29 18:25:00 +01001991 if (!update_open_stateid(state, &data->o_res.stateid,
1992 NULL, data->o_arg.fmode))
1993 return ERR_PTR(-EAGAIN);
Trond Myklebustace9fad2018-09-02 19:19:07 -04001994 refcount_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001995
1996 return state;
Andy Adamsone23008e2012-10-02 21:07:32 -04001997}
1998
Trond Myklebust4e2fcac2017-08-08 09:06:18 -04001999static struct inode *
2000nfs4_opendata_get_inode(struct nfs4_opendata *data)
2001{
2002 struct inode *inode;
2003
2004 switch (data->o_arg.claim) {
2005 case NFS4_OPEN_CLAIM_NULL:
2006 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
2007 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
2008 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
2009 return ERR_PTR(-EAGAIN);
2010 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh,
2011 &data->f_attr, data->f_label);
2012 break;
2013 default:
2014 inode = d_inode(data->dentry);
2015 ihold(inode);
2016 nfs_refresh_inode(inode, &data->f_attr);
2017 }
2018 return inode;
2019}
2020
Andy Adamsone23008e2012-10-02 21:07:32 -04002021static struct nfs4_state *
Trond Myklebust75e8c482017-08-08 10:38:07 -04002022nfs4_opendata_find_nfs4_state(struct nfs4_opendata *data)
2023{
2024 struct nfs4_state *state;
2025 struct inode *inode;
2026
2027 inode = nfs4_opendata_get_inode(data);
2028 if (IS_ERR(inode))
2029 return ERR_CAST(inode);
2030 if (data->state != NULL && data->state->inode == inode) {
2031 state = data->state;
Trond Myklebustace9fad2018-09-02 19:19:07 -04002032 refcount_inc(&state->count);
Trond Myklebust75e8c482017-08-08 10:38:07 -04002033 } else
2034 state = nfs4_get_open_state(inode, data->owner);
2035 iput(inode);
2036 if (state == NULL)
2037 state = ERR_PTR(-ENOMEM);
2038 return state;
2039}
2040
Andy Adamsone23008e2012-10-02 21:07:32 -04002041static struct nfs4_state *
2042_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002043{
Trond Myklebust75e8c482017-08-08 10:38:07 -04002044 struct nfs4_state *state;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002045
Trond Myklebustaac00a82007-07-05 19:02:21 -04002046 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04002047 state = nfs4_try_open_cached(data);
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002048 trace_nfs4_cached_open(data->state);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002049 goto out;
2050 }
2051
Trond Myklebust75e8c482017-08-08 10:38:07 -04002052 state = nfs4_opendata_find_nfs4_state(data);
2053 if (IS_ERR(state))
2054 goto out;
2055
Andy Adamsone23008e2012-10-02 21:07:32 -04002056 if (data->o_res.delegation_type != 0)
2057 nfs4_opendata_check_deleg(data, state);
Trond Myklebuste3c8dc72019-07-29 18:25:00 +01002058 if (!update_open_stateid(state, &data->o_res.stateid,
2059 NULL, data->o_arg.fmode)) {
2060 nfs4_put_open_state(state);
2061 state = ERR_PTR(-EAGAIN);
2062 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002063out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08002064 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002065 return state;
2066}
2067
Andy Adamsone23008e2012-10-02 21:07:32 -04002068static struct nfs4_state *
2069nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
2070{
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002071 struct nfs4_state *ret;
2072
Andy Adamsone23008e2012-10-02 21:07:32 -04002073 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002074 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
2075 else
2076 ret = _nfs4_opendata_to_nfs4_state(data);
2077 nfs4_sequence_free_slot(&data->o_res.seq_res);
2078 return ret;
Andy Adamsone23008e2012-10-02 21:07:32 -04002079}
2080
Trond Myklebust0de43972018-09-02 15:57:01 -04002081static struct nfs_open_context *
2082nfs4_state_find_open_context_mode(struct nfs4_state *state, fmode_t mode)
Trond Myklebust864472e2006-01-03 09:55:15 +01002083{
2084 struct nfs_inode *nfsi = NFS_I(state->inode);
2085 struct nfs_open_context *ctx;
2086
Trond Myklebust0de43972018-09-02 15:57:01 -04002087 rcu_read_lock();
2088 list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
Trond Myklebust864472e2006-01-03 09:55:15 +01002089 if (ctx->state != state)
2090 continue;
Trond Myklebust0de43972018-09-02 15:57:01 -04002091 if ((ctx->mode & mode) != mode)
2092 continue;
2093 if (!get_nfs_open_context(ctx))
2094 continue;
2095 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01002096 return ctx;
2097 }
Trond Myklebust0de43972018-09-02 15:57:01 -04002098 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01002099 return ERR_PTR(-ENOENT);
2100}
2101
Trond Myklebust0de43972018-09-02 15:57:01 -04002102static struct nfs_open_context *
2103nfs4_state_find_open_context(struct nfs4_state *state)
2104{
2105 struct nfs_open_context *ctx;
2106
2107 ctx = nfs4_state_find_open_context_mode(state, FMODE_READ|FMODE_WRITE);
2108 if (!IS_ERR(ctx))
2109 return ctx;
2110 ctx = nfs4_state_find_open_context_mode(state, FMODE_WRITE);
2111 if (!IS_ERR(ctx))
2112 return ctx;
2113 return nfs4_state_find_open_context_mode(state, FMODE_READ);
2114}
2115
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002116static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
2117 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002118{
2119 struct nfs4_opendata *opendata;
2120
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002121 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002122 NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002123 if (opendata == NULL)
2124 return ERR_PTR(-ENOMEM);
2125 opendata->state = state;
Trond Myklebustace9fad2018-09-02 19:19:07 -04002126 refcount_inc(&state->count);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002127 return opendata;
2128}
2129
Trond Myklebust24311f82015-09-20 10:50:17 -04002130static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
2131 fmode_t fmode)
Trond Myklebust864472e2006-01-03 09:55:15 +01002132{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002133 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01002134 int ret;
2135
Trond Myklebust24311f82015-09-20 10:50:17 -04002136 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
NeilBrown39f897f2015-06-29 14:28:54 +10002137 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002138 opendata->o_arg.open_flags = 0;
2139 opendata->o_arg.fmode = fmode;
Trond Myklebustbe36e182015-02-27 17:04:17 -05002140 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
2141 NFS_SB(opendata->dentry->d_sb),
2142 fmode, 0);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002143 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
2144 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
2145 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002146 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002147 if (ret != 0)
2148 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002149 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002150 if (IS_ERR(newstate))
2151 return PTR_ERR(newstate);
Trond Myklebust24311f82015-09-20 10:50:17 -04002152 if (newstate != opendata->state)
2153 ret = -ESTALE;
Al Viro643168c2011-06-22 18:20:23 -04002154 nfs4_close_state(newstate, fmode);
Trond Myklebust24311f82015-09-20 10:50:17 -04002155 return ret;
Trond Myklebust864472e2006-01-03 09:55:15 +01002156}
2157
2158static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
2159{
Trond Myklebust864472e2006-01-03 09:55:15 +01002160 int ret;
2161
2162 /* memory barrier prior to reading state->n_* */
2163 smp_rmb();
Trond Myklebust24311f82015-09-20 10:50:17 -04002164 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2165 if (ret != 0)
2166 return ret;
2167 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2168 if (ret != 0)
2169 return ret;
2170 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
2171 if (ret != 0)
2172 return ret;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002173 /*
2174 * We may have performed cached opens for all three recoveries.
2175 * Check if we need to update the current stateid.
2176 */
2177 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05002178 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04002179 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002180 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05002181 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04002182 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002183 }
Trond Myklebust864472e2006-01-03 09:55:15 +01002184 return 0;
2185}
2186
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187/*
2188 * OPEN_RECLAIM:
2189 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002191static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002193 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01002194 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002195 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 int status;
2197
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002198 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2199 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002200 if (IS_ERR(opendata))
2201 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002202 rcu_read_lock();
2203 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05002204 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04002205 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002206 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01002207 opendata->o_arg.u.delegation_type = delegation_type;
2208 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002209 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 return status;
2211}
2212
Trond Myklebust539cd032007-06-05 11:46:42 -04002213static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214{
2215 struct nfs_server *server = NFS_SERVER(state->inode);
2216 struct nfs4_exception exception = { };
2217 int err;
2218 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002219 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002220 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002221 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2222 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04002223 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002224 break;
2225 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 } while (exception.retry);
2227 return err;
2228}
2229
Trond Myklebust864472e2006-01-03 09:55:15 +01002230static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
2231{
2232 struct nfs_open_context *ctx;
2233 int ret;
2234
2235 ctx = nfs4_state_find_open_context(state);
2236 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002237 return -EAGAIN;
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002238 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2239 nfs_state_clear_open_state_flags(state);
Trond Myklebust539cd032007-06-05 11:46:42 -04002240 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002241 put_nfs_open_context(ctx);
2242 return ret;
2243}
2244
NeilBrowndce26302017-12-13 09:57:09 +11002245static 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 -07002246{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002247 switch (err) {
2248 default:
2249 printk(KERN_ERR "NFS: %s: unhandled error "
2250 "%d.\n", __func__, err);
Gustavo A. R. Silvaffb81712020-11-20 12:26:46 -06002251 fallthrough;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002252 case 0:
2253 case -ENOENT:
Trond Myklebust8eee52a2015-06-04 13:51:13 -04002254 case -EAGAIN:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002255 case -ESTALE:
Trond Myklebust67e7b522019-08-07 07:31:27 -04002256 case -ETIMEDOUT:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002257 break;
2258 case -NFS4ERR_BADSESSION:
2259 case -NFS4ERR_BADSLOT:
2260 case -NFS4ERR_BAD_HIGH_SLOT:
2261 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
2262 case -NFS4ERR_DEADSESSION:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002263 return -EAGAIN;
2264 case -NFS4ERR_STALE_CLIENTID:
2265 case -NFS4ERR_STALE_STATEID:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002266 /* Don't recall a delegation if it was lost */
2267 nfs4_schedule_lease_recovery(server->nfs_client);
2268 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04002269 case -NFS4ERR_MOVED:
2270 nfs4_schedule_migration_recovery(server);
2271 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04002272 case -NFS4ERR_LEASE_MOVED:
2273 nfs4_schedule_lease_moved_recovery(server->nfs_client);
2274 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002275 case -NFS4ERR_DELEG_REVOKED:
2276 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust404ea3562016-09-22 13:39:08 -04002277 case -NFS4ERR_EXPIRED:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002278 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002279 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002280 nfs_inode_find_state_and_recover(state->inode,
2281 stateid);
2282 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05002283 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002284 case -NFS4ERR_DELAY:
2285 case -NFS4ERR_GRACE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002286 ssleep(1);
2287 return -EAGAIN;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002288 case -ENOMEM:
2289 case -NFS4ERR_DENIED:
NeilBrowndce26302017-12-13 09:57:09 +11002290 if (fl) {
2291 struct nfs4_lock_state *lsp = fl->fl_u.nfs4_fl.owner;
2292 if (lsp)
2293 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2294 }
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002295 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002296 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 return err;
2298}
2299
Trond Myklebust24311f82015-09-20 10:50:17 -04002300int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002301 struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002302{
2303 struct nfs_server *server = NFS_SERVER(state->inode);
2304 struct nfs4_opendata *opendata;
Trond Myklebust24311f82015-09-20 10:50:17 -04002305 int err = 0;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002306
2307 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2308 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2309 if (IS_ERR(opendata))
2310 return PTR_ERR(opendata);
2311 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002312 if (!test_bit(NFS_O_RDWR_STATE, &state->flags)) {
Trond Myklebust24311f82015-09-20 10:50:17 -04002313 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2314 if (err)
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002315 goto out;
2316 }
2317 if (!test_bit(NFS_O_WRONLY_STATE, &state->flags)) {
Trond Myklebust24311f82015-09-20 10:50:17 -04002318 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2319 if (err)
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002320 goto out;
Trond Myklebust24311f82015-09-20 10:50:17 -04002321 }
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002322 if (!test_bit(NFS_O_RDONLY_STATE, &state->flags)) {
2323 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2324 if (err)
2325 goto out;
2326 }
2327 nfs_state_clear_delegation(state);
2328out:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002329 nfs4_opendata_put(opendata);
NeilBrowndce26302017-12-13 09:57:09 +11002330 return nfs4_handle_delegation_recall_error(server, state, stateid, NULL, err);
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002331}
2332
Chuck Leverbe05c862013-08-09 12:49:47 -04002333static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2334{
2335 struct nfs4_opendata *data = calldata;
2336
Anna Schumaker7981c8a2017-01-10 11:39:53 -05002337 nfs4_setup_sequence(data->o_arg.server->nfs_client,
2338 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04002339}
2340
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002341static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2342{
2343 struct nfs4_opendata *data = calldata;
2344
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002345 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04002346
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002347 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002348 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05002349 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05002350 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002351 renew_lease(data->o_res.server, data->timestamp);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002352 data->rpc_done = true;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002353 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002354}
2355
2356static void nfs4_open_confirm_release(void *calldata)
2357{
2358 struct nfs4_opendata *data = calldata;
2359 struct nfs4_state *state = NULL;
2360
2361 /* If this request hasn't been cancelled, do nothing */
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002362 if (!data->cancelled)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002363 goto out_free;
2364 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002365 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002366 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002367 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002368 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002369 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002370out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002371 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002372}
2373
2374static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04002375 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002376 .rpc_call_done = nfs4_open_confirm_done,
2377 .rpc_release = nfs4_open_confirm_release,
2378};
2379
2380/*
2381 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2382 */
2383static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2384{
David Howells2b0143b2015-03-17 22:25:59 +00002385 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002386 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002387 struct rpc_message msg = {
2388 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2389 .rpc_argp = &data->c_arg,
2390 .rpc_resp = &data->c_res,
2391 .rpc_cred = data->owner->so_cred,
2392 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002393 struct rpc_task_setup task_setup_data = {
2394 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002395 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002396 .callback_ops = &nfs4_open_confirm_ops,
2397 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002398 .workqueue = nfsiod_workqueue,
Trond Myklebust61296502020-02-07 19:38:12 -05002399 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002400 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 int status;
2402
Anna Schumakerfba83f32018-05-04 16:22:50 -04002403 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1,
2404 data->is_recover);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002405 kref_get(&data->kref);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002406 data->rpc_done = false;
Trond Myklebust3e309912007-07-07 13:19:59 -04002407 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002408 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002409 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05002410 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002411 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002412 status = rpc_wait_for_completion_task(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002413 if (status != 0) {
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002414 data->cancelled = true;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002415 smp_wmb();
2416 } else
2417 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05002418 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 return status;
2420}
2421
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002422static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002424 struct nfs4_opendata *data = calldata;
2425 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04002426 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust2a606182015-08-19 22:30:00 -05002427 enum open_claim_type4 claim = data->o_arg.claim;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002428
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002429 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002430 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002431 /*
2432 * Check if we still need to send an OPEN call, or if we can use
2433 * a delegation instead.
2434 */
2435 if (data->state != NULL) {
2436 struct nfs_delegation *delegation;
2437
Trond Myklebustbe189f72018-09-27 17:12:33 -04002438 if (can_open_cached(data->state, data->o_arg.fmode,
2439 data->o_arg.open_flags, claim))
Trond Myklebust6ee41262007-07-08 14:11:36 -04002440 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002441 rcu_read_lock();
Trond Myklebustbe3df3d2019-10-31 18:40:32 -04002442 delegation = nfs4_get_valid_delegation(data->state->inode);
Trond Myklebust2a606182015-08-19 22:30:00 -05002443 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
Trond Myklebust652f89f2011-12-09 19:05:58 -05002444 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002445 rcu_read_unlock();
2446 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04002447 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04002448 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust2a606182015-08-19 22:30:00 -05002449 switch (claim) {
2450 default:
2451 break;
Trond Myklebust8188df12013-04-23 14:31:19 -04002452 case NFS4_OPEN_CLAIM_PREVIOUS:
2453 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2454 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04002455 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002456 fallthrough;
Trond Myklebust8188df12013-04-23 14:31:19 -04002457 case NFS4_OPEN_CLAIM_FH:
2458 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002459 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002460 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05002461 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
Andy Adamsond8985282009-04-01 09:22:21 -04002462 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002463 &data->o_res.seq_res,
2464 task) != 0)
2465 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04002466
2467 /* Set the create mode (note dependency on the session type) */
2468 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2469 if (data->o_arg.open_flags & O_EXCL) {
2470 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2471 if (nfs4_has_persistent_session(clp))
2472 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2473 else if (clp->cl_mvops->minor_version > 0)
2474 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2475 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04002476 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05002477unlock_no_action:
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002478 trace_nfs4_cached_open(data->state);
Trond Myklebust652f89f2011-12-09 19:05:58 -05002479 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04002480out_no_action:
2481 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002482out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002483 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002484}
2485
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002486static void nfs4_open_done(struct rpc_task *task, void *calldata)
2487{
2488 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002490 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04002491
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002492 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04002493 return;
Andy Adamsond8985282009-04-01 09:22:21 -04002494
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002495 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07002496 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2497 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07002498 case S_IFREG:
2499 break;
2500 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002501 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002502 break;
2503 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002504 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002505 break;
2506 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002507 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07002508 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07002509 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002510 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04002511 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2512 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07002513 }
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002514 data->rpc_done = true;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002515}
Trond Myklebust6f926b52005-10-18 14:20:18 -07002516
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002517static void nfs4_open_release(void *calldata)
2518{
2519 struct nfs4_opendata *data = calldata;
2520 struct nfs4_state *state = NULL;
2521
2522 /* If this request hasn't been cancelled, do nothing */
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002523 if (!data->cancelled)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002524 goto out_free;
2525 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002526 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002527 goto out_free;
2528 /* In case we need an open_confirm, no cleanup! */
2529 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2530 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002531 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002532 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002533 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002534out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002535 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002536}
2537
2538static const struct rpc_call_ops nfs4_open_ops = {
2539 .rpc_call_prepare = nfs4_open_prepare,
2540 .rpc_call_done = nfs4_open_done,
2541 .rpc_release = nfs4_open_release,
2542};
2543
Fred Isaman3b65a302016-09-19 10:06:49 -04002544static int nfs4_run_open_task(struct nfs4_opendata *data,
2545 struct nfs_open_context *ctx)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002546{
David Howells2b0143b2015-03-17 22:25:59 +00002547 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002548 struct nfs_server *server = NFS_SERVER(dir);
2549 struct nfs_openargs *o_arg = &data->o_arg;
2550 struct nfs_openres *o_res = &data->o_res;
2551 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002552 struct rpc_message msg = {
2553 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2554 .rpc_argp = o_arg,
2555 .rpc_resp = o_res,
2556 .rpc_cred = data->owner->so_cred,
2557 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002558 struct rpc_task_setup task_setup_data = {
2559 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002560 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002561 .callback_ops = &nfs4_open_ops,
2562 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002563 .workqueue = nfsiod_workqueue,
Trond Myklebust61296502020-02-07 19:38:12 -05002564 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002565 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002566 int status;
2567
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002568 kref_get(&data->kref);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002569 data->rpc_done = false;
Trond Myklebust3e309912007-07-07 13:19:59 -04002570 data->rpc_status = 0;
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002571 data->cancelled = false;
2572 data->is_recover = false;
Fred Isaman3b65a302016-09-19 10:06:49 -04002573 if (!ctx) {
2574 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 1);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002575 data->is_recover = true;
Trond Myklebust67e7b522019-08-07 07:31:27 -04002576 task_setup_data.flags |= RPC_TASK_TIMEOUT;
Fred Isaman2409a972016-10-06 12:11:21 -04002577 } else {
Fred Isaman3b65a302016-09-19 10:06:49 -04002578 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 0);
Fred Isaman2409a972016-10-06 12:11:21 -04002579 pnfs_lgopen_prepare(data, ctx);
2580 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04002581 task = rpc_run_task(&task_setup_data);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002582 if (IS_ERR(task))
2583 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002584 status = rpc_wait_for_completion_task(task);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002585 if (status != 0) {
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002586 data->cancelled = true;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002587 smp_wmb();
2588 } else
2589 status = data->rpc_status;
2590 rpc_put_task(task);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002591
2592 return status;
2593}
2594
2595static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2596{
David Howells2b0143b2015-03-17 22:25:59 +00002597 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002598 struct nfs_openres *o_res = &data->o_res;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002599 int status;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002600
Fred Isaman3b65a302016-09-19 10:06:49 -04002601 status = nfs4_run_open_task(data, NULL);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002602 if (status != 0 || !data->rpc_done)
2603 return status;
2604
Trond Myklebust6926afd12012-01-07 13:22:46 -05002605 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2606
Anna Schumakerd7e98252017-01-11 16:13:29 -05002607 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002608 status = _nfs4_proc_open_confirm(data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002609
2610 return status;
2611}
2612
Trond Myklebustf3792d62014-07-10 08:54:32 -04002613/*
2614 * Additional permission checks in order to distinguish between an
2615 * open for read, and an open for execute. This works around the
2616 * fact that NFSv4 OPEN treats read and execute permissions as being
2617 * the same.
2618 * Note that in the non-execute case, we want to turn off permission
2619 * checking if we just created a new file (POSIX open() semantics).
2620 */
NeilBrowna52458b2018-12-03 11:30:31 +11002621static int nfs4_opendata_access(const struct cred *cred,
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002622 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002623 struct nfs4_state *state, fmode_t fmode,
2624 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002625{
2626 struct nfs_access_entry cache;
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002627 u32 mask, flags;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002628
2629 /* access call failed or for some reason the server doesn't
2630 * support any access modes -- defer access call until later */
2631 if (opendata->o_res.access_supported == 0)
2632 return 0;
2633
2634 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002635 /*
2636 * Use openflags to check for exec, because fmode won't
2637 * always have FMODE_EXEC set when file open for exec.
2638 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002639 if (openflags & __FMODE_EXEC) {
2640 /* ONLY check for exec rights */
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002641 if (S_ISDIR(state->inode->i_mode))
2642 mask = NFS4_ACCESS_LOOKUP;
2643 else
2644 mask = NFS4_ACCESS_EXECUTE;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002645 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002646 mask = NFS4_ACCESS_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002647
2648 cache.cred = cred;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002649 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2650 nfs_access_add_cache(state->inode, &cache);
2651
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002652 flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP;
2653 if ((mask & ~cache.mask & flags) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002654 return 0;
2655
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002656 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002657}
2658
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002659/*
2660 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2661 */
Fred Isaman3b65a302016-09-19 10:06:49 -04002662static int _nfs4_proc_open(struct nfs4_opendata *data,
2663 struct nfs_open_context *ctx)
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002664{
David Howells2b0143b2015-03-17 22:25:59 +00002665 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002666 struct nfs_server *server = NFS_SERVER(dir);
2667 struct nfs_openargs *o_arg = &data->o_arg;
2668 struct nfs_openres *o_res = &data->o_res;
2669 int status;
2670
Fred Isaman3b65a302016-09-19 10:06:49 -04002671 status = nfs4_run_open_task(data, ctx);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002672 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002673 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002674 if (status != 0) {
2675 if (status == -NFS4ERR_BADNAME &&
2676 !(o_arg->open_flags & O_CREAT))
2677 return -ENOENT;
2678 return status;
2679 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002680
Trond Myklebust6926afd12012-01-07 13:22:46 -05002681 nfs_fattr_map_and_free_names(server, &data->f_attr);
2682
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002683 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002684 if (o_arg->open_flags & O_EXCL)
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002685 data->file_created = true;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002686 else if (o_res->cinfo.before != o_res->cinfo.after)
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002687 data->file_created = true;
Jeff Layton1eb5d982018-01-09 08:21:17 -05002688 if (data->file_created ||
2689 inode_peek_iversion_raw(dir) != o_res->cinfo.after)
Frank van der Linden1b523ca2020-06-23 22:38:59 +00002690 nfs4_update_changeattr(dir, &o_res->cinfo,
2691 o_res->f_attr->time_start,
2692 NFS_INO_INVALID_DATA);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002693 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002694 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2695 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002697 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002699 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 }
Trond Myklebust56e0d712017-04-15 19:20:01 -04002701 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2702 nfs4_sequence_free_slot(&o_res->seq_res);
Trond Myklebusta841b542018-04-07 13:50:59 -04002703 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr,
2704 o_res->f_label, NULL);
Trond Myklebust56e0d712017-04-15 19:20:01 -04002705 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002706 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707}
2708
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709/*
2710 * OPEN_EXPIRED:
2711 * reclaim state on the server after a network partition.
2712 * Assumes caller holds the appropriate lock
2713 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002714static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002716 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002717 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002719 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002720 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002721 if (IS_ERR(opendata))
2722 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002723 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002724 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002725 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002726 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002727 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728}
2729
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002730static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002731{
Trond Myklebust539cd032007-06-05 11:46:42 -04002732 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002733 struct nfs4_exception exception = { };
2734 int err;
2735
2736 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002737 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002738 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002739 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2740 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002741 switch (err) {
2742 default:
2743 goto out;
2744 case -NFS4ERR_GRACE:
2745 case -NFS4ERR_DELAY:
2746 nfs4_handle_exception(server, err, &exception);
2747 err = 0;
2748 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002749 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002750out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002751 return err;
2752}
2753
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2755{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002757 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758
Trond Myklebust864472e2006-01-03 09:55:15 +01002759 ctx = nfs4_state_find_open_context(state);
2760 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002761 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002762 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002763 put_nfs_open_context(ctx);
2764 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765}
2766
Trond Myklebust41020b62016-09-22 13:38:58 -04002767static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2768 const nfs4_stateid *stateid)
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002769{
Trond Myklebust41020b62016-09-22 13:38:58 -04002770 nfs_remove_bad_delegation(state->inode, stateid);
Trond Myklebust9f0c5122018-09-05 14:07:15 -04002771 nfs_state_clear_delegation(state);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002772}
2773
2774static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2775{
2776 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
Trond Myklebust41020b62016-09-22 13:38:58 -04002777 nfs_finish_clear_delegation_stateid(state, NULL);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002778}
2779
2780static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2781{
2782 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2783 nfs40_clear_delegation_stateid(state);
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002784 nfs_state_clear_open_state_flags(state);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002785 return nfs4_open_expired(sp, state);
2786}
2787
Trond Myklebust45870d62016-09-22 13:38:59 -04002788static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2789 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11002790 const struct cred *cred)
Trond Myklebust45870d62016-09-22 13:38:59 -04002791{
2792 return -NFS4ERR_BAD_STATEID;
2793}
2794
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002795#if defined(CONFIG_NFS_V4_1)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002796static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2797 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11002798 const struct cred *cred)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002799{
2800 int status;
2801
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002802 switch (stateid->type) {
2803 default:
2804 break;
2805 case NFS4_INVALID_STATEID_TYPE:
2806 case NFS4_SPECIAL_STATEID_TYPE:
2807 return -NFS4ERR_BAD_STATEID;
2808 case NFS4_REVOKED_STATEID_TYPE:
2809 goto out_free;
2810 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002811
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002812 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002813 switch (status) {
2814 case -NFS4ERR_EXPIRED:
2815 case -NFS4ERR_ADMIN_REVOKED:
2816 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002817 break;
2818 default:
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002819 return status;
2820 }
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002821out_free:
2822 /* Ack the revoked state to the server */
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04002823 nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002824 return -NFS4ERR_EXPIRED;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002825}
2826
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002827static int nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002828{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002829 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002830 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002831 struct nfs_delegation *delegation;
NeilBrowna52458b2018-12-03 11:30:31 +11002832 const struct cred *cred = NULL;
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002833 int status, ret = NFS_OK;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002834
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002835 /* Get the delegation credential for use by test/free_stateid */
2836 rcu_read_lock();
2837 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002838 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002839 rcu_read_unlock();
Trond Myklebust9f0c5122018-09-05 14:07:15 -04002840 nfs_state_clear_delegation(state);
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002841 return NFS_OK;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002842 }
2843
Trond Myklebustfc51b1c2020-04-02 15:27:09 -04002844 spin_lock(&delegation->lock);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002845 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002846
Trond Myklebust994b15b2018-09-05 14:07:14 -04002847 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2848 &delegation->flags)) {
Trond Myklebustfc51b1c2020-04-02 15:27:09 -04002849 spin_unlock(&delegation->lock);
Trond Myklebust994b15b2018-09-05 14:07:14 -04002850 rcu_read_unlock();
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002851 return NFS_OK;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002852 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002853
NeilBrowna52458b2018-12-03 11:30:31 +11002854 if (delegation->cred)
2855 cred = get_cred(delegation->cred);
Trond Myklebustfc51b1c2020-04-02 15:27:09 -04002856 spin_unlock(&delegation->lock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002857 rcu_read_unlock();
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002858 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002859 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002860 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
Trond Myklebust41020b62016-09-22 13:38:58 -04002861 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002862 else
2863 ret = status;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002864
Trond Myklebust8c39a392019-07-19 13:48:44 -04002865 put_cred(cred);
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002866 return ret;
2867}
2868
2869static void nfs41_delegation_recover_stateid(struct nfs4_state *state)
2870{
2871 nfs4_stateid tmp;
2872
2873 if (test_bit(NFS_DELEGATED_STATE, &state->flags) &&
2874 nfs4_copy_delegation_stateid(state->inode, state->state,
2875 &tmp, NULL) &&
2876 nfs4_stateid_match_other(&state->stateid, &tmp))
2877 nfs_state_set_delegation(state, &tmp, state->state);
2878 else
2879 nfs_state_clear_delegation(state);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002880}
2881
2882/**
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002883 * nfs41_check_expired_locks - possibly free a lock stateid
2884 *
2885 * @state: NFSv4 state for an inode
2886 *
2887 * Returns NFS_OK if recovery for this stateid is now finished.
2888 * Otherwise a negative NFS4ERR value is returned.
2889 */
2890static int nfs41_check_expired_locks(struct nfs4_state *state)
2891{
2892 int status, ret = NFS_OK;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002893 struct nfs4_lock_state *lsp, *prev = NULL;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002894 struct nfs_server *server = NFS_SERVER(state->inode);
2895
2896 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2897 goto out;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002898
2899 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002900 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2901 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
NeilBrowna52458b2018-12-03 11:30:31 +11002902 const struct cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002903
Elena Reshetova194bc1f2017-10-20 12:53:36 +03002904 refcount_inc(&lsp->ls_count);
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002905 spin_unlock(&state->state_lock);
2906
2907 nfs4_put_lock_state(prev);
2908 prev = lsp;
2909
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002910 status = nfs41_test_and_free_expired_stateid(server,
2911 &lsp->ls_stateid,
2912 cred);
2913 trace_nfs4_test_lock_stateid(state, lsp, status);
2914 if (status == -NFS4ERR_EXPIRED ||
2915 status == -NFS4ERR_BAD_STATEID) {
2916 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002917 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002918 if (!recover_lost_locks)
2919 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2920 } else if (status != NFS_OK) {
2921 ret = status;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002922 nfs4_put_lock_state(prev);
2923 goto out;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002924 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002925 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002926 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002927 }
2928 spin_unlock(&state->state_lock);
2929 nfs4_put_lock_state(prev);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002930out:
2931 return ret;
2932}
2933
2934/**
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002935 * nfs41_check_open_stateid - possibly free an open stateid
2936 *
2937 * @state: NFSv4 state for an inode
2938 *
2939 * Returns NFS_OK if recovery for this stateid is now finished.
2940 * Otherwise a negative NFS4ERR value is returned.
2941 */
2942static int nfs41_check_open_stateid(struct nfs4_state *state)
2943{
2944 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002945 nfs4_stateid *stateid = &state->open_stateid;
NeilBrowna52458b2018-12-03 11:30:31 +11002946 const struct cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002947 int status;
2948
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002949 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002950 return -NFS4ERR_BAD_STATEID;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002951 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002952 trace_nfs4_test_open_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002953 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002954 nfs_state_clear_open_state_flags(state);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002955 stateid->type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002956 return status;
Trond Myklebustc0ca0e52017-08-08 21:39:28 -04002957 }
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002958 if (nfs_open_stateid_recover_openmode(state))
2959 return -NFS4ERR_OPENMODE;
2960 return NFS_OK;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002961}
2962
2963static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2964{
Chuck Levereb64cf92012-07-11 16:30:05 -04002965 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002966
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002967 status = nfs41_check_delegation_stateid(state);
2968 if (status != NFS_OK)
2969 return status;
2970 nfs41_delegation_recover_stateid(state);
2971
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002972 status = nfs41_check_expired_locks(state);
2973 if (status != NFS_OK)
2974 return status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002975 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002976 if (status != NFS_OK)
2977 status = nfs4_open_expired(sp, state);
2978 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002979}
2980#endif
2981
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002983 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2984 * fields corresponding to attributes that were used to store the verifier.
2985 * Make sure we clobber those fields in the later setattr call
2986 */
Trond Myklebust609339c2018-03-28 16:18:17 -04002987static unsigned nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002988 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002989{
Trond Myklebust609339c2018-03-28 16:18:17 -04002990 const __u32 *bitmask = opendata->o_arg.server->exclcreat_bitmask;
2991 __u32 attrset[3];
2992 unsigned ret;
2993 unsigned i;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002994
Trond Myklebust609339c2018-03-28 16:18:17 -04002995 for (i = 0; i < ARRAY_SIZE(attrset); i++) {
2996 attrset[i] = opendata->o_res.attrset[i];
2997 if (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE4_1)
2998 attrset[i] &= ~bitmask[i];
2999 }
Jeff Laytonaa53ed52007-06-05 14:49:03 -04003000
Trond Myklebust609339c2018-03-28 16:18:17 -04003001 ret = (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE) ?
3002 sattr->ia_valid : 0;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08003003
Trond Myklebust609339c2018-03-28 16:18:17 -04003004 if ((attrset[1] & (FATTR4_WORD1_TIME_ACCESS|FATTR4_WORD1_TIME_ACCESS_SET))) {
3005 if (sattr->ia_valid & ATTR_ATIME_SET)
3006 ret |= ATTR_ATIME_SET;
3007 else
3008 ret |= ATTR_ATIME;
3009 }
Kinglong Mee5334c5b2015-08-26 21:13:37 +08003010
Trond Myklebust609339c2018-03-28 16:18:17 -04003011 if ((attrset[1] & (FATTR4_WORD1_TIME_MODIFY|FATTR4_WORD1_TIME_MODIFY_SET))) {
3012 if (sattr->ia_valid & ATTR_MTIME_SET)
3013 ret |= ATTR_MTIME_SET;
3014 else
3015 ret |= ATTR_MTIME;
3016 }
3017
3018 if (!(attrset[2] & FATTR4_WORD2_SECURITY_LABEL))
Kinglong Mee5334c5b2015-08-26 21:13:37 +08003019 *label = NULL;
Trond Myklebust609339c2018-03-28 16:18:17 -04003020 return ret;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04003021}
3022
Trond Myklebustc21443c2013-02-07 14:26:21 -05003023static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003024 int flags, struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05003025{
3026 struct nfs4_state_owner *sp = opendata->owner;
3027 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04003028 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05003029 struct nfs4_state *state;
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003030 fmode_t acc_mode = _nfs4_ctx_to_accessmode(ctx);
Trond Myklebustcf5b4052020-02-05 09:01:53 -05003031 struct inode *dir = d_inode(opendata->dir);
3032 unsigned long dir_verifier;
Trond Myklebustc21443c2013-02-07 14:26:21 -05003033 unsigned int seq;
3034 int ret;
3035
3036 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
Trond Myklebustcf5b4052020-02-05 09:01:53 -05003037 dir_verifier = nfs_save_change_attribute(dir);
Trond Myklebustc21443c2013-02-07 14:26:21 -05003038
Fred Isaman3b65a302016-09-19 10:06:49 -04003039 ret = _nfs4_proc_open(opendata, ctx);
Trond Myklebustdca780012014-10-23 19:23:03 +03003040 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05003041 goto out;
3042
Trond Myklebustae55e592018-05-22 11:17:16 -04003043 state = _nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebustc21443c2013-02-07 14:26:21 -05003044 ret = PTR_ERR(state);
3045 if (IS_ERR(state))
3046 goto out;
Trond Myklebusta974dee2017-02-08 11:29:46 -05003047 ctx->state = state;
Trond Myklebustc21443c2013-02-07 14:26:21 -05003048 if (server->caps & NFS_CAP_POSIX_LOCK)
3049 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Jeff Laytona8ce3772016-09-17 18:17:35 -04003050 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
3051 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05003052
Trond Myklebust275bb302013-05-29 13:11:28 -04003053 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00003054 if (d_really_is_negative(dentry)) {
Al Viro668d0cd2016-03-08 12:44:17 -05003055 struct dentry *alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04003056 d_drop(dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05003057 alias = d_exact_alias(dentry, state->inode);
3058 if (!alias)
3059 alias = d_splice_alias(igrab(state->inode), dentry);
3060 /* d_splice_alias() can't fail here - it's a non-directory */
3061 if (alias) {
Trond Myklebust275bb302013-05-29 13:11:28 -04003062 dput(ctx->dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05003063 ctx->dentry = dentry = alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04003064 }
Trond Myklebustcf5b4052020-02-05 09:01:53 -05003065 }
3066
3067 switch(opendata->o_arg.claim) {
3068 default:
3069 break;
3070 case NFS4_OPEN_CLAIM_NULL:
3071 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
3072 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
3073 if (!opendata->rpc_done)
3074 break;
3075 if (opendata->o_res.delegation_type != 0)
3076 dir_verifier = nfs_save_change_attribute(dir);
3077 nfs_set_verifier(dentry, dir_verifier);
Trond Myklebust275bb302013-05-29 13:11:28 -04003078 }
3079
Trond Myklebustaf9b6d72018-06-29 12:45:53 -04003080 /* Parse layoutget results before we check for access */
3081 pnfs_parse_lgopen(state->inode, opendata->lgp, ctx);
3082
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003083 ret = nfs4_opendata_access(sp->so_cred, opendata, state,
3084 acc_mode, flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05003085 if (ret != 0)
3086 goto out;
3087
David Howells2b0143b2015-03-17 22:25:59 +00003088 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04003089 nfs_inode_attach_open_context(ctx);
3090 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
3091 nfs4_schedule_stateid_recovery(server, state);
3092 }
Fred Isaman2409a972016-10-06 12:11:21 -04003093
Trond Myklebustc21443c2013-02-07 14:26:21 -05003094out:
Olga Kornievskaia0cb98ab2019-03-19 12:12:13 -04003095 if (!opendata->cancelled)
3096 nfs4_sequence_free_slot(&opendata->o_res.seq_res);
Trond Myklebustc21443c2013-02-07 14:26:21 -05003097 return ret;
3098}
3099
Jeff Laytonaa53ed52007-06-05 14:49:03 -04003100/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01003101 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 */
Andy Adamson82be4172012-05-23 05:02:35 -04003103static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04003104 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04003105 int flags,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003106 const struct nfs4_open_createattrs *c,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003107 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108{
3109 struct nfs4_state_owner *sp;
3110 struct nfs4_state *state = NULL;
3111 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01003112 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04003113 struct dentry *dentry = ctx->dentry;
NeilBrowna52458b2018-12-03 11:30:31 +11003114 const struct cred *cred = ctx->cred;
Trond Myklebust4197a052013-05-29 12:37:49 -04003115 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003116 fmode_t fmode = _nfs4_ctx_to_openmode(ctx);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003117 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003118 struct iattr *sattr = c->sattr;
3119 struct nfs4_label *label = c->label;
David Quigley1775fd32013-05-22 12:50:42 -04003120 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04003121 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122
3123 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05003125 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
3126 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
3128 goto out_err;
3129 }
Anna Schumaker334f87d2017-01-11 16:17:17 -05003130 status = nfs4_client_recover_expired_lease(server->nfs_client);
Trond Myklebust58d97142006-01-03 09:55:24 +01003131 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003132 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00003133 if (d_really_is_positive(dentry))
3134 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01003135 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00003136 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003137 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003138 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
3139 c, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01003140 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05003141 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142
David Quigley14c43f72013-05-22 12:50:43 -04003143 if (label) {
3144 olabel = nfs4_label_alloc(server, GFP_KERNEL);
3145 if (IS_ERR(olabel)) {
3146 status = PTR_ERR(olabel);
3147 goto err_opendata_put;
3148 }
3149 }
3150
Trond Myklebuste911b812014-03-26 13:24:37 -07003151 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
3152 if (!opendata->f_attr.mdsthreshold) {
3153 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
3154 if (!opendata->f_attr.mdsthreshold)
3155 goto err_free_label;
3156 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04003157 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04003158 }
David Howells2b0143b2015-03-17 22:25:59 +00003159 if (d_really_is_positive(dentry))
3160 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04003161
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003162 status = _nfs4_open_and_get_state(opendata, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003163 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04003164 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04003165 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003166
NeilBrownefcbc042015-07-30 13:00:56 +10003167 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04003168 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Trond Myklebust609339c2018-03-28 16:18:17 -04003169 unsigned attrs = nfs4_exclusive_attrset(opendata, sattr, &label);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02003170 /*
3171 * send create attributes which was not set by open
3172 * with an extra setattr.
3173 */
Trond Myklebust609339c2018-03-28 16:18:17 -04003174 if (attrs || label) {
3175 unsigned ia_old = sattr->ia_valid;
3176
3177 sattr->ia_valid = attrs;
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02003178 nfs_fattr_init(opendata->o_res.f_attr);
3179 status = nfs4_do_setattr(state->inode, cred,
3180 opendata->o_res.f_attr, sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11003181 ctx, label, olabel);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02003182 if (status == 0) {
3183 nfs_setattr_update_inode(state->inode, sattr,
3184 opendata->o_res.f_attr);
3185 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
3186 }
Trond Myklebust609339c2018-03-28 16:18:17 -04003187 sattr->ia_valid = ia_old;
David Quigley1775fd32013-05-22 12:50:42 -04003188 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04003189 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08003190 if (opened && opendata->file_created)
Al Viro73a09dd2018-06-08 13:22:02 -04003191 *opened = 1;
Andy Adamson82be4172012-05-23 05:02:35 -04003192
Trond Myklebuste911b812014-03-26 13:24:37 -07003193 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04003194 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07003195 opendata->f_attr.mdsthreshold = NULL;
3196 }
Andy Adamson82be4172012-05-23 05:02:35 -04003197
David Quigley14c43f72013-05-22 12:50:43 -04003198 nfs4_label_free(olabel);
3199
Trond Myklebustc6d00e62007-06-17 16:02:44 -04003200 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04003203err_free_label:
3204 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04003205err_opendata_put:
3206 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01003207err_put_state_owner:
3208 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 return status;
3211}
3212
3213
Andy Adamson82be4172012-05-23 05:02:35 -04003214static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04003215 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04003216 int flags,
3217 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003218 struct nfs4_label *label,
3219 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003221 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust0688e642019-04-07 13:59:09 -04003222 struct nfs4_exception exception = {
3223 .interruptible = true,
3224 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 struct nfs4_state *res;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003226 struct nfs4_open_createattrs c = {
3227 .label = label,
3228 .sattr = sattr,
3229 .verf = {
3230 [0] = (__u32)jiffies,
3231 [1] = (__u32)current->pid,
3232 },
3233 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 int status;
3235
3236 do {
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003237 status = _nfs4_do_open(dir, ctx, flags, &c, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04003238 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04003239 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 if (status == 0)
3241 break;
3242 /* NOTE: BAD_SEQID means the server and client disagree about the
3243 * book-keeping w.r.t. state-changing operations
3244 * (OPEN/CLOSE/LOCK/LOCKU...)
3245 * It is actually a sign of a bug on the client or on the server.
3246 *
3247 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07003248 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249 * have unhashed the old state_owner for us, and that we can
3250 * therefore safely retry using a new one. We should still warn
3251 * the user though...
3252 */
3253 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04003254 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04003255 " returned a bad sequence-id error!\n",
3256 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 exception.retry = 1;
3258 continue;
3259 }
Trond Myklebust550f5742005-10-18 14:20:21 -07003260 /*
3261 * BAD_STATEID on OPEN means that the server cancelled our
3262 * state before it received the OPEN_CONFIRM.
3263 * Recover by retrying the request as per the discussion
3264 * on Page 181 of RFC3530.
3265 */
3266 if (status == -NFS4ERR_BAD_STATEID) {
3267 exception.retry = 1;
3268 continue;
3269 }
Robert Milkowski924491f2020-01-28 08:37:47 +00003270 if (status == -NFS4ERR_EXPIRED) {
3271 nfs4_schedule_lease_recovery(server->nfs_client);
3272 exception.retry = 1;
3273 continue;
3274 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04003275 if (status == -EAGAIN) {
3276 /* We must have found a delegation */
3277 exception.retry = 1;
3278 continue;
3279 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003280 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
3281 continue;
3282 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283 status, &exception));
3284 } while (exception.retry);
3285 return res;
3286}
3287
Trond Myklebust8487c472016-06-26 08:44:35 -04003288static int _nfs4_do_setattr(struct inode *inode,
3289 struct nfs_setattrargs *arg,
3290 struct nfs_setattrres *res,
NeilBrowna52458b2018-12-03 11:30:31 +11003291 const struct cred *cred,
NeilBrown29b59f92016-10-13 15:26:47 +11003292 struct nfs_open_context *ctx)
Trond Myklebust8487c472016-06-26 08:44:35 -04003293{
3294 struct nfs_server *server = NFS_SERVER(inode);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003295 struct rpc_message msg = {
Trond Myklebust8487c472016-06-26 08:44:35 -04003296 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
3297 .rpc_argp = arg,
3298 .rpc_resp = res,
3299 .rpc_cred = cred,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003300 };
NeilBrowna52458b2018-12-03 11:30:31 +11003301 const struct cred *delegation_cred = NULL;
Trond Myklebust8487c472016-06-26 08:44:35 -04003302 unsigned long timestamp = jiffies;
Trond Myklebust8487c472016-06-26 08:44:35 -04003303 bool truncate;
3304 int status;
3305
3306 nfs_fattr_init(res->fattr);
3307
3308 /* Servers should only apply open mode checks for file size changes */
3309 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
Chuck Lever644c9f42020-09-04 17:39:12 -04003310 if (!truncate) {
3311 nfs4_inode_make_writeable(inode);
Trond Myklebust991eedb2018-04-09 11:15:30 -04003312 goto zero_stateid;
Chuck Lever644c9f42020-09-04 17:39:12 -04003313 }
Trond Myklebust8487c472016-06-26 08:44:35 -04003314
Trond Myklebust991eedb2018-04-09 11:15:30 -04003315 if (nfs4_copy_delegation_stateid(inode, FMODE_WRITE, &arg->stateid, &delegation_cred)) {
Trond Myklebust8487c472016-06-26 08:44:35 -04003316 /* Use that stateid */
Trond Myklebust09a54f02019-08-03 10:28:18 -04003317 } else if (ctx != NULL && ctx->state) {
NeilBrown17393472016-10-13 15:26:47 +11003318 struct nfs_lock_context *l_ctx;
NeilBrown29b59f92016-10-13 15:26:47 +11003319 if (!nfs4_valid_open_stateid(ctx->state))
Trond Myklebust8487c472016-06-26 08:44:35 -04003320 return -EBADF;
NeilBrown17393472016-10-13 15:26:47 +11003321 l_ctx = nfs_get_lock_context(ctx);
3322 if (IS_ERR(l_ctx))
3323 return PTR_ERR(l_ctx);
NeilBrown7a0566b2016-12-06 15:50:06 -05003324 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
3325 &arg->stateid, &delegation_cred);
3326 nfs_put_lock_context(l_ctx);
3327 if (status == -EIO)
Trond Myklebust8487c472016-06-26 08:44:35 -04003328 return -EBADF;
Olga Kornievskaiad826e5b2019-12-18 16:50:42 -05003329 else if (status == -EAGAIN)
3330 goto zero_stateid;
Trond Myklebust991eedb2018-04-09 11:15:30 -04003331 } else {
3332zero_stateid:
Trond Myklebust8487c472016-06-26 08:44:35 -04003333 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
Trond Myklebust991eedb2018-04-09 11:15:30 -04003334 }
Trond Myklebust8487c472016-06-26 08:44:35 -04003335 if (delegation_cred)
3336 msg.rpc_cred = delegation_cred;
3337
3338 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
3339
NeilBrowna52458b2018-12-03 11:30:31 +11003340 put_cred(delegation_cred);
NeilBrown29b59f92016-10-13 15:26:47 +11003341 if (status == 0 && ctx != NULL)
Trond Myklebust8487c472016-06-26 08:44:35 -04003342 renew_lease(server, timestamp);
3343 trace_nfs4_setattr(inode, &arg->stateid, status);
3344 return status;
3345}
3346
NeilBrowna52458b2018-12-03 11:30:31 +11003347static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
Trond Myklebust8487c472016-06-26 08:44:35 -04003348 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11003349 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
Trond Myklebust8487c472016-06-26 08:44:35 -04003350 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05003352 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebust30846df2018-04-07 13:44:28 -04003353 __u32 bitmask[NFS4_BITMASK_SZ];
NeilBrown29b59f92016-10-13 15:26:47 +11003354 struct nfs4_state *state = ctx ? ctx->state : NULL;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003355 struct nfs_setattrargs arg = {
3356 .fh = NFS_FH(inode),
3357 .iap = sattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 .server = server,
Trond Myklebust30846df2018-04-07 13:44:28 -04003359 .bitmask = bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04003360 .label = ilabel,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003361 };
3362 struct nfs_setattrres res = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003364 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365 .server = server,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003366 };
Trond Myklebust8487c472016-06-26 08:44:35 -04003367 struct nfs4_exception exception = {
3368 .state = state,
3369 .inode = inode,
3370 .stateid = &arg.stateid,
3371 };
Trond Myklebusta71029b2021-04-10 00:23:03 -04003372 unsigned long adjust_flags = NFS_INO_INVALID_CHANGE;
Trond Myklebust8487c472016-06-26 08:44:35 -04003373 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374
Trond Myklebust720869e2021-04-13 09:41:16 -04003375 if (sattr->ia_valid & (ATTR_MODE | ATTR_KILL_SUID | ATTR_KILL_SGID))
3376 adjust_flags |= NFS_INO_INVALID_MODE;
3377 if (sattr->ia_valid & (ATTR_UID | ATTR_GID))
Trond Myklebusta71029b2021-04-10 00:23:03 -04003378 adjust_flags |= NFS_INO_INVALID_OTHER;
3379
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380 do {
Trond Myklebusta71029b2021-04-10 00:23:03 -04003381 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, olabel),
3382 inode, adjust_flags);
Trond Myklebust30846df2018-04-07 13:44:28 -04003383
NeilBrown29b59f92016-10-13 15:26:47 +11003384 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
Trond Myklebust451146b2012-04-18 16:29:11 -04003385 switch (err) {
3386 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04003387 if (!(sattr->ia_valid & ATTR_SIZE)) {
3388 pr_warn_once("NFSv4: server %s is incorrectly "
3389 "applying open mode checks to "
3390 "a SETATTR that is not "
3391 "changing file size.\n",
3392 server->nfs_client->cl_hostname);
3393 }
Trond Myklebust451146b2012-04-18 16:29:11 -04003394 if (state && !(state->state & FMODE_WRITE)) {
3395 err = -EBADF;
3396 if (sattr->ia_valid & ATTR_OPEN)
3397 err = -EACCES;
3398 goto out;
3399 }
3400 }
3401 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04003403out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404 return err;
3405}
3406
Peng Tao500d7012015-09-22 11:35:22 +08003407static bool
3408nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3409{
3410 if (inode == NULL || !nfs_have_layout(inode))
3411 return false;
3412
3413 return pnfs_wait_on_layoutreturn(inode, task);
3414}
3415
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003416/*
3417 * Update the seqid of an open stateid
3418 */
3419static void nfs4_sync_open_stateid(nfs4_stateid *dst,
3420 struct nfs4_state *state)
3421{
3422 __be32 seqid_open;
3423 u32 dst_seqid;
3424 int seq;
3425
3426 for (;;) {
3427 if (!nfs4_valid_open_stateid(state))
3428 break;
3429 seq = read_seqbegin(&state->seqlock);
3430 if (!nfs4_state_match_open_stateid_other(state, dst)) {
3431 nfs4_stateid_copy(dst, &state->open_stateid);
3432 if (read_seqretry(&state->seqlock, seq))
3433 continue;
3434 break;
3435 }
3436 seqid_open = state->open_stateid.seqid;
3437 if (read_seqretry(&state->seqlock, seq))
3438 continue;
3439
3440 dst_seqid = be32_to_cpu(dst->seqid);
3441 if ((s32)(dst_seqid - be32_to_cpu(seqid_open)) < 0)
3442 dst->seqid = seqid_open;
3443 break;
3444 }
3445}
3446
3447/*
3448 * Update the seqid of an open stateid after receiving
3449 * NFS4ERR_OLD_STATEID
3450 */
3451static bool nfs4_refresh_open_old_stateid(nfs4_stateid *dst,
3452 struct nfs4_state *state)
3453{
3454 __be32 seqid_open;
3455 u32 dst_seqid;
3456 bool ret;
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04003457 int seq, status = -EAGAIN;
3458 DEFINE_WAIT(wait);
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003459
3460 for (;;) {
3461 ret = false;
3462 if (!nfs4_valid_open_stateid(state))
3463 break;
3464 seq = read_seqbegin(&state->seqlock);
3465 if (!nfs4_state_match_open_stateid_other(state, dst)) {
3466 if (read_seqretry(&state->seqlock, seq))
3467 continue;
3468 break;
3469 }
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04003470
3471 write_seqlock(&state->seqlock);
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003472 seqid_open = state->open_stateid.seqid;
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003473
3474 dst_seqid = be32_to_cpu(dst->seqid);
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04003475
3476 /* Did another OPEN bump the state's seqid? try again: */
3477 if ((s32)(be32_to_cpu(seqid_open) - dst_seqid) > 0) {
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003478 dst->seqid = seqid_open;
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04003479 write_sequnlock(&state->seqlock);
3480 ret = true;
3481 break;
3482 }
3483
3484 /* server says we're behind but we haven't seen the update yet */
3485 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
3486 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
3487 write_sequnlock(&state->seqlock);
3488 trace_nfs4_close_stateid_update_wait(state->inode, dst, 0);
3489
3490 if (signal_pending(current))
3491 status = -EINTR;
3492 else
3493 if (schedule_timeout(5*HZ) != 0)
3494 status = 0;
3495
3496 finish_wait(&state->waitq, &wait);
3497
3498 if (!status)
3499 continue;
3500 if (status == -EINTR)
3501 break;
3502
3503 /* we slept the whole 5 seconds, we must have lost a seqid */
3504 dst->seqid = cpu_to_be32(dst_seqid + 1);
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003505 ret = true;
3506 break;
3507 }
3508
3509 return ret;
3510}
3511
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512struct nfs4_closedata {
3513 struct inode *inode;
3514 struct nfs4_state *state;
3515 struct nfs_closeargs arg;
3516 struct nfs_closeres res;
Trond Myklebustcf805162016-11-15 14:56:07 -05003517 struct {
3518 struct nfs4_layoutreturn_args arg;
3519 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003520 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebustcf805162016-11-15 14:56:07 -05003521 u32 roc_barrier;
3522 bool roc;
3523 } lr;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003524 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003525 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526};
3527
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003528static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07003529{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003530 struct nfs4_closedata *calldata = data;
3531 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04003532 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07003533
Trond Myklebustcf805162016-11-15 14:56:07 -05003534 if (calldata->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003535 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3536 calldata->res.lr_ret);
Trond Myklebust95121352005-10-18 14:20:12 -07003537 nfs4_put_open_state(calldata->state);
3538 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07003539 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05003540 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003541 kfree(calldata);
3542}
3543
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003544static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003546 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003549 nfs4_stateid *res_stateid = NULL;
Trond Myklebustb8b8d222017-11-07 10:51:37 -05003550 struct nfs4_exception exception = {
3551 .state = state,
3552 .inode = calldata->inode,
3553 .stateid = &calldata->arg.stateid,
3554 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555
Chuck Levera3ca5652012-03-01 17:00:40 -05003556 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04003557 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3558 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04003559 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Trond Myklebustcf805162016-11-15 14:56:07 -05003560
3561 /* Handle Layoutreturn errors */
Trond Myklebust078000d2021-01-04 13:18:03 -05003562 if (pnfs_roc_done(task, &calldata->arg.lr_args, &calldata->res.lr_res,
3563 &calldata->res.lr_ret) == -EAGAIN)
Trond Myklebust287a9c52019-09-20 07:23:41 -04003564 goto out_restart;
Trond Myklebustcf805162016-11-15 14:56:07 -05003565
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003566 /* hmm. we are done with the inode, and in the process of freeing
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567 * the state_owner. we keep this around to process errors
3568 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569 switch (task->tk_status) {
3570 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04003571 res_stateid = &calldata->res.stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003572 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003573 break;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003574 case -NFS4ERR_ACCESS:
3575 if (calldata->arg.bitmask != NULL) {
3576 calldata->arg.bitmask = NULL;
3577 calldata->res.fattr = NULL;
Trond Myklebust91b30d22017-11-06 15:28:09 -05003578 goto out_restart;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003579
3580 }
3581 break;
Trond Myklebust12f275c2017-11-06 15:28:05 -05003582 case -NFS4ERR_OLD_STATEID:
3583 /* Did we race with OPEN? */
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003584 if (nfs4_refresh_open_old_stateid(&calldata->arg.stateid,
Trond Myklebust91b30d22017-11-06 15:28:09 -05003585 state))
3586 goto out_restart;
Trond Myklebust12f275c2017-11-06 15:28:05 -05003587 goto out_release;
Trond Myklebust69794ad2013-11-20 12:57:19 -05003588 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589 case -NFS4ERR_STALE_STATEID:
Trond Myklebust26d36302016-09-22 13:39:05 -04003590 case -NFS4ERR_EXPIRED:
3591 nfs4_free_revoked_stateid(server,
3592 &calldata->arg.stateid,
3593 task->tk_msg.rpc_cred);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05003594 fallthrough;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003595 case -NFS4ERR_BAD_STATEID:
Trond Myklebuste217e822019-09-20 07:23:46 -04003596 if (calldata->arg.fmode == 0)
3597 break;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05003598 fallthrough;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599 default:
Trond Myklebustb8b8d222017-11-07 10:51:37 -05003600 task->tk_status = nfs4_async_handle_exception(task,
3601 server, task->tk_status, &exception);
3602 if (exception.retry)
Trond Myklebust91b30d22017-11-06 15:28:09 -05003603 goto out_restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07003605 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3606 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003607out_release:
Trond Myklebust91b30d22017-11-06 15:28:09 -05003608 task->tk_status = 0;
Trond Myklebust72211db2009-12-15 14:47:36 -05003609 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustd8d84982016-12-19 12:14:44 -05003610 nfs_refresh_inode(calldata->inode, &calldata->fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05003611 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Trond Myklebust91b30d22017-11-06 15:28:09 -05003612 return;
Trond Myklebust91b30d22017-11-06 15:28:09 -05003613out_restart:
3614 task->tk_status = 0;
3615 rpc_restart_call_prepare(task);
3616 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617}
3618
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003619static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003621 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07003622 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003623 struct inode *inode = calldata->inode;
Trond Myklebust332d1a02021-03-25 18:15:36 -04003624 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustc8bf7072018-06-15 16:31:02 -04003625 struct pnfs_layout_hdr *lo;
Trond Myklebustaee7af32014-08-25 22:33:12 -04003626 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05003627 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07003628
Chuck Levera3ca5652012-03-01 17:00:40 -05003629 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003630 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003631 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04003632
Trond Myklebust88069f72009-12-08 08:33:16 -05003633 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05003634 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04003635 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3636 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3637 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust003707c2007-07-05 18:07:55 -04003638 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003639 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01003640 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003641 if (state->n_rdonly == 0)
3642 call_close |= is_rdonly;
3643 else if (is_rdonly)
3644 calldata->arg.fmode |= FMODE_READ;
3645 if (state->n_wronly == 0)
3646 call_close |= is_wronly;
3647 else if (is_wronly)
3648 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebuste547f262016-06-25 19:19:28 -04003649 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3650 call_close |= is_rdwr;
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003651 } else if (is_rdwr)
3652 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3653
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003654 nfs4_sync_open_stateid(&calldata->arg.stateid, state);
3655 if (!nfs4_valid_open_stateid(state))
Trond Myklebust5d422302013-03-14 16:57:48 -04003656 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05003657 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05003658
3659 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003660 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003661 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07003662 }
Trond Myklebust88069f72009-12-08 08:33:16 -05003663
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003664 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
Peng Tao500d7012015-09-22 11:35:22 +08003665 nfs_release_seqid(calldata->arg.seqid);
3666 goto out_wait;
3667 }
3668
Trond Myklebustc8bf7072018-06-15 16:31:02 -04003669 lo = calldata->arg.lr_args ? calldata->arg.lr_args->layout : NULL;
3670 if (lo && !pnfs_layout_is_valid(lo)) {
3671 calldata->arg.lr_args = NULL;
3672 calldata->res.lr_res = NULL;
3673 }
3674
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003675 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05003676 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003677
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003678 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003679 /* Close-to-open cache consistency revalidation */
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04003680 if (!nfs4_have_delegation(inode, FMODE_READ)) {
Trond Myklebust332d1a02021-03-25 18:15:36 -04003681 nfs4_bitmask_set(calldata->arg.bitmask_store,
3682 server->cache_consistency_bitmask,
3683 inode, server, NULL);
3684 calldata->arg.bitmask = calldata->arg.bitmask_store;
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04003685 } else
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003686 calldata->arg.bitmask = NULL;
3687 }
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003688
Trond Myklebust6ae37332015-01-30 14:21:14 -05003689 calldata->arg.share_access =
3690 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3691 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05003692
Trond Myklebustd8d84982016-12-19 12:14:44 -05003693 if (calldata->res.fattr == NULL)
3694 calldata->arg.bitmask = NULL;
3695 else if (calldata->arg.bitmask == NULL)
3696 calldata->res.fattr = NULL;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003697 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05003698 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
Trond Myklebust9d12b212012-01-17 22:04:25 -05003699 &calldata->arg.seq_args,
3700 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04003701 task) != 0)
3702 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05003703 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003704 return;
3705out_no_action:
3706 task->tk_action = NULL;
3707out_wait:
3708 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709}
3710
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003711static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003712 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003713 .rpc_call_done = nfs4_close_done,
3714 .rpc_release = nfs4_free_closedata,
3715};
3716
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717/*
3718 * It is possible for data to be read/written from a mem-mapped file
3719 * after the sys_close call (which hits the vfs layer as a flush).
3720 * This means that we can't safely call nfsv4 close on a file until
3721 * the inode is cleared. This in turn means that we are not good
3722 * NFSv4 citizens - we do not indicate to the server to update the file's
3723 * share state even when we are done with one of the three share
3724 * stateid's in the inode.
3725 *
3726 * NOTE: Caller must be holding the sp->so_owner semaphore!
3727 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04003728int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003730 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05003731 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04003733 struct nfs4_state_owner *sp = state->owner;
3734 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003735 struct rpc_message msg = {
3736 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3737 .rpc_cred = state->owner->so_cred,
3738 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003739 struct rpc_task_setup task_setup_data = {
3740 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003741 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003742 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003743 .workqueue = nfsiod_workqueue,
Trond Myklebust61296502020-02-07 19:38:12 -05003744 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003745 };
Trond Myklebust95121352005-10-18 14:20:12 -07003746 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04003748 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3749 &task_setup_data.rpc_client, &msg);
3750
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003751 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07003753 goto out;
Anna Schumakerfba83f32018-05-04 16:22:50 -04003754 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1, 0);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003755 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003757 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebustc82bac62017-11-06 15:28:06 -05003758 if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state))
3759 goto out_free_calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05003761 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3762 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05003763 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07003764 goto out_free_calldata;
Trond Myklebustd8d84982016-12-19 12:14:44 -05003765 nfs_fattr_init(&calldata->fattr);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003766 calldata->arg.fmode = 0;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003767 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003768 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003769 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003770 calldata->res.server = server;
Trond Myklebustcf805162016-11-15 14:56:07 -05003771 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003772 calldata->lr.roc = pnfs_roc(state->inode,
3773 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3774 if (calldata->lr.roc) {
3775 calldata->arg.lr_args = &calldata->lr.arg;
3776 calldata->res.lr_res = &calldata->lr.res;
3777 }
Al Viro643168c2011-06-22 18:20:23 -04003778 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003779
Trond Myklebust1174dd12010-12-21 10:52:24 -05003780 msg.rpc_argp = &calldata->arg;
3781 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003782 task_setup_data.callback_data = calldata;
3783 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003784 if (IS_ERR(task))
3785 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003786 status = 0;
3787 if (wait)
3788 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003789 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003790 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003791out_free_calldata:
3792 kfree(calldata);
3793out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003794 nfs4_put_open_state(state);
3795 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003796 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797}
3798
Trond Myklebust2b484292010-09-17 10:56:51 -04003799static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003800nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3801 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003804 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3805
3806 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807
Trond Myklebust565277f2007-10-15 18:17:53 -04003808 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003809 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003810
3811 nfs4_label_release_security(label);
3812
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003813 if (IS_ERR(state))
3814 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003815 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816}
3817
Trond Myklebust1185a552009-12-03 15:54:02 -05003818static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003819{
3820 if (ctx->state == NULL)
3821 return;
3822 if (is_sync)
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003823 nfs4_close_sync(ctx->state, _nfs4_ctx_to_openmode(ctx));
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003824 else
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003825 nfs4_close_state(ctx->state, _nfs4_ctx_to_openmode(ctx));
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003826}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827
Trond Myklebustb944dba2013-11-04 15:20:20 -05003828#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3829#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
Frank van der Lindenb78ef842020-06-23 22:38:55 +00003830#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_XATTR_SUPPORT - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003831
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3833{
Kinglong Mee8c612822015-08-26 21:12:58 +08003834 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003835 struct nfs4_server_caps_arg args = {
3836 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003837 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003838 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839 struct nfs4_server_caps_res res = {};
3840 struct rpc_message msg = {
3841 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003842 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843 .rpc_resp = &res,
3844 };
3845 int status;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003846 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847
Kinglong Mee8c612822015-08-26 21:12:58 +08003848 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3849 FATTR4_WORD0_FH_EXPIRE_TYPE |
3850 FATTR4_WORD0_LINK_SUPPORT |
3851 FATTR4_WORD0_SYMLINK_SUPPORT |
3852 FATTR4_WORD0_ACLSUPPORT;
3853 if (minorversion)
3854 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3855
Bryan Schumaker7c513052011-03-24 17:12:24 +00003856 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003858 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003859 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003860 case 0:
3861 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3862 res.attr_bitmask[2] = 0;
3863 break;
3864 case 1:
3865 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3866 break;
3867 case 2:
3868 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3869 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04003871 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3872 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3873 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3874 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003875 NFS_CAP_CTIME|NFS_CAP_MTIME|
3876 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003877 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3878 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879 server->caps |= NFS_CAP_ACLS;
3880 if (res.has_links != 0)
3881 server->caps |= NFS_CAP_HARDLINKS;
3882 if (res.has_symlinks != 0)
3883 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003884 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3885 server->caps |= NFS_CAP_FILEID;
3886 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3887 server->caps |= NFS_CAP_MODE;
3888 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3889 server->caps |= NFS_CAP_NLINK;
3890 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3891 server->caps |= NFS_CAP_OWNER;
3892 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3893 server->caps |= NFS_CAP_OWNER_GROUP;
3894 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3895 server->caps |= NFS_CAP_ATIME;
3896 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3897 server->caps |= NFS_CAP_CTIME;
3898 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3899 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003900#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3901 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3902 server->caps |= NFS_CAP_SECURITY_LABEL;
3903#endif
3904 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3905 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003906 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003907
Trond Myklebusta65318b2009-03-11 14:10:28 -04003908 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3909 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3910 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003911 server->cache_consistency_bitmask[2] = 0;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003912
3913 /* Avoid a regression due to buggy server */
3914 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3915 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
Kinglong Mee8c612822015-08-26 21:12:58 +08003916 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3917 sizeof(server->exclcreat_bitmask));
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003918
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003920 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003921 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003922
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923 return status;
3924}
3925
Trond Myklebust55a97592006-06-09 09:34:19 -04003926int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927{
Trond Myklebust0688e642019-04-07 13:59:09 -04003928 struct nfs4_exception exception = {
3929 .interruptible = true,
3930 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931 int err;
3932 do {
3933 err = nfs4_handle_exception(server,
3934 _nfs4_server_capabilities(server, fhandle),
3935 &exception);
3936 } while (exception.retry);
3937 return err;
3938}
3939
3940static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3941 struct nfs_fsinfo *info)
3942{
David Quigleyaa9c2662013-05-22 12:50:44 -04003943 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003945 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946 };
3947 struct nfs4_lookup_res res = {
3948 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003949 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950 .fh = fhandle,
3951 };
3952 struct rpc_message msg = {
3953 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3954 .rpc_argp = &args,
3955 .rpc_resp = &res,
3956 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003957
David Quigleyaa9c2662013-05-22 12:50:44 -04003958 bitmask[0] = nfs4_fattr_bitmap[0];
3959 bitmask[1] = nfs4_fattr_bitmap[1];
3960 /*
3961 * Process the label in the upcoming getfattr
3962 */
3963 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3964
Trond Myklebust0e574af2005-10-27 22:12:38 -04003965 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003966 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967}
3968
3969static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3970 struct nfs_fsinfo *info)
3971{
Trond Myklebust0688e642019-04-07 13:59:09 -04003972 struct nfs4_exception exception = {
3973 .interruptible = true,
3974 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975 int err;
3976 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003977 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003978 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003979 switch (err) {
3980 case 0:
3981 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003982 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003983 default:
3984 err = nfs4_handle_exception(server, err, &exception);
3985 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003987out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988 return err;
3989}
3990
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003991static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3992 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3993{
Trond Myklebustc2190662013-08-26 19:23:04 -04003994 struct rpc_auth_create_args auth_args = {
3995 .pseudoflavor = flavor,
3996 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003997 struct rpc_auth *auth;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003998
Trond Myklebustc2190662013-08-26 19:23:04 -04003999 auth = rpcauth_create(&auth_args, server->client);
Anna Schumaker9df13362017-01-11 16:30:08 -05004000 if (IS_ERR(auth))
4001 return -EACCES;
4002 return nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00004003}
4004
Chuck Lever9a744ba2013-03-16 15:56:02 -04004005/*
4006 * Retry pseudoroot lookup with various security flavors. We do this when:
4007 *
4008 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
4009 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
4010 *
4011 * Returns zero on success, or a negative NFS4ERR value, or a
4012 * negative errno value.
4013 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04004014static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04004015 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016{
Chuck Lever9a744ba2013-03-16 15:56:02 -04004017 /* Per 3530bis 15.33.5 */
4018 static const rpc_authflavor_t flav_array[] = {
4019 RPC_AUTH_GSS_KRB5P,
4020 RPC_AUTH_GSS_KRB5I,
4021 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04004022 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04004023 RPC_AUTH_NULL,
4024 };
4025 int status = -EPERM;
4026 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04004028 if (server->auth_info.flavor_len > 0) {
4029 /* try each flavor specified by user */
4030 for (i = 0; i < server->auth_info.flavor_len; i++) {
4031 status = nfs4_lookup_root_sec(server, fhandle, info,
4032 server->auth_info.flavors[i]);
4033 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
4034 continue;
4035 break;
4036 }
4037 } else {
4038 /* no flavors specified by user, try default list */
4039 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
4040 status = nfs4_lookup_root_sec(server, fhandle, info,
4041 flav_array[i]);
4042 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
4043 continue;
4044 break;
4045 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00004046 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04004047
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04004048 /*
Colin Ian Kingd3787af2018-10-26 19:10:31 +01004049 * -EACCES could mean that the user doesn't have correct permissions
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04004050 * to access the mount. It could also mean that we tried to mount
4051 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
4052 * existing mount programs don't handle -EACCES very well so it should
4053 * be mapped to -EPERM instead.
4054 */
4055 if (status == -EACCES)
4056 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04004057 return status;
4058}
4059
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04004060/**
4061 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
4062 * @server: initialized nfs_server handle
4063 * @fhandle: we fill in the pseudo-fs root file handle
4064 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04004065 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04004066 *
4067 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04004068 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04004069int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04004070 struct nfs_fsinfo *info,
4071 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04004072{
Andre Przywarac7757072015-04-23 17:17:40 +01004073 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04004074
Andre Przywarac7757072015-04-23 17:17:40 +01004075 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04004076 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01004077
4078 if (auth_probe || status == NFS4ERR_WRONGSEC)
Trond Myklebust698c9372016-07-25 13:31:14 -04004079 status = server->nfs_client->cl_mvops->find_root_sec(server,
4080 fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04004081
Linus Torvalds1da177e2005-04-16 15:20:36 -07004082 if (status == 0)
4083 status = nfs4_server_capabilities(server, fhandle);
4084 if (status == 0)
4085 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04004086
Trond Myklebustc12e87f2006-03-13 21:20:47 -08004087 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088}
4089
Bryan Schumakerbae36242012-05-10 15:07:31 -04004090static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
4091 struct nfs_fsinfo *info)
4092{
4093 int error;
4094 struct nfs_fattr *fattr = info->fattr;
Scott Mayhew779df6a2020-03-03 17:58:37 -05004095 struct nfs4_label *label = fattr->label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04004096
4097 error = nfs4_server_capabilities(server, mntfh);
4098 if (error < 0) {
4099 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
4100 return error;
4101 }
4102
Trond Myklebusta841b542018-04-07 13:50:59 -04004103 error = nfs4_proc_getattr(server, mntfh, fattr, label, NULL);
Bryan Schumakerbae36242012-05-10 15:07:31 -04004104 if (error < 0) {
4105 dprintk("nfs4_get_root: getattr error = %d\n", -error);
Scott Mayhew779df6a2020-03-03 17:58:37 -05004106 goto out;
Bryan Schumakerbae36242012-05-10 15:07:31 -04004107 }
4108
4109 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
4110 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
4111 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
4112
Scott Mayhew779df6a2020-03-03 17:58:37 -05004113out:
Bryan Schumakerbae36242012-05-10 15:07:31 -04004114 return error;
4115}
4116
Manoj Naik6b97fd32006-06-09 09:34:29 -04004117/*
4118 * Get locations and (maybe) other attributes of a referral.
4119 * Note that we'll actually follow the referral later when
4120 * we detect fsid mismatch in inode revalidation
4121 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004122static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
4123 const struct qstr *name, struct nfs_fattr *fattr,
4124 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04004125{
4126 int status = -ENOMEM;
4127 struct page *page = NULL;
4128 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04004129
4130 page = alloc_page(GFP_KERNEL);
4131 if (page == NULL)
4132 goto out;
4133 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
4134 if (locations == NULL)
4135 goto out;
4136
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004137 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04004138 if (status != 0)
4139 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04004140
4141 /*
4142 * If the fsid didn't change, this is a migration event, not a
4143 * referral. Cause us to drop into the exception handler, which
4144 * will kick off migration recovery.
4145 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04004146 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04004147 dprintk("%s: server did not return a different fsid for"
4148 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04004149 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04004150 goto out;
4151 }
Andy Adamson533eb462011-06-13 18:25:56 -04004152 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
4153 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04004154
Andy Adamson533eb462011-06-13 18:25:56 -04004155 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04004156 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04004157 memset(fhandle, 0, sizeof(struct nfs_fh));
4158out:
4159 if (page)
4160 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04004161 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04004162 return status;
4163}
4164
David Quigley1775fd32013-05-22 12:50:42 -04004165static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebusta841b542018-04-07 13:50:59 -04004166 struct nfs_fattr *fattr, struct nfs4_label *label,
4167 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168{
Trond Myklebust771734f2018-04-07 13:54:23 -04004169 __u32 bitmask[NFS4_BITMASK_SZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170 struct nfs4_getattr_arg args = {
4171 .fh = fhandle,
Trond Myklebust771734f2018-04-07 13:54:23 -04004172 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173 };
4174 struct nfs4_getattr_res res = {
4175 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04004176 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177 .server = server,
4178 };
4179 struct rpc_message msg = {
4180 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
4181 .rpc_argp = &args,
4182 .rpc_resp = &res,
4183 };
Trond Myklebustc74dfe92020-01-06 15:39:37 -05004184 unsigned short task_flags = 0;
4185
4186 /* Is this is an attribute revalidation, subject to softreval? */
4187 if (inode && (server->flags & NFS_MOUNT_SOFTREVAL))
4188 task_flags |= RPC_TASK_TIMEOUT;
David Quigleyaa9c2662013-05-22 12:50:44 -04004189
Trond Myklebusta71029b2021-04-10 00:23:03 -04004190 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, label), inode, 0);
Trond Myklebust0e574af2005-10-27 22:12:38 -04004191 nfs_fattr_init(fattr);
Trond Myklebustc74dfe92020-01-06 15:39:37 -05004192 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
4193 return nfs4_do_call_sync(server->client, server, &msg,
4194 &args.seq_args, &res.seq_res, task_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195}
4196
Olga Kornievskaiaec4b0922019-10-08 16:33:53 -04004197int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebusta841b542018-04-07 13:50:59 -04004198 struct nfs_fattr *fattr, struct nfs4_label *label,
4199 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200{
Trond Myklebust0688e642019-04-07 13:59:09 -04004201 struct nfs4_exception exception = {
4202 .interruptible = true,
4203 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204 int err;
4205 do {
Trond Myklebusta841b542018-04-07 13:50:59 -04004206 err = _nfs4_proc_getattr(server, fhandle, fattr, label, inode);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004207 trace_nfs4_getattr(server, fhandle, fattr, err);
4208 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209 &exception);
4210 } while (exception.retry);
4211 return err;
4212}
4213
4214/*
4215 * The file is not closed if it is opened due to the a request to change
4216 * the size of the file. The open call will not be needed once the
4217 * VFS layer lookup-intents are implemented.
4218 *
4219 * Close is called when the inode is destroyed.
4220 * If we haven't opened the file for O_WRONLY, we
4221 * need to in the size_change case to obtain a stateid.
4222 *
4223 * Got race?
4224 * Because OPEN is always done by name in nfsv4, it is
4225 * possible that we opened a different file by the same
4226 * name. We can recognize this race condition, but we
4227 * can't do anything about it besides returning an error.
4228 *
4229 * This will be fixed with VFS changes (lookup-intent).
4230 */
4231static int
4232nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
4233 struct iattr *sattr)
4234{
David Howells2b0143b2015-03-17 22:25:59 +00004235 struct inode *inode = d_inode(dentry);
NeilBrowna52458b2018-12-03 11:30:31 +11004236 const struct cred *cred = NULL;
NeilBrown29b59f92016-10-13 15:26:47 +11004237 struct nfs_open_context *ctx = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04004238 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239 int status;
4240
Peng Tao88ac8152014-09-12 11:04:10 +08004241 if (pnfs_ld_layoutret_on_setattr(inode) &&
4242 sattr->ia_valid & ATTR_SIZE &&
4243 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04004244 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04004245
Trond Myklebust0e574af2005-10-27 22:12:38 -04004246 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247
Andy Adamson26699402012-05-30 16:12:24 -04004248 /* Deal with open(O_TRUNC) */
4249 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03004250 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04004251
4252 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03004253 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04004254 return 0;
4255
Trond Myklebustd5308382005-11-04 15:33:38 -05004256 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04004257 if (sattr->ia_valid & ATTR_FILE) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00004258
Trond Myklebust659bfcd2008-06-10 19:39:41 -04004259 ctx = nfs_file_open_context(sattr->ia_file);
NeilBrown29b59f92016-10-13 15:26:47 +11004260 if (ctx)
Neil Brown504e5182008-10-16 14:15:16 +11004261 cred = ctx->cred;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04004262 }
4263
David Quigley14c43f72013-05-22 12:50:43 -04004264 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4265 if (IS_ERR(label))
4266 return PTR_ERR(label);
4267
Trond Myklebust199366f2018-03-20 16:43:18 -04004268 /* Return any delegations if we're going to change ACLs */
4269 if ((sattr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
Trond Myklebustc01d3642018-03-20 16:43:20 -04004270 nfs4_inode_make_writeable(inode);
Trond Myklebust199366f2018-03-20 16:43:18 -04004271
NeilBrown29b59f92016-10-13 15:26:47 +11004272 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04004273 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05004274 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04004275 nfs_setsecurity(inode, fattr, label);
4276 }
David Quigley14c43f72013-05-22 12:50:43 -04004277 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278 return status;
4279}
4280
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004281static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004282 struct dentry *dentry, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04004283 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04004284{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004285 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04004286 int status;
4287 struct nfs4_lookup_arg args = {
4288 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004289 .dir_fh = NFS_FH(dir),
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004290 .name = &dentry->d_name,
David Howells2b3de442006-08-22 20:06:09 -04004291 };
4292 struct nfs4_lookup_res res = {
4293 .server = server,
4294 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04004295 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04004296 .fh = fhandle,
4297 };
4298 struct rpc_message msg = {
4299 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
4300 .rpc_argp = &args,
4301 .rpc_resp = &res,
4302 };
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004303 unsigned short task_flags = 0;
4304
4305 /* Is this is an attribute revalidation, subject to softreval? */
4306 if (nfs_lookup_is_soft_revalidate(dentry))
4307 task_flags |= RPC_TASK_TIMEOUT;
David Howells2b3de442006-08-22 20:06:09 -04004308
David Quigleyaa9c2662013-05-22 12:50:44 -04004309 args.bitmask = nfs4_bitmask(server, label);
4310
David Howells2b3de442006-08-22 20:06:09 -04004311 nfs_fattr_init(fattr);
4312
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004313 dprintk("NFS call lookup %pd2\n", dentry);
4314 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
4315 status = nfs4_do_call_sync(clnt, server, &msg,
4316 &args.seq_args, &res.seq_res, task_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317 dprintk("NFS reply lookup: %d\n", status);
4318 return status;
4319}
4320
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004321static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00004322{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00004323 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004324 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00004325 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4326 fattr->nlink = 2;
4327}
4328
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004329static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004330 struct dentry *dentry, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04004331 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004332{
Trond Myklebust0688e642019-04-07 13:59:09 -04004333 struct nfs4_exception exception = {
4334 .interruptible = true,
4335 };
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004336 struct rpc_clnt *client = *clnt;
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004337 const struct qstr *name = &dentry->d_name;
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004338 int err;
4339 do {
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004340 err = _nfs4_proc_lookup(client, dir, dentry, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004341 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004342 switch (err) {
4343 case -NFS4ERR_BADNAME:
4344 err = -ENOENT;
4345 goto out;
4346 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004347 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02004348 if (err == -NFS4ERR_MOVED)
4349 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004350 goto out;
4351 case -NFS4ERR_WRONGSEC:
4352 err = -EPERM;
4353 if (client != *clnt)
4354 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04004355 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004356 if (IS_ERR(client))
4357 return PTR_ERR(client);
4358
4359 exception.retry = 1;
4360 break;
4361 default:
4362 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4363 }
4364 } while (exception.retry);
4365
4366out:
4367 if (err == 0)
4368 *clnt = client;
4369 else if (client != *clnt)
4370 rpc_shutdown_client(client);
4371
4372 return err;
4373}
4374
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004375static int nfs4_proc_lookup(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004376 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4377 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004379 int status;
4380 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004381
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004382 status = nfs4_proc_lookup_common(&client, dir, dentry, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004383 if (client != NFS_CLIENT(dir)) {
4384 rpc_shutdown_client(client);
4385 nfs_fixup_secinfo_attributes(fattr);
4386 }
4387 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388}
4389
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004390struct rpc_clnt *
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004391nfs4_proc_lookup_mountpoint(struct inode *dir, struct dentry *dentry,
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004392 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4393{
Trond Myklebustb72888c2013-08-07 20:38:07 -04004394 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004395 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004396
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004397 status = nfs4_proc_lookup_common(&client, dir, dentry, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04004398 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004399 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04004400 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004401}
4402
Jeff Layton5b5faaf2017-06-29 06:34:52 -07004403static int _nfs4_proc_lookupp(struct inode *inode,
4404 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4405 struct nfs4_label *label)
4406{
4407 struct rpc_clnt *clnt = NFS_CLIENT(inode);
4408 struct nfs_server *server = NFS_SERVER(inode);
4409 int status;
4410 struct nfs4_lookupp_arg args = {
4411 .bitmask = server->attr_bitmask,
4412 .fh = NFS_FH(inode),
4413 };
4414 struct nfs4_lookupp_res res = {
4415 .server = server,
4416 .fattr = fattr,
4417 .label = label,
4418 .fh = fhandle,
4419 };
4420 struct rpc_message msg = {
4421 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
4422 .rpc_argp = &args,
4423 .rpc_resp = &res,
4424 };
Trond Myklebust76998eb2020-10-20 14:30:35 -04004425 unsigned short task_flags = 0;
4426
4427 if (NFS_SERVER(inode)->flags & NFS_MOUNT_SOFTREVAL)
4428 task_flags |= RPC_TASK_TIMEOUT;
Jeff Layton5b5faaf2017-06-29 06:34:52 -07004429
4430 args.bitmask = nfs4_bitmask(server, label);
4431
4432 nfs_fattr_init(fattr);
4433
4434 dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino);
4435 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
Trond Myklebust76998eb2020-10-20 14:30:35 -04004436 &res.seq_res, task_flags);
Jeff Layton5b5faaf2017-06-29 06:34:52 -07004437 dprintk("NFS reply lookupp: %d\n", status);
4438 return status;
4439}
4440
4441static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
4442 struct nfs_fattr *fattr, struct nfs4_label *label)
4443{
Trond Myklebust0688e642019-04-07 13:59:09 -04004444 struct nfs4_exception exception = {
4445 .interruptible = true,
4446 };
Jeff Layton5b5faaf2017-06-29 06:34:52 -07004447 int err;
4448 do {
4449 err = _nfs4_proc_lookupp(inode, fhandle, fattr, label);
4450 trace_nfs4_lookupp(inode, err);
4451 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4452 &exception);
4453 } while (exception.retry);
4454 return err;
4455}
4456
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4458{
Trond Myklebust76b32992007-08-10 17:45:11 -04004459 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460 struct nfs4_accessargs args = {
4461 .fh = NFS_FH(inode),
Anna Schumaker1750d922017-07-26 12:00:21 -04004462 .access = entry->mask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463 };
Trond Myklebust76b32992007-08-10 17:45:11 -04004464 struct nfs4_accessres res = {
4465 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04004466 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004467 struct rpc_message msg = {
4468 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
4469 .rpc_argp = &args,
4470 .rpc_resp = &res,
4471 .rpc_cred = entry->cred,
4472 };
David Quigleyaa9c2662013-05-22 12:50:44 -04004473 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474
Trond Myklebust7c672652018-06-04 15:00:53 -04004475 if (!nfs4_have_delegation(inode, FMODE_READ)) {
Trond Myklebust8bcbe7d2018-03-20 17:03:11 -04004476 res.fattr = nfs_alloc_fattr();
4477 if (res.fattr == NULL)
4478 return -ENOMEM;
4479 args.bitmask = server->cache_consistency_bitmask;
4480 }
Bryan Schumaker7c513052011-03-24 17:12:24 +00004481 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04004483 nfs_access_set_mask(entry, res.access);
Trond Myklebust8bcbe7d2018-03-20 17:03:11 -04004484 if (res.fattr)
4485 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486 }
Trond Myklebustc407d412010-04-16 16:22:48 -04004487 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488 return status;
4489}
4490
4491static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4492{
Trond Myklebust0688e642019-04-07 13:59:09 -04004493 struct nfs4_exception exception = {
4494 .interruptible = true,
4495 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496 int err;
4497 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004498 err = _nfs4_proc_access(inode, entry);
4499 trace_nfs4_access(inode, err);
4500 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501 &exception);
4502 } while (exception.retry);
4503 return err;
4504}
4505
4506/*
4507 * TODO: For the time being, we don't try to get any attributes
4508 * along with any of the zero-copy operations READ, READDIR,
4509 * READLINK, WRITE.
4510 *
4511 * In the case of the first three, we want to put the GETATTR
4512 * after the read-type operation -- this is because it is hard
4513 * to predict the length of a GETATTR response in v4, and thus
4514 * align the READ data correctly. This means that the GETATTR
4515 * may end up partially falling into the page cache, and we should
4516 * shift it into the 'tail' of the xdr_buf before processing.
4517 * To do this efficiently, we need to know the total length
4518 * of data received, which doesn't seem to be available outside
4519 * of the RPC layer.
4520 *
4521 * In the case of WRITE, we also want to put the GETATTR after
4522 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04004523 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524 *
4525 * Both of these changes to the XDR layer would in fact be quite
4526 * minor, but I decided to leave them for a subsequent patch.
4527 */
4528static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4529 unsigned int pgbase, unsigned int pglen)
4530{
4531 struct nfs4_readlink args = {
4532 .fh = NFS_FH(inode),
4533 .pgbase = pgbase,
4534 .pglen = pglen,
4535 .pages = &page,
4536 };
Benny Halevyf50c7002009-04-01 09:21:55 -04004537 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538 struct rpc_message msg = {
4539 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4540 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04004541 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542 };
4543
Bryan Schumaker7c513052011-03-24 17:12:24 +00004544 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 -07004545}
4546
4547static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4548 unsigned int pgbase, unsigned int pglen)
4549{
Trond Myklebust0688e642019-04-07 13:59:09 -04004550 struct nfs4_exception exception = {
4551 .interruptible = true,
4552 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553 int err;
4554 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004555 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4556 trace_nfs4_readlink(inode, err);
4557 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558 &exception);
4559 } while (exception.retry);
4560 return err;
4561}
4562
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004564 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566static int
4567nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004568 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004570 struct nfs_server *server = NFS_SERVER(dir);
David Quigleyaa9c2662013-05-22 12:50:44 -04004571 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004572 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004573 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574 int status = 0;
4575
NeilBrown532d4de2016-10-13 15:26:47 +11004576 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004577 if (IS_ERR(ctx))
4578 return PTR_ERR(ctx);
4579
David Quigleyaa9c2662013-05-22 12:50:44 -04004580 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4581
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004582 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4583 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08004584 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585 if (IS_ERR(state)) {
4586 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04004587 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004588 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589out:
David Quigleyaa9c2662013-05-22 12:50:44 -04004590 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004591 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592 return status;
4593}
4594
Trond Myklebust3c591172018-07-31 15:54:10 -04004595static int
4596_nfs4_proc_remove(struct inode *dir, const struct qstr *name, u32 ftype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597{
Trond Myklebust16e42952005-10-27 22:12:44 -04004598 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004599 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004600 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07004601 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004602 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004603 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04004604 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04004605 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004607 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4608 .rpc_argp = &args,
4609 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610 };
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004611 unsigned long timestamp = jiffies;
Trond Myklebust778d2812012-04-27 13:48:19 -04004612 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04004613
Bryan Schumaker7c513052011-03-24 17:12:24 +00004614 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust3c591172018-07-31 15:54:10 -04004615 if (status == 0) {
4616 spin_lock(&dir->i_lock);
Trond Myklebust3c591172018-07-31 15:54:10 -04004617 /* Removing a directory decrements nlink in the parent */
4618 if (ftype == NF4DIR && dir->i_nlink > 2)
4619 nfs4_dec_nlink_locked(dir);
Trond Myklebust82eae5a2021-04-01 14:59:59 -04004620 nfs4_update_changeattr_locked(dir, &res.cinfo, timestamp,
4621 NFS_INO_INVALID_DATA);
Trond Myklebust3c591172018-07-31 15:54:10 -04004622 spin_unlock(&dir->i_lock);
4623 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624 return status;
4625}
4626
Trond Myklebust912678d2018-03-20 16:43:15 -04004627static int nfs4_proc_remove(struct inode *dir, struct dentry *dentry)
4628{
Trond Myklebust0688e642019-04-07 13:59:09 -04004629 struct nfs4_exception exception = {
4630 .interruptible = true,
4631 };
Trond Myklebust912678d2018-03-20 16:43:15 -04004632 struct inode *inode = d_inode(dentry);
4633 int err;
4634
Trond Myklebustc01d3642018-03-20 16:43:20 -04004635 if (inode) {
4636 if (inode->i_nlink == 1)
4637 nfs4_inode_return_delegation(inode);
4638 else
4639 nfs4_inode_make_writeable(inode);
4640 }
Trond Myklebust912678d2018-03-20 16:43:15 -04004641 do {
Trond Myklebust3c591172018-07-31 15:54:10 -04004642 err = _nfs4_proc_remove(dir, &dentry->d_name, NF4REG);
Trond Myklebust912678d2018-03-20 16:43:15 -04004643 trace_nfs4_remove(dir, &dentry->d_name, err);
4644 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4645 &exception);
4646 } while (exception.retry);
4647 return err;
4648}
4649
4650static int nfs4_proc_rmdir(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651{
Trond Myklebust0688e642019-04-07 13:59:09 -04004652 struct nfs4_exception exception = {
4653 .interruptible = true,
4654 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655 int err;
Trond Myklebust912678d2018-03-20 16:43:15 -04004656
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657 do {
Trond Myklebust3c591172018-07-31 15:54:10 -04004658 err = _nfs4_proc_remove(dir, name, NF4DIR);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004659 trace_nfs4_remove(dir, name, err);
4660 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661 &exception);
4662 } while (exception.retry);
4663 return err;
4664}
4665
Trond Myklebusted7e9ad2018-05-30 16:11:52 -04004666static void nfs4_proc_unlink_setup(struct rpc_message *msg,
4667 struct dentry *dentry,
4668 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004670 struct nfs_removeargs *args = msg->rpc_argp;
4671 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672
Trond Myklebust977fcc22018-03-20 16:43:17 -04004673 res->server = NFS_SB(dentry->d_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Anna Schumakerfba83f32018-05-04 16:22:50 -04004675 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04004676
4677 nfs_fattr_init(res->dir_attr);
Trond Myklebust977fcc22018-03-20 16:43:17 -04004678
4679 if (inode)
4680 nfs4_inode_return_delegation(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681}
4682
Bryan Schumaker34e137c2012-03-19 14:54:41 -04004683static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4684{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004685 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004686 &data->args.seq_args,
4687 &data->res.seq_res,
4688 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689}
4690
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004691static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004692{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004693 struct nfs_unlinkdata *data = task->tk_calldata;
4694 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004695
Trond Myklebust14516c32010-07-31 14:29:06 -04004696 if (!nfs4_sequence_done(task, &res->seq_res))
4697 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004698 if (nfs4_async_handle_error(task, res->server, NULL,
4699 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004700 return 0;
Trond Myklebustc40d52f2017-01-11 12:36:11 -05004701 if (task->tk_status == 0)
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004702 nfs4_update_changeattr(dir, &res->cinfo,
4703 res->dir_attr->time_start,
4704 NFS_INO_INVALID_DATA);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004705 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706}
4707
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004708static void nfs4_proc_rename_setup(struct rpc_message *msg,
4709 struct dentry *old_dentry,
4710 struct dentry *new_dentry)
Jeff Laytond3d41522010-09-17 17:31:57 -04004711{
Jeff Laytond3d41522010-09-17 17:31:57 -04004712 struct nfs_renameargs *arg = msg->rpc_argp;
4713 struct nfs_renameres *res = msg->rpc_resp;
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004714 struct inode *old_inode = d_inode(old_dentry);
4715 struct inode *new_inode = d_inode(new_dentry);
Jeff Laytond3d41522010-09-17 17:31:57 -04004716
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004717 if (old_inode)
Trond Myklebustc01d3642018-03-20 16:43:20 -04004718 nfs4_inode_make_writeable(old_inode);
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004719 if (new_inode)
4720 nfs4_inode_return_delegation(new_inode);
Jeff Laytond3d41522010-09-17 17:31:57 -04004721 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004722 res->server = NFS_SB(old_dentry->d_sb);
Anna Schumakerfba83f32018-05-04 16:22:50 -04004723 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1, 0);
Jeff Laytond3d41522010-09-17 17:31:57 -04004724}
4725
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04004726static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4727{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004728 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004729 &data->args.seq_args,
4730 &data->res.seq_res,
4731 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04004732}
4733
4734static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4735 struct inode *new_dir)
4736{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04004737 struct nfs_renamedata *data = task->tk_calldata;
4738 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04004739
4740 if (!nfs4_sequence_done(task, &res->seq_res))
4741 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004742 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04004743 return 0;
4744
Trond Myklebustc733c492017-01-11 12:32:26 -05004745 if (task->tk_status == 0) {
Trond Myklebust5636ec42018-07-31 15:54:11 -04004746 if (new_dir != old_dir) {
4747 /* Note: If we moved a directory, nlink will change */
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004748 nfs4_update_changeattr(old_dir, &res->old_cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04004749 res->old_fattr->time_start,
Trond Myklebustfabf2b32021-03-25 13:14:42 -04004750 NFS_INO_INVALID_NLINK |
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004751 NFS_INO_INVALID_DATA);
4752 nfs4_update_changeattr(new_dir, &res->new_cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04004753 res->new_fattr->time_start,
Trond Myklebustfabf2b32021-03-25 13:14:42 -04004754 NFS_INO_INVALID_NLINK |
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004755 NFS_INO_INVALID_DATA);
Trond Myklebust5636ec42018-07-31 15:54:11 -04004756 } else
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004757 nfs4_update_changeattr(old_dir, &res->old_cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04004758 res->old_fattr->time_start,
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004759 NFS_INO_INVALID_DATA);
Trond Myklebustc733c492017-01-11 12:32:26 -05004760 }
Jeff Laytond3d41522010-09-17 17:31:57 -04004761 return 1;
4762}
4763
Al Virobeffb8f2016-07-20 16:34:42 -04004764static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004765{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004766 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebust2f28dc32018-04-08 21:06:40 -04004767 __u32 bitmask[NFS4_BITMASK_SZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004768 struct nfs4_link_arg arg = {
4769 .fh = NFS_FH(inode),
4770 .dir_fh = NFS_FH(dir),
4771 .name = name,
Trond Myklebust2f28dc32018-04-08 21:06:40 -04004772 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004773 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004774 struct nfs4_link_res res = {
4775 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04004776 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004777 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778 struct rpc_message msg = {
4779 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4780 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004781 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782 };
Trond Myklebust136f2622010-04-16 16:22:49 -04004783 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784
Trond Myklebust136f2622010-04-16 16:22:49 -04004785 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04004786 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04004787 goto out;
4788
David Quigley14c43f72013-05-22 12:50:43 -04004789 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4790 if (IS_ERR(res.label)) {
4791 status = PTR_ERR(res.label);
4792 goto out;
4793 }
4794
Trond Myklebustc01d3642018-03-20 16:43:20 -04004795 nfs4_inode_make_writeable(inode);
Trond Myklebusta71029b2021-04-10 00:23:03 -04004796 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, res.label), inode,
4797 NFS_INO_INVALID_CHANGE);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004798 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004799 if (!status) {
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004800 nfs4_update_changeattr(dir, &res.cinfo, res.fattr->time_start,
4801 NFS_INO_INVALID_DATA);
Trond Myklebust1301e422021-04-01 14:57:56 -04004802 nfs4_inc_nlink(inode);
David Quigleyaa9c2662013-05-22 12:50:44 -04004803 status = nfs_post_op_update_inode(inode, res.fattr);
4804 if (!status)
4805 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004806 }
David Quigley14c43f72013-05-22 12:50:43 -04004807
4808
4809 nfs4_label_free(res.label);
4810
Trond Myklebust136f2622010-04-16 16:22:49 -04004811out:
Trond Myklebust136f2622010-04-16 16:22:49 -04004812 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813 return status;
4814}
4815
Al Virobeffb8f2016-07-20 16:34:42 -04004816static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004817{
Trond Myklebust0688e642019-04-07 13:59:09 -04004818 struct nfs4_exception exception = {
4819 .interruptible = true,
4820 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821 int err;
4822 do {
4823 err = nfs4_handle_exception(NFS_SERVER(inode),
4824 _nfs4_proc_link(inode, dir, name),
4825 &exception);
4826 } while (exception.retry);
4827 return err;
4828}
4829
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004830struct nfs4_createdata {
4831 struct rpc_message msg;
4832 struct nfs4_create_arg arg;
4833 struct nfs4_create_res res;
4834 struct nfs_fh fh;
4835 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004836 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004837};
4838
4839static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004840 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004841{
4842 struct nfs4_createdata *data;
4843
4844 data = kzalloc(sizeof(*data), GFP_KERNEL);
4845 if (data != NULL) {
4846 struct nfs_server *server = NFS_SERVER(dir);
4847
David Quigley14c43f72013-05-22 12:50:43 -04004848 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4849 if (IS_ERR(data->label))
4850 goto out_free;
4851
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004852 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4853 data->msg.rpc_argp = &data->arg;
4854 data->msg.rpc_resp = &data->res;
4855 data->arg.dir_fh = NFS_FH(dir);
4856 data->arg.server = server;
4857 data->arg.name = name;
4858 data->arg.attrs = sattr;
4859 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04004860 data->arg.bitmask = nfs4_bitmask(server, data->label);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004861 data->arg.umask = current_umask();
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004862 data->res.server = server;
4863 data->res.fh = &data->fh;
4864 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004865 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004866 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004867 }
4868 return data;
David Quigley14c43f72013-05-22 12:50:43 -04004869out_free:
4870 kfree(data);
4871 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004872}
4873
4874static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4875{
Bryan Schumaker7c513052011-03-24 17:12:24 +00004876 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00004877 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004878 if (status == 0) {
Trond Myklebust3c591172018-07-31 15:54:10 -04004879 spin_lock(&dir->i_lock);
Trond Myklebust3c591172018-07-31 15:54:10 -04004880 /* Creating a directory bumps nlink in the parent */
4881 if (data->arg.ftype == NF4DIR)
4882 nfs4_inc_nlink_locked(dir);
Trond Myklebust82eae5a2021-04-01 14:59:59 -04004883 nfs4_update_changeattr_locked(dir, &data->res.dir_cinfo,
4884 data->res.fattr->time_start,
4885 NFS_INO_INVALID_DATA);
Trond Myklebust3c591172018-07-31 15:54:10 -04004886 spin_unlock(&dir->i_lock);
David Quigley1775fd32013-05-22 12:50:42 -04004887 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004888 }
4889 return status;
4890}
4891
4892static void nfs4_free_createdata(struct nfs4_createdata *data)
4893{
David Quigley14c43f72013-05-22 12:50:43 -04004894 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004895 kfree(data);
4896}
4897
Chuck Lever4f390c12006-08-22 20:06:22 -04004898static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004899 struct page *page, unsigned int len, struct iattr *sattr,
4900 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004901{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004902 struct nfs4_createdata *data;
4903 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004904
Chuck Lever94a6d752006-08-22 20:06:23 -04004905 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004906 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04004907
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004908 status = -ENOMEM;
4909 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4910 if (data == NULL)
4911 goto out;
4912
4913 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4914 data->arg.u.symlink.pages = &page;
4915 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004916 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004917
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004918 status = nfs4_do_create(dir, dentry, data);
4919
4920 nfs4_free_createdata(data);
4921out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004922 return status;
4923}
4924
Chuck Lever4f390c12006-08-22 20:06:22 -04004925static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004926 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004927{
Trond Myklebust0688e642019-04-07 13:59:09 -04004928 struct nfs4_exception exception = {
4929 .interruptible = true,
4930 };
David Quigleyaa9c2662013-05-22 12:50:44 -04004931 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004933
4934 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4935
Linus Torvalds1da177e2005-04-16 15:20:36 -07004936 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004937 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4938 trace_nfs4_symlink(dir, &dentry->d_name, err);
4939 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004940 &exception);
4941 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004942
4943 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944 return err;
4945}
4946
4947static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004948 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004950 struct nfs4_createdata *data;
4951 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004952
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004953 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4954 if (data == NULL)
4955 goto out;
4956
David Quigley1775fd32013-05-22 12:50:42 -04004957 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004958 status = nfs4_do_create(dir, dentry, data);
4959
4960 nfs4_free_createdata(data);
4961out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962 return status;
4963}
4964
4965static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4966 struct iattr *sattr)
4967{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004968 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust0688e642019-04-07 13:59:09 -04004969 struct nfs4_exception exception = {
4970 .interruptible = true,
4971 };
David Quigleyaa9c2662013-05-22 12:50:44 -04004972 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004974
David Quigleyaa9c2662013-05-22 12:50:44 -04004975 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4976
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004977 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4978 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004980 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4981 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4982 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983 &exception);
4984 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004985 nfs4_label_release_security(label);
4986
Linus Torvalds1da177e2005-04-16 15:20:36 -07004987 return err;
4988}
4989
Trond Myklebust82e22a52020-11-02 17:34:23 -05004990static int _nfs4_proc_readdir(struct nfs_readdir_arg *nr_arg,
4991 struct nfs_readdir_res *nr_res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004992{
Trond Myklebust82e22a52020-11-02 17:34:23 -05004993 struct inode *dir = d_inode(nr_arg->dentry);
Olga Kornievskaia05ad9172020-11-06 16:03:38 -05004994 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995 struct nfs4_readdir_arg args = {
4996 .fh = NFS_FH(dir),
Trond Myklebust82e22a52020-11-02 17:34:23 -05004997 .pages = nr_arg->pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004998 .pgbase = 0,
Trond Myklebust82e22a52020-11-02 17:34:23 -05004999 .count = nr_arg->page_len,
5000 .plus = nr_arg->plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005001 };
5002 struct nfs4_readdir_res res;
5003 struct rpc_message msg = {
5004 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
5005 .rpc_argp = &args,
5006 .rpc_resp = &res,
Trond Myklebust82e22a52020-11-02 17:34:23 -05005007 .rpc_cred = nr_arg->cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005008 };
5009 int status;
5010
Trond Myklebust82e22a52020-11-02 17:34:23 -05005011 dprintk("%s: dentry = %pd2, cookie = %llu\n", __func__,
5012 nr_arg->dentry, (unsigned long long)nr_arg->cookie);
Olga Kornievskaia05ad9172020-11-06 16:03:38 -05005013 if (!(server->caps & NFS_CAP_SECURITY_LABEL))
5014 args.bitmask = server->attr_bitmask_nl;
5015 else
5016 args.bitmask = server->attr_bitmask;
5017
Trond Myklebust82e22a52020-11-02 17:34:23 -05005018 nfs4_setup_readdir(nr_arg->cookie, nr_arg->verf, nr_arg->dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005019 res.pgbase = args.pgbase;
Olga Kornievskaia05ad9172020-11-06 16:03:38 -05005020 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
5021 &res.seq_res, 0);
Trond Myklebustac396122010-11-15 20:26:22 -05005022 if (status >= 0) {
Trond Myklebust82e22a52020-11-02 17:34:23 -05005023 memcpy(nr_res->verf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05005024 status += args.pgbase;
5025 }
Trond Myklebustc4812992007-09-28 17:11:45 -04005026
5027 nfs_invalidate_atime(dir);
5028
Harvey Harrison3110ff82008-05-02 13:42:44 -07005029 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005030 return status;
5031}
5032
Trond Myklebust82e22a52020-11-02 17:34:23 -05005033static int nfs4_proc_readdir(struct nfs_readdir_arg *arg,
5034 struct nfs_readdir_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005035{
Trond Myklebust0688e642019-04-07 13:59:09 -04005036 struct nfs4_exception exception = {
5037 .interruptible = true,
5038 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005039 int err;
5040 do {
Trond Myklebust82e22a52020-11-02 17:34:23 -05005041 err = _nfs4_proc_readdir(arg, res);
5042 trace_nfs4_readdir(d_inode(arg->dentry), err);
5043 err = nfs4_handle_exception(NFS_SERVER(d_inode(arg->dentry)),
5044 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005045 } while (exception.retry);
5046 return err;
5047}
5048
5049static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04005050 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005051{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04005052 struct nfs4_createdata *data;
5053 int mode = sattr->ia_mode;
5054 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005055
Trond Myklebust57dc9a52008-06-20 15:35:32 -04005056 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
5057 if (data == NULL)
5058 goto out;
5059
Linus Torvalds1da177e2005-04-16 15:20:36 -07005060 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04005061 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005062 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04005063 data->arg.ftype = NF4BLK;
5064 data->arg.u.device.specdata1 = MAJOR(rdev);
5065 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005066 }
5067 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04005068 data->arg.ftype = NF4CHR;
5069 data->arg.u.device.specdata1 = MAJOR(rdev);
5070 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04005071 } else if (!S_ISSOCK(mode)) {
5072 status = -EINVAL;
5073 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005074 }
David Quigley1775fd32013-05-22 12:50:42 -04005075
David Quigleyaa9c2662013-05-22 12:50:44 -04005076 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04005077 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04005078out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04005079 nfs4_free_createdata(data);
5080out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005081 return status;
5082}
5083
5084static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
5085 struct iattr *sattr, dev_t rdev)
5086{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05005087 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust0688e642019-04-07 13:59:09 -04005088 struct nfs4_exception exception = {
5089 .interruptible = true,
5090 };
David Quigleyaa9c2662013-05-22 12:50:44 -04005091 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005092 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00005093
David Quigleyaa9c2662013-05-22 12:50:44 -04005094 label = nfs4_label_init_security(dir, dentry, sattr, &l);
5095
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05005096 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
5097 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005098 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04005099 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
5100 trace_nfs4_mknod(dir, &dentry->d_name, err);
5101 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005102 &exception);
5103 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04005104
5105 nfs4_label_release_security(label);
5106
Linus Torvalds1da177e2005-04-16 15:20:36 -07005107 return err;
5108}
5109
5110static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
5111 struct nfs_fsstat *fsstat)
5112{
5113 struct nfs4_statfs_arg args = {
5114 .fh = fhandle,
5115 .bitmask = server->attr_bitmask,
5116 };
Benny Halevy24ad1482009-04-01 09:21:56 -04005117 struct nfs4_statfs_res res = {
5118 .fsstat = fsstat,
5119 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005120 struct rpc_message msg = {
5121 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
5122 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04005123 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005124 };
5125
Trond Myklebust0e574af2005-10-27 22:12:38 -04005126 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005127 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005128}
5129
5130static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
5131{
Trond Myklebust0688e642019-04-07 13:59:09 -04005132 struct nfs4_exception exception = {
5133 .interruptible = true,
5134 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005135 int err;
5136 do {
5137 err = nfs4_handle_exception(server,
5138 _nfs4_proc_statfs(server, fhandle, fsstat),
5139 &exception);
5140 } while (exception.retry);
5141 return err;
5142}
5143
5144static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
5145 struct nfs_fsinfo *fsinfo)
5146{
5147 struct nfs4_fsinfo_arg args = {
5148 .fh = fhandle,
5149 .bitmask = server->attr_bitmask,
5150 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04005151 struct nfs4_fsinfo_res res = {
5152 .fsinfo = fsinfo,
5153 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005154 struct rpc_message msg = {
5155 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
5156 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04005157 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005158 };
5159
Bryan Schumaker7c513052011-03-24 17:12:24 +00005160 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005161}
5162
5163static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5164{
Trond Myklebust0688e642019-04-07 13:59:09 -04005165 struct nfs4_exception exception = {
5166 .interruptible = true,
5167 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005168 int err;
5169
5170 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04005171 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04005172 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04005173 if (err == 0) {
Robert Milkowski7dc29932020-01-30 09:43:25 +00005174 nfs4_set_lease_period(server->nfs_client, fsinfo->lease_time * HZ);
Chuck Lever83ca7f52013-03-16 15:55:53 -04005175 break;
5176 }
5177 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005178 } while (exception.retry);
5179 return err;
5180}
5181
5182static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5183{
Bryan Schumakere38eb652012-06-20 15:53:40 -04005184 int error;
5185
Trond Myklebust0e574af2005-10-27 22:12:38 -04005186 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04005187 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08005188 if (error == 0) {
5189 /* block layout checks this! */
5190 server->pnfs_blksize = fsinfo->blksize;
Jeff Laytonca440c32016-09-15 14:40:49 -04005191 set_pnfs_layoutdriver(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08005192 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04005193
5194 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005195}
5196
5197static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5198 struct nfs_pathconf *pathconf)
5199{
5200 struct nfs4_pathconf_arg args = {
5201 .fh = fhandle,
5202 .bitmask = server->attr_bitmask,
5203 };
Benny Halevyd45b2982009-04-01 09:21:58 -04005204 struct nfs4_pathconf_res res = {
5205 .pathconf = pathconf,
5206 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005207 struct rpc_message msg = {
5208 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
5209 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04005210 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005211 };
5212
5213 /* None of the pathconf attributes are mandatory to implement */
5214 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
5215 memset(pathconf, 0, sizeof(*pathconf));
5216 return 0;
5217 }
5218
Trond Myklebust0e574af2005-10-27 22:12:38 -04005219 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005220 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005221}
5222
5223static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5224 struct nfs_pathconf *pathconf)
5225{
Trond Myklebust0688e642019-04-07 13:59:09 -04005226 struct nfs4_exception exception = {
5227 .interruptible = true,
5228 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005229 int err;
5230
5231 do {
5232 err = nfs4_handle_exception(server,
5233 _nfs4_proc_pathconf(server, fhandle, pathconf),
5234 &exception);
5235 } while (exception.retry);
5236 return err;
5237}
5238
Trond Myklebust5521abf2013-03-16 20:54:34 -04005239int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04005240 const struct nfs_open_context *ctx,
5241 const struct nfs_lock_context *l_ctx,
5242 fmode_t fmode)
5243{
NeilBrown17393472016-10-13 15:26:47 +11005244 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04005245}
5246EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
5247
Trond Myklebust5521abf2013-03-16 20:54:34 -04005248static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
5249 const struct nfs_open_context *ctx,
5250 const struct nfs_lock_context *l_ctx,
5251 fmode_t fmode)
5252{
Ben Dooksd49dd112019-10-16 17:28:21 +01005253 nfs4_stateid _current_stateid;
Trond Myklebust5521abf2013-03-16 20:54:34 -04005254
Trond Myklebuste1253be2014-03-05 08:44:23 -05005255 /* If the current stateid represents a lost lock, then exit */
Ben Dooksd49dd112019-10-16 17:28:21 +01005256 if (nfs4_set_rw_stateid(&_current_stateid, ctx, l_ctx, fmode) == -EIO)
Trond Myklebuste1253be2014-03-05 08:44:23 -05005257 return true;
Ben Dooksd49dd112019-10-16 17:28:21 +01005258 return nfs4_stateid_match(stateid, &_current_stateid);
Trond Myklebust5521abf2013-03-16 20:54:34 -04005259}
5260
5261static bool nfs4_error_stateid_expired(int err)
5262{
5263 switch (err) {
5264 case -NFS4ERR_DELEG_REVOKED:
5265 case -NFS4ERR_ADMIN_REVOKED:
5266 case -NFS4ERR_BAD_STATEID:
5267 case -NFS4ERR_STALE_STATEID:
5268 case -NFS4ERR_OLD_STATEID:
5269 case -NFS4ERR_OPENMODE:
5270 case -NFS4ERR_EXPIRED:
5271 return true;
5272 }
5273 return false;
5274}
5275
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005276static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005277{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005278 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005279
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005280 trace_nfs4_read(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04005281 if (task->tk_status < 0) {
5282 struct nfs4_exception exception = {
5283 .inode = hdr->inode,
5284 .state = hdr->args.context->state,
5285 .stateid = &hdr->args.stateid,
5286 };
5287 task->tk_status = nfs4_async_handle_exception(task,
5288 server, task->tk_status, &exception);
5289 if (exception.retry) {
5290 rpc_restart_call_prepare(task);
5291 return -EAGAIN;
5292 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005293 }
Trond Myklebust8850df92007-09-28 17:20:07 -04005294
Linus Torvalds1da177e2005-04-16 15:20:36 -07005295 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005296 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05005297 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005298}
5299
Trond Myklebust5521abf2013-03-16 20:54:34 -04005300static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04005301 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04005302{
5303
5304 if (!nfs4_error_stateid_expired(task->tk_status) ||
5305 nfs4_stateid_is_current(&args->stateid,
5306 args->context,
5307 args->lock_context,
5308 FMODE_READ))
5309 return false;
5310 rpc_restart_call_prepare(task);
5311 return true;
5312}
5313
Anna Schumakerc5675522014-05-28 13:41:22 -04005314static bool nfs4_read_plus_not_supported(struct rpc_task *task,
5315 struct nfs_pgio_header *hdr)
5316{
5317 struct nfs_server *server = NFS_SERVER(hdr->inode);
5318 struct rpc_message *msg = &task->tk_msg;
5319
5320 if (msg->rpc_proc == &nfs4_procedures[NFSPROC4_CLNT_READ_PLUS] &&
5321 server->caps & NFS_CAP_READ_PLUS && task->tk_status == -ENOTSUPP) {
5322 server->caps &= ~NFS_CAP_READ_PLUS;
5323 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
5324 rpc_restart_call_prepare(task);
5325 return true;
5326 }
5327 return false;
5328}
5329
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005330static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc72011-03-01 01:34:20 +00005331{
Andy Adamsoncbdabc72011-03-01 01:34:20 +00005332 dprintk("--> %s\n", __func__);
5333
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005334 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc72011-03-01 01:34:20 +00005335 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005336 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04005337 return -EAGAIN;
Anna Schumakerc5675522014-05-28 13:41:22 -04005338 if (nfs4_read_plus_not_supported(task, hdr))
5339 return -EAGAIN;
Trond Myklebustbfc505d2016-09-15 18:26:05 -04005340 if (task->tk_status > 0)
5341 nfs_invalidate_atime(hdr->inode);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005342 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5343 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00005344}
5345
Anna Schumaker21e31402020-12-03 15:18:39 -05005346#if defined CONFIG_NFS_V4_2 && defined CONFIG_NFS_V4_2_READ_PLUS
Trond Myklebust5c3485b2020-12-10 09:34:34 -05005347static void nfs42_read_plus_support(struct nfs_pgio_header *hdr,
5348 struct rpc_message *msg)
Anna Schumakerc5675522014-05-28 13:41:22 -04005349{
Trond Myklebust5c3485b2020-12-10 09:34:34 -05005350 /* Note: We don't use READ_PLUS with pNFS yet */
5351 if (nfs_server_capable(hdr->inode, NFS_CAP_READ_PLUS) && !hdr->ds_clp)
Anna Schumakerc5675522014-05-28 13:41:22 -04005352 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ_PLUS];
Anna Schumakerc5675522014-05-28 13:41:22 -04005353}
5354#else
Trond Myklebust5c3485b2020-12-10 09:34:34 -05005355static void nfs42_read_plus_support(struct nfs_pgio_header *hdr,
5356 struct rpc_message *msg)
Anna Schumakerc5675522014-05-28 13:41:22 -04005357{
Anna Schumakerc5675522014-05-28 13:41:22 -04005358}
5359#endif /* CONFIG_NFS_V4_2 */
5360
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005361static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
5362 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005363{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005364 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04005365 if (!hdr->pgio_done_cb)
5366 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebust5c3485b2020-12-10 09:34:34 -05005367 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
5368 nfs42_read_plus_support(hdr, msg);
Anna Schumakerfba83f32018-05-04 16:22:50 -04005369 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005370}
5371
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005372static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
5373 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04005374{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005375 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005376 &hdr->args.seq_args,
5377 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04005378 task))
NeilBrownef1820f2013-09-04 17:04:49 +10005379 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005380 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
5381 hdr->args.lock_context,
Benjamin Coddingtonfbe77c32017-04-19 10:11:35 -04005382 hdr->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10005383 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005384 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10005385 return -EIO;
5386 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005387}
5388
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005389static int nfs4_write_done_cb(struct rpc_task *task,
5390 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005391{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005392 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10005393
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005394 trace_nfs4_write(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04005395 if (task->tk_status < 0) {
5396 struct nfs4_exception exception = {
5397 .inode = hdr->inode,
5398 .state = hdr->args.context->state,
5399 .stateid = &hdr->args.stateid,
5400 };
5401 task->tk_status = nfs4_async_handle_exception(task,
5402 NFS_SERVER(inode), task->tk_status,
5403 &exception);
5404 if (exception.retry) {
5405 rpc_restart_call_prepare(task);
5406 return -EAGAIN;
5407 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005408 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04005409 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005410 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05005411 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04005412 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05005413 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005414}
5415
Trond Myklebust5521abf2013-03-16 20:54:34 -04005416static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04005417 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04005418{
5419
5420 if (!nfs4_error_stateid_expired(task->tk_status) ||
5421 nfs4_stateid_is_current(&args->stateid,
5422 args->context,
5423 args->lock_context,
5424 FMODE_WRITE))
5425 return false;
5426 rpc_restart_call_prepare(task);
5427 return true;
5428}
5429
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005430static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00005431{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005432 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00005433 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005434 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04005435 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005436 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5437 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00005438}
5439
Trond Myklebust5a37f852012-04-28 14:55:16 -04005440static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005441bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00005442{
Trond Myklebust5a37f852012-04-28 14:55:16 -04005443 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005444 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04005445 return false;
5446 /* Otherwise, request attributes if and only if we don't hold
5447 * a delegation
5448 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04005449 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00005450}
Fred Isamana69aef12011-03-03 15:13:47 +00005451
Trond Myklebust332d1a02021-03-25 18:15:36 -04005452static void nfs4_bitmask_set(__u32 bitmask[NFS4_BITMASK_SZ], const __u32 *src,
5453 struct inode *inode, struct nfs_server *server,
5454 struct nfs4_label *label)
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005455{
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005456 unsigned long cache_validity = READ_ONCE(NFS_I(inode)->cache_validity);
Trond Myklebust332d1a02021-03-25 18:15:36 -04005457 unsigned int i;
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005458
Trond Myklebust332d1a02021-03-25 18:15:36 -04005459 memcpy(bitmask, src, sizeof(*bitmask) * NFS4_BITMASK_SZ);
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005460
Trond Myklebust13c0b082021-03-25 21:07:21 -04005461 if (cache_validity & NFS_INO_INVALID_CHANGE)
Trond Myklebust332d1a02021-03-25 18:15:36 -04005462 bitmask[0] |= FATTR4_WORD0_CHANGE;
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005463 if (cache_validity & NFS_INO_INVALID_ATIME)
5464 bitmask[1] |= FATTR4_WORD1_TIME_ACCESS;
Trond Myklebust720869e2021-04-13 09:41:16 -04005465 if (cache_validity & NFS_INO_INVALID_MODE)
5466 bitmask[1] |= FATTR4_WORD1_MODE;
Trond Myklebust45901a22021-02-08 08:49:32 -05005467 if (cache_validity & NFS_INO_INVALID_OTHER)
Trond Myklebust720869e2021-04-13 09:41:16 -04005468 bitmask[1] |= FATTR4_WORD1_OWNER | FATTR4_WORD1_OWNER_GROUP;
Trond Myklebustfabf2b32021-03-25 13:14:42 -04005469 if (cache_validity & NFS_INO_INVALID_NLINK)
5470 bitmask[1] |= FATTR4_WORD1_NUMLINKS;
Trond Myklebust45901a22021-02-08 08:49:32 -05005471 if (label && label->len && cache_validity & NFS_INO_INVALID_LABEL)
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005472 bitmask[2] |= FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust45901a22021-02-08 08:49:32 -05005473 if (cache_validity & NFS_INO_INVALID_CTIME)
5474 bitmask[1] |= FATTR4_WORD1_TIME_METADATA;
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005475 if (cache_validity & NFS_INO_INVALID_MTIME)
5476 bitmask[1] |= FATTR4_WORD1_TIME_MODIFY;
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005477 if (cache_validity & NFS_INO_INVALID_BLOCKS)
5478 bitmask[1] |= FATTR4_WORD1_SPACE_USED;
Trond Myklebust332d1a02021-03-25 18:15:36 -04005479
5480 if (nfs4_have_delegation(inode, FMODE_READ) &&
5481 !(cache_validity & NFS_INO_REVAL_FORCED))
5482 bitmask[0] &= ~FATTR4_WORD0_SIZE;
Trond Myklebust13c0b082021-03-25 21:07:21 -04005483 else if (cache_validity & NFS_INO_INVALID_SIZE)
Trond Myklebust332d1a02021-03-25 18:15:36 -04005484 bitmask[0] |= FATTR4_WORD0_SIZE;
5485
5486 for (i = 0; i < NFS4_BITMASK_SZ; i++)
5487 bitmask[i] &= server->attr_bitmask[i];
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005488}
5489
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005490static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
Anna Schumakerfb91fb02018-05-04 16:22:48 -04005491 struct rpc_message *msg,
5492 struct rpc_clnt **clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005493{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005494 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005495
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005496 if (!nfs4_write_need_cache_consistency_data(hdr)) {
5497 hdr->args.bitmask = NULL;
5498 hdr->res.fattr = NULL;
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005499 } else {
Trond Myklebust332d1a02021-03-25 18:15:36 -04005500 nfs4_bitmask_set(hdr->args.bitmask_store,
5501 server->cache_consistency_bitmask,
5502 hdr->inode, server, NULL);
5503 hdr->args.bitmask = hdr->args.bitmask_store;
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005504 }
Trond Myklebust5a37f852012-04-28 14:55:16 -04005505
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005506 if (!hdr->pgio_done_cb)
5507 hdr->pgio_done_cb = nfs4_write_done_cb;
5508 hdr->res.server = server;
5509 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005510
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005511 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Olga Kornievskaiacd1b6592020-02-12 17:32:12 -05005512 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
Anna Schumakerfb91fb02018-05-04 16:22:48 -04005513 nfs4_state_protect_write(server->nfs_client, clnt, msg, hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005514}
5515
Fred Isaman0b7c0152012-04-20 14:47:39 -04005516static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
5517{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005518 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005519 &data->args.seq_args,
5520 &data->res.seq_res,
5521 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005522}
5523
Fred Isaman0b7c0152012-04-20 14:47:39 -04005524static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005525{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005526 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04005527
Trond Myklebustcc668ab2013-08-14 15:31:28 -04005528 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10005529 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
5530 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005531 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05005532 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005533 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05005534 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005535}
5536
Fred Isaman0b7c0152012-04-20 14:47:39 -04005537static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00005538{
5539 if (!nfs4_sequence_done(task, &data->res.seq_res))
5540 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04005541 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00005542}
5543
Anna Schumakere9ae1ee2018-05-04 16:22:49 -04005544static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg,
5545 struct rpc_clnt **clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005546{
Trond Myklebust788e7a82006-03-20 13:44:27 -05005547 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00005548
Fred Isaman0b7c0152012-04-20 14:47:39 -04005549 if (data->commit_done_cb == NULL)
5550 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04005551 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005552 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Anna Schumakerfba83f32018-05-04 16:22:50 -04005553 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Anna Schumakere9ae1ee2018-05-04 16:22:49 -04005554 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_COMMIT, clnt, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005555}
5556
Olga Kornievskaia6b8d84e2018-07-09 15:13:36 -04005557static int _nfs4_proc_commit(struct file *dst, struct nfs_commitargs *args,
5558 struct nfs_commitres *res)
5559{
5560 struct inode *dst_inode = file_inode(dst);
5561 struct nfs_server *server = NFS_SERVER(dst_inode);
5562 struct rpc_message msg = {
5563 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
5564 .rpc_argp = args,
5565 .rpc_resp = res,
5566 };
5567
5568 args->fh = NFS_FH(dst_inode);
5569 return nfs4_call_sync(server->client, server, &msg,
5570 &args->seq_args, &res->seq_res, 1);
5571}
5572
5573int nfs4_proc_commit(struct file *dst, __u64 offset, __u32 count, struct nfs_commitres *res)
5574{
5575 struct nfs_commitargs args = {
5576 .offset = offset,
5577 .count = count,
5578 };
5579 struct nfs_server *dst_server = NFS_SERVER(file_inode(dst));
5580 struct nfs4_exception exception = { };
5581 int status;
5582
5583 do {
5584 status = _nfs4_proc_commit(dst, &args, res);
5585 status = nfs4_handle_exception(dst_server, status, &exception);
5586 } while (exception.retry);
5587
5588 return status;
5589}
5590
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005591struct nfs4_renewdata {
5592 struct nfs_client *client;
5593 unsigned long timestamp;
5594};
5595
Linus Torvalds1da177e2005-04-16 15:20:36 -07005596/*
5597 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
5598 * standalone procedure for queueing an asynchronous RENEW.
5599 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005600static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005601{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005602 struct nfs4_renewdata *data = calldata;
5603 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005604
Elena Reshetova212bf412017-10-20 12:53:38 +03005605 if (refcount_read(&clp->cl_count) > 1)
Alexandros Batsakis0851de062010-02-05 03:45:06 -08005606 nfs4_schedule_state_renewal(clp);
5607 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005608 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005609}
5610
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005611static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005612{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005613 struct nfs4_renewdata *data = calldata;
5614 struct nfs_client *clp = data->client;
5615 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005616
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005617 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04005618 switch (task->tk_status) {
5619 case 0:
5620 break;
5621 case -NFS4ERR_LEASE_MOVED:
5622 nfs4_schedule_lease_moved_recovery(clp);
5623 break;
5624 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04005625 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04005626 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
5627 return;
5628 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005629 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04005630 return;
5631 }
5632 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005633 }
Trond Myklebust452e9352010-07-31 14:29:06 -04005634 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005635}
5636
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005637static const struct rpc_call_ops nfs4_renew_ops = {
5638 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005639 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005640};
5641
NeilBrowna52458b2018-12-03 11:30:31 +11005642static int nfs4_proc_async_renew(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005643{
5644 struct rpc_message msg = {
5645 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5646 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01005647 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005648 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005649 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005650
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005651 if (renew_flags == 0)
5652 return 0;
Elena Reshetova212bf412017-10-20 12:53:38 +03005653 if (!refcount_inc_not_zero(&clp->cl_count))
Alexandros Batsakis0851de062010-02-05 03:45:06 -08005654 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04005655 data = kmalloc(sizeof(*data), GFP_NOFS);
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04005656 if (data == NULL) {
5657 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005658 return -ENOMEM;
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04005659 }
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005660 data->client = clp;
5661 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005662 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005663 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005664}
5665
NeilBrowna52458b2018-12-03 11:30:31 +11005666static int nfs4_proc_renew(struct nfs_client *clp, const struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005667{
5668 struct rpc_message msg = {
5669 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5670 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01005671 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005672 };
5673 unsigned long now = jiffies;
5674 int status;
5675
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005676 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005677 if (status < 0)
5678 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04005679 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005680 return 0;
5681}
5682
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005683static inline int nfs4_server_supports_acls(struct nfs_server *server)
5684{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06005685 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005686}
5687
Trond Myklebust21f498c2012-08-24 10:59:25 -04005688/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
5689 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005690 * the stack.
5691 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04005692#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005693
Frank van der Lindenccde1e92020-06-23 22:39:01 +00005694int nfs4_buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005695 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05005696{
5697 struct page *newpage, **spages;
5698 int rc = 0;
5699 size_t len;
5700 spages = pages;
5701
5702 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04005703 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05005704 newpage = alloc_page(GFP_KERNEL);
5705
5706 if (newpage == NULL)
5707 goto unwind;
5708 memcpy(page_address(newpage), buf, len);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005709 buf += len;
5710 buflen -= len;
Neil Hormane9e3d722011-03-04 19:26:03 -05005711 *pages++ = newpage;
5712 rc++;
5713 } while (buflen != 0);
5714
5715 return rc;
5716
5717unwind:
5718 for(; rc > 0; rc--)
5719 __free_page(spages[rc-1]);
5720 return -ENOMEM;
5721}
5722
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005723struct nfs4_cached_acl {
5724 int cached;
5725 size_t len;
Gustavo A. R. Silva5601cda2020-03-09 13:24:42 -05005726 char data[];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005727};
5728
5729static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005730{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005731 struct nfs_inode *nfsi = NFS_I(inode);
5732
5733 spin_lock(&inode->i_lock);
5734 kfree(nfsi->nfs4_acl);
5735 nfsi->nfs4_acl = acl;
5736 spin_unlock(&inode->i_lock);
5737}
5738
5739static void nfs4_zap_acl_attr(struct inode *inode)
5740{
5741 nfs4_set_cached_acl(inode, NULL);
5742}
5743
5744static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
5745{
5746 struct nfs_inode *nfsi = NFS_I(inode);
5747 struct nfs4_cached_acl *acl;
5748 int ret = -ENOENT;
5749
5750 spin_lock(&inode->i_lock);
5751 acl = nfsi->nfs4_acl;
5752 if (acl == NULL)
5753 goto out;
5754 if (buf == NULL) /* user is just asking for length */
5755 goto out_len;
5756 if (acl->cached == 0)
5757 goto out;
5758 ret = -ERANGE; /* see getxattr(2) man page */
5759 if (acl->len > buflen)
5760 goto out;
5761 memcpy(buf, acl->data, acl->len);
5762out_len:
5763 ret = acl->len;
5764out:
5765 spin_unlock(&inode->i_lock);
5766 return ret;
5767}
5768
Sachin Prabhu5794d212012-04-17 14:36:40 +01005769static 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 +00005770{
5771 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005772 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005773
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005774 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005775 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005776 if (acl == NULL)
5777 goto out;
5778 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01005779 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005780 } else {
5781 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5782 if (acl == NULL)
5783 goto out;
5784 acl->cached = 0;
5785 }
5786 acl->len = acl_len;
5787out:
5788 nfs4_set_cached_acl(inode, acl);
5789}
5790
Andy Adamsonbf118a32011-12-07 11:55:27 -05005791/*
5792 * The getxattr API returns the required buffer length when called with a
5793 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5794 * the required buf. On a NULL buf, we send a page of data to the server
5795 * guessing that the ACL request can be serviced by a page. If so, we cache
5796 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5797 * the cache. If not so, we throw away the page, and cache the required
5798 * length. The next getxattr call will then produce another round trip to
5799 * the server, this time with the input buf of the required size.
5800 */
Trond Myklebust16b42892006-08-24 12:27:15 -04005801static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005802{
Olga Kornievskaia62a15732020-01-02 17:09:54 -05005803 struct page **pages;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005804 struct nfs_getaclargs args = {
5805 .fh = NFS_FH(inode),
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005806 .acl_len = buflen,
5807 };
Benny Halevy663c79b2009-04-01 09:21:59 -04005808 struct nfs_getaclres res = {
5809 .acl_len = buflen,
5810 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005811 struct rpc_message msg = {
5812 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5813 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04005814 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005815 };
Olga Kornievskaia62a15732020-01-02 17:09:54 -05005816 unsigned int npages;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005817 int ret = -ENOMEM, i;
Olga Kornievskaia62a15732020-01-02 17:09:54 -05005818 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005819
Olga Kornievskaia62a15732020-01-02 17:09:54 -05005820 if (buflen == 0)
5821 buflen = server->rsize;
5822
5823 npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
5824 pages = kmalloc_array(npages, sizeof(struct page *), GFP_NOFS);
5825 if (!pages)
5826 return -ENOMEM;
5827
5828 args.acl_pages = pages;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005829
Andy Adamsonbf118a32011-12-07 11:55:27 -05005830 for (i = 0; i < npages; i++) {
5831 pages[i] = alloc_page(GFP_KERNEL);
5832 if (!pages[i])
5833 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005834 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01005835
5836 /* for decoding across pages */
5837 res.acl_scratch = alloc_page(GFP_KERNEL);
5838 if (!res.acl_scratch)
5839 goto out_free;
5840
Andy Adamsonbf118a32011-12-07 11:55:27 -05005841 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005842
Peng Taode040be2012-01-10 22:42:47 +08005843 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05005844 __func__, buf, buflen, npages, args.acl_len);
5845 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5846 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005847 if (ret)
5848 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05005849
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005850 /* Handle the case where the passed-in buffer is too short */
5851 if (res.acl_flags & NFS4_ACL_TRUNC) {
5852 /* Did the user only issue a request for the acl length? */
5853 if (buf == NULL)
5854 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005855 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005856 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005857 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005858 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005859 if (buf) {
5860 if (res.acl_len > buflen) {
5861 ret = -ERANGE;
5862 goto out_free;
5863 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005864 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005865 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005866out_ok:
5867 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005868out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05005869 for (i = 0; i < npages; i++)
5870 if (pages[i])
5871 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05005872 if (res.acl_scratch)
5873 __free_page(res.acl_scratch);
Olga Kornievskaia62a15732020-01-02 17:09:54 -05005874 kfree(pages);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005875 return ret;
5876}
5877
Trond Myklebust16b42892006-08-24 12:27:15 -04005878static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5879{
Trond Myklebust0688e642019-04-07 13:59:09 -04005880 struct nfs4_exception exception = {
5881 .interruptible = true,
5882 };
Trond Myklebust16b42892006-08-24 12:27:15 -04005883 ssize_t ret;
5884 do {
5885 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04005886 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b42892006-08-24 12:27:15 -04005887 if (ret >= 0)
5888 break;
5889 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5890 } while (exception.retry);
5891 return ret;
5892}
5893
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005894static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5895{
5896 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005897 int ret;
5898
5899 if (!nfs4_server_supports_acls(server))
5900 return -EOPNOTSUPP;
Trond Myklebust1f3208b2021-03-25 11:04:34 -04005901 ret = nfs_revalidate_inode(inode, NFS_INO_INVALID_CHANGE);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005902 if (ret < 0)
5903 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005904 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5905 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005906 ret = nfs4_read_cached_acl(inode, buf, buflen);
5907 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05005908 /* -ENOENT is returned if there is no ACL or if there is an ACL
5909 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005910 return ret;
5911 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005912}
5913
Trond Myklebust16b42892006-08-24 12:27:15 -04005914static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005915{
5916 struct nfs_server *server = NFS_SERVER(inode);
5917 struct page *pages[NFS4ACL_MAXPAGES];
5918 struct nfs_setaclargs arg = {
5919 .fh = NFS_FH(inode),
5920 .acl_pages = pages,
5921 .acl_len = buflen,
5922 };
Benny Halevy73c403a2009-04-01 09:22:01 -04005923 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005924 struct rpc_message msg = {
5925 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5926 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04005927 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005928 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005929 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05005930 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005931
J. Bruce Fields4f8be1f2021-01-28 17:36:38 -05005932 /* You can't remove system.nfs4_acl: */
5933 if (buflen == 0)
5934 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005935 if (!nfs4_server_supports_acls(server))
5936 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005937 if (npages > ARRAY_SIZE(pages))
5938 return -ERANGE;
Frank van der Lindenccde1e92020-06-23 22:39:01 +00005939 i = nfs4_buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05005940 if (i < 0)
5941 return i;
Trond Myklebustc01d3642018-03-20 16:43:20 -04005942 nfs4_inode_make_writeable(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005943 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05005944
5945 /*
5946 * Free each page after tx, so the only ref left is
5947 * held by the network stack
5948 */
5949 for (; i > 0; i--)
5950 put_page(pages[i-1]);
5951
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005952 /*
5953 * Acl update can result in inode attribute update.
5954 * so mark the attribute cache invalid.
5955 */
5956 spin_lock(&inode->i_lock);
Trond Myklebustb6f80a22021-03-08 14:42:55 -05005957 nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE |
5958 NFS_INO_INVALID_CTIME |
5959 NFS_INO_REVAL_FORCED);
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005960 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04005961 nfs_access_zap_cache(inode);
5962 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005963 return ret;
5964}
5965
Trond Myklebust16b42892006-08-24 12:27:15 -04005966static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5967{
5968 struct nfs4_exception exception = { };
5969 int err;
5970 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005971 err = __nfs4_proc_set_acl(inode, buf, buflen);
5972 trace_nfs4_set_acl(inode, err);
5973 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b42892006-08-24 12:27:15 -04005974 &exception);
5975 } while (exception.retry);
5976 return err;
5977}
5978
David Quigleyaa9c2662013-05-22 12:50:44 -04005979#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5980static int _nfs4_get_security_label(struct inode *inode, void *buf,
5981 size_t buflen)
5982{
5983 struct nfs_server *server = NFS_SERVER(inode);
5984 struct nfs_fattr fattr;
5985 struct nfs4_label label = {0, 0, buflen, buf};
5986
5987 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005988 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005989 .fh = NFS_FH(inode),
5990 .bitmask = bitmask,
5991 };
5992 struct nfs4_getattr_res res = {
5993 .fattr = &fattr,
5994 .label = &label,
5995 .server = server,
5996 };
5997 struct rpc_message msg = {
5998 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005999 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04006000 .rpc_resp = &res,
6001 };
6002 int ret;
6003
6004 nfs_fattr_init(&fattr);
6005
Trond Myklebustfcb63a92013-11-01 12:42:25 -04006006 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04006007 if (ret)
6008 return ret;
6009 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
6010 return -ENOENT;
Ondrej Mosnacek53cb2452021-01-15 18:43:56 +01006011 return label.len;
David Quigleyaa9c2662013-05-22 12:50:44 -04006012}
6013
6014static int nfs4_get_security_label(struct inode *inode, void *buf,
6015 size_t buflen)
6016{
Trond Myklebust0688e642019-04-07 13:59:09 -04006017 struct nfs4_exception exception = {
6018 .interruptible = true,
6019 };
David Quigleyaa9c2662013-05-22 12:50:44 -04006020 int err;
6021
6022 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
6023 return -EOPNOTSUPP;
6024
6025 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04006026 err = _nfs4_get_security_label(inode, buf, buflen);
6027 trace_nfs4_get_security_label(inode, err);
6028 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04006029 &exception);
6030 } while (exception.retry);
6031 return err;
6032}
6033
6034static int _nfs4_do_set_security_label(struct inode *inode,
6035 struct nfs4_label *ilabel,
6036 struct nfs_fattr *fattr,
6037 struct nfs4_label *olabel)
6038{
6039
6040 struct iattr sattr = {0};
6041 struct nfs_server *server = NFS_SERVER(inode);
6042 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04006043 struct nfs_setattrargs arg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05006044 .fh = NFS_FH(inode),
6045 .iap = &sattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04006046 .server = server,
6047 .bitmask = bitmask,
6048 .label = ilabel,
6049 };
6050 struct nfs_setattrres res = {
6051 .fattr = fattr,
6052 .label = olabel,
6053 .server = server,
6054 };
6055 struct rpc_message msg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05006056 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
6057 .rpc_argp = &arg,
6058 .rpc_resp = &res,
David Quigleyaa9c2662013-05-22 12:50:44 -04006059 };
6060 int status;
6061
Jeff Layton12207f62013-11-01 10:49:32 -04006062 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04006063
Jeff Layton12207f62013-11-01 10:49:32 -04006064 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04006065 if (status)
6066 dprintk("%s failed: %d\n", __func__, status);
6067
6068 return status;
6069}
6070
6071static int nfs4_do_set_security_label(struct inode *inode,
6072 struct nfs4_label *ilabel,
6073 struct nfs_fattr *fattr,
6074 struct nfs4_label *olabel)
6075{
6076 struct nfs4_exception exception = { };
6077 int err;
6078
6079 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04006080 err = _nfs4_do_set_security_label(inode, ilabel,
6081 fattr, olabel);
6082 trace_nfs4_set_security_label(inode, err);
6083 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04006084 &exception);
6085 } while (exception.retry);
6086 return err;
6087}
6088
6089static int
Al Viro59301222016-05-27 10:19:30 -04006090nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04006091{
6092 struct nfs4_label ilabel, *olabel = NULL;
6093 struct nfs_fattr fattr;
David Quigleyaa9c2662013-05-22 12:50:44 -04006094 int status;
6095
6096 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
6097 return -EOPNOTSUPP;
6098
6099 nfs_fattr_init(&fattr);
6100
6101 ilabel.pi = 0;
6102 ilabel.lfs = 0;
6103 ilabel.label = (char *)buf;
6104 ilabel.len = buflen;
6105
David Quigleyaa9c2662013-05-22 12:50:44 -04006106 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
6107 if (IS_ERR(olabel)) {
6108 status = -PTR_ERR(olabel);
6109 goto out;
6110 }
6111
6112 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
6113 if (status == 0)
6114 nfs_setsecurity(inode, &fattr, olabel);
6115
6116 nfs4_label_free(olabel);
6117out:
David Quigleyaa9c2662013-05-22 12:50:44 -04006118 return status;
6119}
6120#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
6121
6122
Chuck Leverf0920752012-05-21 22:45:41 -04006123static void nfs4_init_boot_verifier(const struct nfs_client *clp,
6124 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05006125{
6126 __be32 verf[2];
6127
Chuck Lever2c820d92012-05-21 22:45:33 -04006128 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
6129 /* An impossible timestamp guarantees this value
6130 * will never match a generated boot time. */
Deepa Dinamani2f86e092016-10-01 16:46:26 -07006131 verf[0] = cpu_to_be32(U32_MAX);
6132 verf[1] = cpu_to_be32(U32_MAX);
Chuck Lever2c820d92012-05-21 22:45:33 -04006133 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04006134 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Deepa Dinamani2f86e092016-10-01 16:46:26 -07006135 u64 ns = ktime_to_ns(nn->boot_time);
6136
6137 verf[0] = cpu_to_be32(ns >> 32);
6138 verf[1] = cpu_to_be32(ns);
Chuck Lever2c820d92012-05-21 22:45:33 -04006139 }
Chuck Levercd937102012-03-02 17:14:31 -05006140 memcpy(bootverf->data, verf, sizeof(bootverf->data));
6141}
6142
Trond Myklebust1aee5512020-10-07 18:24:17 -04006143static size_t
Trond Myklebust39d43d12020-10-07 18:24:18 -04006144nfs4_get_uniquifier(struct nfs_client *clp, char *buf, size_t buflen)
Trond Myklebust1aee5512020-10-07 18:24:17 -04006145{
Trond Myklebust39d43d12020-10-07 18:24:18 -04006146 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
6147 struct nfs_netns_client *nn_clp = nn->nfs_client;
6148 const char *id;
6149
Trond Myklebust1aee5512020-10-07 18:24:17 -04006150 buf[0] = '\0';
6151
Trond Myklebust39d43d12020-10-07 18:24:18 -04006152 if (nn_clp) {
6153 rcu_read_lock();
6154 id = rcu_dereference(nn_clp->identifier);
6155 if (id)
6156 strscpy(buf, id, buflen);
6157 rcu_read_unlock();
6158 }
6159
6160 if (nfs4_client_id_uniquifier[0] != '\0' && buf[0] == '\0')
Trond Myklebust1aee5512020-10-07 18:24:17 -04006161 strscpy(buf, nfs4_client_id_uniquifier, buflen);
6162
6163 return strlen(buf);
6164}
6165
Jeff Laytona3192682015-06-09 19:43:59 -04006166static int
6167nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04006168{
Trond Myklebust1aee5512020-10-07 18:24:17 -04006169 char buf[NFS4_CLIENT_ID_UNIQ_LEN];
6170 size_t buflen;
Jeff Laytona3192682015-06-09 19:43:59 -04006171 size_t len;
6172 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04006173
Trond Myklebustceb3a162015-01-03 15:16:04 -05006174 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04006175 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08006176
Jeff Laytona3192682015-06-09 19:43:59 -04006177 rcu_read_lock();
Chuck Lever848a4eb2018-06-04 10:53:29 -04006178 len = 14 +
6179 strlen(clp->cl_rpcclient->cl_nodename) +
6180 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04006181 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
Jeff Laytona3192682015-06-09 19:43:59 -04006182 1;
6183 rcu_read_unlock();
Trond Myklebust1aee5512020-10-07 18:24:17 -04006184
Trond Myklebust39d43d12020-10-07 18:24:18 -04006185 buflen = nfs4_get_uniquifier(clp, buf, sizeof(buf));
Trond Myklebust1aee5512020-10-07 18:24:17 -04006186 if (buflen)
6187 len += buflen + 1;
6188
Jeff Laytona3192682015-06-09 19:43:59 -04006189 if (len > NFS4_OPAQUE_LIMIT + 1)
6190 return -EINVAL;
6191
6192 /*
6193 * Since this string is allocated at mount time, and held until the
6194 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6195 * about a memory-reclaim deadlock.
6196 */
6197 str = kmalloc(len, GFP_KERNEL);
6198 if (!str)
6199 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05006200
Chuck Levere984a552012-09-14 17:24:21 -04006201 rcu_read_lock();
Trond Myklebust1aee5512020-10-07 18:24:17 -04006202 if (buflen)
Chuck Lever025bb9f2018-06-04 10:53:34 -04006203 scnprintf(str, len, "Linux NFSv4.0 %s/%s/%s",
Trond Myklebust1aee5512020-10-07 18:24:17 -04006204 clp->cl_rpcclient->cl_nodename, buf,
Chuck Lever848a4eb2018-06-04 10:53:29 -04006205 rpc_peeraddr2str(clp->cl_rpcclient,
Chuck Lever025bb9f2018-06-04 10:53:34 -04006206 RPC_DISPLAY_ADDR));
Chuck Lever848a4eb2018-06-04 10:53:29 -04006207 else
Chuck Lever025bb9f2018-06-04 10:53:34 -04006208 scnprintf(str, len, "Linux NFSv4.0 %s/%s",
Chuck Lever848a4eb2018-06-04 10:53:29 -04006209 clp->cl_rpcclient->cl_nodename,
6210 rpc_peeraddr2str(clp->cl_rpcclient,
Chuck Lever025bb9f2018-06-04 10:53:34 -04006211 RPC_DISPLAY_ADDR));
Chuck Levere984a552012-09-14 17:24:21 -04006212 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04006213
Jeff Laytona3192682015-06-09 19:43:59 -04006214 clp->cl_owner_id = str;
6215 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04006216}
6217
Jeff Layton873e3852015-06-09 19:44:00 -04006218static int
Jeff Layton873e3852015-06-09 19:44:00 -04006219nfs4_init_uniform_client_string(struct nfs_client *clp)
6220{
Trond Myklebust1aee5512020-10-07 18:24:17 -04006221 char buf[NFS4_CLIENT_ID_UNIQ_LEN];
6222 size_t buflen;
Jeff Layton873e3852015-06-09 19:44:00 -04006223 size_t len;
6224 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05006225
6226 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04006227 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04006228
Jeff Layton873e3852015-06-09 19:44:00 -04006229 len = 10 + 10 + 1 + 10 + 1 +
6230 strlen(clp->cl_rpcclient->cl_nodename) + 1;
6231
Trond Myklebust39d43d12020-10-07 18:24:18 -04006232 buflen = nfs4_get_uniquifier(clp, buf, sizeof(buf));
Trond Myklebust1aee5512020-10-07 18:24:17 -04006233 if (buflen)
6234 len += buflen + 1;
6235
Jeff Layton873e3852015-06-09 19:44:00 -04006236 if (len > NFS4_OPAQUE_LIMIT + 1)
6237 return -EINVAL;
6238
6239 /*
6240 * Since this string is allocated at mount time, and held until the
6241 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6242 * about a memory-reclaim deadlock.
6243 */
6244 str = kmalloc(len, GFP_KERNEL);
6245 if (!str)
6246 return -ENOMEM;
6247
Trond Myklebust1aee5512020-10-07 18:24:17 -04006248 if (buflen)
6249 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
6250 clp->rpc_ops->version, clp->cl_minorversion,
6251 buf, clp->cl_rpcclient->cl_nodename);
6252 else
6253 scnprintf(str, len, "Linux NFSv%u.%u %s",
6254 clp->rpc_ops->version, clp->cl_minorversion,
6255 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04006256 clp->cl_owner_id = str;
6257 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04006258}
6259
Chuck Lever706cb8d2014-03-12 12:51:47 -04006260/*
6261 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
6262 * services. Advertise one based on the address family of the
6263 * clientaddr.
6264 */
6265static unsigned int
6266nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
6267{
6268 if (strchr(clp->cl_ipaddr, ':') != NULL)
6269 return scnprintf(buf, len, "tcp6");
6270 else
6271 return scnprintf(buf, len, "tcp");
6272}
6273
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006274static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
6275{
6276 struct nfs4_setclientid *sc = calldata;
6277
6278 if (task->tk_status == 0)
6279 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
6280}
6281
6282static const struct rpc_call_ops nfs4_setclientid_ops = {
6283 .rpc_call_done = nfs4_setclientid_done,
6284};
6285
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006286/**
6287 * nfs4_proc_setclientid - Negotiate client ID
6288 * @clp: state data structure
6289 * @program: RPC program for NFSv4 callback service
6290 * @port: IP port number for NFS4 callback service
NeilBrowna52458b2018-12-03 11:30:31 +11006291 * @cred: credential to use for this call
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006292 * @res: where to place the result
6293 *
6294 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6295 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006296int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
NeilBrowna52458b2018-12-03 11:30:31 +11006297 unsigned short port, const struct cred *cred,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006298 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006299{
6300 nfs4_verifier sc_verifier;
6301 struct nfs4_setclientid setclientid = {
6302 .sc_verifier = &sc_verifier,
6303 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04006304 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006305 };
6306 struct rpc_message msg = {
6307 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
6308 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006309 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01006310 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006311 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006312 struct rpc_task_setup task_setup_data = {
6313 .rpc_client = clp->cl_rpcclient,
6314 .rpc_message = &msg,
6315 .callback_ops = &nfs4_setclientid_ops,
6316 .callback_data = &setclientid,
NeilBrown5a0c2572019-05-30 10:41:28 +10006317 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006318 };
Robert Milkowski7dc29932020-01-30 09:43:25 +00006319 unsigned long now = jiffies;
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006320 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006321
Chuck Leverde734832012-07-11 16:30:50 -04006322 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04006323 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04006324
6325 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
6326 status = nfs4_init_uniform_client_string(clp);
6327 else
Jeff Laytona3192682015-06-09 19:43:59 -04006328 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04006329
6330 if (status)
6331 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04006332
Chuck Leverde734832012-07-11 16:30:50 -04006333 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04006334 setclientid.sc_netid_len =
6335 nfs4_init_callback_netid(clp,
6336 setclientid.sc_netid,
6337 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04006338 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05006339 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006340 clp->cl_ipaddr, port >> 8, port & 255);
6341
Jeff Layton3a6bb732015-06-09 19:43:57 -04006342 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006343 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04006344 clp->cl_owner_id);
Anna Schumakerdae40962019-08-14 15:28:28 -04006345
6346 status = nfs4_call_sync_custom(&task_setup_data);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006347 if (setclientid.sc_cred) {
Chuck Lever1047ec82019-10-04 09:58:54 -04006348 kfree(clp->cl_acceptor);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006349 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
6350 put_rpccred(setclientid.sc_cred);
6351 }
Robert Milkowski7dc29932020-01-30 09:43:25 +00006352
6353 if (status == 0)
6354 do_renew_lease(clp, now);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006355out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006356 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006357 dprintk("NFS reply setclientid: %d\n", status);
6358 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006359}
6360
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006361/**
6362 * nfs4_proc_setclientid_confirm - Confirm client ID
6363 * @clp: state data structure
Trond Myklebust302fad72019-02-18 13:32:38 -05006364 * @arg: result of a previous SETCLIENTID
NeilBrowna52458b2018-12-03 11:30:31 +11006365 * @cred: credential to use for this call
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006366 *
6367 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6368 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04006369int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006370 struct nfs4_setclientid_res *arg,
NeilBrowna52458b2018-12-03 11:30:31 +11006371 const struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006372{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006373 struct rpc_message msg = {
6374 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006375 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01006376 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006377 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006378 int status;
6379
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006380 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
6381 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6382 clp->cl_clientid);
NeilBrown5a0c2572019-05-30 10:41:28 +10006383 status = rpc_call_sync(clp->cl_rpcclient, &msg,
6384 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006385 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006386 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006387 return status;
6388}
6389
Trond Myklebustfe650402006-01-03 09:55:18 +01006390struct nfs4_delegreturndata {
6391 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01006392 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01006393 struct nfs_fh fh;
6394 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006395 unsigned long timestamp;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006396 struct {
6397 struct nfs4_layoutreturn_args arg;
6398 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04006399 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006400 u32 roc_barrier;
6401 bool roc;
6402 } lr;
Trond Myklebustfa178f22006-01-03 09:55:38 +01006403 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01006404 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08006405 struct inode *inode;
Trond Myklebustfe650402006-01-03 09:55:18 +01006406};
6407
Trond Myklebustfe650402006-01-03 09:55:18 +01006408static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
6409{
6410 struct nfs4_delegreturndata *data = calldata;
Trond Myklebuste0dba012017-11-07 11:02:32 -05006411 struct nfs4_exception exception = {
6412 .inode = data->inode,
6413 .stateid = &data->stateid,
6414 };
Andy Adamson938e1012009-04-01 09:22:28 -04006415
Trond Myklebust14516c32010-07-31 14:29:06 -04006416 if (!nfs4_sequence_done(task, &data->res.seq_res))
6417 return;
Andy Adamson938e1012009-04-01 09:22:28 -04006418
Trond Myklebustca8acf82013-08-13 10:36:56 -04006419 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Trond Myklebust586f1c32016-11-15 15:03:33 -05006420
6421 /* Handle Layoutreturn errors */
Trond Myklebust078000d2021-01-04 13:18:03 -05006422 if (pnfs_roc_done(task, &data->args.lr_args, &data->res.lr_res,
6423 &data->res.lr_ret) == -EAGAIN)
Trond Myklebust287a9c52019-09-20 07:23:41 -04006424 goto out_restart;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006425
Ricardo Labiaga79708862009-12-07 09:23:21 -05006426 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05006427 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01006428 renew_lease(data->res.server, data->timestamp);
Trond Myklebust23ea44c2016-11-10 16:06:28 -05006429 break;
Trond Myklebustc97cf602013-11-19 16:34:14 -05006430 case -NFS4ERR_ADMIN_REVOKED:
6431 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust26d36302016-09-22 13:39:05 -04006432 case -NFS4ERR_EXPIRED:
6433 nfs4_free_revoked_stateid(data->res.server,
6434 data->args.stateid,
6435 task->tk_msg.rpc_cred);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05006436 fallthrough;
Trond Myklebustc97cf602013-11-19 16:34:14 -05006437 case -NFS4ERR_BAD_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05006438 case -NFS4ERR_STALE_STATEID:
Trond Myklebust244fcd22019-12-20 10:43:37 -05006439 case -ETIMEDOUT:
Trond Myklebustc97cf602013-11-19 16:34:14 -05006440 task->tk_status = 0;
6441 break;
Trond Myklebust12f275c2017-11-06 15:28:05 -05006442 case -NFS4ERR_OLD_STATEID:
Trond Myklebust246afc0a2019-10-24 18:00:35 -04006443 if (!nfs4_refresh_delegation_stateid(&data->stateid, data->inode))
6444 nfs4_stateid_seqid_inc(&data->stateid);
Trond Myklebust70d136b2019-10-26 22:37:40 -04006445 if (data->args.bitmask) {
6446 data->args.bitmask = NULL;
6447 data->res.fattr = NULL;
6448 }
Trond Myklebust246afc0a2019-10-24 18:00:35 -04006449 goto out_restart;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05006450 case -NFS4ERR_ACCESS:
6451 if (data->args.bitmask) {
6452 data->args.bitmask = NULL;
6453 data->res.fattr = NULL;
Trond Myklebust140087fd2017-11-06 15:28:10 -05006454 goto out_restart;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05006455 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05006456 fallthrough;
Ricardo Labiaga79708862009-12-07 09:23:21 -05006457 default:
Trond Myklebuste0dba012017-11-07 11:02:32 -05006458 task->tk_status = nfs4_async_handle_exception(task,
6459 data->res.server, task->tk_status,
6460 &exception);
6461 if (exception.retry)
Trond Myklebust140087fd2017-11-06 15:28:10 -05006462 goto out_restart;
Ricardo Labiaga79708862009-12-07 09:23:21 -05006463 }
Trond Myklebustd51f91d2019-10-21 14:22:14 -04006464 nfs_delegation_mark_returned(data->inode, data->args.stateid);
Ricardo Labiaga79708862009-12-07 09:23:21 -05006465 data->rpc_status = task->tk_status;
Trond Myklebust140087fd2017-11-06 15:28:10 -05006466 return;
Trond Myklebust140087fd2017-11-06 15:28:10 -05006467out_restart:
6468 task->tk_status = 0;
6469 rpc_restart_call_prepare(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01006470}
6471
6472static void nfs4_delegreturn_release(void *calldata)
6473{
Peng Tao039b7562014-07-03 13:05:02 +08006474 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006475 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08006476
Trond Myklebust078000d2021-01-04 13:18:03 -05006477 if (data->lr.roc)
6478 pnfs_roc_release(&data->lr.arg, &data->lr.res,
6479 data->res.lr_ret);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006480 if (inode) {
Trond Myklebust0bc2c9b2016-12-16 19:48:09 -05006481 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006482 nfs_iput_and_deactive(inode);
6483 }
Trond Myklebustfe650402006-01-03 09:55:18 +01006484 kfree(calldata);
6485}
6486
Andy Adamson938e1012009-04-01 09:22:28 -04006487static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
6488{
6489 struct nfs4_delegreturndata *d_data;
Trond Myklebustc8bf7072018-06-15 16:31:02 -04006490 struct pnfs_layout_hdr *lo;
Andy Adamson938e1012009-04-01 09:22:28 -04006491
6492 d_data = (struct nfs4_delegreturndata *)data;
6493
Trond Myklebust5326de92019-11-13 09:39:36 +01006494 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task)) {
6495 nfs4_sequence_done(task, &d_data->res.seq_res);
Peng Tao500d7012015-09-22 11:35:22 +08006496 return;
Trond Myklebust5326de92019-11-13 09:39:36 +01006497 }
Peng Tao500d7012015-09-22 11:35:22 +08006498
Trond Myklebustc8bf7072018-06-15 16:31:02 -04006499 lo = d_data->args.lr_args ? d_data->args.lr_args->layout : NULL;
6500 if (lo && !pnfs_layout_is_valid(lo)) {
6501 d_data->args.lr_args = NULL;
6502 d_data->res.lr_res = NULL;
6503 }
6504
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006505 nfs4_setup_sequence(d_data->res.server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006506 &d_data->args.seq_args,
6507 &d_data->res.seq_res,
6508 task);
Andy Adamson938e1012009-04-01 09:22:28 -04006509}
Andy Adamson938e1012009-04-01 09:22:28 -04006510
Jesper Juhlc8d149f2006-03-20 13:44:07 -05006511static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04006512 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01006513 .rpc_call_done = nfs4_delegreturn_done,
6514 .rpc_release = nfs4_delegreturn_release,
6515};
6516
NeilBrowna52458b2018-12-03 11:30:31 +11006517static int _nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01006518{
6519 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01006520 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01006521 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006522 struct rpc_message msg = {
6523 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
6524 .rpc_cred = cred,
6525 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006526 struct rpc_task_setup task_setup_data = {
6527 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04006528 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006529 .callback_ops = &nfs4_delegreturn_ops,
Trond Myklebustf304a802020-05-11 10:42:04 -04006530 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006531 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05006532 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01006533
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006534 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01006535 if (data == NULL)
6536 return -ENOMEM;
Anna Schumakerfba83f32018-05-04 16:22:50 -04006537 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Andrew Elble99ade3c2015-12-02 09:39:51 -05006538
6539 nfs4_state_protect(server->nfs_client,
6540 NFS_SP4_MACH_CRED_CLEANUP,
6541 &task_setup_data.rpc_client, &msg);
6542
Trond Myklebustfe650402006-01-03 09:55:18 +01006543 data->args.fhandle = &data->fh;
6544 data->args.stateid = &data->stateid;
Trond Myklebust332d1a02021-03-25 18:15:36 -04006545 nfs4_bitmask_set(data->args.bitmask_store,
6546 server->cache_consistency_bitmask, inode, server,
6547 NULL);
6548 data->args.bitmask = data->args.bitmask_store;
Trond Myklebustfe650402006-01-03 09:55:18 +01006549 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05006550 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01006551 data->res.fattr = &data->fattr;
6552 data->res.server = server;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006553 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust4d796d72016-09-23 11:38:08 -04006554 data->lr.arg.ld_private = &data->lr.ld_private;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006555 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01006556 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01006557 data->rpc_status = 0;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006558 data->inode = nfs_igrab_and_active(inode);
Trond Myklebust078000d2021-01-04 13:18:03 -05006559 if (data->inode || issync) {
6560 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res,
6561 cred);
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006562 if (data->lr.roc) {
6563 data->args.lr_args = &data->lr.arg;
6564 data->res.lr_res = &data->lr.res;
6565 }
6566 }
Trond Myklebustfe650402006-01-03 09:55:18 +01006567
Trond Myklebustc970aa82007-07-14 15:39:59 -04006568 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05006569 msg.rpc_argp = &data->args;
6570 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006571 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006572 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01006573 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05006574 if (!issync)
6575 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05006576 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05006577 if (status != 0)
6578 goto out;
6579 status = data->rpc_status;
Trond Myklebuste6f81072008-01-24 18:14:34 -05006580out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006581 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01006582 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006583}
6584
NeilBrowna52458b2018-12-03 11:30:31 +11006585int nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006586{
6587 struct nfs_server *server = NFS_SERVER(inode);
6588 struct nfs4_exception exception = { };
6589 int err;
6590 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05006591 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006592 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006593 switch (err) {
6594 case -NFS4ERR_STALE_STATEID:
6595 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006596 case 0:
6597 return 0;
6598 }
6599 err = nfs4_handle_exception(server, err, &exception);
6600 } while (exception.retry);
6601 return err;
6602}
6603
Linus Torvalds1da177e2005-04-16 15:20:36 -07006604static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6605{
6606 struct inode *inode = state->inode;
6607 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04006608 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006609 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006610 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006611 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006612 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006613 struct nfs_lockt_res res = {
6614 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006615 };
6616 struct rpc_message msg = {
6617 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
Anna Schumakerd9b67e12017-01-11 15:04:25 -05006618 .rpc_argp = &arg,
6619 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006620 .rpc_cred = state->owner->so_cred,
6621 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006622 struct nfs4_lock_state *lsp;
6623 int status;
6624
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006625 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00006626 status = nfs4_set_lock_state(state, request);
6627 if (status != 0)
6628 goto out;
6629 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006630 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006631 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00006632 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006633 switch (status) {
6634 case 0:
6635 request->fl_type = F_UNLCK;
6636 break;
6637 case -NFS4ERR_DENIED:
6638 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006639 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05006640 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04006641 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00006642out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006643 return status;
6644}
6645
6646static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6647{
Trond Myklebust0688e642019-04-07 13:59:09 -04006648 struct nfs4_exception exception = {
6649 .interruptible = true,
6650 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006651 int err;
6652
6653 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006654 err = _nfs4_proc_getlk(state, cmd, request);
6655 trace_nfs4_get_lock(request, state, cmd, err);
6656 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006657 &exception);
6658 } while (exception.retry);
6659 return err;
6660}
6661
Trond Myklebust32c6e7e2019-09-20 07:23:48 -04006662/*
6663 * Update the seqid of a lock stateid after receiving
6664 * NFS4ERR_OLD_STATEID
6665 */
6666static bool nfs4_refresh_lock_old_stateid(nfs4_stateid *dst,
6667 struct nfs4_lock_state *lsp)
6668{
6669 struct nfs4_state *state = lsp->ls_state;
6670 bool ret = false;
6671
6672 spin_lock(&state->state_lock);
6673 if (!nfs4_stateid_match_other(dst, &lsp->ls_stateid))
6674 goto out;
6675 if (!nfs4_stateid_is_newer(&lsp->ls_stateid, dst))
6676 nfs4_stateid_seqid_inc(dst);
6677 else
6678 dst->seqid = lsp->ls_stateid.seqid;
6679 ret = true;
6680out:
6681 spin_unlock(&state->state_lock);
6682 return ret;
6683}
6684
6685static bool nfs4_sync_lock_stateid(nfs4_stateid *dst,
6686 struct nfs4_lock_state *lsp)
6687{
6688 struct nfs4_state *state = lsp->ls_state;
6689 bool ret;
6690
6691 spin_lock(&state->state_lock);
6692 ret = !nfs4_stateid_match_other(dst, &lsp->ls_stateid);
6693 nfs4_stateid_copy(dst, &lsp->ls_stateid);
6694 spin_unlock(&state->state_lock);
6695 return ret;
6696}
6697
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006698struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006699 struct nfs_locku_args arg;
6700 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006701 struct nfs4_lock_state *lsp;
6702 struct nfs_open_context *ctx;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006703 struct nfs_lock_context *l_ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006704 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04006705 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006706 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006707};
6708
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006709static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
6710 struct nfs_open_context *ctx,
6711 struct nfs4_lock_state *lsp,
6712 struct nfs_seqid *seqid)
6713{
6714 struct nfs4_unlockdata *p;
Trond Myklebust32c6e7e2019-09-20 07:23:48 -04006715 struct nfs4_state *state = lsp->ls_state;
6716 struct inode *inode = state->inode;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006717
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006718 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006719 if (p == NULL)
6720 return NULL;
6721 p->arg.fh = NFS_FH(inode);
6722 p->arg.fl = &p->fl;
6723 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006724 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006725 p->lsp = lsp;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006726 /* Ensure we don't close file until we're done freeing locks! */
6727 p->ctx = get_nfs_open_context(ctx);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006728 p->l_ctx = nfs_get_lock_context(ctx);
NeilBrown7b587e12018-11-30 10:04:08 +11006729 locks_init_lock(&p->fl);
6730 locks_copy_lock(&p->fl, fl);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006731 p->server = NFS_SERVER(inode);
Trond Myklebust32c6e7e2019-09-20 07:23:48 -04006732 spin_lock(&state->state_lock);
6733 nfs4_stateid_copy(&p->arg.stateid, &lsp->ls_stateid);
6734 spin_unlock(&state->state_lock);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006735 return p;
6736}
6737
Trond Myklebust06f814a2006-01-03 09:55:07 +01006738static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006739{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006740 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006741 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006742 nfs4_put_lock_state(calldata->lsp);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006743 nfs_put_lock_context(calldata->l_ctx);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006744 put_nfs_open_context(calldata->ctx);
6745 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006746}
6747
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006748static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006749{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006750 struct nfs4_unlockdata *calldata = data;
Trond Myklebust82571552017-11-07 11:14:49 -05006751 struct nfs4_exception exception = {
6752 .inode = calldata->lsp->ls_state->inode,
6753 .stateid = &calldata->arg.stateid,
6754 };
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006755
Trond Myklebust14516c32010-07-31 14:29:06 -04006756 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
6757 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006758 switch (task->tk_status) {
6759 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01006760 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton75575dd2016-09-17 18:17:32 -04006761 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006762 if (nfs4_update_lock_stateid(calldata->lsp,
6763 &calldata->res.stateid))
6764 break;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05006765 fallthrough;
Trond Myklebust26d36302016-09-22 13:39:05 -04006766 case -NFS4ERR_ADMIN_REVOKED:
6767 case -NFS4ERR_EXPIRED:
6768 nfs4_free_revoked_stateid(calldata->server,
6769 &calldata->arg.stateid,
6770 task->tk_msg.rpc_cred);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05006771 fallthrough;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05006772 case -NFS4ERR_BAD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006773 case -NFS4ERR_STALE_STATEID:
Trond Myklebust32c6e7e2019-09-20 07:23:48 -04006774 if (nfs4_sync_lock_stateid(&calldata->arg.stateid,
6775 calldata->lsp))
6776 rpc_restart_call_prepare(task);
6777 break;
6778 case -NFS4ERR_OLD_STATEID:
6779 if (nfs4_refresh_lock_old_stateid(&calldata->arg.stateid,
6780 calldata->lsp))
Trond Myklebust425c1d42015-01-24 14:57:53 -05006781 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006782 break;
6783 default:
Trond Myklebust82571552017-11-07 11:14:49 -05006784 task->tk_status = nfs4_async_handle_exception(task,
6785 calldata->server, task->tk_status,
6786 &exception);
6787 if (exception.retry)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006788 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006789 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04006790 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006791}
6792
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006793static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006794{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006795 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006796
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006797 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
6798 nfs_async_iocounter_wait(task, calldata->l_ctx))
6799 return;
6800
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006801 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006802 goto out_wait;
Trond Myklebust795a88c2012-09-10 13:26:49 -04006803 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006804 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006805 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006806 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006807 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006808 if (nfs4_setup_sequence(calldata->server->nfs_client,
Andy Adamsona8936932009-04-01 09:22:23 -04006809 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006810 &calldata->res.seq_res,
6811 task) != 0)
6812 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006813 return;
6814out_no_action:
6815 task->tk_action = NULL;
6816out_wait:
6817 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006818}
6819
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006820static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006821 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006822 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01006823 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006824};
6825
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006826static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6827 struct nfs_open_context *ctx,
6828 struct nfs4_lock_state *lsp,
6829 struct nfs_seqid *seqid)
6830{
6831 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006832 struct rpc_message msg = {
6833 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6834 .rpc_cred = ctx->cred,
6835 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006836 struct rpc_task_setup task_setup_data = {
6837 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006838 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006839 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006840 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006841 .flags = RPC_TASK_ASYNC,
6842 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006843
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006844 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6845 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6846
Frank Filz137d6ac2007-07-09 15:32:29 -07006847 /* Ensure this is an unlock - when canceling a lock, the
6848 * canceled lock is passed in, and it won't be an unlock.
6849 */
6850 fl->fl_type = F_UNLCK;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006851 if (fl->fl_flags & FL_CLOSE)
6852 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
Frank Filz137d6ac2007-07-09 15:32:29 -07006853
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006854 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6855 if (data == NULL) {
6856 nfs_free_seqid(seqid);
6857 return ERR_PTR(-ENOMEM);
6858 }
6859
Anna Schumakerfba83f32018-05-04 16:22:50 -04006860 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1, 0);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006861 msg.rpc_argp = &data->arg;
6862 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006863 task_setup_data.callback_data = data;
6864 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006865}
6866
Linus Torvalds1da177e2005-04-16 15:20:36 -07006867static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6868{
Trond Myklebust65b62a22013-02-07 10:54:07 -05006869 struct inode *inode = state->inode;
6870 struct nfs4_state_owner *sp = state->owner;
6871 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006872 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006873 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01006874 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006875 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006876 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006877 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006878
Trond Myklebust9b073572006-06-29 16:38:34 -04006879 status = nfs4_set_lock_state(state, request);
6880 /* Unlock _before_ we do the RPC call */
6881 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05006882 /* Exclude nfs_delegation_claim_locks() */
6883 mutex_lock(&sp->so_delegreturn_mutex);
6884 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006885 down_read(&nfsi->rwsem);
Jeff Layton75575dd2016-09-17 18:17:32 -04006886 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05006887 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006888 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006889 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05006890 }
6891 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006892 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006893 if (status != 0)
6894 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006895 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006896 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04006897 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6898 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006899 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6900 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04006901 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006902 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04006903 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006904 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006905 status = PTR_ERR(task);
6906 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04006907 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05006908 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006909 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04006910out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006911 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006912 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006913 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006914}
6915
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006916struct nfs4_lockdata {
6917 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006918 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006919 struct nfs4_lock_state *lsp;
6920 struct nfs_open_context *ctx;
6921 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006922 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006923 int rpc_status;
6924 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04006925 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006926};
6927
6928static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006929 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6930 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006931{
6932 struct nfs4_lockdata *p;
6933 struct inode *inode = lsp->ls_state->inode;
6934 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05006935 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006936
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006937 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006938 if (p == NULL)
6939 return NULL;
6940
6941 p->arg.fh = NFS_FH(inode);
6942 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006943 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006944 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006945 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006946 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6947 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006948 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006949 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04006950 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006951 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006952 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006953 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006954 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04006955 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006956 p->ctx = get_nfs_open_context(ctx);
NeilBrown7b587e12018-11-30 10:04:08 +11006957 locks_init_lock(&p->fl);
6958 locks_copy_lock(&p->fl, fl);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006959 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006960out_free_seqid:
6961 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006962out_free:
6963 kfree(p);
6964 return NULL;
6965}
6966
6967static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6968{
6969 struct nfs4_lockdata *data = calldata;
6970 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006971
Harvey Harrison3110ff82008-05-02 13:42:44 -07006972 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006973 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006974 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006975 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05006976 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006977 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006978 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006979 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05006980 nfs4_stateid_copy(&data->arg.open_stateid,
6981 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006982 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006983 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006984 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006985 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006986 nfs4_stateid_copy(&data->arg.lock_stateid,
6987 &data->lsp->ls_stateid);
6988 }
Trond Myklebust5d422302013-03-14 16:57:48 -04006989 if (!nfs4_valid_open_stateid(state)) {
6990 data->rpc_status = -EBADF;
6991 task->tk_action = NULL;
6992 goto out_release_open_seqid;
6993 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006994 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006995 if (nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust035168ab2010-06-16 09:52:26 -04006996 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006997 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006998 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04006999 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04007000out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04007001 nfs_release_seqid(data->arg.open_seqid);
7002out_release_lock_seqid:
7003 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05007004out_wait:
7005 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007006 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08007007}
7008
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007009static void nfs4_lock_done(struct rpc_task *task, void *calldata)
7010{
7011 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05007012 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007013
Harvey Harrison3110ff82008-05-02 13:42:44 -07007014 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007015
Trond Myklebust14516c32010-07-31 14:29:06 -04007016 if (!nfs4_sequence_done(task, &data->res.seq_res))
7017 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04007018
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007019 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05007020 switch (task->tk_status) {
7021 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00007022 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05007023 data->timestamp);
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04007024 if (data->arg.new_lock && !data->cancelled) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05007025 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04007026 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0)
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04007027 goto out_restart;
Trond Myklebustc69899a2015-01-24 16:03:52 -05007028 }
Trond Myklebust39071e62015-01-24 15:07:56 -05007029 if (data->arg.new_lock_owner != 0) {
7030 nfs_confirm_seqid(&lsp->ls_seqid, 0);
7031 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
7032 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04007033 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
7034 goto out_restart;
Trond Myklebust425c1d42015-01-24 14:57:53 -05007035 break;
7036 case -NFS4ERR_BAD_STATEID:
7037 case -NFS4ERR_OLD_STATEID:
7038 case -NFS4ERR_STALE_STATEID:
7039 case -NFS4ERR_EXPIRED:
7040 if (data->arg.new_lock_owner != 0) {
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04007041 if (!nfs4_stateid_match(&data->arg.open_stateid,
Trond Myklebust425c1d42015-01-24 14:57:53 -05007042 &lsp->ls_state->open_stateid))
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04007043 goto out_restart;
7044 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
Trond Myklebust425c1d42015-01-24 14:57:53 -05007045 &lsp->ls_stateid))
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04007046 goto out_restart;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007047 }
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04007048out_done:
Harvey Harrison3110ff82008-05-02 13:42:44 -07007049 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04007050 return;
7051out_restart:
7052 if (!data->cancelled)
7053 rpc_restart_call_prepare(task);
7054 goto out_done;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007055}
7056
7057static void nfs4_lock_release(void *calldata)
7058{
7059 struct nfs4_lockdata *data = calldata;
7060
Harvey Harrison3110ff82008-05-02 13:42:44 -07007061 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05007062 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04007063 if (data->cancelled && data->rpc_status == 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007064 struct rpc_task *task;
7065 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
7066 data->arg.lock_seqid);
7067 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08007068 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07007069 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007070 } else
7071 nfs_free_seqid(data->arg.lock_seqid);
7072 nfs4_put_lock_state(data->lsp);
7073 put_nfs_open_context(data->ctx);
7074 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07007075 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007076}
7077
7078static const struct rpc_call_ops nfs4_lock_ops = {
7079 .rpc_call_prepare = nfs4_lock_prepare,
7080 .rpc_call_done = nfs4_lock_done,
7081 .rpc_release = nfs4_lock_release,
7082};
7083
Trond Myklebust2bee72a2010-01-26 15:42:21 -05007084static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
7085{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05007086 switch (error) {
7087 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustd7f3e4b2016-09-22 13:39:09 -04007088 case -NFS4ERR_EXPIRED:
Trond Myklebust2bee72a2010-01-26 15:42:21 -05007089 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05007090 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05007091 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04007092 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05007093 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05007094 break;
7095 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05007096 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05007097 nfs4_schedule_lease_recovery(server->nfs_client);
zhengbin8b98a532019-12-19 18:34:47 +08007098 }
Trond Myklebust2bee72a2010-01-26 15:42:21 -05007099}
7100
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08007101static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007102{
7103 struct nfs4_lockdata *data;
7104 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04007105 struct rpc_message msg = {
7106 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
7107 .rpc_cred = state->owner->so_cred,
7108 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04007109 struct rpc_task_setup task_setup_data = {
7110 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04007111 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04007112 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05007113 .workqueue = nfsiod_workqueue,
Trond Myklebust61296502020-02-07 19:38:12 -05007114 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
Trond Myklebustc970aa82007-07-14 15:39:59 -04007115 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007116 int ret;
7117
Harvey Harrison3110ff82008-05-02 13:42:44 -07007118 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04007119 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04007120 fl->fl_u.nfs4_fl.owner,
7121 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007122 if (data == NULL)
7123 return -ENOMEM;
7124 if (IS_SETLKW(cmd))
7125 data->arg.block = 1;
Anna Schumakerfba83f32018-05-04 16:22:50 -04007126 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1,
7127 recovery_type > NFS_LOCK_NEW);
Trond Myklebust1174dd12010-12-21 10:52:24 -05007128 msg.rpc_argp = &data->arg;
7129 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04007130 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007131 if (recovery_type > NFS_LOCK_NEW) {
7132 if (recovery_type == NFS_LOCK_RECLAIM)
7133 data->arg.reclaim = NFS_LOCK_RECLAIM;
Trond Myklebustc69899a2015-01-24 16:03:52 -05007134 } else
7135 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04007136 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05007137 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007138 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05007139 ret = rpc_wait_for_completion_task(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007140 if (ret == 0) {
7141 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05007142 if (ret)
7143 nfs4_handle_setlk_error(data->server, data->lsp,
7144 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007145 } else
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04007146 data->cancelled = true;
Dave Wysochanski3d1a90a2020-12-11 05:12:51 -05007147 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05007148 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07007149 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007150 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007151}
7152
7153static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
7154{
Trond Myklebust202b50d2005-06-22 17:16:29 +00007155 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04007156 struct nfs4_exception exception = {
7157 .inode = state->inode,
7158 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00007159 int err;
7160
7161 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04007162 /* Cache the lock if possible... */
7163 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
7164 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08007165 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04007166 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00007167 break;
7168 nfs4_handle_exception(server, err, &exception);
7169 } while (exception.retry);
7170 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007171}
7172
7173static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
7174{
Trond Myklebust202b50d2005-06-22 17:16:29 +00007175 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04007176 struct nfs4_exception exception = {
7177 .inode = state->inode,
7178 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00007179 int err;
7180
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05007181 err = nfs4_set_lock_state(state, request);
7182 if (err != 0)
7183 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04007184 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10007185 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
7186 return 0;
7187 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00007188 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04007189 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
7190 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08007191 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05007192 switch (err) {
7193 default:
7194 goto out;
7195 case -NFS4ERR_GRACE:
7196 case -NFS4ERR_DELAY:
7197 nfs4_handle_exception(server, err, &exception);
7198 err = 0;
7199 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00007200 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05007201out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00007202 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007203}
7204
Bryan Schumakerf062eb62011-06-02 14:59:10 -04007205#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05007206static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
7207{
Trond Myklebustc5896fc2016-09-22 13:39:03 -04007208 struct nfs4_lock_state *lsp;
7209 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05007210
Trond Myklebustc5896fc2016-09-22 13:39:03 -04007211 status = nfs4_set_lock_state(state, request);
7212 if (status != 0)
7213 return status;
7214 lsp = request->fl_u.nfs4_fl.owner;
7215 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
7216 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
7217 return 0;
Anna Schumaker81b68de2017-01-11 16:41:34 -05007218 return nfs4_lock_expired(state, request);
Bryan Schumakerf062eb62011-06-02 14:59:10 -04007219}
7220#endif
7221
Linus Torvalds1da177e2005-04-16 15:20:36 -07007222static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7223{
Trond Myklebust19e03c52008-12-23 15:21:44 -05007224 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04007225 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04007226 unsigned char fl_flags = request->fl_flags;
Jeff Layton1ea67db2016-09-17 18:17:37 -04007227 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007228
Trond Myklebust01c3b862006-06-29 16:38:39 -04007229 request->fl_flags |= FL_ACCESS;
Jeff Layton75575dd2016-09-17 18:17:32 -04007230 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04007231 if (status < 0)
7232 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04007233 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05007234 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04007235 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04007236 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04007237 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05007238 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton75575dd2016-09-17 18:17:32 -04007239 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05007240 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04007241 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05007242 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04007243 }
Trond Myklebust9a99af42013-02-04 20:17:49 -05007244 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04007245 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08007246 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04007247out:
7248 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007249 return status;
7250}
7251
7252static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7253{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05007254 struct nfs4_exception exception = {
7255 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04007256 .inode = state->inode,
Trond Myklebust0688e642019-04-07 13:59:09 -04007257 .interruptible = true,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05007258 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07007259 int err;
7260
7261 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07007262 err = _nfs4_proc_setlk(state, cmd, request);
7263 if (err == -NFS4ERR_DENIED)
7264 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007265 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07007266 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007267 } while (exception.retry);
7268 return err;
7269}
7270
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04007271#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
7272#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
7273
7274static int
Jeff Laytona1d617d82016-09-17 18:17:39 -04007275nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
7276 struct file_lock *request)
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04007277{
7278 int status = -ERESTARTSYS;
7279 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
7280
7281 while(!signalled()) {
7282 status = nfs4_proc_setlk(state, cmd, request);
7283 if ((status != -EAGAIN) || IS_SETLK(cmd))
7284 break;
7285 freezable_schedule_timeout_interruptible(timeout);
7286 timeout *= 2;
7287 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
7288 status = -ERESTARTSYS;
7289 }
7290 return status;
7291}
7292
Jeff Laytona1d617d82016-09-17 18:17:39 -04007293#ifdef CONFIG_NFS_V4_1
7294struct nfs4_lock_waiter {
Jeff Laytona1d617d82016-09-17 18:17:39 -04007295 struct inode *inode;
Trond Myklebust86438182021-02-02 14:22:51 -05007296 struct nfs_lowner owner;
7297 wait_queue_entry_t wait;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007298};
7299
7300static int
Ingo Molnarac6424b2017-06-20 12:06:13 +02007301nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
Jeff Laytona1d617d82016-09-17 18:17:39 -04007302{
Trond Myklebust86438182021-02-02 14:22:51 -05007303 struct nfs4_lock_waiter *waiter =
7304 container_of(wait, struct nfs4_lock_waiter, wait);
Jeff Laytona1d617d82016-09-17 18:17:39 -04007305
Jeff Layton57174592018-03-18 08:37:03 -04007306 /* NULL key means to wake up everyone */
7307 if (key) {
7308 struct cb_notify_lock_args *cbnl = key;
7309 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
Trond Myklebust86438182021-02-02 14:22:51 -05007310 *wowner = &waiter->owner;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007311
Jeff Layton57174592018-03-18 08:37:03 -04007312 /* Only wake if the callback was for the same owner. */
7313 if (lowner->id != wowner->id || lowner->s_dev != wowner->s_dev)
7314 return 0;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007315
Jeff Layton57174592018-03-18 08:37:03 -04007316 /* Make sure it's for the right inode */
7317 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
7318 return 0;
Jeff Layton57174592018-03-18 08:37:03 -04007319 }
Jeff Laytona1d617d82016-09-17 18:17:39 -04007320
Trond Myklebust86438182021-02-02 14:22:51 -05007321 return woken_wake_function(wait, mode, flags, key);
Jeff Laytona1d617d82016-09-17 18:17:39 -04007322}
7323
7324static int
7325nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7326{
Jeff Laytona1d617d82016-09-17 18:17:39 -04007327 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
7328 struct nfs_server *server = NFS_SERVER(state->inode);
7329 struct nfs_client *clp = server->nfs_client;
7330 wait_queue_head_t *q = &clp->cl_lock_waitq;
Trond Myklebust86438182021-02-02 14:22:51 -05007331 struct nfs4_lock_waiter waiter = {
7332 .inode = state->inode,
7333 .owner = { .clientid = clp->cl_clientid,
7334 .id = lsp->ls_seqid.owner_id,
7335 .s_dev = server->s_dev },
7336 };
7337 int status;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007338
7339 /* Don't bother with waitqueue if we don't expect a callback */
7340 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
7341 return nfs4_retry_setlk_simple(state, cmd, request);
7342
Trond Myklebust86438182021-02-02 14:22:51 -05007343 init_wait(&waiter.wait);
7344 waiter.wait.func = nfs4_wake_lock_waiter;
7345 add_wait_queue(q, &waiter.wait);
Jeff Laytona1d617d82016-09-17 18:17:39 -04007346
Trond Myklebust86438182021-02-02 14:22:51 -05007347 do {
Jeff Laytona1d617d82016-09-17 18:17:39 -04007348 status = nfs4_proc_setlk(state, cmd, request);
Trond Myklebust86438182021-02-02 14:22:51 -05007349 if (status != -EAGAIN || IS_SETLK(cmd))
Jeff Laytona1d617d82016-09-17 18:17:39 -04007350 break;
7351
7352 status = -ERESTARTSYS;
Yihao Wu52b042a2019-05-22 01:57:10 +08007353 freezer_do_not_count();
Trond Myklebust86438182021-02-02 14:22:51 -05007354 wait_woken(&waiter.wait, TASK_INTERRUPTIBLE,
7355 NFS4_LOCK_MAXTIMEOUT);
Yihao Wu52b042a2019-05-22 01:57:10 +08007356 freezer_count();
Trond Myklebust86438182021-02-02 14:22:51 -05007357 } while (!signalled());
7358
7359 remove_wait_queue(q, &waiter.wait);
Jeff Laytona1d617d82016-09-17 18:17:39 -04007360
Jeff Laytona1d617d82016-09-17 18:17:39 -04007361 return status;
7362}
7363#else /* !CONFIG_NFS_V4_1 */
7364static inline int
7365nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7366{
7367 return nfs4_retry_setlk_simple(state, cmd, request);
7368}
7369#endif
7370
Linus Torvalds1da177e2005-04-16 15:20:36 -07007371static int
7372nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
7373{
7374 struct nfs_open_context *ctx;
7375 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007376 int status;
7377
7378 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04007379 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007380 state = ctx->state;
7381
Trond Myklebustd9531262009-07-21 19:22:38 -04007382 if (IS_GETLK(cmd)) {
7383 if (state != NULL)
7384 return nfs4_proc_getlk(state, F_GETLK, request);
7385 return 0;
7386 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007387
7388 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
7389 return -EINVAL;
7390
Trond Myklebustd9531262009-07-21 19:22:38 -04007391 if (request->fl_type == F_UNLCK) {
7392 if (state != NULL)
7393 return nfs4_proc_unlck(state, cmd, request);
7394 return 0;
7395 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007396
Trond Myklebustd9531262009-07-21 19:22:38 -04007397 if (state == NULL)
7398 return -ENOLCK;
Jeff Layton1ea67db2016-09-17 18:17:37 -04007399
7400 if ((request->fl_flags & FL_POSIX) &&
7401 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
7402 return -ENOLCK;
7403
Benjamin Coddingtonfcfa4472017-11-10 06:27:49 -05007404 /*
7405 * Don't rely on the VFS having checked the file open mode,
7406 * since it won't do this for flock() locks.
7407 */
7408 switch (request->fl_type) {
7409 case F_RDLCK:
7410 if (!(filp->f_mode & FMODE_READ))
7411 return -EBADF;
7412 break;
7413 case F_WRLCK:
7414 if (!(filp->f_mode & FMODE_WRITE))
7415 return -EBADF;
7416 }
7417
Jeff Layton1ea67db2016-09-17 18:17:37 -04007418 status = nfs4_set_lock_state(state, request);
7419 if (status != 0)
7420 return status;
7421
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04007422 return nfs4_retry_setlk(state, cmd, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007423}
7424
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04007425int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05007426{
7427 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05007428 int err;
7429
7430 err = nfs4_set_lock_state(state, fl);
7431 if (err != 0)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04007432 return err;
Olga Kornievskaia3d7a9522020-08-20 18:52:43 -04007433 do {
7434 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
7435 if (err != -NFS4ERR_DELAY)
7436 break;
7437 ssleep(1);
7438 } while (err == -NFS4ERR_DELAY);
NeilBrowndce26302017-12-13 09:57:09 +11007439 return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05007440}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007441
Trond Myklebustcf470c32012-03-07 13:49:12 -05007442struct nfs_release_lockowner_data {
7443 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04007444 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007445 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08007446 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04007447 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007448};
7449
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007450static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
7451{
7452 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08007453 struct nfs_server *server = data->server;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05007454 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
7455 &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08007456 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04007457 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007458}
7459
7460static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
7461{
7462 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04007463 struct nfs_server *server = data->server;
7464
Trond Myklebustb7e63a12014-02-26 11:19:14 -08007465 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04007466
7467 switch (task->tk_status) {
7468 case 0:
7469 renew_lease(server, data->timestamp);
7470 break;
7471 case -NFS4ERR_STALE_CLIENTID:
7472 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08007473 nfs4_schedule_lease_recovery(server->nfs_client);
7474 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04007475 case -NFS4ERR_LEASE_MOVED:
7476 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10007477 if (nfs4_async_handle_error(task, server,
7478 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04007479 rpc_restart_call_prepare(task);
7480 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007481}
7482
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007483static void nfs4_release_lockowner_release(void *calldata)
7484{
Trond Myklebustcf470c32012-03-07 13:49:12 -05007485 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04007486 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007487 kfree(calldata);
7488}
7489
Trond Myklebust17280172012-03-11 13:11:00 -04007490static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007491 .rpc_call_prepare = nfs4_release_lockowner_prepare,
7492 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007493 .rpc_release = nfs4_release_lockowner_release,
7494};
7495
Jeff Laytonf1cdae82014-05-01 06:28:47 -04007496static void
7497nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007498{
Trond Myklebustcf470c32012-03-07 13:49:12 -05007499 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007500 struct rpc_message msg = {
7501 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
7502 };
7503
7504 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04007505 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007506
Trond Myklebustcf470c32012-03-07 13:49:12 -05007507 data = kmalloc(sizeof(*data), GFP_NOFS);
7508 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04007509 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007510 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04007511 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007512 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7513 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
7514 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007515
Trond Myklebustcf470c32012-03-07 13:49:12 -05007516 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08007517 msg.rpc_resp = &data->res;
Anna Schumakerfba83f32018-05-04 16:22:50 -04007518 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05007519 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007520}
7521
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00007522#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
7523
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007524static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Christian Braunere65ce2a2021-01-21 14:19:27 +01007525 struct user_namespace *mnt_userns,
Al Viro59301222016-05-27 10:19:30 -04007526 struct dentry *unused, struct inode *inode,
7527 const char *key, const void *buf,
7528 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007529{
Al Viro59301222016-05-27 10:19:30 -04007530 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007531}
7532
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007533static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04007534 struct dentry *unused, struct inode *inode,
7535 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007536{
Al Virob2968212016-04-10 20:48:24 -04007537 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007538}
7539
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01007540static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007541{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01007542 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007543}
7544
David Quigleyc9bccef2013-05-22 12:50:45 -04007545#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04007546
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007547static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Christian Braunere65ce2a2021-01-21 14:19:27 +01007548 struct user_namespace *mnt_userns,
Al Viro59301222016-05-27 10:19:30 -04007549 struct dentry *unused, struct inode *inode,
7550 const char *key, const void *buf,
7551 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04007552{
7553 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04007554 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04007555
7556 return -EOPNOTSUPP;
7557}
7558
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007559static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04007560 struct dentry *unused, struct inode *inode,
7561 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04007562{
7563 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04007564 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04007565 return -EOPNOTSUPP;
7566}
7567
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007568static ssize_t
7569nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04007570{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007571 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04007572
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007573 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
7574 len = security_inode_listsecurity(inode, list, list_len);
Chengguang Xu82c596e2020-06-17 09:09:39 +08007575 if (len >= 0 && list_len && len > list_len)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007576 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04007577 }
7578 return len;
7579}
7580
7581static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
7582 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04007583 .get = nfs4_xattr_get_nfs4_label,
7584 .set = nfs4_xattr_set_nfs4_label,
7585};
David Quigleyc9bccef2013-05-22 12:50:45 -04007586
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007587#else
7588
7589static ssize_t
7590nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7591{
7592 return 0;
7593}
7594
7595#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04007596
Frank van der Linden012a2112020-06-23 22:39:03 +00007597#ifdef CONFIG_NFS_V4_2
7598static int nfs4_xattr_set_nfs4_user(const struct xattr_handler *handler,
Christian Braunere65ce2a2021-01-21 14:19:27 +01007599 struct user_namespace *mnt_userns,
Frank van der Linden012a2112020-06-23 22:39:03 +00007600 struct dentry *unused, struct inode *inode,
7601 const char *key, const void *buf,
7602 size_t buflen, int flags)
7603{
7604 struct nfs_access_entry cache;
Frank van der Linden95ad37f2020-06-23 22:39:04 +00007605 int ret;
Frank van der Linden012a2112020-06-23 22:39:03 +00007606
7607 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7608 return -EOPNOTSUPP;
7609
7610 /*
7611 * There is no mapping from the MAY_* flags to the NFS_ACCESS_XA*
7612 * flags right now. Handling of xattr operations use the normal
7613 * file read/write permissions.
7614 *
7615 * Just in case the server has other ideas (which RFC 8276 allows),
7616 * do a cached access check for the XA* flags to possibly avoid
7617 * doing an RPC and getting EACCES back.
7618 */
7619 if (!nfs_access_get_cached(inode, current_cred(), &cache, true)) {
7620 if (!(cache.mask & NFS_ACCESS_XAWRITE))
7621 return -EACCES;
7622 }
7623
Frank van der Linden95ad37f2020-06-23 22:39:04 +00007624 if (buf == NULL) {
7625 ret = nfs42_proc_removexattr(inode, key);
7626 if (!ret)
7627 nfs4_xattr_cache_remove(inode, key);
7628 } else {
7629 ret = nfs42_proc_setxattr(inode, key, buf, buflen, flags);
7630 if (!ret)
7631 nfs4_xattr_cache_add(inode, key, buf, NULL, buflen);
7632 }
7633
7634 return ret;
Frank van der Linden012a2112020-06-23 22:39:03 +00007635}
7636
7637static int nfs4_xattr_get_nfs4_user(const struct xattr_handler *handler,
7638 struct dentry *unused, struct inode *inode,
7639 const char *key, void *buf, size_t buflen)
7640{
7641 struct nfs_access_entry cache;
Frank van der Linden95ad37f2020-06-23 22:39:04 +00007642 ssize_t ret;
Frank van der Linden012a2112020-06-23 22:39:03 +00007643
7644 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7645 return -EOPNOTSUPP;
7646
7647 if (!nfs_access_get_cached(inode, current_cred(), &cache, true)) {
7648 if (!(cache.mask & NFS_ACCESS_XAREAD))
7649 return -EACCES;
7650 }
7651
Trond Myklebust1f3208b2021-03-25 11:04:34 -04007652 ret = nfs_revalidate_inode(inode, NFS_INO_INVALID_CHANGE);
Frank van der Linden95ad37f2020-06-23 22:39:04 +00007653 if (ret)
7654 return ret;
7655
7656 ret = nfs4_xattr_cache_get(inode, key, buf, buflen);
7657 if (ret >= 0 || (ret < 0 && ret != -ENOENT))
7658 return ret;
7659
7660 ret = nfs42_proc_getxattr(inode, key, buf, buflen);
7661
7662 return ret;
Frank van der Linden012a2112020-06-23 22:39:03 +00007663}
7664
7665static ssize_t
7666nfs4_listxattr_nfs4_user(struct inode *inode, char *list, size_t list_len)
7667{
7668 u64 cookie;
7669 bool eof;
Frank van der Linden95ad37f2020-06-23 22:39:04 +00007670 ssize_t ret, size;
Frank van der Linden012a2112020-06-23 22:39:03 +00007671 char *buf;
7672 size_t buflen;
7673 struct nfs_access_entry cache;
7674
7675 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7676 return 0;
7677
7678 if (!nfs_access_get_cached(inode, current_cred(), &cache, true)) {
7679 if (!(cache.mask & NFS_ACCESS_XALIST))
7680 return 0;
7681 }
7682
Trond Myklebust1f3208b2021-03-25 11:04:34 -04007683 ret = nfs_revalidate_inode(inode, NFS_INO_INVALID_CHANGE);
Frank van der Linden95ad37f2020-06-23 22:39:04 +00007684 if (ret)
7685 return ret;
7686
7687 ret = nfs4_xattr_cache_list(inode, list, list_len);
7688 if (ret >= 0 || (ret < 0 && ret != -ENOENT))
7689 return ret;
7690
Frank van der Linden012a2112020-06-23 22:39:03 +00007691 cookie = 0;
7692 eof = false;
7693 buflen = list_len ? list_len : XATTR_LIST_MAX;
7694 buf = list_len ? list : NULL;
7695 size = 0;
7696
7697 while (!eof) {
7698 ret = nfs42_proc_listxattrs(inode, buf, buflen,
7699 &cookie, &eof);
7700 if (ret < 0)
7701 return ret;
7702
7703 if (list_len) {
7704 buf += ret;
7705 buflen -= ret;
7706 }
7707 size += ret;
7708 }
7709
Frank van der Linden95ad37f2020-06-23 22:39:04 +00007710 if (list_len)
7711 nfs4_xattr_cache_set_list(inode, list, size);
7712
Frank van der Linden012a2112020-06-23 22:39:03 +00007713 return size;
7714}
7715
7716#else
7717
7718static ssize_t
7719nfs4_listxattr_nfs4_user(struct inode *inode, char *list, size_t list_len)
7720{
7721 return 0;
7722}
7723#endif /* CONFIG_NFS_V4_2 */
7724
Andy Adamson533eb462011-06-13 18:25:56 -04007725/*
7726 * nfs_fhget will use either the mounted_on_fileid or the fileid
7727 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007728static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
7729{
Andy Adamson533eb462011-06-13 18:25:56 -04007730 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
7731 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
7732 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05007733 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007734 return;
7735
7736 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05007737 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007738 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
7739 fattr->nlink = 2;
7740}
7741
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007742static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7743 const struct qstr *name,
7744 struct nfs4_fs_locations *fs_locations,
7745 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04007746{
7747 struct nfs_server *server = NFS_SERVER(dir);
Chuck Leverc05cefc2017-11-05 15:45:22 -05007748 u32 bitmask[3];
Trond Myklebust683b57b2006-06-09 09:34:22 -04007749 struct nfs4_fs_locations_arg args = {
7750 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05007751 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04007752 .page = page,
7753 .bitmask = bitmask,
7754 };
Benny Halevy22958462009-04-01 09:22:02 -04007755 struct nfs4_fs_locations_res res = {
7756 .fs_locations = fs_locations,
7757 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04007758 struct rpc_message msg = {
7759 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7760 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04007761 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04007762 };
7763 int status;
7764
Harvey Harrison3110ff82008-05-02 13:42:44 -07007765 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04007766
Chuck Leverc05cefc2017-11-05 15:45:22 -05007767 bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
7768 bitmask[1] = nfs4_fattr_bitmap[1];
7769
Andy Adamson533eb462011-06-13 18:25:56 -04007770 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
7771 * is not supported */
7772 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
Chuck Leverc05cefc2017-11-05 15:45:22 -05007773 bitmask[0] &= ~FATTR4_WORD0_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04007774 else
Chuck Leverc05cefc2017-11-05 15:45:22 -05007775 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04007776
Trond Myklebustc228fd32007-01-13 02:28:11 -05007777 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04007778 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04007779 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007780 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07007781 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04007782 return status;
7783}
7784
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007785int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7786 const struct qstr *name,
7787 struct nfs4_fs_locations *fs_locations,
7788 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007789{
Trond Myklebust0688e642019-04-07 13:59:09 -04007790 struct nfs4_exception exception = {
7791 .interruptible = true,
7792 };
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007793 int err;
7794 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007795 err = _nfs4_proc_fs_locations(client, dir, name,
7796 fs_locations, page);
7797 trace_nfs4_get_fs_locations(dir, name, err);
7798 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007799 &exception);
7800 } while (exception.retry);
7801 return err;
7802}
7803
Chuck Leverb03d7352013-10-17 14:12:50 -04007804/*
7805 * This operation also signals the server that this client is
7806 * performing migration recovery. The server can stop returning
7807 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
7808 * appended to this compound to identify the client ID which is
7809 * performing recovery.
7810 */
7811static int _nfs40_proc_get_locations(struct inode *inode,
7812 struct nfs4_fs_locations *locations,
NeilBrowna52458b2018-12-03 11:30:31 +11007813 struct page *page, const struct cred *cred)
Chuck Leverb03d7352013-10-17 14:12:50 -04007814{
7815 struct nfs_server *server = NFS_SERVER(inode);
7816 struct rpc_clnt *clnt = server->client;
7817 u32 bitmask[2] = {
7818 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7819 };
7820 struct nfs4_fs_locations_arg args = {
7821 .clientid = server->nfs_client->cl_clientid,
7822 .fh = NFS_FH(inode),
7823 .page = page,
7824 .bitmask = bitmask,
7825 .migration = 1, /* skip LOOKUP */
7826 .renew = 1, /* append RENEW */
7827 };
7828 struct nfs4_fs_locations_res res = {
7829 .fs_locations = locations,
7830 .migration = 1,
7831 .renew = 1,
7832 };
7833 struct rpc_message msg = {
7834 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7835 .rpc_argp = &args,
7836 .rpc_resp = &res,
7837 .rpc_cred = cred,
7838 };
7839 unsigned long now = jiffies;
7840 int status;
7841
7842 nfs_fattr_init(&locations->fattr);
7843 locations->server = server;
7844 locations->nlocations = 0;
7845
Anna Schumakerfba83f32018-05-04 16:22:50 -04007846 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Leverb03d7352013-10-17 14:12:50 -04007847 status = nfs4_call_sync_sequence(clnt, server, &msg,
7848 &args.seq_args, &res.seq_res);
7849 if (status)
7850 return status;
7851
7852 renew_lease(server, now);
7853 return 0;
7854}
7855
7856#ifdef CONFIG_NFS_V4_1
7857
7858/*
7859 * This operation also signals the server that this client is
7860 * performing migration recovery. The server can stop asserting
7861 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
7862 * performing this operation is identified in the SEQUENCE
7863 * operation in this compound.
7864 *
7865 * When the client supports GETATTR(fs_locations_info), it can
7866 * be plumbed in here.
7867 */
7868static int _nfs41_proc_get_locations(struct inode *inode,
7869 struct nfs4_fs_locations *locations,
NeilBrowna52458b2018-12-03 11:30:31 +11007870 struct page *page, const struct cred *cred)
Chuck Leverb03d7352013-10-17 14:12:50 -04007871{
7872 struct nfs_server *server = NFS_SERVER(inode);
7873 struct rpc_clnt *clnt = server->client;
7874 u32 bitmask[2] = {
7875 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7876 };
7877 struct nfs4_fs_locations_arg args = {
7878 .fh = NFS_FH(inode),
7879 .page = page,
7880 .bitmask = bitmask,
7881 .migration = 1, /* skip LOOKUP */
7882 };
7883 struct nfs4_fs_locations_res res = {
7884 .fs_locations = locations,
7885 .migration = 1,
7886 };
7887 struct rpc_message msg = {
7888 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7889 .rpc_argp = &args,
7890 .rpc_resp = &res,
7891 .rpc_cred = cred,
7892 };
7893 int status;
7894
7895 nfs_fattr_init(&locations->fattr);
7896 locations->server = server;
7897 locations->nlocations = 0;
7898
Anna Schumakerfba83f32018-05-04 16:22:50 -04007899 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Leverb03d7352013-10-17 14:12:50 -04007900 status = nfs4_call_sync_sequence(clnt, server, &msg,
7901 &args.seq_args, &res.seq_res);
7902 if (status == NFS4_OK &&
7903 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7904 status = -NFS4ERR_LEASE_MOVED;
7905 return status;
7906}
7907
7908#endif /* CONFIG_NFS_V4_1 */
7909
7910/**
7911 * nfs4_proc_get_locations - discover locations for a migrated FSID
7912 * @inode: inode on FSID that is migrating
7913 * @locations: result of query
7914 * @page: buffer
7915 * @cred: credential to use for this operation
7916 *
7917 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
7918 * operation failed, or a negative errno if a local error occurred.
7919 *
7920 * On success, "locations" is filled in, but if the server has
7921 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
7922 * asserted.
7923 *
7924 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
7925 * from this client that require migration recovery.
7926 */
7927int nfs4_proc_get_locations(struct inode *inode,
7928 struct nfs4_fs_locations *locations,
NeilBrowna52458b2018-12-03 11:30:31 +11007929 struct page *page, const struct cred *cred)
Chuck Leverb03d7352013-10-17 14:12:50 -04007930{
7931 struct nfs_server *server = NFS_SERVER(inode);
7932 struct nfs_client *clp = server->nfs_client;
7933 const struct nfs4_mig_recovery_ops *ops =
7934 clp->cl_mvops->mig_recovery_ops;
Trond Myklebust0688e642019-04-07 13:59:09 -04007935 struct nfs4_exception exception = {
7936 .interruptible = true,
7937 };
Chuck Leverb03d7352013-10-17 14:12:50 -04007938 int status;
7939
7940 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7941 (unsigned long long)server->fsid.major,
7942 (unsigned long long)server->fsid.minor,
7943 clp->cl_hostname);
7944 nfs_display_fhandle(NFS_FH(inode), __func__);
7945
7946 do {
7947 status = ops->get_locations(inode, locations, page, cred);
7948 if (status != -NFS4ERR_DELAY)
7949 break;
7950 nfs4_handle_exception(server, status, &exception);
7951 } while (exception.retry);
7952 return status;
7953}
7954
Chuck Lever44c99932013-10-17 14:13:30 -04007955/*
7956 * This operation also signals the server that this client is
7957 * performing "lease moved" recovery. The server can stop
7958 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
7959 * is appended to this compound to identify the client ID which is
7960 * performing recovery.
7961 */
NeilBrowna52458b2018-12-03 11:30:31 +11007962static int _nfs40_proc_fsid_present(struct inode *inode, const struct cred *cred)
Chuck Lever44c99932013-10-17 14:13:30 -04007963{
7964 struct nfs_server *server = NFS_SERVER(inode);
7965 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
7966 struct rpc_clnt *clnt = server->client;
7967 struct nfs4_fsid_present_arg args = {
7968 .fh = NFS_FH(inode),
7969 .clientid = clp->cl_clientid,
7970 .renew = 1, /* append RENEW */
7971 };
7972 struct nfs4_fsid_present_res res = {
7973 .renew = 1,
7974 };
7975 struct rpc_message msg = {
7976 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7977 .rpc_argp = &args,
7978 .rpc_resp = &res,
7979 .rpc_cred = cred,
7980 };
7981 unsigned long now = jiffies;
7982 int status;
7983
7984 res.fh = nfs_alloc_fhandle();
7985 if (res.fh == NULL)
7986 return -ENOMEM;
7987
Anna Schumakerfba83f32018-05-04 16:22:50 -04007988 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Lever44c99932013-10-17 14:13:30 -04007989 status = nfs4_call_sync_sequence(clnt, server, &msg,
7990 &args.seq_args, &res.seq_res);
7991 nfs_free_fhandle(res.fh);
7992 if (status)
7993 return status;
7994
7995 do_renew_lease(clp, now);
7996 return 0;
7997}
7998
7999#ifdef CONFIG_NFS_V4_1
8000
8001/*
8002 * This operation also signals the server that this client is
8003 * performing "lease moved" recovery. The server can stop asserting
8004 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
8005 * this operation is identified in the SEQUENCE operation in this
8006 * compound.
8007 */
NeilBrowna52458b2018-12-03 11:30:31 +11008008static int _nfs41_proc_fsid_present(struct inode *inode, const struct cred *cred)
Chuck Lever44c99932013-10-17 14:13:30 -04008009{
8010 struct nfs_server *server = NFS_SERVER(inode);
8011 struct rpc_clnt *clnt = server->client;
8012 struct nfs4_fsid_present_arg args = {
8013 .fh = NFS_FH(inode),
8014 };
8015 struct nfs4_fsid_present_res res = {
8016 };
8017 struct rpc_message msg = {
8018 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
8019 .rpc_argp = &args,
8020 .rpc_resp = &res,
8021 .rpc_cred = cred,
8022 };
8023 int status;
8024
8025 res.fh = nfs_alloc_fhandle();
8026 if (res.fh == NULL)
8027 return -ENOMEM;
8028
Anna Schumakerfba83f32018-05-04 16:22:50 -04008029 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Lever44c99932013-10-17 14:13:30 -04008030 status = nfs4_call_sync_sequence(clnt, server, &msg,
8031 &args.seq_args, &res.seq_res);
8032 nfs_free_fhandle(res.fh);
8033 if (status == NFS4_OK &&
8034 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
8035 status = -NFS4ERR_LEASE_MOVED;
8036 return status;
8037}
8038
8039#endif /* CONFIG_NFS_V4_1 */
8040
8041/**
8042 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
8043 * @inode: inode on FSID to check
8044 * @cred: credential to use for this operation
8045 *
8046 * Server indicates whether the FSID is present, moved, or not
8047 * recognized. This operation is necessary to clear a LEASE_MOVED
8048 * condition for this client ID.
8049 *
8050 * Returns NFS4_OK if the FSID is present on this server,
8051 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
8052 * NFS4ERR code if some error occurred on the server, or a
8053 * negative errno if a local failure occurred.
8054 */
NeilBrowna52458b2018-12-03 11:30:31 +11008055int nfs4_proc_fsid_present(struct inode *inode, const struct cred *cred)
Chuck Lever44c99932013-10-17 14:13:30 -04008056{
8057 struct nfs_server *server = NFS_SERVER(inode);
8058 struct nfs_client *clp = server->nfs_client;
8059 const struct nfs4_mig_recovery_ops *ops =
8060 clp->cl_mvops->mig_recovery_ops;
Trond Myklebust0688e642019-04-07 13:59:09 -04008061 struct nfs4_exception exception = {
8062 .interruptible = true,
8063 };
Chuck Lever44c99932013-10-17 14:13:30 -04008064 int status;
8065
8066 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
8067 (unsigned long long)server->fsid.major,
8068 (unsigned long long)server->fsid.minor,
8069 clp->cl_hostname);
8070 nfs_display_fhandle(NFS_FH(inode), __func__);
8071
8072 do {
8073 status = ops->fsid_present(inode, cred);
8074 if (status != -NFS4ERR_DELAY)
8075 break;
8076 nfs4_handle_exception(server, status, &exception);
8077 } while (exception.retry);
8078 return status;
8079}
8080
Trond Myklebust302fad72019-02-18 13:32:38 -05008081/*
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04008082 * If 'use_integrity' is true and the state managment nfs_client
8083 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
8084 * and the machine credential as per RFC3530bis and RFC5661 Security
8085 * Considerations sections. Otherwise, just use the user cred with the
8086 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04008087 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04008088static 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 +00008089{
8090 int status;
Anna Schumaker50493362019-08-14 15:30:16 -04008091 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
8092 struct nfs_client *clp = NFS_SERVER(dir)->nfs_client;
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00008093 struct nfs4_secinfo_arg args = {
8094 .dir_fh = NFS_FH(dir),
8095 .name = name,
8096 };
8097 struct nfs4_secinfo_res res = {
8098 .flavors = flavors,
8099 };
8100 struct rpc_message msg = {
8101 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
8102 .rpc_argp = &args,
8103 .rpc_resp = &res,
8104 };
Anna Schumaker50493362019-08-14 15:30:16 -04008105 struct nfs4_call_sync_data data = {
8106 .seq_server = NFS_SERVER(dir),
8107 .seq_args = &args.seq_args,
8108 .seq_res = &res.seq_res,
8109 };
8110 struct rpc_task_setup task_setup = {
8111 .rpc_client = clnt,
8112 .rpc_message = &msg,
8113 .callback_ops = clp->cl_mvops->call_sync_ops,
8114 .callback_data = &data,
8115 .flags = RPC_TASK_NO_ROUND_ROBIN,
8116 };
NeilBrowna52458b2018-12-03 11:30:31 +11008117 const struct cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04008118
8119 if (use_integrity) {
Anna Schumaker50493362019-08-14 15:30:16 -04008120 clnt = clp->cl_rpcclient;
8121 task_setup.rpc_client = clnt;
8122
8123 cred = nfs4_get_clid_cred(clp);
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008124 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04008125 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00008126
8127 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008128
Anna Schumaker50493362019-08-14 15:30:16 -04008129 nfs4_state_protect(clp, NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
8130 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
8131 status = nfs4_call_sync_custom(&task_setup);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008132
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00008133 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04008134
NeilBrowna52458b2018-12-03 11:30:31 +11008135 put_cred(cred);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00008136 return status;
8137}
8138
Bryan Schumaker72de53e2012-04-27 13:27:40 -04008139int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
8140 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00008141{
Trond Myklebust0688e642019-04-07 13:59:09 -04008142 struct nfs4_exception exception = {
8143 .interruptible = true,
8144 };
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00008145 int err;
8146 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04008147 err = -NFS4ERR_WRONGSEC;
8148
8149 /* try to use integrity protection with machine cred */
8150 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
8151 err = _nfs4_proc_secinfo(dir, name, flavors, true);
8152
8153 /*
8154 * if unable to use integrity protection, or SECINFO with
8155 * integrity protection returns NFS4ERR_WRONGSEC (which is
8156 * disallowed by spec, but exists in deployed servers) use
8157 * the current filesystem's rpc_client and the user cred.
8158 */
8159 if (err == -NFS4ERR_WRONGSEC)
8160 err = _nfs4_proc_secinfo(dir, name, flavors, false);
8161
Trond Myklebust078ea3d2013-08-12 16:45:55 -04008162 trace_nfs4_secinfo(dir, name, err);
8163 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00008164 &exception);
8165 } while (exception.retry);
8166 return err;
8167}
8168
Andy Adamson557134a2009-04-01 09:21:53 -04008169#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04008170/*
Andy Adamson357f54d2010-12-14 10:11:57 -05008171 * Check the exchange flags returned by the server for invalid flags, having
8172 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
8173 * DS flags set.
8174 */
Olga Kornievskaia8c390762020-10-16 09:25:45 -04008175static int nfs4_check_cl_exchange_flags(u32 flags, u32 version)
Andy Adamson357f54d2010-12-14 10:11:57 -05008176{
Olga Kornievskaia8c390762020-10-16 09:25:45 -04008177 if (version >= 2 && (flags & ~EXCHGID4_2_FLAG_MASK_R))
8178 goto out_inval;
8179 else if (version < 2 && (flags & ~EXCHGID4_FLAG_MASK_R))
Andy Adamson357f54d2010-12-14 10:11:57 -05008180 goto out_inval;
8181 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
8182 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
8183 goto out_inval;
8184 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
8185 goto out_inval;
8186 return NFS_OK;
8187out_inval:
8188 return -NFS4ERR_INVAL;
8189}
8190
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04008191static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04008192nfs41_same_server_scope(struct nfs41_server_scope *a,
8193 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04008194{
Anna Schumaker49ad0142017-01-11 16:51:59 -05008195 if (a->server_scope_sz != b->server_scope_sz)
8196 return false;
8197 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04008198}
8199
Andy Adamson02a95de2016-02-05 16:08:37 -05008200static void
8201nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
8202{
Trond Myklebust5c441542019-11-13 08:34:00 +01008203 struct nfs41_bind_conn_to_session_args *args = task->tk_msg.rpc_argp;
Olga Kornievskaiadff58532020-04-24 17:45:50 -04008204 struct nfs41_bind_conn_to_session_res *res = task->tk_msg.rpc_resp;
Trond Myklebust5c441542019-11-13 08:34:00 +01008205 struct nfs_client *clp = args->client;
8206
8207 switch (task->tk_status) {
8208 case -NFS4ERR_BADSESSION:
8209 case -NFS4ERR_DEADSESSION:
8210 nfs4_schedule_session_recovery(clp->cl_session,
8211 task->tk_status);
8212 }
Olga Kornievskaiadff58532020-04-24 17:45:50 -04008213 if (args->dir == NFS4_CDFC4_FORE_OR_BOTH &&
8214 res->dir != NFS4_CDFS4_BOTH) {
8215 rpc_task_close_connection(task);
8216 if (args->retries++ < MAX_BIND_CONN_TO_SESSION_RETRIES)
8217 rpc_restart_call(task);
8218 }
Andy Adamson02a95de2016-02-05 16:08:37 -05008219}
8220
8221static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
Olga Kornievskaia1c709b72020-04-26 11:30:00 -04008222 .rpc_call_done = nfs4_bind_one_conn_to_session_done,
Andy Adamson02a95de2016-02-05 16:08:37 -05008223};
8224
Andy Adamson357f54d2010-12-14 10:11:57 -05008225/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008226 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008227 *
8228 * The 4.1 client currently uses the same TCP connection for the
8229 * fore and backchannel.
8230 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008231static
8232int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
8233 struct rpc_xprt *xprt,
8234 struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008235 const struct cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008236{
8237 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08008238 struct nfs41_bind_conn_to_session_args args = {
8239 .client = clp,
8240 .dir = NFS4_CDFC4_FORE_OR_BOTH,
Olga Kornievskaiadff58532020-04-24 17:45:50 -04008241 .retries = 0,
Trond Myklebust71a097c2015-02-18 09:27:18 -08008242 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008243 struct nfs41_bind_conn_to_session_res res;
8244 struct rpc_message msg = {
8245 .rpc_proc =
8246 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08008247 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008248 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04008249 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008250 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008251 struct rpc_task_setup task_setup_data = {
8252 .rpc_client = clnt,
8253 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05008254 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008255 .rpc_message = &msg,
8256 .flags = RPC_TASK_TIMEOUT,
8257 };
8258 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008259
Trond Myklebust71a097c2015-02-18 09:27:18 -08008260 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
8261 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
8262 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008263
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008264 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
8265 if (xprt != rcu_access_pointer(clnt->cl_xprt))
8266 args.dir = NFS4_CDFC4_FORE;
8267
8268 task = rpc_run_task(&task_setup_data);
8269 if (!IS_ERR(task)) {
8270 status = task->tk_status;
8271 rpc_put_task(task);
8272 } else
8273 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008274 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008275 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08008276 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008277 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
8278 dprintk("NFS: %s: Session ID mismatch\n", __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04008279 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008280 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08008281 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008282 dprintk("NFS: %s: Unexpected direction from server\n",
8283 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04008284 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008285 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08008286 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008287 dprintk("NFS: %s: Server returned RDMA mode = true\n",
8288 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04008289 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008290 }
8291 }
Anna Schumakerc7ae7632017-04-07 14:15:21 -04008292
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008293 return status;
8294}
8295
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008296struct rpc_bind_conn_calldata {
8297 struct nfs_client *clp;
NeilBrowna52458b2018-12-03 11:30:31 +11008298 const struct cred *cred;
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008299};
8300
8301static int
8302nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
8303 struct rpc_xprt *xprt,
8304 void *calldata)
8305{
8306 struct rpc_bind_conn_calldata *p = calldata;
8307
8308 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
8309}
8310
NeilBrowna52458b2018-12-03 11:30:31 +11008311int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, const struct cred *cred)
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008312{
8313 struct rpc_bind_conn_calldata data = {
8314 .clp = clp,
8315 .cred = cred,
8316 };
8317 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
8318 nfs4_proc_bind_conn_to_session_callback, &data);
8319}
8320
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008321/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04008322 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
8323 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04008324 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008325static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
8326 .how = SP4_MACH_CRED,
8327 .enforce.u.words = {
8328 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
8329 1 << (OP_EXCHANGE_ID - 32) |
8330 1 << (OP_CREATE_SESSION - 32) |
8331 1 << (OP_DESTROY_SESSION - 32) |
8332 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04008333 },
8334 .allow.u.words = {
8335 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05008336 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04008337 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05008338 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04008339 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008340 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008341 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05008342 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008343 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04008344 1 << (OP_FREE_STATEID - 32) |
8345 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008346 }
8347};
8348
8349/*
8350 * Select the state protection mode for client `clp' given the server results
8351 * from exchange_id in `sp'.
8352 *
8353 * Returns 0 on success, negative errno otherwise.
8354 */
8355static int nfs4_sp4_select_mode(struct nfs_client *clp,
8356 struct nfs41_state_protection *sp)
8357{
8358 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
8359 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
8360 1 << (OP_EXCHANGE_ID - 32) |
8361 1 << (OP_CREATE_SESSION - 32) |
8362 1 << (OP_DESTROY_SESSION - 32) |
8363 1 << (OP_DESTROY_CLIENTID - 32)
8364 };
Trond Myklebust937e3132017-08-01 07:32:50 -04008365 unsigned long flags = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008366 unsigned int i;
Trond Myklebust937e3132017-08-01 07:32:50 -04008367 int ret = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008368
8369 if (sp->how == SP4_MACH_CRED) {
8370 /* Print state protect result */
8371 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
8372 for (i = 0; i <= LAST_NFS4_OP; i++) {
8373 if (test_bit(i, sp->enforce.u.longs))
8374 dfprintk(MOUNT, " enforce op %d\n", i);
8375 if (test_bit(i, sp->allow.u.longs))
8376 dfprintk(MOUNT, " allow op %d\n", i);
8377 }
8378
8379 /* make sure nothing is on enforce list that isn't supported */
8380 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
8381 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
8382 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008383 ret = -EINVAL;
8384 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008385 }
8386 }
8387
8388 /*
8389 * Minimal mode - state operations are allowed to use machine
8390 * credential. Note this already happens by default, so the
8391 * client doesn't have to do anything more than the negotiation.
8392 *
8393 * NOTE: we don't care if EXCHANGE_ID is in the list -
8394 * we're already using the machine cred for exchange_id
8395 * and will never use a different cred.
8396 */
8397 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
8398 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
8399 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
8400 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
8401 dfprintk(MOUNT, "sp4_mach_cred:\n");
8402 dfprintk(MOUNT, " minimal mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008403 __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008404 } else {
8405 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008406 ret = -EINVAL;
8407 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008408 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04008409
8410 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05008411 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
8412 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04008413 test_bit(OP_LOCKU, sp->allow.u.longs)) {
8414 dfprintk(MOUNT, " cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008415 __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04008416 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008417
Andrew Elble99ade3c2015-12-02 09:39:51 -05008418 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
8419 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008420 __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
Andrew Elble99ade3c2015-12-02 09:39:51 -05008421 }
8422
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008423 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
8424 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
8425 dfprintk(MOUNT, " secinfo mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008426 __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008427 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008428
8429 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
8430 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
8431 dfprintk(MOUNT, " stateid mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008432 __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008433 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04008434
8435 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
8436 dfprintk(MOUNT, " write mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008437 __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04008438 }
8439
8440 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
8441 dfprintk(MOUNT, " commit mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008442 __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04008443 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008444 }
Trond Myklebust937e3132017-08-01 07:32:50 -04008445out:
8446 clp->cl_sp4_flags = flags;
Wei Yongjun72bf75c2018-08-02 05:42:04 +00008447 return ret;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008448}
8449
Andy Adamson8d89bd72016-09-09 09:22:18 -04008450struct nfs41_exchange_id_data {
8451 struct nfs41_exchange_id_res res;
8452 struct nfs41_exchange_id_args args;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008453};
8454
Andy Adamson8d89bd72016-09-09 09:22:18 -04008455static void nfs4_exchange_id_release(void *data)
8456{
8457 struct nfs41_exchange_id_data *cdata =
8458 (struct nfs41_exchange_id_data *)data;
8459
Olga Kornievskaia63513232017-03-13 10:36:19 -04008460 nfs_put_client(cdata->args.client);
Andy Adamson8d89bd72016-09-09 09:22:18 -04008461 kfree(cdata->res.impl_id);
8462 kfree(cdata->res.server_scope);
8463 kfree(cdata->res.server_owner);
8464 kfree(cdata);
8465}
8466
8467static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
Andy Adamson8d89bd72016-09-09 09:22:18 -04008468 .rpc_release = nfs4_exchange_id_release,
8469};
8470
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008471/*
8472 * _nfs4_proc_exchange_id()
8473 *
8474 * Wrapper for EXCHANGE_ID operation.
8475 */
Trond Myklebust9c760d12017-07-31 18:38:50 -04008476static struct rpc_task *
NeilBrowna52458b2018-12-03 11:30:31 +11008477nfs4_run_exchange_id(struct nfs_client *clp, const struct cred *cred,
Andy Adamsonad0849a2016-09-09 09:22:28 -04008478 u32 sp4_how, struct rpc_xprt *xprt)
Benny Halevy99fe60d2009-04-01 09:22:29 -04008479{
Benny Halevy99fe60d2009-04-01 09:22:29 -04008480 struct rpc_message msg = {
8481 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
Benny Halevy99fe60d2009-04-01 09:22:29 -04008482 .rpc_cred = cred,
8483 };
Andy Adamson8d89bd72016-09-09 09:22:18 -04008484 struct rpc_task_setup task_setup_data = {
8485 .rpc_client = clp->cl_rpcclient,
8486 .callback_ops = &nfs4_exchange_id_call_ops,
8487 .rpc_message = &msg,
NeilBrown5a0c2572019-05-30 10:41:28 +10008488 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
Andy Adamson8d89bd72016-09-09 09:22:18 -04008489 };
8490 struct nfs41_exchange_id_data *calldata;
Anna Schumakere917f0d2017-04-07 14:15:22 -04008491 int status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008492
Elena Reshetova212bf412017-10-20 12:53:38 +03008493 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust9c760d12017-07-31 18:38:50 -04008494 return ERR_PTR(-EIO);
Andy Adamson8d89bd72016-09-09 09:22:18 -04008495
Trond Myklebust9c760d12017-07-31 18:38:50 -04008496 status = -ENOMEM;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008497 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust9c760d12017-07-31 18:38:50 -04008498 if (!calldata)
8499 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04008500
Trond Myklebustfd405592017-08-01 16:02:47 -04008501 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04008502
8503 status = nfs4_init_uniform_client_string(clp);
8504 if (status)
Andy Adamson8d89bd72016-09-09 09:22:18 -04008505 goto out_calldata;
Jeff Layton3a6bb732015-06-09 19:43:57 -04008506
Andy Adamson8d89bd72016-09-09 09:22:18 -04008507 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
8508 GFP_NOFS);
8509 status = -ENOMEM;
8510 if (unlikely(calldata->res.server_owner == NULL))
8511 goto out_calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04008512
Andy Adamson8d89bd72016-09-09 09:22:18 -04008513 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04008514 GFP_NOFS);
Andy Adamson8d89bd72016-09-09 09:22:18 -04008515 if (unlikely(calldata->res.server_scope == NULL))
Chuck Leveracdeb692012-05-21 22:46:16 -04008516 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04008517
Andy Adamson8d89bd72016-09-09 09:22:18 -04008518 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
8519 if (unlikely(calldata->res.impl_id == NULL))
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05008520 goto out_server_scope;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05008521
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008522 switch (sp4_how) {
8523 case SP4_NONE:
Andy Adamson8d89bd72016-09-09 09:22:18 -04008524 calldata->args.state_protect.how = SP4_NONE;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008525 break;
8526
8527 case SP4_MACH_CRED:
Andy Adamson8d89bd72016-09-09 09:22:18 -04008528 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008529 break;
8530
8531 default:
8532 /* unsupported! */
8533 WARN_ON_ONCE(1);
8534 status = -EINVAL;
Kinglong Mee6b559702015-07-01 11:54:53 +08008535 goto out_impl_id;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008536 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04008537 if (xprt) {
Andy Adamsonad0849a2016-09-09 09:22:28 -04008538 task_setup_data.rpc_xprt = xprt;
Trond Myklebustd9cb7332017-08-01 16:02:48 -04008539 task_setup_data.flags |= RPC_TASK_SOFTCONN;
Trond Myklebustfd405592017-08-01 16:02:47 -04008540 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
8541 sizeof(calldata->args.verifier.data));
Andy Adamsonad0849a2016-09-09 09:22:28 -04008542 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04008543 calldata->args.client = clp;
Trond Myklebustbfab2812017-08-01 08:17:34 -04008544 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
8545 EXCHGID4_FLAG_BIND_PRINC_STATEID;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008546#ifdef CONFIG_NFS_V4_1_MIGRATION
Trond Myklebustbfab2812017-08-01 08:17:34 -04008547 calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008548#endif
8549 msg.rpc_argp = &calldata->args;
8550 msg.rpc_resp = &calldata->res;
8551 task_setup_data.callback_data = calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04008552
Trond Myklebust9c760d12017-07-31 18:38:50 -04008553 return rpc_run_task(&task_setup_data);
Andy Adamson8d89bd72016-09-09 09:22:18 -04008554
8555out_impl_id:
8556 kfree(calldata->res.impl_id);
8557out_server_scope:
8558 kfree(calldata->res.server_scope);
8559out_server_owner:
8560 kfree(calldata->res.server_owner);
8561out_calldata:
8562 kfree(calldata);
Trond Myklebust9c760d12017-07-31 18:38:50 -04008563out:
Olga Kornievskaia63513232017-03-13 10:36:19 -04008564 nfs_put_client(clp);
Trond Myklebust9c760d12017-07-31 18:38:50 -04008565 return ERR_PTR(status);
8566}
8567
8568/*
8569 * _nfs4_proc_exchange_id()
8570 *
8571 * Wrapper for EXCHANGE_ID operation.
8572 */
NeilBrowna52458b2018-12-03 11:30:31 +11008573static int _nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred,
Trond Myklebust9c760d12017-07-31 18:38:50 -04008574 u32 sp4_how)
8575{
8576 struct rpc_task *task;
8577 struct nfs41_exchange_id_args *argp;
8578 struct nfs41_exchange_id_res *resp;
Robert Milkowski7dc29932020-01-30 09:43:25 +00008579 unsigned long now = jiffies;
Trond Myklebust9c760d12017-07-31 18:38:50 -04008580 int status;
8581
8582 task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
8583 if (IS_ERR(task))
8584 return PTR_ERR(task);
8585
8586 argp = task->tk_msg.rpc_argp;
8587 resp = task->tk_msg.rpc_resp;
8588 status = task->tk_status;
8589 if (status != 0)
8590 goto out;
8591
Olga Kornievskaia8c390762020-10-16 09:25:45 -04008592 status = nfs4_check_cl_exchange_flags(resp->flags,
8593 clp->cl_mvops->minor_version);
Trond Myklebust9c760d12017-07-31 18:38:50 -04008594 if (status != 0)
8595 goto out;
8596
8597 status = nfs4_sp4_select_mode(clp, &resp->state_protect);
8598 if (status != 0)
8599 goto out;
8600
Robert Milkowski7dc29932020-01-30 09:43:25 +00008601 do_renew_lease(clp, now);
8602
Trond Myklebust9c760d12017-07-31 18:38:50 -04008603 clp->cl_clientid = resp->clientid;
8604 clp->cl_exchange_flags = resp->flags;
8605 clp->cl_seqid = resp->seqid;
8606 /* Client ID is not confirmed */
8607 if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
8608 clear_bit(NFS4_SESSION_ESTABLISHED,
8609 &clp->cl_session->session_state);
8610
8611 if (clp->cl_serverscope != NULL &&
8612 !nfs41_same_server_scope(clp->cl_serverscope,
8613 resp->server_scope)) {
8614 dprintk("%s: server_scope mismatch detected\n",
8615 __func__);
8616 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
8617 }
8618
8619 swap(clp->cl_serverowner, resp->server_owner);
8620 swap(clp->cl_serverscope, resp->server_scope);
8621 swap(clp->cl_implid, resp->impl_id);
8622
8623 /* Save the EXCHANGE_ID verifier session trunk tests */
8624 memcpy(clp->cl_confirm.data, argp->verifier.data,
8625 sizeof(clp->cl_confirm.data));
8626out:
8627 trace_nfs4_exchange_id(clp, status);
8628 rpc_put_task(task);
8629 return status;
Benny Halevy99fe60d2009-04-01 09:22:29 -04008630}
8631
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008632/*
8633 * nfs4_proc_exchange_id()
8634 *
8635 * Returns zero, a negative errno, or a negative NFS4ERR status code.
8636 *
8637 * Since the clientid has expired, all compounds using sessions
8638 * associated with the stale clientid will be returning
8639 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
8640 * be in some phase of session reset.
8641 *
8642 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
8643 */
NeilBrowna52458b2018-12-03 11:30:31 +11008644int nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008645{
8646 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
8647 int status;
8648
8649 /* try SP4_MACH_CRED if krb5i/p */
8650 if (authflavor == RPC_AUTH_GSS_KRB5I ||
8651 authflavor == RPC_AUTH_GSS_KRB5P) {
Trond Myklebust9c760d12017-07-31 18:38:50 -04008652 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008653 if (!status)
8654 return 0;
8655 }
8656
8657 /* try SP4_NONE */
Trond Myklebust9c760d12017-07-31 18:38:50 -04008658 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008659}
8660
Andy Adamson04fa2c62016-09-09 09:22:29 -04008661/**
8662 * nfs4_test_session_trunk
8663 *
8664 * This is an add_xprt_test() test function called from
8665 * rpc_clnt_setup_test_and_add_xprt.
8666 *
8667 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
8668 * and is dereferrenced in nfs4_exchange_id_release
8669 *
8670 * Upon success, add the new transport to the rpc_clnt
8671 *
8672 * @clnt: struct rpc_clnt to get new transport
8673 * @xprt: the rpc_xprt to test
8674 * @data: call data for _nfs4_proc_exchange_id.
8675 */
Santosh kumar pradhan10e037d2018-12-19 12:29:57 +05308676void nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
Andy Adamson04fa2c62016-09-09 09:22:29 -04008677 void *data)
8678{
8679 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
Trond Myklebust9c760d12017-07-31 18:38:50 -04008680 struct rpc_task *task;
8681 int status;
8682
Andy Adamson04fa2c62016-09-09 09:22:29 -04008683 u32 sp4_how;
8684
8685 dprintk("--> %s try %s\n", __func__,
8686 xprt->address_strings[RPC_DISPLAY_ADDR]);
8687
8688 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
8689
8690 /* Test connection for session trunking. Async exchange_id call */
Trond Myklebust9c760d12017-07-31 18:38:50 -04008691 task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
8692 if (IS_ERR(task))
Santosh kumar pradhan10e037d2018-12-19 12:29:57 +05308693 return;
Trond Myklebust9c760d12017-07-31 18:38:50 -04008694
8695 status = task->tk_status;
8696 if (status == 0)
8697 status = nfs4_detect_session_trunking(adata->clp,
8698 task->tk_msg.rpc_resp, xprt);
8699
Santosh kumar pradhan10e037d2018-12-19 12:29:57 +05308700 if (status == 0)
8701 rpc_clnt_xprt_switch_add_xprt(clnt, xprt);
8702
Trond Myklebust9c760d12017-07-31 18:38:50 -04008703 rpc_put_task(task);
Andy Adamson04fa2c62016-09-09 09:22:29 -04008704}
8705EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
8706
Trond Myklebust66245532012-05-25 17:18:09 -04008707static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008708 const struct cred *cred)
Trond Myklebust66245532012-05-25 17:18:09 -04008709{
8710 struct rpc_message msg = {
8711 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
8712 .rpc_argp = clp,
8713 .rpc_cred = cred,
8714 };
8715 int status;
8716
NeilBrown5a0c2572019-05-30 10:41:28 +10008717 status = rpc_call_sync(clp->cl_rpcclient, &msg,
8718 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008719 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04008720 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04008721 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04008722 "DESTROY_CLIENTID.", status, clp->cl_hostname);
8723 return status;
8724}
8725
8726static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008727 const struct cred *cred)
Trond Myklebust66245532012-05-25 17:18:09 -04008728{
8729 unsigned int loop;
8730 int ret;
8731
8732 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
8733 ret = _nfs4_proc_destroy_clientid(clp, cred);
8734 switch (ret) {
8735 case -NFS4ERR_DELAY:
8736 case -NFS4ERR_CLIENTID_BUSY:
8737 ssleep(1);
8738 break;
8739 default:
8740 return ret;
8741 }
8742 }
8743 return 0;
8744}
8745
8746int nfs4_destroy_clientid(struct nfs_client *clp)
8747{
NeilBrowna52458b2018-12-03 11:30:31 +11008748 const struct cred *cred;
Trond Myklebust66245532012-05-25 17:18:09 -04008749 int ret = 0;
8750
8751 if (clp->cl_mvops->minor_version < 1)
8752 goto out;
8753 if (clp->cl_exchange_flags == 0)
8754 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04008755 if (clp->cl_preserve_clid)
8756 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04008757 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04008758 ret = nfs4_proc_destroy_clientid(clp, cred);
NeilBrowna52458b2018-12-03 11:30:31 +11008759 put_cred(cred);
Trond Myklebust66245532012-05-25 17:18:09 -04008760 switch (ret) {
8761 case 0:
8762 case -NFS4ERR_STALE_CLIENTID:
8763 clp->cl_exchange_flags = 0;
8764 }
8765out:
8766 return ret;
8767}
8768
Donald Buczek0efb01b2019-07-07 21:26:08 +02008769#endif /* CONFIG_NFS_V4_1 */
8770
Andy Adamson2050f0c2009-04-01 09:22:30 -04008771struct nfs4_get_lease_time_data {
8772 struct nfs4_get_lease_time_args *args;
8773 struct nfs4_get_lease_time_res *res;
8774 struct nfs_client *clp;
8775};
8776
8777static void nfs4_get_lease_time_prepare(struct rpc_task *task,
8778 void *calldata)
8779{
Andy Adamson2050f0c2009-04-01 09:22:30 -04008780 struct nfs4_get_lease_time_data *data =
8781 (struct nfs4_get_lease_time_data *)calldata;
8782
8783 dprintk("--> %s\n", __func__);
8784 /* just setup sequence, do not trigger session recovery
8785 since we're invoked within one */
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008786 nfs4_setup_sequence(data->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008787 &data->args->la_seq_args,
8788 &data->res->lr_seq_res,
8789 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008790 dprintk("<-- %s\n", __func__);
8791}
8792
8793/*
8794 * Called from nfs4_state_manager thread for session setup, so don't recover
8795 * from sequence operation or clientid errors.
8796 */
8797static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
8798{
8799 struct nfs4_get_lease_time_data *data =
8800 (struct nfs4_get_lease_time_data *)calldata;
8801
8802 dprintk("--> %s\n", __func__);
Donald Buczek0efb01b2019-07-07 21:26:08 +02008803 if (!nfs4_sequence_done(task, &data->res->lr_seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04008804 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04008805 switch (task->tk_status) {
8806 case -NFS4ERR_DELAY:
8807 case -NFS4ERR_GRACE:
8808 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
8809 rpc_delay(task, NFS4_POLL_RETRY_MIN);
8810 task->tk_status = 0;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05008811 fallthrough;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008812 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008813 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008814 return;
8815 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04008816 dprintk("<-- %s\n", __func__);
8817}
8818
Trond Myklebust17280172012-03-11 13:11:00 -04008819static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04008820 .rpc_call_prepare = nfs4_get_lease_time_prepare,
8821 .rpc_call_done = nfs4_get_lease_time_done,
8822};
8823
8824int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
8825{
Andy Adamson2050f0c2009-04-01 09:22:30 -04008826 struct nfs4_get_lease_time_args args;
8827 struct nfs4_get_lease_time_res res = {
8828 .lr_fsinfo = fsinfo,
8829 };
8830 struct nfs4_get_lease_time_data data = {
8831 .args = &args,
8832 .res = &res,
8833 .clp = clp,
8834 };
8835 struct rpc_message msg = {
8836 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
8837 .rpc_argp = &args,
8838 .rpc_resp = &res,
8839 };
8840 struct rpc_task_setup task_setup = {
8841 .rpc_client = clp->cl_rpcclient,
8842 .rpc_message = &msg,
8843 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008844 .callback_data = &data,
8845 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04008846 };
Andy Adamson2050f0c2009-04-01 09:22:30 -04008847
Anna Schumakerfba83f32018-05-04 16:22:50 -04008848 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0, 1);
Anna Schumakerf836b272019-08-19 10:18:28 -04008849 return nfs4_call_sync_custom(&task_setup);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008850}
8851
Donald Buczek0efb01b2019-07-07 21:26:08 +02008852#ifdef CONFIG_NFS_V4_1
8853
Andy Adamsonfc931582009-04-01 09:22:31 -04008854/*
8855 * Initialize the values to be used by the client in CREATE_SESSION
8856 * If nfs4_init_session set the fore channel request and response sizes,
8857 * use them.
8858 *
8859 * Set the back channel max_resp_sz_cached to zero to force the client to
8860 * always set csa_cachethis to FALSE because the current implementation
8861 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
8862 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04008863static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
8864 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04008865{
Andy Adamson18aad3d2013-06-26 12:21:49 -04008866 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04008867 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Trond Myklebust7402a4f2019-07-16 13:51:29 -04008868 unsigned int max_bc_slots = rpc_num_bc_slots(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04008869
Andy Adamson18aad3d2013-06-26 12:21:49 -04008870 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
8871 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
8872
Andy Adamsonfc931582009-04-01 09:22:31 -04008873 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04008874 args->fc_attrs.max_rqst_sz = max_rqst_sz;
8875 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04008876 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05008877 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04008878
8879 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05008880 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04008881 __func__,
8882 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05008883 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04008884
8885 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04008886 args->bc_attrs.max_rqst_sz = max_bc_payload;
8887 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04008888 args->bc_attrs.max_resp_sz_cached = 0;
8889 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust62421cd2018-08-11 11:52:39 -04008890 args->bc_attrs.max_reqs = max_t(unsigned short, max_session_cb_slots, 1);
Trond Myklebust7402a4f2019-07-16 13:51:29 -04008891 if (args->bc_attrs.max_reqs > max_bc_slots)
8892 args->bc_attrs.max_reqs = max_bc_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04008893
8894 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
8895 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
8896 __func__,
8897 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
8898 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
8899 args->bc_attrs.max_reqs);
8900}
8901
Trond Myklebust79969dd2015-02-18 11:30:18 -08008902static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
8903 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008904{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008905 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008906 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008907
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008908 if (rcvd->max_resp_sz > sent->max_resp_sz)
8909 return -EINVAL;
8910 /*
8911 * Our requested max_ops is the minimum we need; we're not
8912 * prepared to break up compounds into smaller pieces than that.
8913 * So, no point even trying to continue if the server won't
8914 * cooperate:
8915 */
8916 if (rcvd->max_ops < sent->max_ops)
8917 return -EINVAL;
8918 if (rcvd->max_reqs == 0)
8919 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04008920 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
8921 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008922 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04008923}
8924
Trond Myklebust79969dd2015-02-18 11:30:18 -08008925static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
8926 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008927{
8928 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008929 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04008930
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008931 if (!(res->flags & SESSION4_BACK_CHAN))
8932 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008933 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
8934 return -EINVAL;
8935 if (rcvd->max_resp_sz < sent->max_resp_sz)
8936 return -EINVAL;
8937 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
8938 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008939 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008940 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008941 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008942 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008943out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008944 return 0;
8945}
Andy Adamson8d353012009-04-01 09:22:32 -04008946
Andy Adamson8d353012009-04-01 09:22:32 -04008947static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08008948 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008949{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008950 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04008951
Trond Myklebust79969dd2015-02-18 11:30:18 -08008952 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008953 if (ret)
8954 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008955 return nfs4_verify_back_channel_attrs(args, res);
8956}
8957
8958static void nfs4_update_session(struct nfs4_session *session,
8959 struct nfs41_create_session_res *res)
8960{
8961 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05008962 /* Mark client id and session as being confirmed */
8963 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
8964 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08008965 session->flags = res->flags;
8966 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008967 if (res->flags & SESSION4_BACK_CHAN)
8968 memcpy(&session->bc_attrs, &res->bc_attrs,
8969 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04008970}
8971
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008972static int _nfs4_proc_create_session(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008973 const struct cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008974{
8975 struct nfs4_session *session = clp->cl_session;
8976 struct nfs41_create_session_args args = {
8977 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08008978 .clientid = clp->cl_clientid,
8979 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04008980 .cb_program = NFS4_CALLBACK,
8981 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08008982 struct nfs41_create_session_res res;
8983
Andy Adamsonfc931582009-04-01 09:22:31 -04008984 struct rpc_message msg = {
8985 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
8986 .rpc_argp = &args,
8987 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008988 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04008989 };
8990 int status;
8991
Chuck Lever6b26cc82016-05-02 14:40:40 -04008992 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04008993 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04008994
NeilBrown5a0c2572019-05-30 10:41:28 +10008995 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
8996 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008997 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008998
Trond Myklebustb519d402016-09-11 14:50:01 -04008999 switch (status) {
9000 case -NFS4ERR_STALE_CLIENTID:
9001 case -NFS4ERR_DELAY:
9002 case -ETIMEDOUT:
9003 case -EACCES:
9004 case -EAGAIN:
9005 goto out;
zhengbin8b98a532019-12-19 18:34:47 +08009006 }
Trond Myklebustb519d402016-09-11 14:50:01 -04009007
9008 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05009009 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04009010 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08009011 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04009012 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08009013 if (status)
9014 goto out;
9015 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04009016 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08009017out:
Andy Adamsonfc931582009-04-01 09:22:31 -04009018 return status;
9019}
9020
9021/*
9022 * Issues a CREATE_SESSION operation to the server.
9023 * It is the responsibility of the caller to verify the session is
9024 * expired before calling this routine.
9025 */
NeilBrowna52458b2018-12-03 11:30:31 +11009026int nfs4_proc_create_session(struct nfs_client *clp, const struct cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04009027{
9028 int status;
9029 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04009030 struct nfs4_session *session = clp->cl_session;
9031
9032 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
9033
Trond Myklebust848f5bd2012-05-25 17:51:23 -04009034 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04009035 if (status)
9036 goto out;
9037
Andy Adamsonaacd5532011-11-09 13:58:21 -05009038 /* Init or reset the session slot tables */
9039 status = nfs4_setup_session_slot_tables(session);
9040 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04009041 if (status)
9042 goto out;
9043
9044 ptr = (unsigned *)&session->sess_id.data[0];
9045 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
9046 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04009047out:
9048 dprintk("<-- %s\n", __func__);
9049 return status;
9050}
9051
Andy Adamson0f3e66c2009-04-01 09:22:34 -04009052/*
9053 * Issue the over-the-wire RPC DESTROY_SESSION.
9054 * The caller must serialize access to this routine.
9055 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04009056int nfs4_proc_destroy_session(struct nfs4_session *session,
NeilBrowna52458b2018-12-03 11:30:31 +11009057 const struct cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04009058{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04009059 struct rpc_message msg = {
9060 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
9061 .rpc_argp = session,
9062 .rpc_cred = cred,
9063 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04009064 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04009065
9066 dprintk("--> nfs4_proc_destroy_session\n");
9067
9068 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05009069 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
9070 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04009071
NeilBrown5a0c2572019-05-30 10:41:28 +10009072 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
9073 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04009074 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04009075
9076 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04009077 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04009078 "Session has been destroyed regardless...\n", status);
9079
9080 dprintk("<-- nfs4_proc_destroy_session\n");
9081 return status;
9082}
9083
Trond Myklebust7b38c362012-05-23 13:23:31 -04009084/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009085 * Renew the cl_session lease.
9086 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04009087struct nfs4_sequence_data {
9088 struct nfs_client *clp;
9089 struct nfs4_sequence_args args;
9090 struct nfs4_sequence_res res;
9091};
9092
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08009093static void nfs41_sequence_release(void *data)
9094{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04009095 struct nfs4_sequence_data *calldata = data;
9096 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08009097
Elena Reshetova212bf412017-10-20 12:53:38 +03009098 if (refcount_read(&clp->cl_count) > 1)
Alexandros Batsakis71358402010-02-05 03:45:05 -08009099 nfs4_schedule_state_renewal(clp);
9100 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04009101 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08009102}
9103
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009104static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
9105{
9106 switch(task->tk_status) {
9107 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009108 rpc_delay(task, NFS4_POLL_RETRY_MAX);
9109 return -EAGAIN;
9110 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05009111 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009112 }
9113 return 0;
9114}
9115
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08009116static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009117{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04009118 struct nfs4_sequence_data *calldata = data;
9119 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009120
Trond Myklebust14516c32010-07-31 14:29:06 -04009121 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
9122 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009123
Trond Myklebustc6d01c62013-08-09 11:51:26 -04009124 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009125 if (task->tk_status < 0) {
9126 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Elena Reshetova212bf412017-10-20 12:53:38 +03009127 if (refcount_read(&clp->cl_count) == 1)
Alexandros Batsakis71358402010-02-05 03:45:05 -08009128 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009129
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009130 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
9131 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009132 return;
9133 }
9134 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009135 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08009136out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009137 dprintk("<-- %s\n", __func__);
9138}
9139
9140static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
9141{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04009142 struct nfs4_sequence_data *calldata = data;
9143 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009144 struct nfs4_sequence_args *args;
9145 struct nfs4_sequence_res *res;
9146
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009147 args = task->tk_msg.rpc_argp;
9148 res = task->tk_msg.rpc_resp;
9149
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009150 nfs4_setup_sequence(clp, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009151}
9152
9153static const struct rpc_call_ops nfs41_sequence_ops = {
9154 .rpc_call_done = nfs41_sequence_call_done,
9155 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08009156 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009157};
9158
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009159static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11009160 const struct cred *cred,
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009161 struct nfs4_slot *slot,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009162 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009163{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04009164 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009165 struct rpc_message msg = {
9166 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
9167 .rpc_cred = cred,
9168 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009169 struct rpc_task_setup task_setup_data = {
9170 .rpc_client = clp->cl_rpcclient,
9171 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009172 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04009173 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009174 };
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009175 struct rpc_task *ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009176
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009177 ret = ERR_PTR(-EIO);
Elena Reshetova212bf412017-10-20 12:53:38 +03009178 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009179 goto out_err;
9180
9181 ret = ERR_PTR(-ENOMEM);
Benny Halevydfb4f3092010-09-24 09:17:01 -04009182 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009183 if (calldata == NULL)
9184 goto out_put_clp;
Anna Schumakerfba83f32018-05-04 16:22:50 -04009185 nfs4_init_sequence(&calldata->args, &calldata->res, 0, is_privileged);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009186 nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04009187 msg.rpc_argp = &calldata->args;
9188 msg.rpc_resp = &calldata->res;
9189 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009190 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009191
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009192 ret = rpc_run_task(&task_setup_data);
9193 if (IS_ERR(ret))
9194 goto out_err;
9195 return ret;
9196out_put_clp:
9197 nfs_put_client(clp);
9198out_err:
9199 nfs41_release_slot(slot);
9200 return ret;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009201}
9202
NeilBrowna52458b2018-12-03 11:30:31 +11009203static int nfs41_proc_async_sequence(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009204{
9205 struct rpc_task *task;
9206 int ret = 0;
9207
Trond Myklebust2f60ea62011-08-24 15:07:37 -04009208 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04009209 return -EAGAIN;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009210 task = _nfs41_proc_sequence(clp, cred, NULL, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009211 if (IS_ERR(task))
9212 ret = PTR_ERR(task);
9213 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08009214 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009215 dprintk("<-- %s status=%d\n", __func__, ret);
9216 return ret;
9217}
9218
NeilBrowna52458b2018-12-03 11:30:31 +11009219static int nfs4_proc_sequence(struct nfs_client *clp, const struct cred *cred)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009220{
9221 struct rpc_task *task;
9222 int ret;
9223
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009224 task = _nfs41_proc_sequence(clp, cred, NULL, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009225 if (IS_ERR(task)) {
9226 ret = PTR_ERR(task);
9227 goto out;
9228 }
9229 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04009230 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009231 ret = task->tk_status;
9232 rpc_put_task(task);
9233out:
9234 dprintk("<-- %s status=%d\n", __func__, ret);
9235 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009236}
9237
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009238struct nfs4_reclaim_complete_data {
9239 struct nfs_client *clp;
9240 struct nfs41_reclaim_complete_args arg;
9241 struct nfs41_reclaim_complete_res res;
9242};
9243
9244static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
9245{
9246 struct nfs4_reclaim_complete_data *calldata = data;
9247
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009248 nfs4_setup_sequence(calldata->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04009249 &calldata->arg.seq_args,
9250 &calldata->res.seq_res,
9251 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009252}
9253
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009254static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
9255{
9256 switch(task->tk_status) {
9257 case 0:
Jeff Layton57174592018-03-18 08:37:03 -04009258 wake_up_all(&clp->cl_lock_waitq);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05009259 fallthrough;
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009260 case -NFS4ERR_COMPLETE_ALREADY:
9261 case -NFS4ERR_WRONG_CRED: /* What to do here? */
9262 break;
9263 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009264 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05009265 fallthrough;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04009266 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009267 return -EAGAIN;
Trond Myklebust0048fdd2017-05-04 13:44:04 -04009268 case -NFS4ERR_BADSESSION:
9269 case -NFS4ERR_DEADSESSION:
9270 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
Trond Myklebust0048fdd2017-05-04 13:44:04 -04009271 break;
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009272 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05009273 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009274 }
9275 return 0;
9276}
9277
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009278static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
9279{
9280 struct nfs4_reclaim_complete_data *calldata = data;
9281 struct nfs_client *clp = calldata->clp;
9282 struct nfs4_sequence_res *res = &calldata->res.seq_res;
9283
9284 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04009285 if (!nfs41_sequence_done(task, res))
9286 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009287
Trond Myklebustc6d01c62013-08-09 11:51:26 -04009288 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009289 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
9290 rpc_restart_call_prepare(task);
9291 return;
9292 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009293 dprintk("<-- %s\n", __func__);
9294}
9295
9296static void nfs4_free_reclaim_complete_data(void *data)
9297{
9298 struct nfs4_reclaim_complete_data *calldata = data;
9299
9300 kfree(calldata);
9301}
9302
9303static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
9304 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
9305 .rpc_call_done = nfs4_reclaim_complete_done,
9306 .rpc_release = nfs4_free_reclaim_complete_data,
9307};
9308
9309/*
9310 * Issue a global reclaim complete.
9311 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04009312static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11009313 const struct cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009314{
9315 struct nfs4_reclaim_complete_data *calldata;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009316 struct rpc_message msg = {
9317 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04009318 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009319 };
9320 struct rpc_task_setup task_setup_data = {
9321 .rpc_client = clp->cl_rpcclient,
9322 .rpc_message = &msg,
9323 .callback_ops = &nfs4_reclaim_complete_call_ops,
Anna Schumaker4c952e32019-08-14 15:46:48 -04009324 .flags = RPC_TASK_NO_ROUND_ROBIN,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009325 };
9326 int status = -ENOMEM;
9327
9328 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04009329 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009330 if (calldata == NULL)
9331 goto out;
9332 calldata->clp = clp;
9333 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009334
Anna Schumakerfba83f32018-05-04 16:22:50 -04009335 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0, 1);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009336 msg.rpc_argp = &calldata->arg;
9337 msg.rpc_resp = &calldata->res;
9338 task_setup_data.callback_data = calldata;
Anna Schumaker4c952e32019-08-14 15:46:48 -04009339 status = nfs4_call_sync_custom(&task_setup_data);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009340out:
9341 dprintk("<-- %s status=%d\n", __func__, status);
9342 return status;
9343}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009344
9345static void
9346nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
9347{
9348 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00009349 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009350
9351 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009352 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
Jeff Layton183d9e72016-05-17 12:28:47 -04009353 &lgp->res.seq_res, task);
9354 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009355}
9356
9357static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
9358{
9359 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04009360
9361 dprintk("--> %s\n", __func__);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04009362 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04009363 dprintk("<-- %s\n", __func__);
9364}
9365
9366static int
9367nfs4_layoutget_handle_exception(struct rpc_task *task,
9368 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
9369{
Trond Myklebustee314c22012-10-01 17:25:48 -07009370 struct inode *inode = lgp->args.inode;
9371 struct nfs_server *server = NFS_SERVER(inode);
9372 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04009373 int nfs4err = task->tk_status;
9374 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04009375 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009376
Boaz Harroshed7e5422014-01-22 20:34:54 +02009377 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009378
Trond Myklebust2dbf8df2018-06-15 15:58:45 -04009379 nfs4_sequence_free_slot(&lgp->res.seq_res);
9380
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04009381 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009382 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07009383 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08009384
9385 /*
9386 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
9387 * on the file. set tk_status to -ENODATA to tell upper layer to
9388 * retry go inband.
9389 */
9390 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04009391 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08009392 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02009393 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07009394 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
9395 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
9396 */
9397 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04009398 status = -EOVERFLOW;
9399 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07009400 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02009401 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07009402 * (or clients) writing to the same RAID stripe except when
9403 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04009404 *
9405 * Treat it like we would RECALLCONFLICT -- we retry for a little
9406 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02009407 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009408 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04009409 if (lgp->args.minlength == 0) {
9410 status = -EOVERFLOW;
9411 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02009412 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04009413 status = -EBUSY;
9414 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04009415 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04009416 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04009417 break;
Trond Myklebust26f47442016-09-22 13:39:10 -04009418 case -NFS4ERR_DELEG_REVOKED:
9419 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustee314c22012-10-01 17:25:48 -07009420 case -NFS4ERR_EXPIRED:
9421 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04009422 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07009423 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04009424 lo = NFS_I(inode)->layout;
9425 /* If the open stateid was bad, then recover it. */
9426 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
Trond Myklebuste8fa33a2017-10-04 13:49:12 -04009427 !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07009428 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04009429 exception->state = lgp->args.ctx->state;
Trond Myklebust26f47442016-09-22 13:39:10 -04009430 exception->stateid = &lgp->args.stateid;
Trond Myklebust2259f962015-09-20 13:30:30 -04009431 break;
9432 }
Trond Myklebustee314c22012-10-01 17:25:48 -07009433
Trond Myklebustf7db0b22016-07-14 15:14:02 -04009434 /*
9435 * Mark the bad layout state as invalid, then retry
9436 */
Trond Myklebust668f4552016-07-24 17:08:59 -04009437 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04009438 spin_unlock(&inode->i_lock);
Trond Myklebust1f18b822017-04-29 10:10:17 -04009439 nfs_commit_inode(inode, 0);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04009440 pnfs_free_lseg_list(&head);
9441 status = -EAGAIN;
9442 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009443 }
Jeff Layton183d9e72016-05-17 12:28:47 -04009444
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04009445 err = nfs4_handle_exception(server, nfs4err, exception);
9446 if (!status) {
9447 if (exception->retry)
9448 status = -EAGAIN;
9449 else
9450 status = err;
9451 }
Trond Myklebustee314c22012-10-01 17:25:48 -07009452out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009453 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04009454 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009455}
9456
Fred Isamandacb4522016-09-19 17:47:09 -04009457size_t max_response_pages(struct nfs_server *server)
Idan Kedar85541162012-08-02 11:47:10 +03009458{
9459 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
9460 return nfs_page_array_len(0, max_resp_sz);
9461}
9462
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009463static void nfs4_layoutget_release(void *calldata)
9464{
9465 struct nfs4_layoutget *lgp = calldata;
9466
9467 dprintk("--> %s\n", __func__);
Trond Myklebustbd171932017-06-27 17:33:38 -04009468 nfs4_sequence_free_slot(&lgp->res.seq_res);
Trond Myklebust29a8bfe2018-05-30 17:16:20 -04009469 pnfs_layoutget_free(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009470 dprintk("<-- %s\n", __func__);
9471}
9472
9473static const struct rpc_call_ops nfs4_layoutget_call_ops = {
9474 .rpc_call_prepare = nfs4_layoutget_prepare,
9475 .rpc_call_done = nfs4_layoutget_done,
9476 .rpc_release = nfs4_layoutget_release,
9477};
9478
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04009479struct pnfs_layout_segment *
Fred Isamandacb4522016-09-19 17:47:09 -04009480nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009481{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05009482 struct inode *inode = lgp->args.inode;
9483 struct nfs_server *server = NFS_SERVER(inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009484 struct rpc_task *task;
9485 struct rpc_message msg = {
9486 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
9487 .rpc_argp = &lgp->args,
9488 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04009489 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009490 };
9491 struct rpc_task_setup task_setup_data = {
9492 .rpc_client = server->client,
9493 .rpc_message = &msg,
9494 .callback_ops = &nfs4_layoutget_call_ops,
9495 .callback_data = lgp,
Trond Myklebust63ec2b62020-02-07 19:40:14 -05009496 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009497 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04009498 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04009499 struct nfs4_exception exception = {
9500 .inode = inode,
9501 .timeout = *timeout,
9502 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009503 int status = 0;
9504
9505 dprintk("--> %s\n", __func__);
9506
Peng Tao4bd5a982014-11-17 11:05:17 +08009507 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
9508 pnfs_get_layout_hdr(NFS_I(inode)->layout);
9509
Anna Schumakerfba83f32018-05-04 16:22:50 -04009510 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05009511
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009512 task = rpc_run_task(&task_setup_data);
Xiyu Yang6e476662020-04-25 21:04:40 +08009513
Anna Schumaker820bf852017-01-11 15:01:43 -05009514 status = rpc_wait_for_completion_task(task);
Trond Myklebust2dbf8df2018-06-15 15:58:45 -04009515 if (status != 0)
9516 goto out;
9517
Trond Myklebust18c07782019-02-13 07:55:31 -05009518 if (task->tk_status < 0) {
Jeff Layton183d9e72016-05-17 12:28:47 -04009519 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
9520 *timeout = exception.timeout;
Trond Myklebust18c07782019-02-13 07:55:31 -05009521 } else if (lgp->res.layoutp->len == 0) {
9522 status = -EAGAIN;
9523 *timeout = nfs4_update_delay(&exception.timeout);
Trond Myklebust2dbf8df2018-06-15 15:58:45 -04009524 } else
9525 lseg = pnfs_layout_process(lgp);
9526out:
Trond Myklebust1037e6e2013-08-14 16:36:51 -04009527 trace_nfs4_layoutget(lgp->args.ctx,
9528 &lgp->args.range,
9529 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05009530 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04009531 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04009532
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009533 rpc_put_task(task);
9534 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04009535 if (status)
9536 return ERR_PTR(status);
9537 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009538}
9539
Benny Halevycbe82602011-05-22 19:52:37 +03009540static void
9541nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
9542{
9543 struct nfs4_layoutreturn *lrp = calldata;
9544
9545 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009546 nfs4_setup_sequence(lrp->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04009547 &lrp->args.seq_args,
9548 &lrp->res.seq_res,
9549 task);
Trond Myklebustc8bf7072018-06-15 16:31:02 -04009550 if (!pnfs_layout_is_valid(lrp->args.layout))
9551 rpc_exit(task, 0);
Benny Halevycbe82602011-05-22 19:52:37 +03009552}
9553
9554static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
9555{
9556 struct nfs4_layoutreturn *lrp = calldata;
9557 struct nfs_server *server;
9558
9559 dprintk("--> %s\n", __func__);
9560
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04009561 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03009562 return;
9563
Trond Myklebust6109bcf2019-09-20 07:23:43 -04009564 /*
9565 * Was there an RPC level error? Assume the call succeeded,
9566 * and that we need to release the layout
9567 */
9568 if (task->tk_rpc_status != 0 && RPC_WAS_SENT(task)) {
9569 lrp->res.lrs_present = 0;
9570 return;
9571 }
9572
Benny Halevycbe82602011-05-22 19:52:37 +03009573 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05009574 switch (task->tk_status) {
Trond Myklebustff905142017-11-06 15:28:08 -05009575 case -NFS4ERR_OLD_STATEID:
Trond Myklebust30cb3ee2019-09-20 07:23:45 -04009576 if (nfs4_layout_refresh_old_stateid(&lrp->args.stateid,
Trond Myklebustecf84022018-08-15 21:35:46 -04009577 &lrp->args.range,
Trond Myklebustff905142017-11-06 15:28:08 -05009578 lrp->args.inode))
9579 goto out_restart;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05009580 fallthrough;
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05009581 default:
9582 task->tk_status = 0;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05009583 fallthrough;
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05009584 case 0:
9585 break;
9586 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10009587 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05009588 break;
Trond Myklebustff905142017-11-06 15:28:08 -05009589 goto out_restart;
Benny Halevycbe82602011-05-22 19:52:37 +03009590 }
Benny Halevycbe82602011-05-22 19:52:37 +03009591 dprintk("<-- %s\n", __func__);
Trond Myklebustff905142017-11-06 15:28:08 -05009592 return;
9593out_restart:
9594 task->tk_status = 0;
9595 nfs4_sequence_free_slot(&lrp->res.seq_res);
9596 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03009597}
9598
9599static void nfs4_layoutreturn_release(void *calldata)
9600{
9601 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04009602 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03009603
9604 dprintk("--> %s\n", __func__);
Trond Myklebust2a974422016-11-20 13:13:54 -05009605 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
Trond Myklebust68f74472016-10-12 19:50:54 -04009606 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04009607 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebust4d796d72016-09-23 11:38:08 -04009608 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
9609 lrp->ld_private.ops->free(&lrp->ld_private);
Trond Myklebust2f065dd2016-12-07 12:29:26 -05009610 pnfs_put_layout_hdr(lrp->args.layout);
9611 nfs_iput_and_deactive(lrp->inode);
Trond Myklebust44ea8df2020-04-02 15:37:02 -04009612 put_cred(lrp->cred);
Benny Halevycbe82602011-05-22 19:52:37 +03009613 kfree(calldata);
9614 dprintk("<-- %s\n", __func__);
9615}
9616
9617static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
9618 .rpc_call_prepare = nfs4_layoutreturn_prepare,
9619 .rpc_call_done = nfs4_layoutreturn_done,
9620 .rpc_release = nfs4_layoutreturn_release,
9621};
9622
Peng Tao6c166052014-11-17 09:30:40 +08009623int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03009624{
9625 struct rpc_task *task;
9626 struct rpc_message msg = {
9627 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
9628 .rpc_argp = &lrp->args,
9629 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04009630 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03009631 };
9632 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04009633 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03009634 .rpc_message = &msg,
9635 .callback_ops = &nfs4_layoutreturn_call_ops,
9636 .callback_data = lrp,
9637 };
Peng Tao6c166052014-11-17 09:30:40 +08009638 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03009639
Andrew Elble99ade3c2015-12-02 09:39:51 -05009640 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
9641 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
9642 &task_setup_data.rpc_client, &msg);
9643
Benny Halevycbe82602011-05-22 19:52:37 +03009644 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05009645 if (!sync) {
9646 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
9647 if (!lrp->inode) {
9648 nfs4_layoutreturn_release(lrp);
9649 return -EAGAIN;
9650 }
9651 task_setup_data.flags |= RPC_TASK_ASYNC;
9652 }
Anna Schumakerfba83f32018-05-04 16:22:50 -04009653 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1, 0);
Benny Halevycbe82602011-05-22 19:52:37 +03009654 task = rpc_run_task(&task_setup_data);
9655 if (IS_ERR(task))
9656 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05009657 if (sync)
9658 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05009659 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03009660 dprintk("<-- %s status=%d\n", __func__, status);
9661 rpc_put_task(task);
9662 return status;
9663}
9664
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009665static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009666_nfs4_proc_getdeviceinfo(struct nfs_server *server,
9667 struct pnfs_device *pdev,
NeilBrowna52458b2018-12-03 11:30:31 +11009668 const struct cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009669{
9670 struct nfs4_getdeviceinfo_args args = {
9671 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04009672 .notify_types = NOTIFY_DEVICEID4_CHANGE |
9673 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009674 };
9675 struct nfs4_getdeviceinfo_res res = {
9676 .pdev = pdev,
9677 };
9678 struct rpc_message msg = {
9679 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
9680 .rpc_argp = &args,
9681 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009682 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009683 };
9684 int status;
9685
9686 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00009687 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04009688 if (res.notification & ~args.notify_types)
9689 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04009690 if (res.notification != args.notify_types)
9691 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04009692
Trond Myklebustcac1d3a2020-12-16 16:31:26 -05009693 trace_nfs4_getdeviceinfo(server, &pdev->dev_id, status);
9694
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009695 dprintk("<-- %s status=%d\n", __func__, status);
9696
9697 return status;
9698}
9699
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009700int nfs4_proc_getdeviceinfo(struct nfs_server *server,
9701 struct pnfs_device *pdev,
NeilBrowna52458b2018-12-03 11:30:31 +11009702 const struct cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009703{
9704 struct nfs4_exception exception = { };
9705 int err;
9706
9707 do {
9708 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009709 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009710 &exception);
9711 } while (exception.retry);
9712 return err;
9713}
9714EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
9715
Andy Adamson863a3c62011-03-23 13:27:54 +00009716static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
9717{
9718 struct nfs4_layoutcommit_data *data = calldata;
9719 struct nfs_server *server = NFS_SERVER(data->args.inode);
9720
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009721 nfs4_setup_sequence(server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04009722 &data->args.seq_args,
9723 &data->res.seq_res,
9724 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00009725}
9726
9727static void
9728nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
9729{
9730 struct nfs4_layoutcommit_data *data = calldata;
9731 struct nfs_server *server = NFS_SERVER(data->args.inode);
9732
Trond Myklebust6ba7db32012-10-22 20:07:20 -04009733 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00009734 return;
9735
9736 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009737 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
9738 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
9739 case -NFS4ERR_BADLAYOUT: /* no layout */
9740 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00009741 task->tk_status = 0;
Gustavo A. R. Silvaffb81712020-11-20 12:26:46 -06009742 break;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009743 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009744 break;
9745 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10009746 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009747 rpc_restart_call_prepare(task);
9748 return;
9749 }
9750 }
Andy Adamson863a3c62011-03-23 13:27:54 +00009751}
9752
9753static void nfs4_layoutcommit_release(void *calldata)
9754{
9755 struct nfs4_layoutcommit_data *data = calldata;
9756
Andy Adamsondb29c082011-07-30 20:52:38 -04009757 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05009758 nfs_post_op_update_inode_force_wcc(data->args.inode,
9759 data->res.fattr);
NeilBrowna52458b2018-12-03 11:30:31 +11009760 put_cred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05009761 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00009762 kfree(data);
9763}
9764
9765static const struct rpc_call_ops nfs4_layoutcommit_ops = {
9766 .rpc_call_prepare = nfs4_layoutcommit_prepare,
9767 .rpc_call_done = nfs4_layoutcommit_done,
9768 .rpc_release = nfs4_layoutcommit_release,
9769};
9770
9771int
Andy Adamsonef311532011-03-12 02:58:10 -05009772nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00009773{
9774 struct rpc_message msg = {
9775 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
9776 .rpc_argp = &data->args,
9777 .rpc_resp = &data->res,
9778 .rpc_cred = data->cred,
9779 };
9780 struct rpc_task_setup task_setup_data = {
9781 .task = &data->task,
9782 .rpc_client = NFS_CLIENT(data->args.inode),
9783 .rpc_message = &msg,
9784 .callback_ops = &nfs4_layoutcommit_ops,
9785 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00009786 };
9787 struct rpc_task *task;
9788 int status = 0;
9789
Kinglong Meeb4839eb2015-07-01 12:00:13 +08009790 dprintk("NFS: initiating layoutcommit call. sync %d "
9791 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00009792 data->args.lastbytewritten,
9793 data->args.inode->i_ino);
9794
Trond Myklebust472e2592015-02-05 16:50:30 -05009795 if (!sync) {
9796 data->inode = nfs_igrab_and_active(data->args.inode);
9797 if (data->inode == NULL) {
9798 nfs4_layoutcommit_release(data);
9799 return -EAGAIN;
9800 }
9801 task_setup_data.flags = RPC_TASK_ASYNC;
9802 }
Anna Schumakerfba83f32018-05-04 16:22:50 -04009803 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Andy Adamson863a3c62011-03-23 13:27:54 +00009804 task = rpc_run_task(&task_setup_data);
9805 if (IS_ERR(task))
9806 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05009807 if (sync)
9808 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05009809 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00009810 dprintk("%s: status %d\n", __func__, status);
9811 rpc_put_task(task);
9812 return status;
9813}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009814
Trond Myklebust302fad72019-02-18 13:32:38 -05009815/*
Andy Adamson97431202013-08-08 10:57:56 -04009816 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
9817 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
9818 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009819static int
9820_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009821 struct nfs_fsinfo *info,
9822 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009823{
9824 struct nfs41_secinfo_no_name_args args = {
9825 .style = SECINFO_STYLE_CURRENT_FH,
9826 };
9827 struct nfs4_secinfo_res res = {
9828 .flavors = flavors,
9829 };
9830 struct rpc_message msg = {
9831 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
9832 .rpc_argp = &args,
9833 .rpc_resp = &res,
9834 };
Anna Schumakercc15e242019-08-14 16:22:31 -04009835 struct nfs4_call_sync_data data = {
9836 .seq_server = server,
9837 .seq_args = &args.seq_args,
9838 .seq_res = &res.seq_res,
9839 };
9840 struct rpc_task_setup task_setup = {
9841 .rpc_client = server->client,
9842 .rpc_message = &msg,
9843 .callback_ops = server->nfs_client->cl_mvops->call_sync_ops,
9844 .callback_data = &data,
9845 .flags = RPC_TASK_NO_ROUND_ROBIN,
9846 };
NeilBrowna52458b2018-12-03 11:30:31 +11009847 const struct cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009848 int status;
9849
9850 if (use_integrity) {
Colin Ian King48bb6ec2020-06-10 00:22:57 +01009851 task_setup.rpc_client = server->nfs_client->cl_rpcclient;
Anna Schumakercc15e242019-08-14 16:22:31 -04009852
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04009853 cred = nfs4_get_clid_cred(server->nfs_client);
9854 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009855 }
9856
9857 dprintk("--> %s\n", __func__);
Anna Schumakercc15e242019-08-14 16:22:31 -04009858 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
9859 status = nfs4_call_sync_custom(&task_setup);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009860 dprintk("<-- %s status=%d\n", __func__, status);
9861
NeilBrowna52458b2018-12-03 11:30:31 +11009862 put_cred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009863
9864 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009865}
9866
9867static int
9868nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9869 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
9870{
Trond Myklebust0688e642019-04-07 13:59:09 -04009871 struct nfs4_exception exception = {
9872 .interruptible = true,
9873 };
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009874 int err;
9875 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009876 /* first try using integrity protection */
9877 err = -NFS4ERR_WRONGSEC;
9878
9879 /* try to use integrity protection with machine cred */
9880 if (_nfs4_is_integrity_protected(server->nfs_client))
9881 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9882 flavors, true);
9883
9884 /*
9885 * if unable to use integrity protection, or SECINFO with
9886 * integrity protection returns NFS4ERR_WRONGSEC (which is
9887 * disallowed by spec, but exists in deployed servers) use
9888 * the current filesystem's rpc_client and the user cred.
9889 */
9890 if (err == -NFS4ERR_WRONGSEC)
9891 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9892 flavors, false);
9893
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009894 switch (err) {
9895 case 0:
9896 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05009897 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009898 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009899 default:
9900 err = nfs4_handle_exception(server, err, &exception);
9901 }
9902 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009903out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009904 return err;
9905}
9906
9907static int
9908nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
9909 struct nfs_fsinfo *info)
9910{
9911 int err;
9912 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04009913 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009914 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009915 struct nfs4_secinfo4 *secinfo;
9916 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009917
9918 page = alloc_page(GFP_KERNEL);
9919 if (!page) {
9920 err = -ENOMEM;
9921 goto out;
9922 }
9923
9924 flavors = page_address(page);
9925 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
9926
9927 /*
9928 * Fall back on "guess and check" method if
9929 * the server doesn't support SECINFO_NO_NAME
9930 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05009931 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009932 err = nfs4_find_root_sec(server, fhandle, info);
9933 goto out_freepage;
9934 }
9935 if (err)
9936 goto out_freepage;
9937
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009938 for (i = 0; i < flavors->num_flavors; i++) {
9939 secinfo = &flavors->flavors[i];
9940
9941 switch (secinfo->flavor) {
9942 case RPC_AUTH_NULL:
9943 case RPC_AUTH_UNIX:
9944 case RPC_AUTH_GSS:
9945 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
9946 &secinfo->flavor_info);
9947 break;
9948 default:
9949 flavor = RPC_AUTH_MAXFLAVOR;
9950 break;
9951 }
9952
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04009953 if (!nfs_auth_info_match(&server->auth_info, flavor))
9954 flavor = RPC_AUTH_MAXFLAVOR;
9955
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009956 if (flavor != RPC_AUTH_MAXFLAVOR) {
9957 err = nfs4_lookup_root_sec(server, fhandle,
9958 info, flavor);
9959 if (!err)
9960 break;
9961 }
9962 }
9963
9964 if (flavor == RPC_AUTH_MAXFLAVOR)
9965 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009966
9967out_freepage:
9968 put_page(page);
9969 if (err == -EACCES)
9970 return -EPERM;
9971out:
9972 return err;
9973}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009974
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009975static int _nfs41_test_stateid(struct nfs_server *server,
9976 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11009977 const struct cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009978{
9979 int status;
9980 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009981 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009982 };
9983 struct nfs41_test_stateid_res res;
9984 struct rpc_message msg = {
9985 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
9986 .rpc_argp = &args,
9987 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009988 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009989 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009990 struct rpc_clnt *rpc_client = server->client;
9991
9992 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9993 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009994
Chuck Lever38527b12012-07-11 16:30:23 -04009995 dprintk("NFS call test_stateid %p\n", stateid);
Anna Schumakerfba83f32018-05-04 16:22:50 -04009996 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009997 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009998 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04009999 if (status != NFS_OK) {
10000 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -040010001 return status;
Chuck Lever38527b12012-07-11 16:30:23 -040010002 }
10003 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -040010004 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -040010005}
10006
Trond Myklebust43912bb2016-09-22 13:38:56 -040010007static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
10008 int err, struct nfs4_exception *exception)
10009{
10010 exception->retry = 0;
10011 switch(err) {
10012 case -NFS4ERR_DELAY:
Trond Myklebust76e8a1b2016-09-22 13:39:19 -040010013 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust43912bb2016-09-22 13:38:56 -040010014 nfs4_handle_exception(server, err, exception);
10015 break;
10016 case -NFS4ERR_BADSESSION:
10017 case -NFS4ERR_BADSLOT:
10018 case -NFS4ERR_BAD_HIGH_SLOT:
10019 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
10020 case -NFS4ERR_DEADSESSION:
10021 nfs4_do_handle_exception(server, err, exception);
10022 }
10023}
10024
Chuck Lever38527b12012-07-11 16:30:23 -040010025/**
10026 * nfs41_test_stateid - perform a TEST_STATEID operation
10027 *
10028 * @server: server / transport on which to perform the operation
10029 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -040010030 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -040010031 *
10032 * Returns NFS_OK if the server recognizes that "stateid" is valid.
10033 * Otherwise a negative NFS4ERR value is returned if the operation
10034 * failed or the state ID is not currently valid.
10035 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -040010036static int nfs41_test_stateid(struct nfs_server *server,
10037 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +110010038 const struct cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -040010039{
Trond Myklebust0688e642019-04-07 13:59:09 -040010040 struct nfs4_exception exception = {
10041 .interruptible = true,
10042 };
Bryan Schumaker7d974792011-06-02 14:59:08 -040010043 int err;
10044 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -040010045 err = _nfs41_test_stateid(server, stateid, cred);
Trond Myklebust43912bb2016-09-22 13:38:56 -040010046 nfs4_handle_delay_or_session_error(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -040010047 } while (exception.retry);
10048 return err;
10049}
Bryan Schumaker9aeda352011-06-02 14:59:09 -040010050
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010051struct nfs_free_stateid_data {
10052 struct nfs_server *server;
10053 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -040010054 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010055};
10056
10057static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
10058{
10059 struct nfs_free_stateid_data *data = calldata;
Anna Schumaker7981c8a2017-01-10 11:39:53 -050010060 nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010061 &data->args.seq_args,
10062 &data->res.seq_res,
10063 task);
10064}
10065
10066static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
10067{
10068 struct nfs_free_stateid_data *data = calldata;
10069
10070 nfs41_sequence_done(task, &data->res.seq_res);
10071
10072 switch (task->tk_status) {
10073 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +100010074 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010075 rpc_restart_call_prepare(task);
10076 }
10077}
10078
10079static void nfs41_free_stateid_release(void *calldata)
10080{
10081 kfree(calldata);
10082}
10083
Trond Myklebust17f26b12013-08-21 15:48:42 -040010084static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010085 .rpc_call_prepare = nfs41_free_stateid_prepare,
10086 .rpc_call_done = nfs41_free_stateid_done,
10087 .rpc_release = nfs41_free_stateid_release,
10088};
10089
Anna Schumaker2f261022018-05-15 13:03:39 -040010090/**
10091 * nfs41_free_stateid - perform a FREE_STATEID operation
10092 *
10093 * @server: server / transport on which to perform the operation
10094 * @stateid: state ID to release
10095 * @cred: credential
Trond Myklebust302fad72019-02-18 13:32:38 -050010096 * @privileged: set to true if this call needs to be privileged
Anna Schumaker2f261022018-05-15 13:03:39 -040010097 *
10098 * Note: this function is always asynchronous.
10099 */
10100static int nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -040010101 const nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +110010102 const struct cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010103 bool privileged)
10104{
Bryan Schumaker9aeda352011-06-02 14:59:09 -040010105 struct rpc_message msg = {
10106 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -040010107 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -040010108 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010109 struct rpc_task_setup task_setup = {
10110 .rpc_client = server->client,
10111 .rpc_message = &msg,
10112 .callback_ops = &nfs41_free_stateid_ops,
10113 .flags = RPC_TASK_ASYNC,
10114 };
10115 struct nfs_free_stateid_data *data;
Anna Schumaker2f261022018-05-15 13:03:39 -040010116 struct rpc_task *task;
Bryan Schumaker9aeda352011-06-02 14:59:09 -040010117
Weston Andros Adamson3787d502013-08-13 16:37:36 -040010118 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
10119 &task_setup.rpc_client, &msg);
10120
Chuck Lever38527b12012-07-11 16:30:23 -040010121 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010122 data = kmalloc(sizeof(*data), GFP_NOFS);
10123 if (!data)
Anna Schumaker2f261022018-05-15 13:03:39 -040010124 return -ENOMEM;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010125 data->server = server;
10126 nfs4_stateid_copy(&data->args.stateid, stateid);
10127
10128 task_setup.callback_data = data;
10129
10130 msg.rpc_argp = &data->args;
10131 msg.rpc_resp = &data->res;
Anna Schumakerfba83f32018-05-04 16:22:50 -040010132 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, privileged);
Anna Schumaker2f261022018-05-15 13:03:39 -040010133 task = rpc_run_task(&task_setup);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010134 if (IS_ERR(task))
10135 return PTR_ERR(task);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010136 rpc_put_task(task);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -040010137 return 0;
Bryan Schumaker9aeda352011-06-02 14:59:09 -040010138}
Trond Myklebust36281ca2012-03-04 18:13:56 -050010139
Jeff Laytonf1cdae82014-05-01 06:28:47 -040010140static void
10141nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -040010142{
NeilBrowna52458b2018-12-03 11:30:31 +110010143 const struct cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -040010144
Trond Myklebustf0b0bf82016-09-22 13:39:04 -040010145 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -040010146 nfs4_free_lock_state(server, lsp);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -040010147}
10148
Trond Myklebust36281ca2012-03-04 18:13:56 -050010149static bool nfs41_match_stateid(const nfs4_stateid *s1,
10150 const nfs4_stateid *s2)
10151{
Trond Myklebust93b717f2016-05-16 17:42:43 -040010152 if (s1->type != s2->type)
10153 return false;
10154
Trond Myklebust2d2f24a2012-03-04 18:13:57 -050010155 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -050010156 return false;
10157
Trond Myklebust2d2f24a2012-03-04 18:13:57 -050010158 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -050010159 return true;
Trond Myklebust36281ca2012-03-04 18:13:56 -050010160
Anna Schumaker045c5512017-01-11 16:59:48 -050010161 return s1->seqid == 0 || s2->seqid == 0;
Trond Myklebust36281ca2012-03-04 18:13:56 -050010162}
10163
Andy Adamson557134a2009-04-01 09:21:53 -040010164#endif /* CONFIG_NFS_V4_1 */
10165
Trond Myklebust36281ca2012-03-04 18:13:56 -050010166static bool nfs4_match_stateid(const nfs4_stateid *s1,
10167 const nfs4_stateid *s2)
10168{
Trond Myklebustf597c532012-03-04 18:13:56 -050010169 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -050010170}
10171
10172
Trond Myklebust17280172012-03-11 13:11:00 -040010173static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -050010174 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -050010175 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010176 .recover_open = nfs4_open_reclaim,
10177 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -040010178 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -040010179 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010180};
10181
Andy Adamson591d71c2009-04-01 09:22:47 -040010182#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -040010183static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -040010184 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
10185 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
10186 .recover_open = nfs4_open_reclaim,
10187 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -050010188 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -050010189 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -040010190 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -040010191};
10192#endif /* CONFIG_NFS_V4_1 */
10193
Trond Myklebust17280172012-03-11 13:11:00 -040010194static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -050010195 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -050010196 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +030010197 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010198 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -040010199 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010200};
10201
Andy Adamson591d71c2009-04-01 09:22:47 -040010202#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -040010203static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -040010204 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
10205 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -040010206 .recover_open = nfs41_open_expired,
10207 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -050010208 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -040010209};
10210#endif /* CONFIG_NFS_V4_1 */
10211
Trond Myklebust17280172012-03-11 13:11:00 -040010212static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -040010213 .sched_state_renewal = nfs4_proc_async_renew,
NeilBrownf15e1e82018-12-03 11:30:30 +110010214 .get_state_renewal_cred = nfs4_get_renew_cred,
Benny Halevy8e69514f2009-04-01 09:22:45 -040010215 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -040010216};
10217
10218#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -040010219static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -040010220 .sched_state_renewal = nfs41_proc_async_sequence,
NeilBrownf15e1e82018-12-03 11:30:30 +110010221 .get_state_renewal_cred = nfs4_get_machine_cred,
Benny Halevy8e69514f2009-04-01 09:22:45 -040010222 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -040010223};
10224#endif
10225
Chuck Leverec011fe2013-10-17 14:12:39 -040010226static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -040010227 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -040010228 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -040010229};
10230
10231#if defined(CONFIG_NFS_V4_1)
10232static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -040010233 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -040010234 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -040010235};
10236#endif /* CONFIG_NFS_V4_1 */
10237
Trond Myklebust97dc1352010-06-16 09:52:26 -040010238static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
10239 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -040010240 .init_caps = NFS_CAP_READDIRPLUS
10241 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -040010242 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -040010243 .init_client = nfs40_init_client,
10244 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -050010245 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -040010246 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -040010247 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust45870d62016-09-22 13:38:59 -040010248 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -050010249 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -040010250 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -040010251 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
10252 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
10253 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -040010254 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -040010255};
10256
10257#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -050010258static struct nfs_seqid *
10259nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
10260{
10261 return NULL;
10262}
10263
Trond Myklebust97dc1352010-06-16 09:52:26 -040010264static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
10265 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -040010266 .init_caps = NFS_CAP_READDIRPLUS
10267 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -040010268 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -040010269 | NFS_CAP_STATEID_NFSV41
Fred Isaman6e012602016-10-04 15:26:41 -040010270 | NFS_CAP_ATOMIC_OPEN_V1
10271 | NFS_CAP_LGOPEN,
Chuck Leverabf79bb2013-08-09 12:49:11 -040010272 .init_client = nfs41_init_client,
10273 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -050010274 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -040010275 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -040010276 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust45870d62016-09-22 13:38:59 -040010277 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -050010278 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -040010279 .session_trunk = nfs4_test_session_trunk,
Chuck Lever9915ea72013-08-09 12:48:27 -040010280 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -040010281 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
10282 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
10283 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -040010284 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -040010285};
10286#endif
10287
Steve Dickson42c2c422013-05-22 12:50:38 -040010288#if defined(CONFIG_NFS_V4_2)
10289static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
10290 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -040010291 .init_caps = NFS_CAP_READDIRPLUS
10292 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -040010293 | NFS_CAP_POSIX_LOCK
10294 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -040010295 | NFS_CAP_ATOMIC_OPEN_V1
Fred Isaman6e012602016-10-04 15:26:41 -040010296 | NFS_CAP_LGOPEN
Anna Schumakerf4ac1672014-11-25 13:18:15 -050010297 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -040010298 | NFS_CAP_COPY
Olga Kornievskaiacb95dee2018-07-09 15:13:29 -040010299 | NFS_CAP_OFFLOAD_CANCEL
Olga Kornievskaia04915672019-06-04 16:14:30 -040010300 | NFS_CAP_COPY_NOTIFY
Anna Schumaker624bd5b2014-11-25 13:18:16 -050010301 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -040010302 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +080010303 | NFS_CAP_LAYOUTSTATS
Trond Myklebust3eb86092019-02-08 10:31:05 -050010304 | NFS_CAP_CLONE
Anna Schumakerc5675522014-05-28 13:41:22 -040010305 | NFS_CAP_LAYOUTERROR
10306 | NFS_CAP_READ_PLUS,
Chuck Leverabf79bb2013-08-09 12:49:11 -040010307 .init_client = nfs41_init_client,
10308 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -040010309 .match_stateid = nfs41_match_stateid,
10310 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -040010311 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -040010312 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust45870d62016-09-22 13:38:59 -040010313 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -050010314 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -040010315 .session_trunk = nfs4_test_session_trunk,
Steve Dickson42c2c422013-05-22 12:50:38 -040010316 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
10317 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
10318 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +080010319 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -040010320};
10321#endif
10322
Trond Myklebust97dc1352010-06-16 09:52:26 -040010323const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
10324 [0] = &nfs_v4_0_minor_ops,
10325#if defined(CONFIG_NFS_V4_1)
10326 [1] = &nfs_v4_1_minor_ops,
10327#endif
Steve Dickson42c2c422013-05-22 12:50:38 -040010328#if defined(CONFIG_NFS_V4_2)
10329 [2] = &nfs_v4_2_minor_ops,
10330#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -040010331};
10332
Trond Myklebust13997822016-07-24 17:10:52 -040010333static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +010010334{
Frank van der Linden012a2112020-06-23 22:39:03 +000010335 ssize_t error, error2, error3;
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +010010336
10337 error = generic_listxattr(dentry, list, size);
10338 if (error < 0)
10339 return error;
10340 if (list) {
10341 list += error;
10342 size -= error;
10343 }
10344
10345 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
10346 if (error2 < 0)
10347 return error2;
Frank van der Linden012a2112020-06-23 22:39:03 +000010348
10349 if (list) {
10350 list += error2;
10351 size -= error2;
10352 }
10353
10354 error3 = nfs4_listxattr_nfs4_user(d_inode(dentry), list, size);
10355 if (error3 < 0)
10356 return error3;
10357
10358 return error + error2 + error3;
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +010010359}
10360
Trond Myklebust17f26b12013-08-21 15:48:42 -040010361static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -040010362 .create = nfs_create,
10363 .lookup = nfs_lookup,
10364 .atomic_open = nfs_atomic_open,
10365 .link = nfs_link,
10366 .unlink = nfs_unlink,
10367 .symlink = nfs_symlink,
10368 .mkdir = nfs_mkdir,
10369 .rmdir = nfs_rmdir,
10370 .mknod = nfs_mknod,
10371 .rename = nfs_rename,
10372 .permission = nfs_permission,
10373 .getattr = nfs_getattr,
10374 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +010010375 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -040010376};
10377
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -080010378static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +000010379 .permission = nfs_permission,
10380 .getattr = nfs_getattr,
10381 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +010010382 .listxattr = nfs4_listxattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +000010383};
10384
David Howells509de812006-08-22 20:06:11 -040010385const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010386 .version = 4, /* protocol version */
10387 .dentry_ops = &nfs4_dentry_operations,
10388 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +000010389 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -040010390 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010391 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -040010392 .submount = nfs4_submount,
David Howellsf2aedb72019-12-10 07:31:13 -050010393 .try_get_tree = nfs4_try_get_tree,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010394 .getattr = nfs4_proc_getattr,
10395 .setattr = nfs4_proc_setattr,
10396 .lookup = nfs4_proc_lookup,
Jeff Layton5b5faaf2017-06-29 06:34:52 -070010397 .lookupp = nfs4_proc_lookupp,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010398 .access = nfs4_proc_access,
10399 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010400 .create = nfs4_proc_create,
10401 .remove = nfs4_proc_remove,
10402 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -040010403 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010404 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -040010405 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -040010406 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -040010407 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010408 .link = nfs4_proc_link,
10409 .symlink = nfs4_proc_symlink,
10410 .mkdir = nfs4_proc_mkdir,
Trond Myklebust912678d2018-03-20 16:43:15 -040010411 .rmdir = nfs4_proc_rmdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010412 .readdir = nfs4_proc_readdir,
10413 .mknod = nfs4_proc_mknod,
10414 .statfs = nfs4_proc_statfs,
10415 .fsinfo = nfs4_proc_fsinfo,
10416 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -040010417 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010418 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -040010419 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010420 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -050010421 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010422 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -050010423 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010424 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -040010425 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -050010426 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010427 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +000010428 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -040010429 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -040010430 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -040010431 .have_delegation = nfs4_have_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -040010432 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +000010433 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -040010434 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -040010435 .create_server = nfs4_create_server,
10436 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010437};
10438
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000010439static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +010010440 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000010441 .list = nfs4_xattr_list_nfs4_acl,
10442 .get = nfs4_xattr_get_nfs4_acl,
10443 .set = nfs4_xattr_set_nfs4_acl,
10444};
10445
Frank van der Linden012a2112020-06-23 22:39:03 +000010446#ifdef CONFIG_NFS_V4_2
10447static const struct xattr_handler nfs4_xattr_nfs4_user_handler = {
10448 .prefix = XATTR_USER_PREFIX,
10449 .get = nfs4_xattr_get_nfs4_user,
10450 .set = nfs4_xattr_set_nfs4_user,
10451};
10452#endif
10453
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000010454const struct xattr_handler *nfs4_xattr_handlers[] = {
10455 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -040010456#ifdef CONFIG_NFS_V4_SECURITY_LABEL
10457 &nfs4_xattr_nfs4_label_handler,
10458#endif
Frank van der Linden012a2112020-06-23 22:39:03 +000010459#ifdef CONFIG_NFS_V4_2
10460 &nfs4_xattr_nfs4_user_handler,
10461#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000010462 NULL
10463};
10464
Linus Torvalds1da177e2005-04-16 15:20:36 -070010465/*
10466 * Local variables:
10467 * c-basic-offset: 8
10468 * End:
10469 */